Wed, 30 Jan 2002 08:55:53 +0000
changed program name to "t2p"
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.16 2002/01/30 00:55:34 eric Exp $
8 * Copyright 2001 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 <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <tiffio.h>
30 #include <panda/functions.h>
31 #include <panda/constants.h>
33 #include "type.h"
34 #include "bitblt.h"
35 #include "semantics.h"
36 #include "parser.tab.h"
37 #include "t2p.h"
40 #define POINTS_PER_INCH 72
42 /* page size limited by Acrobat Reader to 45 inches on a side */
43 #define PAGE_MAX_INCHES 45
44 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
47 typedef struct output_file_t
48 {
49 struct output_file_t *next;
50 char *name;
51 panda_pdf *pdf;
52 } output_file_t;
55 char *in_filename;
56 TIFF *in;
57 output_file_t *output_files;
58 output_file_t *out;
59 /* panda_pdf *out; */
62 boolean close_tiff_input_file (void)
63 {
64 if (in)
65 {
66 free (in_filename);
67 TIFFClose (in);
68 }
69 in = NULL;
70 in_filename = NULL;
71 return (1);
72 }
74 boolean open_tiff_input_file (char *name)
75 {
76 if (in)
77 {
78 if (strcmp (name, in_filename) == 0)
79 return (1);
80 close_tiff_input_file ();
81 }
82 in_filename = strdup (name);
83 if (! in_filename)
84 {
85 fprintf (stderr, "can't strdup input filename '%s'\n", name);
86 return (0);
87 }
88 in = TIFFOpen (name, "r");
89 if (! in)
90 {
91 fprintf (stderr, "can't open input file '%s'\n", name);
92 free (in_filename);
93 return (0);
94 }
95 return (1);
96 }
99 boolean close_pdf_output_files (void)
100 {
101 output_file_t *o, *n;
103 for (o = output_files; o; o = n)
104 {
105 n = o->next;
106 panda_close (o->pdf);
107 free (o->name);
108 free (o);
109 }
110 out = NULL;
111 output_files = NULL;
112 return (1);
113 }
115 boolean open_pdf_output_file (char *name,
116 pdf_file_attributes_t *attributes)
117 {
118 output_file_t *o;
120 if (out && (strcmp (name, out->name) == 0))
121 return (1);
122 for (o = output_files; o; o = o->next)
123 if (strcmp (name, o->name) == 0)
124 {
125 out = o;
126 return (1);
127 }
128 o = calloc (1, sizeof (output_file_t));
129 if (! o)
130 {
131 fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
132 return (0);
133 }
135 o->name = strdup (name);
136 if (! o->name)
137 {
138 fprintf (stderr, "can't strdup output filename '%s'\n", name);
139 free (o);
140 return (0);
141 }
143 o->pdf = panda_open (name, "w");
144 if (! o->pdf)
145 {
146 fprintf (stderr, "can't open output file '%s'\n", name);
147 free (o->name);
148 free (o);
149 return (0);
150 }
152 if (attributes->author)
153 panda_setauthor (o->pdf, attributes->author);
154 if (attributes->creator)
155 panda_setcreator (o->pdf, attributes->creator);
156 if (attributes->title)
157 panda_settitle (o->pdf, attributes->title);
158 if (attributes->subject)
159 panda_setsubject (o->pdf, attributes->subject);
160 if (attributes->keywords)
161 panda_setkeywords (o->pdf, attributes->keywords);
163 /* prepend new output file onto list */
164 o->next = output_files;
165 output_files = o;
167 out = o;
168 return (1);
169 }
172 void process_page_numbers (int page_index,
173 int count,
174 int base,
175 page_label_t *page_label)
176 {
177 }
180 /* frees original! */
181 static Bitmap *resize_bitmap (Bitmap *src,
182 float x_resolution,
183 float y_resolution,
184 input_attributes_t input_attributes)
185 {
186 Rect src_rect;
187 Point dest_min;
188 Bitmap *dest;
190 int width_pixels = input_attributes.page_size.width * x_resolution;
191 int height_pixels = input_attributes.page_size.height * y_resolution;
193 src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
194 src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
195 src_rect.max.x = src_rect.min.x + width_pixels;
196 src_rect.max.y = src_rect.min.y + height_pixels;
198 dest_min.x = 0;
199 dest_min.y = 0;
201 dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
202 free_bitmap (src);
203 return (dest);
204 }
207 /* "in place" rotation */
208 static void rotate_bitmap (Bitmap *src,
209 input_attributes_t input_attributes)
210 {
211 switch (input_attributes.rotation)
212 {
213 case 0: break;
214 case 90: rot_90 (src); break;
215 case 180: rot_180 (src); break;
216 case 270: rot_270 (src); break;
217 default:
218 fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
219 }
220 }
223 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
225 boolean process_page (int image, /* range 1 .. n */
226 input_attributes_t input_attributes,
227 bookmark_t *bookmarks)
228 {
229 int result = 0;
231 u32 image_length, image_width;
232 u32 dest_image_length, dest_image_width;
233 #ifdef CHECK_DEPTH
234 u32 image_depth;
235 #endif
237 u16 samples_per_pixel;
238 u16 bits_per_sample;
239 u16 planar_config;
241 u16 resolution_unit;
242 float x_resolution, y_resolution;
243 float dest_x_resolution, dest_y_resolution;
245 int width_points, height_points; /* really 1/72 inch units rather than
246 points */
248 Rect rect;
249 Bitmap *bitmap;
251 int row;
253 panda_page *page;
255 int tiff_temp_fd;
256 char tiff_temp_fn [] = "/var/tmp/t2p-XXXXXX\0";
257 TIFF *tiff_temp;
259 char pagesize [26]; /* Needs to hold two ints of four characters (0..3420),
260 two zeros, three spaces, two brackets, and a NULL.
261 Added an extra ten characters just in case. */
263 if (! TIFFSetDirectory (in, image - 1))
264 {
265 fprintf (stderr, "can't find page %d of input file\n", image);
266 goto fail;
267 }
268 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
269 {
270 fprintf (stderr, "can't get image length\n");
271 goto fail;
272 }
273 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
274 {
275 fprintf (stderr, "can't get image width\n");
276 goto fail;
277 }
279 if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
280 {
281 fprintf (stderr, "can't get samples per pixel\n");
282 goto fail;
283 }
285 #ifdef CHECK_DEPTH
286 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
287 {
288 fprintf (stderr, "can't get image depth\n");
289 goto fail;
290 }
291 #endif
293 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
294 {
295 fprintf (stderr, "can't get bits per sample\n");
296 goto fail;
297 }
299 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
300 planar_config = 1;
302 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
303 resolution_unit = 2;
304 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
305 x_resolution = 300;
306 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
307 y_resolution = 300;
309 if (samples_per_pixel != 1)
310 {
311 fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
312 goto fail;
313 }
315 #ifdef CHECK_DEPTH
316 if (image_depth != 1)
317 {
318 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
319 goto fail;
320 }
321 #endif
323 if (bits_per_sample != 1)
324 {
325 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
326 goto fail;
327 }
329 if (planar_config != 1)
330 {
331 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
332 goto fail;
333 }
335 if (input_attributes.has_resolution)
336 {
337 x_resolution = input_attributes.x_resolution;
338 y_resolution = input_attributes.y_resolution;
339 }
341 if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
342 {
343 dest_image_width = image_length;
344 dest_image_length = image_width;
345 dest_x_resolution = y_resolution;
346 dest_y_resolution = x_resolution;
347 SWAP (int, width_points, height_points);
348 }
349 else
350 {
351 dest_image_width = image_width;
352 dest_image_length = image_length;
353 dest_x_resolution = x_resolution;
354 dest_y_resolution = y_resolution;
355 }
357 rect.min.x = 0;
358 rect.min.y = 0;
359 rect.max.x = image_width;
360 rect.max.y = image_length;
362 bitmap = create_bitmap (& rect);
364 if (! bitmap)
365 {
366 fprintf (stderr, "can't allocate bitmap\n");
367 goto fail;
368 }
370 for (row = 0; row < image_length; row++)
371 if (1 != TIFFReadScanline (in,
372 bitmap->bits + row * bitmap->row_words,
373 row,
374 0))
375 {
376 fprintf (stderr, "can't read TIFF scanline\n");
377 goto fail;
378 }
380 bitmap = resize_bitmap (bitmap,
381 x_resolution,
382 y_resolution,
383 input_attributes);
385 rotate_bitmap (bitmap,
386 input_attributes);
388 tiff_temp_fd = mkstemp (tiff_temp_fn);
389 if (tiff_temp_fd < 0)
390 {
391 fprintf (stderr, "can't create temporary TIFF file\n");
392 goto fail;
393 }
395 tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
396 if (! out)
397 {
398 fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
399 goto fail;
400 }
402 TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect));
403 TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect));
404 TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
406 TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect));
408 TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
409 TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
410 TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
412 TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
413 TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
414 TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
415 TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
417 for (row = 0; row < rect_height (& bitmap->rect); row++)
418 if (1 != TIFFWriteScanline (tiff_temp,
419 bitmap->bits + row * bitmap->row_words,
420 row,
421 0))
422 {
423 fprintf (stderr, "can't write TIFF scanline\n");
424 goto fail;
425 }
427 TIFFClose (tiff_temp);
429 width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
430 height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
432 free_bitmap (bitmap);
434 if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
435 {
436 fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
437 goto fail;
438 }
440 sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
442 page = panda_newpage (out->pdf, pagesize);
443 panda_imagebox (out->pdf,
444 page,
445 0, /* top */
446 0, /* left */
447 height_points, /* bottom */
448 width_points, /* right */
449 tiff_temp_fn,
450 panda_image_tiff);
452 result = 1;
454 fail:
455 if (tiff_temp_fd)
456 unlink (tiff_temp_fn);
457 return (result);
458 }
461 int main (int argc, char *argv[])
462 {
463 int result = 0;
465 panda_init ();
467 if (argc != 2)
468 {
469 fprintf (stderr, "usage: %s spec\n", argv [0]);
470 result = 1;
471 goto fail;
472 }
474 if (! parse_spec_file (argv [1]))
475 {
476 result = 2;
477 goto fail;
478 }
480 if (! process_specs ())
481 {
482 result = 3;
483 goto fail;
484 }
486 fail:
487 close_tiff_input_file ();
488 close_pdf_output_files ();
489 return (result);
490 }