tumble.c

Tue, 22 Jan 2002 09:42:42 +0000

author
eric
date
Tue, 22 Jan 2002 09:42:42 +0000
changeset 43
b80cb5a4282a
parent 42
9c85a4cd88a3
child 44
0f78374addae
permissions
-rw-r--r--

*** empty log message ***

     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.15 2002/01/22 01:42:42 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, 0);
   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 width_points, height_points;  /* really 1/72 inch units rather than
   248 				       points */
   250   Rect rect;
   251   Bitmap *bitmap;
   253   int row;
   255   panda_page *page;
   257   int tiff_temp_fd;
   258   char tiff_temp_fn [] = "/var/tmp/tiff2pdf-XXXXXX\0";
   259   TIFF *tiff_temp;
   261   char pagesize [26];  /* Needs to hold two ints of four characters (0..3420),
   262 			  two zeros, three spaces, two brackets, and a NULL.
   263                           Added an extra ten characters just in case. */
   265   if (! TIFFSetDirectory (in, image - 1))
   266     {
   267       fprintf (stderr, "can't find page %d of input file\n", image);
   268       goto fail;
   269     }
   270   if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   271     {
   272       fprintf (stderr, "can't get image length\n");
   273       goto fail;
   274     }
   275   if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   276     {
   277       fprintf (stderr, "can't get image width\n");
   278       goto fail;
   279     }
   281   if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
   282     {
   283       fprintf (stderr, "can't get samples per pixel\n");
   284       goto fail;
   285     }
   287 #ifdef CHECK_DEPTH
   288   if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   289     {
   290       fprintf (stderr, "can't get image depth\n");
   291       goto fail;
   292     }
   293 #endif
   295   if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   296     {
   297       fprintf (stderr, "can't get bits per sample\n");
   298       goto fail;
   299     }
   301   if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   302     planar_config = 1;
   304   if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   305     resolution_unit = 2;
   306   if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   307     x_resolution = 300;
   308   if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   309     y_resolution = 300;
   311   if (samples_per_pixel != 1)
   312     {
   313       fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
   314       goto fail;
   315     }
   317 #ifdef CHECK_DEPTH
   318   if (image_depth != 1)
   319     {
   320       fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   321       goto fail;
   322     }
   323 #endif
   325   if (bits_per_sample != 1)
   326     {
   327       fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   328       goto fail;
   329     }
   331   if (planar_config != 1)
   332     {
   333       fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   334       goto fail;
   335     }
   337   if (input_attributes.has_resolution)
   338     {
   339       x_resolution = input_attributes.x_resolution;
   340       y_resolution = input_attributes.y_resolution;
   341     }
   343   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   344     {
   345       dest_image_width  = image_length;
   346       dest_image_length = image_width;
   347       dest_x_resolution = y_resolution;
   348       dest_y_resolution = x_resolution;
   349       SWAP (int, width_points, height_points);
   350     }
   351   else
   352     {
   353       dest_image_width = image_width;
   354       dest_image_length = image_length;
   355       dest_x_resolution = x_resolution;
   356       dest_y_resolution = y_resolution;
   357     }
   359   rect.min.x = 0;
   360   rect.min.y = 0;
   361   rect.max.x = image_width;
   362   rect.max.y = image_length;
   364   bitmap = create_bitmap (& rect);
   366   if (! bitmap)
   367     {
   368       fprintf (stderr, "can't allocate bitmap\n");
   369       goto fail;
   370     }
   372   for (row = 0; row < image_length; row++)
   373     if (1 != TIFFReadScanline (in,
   374 			       bitmap->bits + row * bitmap->row_words,
   375 			       row,
   376 			       0))
   377       {
   378 	fprintf (stderr, "can't read TIFF scanline\n");
   379 	goto fail;
   380       }
   382   bitmap = resize_bitmap (bitmap,
   383 			      x_resolution,
   384 			      y_resolution,
   385 			      input_attributes);
   387   rotate_bitmap (bitmap,
   388 		 input_attributes);
   390   tiff_temp_fd = mkstemp (tiff_temp_fn);
   391   if (tiff_temp_fd < 0)
   392     {
   393       fprintf (stderr, "can't create temporary TIFF file\n");
   394       goto fail;
   395     }
   397   tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
   398   if (! out)
   399     {
   400       fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
   401       goto fail;
   402     }
   404   TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect));
   405   TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect));
   406   TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
   408   TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect));
   410   TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   411   TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
   412   TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
   414   TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
   415   TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   416   TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   417   TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   419   for (row = 0; row < rect_height (& bitmap->rect); row++)
   420     if (1 != TIFFWriteScanline (tiff_temp,
   421 				bitmap->bits + row * bitmap->row_words,
   422 				row,
   423 				0))
   424       {
   425 	fprintf (stderr, "can't write TIFF scanline\n");
   426 	goto fail;
   427       }
   429   TIFFClose (tiff_temp);
   431   width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
   432   height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
   434   free_bitmap (bitmap);
   436   if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   437     {
   438       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   439       goto fail;
   440     }
   442   sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
   444   page = panda_newpage (out->pdf, pagesize);
   445   panda_imagebox (out->pdf,
   446 		  page,
   447 		  0, /* top */
   448 		  0, /* left */
   449 		  height_points, /* bottom */
   450 		  width_points, /* right */
   451 		  tiff_temp_fn,
   452 		  panda_image_tiff);
   454   result = 1;
   456  fail:
   457   if (tiff_temp_fd)
   458     unlink (tiff_temp_fn);
   459   return (result);
   460 }
   463 int main (int argc, char *argv[])
   464 {
   465   int result = 0;
   467   panda_init ();
   469   if (argc != 2)
   470     {
   471       fprintf (stderr, "usage: %s spec\n", argv [0]);
   472       result = 1;
   473       goto fail;
   474     }
   476   if (! parse_spec_file (argv [1]))
   477     {
   478       result = 2;
   479       goto fail;
   480     }
   482   if (! process_specs ())
   483     {
   484       result = 3;
   485       goto fail;
   486     }
   488  fail:
   489   close_tiff_input_file ();
   490   close_pdf_output_files ();
   491   return (result);
   492 }