Back to index

tetex-bin  3.0
thumbnail.c
Go to the documentation of this file.
00001 /*  $Header$
00002 
00003     This is dvipdfm, a DVI to PDF translator.
00004     Copyright (C) 1998, 1999 by Mark A. Wicks
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019     
00020     The author may be contacted via the e-mail address
00021 
00022        mwicks@kettering.edu
00023 */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <kpathsea/config.h>
00028 #include <kpathsea/c-ctype.h>
00029 #include "system.h"
00030 #include "config.h"
00031 #include "mfileio.h"
00032 #include "mem.h"
00033 #include "pdfobj.h"
00034 #include "thumbnail.h"
00035 
00036 #ifdef HAVE_LIBPNG
00037 #include <png.h>
00038 #include "pngimage.h"
00039 
00040 #define TMP "/tmp"
00041 
00042 static char *guess_name (const char *thumb_filename)
00043 {
00044   /* Build path name for anticipated thumbnail image */
00045   char *tmpdir, *tmpname;
00046   if (!(tmpdir = getenv ("TMP")) &&
00047       !(tmpdir = getenv ("TEMP"))) 
00048     tmpdir = TMP;
00049   tmpname = NEW (strlen(tmpdir)+strlen(thumb_filename)+strlen(DIR_SEP_STRING)+1,
00050                char);
00051   strcpy (tmpname, tmpdir);
00052   if (!IS_DIR_SEP (tmpname[strlen(tmpname)-1])) {
00053     strcat (tmpname, DIR_SEP_STRING);
00054   }
00055   strcat (tmpname, thumb_filename);
00056   return tmpname;
00057 }
00058 
00059 static char thumbnail_remove_opt = 0;
00060 void thumb_remove(void)
00061 {
00062   thumbnail_remove_opt = 1;
00063 }
00064 
00065 static unsigned char sigbytes[4];
00066 
00067 pdf_obj *do_thumbnail (const char *thumb_filename) 
00068 {
00069   pdf_obj *image_stream = NULL, *image_ref = NULL;
00070   int found_in_cwd = 0;
00071   FILE *thumb_file;
00072   char *guess_filename = NULL;
00073   guess_filename = guess_name (thumb_filename);
00074   if ((thumb_file = MFOPEN (thumb_filename, FOPEN_RBIN_MODE))) {
00075     found_in_cwd = 1;
00076   } else if (!(thumb_file = MFOPEN (guess_filename, FOPEN_RBIN_MODE))) {
00077     fprintf (stderr, "\nNo thumbnail file\n");
00078     return NULL;
00079   }
00080   if (fread (sigbytes, 1, sizeof(sigbytes), thumb_file) !=
00081       sizeof(sigbytes) ||
00082       (!png_check_sig (sigbytes, sizeof(sigbytes)))) {
00083     fprintf (stderr, "\nThumbnail not a png file! Skipping\n");
00084     return NULL;
00085   }
00086   rewind (thumb_file);
00087 
00088   if ((image_stream = start_png_image (thumb_file, NULL))) {
00089     image_ref = pdf_ref_obj (image_stream);
00090     pdf_release_obj (image_stream);
00091   } else {
00092     image_ref = NULL;
00093   }
00094   if (thumbnail_remove_opt && found_in_cwd) 
00095     remove (thumb_filename);
00096   else if (thumbnail_remove_opt)
00097     remove (guess_filename);
00098   if (guess_filename)
00099     RELEASE (guess_filename);
00100   MFCLOSE (thumb_file);
00101   return image_ref;
00102 }
00103 
00104 #endif /* HAVE_LIBPNG */
00105 
00106 
00107