tumble.c

Wed, 02 Jan 2002 10:18:13 +0000

author
eric
date
Wed, 02 Jan 2002 10:18:13 +0000
changeset 36
a338db73c6f4
parent 32
3aac131058da
child 42
9c85a4cd88a3
permissions
-rw-r--r--

bug fixes.

     1 /*
     2  * tiff2pdf: Create a PDF file from the contents of one or more
     3  *           TIFF bilevel image files.  The images in the resulting
     4  *           PDF file will be compressed using ITU-T T.6 (G4) fax
     5  *           encoding.
     6  *
     7  * Main program
     8  * $Id: tumble.c,v 1.13 2002/01/02 02:18:13 eric Exp $
     9  * Copyright 2001 Eric Smith <eric@brouhaha.com>
    10  *
    11  * This program is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License version 2 as
    13  * published by the Free Software Foundation.  Note that permission is
    14  * not granted to redistribute this program under the terms of any
    15  * other version of the General Public License.
    16  *
    17  * This program is distributed in the hope that it will be useful,
    18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    20  * GNU General Public License for more details.
    21  *
    22  * You should have received a copy of the GNU General Public License
    23  * along with this program; if not, write to the Free Software
    24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111  USA
    25  */
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <unistd.h>
    31 #include <tiffio.h>
    32 #include <panda/functions.h>
    33 #include <panda/constants.h>
    35 #include "type.h"
    36 #include "bitblt.h"
    37 #include "semantics.h"
    38 #include "parser.tab.h"
    39 #include "tiff2pdf.h"
    42 #define POINTS_PER_INCH 72
    44 /* page size limited by Acrobat Reader to 45 inches on a side */
    45 #define PAGE_MAX_INCHES 45
    46 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
    49 typedef struct output_file_t
    50 {
    51   struct output_file_t *next;
    52   char *name;
    53   panda_pdf *pdf;
    54 } output_file_t;
    57 char *in_filename;
    58 TIFF *in;
    59 output_file_t *output_files;
    60 output_file_t *out;
    61 /* panda_pdf *out; */
    64 boolean close_tiff_input_file (void)
    65 {
    66   if (in)
    67     {
    68       free (in_filename);
    69       TIFFClose (in);
    70     }
    71   in = NULL;
    72   in_filename = NULL;
    73   return (1);
    74 }
    76 boolean open_tiff_input_file (char *name)
    77 {
    78   if (in)
    79     {
    80       if (strcmp (name, in_filename) == 0)
    81 	return (1);
    82       close_tiff_input_file ();
    83     }
    84   in_filename = strdup (name);
    85   if (! in_filename)
    86     {
    87       fprintf (stderr, "can't strdup input filename '%s'\n", name);
    88       return (0);
    89     }
    90   in = TIFFOpen (name, "r");
    91   if (! in)
    92     {
    93       fprintf (stderr, "can't open input file '%s'\n", name);
    94       free (in_filename);
    95       return (0);
    96     }
    97   return (1);
    98 }
   101 boolean close_pdf_output_files (void)
   102 {
   103   output_file_t *o, *n;
   105   for (o = output_files; o; o = n)
   106     {
   107       n = o->next;
   108       panda_close (o->pdf);
   109       free (o->name);
   110       free (o);
   111     }
   112   out = NULL;
   113   output_files = NULL;
   114   return (1);
   115 }
   117 boolean open_pdf_output_file (char *name,
   118 			      pdf_file_attributes_t *attributes)
   119 {
   120   output_file_t *o;
   122   if (out && (strcmp (name, out->name) == 0))
   123     return (1);
   124   for (o = output_files; o; o = o->next)
   125     if (strcmp (name, o->name) == 0)
   126       {
   127 	out = o;
   128 	return (1);
   129       }
   130   o = calloc (1, sizeof (output_file_t));
   131   if (! o)
   132     {
   133       fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   134       return (0);
   135    }
   137   o->name = strdup (name);
   138   if (! o->name)
   139     {
   140       fprintf (stderr, "can't strdup output filename '%s'\n", name);
   141       free (o);
   142       return (0);
   143     }
   145   o->pdf = panda_open (name, "w");
   146   if (! o->pdf)
   147     {
   148       fprintf (stderr, "can't open output file '%s'\n", name);
   149       free (o->name);
   150       free (o);
   151       return (0);
   152     }
   154   if (attributes->author)
   155     panda_setauthor (o->pdf, attributes->author);
   156   if (attributes->creator)
   157     panda_setcreator (o->pdf, attributes->creator);
   158   if (attributes->title)
   159     panda_settitle (o->pdf, attributes->title);
   160   if (attributes->subject)
   161     panda_setsubject (o->pdf, attributes->subject);
   162   if (attributes->keywords)
   163     panda_setkeywords (o->pdf, attributes->keywords);
   165   /* prepend new output file onto list */
   166   o->next = output_files;
   167   output_files = o;
   169   out = o;
   170   return (1);
   171 }
   174 void process_page_numbers (int page_index,
   175 			   int count,
   176 			   int base,
   177 			   page_label_t *page_label)
   178 {
   179 }
   182 static Bitmap *rotate_bitmap (Bitmap *src,
   183 			      float x_resolution,
   184 			      float y_resolution,
   185 			      input_attributes_t input_attributes)
   186 {
   187   Rect src_rect;
   188   Point dest_upper_left;
   189   int scan;
   191   if (input_attributes.has_page_size)
   192     {
   193       int width_pixels = input_attributes.page_size.width * x_resolution;
   194       int height_pixels = input_attributes.page_size.height * y_resolution;
   196       src_rect.upper_left.x = (src->width - width_pixels) / 2;
   197       src_rect.upper_left.y = (src->height - height_pixels) / 2;
   198       src_rect.lower_right.x = src_rect.upper_left.x + width_pixels;
   199       src_rect.lower_right.y = src_rect.upper_left.y + height_pixels;
   200     }
   201   else
   202     {
   203       src_rect.upper_left.x = 0;
   204       src_rect.upper_left.y = 0;
   205       src_rect.lower_right.x = src->width;
   206       src_rect.lower_right.y = src->height;
   207     }
   209   dest_upper_left.x = 0;
   210   dest_upper_left.y = 0;
   212   switch (input_attributes.rotation)
   213     {
   214     case 0: scan = ROT_0; break;
   215     case 90: scan = ROT_90; break;
   216     case 180: scan = ROT_180; break;
   217     case 270: scan = ROT_270; break;
   218     default:
   219       fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
   220       return (NULL);
   221     }
   223   return (bitblt (src,
   224 		  src_rect,
   225 		  NULL,  /* dest_bitmap */
   226 		  dest_upper_left,
   227 		  scan,
   228 		  TF_SRC));
   229 }
   232 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
   234 boolean process_page (int image,  /* range 1 .. n */
   235 		      input_attributes_t input_attributes,
   236 		      bookmark_t *bookmarks)
   237 {
   238   int result = 0;
   240   u32 image_length, image_width;
   241   u32 dest_image_length, dest_image_width;
   242 #ifdef CHECK_DEPTH
   243   u32 image_depth;
   244 #endif
   246   u16 samples_per_pixel;
   247   u16 bits_per_sample;
   248   u16 planar_config;
   250   u16 resolution_unit;
   251   float x_resolution, y_resolution;
   252   float dest_x_resolution, dest_y_resolution;
   254   int scanline_size;
   256   int width_points, height_points;  /* really 1/72 inch units rather than
   257 				       points */
   259   Bitmap *src_bitmap;
   260   Bitmap *dest_bitmap;
   261   int row;
   263   panda_page *page;
   265   int tiff_temp_fd;
   266   char tiff_temp_fn [] = "/var/tmp/tiff2pdf-XXXXXX\0";
   267   TIFF *tiff_temp;
   269   char pagesize [26];  /* Needs to hold two ints of four characters (0..3420),
   270 			  two zeros, three spaces, two brackets, and a NULL.
   271                           Added an extra ten characters just in case. */
   273   if (! TIFFSetDirectory (in, image - 1))
   274     {
   275       fprintf (stderr, "can't find page %d of input file\n", image);
   276       goto fail;
   277     }
   278   if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   279     {
   280       fprintf (stderr, "can't get image length\n");
   281       goto fail;
   282     }
   283   if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   284     {
   285       fprintf (stderr, "can't get image width\n");
   286       goto fail;
   287     }
   289   if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
   290     {
   291       fprintf (stderr, "can't get samples per pixel\n");
   292       goto fail;
   293     }
   295 #ifdef CHECK_DEPTH
   296   if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   297     {
   298       fprintf (stderr, "can't get image depth\n");
   299       goto fail;
   300     }
   301 #endif
   303   if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   304     {
   305       fprintf (stderr, "can't get bits per sample\n");
   306       goto fail;
   307     }
   309   if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   310     planar_config = 1;
   312   if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   313     resolution_unit = 2;
   314   if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   315     x_resolution = 300;
   316   if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   317     y_resolution = 300;
   319   if (samples_per_pixel != 1)
   320     {
   321       fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
   322       goto fail;
   323     }
   325 #ifdef CHECK_DEPTH
   326   if (image_depth != 1)
   327     {
   328       fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   329       goto fail;
   330     }
   331 #endif
   333   if (bits_per_sample != 1)
   334     {
   335       fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   336       goto fail;
   337     }
   339   if (planar_config != 1)
   340     {
   341       fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   342       goto fail;
   343     }
   345   if (input_attributes.has_resolution)
   346     {
   347       x_resolution = input_attributes.x_resolution;
   348       y_resolution = input_attributes.y_resolution;
   349     }
   351   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   352     {
   353       dest_image_width  = image_length;
   354       dest_image_length = image_width;
   355       dest_x_resolution = y_resolution;
   356       dest_y_resolution = x_resolution;
   357       SWAP (int, width_points, height_points);
   358     }
   359   else
   360     {
   361       dest_image_width = image_width;
   362       dest_image_length = image_length;
   363       dest_x_resolution = x_resolution;
   364       dest_y_resolution = y_resolution;
   365     }
   367   scanline_size = TIFFScanlineSize (in);
   369   src_bitmap = create_bitmap (image_width, image_length);
   370   if (! src_bitmap)
   371     {
   372       fprintf (stderr, "can't allocate bitmap\n");
   373       goto fail;
   374     }
   376   if (src_bitmap->rowbytes != scanline_size)
   377     {
   378       printf ("image_width %d\n", image_width);
   379       printf ("rowbytes %d\n", src_bitmap->rowbytes);
   380       printf ("TIFFScanlineSize %d\n", scanline_size);
   381     }
   383   for (row = 0; row < image_length; row++)
   384     TIFFReadScanline (in,
   385 		      src_bitmap->bits + row * src_bitmap->rowbytes,
   386 		      row,
   387 		      0);
   389   for (row = 0; row < dest_image_length; row++)
   390     if (1 != TIFFReadScanline (in,
   391 			       src_bitmap->bits + row * src_bitmap->rowbytes,
   392 			       row,
   393 			       0))
   394       {
   395 	fprintf (stderr, "can't read TIFF scanline\n");
   396 	goto fail;
   397       }
   399   dest_bitmap = rotate_bitmap (src_bitmap,
   400 			       x_resolution,
   401 			       y_resolution,
   402 			       input_attributes);
   403   if (! dest_bitmap)
   404     {
   405       fprintf (stderr, "can't allocate bitmap\n");
   406       goto fail;
   407     }
   409   tiff_temp_fd = mkstemp (tiff_temp_fn);
   410   if (tiff_temp_fd < 0)
   411     {
   412       fprintf (stderr, "can't create temporary TIFF file\n");
   413       goto fail;
   414     }
   416   tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
   417   if (! out)
   418     {
   419       fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
   420       goto fail;
   421     }
   423   TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, dest_bitmap->height);
   424   TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, dest_bitmap->width);
   425   TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
   427   TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, dest_bitmap->height);
   429   TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   430   TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
   431   TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
   433   TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
   434   TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   435   TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   436   TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   438   for (row = 0; row < dest_bitmap->height; row++)
   439     if (1 != TIFFWriteScanline (tiff_temp,
   440 				dest_bitmap->bits + row * dest_bitmap->rowbytes,
   441 				row,
   442 				0))
   443       {
   444 	fprintf (stderr, "can't write TIFF scanline\n");
   445 	goto fail;
   446       }
   448   TIFFClose (tiff_temp);
   450   width_points = (dest_bitmap->width / dest_x_resolution) * POINTS_PER_INCH;
   451   height_points = (dest_bitmap->height / dest_y_resolution) * POINTS_PER_INCH;
   453   free_bitmap (dest_bitmap);
   454   free_bitmap (src_bitmap);
   456   if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   457     {
   458       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   459       goto fail;
   460     }
   462   sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
   464   page = panda_newpage (out->pdf, pagesize);
   465   panda_imagebox (out->pdf,
   466 		  page,
   467 		  0, /* top */
   468 		  0, /* left */
   469 		  height_points, /* bottom */
   470 		  width_points, /* right */
   471 		  tiff_temp_fn,
   472 		  panda_image_tiff);
   474   result = 1;
   476  fail:
   477   if (tiff_temp_fd)
   478     unlink (tiff_temp_fn);
   479   return (result);
   480 }
   483 int main (int argc, char *argv[])
   484 {
   485   int result = 0;
   487   panda_init ();
   489   if (argc != 2)
   490     {
   491       fprintf (stderr, "usage: %s spec\n", argv [0]);
   492       result = 1;
   493       goto fail;
   494     }
   496   if (! parse_spec_file (argv [1]))
   497     {
   498       result = 2;
   499       goto fail;
   500     }
   502   if (! process_specs ())
   503     {
   504       result = 3;
   505       goto fail;
   506     }
   508  fail:
   509   close_tiff_input_file ();
   510   close_pdf_output_files ();
   511   return (result);
   512 }