t2p.c

Tue, 21 Jan 2003 18:39:55 +0000

author
eric
date
Tue, 21 Jan 2003 18:39:55 +0000
changeset 50
efbc5e0957df
parent 49
be20d7e8466f
child 62
9bd354b83e16
permissions
-rw-r--r--

output copyright message and URL before usage message

     1 /*
     2  * t2p: Create a PDF file from the contents of one or more TIFF
     3  *      bilevel image files.  The images in the resulting PDF file
     4  *      will be compressed using ITU-T T.6 (G4) fax encoding.
     5  *
     6  * Main program
     7  * $Id: t2p.c,v 1.21 2003/01/21 10:39:55 eric Exp $
     8  * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License version 2 as
    12  * published by the Free Software Foundation.  Note that permission is
    13  * not granted to redistribute this program under the terms of any
    14  * other version of the General Public License.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  *
    21  * You should have received a copy of the GNU General Public License
    22  * along with this program; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */
    26 #include <stdarg.h>
    27 #include <stdbool.h>
    28 #include <stdint.h>
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 #include <unistd.h>
    33 #include <tiffio.h>
    34 #define TIFF_REVERSE_BITS
    36 #include <panda/functions.h>
    37 #include <panda/constants.h>
    39 #include "bitblt.h"
    40 #include "semantics.h"
    41 #include "parser.tab.h"
    42 #include "t2p.h"
    45 #define MAX_INPUT_FILES 5000
    47 #define POINTS_PER_INCH 72
    49 /* page size limited by Acrobat Reader to 45 inches on a side */
    50 #define PAGE_MAX_INCHES 45
    51 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
    54 typedef struct output_file_t
    55 {
    56   struct output_file_t *next;
    57   char *name;
    58   panda_pdf *pdf;
    59 } output_file_t;
    62 int verbose;
    65 char *in_filename;
    66 TIFF *in;
    67 output_file_t *output_files;
    68 output_file_t *out;
    69 /* panda_pdf *out; */
    72 char *progname;
    75 bool close_tiff_input_file (void);
    76 bool close_pdf_output_files (void);
    79 void usage (void)
    80 {
    81   fprintf (stderr, "\n");
    82   fprintf (stderr, "t2p - Copyright 2001-2003 Eric Smith <eric@brouhaha.com>\n");
    83   fprintf (stderr, "http://www.brouhaha.com/~eric/software/t2p/\n");
    84   fprintf (stderr, "\n");
    85   fprintf (stderr, "usage:\n");
    86   fprintf (stderr, "    %s [options] -s spec\n", progname);
    87   fprintf (stderr, "    %s [options] <input.tif>... -o <output.pdf>\n", progname);
    88   fprintf (stderr, "options:\n");
    89   fprintf (stderr, "    -v   verbose\n");
    90 }
    93 /* generate fatal error message to stderr, doesn't return */
    94 void fatal (int ret, char *format, ...)
    95 {
    96   va_list ap;
    98   fprintf (stderr, "fatal error");
    99   if (format)
   100     {
   101       fprintf (stderr, ": ");
   102       va_start (ap, format);
   103       vfprintf (stderr, format, ap);
   104       va_end (ap);
   105     }
   106   else
   107     fprintf (stderr, "\n");
   108   if (ret == 1)
   109     usage ();
   110   close_tiff_input_file ();
   111   close_pdf_output_files ();
   112   exit (ret);
   113 }
   116 bool close_tiff_input_file (void)
   117 {
   118   if (in)
   119     {
   120       free (in_filename);
   121       TIFFClose (in);
   122     }
   123   in = NULL;
   124   in_filename = NULL;
   125   return (1);
   126 }
   129 bool open_tiff_input_file (char *name)
   130 {
   131   if (in)
   132     {
   133       if (strcmp (name, in_filename) == 0)
   134 	return (1);
   135       close_tiff_input_file ();
   136     }
   137   in_filename = strdup (name);
   138   if (! in_filename)
   139     {
   140       fprintf (stderr, "can't strdup input filename '%s'\n", name);
   141       return (0);
   142     }
   143   in = TIFFOpen (name, "r");
   144   if (! in)
   145     {
   146       fprintf (stderr, "can't open input file '%s'\n", name);
   147       free (in_filename);
   148       return (0);
   149     }
   150   return (1);
   151 }
   154 bool close_pdf_output_files (void)
   155 {
   156   output_file_t *o, *n;
   158   for (o = output_files; o; o = n)
   159     {
   160       n = o->next;
   161       panda_close (o->pdf);
   162       free (o->name);
   163       free (o);
   164     }
   165   out = NULL;
   166   output_files = NULL;
   167   return (1);
   168 }
   170 bool open_pdf_output_file (char *name,
   171 			   pdf_file_attributes_t *attributes)
   172 {
   173   output_file_t *o;
   175   if (out && (strcmp (name, out->name) == 0))
   176     return (1);
   177   for (o = output_files; o; o = o->next)
   178     if (strcmp (name, o->name) == 0)
   179       {
   180 	out = o;
   181 	return (1);
   182       }
   183   o = calloc (1, sizeof (output_file_t));
   184   if (! o)
   185     {
   186       fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   187       return (0);
   188    }
   190   o->name = strdup (name);
   191   if (! o->name)
   192     {
   193       fprintf (stderr, "can't strdup output filename '%s'\n", name);
   194       free (o);
   195       return (0);
   196     }
   198   o->pdf = panda_open (name, "w");
   199   if (! o->pdf)
   200     {
   201       fprintf (stderr, "can't open output file '%s'\n", name);
   202       free (o->name);
   203       free (o);
   204       return (0);
   205     }
   207   if (attributes->author)
   208     panda_setauthor (o->pdf, attributes->author);
   209   if (attributes->creator)
   210     panda_setcreator (o->pdf, attributes->creator);
   211   if (attributes->title)
   212     panda_settitle (o->pdf, attributes->title);
   213   if (attributes->subject)
   214     panda_setsubject (o->pdf, attributes->subject);
   215   if (attributes->keywords)
   216     panda_setkeywords (o->pdf, attributes->keywords);
   218   /* prepend new output file onto list */
   219   o->next = output_files;
   220   output_files = o;
   222   out = o;
   223   return (1);
   224 }
   227 void process_page_numbers (int page_index,
   228 			   int count,
   229 			   int base,
   230 			   page_label_t *page_label)
   231 {
   232 }
   235 /* frees original! */
   236 static Bitmap *resize_bitmap (Bitmap *src,
   237 			      float x_resolution,
   238 			      float y_resolution,
   239 			      input_attributes_t input_attributes)
   240 {
   241   Rect src_rect;
   242   Point dest_min;
   243   Bitmap *dest;
   245   int width_pixels = input_attributes.page_size.width * x_resolution;
   246   int height_pixels = input_attributes.page_size.height * y_resolution;
   248   src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
   249   src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
   250   src_rect.max.x = src_rect.min.x + width_pixels;
   251   src_rect.max.y = src_rect.min.y + height_pixels;
   253   dest_min.x = 0;
   254   dest_min.y = 0;
   256   dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
   257   free_bitmap (src);
   258   return (dest);
   259 }
   262 /* "in place" rotation */
   263 static void rotate_bitmap (Bitmap *src,
   264 			   input_attributes_t input_attributes)
   265 {
   266   switch (input_attributes.rotation)
   267     {
   268     case 0: break;
   269     case 90: rot_90 (src); break;
   270     case 180: rot_180 (src); break;
   271     case 270: rot_270 (src); break;
   272     default:
   273       fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
   274     }
   275 }
   278 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
   281 bool last_tiff_page (void)
   282 {
   283   return (TIFFLastDirectory (in));
   284 }
   287 bool process_page (int image,  /* range 1 .. n */
   288 		   input_attributes_t input_attributes,
   289 		   bookmark_t *bookmarks)
   290 {
   291   int result = 0;
   293   uint32_t image_length, image_width;
   294   uint32_t dest_image_length, dest_image_width;
   295 #ifdef CHECK_DEPTH
   296   uint32_t image_depth;
   297 #endif
   299   uint16_t samples_per_pixel;
   300   uint16_t bits_per_sample;
   301   uint16_t planar_config;
   303   uint16_t resolution_unit;
   304   float x_resolution, y_resolution;
   305   float dest_x_resolution, dest_y_resolution;
   307   int width_points, height_points;  /* really 1/72 inch units rather than
   308 				       points */
   310   Rect rect;
   311   Bitmap *bitmap;
   313   int row;
   315   panda_page *page;
   317   int tiff_temp_fd;
   318   char tiff_temp_fn [] = "/var/tmp/t2p-XXXXXX\0";
   319   TIFF *tiff_temp;
   321   char pagesize [26];  /* Needs to hold two ints of four characters (0..3420),
   322 			  two zeros, three spaces, two brackets, and a NULL.
   323                           Added an extra ten characters just in case. */
   325   if (! TIFFSetDirectory (in, image - 1))
   326     {
   327       fprintf (stderr, "can't find page %d of input file\n", image);
   328       goto fail;
   329     }
   330   if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   331     {
   332       fprintf (stderr, "can't get image length\n");
   333       goto fail;
   334     }
   335   if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   336     {
   337       fprintf (stderr, "can't get image width\n");
   338       goto fail;
   339     }
   341   if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
   342     {
   343       fprintf (stderr, "can't get samples per pixel\n");
   344       goto fail;
   345     }
   347 #ifdef CHECK_DEPTH
   348   if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   349     {
   350       fprintf (stderr, "can't get image depth\n");
   351       goto fail;
   352     }
   353 #endif
   355   if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   356     {
   357       fprintf (stderr, "can't get bits per sample\n");
   358       goto fail;
   359     }
   361   if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   362     planar_config = 1;
   364   if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   365     resolution_unit = 2;
   366   if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   367     x_resolution = 300;
   368   if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   369     y_resolution = 300;
   371   if (samples_per_pixel != 1)
   372     {
   373       fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
   374       goto fail;
   375     }
   377 #ifdef CHECK_DEPTH
   378   if (image_depth != 1)
   379     {
   380       fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   381       goto fail;
   382     }
   383 #endif
   385   if (bits_per_sample != 1)
   386     {
   387       fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   388       goto fail;
   389     }
   391   if (planar_config != 1)
   392     {
   393       fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   394       goto fail;
   395     }
   397   if (input_attributes.has_resolution)
   398     {
   399       x_resolution = input_attributes.x_resolution;
   400       y_resolution = input_attributes.y_resolution;
   401     }
   403   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   404     {
   405       dest_image_width  = image_length;
   406       dest_image_length = image_width;
   407       dest_x_resolution = y_resolution;
   408       dest_y_resolution = x_resolution;
   409       SWAP (int, width_points, height_points);
   410     }
   411   else
   412     {
   413       dest_image_width = image_width;
   414       dest_image_length = image_length;
   415       dest_x_resolution = x_resolution;
   416       dest_y_resolution = y_resolution;
   417     }
   419   rect.min.x = 0;
   420   rect.min.y = 0;
   421   rect.max.x = image_width;
   422   rect.max.y = image_length;
   424   bitmap = create_bitmap (& rect);
   426   if (! bitmap)
   427     {
   428       fprintf (stderr, "can't allocate bitmap\n");
   429       goto fail;
   430     }
   432   for (row = 0; row < image_length; row++)
   433     if (1 != TIFFReadScanline (in,
   434 			       bitmap->bits + row * bitmap->row_words,
   435 			       row,
   436 			       0))
   437       {
   438 	fprintf (stderr, "can't read TIFF scanline\n");
   439 	goto fail;
   440       }
   442 #ifdef TIFF_REVERSE_BITS
   443   reverse_bits ((uint8_t *) bitmap->bits,
   444 		image_length * bitmap->row_words * sizeof (word_type));
   445 #endif /* TIFF_REVERSE_BITS */
   447   if (input_attributes.has_page_size)
   448     bitmap = resize_bitmap (bitmap,
   449 			    x_resolution,
   450 			    y_resolution,
   451 			    input_attributes);
   453   rotate_bitmap (bitmap,
   454 		 input_attributes);
   456   tiff_temp_fd = mkstemp (tiff_temp_fn);
   457   if (tiff_temp_fd < 0)
   458     {
   459       fprintf (stderr, "can't create temporary TIFF file\n");
   460       goto fail;
   461     }
   463   tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
   464   if (! out)
   465     {
   466       fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
   467       goto fail;
   468     }
   470   TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect));
   471   TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect));
   472   TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
   474   TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect));
   476   TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   477   TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
   478   TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
   480   TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
   481   TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   482   TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   483   TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   485 #ifdef TIFF_REVERSE_BITS
   486   reverse_bits ((uint8_t *) bitmap->bits,
   487 		image_length * bitmap->row_words * sizeof (word_type));
   488 #endif /* TIFF_REVERSE_BITS */
   490   for (row = 0; row < rect_height (& bitmap->rect); row++)
   491     if (1 != TIFFWriteScanline (tiff_temp,
   492 				bitmap->bits + row * bitmap->row_words,
   493 				row,
   494 				0))
   495       {
   496 	fprintf (stderr, "can't write TIFF scanline\n");
   497 	goto fail;
   498       }
   500   TIFFClose (tiff_temp);
   502   width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
   503   height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
   505   free_bitmap (bitmap);
   507   if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   508     {
   509       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   510       goto fail;
   511     }
   513   sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
   515   page = panda_newpage (out->pdf, pagesize);
   516   panda_imagebox (out->pdf,
   517 		  page,
   518 		  0, /* top */
   519 		  0, /* left */
   520 		  height_points, /* bottom */
   521 		  width_points, /* right */
   522 		  tiff_temp_fn,
   523 		  panda_image_tiff);
   525   result = 1;
   527  fail:
   528   if (tiff_temp_fd)
   529     unlink (tiff_temp_fn);
   530   return (result);
   531 }
   534 void main_args (char *out_fn, int inf_count, char **in_fn)
   535 {
   536   int i, ip;
   537   input_attributes_t input_attributes;
   538   pdf_file_attributes_t output_attributes;
   540   memset (& input_attributes, 0, sizeof (input_attributes));
   541   memset (& output_attributes, 0, sizeof (output_attributes));
   543   if (! open_pdf_output_file (out_fn, & output_attributes))
   544     fatal (3, "error opening output file \"%s\"\n", out_fn);
   545   for (i = 0; i < inf_count; i++)
   546     {
   547       if (! open_tiff_input_file (in_fn [i]))
   548 	fatal (3, "error opening input file \"%s\"\n", in_fn [i]);
   549       for (ip = 1;; ip++)
   550 	{
   551 	  if (! process_page (ip, input_attributes, NULL))
   552 	    fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
   553 	  if (last_tiff_page ())
   554 	    break;
   555 	}
   556       if (verbose)
   557 	fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]);
   558       if (! close_tiff_input_file ())
   559 	fatal (3, "error closing input file \"%s\"\n", in_fn [i]);
   560     }
   561   if (! close_pdf_output_files ())
   562     fatal (3, "error closing output file \"%s\"\n", out_fn);
   563 }
   566 void main_spec (char *spec_fn)
   567 {
   568   if (! parse_spec_file (spec_fn))
   569     fatal (2, "error parsing spec file\n");
   570   if (! process_specs ())
   571     fatal (3, "error processing spec file\n");
   572 }
   575 int main (int argc, char *argv[])
   576 {
   577   char *spec_fn = NULL;
   578   char *out_fn = NULL;
   579   int inf_count = 0;
   580   char *in_fn [MAX_INPUT_FILES];
   582   progname = argv [0];
   584   panda_init ();
   586   while (--argc)
   587     {
   588       if (argv [1][0] == '-')
   589 	{
   590 	  if (strcmp (argv [1], "-v") == 0)
   591 	    verbose++;
   592 	  else if (strcmp (argv [1], "-o") == 0)
   593 	    {
   594 	      if (argc)
   595 		{
   596 		  argc--;
   597 		  argv++;
   598 		  out_fn = argv [1];
   599 		}
   600 	      else
   601 		fatal (1, "missing filename after \"-o\" option\n");
   602 	    }
   603 	  else if (strcmp (argv [1], "-s") == 0)
   604 	    {
   605 	      if (argc)
   606 		{
   607 		  argc--;
   608 		  argv++;
   609 		  spec_fn = argv [1];
   610 		}
   611 	      else
   612 		fatal (1, "missing filename after \"-s\" option\n");
   613 	    }
   614 	  else
   615 	    fatal (1, "unrecognized option \"%s\"\n", argv [1]);
   616 	}
   617       else if (inf_count < MAX_INPUT_FILES)
   618 	in_fn [inf_count++] = argv [1];
   619       else
   620 	fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES);
   621       argv++;
   622     }
   624   if (! ((! out_fn) ^ (! spec_fn)))
   625     fatal (1, "either a spec file or an output file (but not both) must be specified\n");
   627   if (out_fn && ! inf_count)
   628     fatal (1, "no input files specified\n");
   630   if (spec_fn && inf_count)
   631     fatal (1, "if spec file is provided, input files can't be specified as arguments\n");
   633   if (spec_fn)
   634     main_spec (spec_fn);
   635   else
   636     main_args (out_fn, inf_count, in_fn);
   638   close_tiff_input_file ();
   639   close_pdf_output_files ();
   640   exit (0);
   641 }