w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

thumbnail.c
Go to the documentation of this file.
1 /* $Header$
2 
3  This is dvipdfm, a DVI to PDF translator.
4  Copyright (C) 1998, 1999 by Mark A. Wicks
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  The author may be contacted via the e-mail address
21 
22  mwicks@kettering.edu
23 */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <kpathsea/c-ctype.h>
28 #include "system.h"
29 #include "config.h"
30 #include "mfileio.h"
31 #include "mem.h"
32 #include "pdfobj.h"
33 #include "thumbnail.h"
34 
35 #ifdef HAVE_LIBPNG
36 #include <png.h>
37 #include "pngimage.h"
38 
39 #define TMP "/tmp"
40 
41 static char *guess_name (const char *thumb_filename)
42 {
43  /* Build path name for anticipated thumbnail image */
44  char *tmpdir, *tmpname;
45  if (!(tmpdir = getenv ("TMP")) &&
46  !(tmpdir = getenv ("TEMP")))
47  tmpdir = TMP;
48  tmpname = NEW (strlen(tmpdir)+strlen(thumb_filename)+strlen(DIR_SEP_STRING)+1,
49  char);
50  strcpy (tmpname, tmpdir);
51  if (!IS_DIR_SEP (tmpname[strlen(tmpname)-1])) {
52  strcat (tmpname, DIR_SEP_STRING);
53  }
54  strcat (tmpname, thumb_filename);
55  return tmpname;
56 }
57 
58 static char thumbnail_remove_opt = 0;
59 void thumb_remove(void)
60 {
62 }
63 
64 static unsigned char sigbytes[4];
65 
66 pdf_obj *do_thumbnail (const char *thumb_filename)
67 {
68  pdf_obj *image_stream = NULL, *image_ref = NULL;
69  int found_in_cwd = 0;
70  FILE *thumb_file;
71  char *guess_filename = NULL;
72  guess_filename = guess_name (thumb_filename);
73  if ((thumb_file = MFOPEN (thumb_filename, FOPEN_RBIN_MODE))) {
74  found_in_cwd = 1;
75  } else if (!(thumb_file = MFOPEN (guess_filename, FOPEN_RBIN_MODE))) {
76  fprintf (stderr, "\nNo thumbnail file\n");
77  return NULL;
78  }
79  if (fread (sigbytes, 1, sizeof(sigbytes), thumb_file) !=
80  sizeof(sigbytes) ||
81  (png_sig_cmp (sigbytes, 0, sizeof(sigbytes)))) {
82  fprintf (stderr, "\nThumbnail not a png file! Skipping\n");
83  return NULL;
84  }
85  rewind (thumb_file);
86 
87  if ((image_stream = start_png_image (thumb_file, NULL))) {
88  image_ref = pdf_ref_obj (image_stream);
89  pdf_release_obj (image_stream);
90  } else {
91  image_ref = NULL;
92  }
93  if (thumbnail_remove_opt && found_in_cwd)
94  remove (thumb_filename);
95  else if (thumbnail_remove_opt)
96  remove (guess_filename);
97  if (guess_filename)
98  RELEASE (guess_filename);
99  MFCLOSE (thumb_file);
100  return image_ref;
101 }
102 
103 #endif /* HAVE_LIBPNG */
104 
105 
106 
#define fread
Definition: xxstdio.h:25
#define FOPEN_RBIN_MODE
Definition: dvips.h:86
char * strcpy()
#define IS_DIR_SEP
Definition: dvidvi.c:106
char tmpdir[]
#define NULL
Definition: ftobjs.h:61
#define NEW
Definition: gdkanji.c:77
char * getenv()
int png_sig_cmp(png_const_bytep sig, size_t start, size_t num_to_check)
Definition: png.c:74
#define RELEASE(p)
Definition: mem.h:39
#define MFOPEN(name, mode)
Definition: mfileio.h:42
#define MFCLOSE(file)
Definition: mfileio.h:44
pdf_obj * pdf_ref_obj(pdf_obj *object)
Definition: pdfobj.c:960
void pdf_release_obj(pdf_obj *object)
Definition: pdfobj.c:3217
pdf_obj * start_png_image(FILE *file, char *res_name)
Definition: pngimage.c:50
#define DIR_SEP_STRING
Definition: c-pathch.h:56
#define fprintf
Definition: mendex.h:64
int remove()
Definition: pdfobj.c:63
#define FILE
Definition: t1stdio.h:34
*job_name strlen((char *) job_name) - 4)
pdf_obj * do_thumbnail(const char *thumb_filename)
Definition: thumbnail.c:66
static unsigned char sigbytes[4]
Definition: thumbnail.c:64
#define TMP
Definition: thumbnail.c:39
static char thumbnail_remove_opt
Definition: thumbnail.c:58
static char * guess_name(const char *thumb_filename)
Definition: thumbnail.c:41
void thumb_remove(void)
Definition: thumbnail.c:59