Mon, 26 Aug 2002 05:43:49 +0000
fixed 'middle-endian' output from TIFFReadScanline
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: tumble.c,v 1.18 2002/08/25 21:43:49 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>
30 #include <tiffio.h>
31 #define TIFF_REVERSE_BITS
33 #include <panda/functions.h>
34 #include <panda/constants.h>
36 #include "type.h"
37 #include "bitblt.h"
38 #include "semantics.h"
39 #include "parser.tab.h"
40 #include "t2p.h"
43 #define POINTS_PER_INCH 72
45 /* page size limited by Acrobat Reader to 45 inches on a side */
46 #define PAGE_MAX_INCHES 45
47 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
50 typedef struct output_file_t
51 {
52 struct output_file_t *next;
53 char *name;
54 panda_pdf *pdf;
55 } output_file_t;
58 char *in_filename;
59 TIFF *in;
60 output_file_t *output_files;
61 output_file_t *out;
62 /* panda_pdf *out; */
65 boolean close_tiff_input_file (void)
66 {
67 if (in)
68 {
69 free (in_filename);
70 TIFFClose (in);
71 }
72 in = NULL;
73 in_filename = NULL;
74 return (1);
75 }
77 boolean open_tiff_input_file (char *name)
78 {
79 if (in)
80 {
81 if (strcmp (name, in_filename) == 0)
82 return (1);
83 close_tiff_input_file ();
84 }
85 in_filename = strdup (name);
86 if (! in_filename)
87 {
88 fprintf (stderr, "can't strdup input filename '%s'\n", name);
89 return (0);
90 }
91 in = TIFFOpen (name, "r");
92 if (! in)
93 {
94 fprintf (stderr, "can't open input file '%s'\n", name);
95 free (in_filename);
96 return (0);
97 }
98 return (1);
99 }
102 boolean close_pdf_output_files (void)
103 {
104 output_file_t *o, *n;
106 for (o = output_files; o; o = n)
107 {
108 n = o->next;
109 panda_close (o->pdf);
110 free (o->name);
111 free (o);
112 }
113 out = NULL;
114 output_files = NULL;
115 return (1);
116 }
118 boolean open_pdf_output_file (char *name,
119 pdf_file_attributes_t *attributes)
120 {
121 output_file_t *o;
123 if (out && (strcmp (name, out->name) == 0))
124 return (1);
125 for (o = output_files; o; o = o->next)
126 if (strcmp (name, o->name) == 0)
127 {
128 out = o;
129 return (1);
130 }
131 o = calloc (1, sizeof (output_file_t));
132 if (! o)
133 {
134 fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
135 return (0);
136 }
138 o->name = strdup (name);
139 if (! o->name)
140 {
141 fprintf (stderr, "can't strdup output filename '%s'\n", name);
142 free (o);
143 return (0);
144 }
146 o->pdf = panda_open (name, "w");
147 if (! o->pdf)
148 {
149 fprintf (stderr, "can't open output file '%s'\n", name);
150 free (o->name);
151 free (o);
152 return (0);
153 }
155 if (attributes->author)
156 panda_setauthor (o->pdf, attributes->author);
157 if (attributes->creator)
158 panda_setcreator (o->pdf, attributes->creator);
159 if (attributes->title)
160 panda_settitle (o->pdf, attributes->title);
161 if (attributes->subject)
162 panda_setsubject (o->pdf, attributes->subject);
163 if (attributes->keywords)
164 panda_setkeywords (o->pdf, attributes->keywords);
166 /* prepend new output file onto list */
167 o->next = output_files;
168 output_files = o;
170 out = o;
171 return (1);
172 }
175 void process_page_numbers (int page_index,
176 int count,
177 int base,
178 page_label_t *page_label)
179 {
180 }
183 /* frees original! */
184 static Bitmap *resize_bitmap (Bitmap *src,
185 float x_resolution,
186 float y_resolution,
187 input_attributes_t input_attributes)
188 {
189 Rect src_rect;
190 Point dest_min;
191 Bitmap *dest;
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.min.x = (rect_width (& src->rect) - width_pixels) / 2;
197 src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
198 src_rect.max.x = src_rect.min.x + width_pixels;
199 src_rect.max.y = src_rect.min.y + height_pixels;
201 dest_min.x = 0;
202 dest_min.y = 0;
204 dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
205 free_bitmap (src);
206 return (dest);
207 }
210 /* "in place" rotation */
211 static void rotate_bitmap (Bitmap *src,
212 input_attributes_t input_attributes)
213 {
214 switch (input_attributes.rotation)
215 {
216 case 0: break;
217 case 90: rot_90 (src); break;
218 case 180: rot_180 (src); break;
219 case 270: rot_270 (src); break;
220 default:
221 fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
222 }
223 }
226 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
228 boolean process_page (int image, /* range 1 .. n */
229 input_attributes_t input_attributes,
230 bookmark_t *bookmarks)
231 {
232 int result = 0;
234 u32 image_length, image_width;
235 u32 dest_image_length, dest_image_width;
236 #ifdef CHECK_DEPTH
237 u32 image_depth;
238 #endif
240 u16 samples_per_pixel;
241 u16 bits_per_sample;
242 u16 planar_config;
244 u16 resolution_unit;
245 float x_resolution, y_resolution;
246 float dest_x_resolution, dest_y_resolution;
248 int width_points, height_points; /* really 1/72 inch units rather than
249 points */
251 Rect rect;
252 Bitmap *bitmap;
254 int row;
256 panda_page *page;
258 int tiff_temp_fd;
259 char tiff_temp_fn [] = "/var/tmp/t2p-XXXXXX\0";
260 TIFF *tiff_temp;
262 char pagesize [26]; /* Needs to hold two ints of four characters (0..3420),
263 two zeros, three spaces, two brackets, and a NULL.
264 Added an extra ten characters just in case. */
266 if (! TIFFSetDirectory (in, image - 1))
267 {
268 fprintf (stderr, "can't find page %d of input file\n", image);
269 goto fail;
270 }
271 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
272 {
273 fprintf (stderr, "can't get image length\n");
274 goto fail;
275 }
276 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
277 {
278 fprintf (stderr, "can't get image width\n");
279 goto fail;
280 }
282 if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
283 {
284 fprintf (stderr, "can't get samples per pixel\n");
285 goto fail;
286 }
288 #ifdef CHECK_DEPTH
289 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
290 {
291 fprintf (stderr, "can't get image depth\n");
292 goto fail;
293 }
294 #endif
296 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
297 {
298 fprintf (stderr, "can't get bits per sample\n");
299 goto fail;
300 }
302 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
303 planar_config = 1;
305 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
306 resolution_unit = 2;
307 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
308 x_resolution = 300;
309 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
310 y_resolution = 300;
312 if (samples_per_pixel != 1)
313 {
314 fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
315 goto fail;
316 }
318 #ifdef CHECK_DEPTH
319 if (image_depth != 1)
320 {
321 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
322 goto fail;
323 }
324 #endif
326 if (bits_per_sample != 1)
327 {
328 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
329 goto fail;
330 }
332 if (planar_config != 1)
333 {
334 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
335 goto fail;
336 }
338 if (input_attributes.has_resolution)
339 {
340 x_resolution = input_attributes.x_resolution;
341 y_resolution = input_attributes.y_resolution;
342 }
344 if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
345 {
346 dest_image_width = image_length;
347 dest_image_length = image_width;
348 dest_x_resolution = y_resolution;
349 dest_y_resolution = x_resolution;
350 SWAP (int, width_points, height_points);
351 }
352 else
353 {
354 dest_image_width = image_width;
355 dest_image_length = image_length;
356 dest_x_resolution = x_resolution;
357 dest_y_resolution = y_resolution;
358 }
360 rect.min.x = 0;
361 rect.min.y = 0;
362 rect.max.x = image_width;
363 rect.max.y = image_length;
365 bitmap = create_bitmap (& rect);
367 if (! bitmap)
368 {
369 fprintf (stderr, "can't allocate bitmap\n");
370 goto fail;
371 }
373 for (row = 0; row < image_length; row++)
374 if (1 != TIFFReadScanline (in,
375 bitmap->bits + row * bitmap->row_words,
376 row,
377 0))
378 {
379 fprintf (stderr, "can't read TIFF scanline\n");
380 goto fail;
381 }
383 #ifdef TIFF_REVERSE_BITS
384 reverse_bits ((u8 *) bitmap->bits,
385 image_length * bitmap->row_words * sizeof (word_type));
386 #endif /* TIFF_REVERSE_BITS */
388 if (input_attributes.has_page_size)
389 bitmap = resize_bitmap (bitmap,
390 x_resolution,
391 y_resolution,
392 input_attributes);
394 rotate_bitmap (bitmap,
395 input_attributes);
397 tiff_temp_fd = mkstemp (tiff_temp_fn);
398 if (tiff_temp_fd < 0)
399 {
400 fprintf (stderr, "can't create temporary TIFF file\n");
401 goto fail;
402 }
404 tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
405 if (! out)
406 {
407 fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
408 goto fail;
409 }
411 TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect));
412 TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect));
413 TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
415 TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect));
417 TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
418 TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
419 TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
421 TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
422 TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
423 TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
424 TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
426 #ifdef TIFF_REVERSE_BITS
427 reverse_bits ((u8 *) bitmap->bits,
428 image_length * bitmap->row_words * sizeof (word_type));
429 #endif /* TIFF_REVERSE_BITS */
431 for (row = 0; row < rect_height (& bitmap->rect); row++)
432 if (1 != TIFFWriteScanline (tiff_temp,
433 bitmap->bits + row * bitmap->row_words,
434 row,
435 0))
436 {
437 fprintf (stderr, "can't write TIFF scanline\n");
438 goto fail;
439 }
441 TIFFClose (tiff_temp);
443 width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
444 height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
446 free_bitmap (bitmap);
448 if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
449 {
450 fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
451 goto fail;
452 }
454 sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
456 page = panda_newpage (out->pdf, pagesize);
457 panda_imagebox (out->pdf,
458 page,
459 0, /* top */
460 0, /* left */
461 height_points, /* bottom */
462 width_points, /* right */
463 tiff_temp_fn,
464 panda_image_tiff);
466 result = 1;
468 fail:
469 if (tiff_temp_fd)
470 unlink (tiff_temp_fn);
471 return (result);
472 }
475 int main (int argc, char *argv[])
476 {
477 int result = 0;
479 panda_init ();
481 if (argc != 2)
482 {
483 fprintf (stderr, "usage: %s spec\n", argv [0]);
484 result = 1;
485 goto fail;
486 }
488 if (! parse_spec_file (argv [1]))
489 {
490 result = 2;
491 goto fail;
492 }
494 if (! process_specs ())
495 {
496 result = 3;
497 goto fail;
498 }
500 fail:
501 close_tiff_input_file ();
502 close_pdf_output_files ();
503 return (result);
504 }