tumble.c

Wed, 02 Jan 2002 16:39:39 +0000

author
eric
date
Wed, 02 Jan 2002 16:39:39 +0000
changeset 42
9c85a4cd88a3
parent 36
a338db73c6f4
child 43
b80cb5a4282a
permissions
-rw-r--r--

started work on bitblt optimization - split rotate functions from bitblt.

     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.14 2002/01/02 08:39:39 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 /* frees original! */
   183 static Bitmap *resize_bitmap (Bitmap *src,
   184 			      float x_resolution,
   185 			      float y_resolution,
   186 			      input_attributes_t input_attributes)
   187 {
   188   Rect src_rect;
   189   Point dest_min;
   190   Bitmap *dest;
   192   int width_pixels = input_attributes.page_size.width * x_resolution;
   193   int height_pixels = input_attributes.page_size.height * y_resolution;
   195   src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
   196   src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
   197   src_rect.max.x = src_rect.min.x + width_pixels;
   198   src_rect.max.y = src_rect.min.y + height_pixels;
   200   dest_min.x = 0;
   201   dest_min.y = 0;
   203   dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC);
   204   free_bitmap (src);
   205   return (dest);
   206 }
   209 /* "in place" rotation */
   210 static void rotate_bitmap (Bitmap *src,
   211 			   input_attributes_t input_attributes)
   212 {
   213   switch (input_attributes.rotation)
   214     {
   215     case 0: break;
   216     case 90: rot_90 (src); break;
   217     case 180: rot_180 (src); break;
   218     case 270: rot_270 (src); break;
   219     default:
   220       fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
   221     }
   222 }
   225 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
   227 boolean process_page (int image,  /* range 1 .. n */
   228 		      input_attributes_t input_attributes,
   229 		      bookmark_t *bookmarks)
   230 {
   231   int result = 0;
   233   u32 image_length, image_width;
   234   u32 dest_image_length, dest_image_width;
   235 #ifdef CHECK_DEPTH
   236   u32 image_depth;
   237 #endif
   239   u16 samples_per_pixel;
   240   u16 bits_per_sample;
   241   u16 planar_config;
   243   u16 resolution_unit;
   244   float x_resolution, y_resolution;
   245   float dest_x_resolution, dest_y_resolution;
   247   int scanline_size;
   249   int width_points, height_points;  /* really 1/72 inch units rather than
   250 				       points */
   252   Rect rect;
   253   Bitmap *bitmap;
   255   int row;
   257   panda_page *page;
   259   int tiff_temp_fd;
   260   char tiff_temp_fn [] = "/var/tmp/tiff2pdf-XXXXXX\0";
   261   TIFF *tiff_temp;
   263   char pagesize [26];  /* Needs to hold two ints of four characters (0..3420),
   264 			  two zeros, three spaces, two brackets, and a NULL.
   265                           Added an extra ten characters just in case. */
   267   if (! TIFFSetDirectory (in, image - 1))
   268     {
   269       fprintf (stderr, "can't find page %d of input file\n", image);
   270       goto fail;
   271     }
   272   if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   273     {
   274       fprintf (stderr, "can't get image length\n");
   275       goto fail;
   276     }
   277   if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   278     {
   279       fprintf (stderr, "can't get image width\n");
   280       goto fail;
   281     }
   283   if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
   284     {
   285       fprintf (stderr, "can't get samples per pixel\n");
   286       goto fail;
   287     }
   289 #ifdef CHECK_DEPTH
   290   if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   291     {
   292       fprintf (stderr, "can't get image depth\n");
   293       goto fail;
   294     }
   295 #endif
   297   if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   298     {
   299       fprintf (stderr, "can't get bits per sample\n");
   300       goto fail;
   301     }
   303   if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   304     planar_config = 1;
   306   if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   307     resolution_unit = 2;
   308   if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   309     x_resolution = 300;
   310   if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   311     y_resolution = 300;
   313   if (samples_per_pixel != 1)
   314     {
   315       fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
   316       goto fail;
   317     }
   319 #ifdef CHECK_DEPTH
   320   if (image_depth != 1)
   321     {
   322       fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   323       goto fail;
   324     }
   325 #endif
   327   if (bits_per_sample != 1)
   328     {
   329       fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   330       goto fail;
   331     }
   333   if (planar_config != 1)
   334     {
   335       fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   336       goto fail;
   337     }
   339   if (input_attributes.has_resolution)
   340     {
   341       x_resolution = input_attributes.x_resolution;
   342       y_resolution = input_attributes.y_resolution;
   343     }
   345   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   346     {
   347       dest_image_width  = image_length;
   348       dest_image_length = image_width;
   349       dest_x_resolution = y_resolution;
   350       dest_y_resolution = x_resolution;
   351       SWAP (int, width_points, height_points);
   352     }
   353   else
   354     {
   355       dest_image_width = image_width;
   356       dest_image_length = image_length;
   357       dest_x_resolution = x_resolution;
   358       dest_y_resolution = y_resolution;
   359     }
   361   scanline_size = TIFFScanlineSize (in);
   363   rect.min.x = 0;
   364   rect.min.y = 0;
   365   rect.max.x = image_width;
   366   rect.max.y = image_length;
   368   bitmap = create_bitmap (& rect);
   370   if (! bitmap)
   371     {
   372       fprintf (stderr, "can't allocate bitmap\n");
   373       goto fail;
   374     }
   376   if (bitmap->rowbytes != scanline_size)
   377     {
   378       printf ("image_width %d\n", image_width);
   379       printf ("rowbytes %d\n", bitmap->rowbytes);
   380       printf ("TIFFScanlineSize %d\n", scanline_size);
   381     }
   383   for (row = 0; row < image_length; row++)
   384     TIFFReadScanline (in,
   385 		      bitmap->bits + row * bitmap->rowbytes,
   386 		      row,
   387 		      0);
   389   for (row = 0; row < dest_image_length; row++)
   390     if (1 != TIFFReadScanline (in,
   391 			       bitmap->bits + row * bitmap->rowbytes,
   392 			       row,
   393 			       0))
   394       {
   395 	fprintf (stderr, "can't read TIFF scanline\n");
   396 	goto fail;
   397       }
   399   bitmap = resize_bitmap (bitmap,
   400 			      x_resolution,
   401 			      y_resolution,
   402 			      input_attributes);
   404   rotate_bitmap (bitmap,
   405 		 input_attributes);
   407   tiff_temp_fd = mkstemp (tiff_temp_fn);
   408   if (tiff_temp_fd < 0)
   409     {
   410       fprintf (stderr, "can't create temporary TIFF file\n");
   411       goto fail;
   412     }
   414   tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
   415   if (! out)
   416     {
   417       fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
   418       goto fail;
   419     }
   421   TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect));
   422   TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect));
   423   TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
   425   TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect));
   427   TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   428   TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
   429   TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
   431   TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
   432   TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   433   TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   434   TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   436   for (row = 0; row < rect_height (& bitmap->rect); row++)
   437     if (1 != TIFFWriteScanline (tiff_temp,
   438 				bitmap->bits + row * bitmap->rowbytes,
   439 				row,
   440 				0))
   441       {
   442 	fprintf (stderr, "can't write TIFF scanline\n");
   443 	goto fail;
   444       }
   446   TIFFClose (tiff_temp);
   448   width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
   449   height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
   451   free_bitmap (bitmap);
   453   if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   454     {
   455       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   456       goto fail;
   457     }
   459   sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
   461   page = panda_newpage (out->pdf, pagesize);
   462   panda_imagebox (out->pdf,
   463 		  page,
   464 		  0, /* top */
   465 		  0, /* left */
   466 		  height_points, /* bottom */
   467 		  width_points, /* right */
   468 		  tiff_temp_fn,
   469 		  panda_image_tiff);
   471   result = 1;
   473  fail:
   474   if (tiff_temp_fd)
   475     unlink (tiff_temp_fn);
   476   return (result);
   477 }
   480 int main (int argc, char *argv[])
   481 {
   482   int result = 0;
   484   panda_init ();
   486   if (argc != 2)
   487     {
   488       fprintf (stderr, "usage: %s spec\n", argv [0]);
   489       result = 1;
   490       goto fail;
   491     }
   493   if (! parse_spec_file (argv [1]))
   494     {
   495       result = 2;
   496       goto fail;
   497     }
   499   if (! process_specs ())
   500     {
   501       result = 3;
   502       goto fail;
   503     }
   505  fail:
   506   close_tiff_input_file ();
   507   close_pdf_output_files ();
   508   return (result);
   509 }