Fri, 14 Mar 2003 07:08:52 +0000
bookmarks should only be created for first page in a group.
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * Main program
5 * $Id: tumble.c,v 1.33 2003/03/13 03:42:46 eric Exp $
6 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. Note that permission is
11 * not granted to redistribute this program under the terms of any
12 * other version of the General Public License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 */
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <tiffio.h>
34 #define TIFF_REVERSE_BITS
36 #include "bitblt.h"
37 #include "semantics.h"
38 #include "parser.tab.h"
39 #include "tumble.h"
40 #include "pdf.h"
43 #define MAX_INPUT_FILES 5000
45 #define POINTS_PER_INCH 72
47 /* page size limited by Acrobat Reader to 45 inches on a side */
48 #define PAGE_MAX_INCHES 45
49 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
52 typedef struct output_file_t
53 {
54 struct output_file_t *next;
55 char *name;
56 pdf_file_handle pdf;
57 } output_file_t;
60 int verbose;
63 char *in_filename;
64 TIFF *in;
65 output_file_t *output_files;
66 output_file_t *out;
69 char *progname;
72 bool close_tiff_input_file (void);
73 bool close_pdf_output_files (void);
76 void usage (void)
77 {
78 fprintf (stderr, "\n");
79 fprintf (stderr, "tumble - Copyright 2001-2003 Eric Smith <eric@brouhaha.com>\n");
80 fprintf (stderr, "http://tumble.brouhaha.com/\n");
81 fprintf (stderr, "\n");
82 fprintf (stderr, "usage:\n");
83 fprintf (stderr, " %s [options] -s spec\n", progname);
84 fprintf (stderr, " %s [options] <input.tif>... -o <output.pdf>\n", progname);
85 fprintf (stderr, "options:\n");
86 fprintf (stderr, " -v verbose\n");
87 fprintf (stderr, " -b fmt create bookmarks\n");
88 fprintf (stderr, "bookmark format:\n");
89 fprintf (stderr, " %%F file name (sans suffix)\n");
90 fprintf (stderr, " %%p page number\n");
91 }
94 /* generate fatal error message to stderr, doesn't return */
95 void fatal (int ret, char *format, ...)
96 {
97 va_list ap;
99 fprintf (stderr, "fatal error");
100 if (format)
101 {
102 fprintf (stderr, ": ");
103 va_start (ap, format);
104 vfprintf (stderr, format, ap);
105 va_end (ap);
106 }
107 else
108 fprintf (stderr, "\n");
109 if (ret == 1)
110 usage ();
111 close_tiff_input_file ();
112 close_pdf_output_files ();
113 exit (ret);
114 }
117 bool close_tiff_input_file (void)
118 {
119 if (in)
120 {
121 free (in_filename);
122 TIFFClose (in);
123 }
124 in = NULL;
125 in_filename = NULL;
126 return (1);
127 }
130 bool open_tiff_input_file (char *name)
131 {
132 if (in)
133 {
134 if (strcmp (name, in_filename) == 0)
135 return (1);
136 close_tiff_input_file ();
137 }
138 in_filename = strdup (name);
139 if (! in_filename)
140 {
141 fprintf (stderr, "can't strdup input filename '%s'\n", name);
142 return (0);
143 }
144 in = TIFFOpen (name, "r");
145 if (! in)
146 {
147 fprintf (stderr, "can't open input file '%s'\n", name);
148 free (in_filename);
149 return (0);
150 }
151 return (1);
152 }
155 bool close_pdf_output_files (void)
156 {
157 output_file_t *o, *n;
159 for (o = output_files; o; o = n)
160 {
161 n = o->next;
162 pdf_close (o->pdf);
163 free (o->name);
164 free (o);
165 }
166 out = NULL;
167 output_files = NULL;
168 return (1);
169 }
171 bool open_pdf_output_file (char *name,
172 pdf_file_attributes_t *attributes)
173 {
174 output_file_t *o;
176 if (out && (strcmp (name, out->name) == 0))
177 return (1);
178 for (o = output_files; o; o = o->next)
179 if (strcmp (name, o->name) == 0)
180 {
181 out = o;
182 return (1);
183 }
184 o = calloc (1, sizeof (output_file_t));
185 if (! o)
186 {
187 fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
188 return (0);
189 }
191 o->name = strdup (name);
192 if (! o->name)
193 {
194 fprintf (stderr, "can't strdup output filename '%s'\n", name);
195 free (o);
196 return (0);
197 }
199 o->pdf = pdf_create (name, (attributes->has_bookmarks ?
200 PDF_PAGE_MODE_USE_OUTLINES :
201 PDF_PAGE_MODE_USE_NONE));
202 if (! o->pdf)
203 {
204 fprintf (stderr, "can't open output file '%s'\n", name);
205 free (o->name);
206 free (o);
207 return (0);
208 }
210 if (attributes->author)
211 pdf_set_author (o->pdf, attributes->author);
212 if (attributes->creator)
213 pdf_set_creator (o->pdf, attributes->creator);
214 if (attributes->title)
215 pdf_set_title (o->pdf, attributes->title);
216 if (attributes->subject)
217 pdf_set_subject (o->pdf, attributes->subject);
218 if (attributes->keywords)
219 pdf_set_keywords (o->pdf, attributes->keywords);
221 /* prepend new output file onto list */
222 o->next = output_files;
223 output_files = o;
225 out = o;
226 return (1);
227 }
230 void process_page_numbers (int page_index,
231 int count,
232 int base,
233 page_label_t *page_label)
234 {
235 }
238 /* frees original! */
239 static Bitmap *resize_bitmap (Bitmap *src,
240 double x_resolution,
241 double y_resolution,
242 input_attributes_t input_attributes)
243 {
244 Rect src_rect;
245 Point dest_min;
246 Bitmap *dest;
248 int width_pixels = input_attributes.page_size.width * x_resolution;
249 int height_pixels = input_attributes.page_size.height * y_resolution;
251 src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
252 src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
253 src_rect.max.x = src_rect.min.x + width_pixels;
254 src_rect.max.y = src_rect.min.y + height_pixels;
256 dest_min.x = 0;
257 dest_min.y = 0;
259 dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
260 free_bitmap (src);
261 return (dest);
262 }
265 /* "in place" rotation */
266 static void rotate_bitmap (Bitmap *src,
267 input_attributes_t input_attributes)
268 {
269 switch (input_attributes.rotation)
270 {
271 case 0: break;
272 case 90: rot_90 (src); break;
273 case 180: rot_180 (src); break;
274 case 270: rot_270 (src); break;
275 default:
276 fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
277 }
278 }
281 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
284 bool last_tiff_page (void)
285 {
286 return (TIFFLastDirectory (in));
287 }
290 bool process_tiff_page (int image, /* range 1 .. n */
291 input_attributes_t input_attributes,
292 bookmark_t *bookmarks)
293 {
294 int result = 0;
296 uint32_t image_length, image_width;
297 uint32_t dest_image_length, dest_image_width;
298 #ifdef CHECK_DEPTH
299 uint32_t image_depth;
300 #endif
302 uint16_t samples_per_pixel;
303 uint16_t bits_per_sample;
304 uint16_t planar_config;
306 uint16_t resolution_unit;
307 float x_resolution, y_resolution;
308 double dest_x_resolution, dest_y_resolution;
310 double width_points, height_points; /* really 1/72 inch units rather than
311 points */
313 Rect rect;
314 Bitmap *bitmap = NULL;
316 int row;
318 pdf_page_handle page;
320 if (! TIFFSetDirectory (in, image - 1))
321 {
322 fprintf (stderr, "can't find page %d of input file\n", image);
323 goto fail;
324 }
325 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
326 {
327 fprintf (stderr, "can't get image length\n");
328 goto fail;
329 }
330 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
331 {
332 fprintf (stderr, "can't get image width\n");
333 goto fail;
334 }
336 if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
337 {
338 fprintf (stderr, "can't get samples per pixel\n");
339 goto fail;
340 }
342 #ifdef CHECK_DEPTH
343 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
344 {
345 fprintf (stderr, "can't get image depth\n");
346 goto fail;
347 }
348 #endif
350 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
351 {
352 fprintf (stderr, "can't get bits per sample\n");
353 goto fail;
354 }
356 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
357 planar_config = 1;
359 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
360 resolution_unit = 2;
361 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
362 x_resolution = 300;
363 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
364 y_resolution = 300;
366 if (samples_per_pixel != 1)
367 {
368 fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
369 goto fail;
370 }
372 #ifdef CHECK_DEPTH
373 if (image_depth != 1)
374 {
375 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
376 goto fail;
377 }
378 #endif
380 if (bits_per_sample != 1)
381 {
382 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
383 goto fail;
384 }
386 if (planar_config != 1)
387 {
388 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
389 goto fail;
390 }
392 if (input_attributes.has_resolution)
393 {
394 x_resolution = input_attributes.x_resolution;
395 y_resolution = input_attributes.y_resolution;
396 }
398 if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
399 {
400 dest_image_width = image_length;
401 dest_image_length = image_width;
402 dest_x_resolution = y_resolution;
403 dest_y_resolution = x_resolution;
404 SWAP (double, width_points, height_points); /* $$$ not yet set!!! */
405 }
406 else
407 {
408 dest_image_width = image_width;
409 dest_image_length = image_length;
410 dest_x_resolution = x_resolution;
411 dest_y_resolution = y_resolution;
412 }
414 rect.min.x = 0;
415 rect.min.y = 0;
416 rect.max.x = image_width;
417 rect.max.y = image_length;
419 bitmap = create_bitmap (& rect);
421 if (! bitmap)
422 {
423 fprintf (stderr, "can't allocate bitmap\n");
424 goto fail;
425 }
427 for (row = 0; row < image_length; row++)
428 if (1 != TIFFReadScanline (in,
429 bitmap->bits + row * bitmap->row_words,
430 row,
431 0))
432 {
433 fprintf (stderr, "can't read TIFF scanline\n");
434 goto fail;
435 }
437 #ifdef TIFF_REVERSE_BITS
438 reverse_bits ((uint8_t *) bitmap->bits,
439 image_length * bitmap->row_words * sizeof (word_t));
440 #endif /* TIFF_REVERSE_BITS */
442 #if 0
443 if (input_attributes.has_page_size)
444 bitmap = resize_bitmap (bitmap,
445 x_resolution,
446 y_resolution,
447 input_attributes);
448 #endif
450 rotate_bitmap (bitmap,
451 input_attributes);
453 width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
454 height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
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 page = pdf_new_page (out->pdf, width_points, height_points);
464 #if 0
465 pdf_write_text (page);
466 #else
467 pdf_write_g4_fax_image (page,
468 0, 0, /* x, y */
469 width_points, height_points,
470 bitmap,
471 0, /* ImageMask */
472 0, 0, 0, /* r, g, b */
473 0); /* BlackIs1 */
474 #endif
476 while (bookmarks)
477 {
478 /* $$$ need to handle level here */
479 pdf_new_bookmark (NULL, bookmarks->name, 0, page);
480 bookmarks = bookmarks->next;
481 }
483 result = 1;
485 fail:
486 if (bitmap)
487 free_bitmap (bitmap);
489 return (result);
490 }
493 #if 0
494 bool process_jpeg_page (int image, /* range 1 .. n */
495 input_attributes_t input_attributes,
496 bookmark_t *bookmarks)
497 {
498 int result = 0;
499 FILE *f;
500 pdf_page_handle page;
502 f = fopen (filename, "rb");
503 if (! f)
504 fatal ("error opening input file '%s'\n", filename);
506 page = pdf_new_page (out->pdf, width_points, height_points);
508 pdf_write_jpeg_image (page,
509 0, 0, /* x, y */
510 width_points, height_points,
511 f);
513 return (result);
514 }
515 #endif
518 bool process_page (int image, /* range 1 .. n */
519 input_attributes_t input_attributes,
520 bookmark_t *bookmarks)
521 {
522 int result = 0;
524 result = process_tiff_page (image, input_attributes, bookmarks);
526 return (result);
527 }
530 #define MAX_BOOKMARK_NAME_LEN 500
533 static int filename_length_without_suffix (char *in_fn)
534 {
535 char *p;
536 int len = strlen (in_fn);
538 p = strrchr (in_fn, '.');
539 if (p && ((strcasecmp (p, ".tif") == 0) ||
540 (strcasecmp (p, ".tiff") == 0)))
541 return (p - in_fn);
542 return (len);
543 }
546 /* $$$ this function should ensure that it doesn't overflow the name string! */
547 static void generate_bookmark_name (char *name,
548 char *bookmark_fmt,
549 char *in_fn,
550 int page)
551 {
552 bool meta = 0;
553 int len;
555 while (*bookmark_fmt)
556 {
557 if (meta)
558 {
559 meta = 0;
560 switch (*bookmark_fmt)
561 {
562 case '%':
563 *(name++) = '%';
564 break;
565 case 'F':
566 len = filename_length_without_suffix (in_fn);
567 strncpy (name, in_fn, len);
568 name += len;
569 break;
570 case 'p':
571 sprintf (name, "%d", page);
572 name += strlen (name);
573 break;
574 default:
575 break;
576 }
577 }
578 else
579 switch (*bookmark_fmt)
580 {
581 case '%':
582 meta = 1;
583 break;
584 default:
585 *(name++) = *bookmark_fmt;
586 }
587 bookmark_fmt++;
588 }
589 *name = '\0';
590 }
593 void main_args (char *out_fn,
594 int inf_count,
595 char **in_fn,
596 char *bookmark_fmt)
597 {
598 int i, ip;
599 input_attributes_t input_attributes;
600 pdf_file_attributes_t output_attributes;
601 bookmark_t bookmark;
602 char bookmark_name [MAX_BOOKMARK_NAME_LEN];
604 bookmark.next = NULL;
605 bookmark.level = 1;
606 bookmark.name = & bookmark_name [0];
608 memset (& input_attributes, 0, sizeof (input_attributes));
609 memset (& output_attributes, 0, sizeof (output_attributes));
611 output_attributes.has_bookmarks = (bookmark_fmt != NULL);
613 if (! open_pdf_output_file (out_fn, & output_attributes))
614 fatal (3, "error opening output file \"%s\"\n", out_fn);
615 for (i = 0; i < inf_count; i++)
616 {
617 if (! open_tiff_input_file (in_fn [i]))
618 fatal (3, "error opening input file \"%s\"\n", in_fn [i]);
619 for (ip = 1;; ip++)
620 {
621 fprintf (stderr, "processing page %d of file \"%s\"\r", ip, in_fn [i]);
622 if (bookmark_fmt)
623 generate_bookmark_name (& bookmark_name [0],
624 bookmark_fmt,
625 in_fn [i],
626 ip);
627 if (! process_page (ip, input_attributes,
628 bookmark_fmt ? & bookmark : NULL))
629 fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
630 if (last_tiff_page ())
631 break;
632 }
633 if (verbose)
634 fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]);
635 if (! close_tiff_input_file ())
636 fatal (3, "error closing input file \"%s\"\n", in_fn [i]);
637 }
638 if (! close_pdf_output_files ())
639 fatal (3, "error closing output file \"%s\"\n", out_fn);
640 }
643 void main_spec (char *spec_fn)
644 {
645 if (! parse_spec_file (spec_fn))
646 fatal (2, "error parsing spec file\n");
647 if (! process_specs ())
648 fatal (3, "error processing spec file\n");
649 }
652 int main (int argc, char *argv[])
653 {
654 char *spec_fn = NULL;
655 char *out_fn = NULL;
656 char *bookmark_fmt = NULL;
657 int inf_count = 0;
658 char *in_fn [MAX_INPUT_FILES];
660 progname = argv [0];
662 pdf_init ();
664 while (--argc)
665 {
666 if (argv [1][0] == '-')
667 {
668 if (strcmp (argv [1], "-v") == 0)
669 verbose++;
670 else if (strcmp (argv [1], "-o") == 0)
671 {
672 if (argc)
673 {
674 argc--;
675 argv++;
676 out_fn = argv [1];
677 }
678 else
679 fatal (1, "missing filename after \"-o\" option\n");
680 }
681 else if (strcmp (argv [1], "-s") == 0)
682 {
683 if (argc)
684 {
685 argc--;
686 argv++;
687 spec_fn = argv [1];
688 }
689 else
690 fatal (1, "missing filename after \"-s\" option\n");
691 }
692 else if (strcmp (argv [1], "-b") == 0)
693 {
694 if (argc)
695 {
696 argc--;
697 argv++;
698 bookmark_fmt = argv [1];
699 }
700 else
701 fatal (1, "missing format string after \"-b\" option\n");
702 }
703 else
704 fatal (1, "unrecognized option \"%s\"\n", argv [1]);
705 }
706 else if (inf_count < MAX_INPUT_FILES)
707 in_fn [inf_count++] = argv [1];
708 else
709 fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES);
710 argv++;
711 }
713 if (! ((! out_fn) ^ (! spec_fn)))
714 fatal (1, "either a spec file or an output file (but not both) must be specified\n");
716 if (out_fn && ! inf_count)
717 fatal (1, "no input files specified\n");
719 if (spec_fn && inf_count)
720 fatal (1, "if spec file is provided, input files can't be specified as arguments\n");
722 if (spec_fn)
723 main_spec (spec_fn);
724 else
725 main_args (out_fn, inf_count, in_fn, bookmark_fmt);
727 close_tiff_input_file ();
728 close_pdf_output_files ();
729 exit (0);
730 }