tumble.c

Fri, 14 Mar 2003 08:57:40 +0000

author
eric
date
Fri, 14 Mar 2003 08:57:40 +0000
changeset 133
76c197fe2eeb
parent 131
4b8c80d77f76
child 134
313aba417199
permissions
-rw-r--r--

specify page mode when file is closed rather than when it is initially created. if USE_OUTLINES but no bookmarks, use USE_NONE instead.

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * Main program
     5  * $Id: tumble.c,v 1.35 2003/03/14 00:57:40 eric Exp $
     6  * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License version 2 as
    10  * published by the Free Software Foundation.  Note that permission is
    11  * not granted to redistribute this program under the terms of any
    12  * other version of the General Public License.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  *
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; if not, write to the Free Software
    21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    22  */
    25 #include <stdarg.h>
    26 #include <stdbool.h>
    27 #include <stdint.h>
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <string.h>
    31 #include <unistd.h>
    33 #include <tiffio.h>
    34 #define TIFF_REVERSE_BITS
    36 #include "bitblt.h"
    37 #include "semantics.h"
    38 #include "parser.tab.h"
    39 #include "tumble.h"
    40 #include "pdf.h"
    43 #define MAX_INPUT_FILES 5000
    45 #define POINTS_PER_INCH 72
    47 /* page size limited by Acrobat Reader to 45 inches on a side */
    48 #define PAGE_MAX_INCHES 45
    49 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
    52 typedef struct output_file_t
    53 {
    54   struct output_file_t *next;
    55   char *name;
    56   pdf_file_handle pdf;
    57 } output_file_t;
    60 int verbose;
    63 char *in_filename;
    64 TIFF *in;
    65 output_file_t *output_files;
    66 output_file_t *out;
    69 char *progname;
    72 bool close_tiff_input_file (void);
    73 bool close_pdf_output_files (void);
    76 void usage (void)
    77 {
    78   fprintf (stderr, "\n");
    79   fprintf (stderr, "tumble - Copyright 2001-2003 Eric Smith <eric@brouhaha.com>\n");
    80   fprintf (stderr, "http://tumble.brouhaha.com/\n");
    81   fprintf (stderr, "\n");
    82   fprintf (stderr, "usage:\n");
    83   fprintf (stderr, "    %s [options] -s spec\n", progname);
    84   fprintf (stderr, "    %s [options] <input.tif>... -o <output.pdf>\n", progname);
    85   fprintf (stderr, "options:\n");
    86   fprintf (stderr, "    -v   verbose\n");
    87   fprintf (stderr, "    -b fmt  create bookmarks\n");
    88   fprintf (stderr, "bookmark format:\n");
    89   fprintf (stderr, "    %%F  file name (sans suffix)\n");
    90   fprintf (stderr, "    %%p  page number\n");
    91 }
    94 /* generate fatal error message to stderr, doesn't return */
    95 void fatal (int ret, char *format, ...)
    96 {
    97   va_list ap;
    99   fprintf (stderr, "fatal error");
   100   if (format)
   101     {
   102       fprintf (stderr, ": ");
   103       va_start (ap, format);
   104       vfprintf (stderr, format, ap);
   105       va_end (ap);
   106     }
   107   else
   108     fprintf (stderr, "\n");
   109   if (ret == 1)
   110     usage ();
   111   close_tiff_input_file ();
   112   close_pdf_output_files ();
   113   exit (ret);
   114 }
   117 bool close_tiff_input_file (void)
   118 {
   119   if (in)
   120     {
   121       free (in_filename);
   122       TIFFClose (in);
   123     }
   124   in = NULL;
   125   in_filename = NULL;
   126   return (1);
   127 }
   130 bool open_tiff_input_file (char *name)
   131 {
   132   if (in)
   133     {
   134       if (strcmp (name, in_filename) == 0)
   135 	return (1);
   136       close_tiff_input_file ();
   137     }
   138   in_filename = strdup (name);
   139   if (! in_filename)
   140     {
   141       fprintf (stderr, "can't strdup input filename '%s'\n", name);
   142       return (0);
   143     }
   144   in = TIFFOpen (name, "r");
   145   if (! in)
   146     {
   147       fprintf (stderr, "can't open input file '%s'\n", name);
   148       free (in_filename);
   149       return (0);
   150     }
   151   return (1);
   152 }
   155 bool close_pdf_output_files (void)
   156 {
   157   output_file_t *o, *n;
   159   for (o = output_files; o; o = n)
   160     {
   161       n = o->next;
   162       pdf_close (o->pdf, PDF_PAGE_MODE_USE_OUTLINES);
   163       free (o->name);
   164       free (o);
   165     }
   166   out = NULL;
   167   output_files = NULL;
   168   return (1);
   169 }
   171 bool open_pdf_output_file (char *name,
   172 			   pdf_file_attributes_t *attributes)
   173 {
   174   output_file_t *o;
   176   if (out && (strcmp (name, out->name) == 0))
   177     return (1);
   178   for (o = output_files; o; o = o->next)
   179     if (strcmp (name, o->name) == 0)
   180       {
   181 	out = o;
   182 	return (1);
   183       }
   184   o = calloc (1, sizeof (output_file_t));
   185   if (! o)
   186     {
   187       fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   188       return (0);
   189    }
   191   o->name = strdup (name);
   192   if (! o->name)
   193     {
   194       fprintf (stderr, "can't strdup output filename '%s'\n", name);
   195       free (o);
   196       return (0);
   197     }
   199   o->pdf = pdf_create (name);
   200   if (! o->pdf)
   201     {
   202       fprintf (stderr, "can't open output file '%s'\n", name);
   203       free (o->name);
   204       free (o);
   205       return (0);
   206     }
   208   if (attributes->author)
   209     pdf_set_author (o->pdf, attributes->author);
   210   if (attributes->creator)
   211     pdf_set_creator (o->pdf, attributes->creator);
   212   if (attributes->title)
   213     pdf_set_title (o->pdf, attributes->title);
   214   if (attributes->subject)
   215     pdf_set_subject (o->pdf, attributes->subject);
   216   if (attributes->keywords)
   217     pdf_set_keywords (o->pdf, attributes->keywords);
   219   /* prepend new output file onto list */
   220   o->next = output_files;
   221   output_files = o;
   223   out = o;
   224   return (1);
   225 }
   228 /* frees original! */
   229 static Bitmap *resize_bitmap (Bitmap *src,
   230 			      double x_resolution,
   231 			      double y_resolution,
   232 			      input_attributes_t input_attributes)
   233 {
   234   Rect src_rect;
   235   Point dest_min;
   236   Bitmap *dest;
   238   int width_pixels = input_attributes.page_size.width * x_resolution;
   239   int height_pixels = input_attributes.page_size.height * y_resolution;
   241   src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
   242   src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
   243   src_rect.max.x = src_rect.min.x + width_pixels;
   244   src_rect.max.y = src_rect.min.y + height_pixels;
   246   dest_min.x = 0;
   247   dest_min.y = 0;
   249   dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
   250   free_bitmap (src);
   251   return (dest);
   252 }
   255 /* "in place" rotation */
   256 static void rotate_bitmap (Bitmap *src,
   257 			   input_attributes_t input_attributes)
   258 {
   259   switch (input_attributes.rotation)
   260     {
   261     case 0: break;
   262     case 90: rot_90 (src); break;
   263     case 180: rot_180 (src); break;
   264     case 270: rot_270 (src); break;
   265     default:
   266       fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
   267     }
   268 }
   271 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
   274 bool last_tiff_page (void)
   275 {
   276   return (TIFFLastDirectory (in));
   277 }
   280 static pdf_page_handle process_tiff_page (int image,  /* range 1 .. n */
   281 					  input_attributes_t input_attributes)
   282 {
   283   uint32_t image_length, image_width;
   284   uint32_t dest_image_length, dest_image_width;
   285 #ifdef CHECK_DEPTH
   286   uint32_t image_depth;
   287 #endif
   289   uint16_t samples_per_pixel;
   290   uint16_t bits_per_sample;
   291   uint16_t planar_config;
   293   uint16_t resolution_unit;
   294   float x_resolution, y_resolution;
   295   double dest_x_resolution, dest_y_resolution;
   297   double width_points, height_points;  /* really 1/72 inch units rather than
   298 					  points */
   300   Rect rect;
   301   Bitmap *bitmap = NULL;
   303   int row;
   305   pdf_page_handle page = NULL;
   307   if (! TIFFSetDirectory (in, image - 1))
   308     {
   309       fprintf (stderr, "can't find page %d of input file\n", image);
   310       goto fail;
   311     }
   312   if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   313     {
   314       fprintf (stderr, "can't get image length\n");
   315       goto fail;
   316     }
   317   if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   318     {
   319       fprintf (stderr, "can't get image width\n");
   320       goto fail;
   321     }
   323   if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
   324     {
   325       fprintf (stderr, "can't get samples per pixel\n");
   326       goto fail;
   327     }
   329 #ifdef CHECK_DEPTH
   330   if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   331     {
   332       fprintf (stderr, "can't get image depth\n");
   333       goto fail;
   334     }
   335 #endif
   337   if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   338     {
   339       fprintf (stderr, "can't get bits per sample\n");
   340       goto fail;
   341     }
   343   if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   344     planar_config = 1;
   346   if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   347     resolution_unit = 2;
   348   if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   349     x_resolution = 300;
   350   if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   351     y_resolution = 300;
   353   if (samples_per_pixel != 1)
   354     {
   355       fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
   356       goto fail;
   357     }
   359 #ifdef CHECK_DEPTH
   360   if (image_depth != 1)
   361     {
   362       fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   363       goto fail;
   364     }
   365 #endif
   367   if (bits_per_sample != 1)
   368     {
   369       fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   370       goto fail;
   371     }
   373   if (planar_config != 1)
   374     {
   375       fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   376       goto fail;
   377     }
   379   if (input_attributes.has_resolution)
   380     {
   381       x_resolution = input_attributes.x_resolution;
   382       y_resolution = input_attributes.y_resolution;
   383     }
   385   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   386     {
   387       dest_image_width  = image_length;
   388       dest_image_length = image_width;
   389       dest_x_resolution = y_resolution;
   390       dest_y_resolution = x_resolution;
   391       SWAP (double, width_points, height_points);  /* $$$ not yet set!!! */
   392     }
   393   else
   394     {
   395       dest_image_width = image_width;
   396       dest_image_length = image_length;
   397       dest_x_resolution = x_resolution;
   398       dest_y_resolution = y_resolution;
   399     }
   401   rect.min.x = 0;
   402   rect.min.y = 0;
   403   rect.max.x = image_width;
   404   rect.max.y = image_length;
   406   bitmap = create_bitmap (& rect);
   408   if (! bitmap)
   409     {
   410       fprintf (stderr, "can't allocate bitmap\n");
   411       goto fail;
   412     }
   414   for (row = 0; row < image_length; row++)
   415     if (1 != TIFFReadScanline (in,
   416 			       bitmap->bits + row * bitmap->row_words,
   417 			       row,
   418 			       0))
   419       {
   420 	fprintf (stderr, "can't read TIFF scanline\n");
   421 	goto fail;
   422       }
   424 #ifdef TIFF_REVERSE_BITS
   425   reverse_bits ((uint8_t *) bitmap->bits,
   426 		image_length * bitmap->row_words * sizeof (word_t));
   427 #endif /* TIFF_REVERSE_BITS */
   429 #if 0
   430   if (input_attributes.has_page_size)
   431     bitmap = resize_bitmap (bitmap,
   432 			    x_resolution,
   433 			    y_resolution,
   434 			    input_attributes);
   435 #endif
   437   rotate_bitmap (bitmap,
   438 		 input_attributes);
   440   width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
   441   height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
   443   if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   444     {
   445       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   446       goto fail;
   447     }
   449   page = pdf_new_page (out->pdf, width_points, height_points);
   451 #if 0
   452   pdf_write_text (page);
   453 #else
   454   pdf_write_g4_fax_image (page,
   455 			  0, 0,  /* x, y */
   456 			  width_points, height_points,
   457 			  bitmap,
   458 			  0, /* ImageMask */
   459 			  0, 0, 0,  /* r, g, b */
   460 			  0); /* BlackIs1 */
   461 #endif
   463   if (bitmap)
   464     free_bitmap (bitmap);
   465   return (page);
   467  fail:
   468   if (bitmap)
   469     free_bitmap (bitmap);
   471   return (NULL);
   472 }
   475 #if 0
   476 pdf_page_handle process_jpeg_page (int image,  /* range 1 .. n */
   477 				   input_attributes_t input_attributes)
   478 {
   479   FILE *f;
   480   pdf_page_handle page;
   482   f = fopen (filename, "rb");
   483   if (! f)
   484     fatal ("error opening input file '%s'\n", filename);
   486   page = pdf_new_page (out->pdf, width_points, height_points);
   488   pdf_write_jpeg_image (page,
   489 			0, 0,  /* x, y */
   490 			width_points, height_points,
   491 			f);
   493   return (page);
   494 }
   495 #endif
   498 bool process_page (int image,  /* range 1 .. n */
   499 		   input_attributes_t input_attributes,
   500 		   bookmark_t *bookmarks,
   501 		   page_label_t *page_label)
   502 {
   503   pdf_page_handle page;
   505   page = process_tiff_page (image, input_attributes);
   507   while (bookmarks)
   508     {
   509       /* $$$ need to handle level here */
   510       pdf_new_bookmark (NULL, bookmarks->name, 0, page);
   511       bookmarks = bookmarks->next;
   512     }
   514   if (page_label)
   515     pdf_new_page_label (out->pdf,
   516 			page_label->page_index,
   517 			page_label->base,
   518 			page_label->count,
   519 			page_label->style,
   520 			page_label->prefix);
   522   return (page != NULL);
   523 }
   526 #define MAX_BOOKMARK_NAME_LEN 500
   529 static int filename_length_without_suffix (char *in_fn)
   530 {
   531   char *p;
   532   int len = strlen (in_fn);
   534   p = strrchr (in_fn, '.');
   535   if (p && ((strcasecmp (p, ".tif") == 0) ||
   536 	    (strcasecmp (p, ".tiff") == 0)))
   537     return (p - in_fn);
   538   return (len);
   539 }
   542 /* $$$ this function should ensure that it doesn't overflow the name string! */
   543 static void generate_bookmark_name (char *name,
   544 				    char *bookmark_fmt, 
   545 				    char *in_fn,
   546 				    int page)
   547 {
   548   bool meta = 0;
   549   int len;
   551   while (*bookmark_fmt)
   552     {
   553       if (meta)
   554 	{
   555 	  meta = 0;
   556 	  switch (*bookmark_fmt)
   557 	    {
   558 	    case '%':
   559 	      *(name++) = '%';
   560 	      break;
   561 	    case 'F':
   562 	      len = filename_length_without_suffix (in_fn);
   563 	      strncpy (name, in_fn, len);
   564 	      name += len;
   565 	      break;
   566 	    case 'p':
   567 	      sprintf (name, "%d", page);
   568 	      name += strlen (name);
   569 	      break;
   570 	    default:
   571 	      break;
   572 	    }
   573 	}
   574       else
   575 	switch (*bookmark_fmt)
   576 	  {
   577 	  case '%':
   578 	    meta = 1;
   579 	    break;
   580 	  default:
   581 	    *(name++) = *bookmark_fmt;
   582 	  }
   583       bookmark_fmt++;
   584     }
   585   *name = '\0';
   586 }
   589 void main_args (char *out_fn,
   590 		int inf_count,
   591 		char **in_fn,
   592 		char *bookmark_fmt)
   593 {
   594   int i, ip;
   595   input_attributes_t input_attributes;
   596   pdf_file_attributes_t output_attributes;
   597   bookmark_t bookmark;
   598   char bookmark_name [MAX_BOOKMARK_NAME_LEN];
   600   bookmark.next = NULL;
   601   bookmark.level = 1;
   602   bookmark.name = & bookmark_name [0];
   604   memset (& input_attributes, 0, sizeof (input_attributes));
   605   memset (& output_attributes, 0, sizeof (output_attributes));
   607   if (! open_pdf_output_file (out_fn, & output_attributes))
   608     fatal (3, "error opening output file \"%s\"\n", out_fn);
   609   for (i = 0; i < inf_count; i++)
   610     {
   611       if (! open_tiff_input_file (in_fn [i]))
   612 	fatal (3, "error opening input file \"%s\"\n", in_fn [i]);
   613       for (ip = 1;; ip++)
   614 	{
   615 	  fprintf (stderr, "processing page %d of file \"%s\"\r", ip, in_fn [i]);
   616 	  if (bookmark_fmt)
   617 	    generate_bookmark_name (& bookmark_name [0],
   618 				    bookmark_fmt, 
   619 				    in_fn [i],
   620 				    ip);
   621 	  if (! process_page (ip, input_attributes,
   622 			      bookmark_fmt ? & bookmark : NULL,
   623 			      NULL))
   624 	    fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
   625 	  if (last_tiff_page ())
   626 	    break;
   627 	}
   628       if (verbose)
   629 	fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]);
   630       if (! close_tiff_input_file ())
   631 	fatal (3, "error closing input file \"%s\"\n", in_fn [i]);
   632     }
   633   if (! close_pdf_output_files ())
   634     fatal (3, "error closing output file \"%s\"\n", out_fn);
   635 }
   638 void main_spec (char *spec_fn)
   639 {
   640   if (! parse_spec_file (spec_fn))
   641     fatal (2, "error parsing spec file\n");
   642   if (! process_specs ())
   643     fatal (3, "error processing spec file\n");
   644 }
   647 int main (int argc, char *argv[])
   648 {
   649   char *spec_fn = NULL;
   650   char *out_fn = NULL;
   651   char *bookmark_fmt = NULL;
   652   int inf_count = 0;
   653   char *in_fn [MAX_INPUT_FILES];
   655   progname = argv [0];
   657   pdf_init ();
   659   while (--argc)
   660     {
   661       if (argv [1][0] == '-')
   662 	{
   663 	  if (strcmp (argv [1], "-v") == 0)
   664 	    verbose++;
   665 	  else if (strcmp (argv [1], "-o") == 0)
   666 	    {
   667 	      if (argc)
   668 		{
   669 		  argc--;
   670 		  argv++;
   671 		  out_fn = argv [1];
   672 		}
   673 	      else
   674 		fatal (1, "missing filename after \"-o\" option\n");
   675 	    }
   676 	  else if (strcmp (argv [1], "-s") == 0)
   677 	    {
   678 	      if (argc)
   679 		{
   680 		  argc--;
   681 		  argv++;
   682 		  spec_fn = argv [1];
   683 		}
   684 	      else
   685 		fatal (1, "missing filename after \"-s\" option\n");
   686 	    }
   687 	  else if (strcmp (argv [1], "-b") == 0)
   688 	    {
   689 	      if (argc)
   690 		{
   691 		  argc--;
   692 		  argv++;
   693 		  bookmark_fmt = argv [1];
   694 		}
   695 	      else
   696 		fatal (1, "missing format string after \"-b\" option\n");
   697 	    }
   698 	  else
   699 	    fatal (1, "unrecognized option \"%s\"\n", argv [1]);
   700 	}
   701       else if (inf_count < MAX_INPUT_FILES)
   702 	in_fn [inf_count++] = argv [1];
   703       else
   704 	fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES);
   705       argv++;
   706     }
   708   if (! ((! out_fn) ^ (! spec_fn)))
   709     fatal (1, "either a spec file or an output file (but not both) must be specified\n");
   711   if (out_fn && ! inf_count)
   712     fatal (1, "no input files specified\n");
   714   if (spec_fn && inf_count)
   715     fatal (1, "if spec file is provided, input files can't be specified as arguments\n");
   717   if (spec_fn)
   718     main_spec (spec_fn);
   719   else
   720     main_args (out_fn, inf_count, in_fn, bookmark_fmt);
   722   close_tiff_input_file ();
   723   close_pdf_output_files ();
   724   exit (0);
   725 }