Tue, 21 Jan 2003 18:39:55 +0000
output copyright message and URL before usage message
eric@10 | 1 | /* |
eric@44 | 2 | * t2p: Create a PDF file from the contents of one or more TIFF |
eric@44 | 3 | * bilevel image files. The images in the resulting PDF file |
eric@44 | 4 | * will be compressed using ITU-T T.6 (G4) fax encoding. |
eric@29 | 5 | * |
eric@10 | 6 | * Main program |
eric@50 | 7 | * $Id: t2p.c,v 1.21 2003/01/21 10:39:55 eric Exp $ |
eric@49 | 8 | * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> |
eric@10 | 9 | * |
eric@10 | 10 | * This program is free software; you can redistribute it and/or modify |
eric@10 | 11 | * it under the terms of the GNU General Public License version 2 as |
eric@10 | 12 | * published by the Free Software Foundation. Note that permission is |
eric@10 | 13 | * not granted to redistribute this program under the terms of any |
eric@10 | 14 | * other version of the General Public License. |
eric@10 | 15 | * |
eric@10 | 16 | * This program is distributed in the hope that it will be useful, |
eric@10 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@10 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@10 | 19 | * GNU General Public License for more details. |
eric@10 | 20 | * |
eric@10 | 21 | * You should have received a copy of the GNU General Public License |
eric@10 | 22 | * along with this program; if not, write to the Free Software |
eric@44 | 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */ |
eric@10 | 24 | |
eric@10 | 25 | |
eric@49 | 26 | #include <stdarg.h> |
eric@48 | 27 | #include <stdbool.h> |
eric@48 | 28 | #include <stdint.h> |
eric@10 | 29 | #include <stdio.h> |
eric@28 | 30 | #include <stdlib.h> |
eric@28 | 31 | #include <unistd.h> |
eric@47 | 32 | |
eric@10 | 33 | #include <tiffio.h> |
eric@47 | 34 | #define TIFF_REVERSE_BITS |
eric@47 | 35 | |
eric@10 | 36 | #include <panda/functions.h> |
eric@10 | 37 | #include <panda/constants.h> |
eric@10 | 38 | |
eric@10 | 39 | #include "bitblt.h" |
eric@18 | 40 | #include "semantics.h" |
eric@10 | 41 | #include "parser.tab.h" |
eric@44 | 42 | #include "t2p.h" |
eric@10 | 43 | |
eric@10 | 44 | |
eric@49 | 45 | #define MAX_INPUT_FILES 5000 |
eric@49 | 46 | |
eric@28 | 47 | #define POINTS_PER_INCH 72 |
eric@28 | 48 | |
eric@28 | 49 | /* page size limited by Acrobat Reader to 45 inches on a side */ |
eric@28 | 50 | #define PAGE_MAX_INCHES 45 |
eric@28 | 51 | #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH) |
eric@28 | 52 | |
eric@28 | 53 | |
eric@26 | 54 | typedef struct output_file_t |
eric@26 | 55 | { |
eric@26 | 56 | struct output_file_t *next; |
eric@26 | 57 | char *name; |
eric@26 | 58 | panda_pdf *pdf; |
eric@26 | 59 | } output_file_t; |
eric@26 | 60 | |
eric@26 | 61 | |
eric@49 | 62 | int verbose; |
eric@49 | 63 | |
eric@49 | 64 | |
eric@26 | 65 | char *in_filename; |
eric@10 | 66 | TIFF *in; |
eric@26 | 67 | output_file_t *output_files; |
eric@26 | 68 | output_file_t *out; |
eric@26 | 69 | /* panda_pdf *out; */ |
eric@10 | 70 | |
eric@10 | 71 | |
eric@49 | 72 | char *progname; |
eric@49 | 73 | |
eric@49 | 74 | |
eric@49 | 75 | bool close_tiff_input_file (void); |
eric@49 | 76 | bool close_pdf_output_files (void); |
eric@49 | 77 | |
eric@49 | 78 | |
eric@49 | 79 | void usage (void) |
eric@49 | 80 | { |
eric@50 | 81 | fprintf (stderr, "\n"); |
eric@50 | 82 | fprintf (stderr, "t2p - Copyright 2001-2003 Eric Smith <eric@brouhaha.com>\n"); |
eric@50 | 83 | fprintf (stderr, "http://www.brouhaha.com/~eric/software/t2p/\n"); |
eric@50 | 84 | fprintf (stderr, "\n"); |
eric@49 | 85 | fprintf (stderr, "usage:\n"); |
eric@49 | 86 | fprintf (stderr, " %s [options] -s spec\n", progname); |
eric@49 | 87 | fprintf (stderr, " %s [options] <input.tif>... -o <output.pdf>\n", progname); |
eric@49 | 88 | fprintf (stderr, "options:\n"); |
eric@49 | 89 | fprintf (stderr, " -v verbose\n"); |
eric@49 | 90 | } |
eric@49 | 91 | |
eric@49 | 92 | |
eric@49 | 93 | /* generate fatal error message to stderr, doesn't return */ |
eric@49 | 94 | void fatal (int ret, char *format, ...) |
eric@49 | 95 | { |
eric@49 | 96 | va_list ap; |
eric@49 | 97 | |
eric@49 | 98 | fprintf (stderr, "fatal error"); |
eric@49 | 99 | if (format) |
eric@49 | 100 | { |
eric@49 | 101 | fprintf (stderr, ": "); |
eric@49 | 102 | va_start (ap, format); |
eric@49 | 103 | vfprintf (stderr, format, ap); |
eric@49 | 104 | va_end (ap); |
eric@49 | 105 | } |
eric@49 | 106 | else |
eric@49 | 107 | fprintf (stderr, "\n"); |
eric@49 | 108 | if (ret == 1) |
eric@49 | 109 | usage (); |
eric@49 | 110 | close_tiff_input_file (); |
eric@49 | 111 | close_pdf_output_files (); |
eric@49 | 112 | exit (ret); |
eric@49 | 113 | } |
eric@49 | 114 | |
eric@49 | 115 | |
eric@48 | 116 | bool close_tiff_input_file (void) |
eric@10 | 117 | { |
eric@10 | 118 | if (in) |
eric@26 | 119 | { |
eric@26 | 120 | free (in_filename); |
eric@26 | 121 | TIFFClose (in); |
eric@26 | 122 | } |
eric@10 | 123 | in = NULL; |
eric@26 | 124 | in_filename = NULL; |
eric@10 | 125 | return (1); |
eric@10 | 126 | } |
eric@10 | 127 | |
eric@49 | 128 | |
eric@48 | 129 | bool open_tiff_input_file (char *name) |
eric@10 | 130 | { |
eric@10 | 131 | if (in) |
eric@26 | 132 | { |
eric@26 | 133 | if (strcmp (name, in_filename) == 0) |
eric@26 | 134 | return (1); |
eric@26 | 135 | close_tiff_input_file (); |
eric@26 | 136 | } |
eric@26 | 137 | in_filename = strdup (name); |
eric@26 | 138 | if (! in_filename) |
eric@26 | 139 | { |
eric@26 | 140 | fprintf (stderr, "can't strdup input filename '%s'\n", name); |
eric@26 | 141 | return (0); |
eric@26 | 142 | } |
eric@10 | 143 | in = TIFFOpen (name, "r"); |
eric@10 | 144 | if (! in) |
eric@10 | 145 | { |
eric@10 | 146 | fprintf (stderr, "can't open input file '%s'\n", name); |
eric@26 | 147 | free (in_filename); |
eric@10 | 148 | return (0); |
eric@10 | 149 | } |
eric@10 | 150 | return (1); |
eric@10 | 151 | } |
eric@10 | 152 | |
eric@10 | 153 | |
eric@48 | 154 | bool close_pdf_output_files (void) |
eric@10 | 155 | { |
eric@26 | 156 | output_file_t *o, *n; |
eric@26 | 157 | |
eric@26 | 158 | for (o = output_files; o; o = n) |
eric@26 | 159 | { |
eric@26 | 160 | n = o->next; |
eric@26 | 161 | panda_close (o->pdf); |
eric@26 | 162 | free (o->name); |
eric@26 | 163 | free (o); |
eric@26 | 164 | } |
eric@10 | 165 | out = NULL; |
eric@26 | 166 | output_files = NULL; |
eric@10 | 167 | return (1); |
eric@10 | 168 | } |
eric@10 | 169 | |
eric@48 | 170 | bool open_pdf_output_file (char *name, |
eric@48 | 171 | pdf_file_attributes_t *attributes) |
eric@10 | 172 | { |
eric@26 | 173 | output_file_t *o; |
eric@26 | 174 | |
eric@26 | 175 | if (out && (strcmp (name, out->name) == 0)) |
eric@26 | 176 | return (1); |
eric@26 | 177 | for (o = output_files; o; o = o->next) |
eric@26 | 178 | if (strcmp (name, o->name) == 0) |
eric@26 | 179 | { |
eric@26 | 180 | out = o; |
eric@26 | 181 | return (1); |
eric@26 | 182 | } |
eric@26 | 183 | o = calloc (1, sizeof (output_file_t)); |
eric@29 | 184 | if (! o) |
eric@10 | 185 | { |
eric@26 | 186 | fprintf (stderr, "can't calloc output file struct for '%s'\n", name); |
eric@26 | 187 | return (0); |
eric@26 | 188 | } |
eric@26 | 189 | |
eric@26 | 190 | o->name = strdup (name); |
eric@26 | 191 | if (! o->name) |
eric@26 | 192 | { |
eric@26 | 193 | fprintf (stderr, "can't strdup output filename '%s'\n", name); |
eric@26 | 194 | free (o); |
eric@10 | 195 | return (0); |
eric@10 | 196 | } |
eric@26 | 197 | |
eric@26 | 198 | o->pdf = panda_open (name, "w"); |
eric@26 | 199 | if (! o->pdf) |
eric@26 | 200 | { |
eric@26 | 201 | fprintf (stderr, "can't open output file '%s'\n", name); |
eric@26 | 202 | free (o->name); |
eric@26 | 203 | free (o); |
eric@26 | 204 | return (0); |
eric@26 | 205 | } |
eric@26 | 206 | |
eric@30 | 207 | if (attributes->author) |
eric@30 | 208 | panda_setauthor (o->pdf, attributes->author); |
eric@30 | 209 | if (attributes->creator) |
eric@30 | 210 | panda_setcreator (o->pdf, attributes->creator); |
eric@30 | 211 | if (attributes->title) |
eric@30 | 212 | panda_settitle (o->pdf, attributes->title); |
eric@30 | 213 | if (attributes->subject) |
eric@30 | 214 | panda_setsubject (o->pdf, attributes->subject); |
eric@30 | 215 | if (attributes->keywords) |
eric@30 | 216 | panda_setkeywords (o->pdf, attributes->keywords); |
eric@30 | 217 | |
eric@26 | 218 | /* prepend new output file onto list */ |
eric@26 | 219 | o->next = output_files; |
eric@26 | 220 | output_files = o; |
eric@26 | 221 | |
eric@26 | 222 | out = o; |
eric@10 | 223 | return (1); |
eric@10 | 224 | } |
eric@10 | 225 | |
eric@10 | 226 | |
eric@25 | 227 | void process_page_numbers (int page_index, |
eric@25 | 228 | int count, |
eric@25 | 229 | int base, |
eric@25 | 230 | page_label_t *page_label) |
eric@25 | 231 | { |
eric@25 | 232 | } |
eric@25 | 233 | |
eric@25 | 234 | |
eric@42 | 235 | /* frees original! */ |
eric@42 | 236 | static Bitmap *resize_bitmap (Bitmap *src, |
eric@36 | 237 | float x_resolution, |
eric@36 | 238 | float y_resolution, |
eric@36 | 239 | input_attributes_t input_attributes) |
eric@32 | 240 | { |
eric@32 | 241 | Rect src_rect; |
eric@42 | 242 | Point dest_min; |
eric@42 | 243 | Bitmap *dest; |
eric@32 | 244 | |
eric@42 | 245 | int width_pixels = input_attributes.page_size.width * x_resolution; |
eric@42 | 246 | int height_pixels = input_attributes.page_size.height * y_resolution; |
eric@42 | 247 | |
eric@42 | 248 | src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2; |
eric@42 | 249 | src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2; |
eric@42 | 250 | src_rect.max.x = src_rect.min.x + width_pixels; |
eric@42 | 251 | src_rect.max.y = src_rect.min.y + height_pixels; |
eric@36 | 252 | |
eric@42 | 253 | dest_min.x = 0; |
eric@42 | 254 | dest_min.y = 0; |
eric@32 | 255 | |
eric@43 | 256 | dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0); |
eric@42 | 257 | free_bitmap (src); |
eric@42 | 258 | return (dest); |
eric@42 | 259 | } |
eric@32 | 260 | |
eric@42 | 261 | |
eric@42 | 262 | /* "in place" rotation */ |
eric@42 | 263 | static void rotate_bitmap (Bitmap *src, |
eric@42 | 264 | input_attributes_t input_attributes) |
eric@42 | 265 | { |
eric@36 | 266 | switch (input_attributes.rotation) |
eric@32 | 267 | { |
eric@42 | 268 | case 0: break; |
eric@42 | 269 | case 90: rot_90 (src); break; |
eric@42 | 270 | case 180: rot_180 (src); break; |
eric@42 | 271 | case 270: rot_270 (src); break; |
eric@32 | 272 | default: |
eric@32 | 273 | fprintf (stderr, "rotation must be 0, 90, 180, or 270\n"); |
eric@32 | 274 | } |
eric@32 | 275 | } |
eric@32 | 276 | |
eric@32 | 277 | |
eric@32 | 278 | #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) |
eric@32 | 279 | |
eric@49 | 280 | |
eric@49 | 281 | bool last_tiff_page (void) |
eric@49 | 282 | { |
eric@49 | 283 | return (TIFFLastDirectory (in)); |
eric@49 | 284 | } |
eric@49 | 285 | |
eric@49 | 286 | |
eric@48 | 287 | bool process_page (int image, /* range 1 .. n */ |
eric@48 | 288 | input_attributes_t input_attributes, |
eric@48 | 289 | bookmark_t *bookmarks) |
eric@10 | 290 | { |
eric@29 | 291 | int result = 0; |
eric@29 | 292 | |
eric@48 | 293 | uint32_t image_length, image_width; |
eric@48 | 294 | uint32_t dest_image_length, dest_image_width; |
eric@10 | 295 | #ifdef CHECK_DEPTH |
eric@48 | 296 | uint32_t image_depth; |
eric@10 | 297 | #endif |
eric@29 | 298 | |
eric@48 | 299 | uint16_t samples_per_pixel; |
eric@48 | 300 | uint16_t bits_per_sample; |
eric@48 | 301 | uint16_t planar_config; |
eric@32 | 302 | |
eric@48 | 303 | uint16_t resolution_unit; |
eric@10 | 304 | float x_resolution, y_resolution; |
eric@32 | 305 | float dest_x_resolution, dest_y_resolution; |
eric@32 | 306 | |
eric@28 | 307 | int width_points, height_points; /* really 1/72 inch units rather than |
eric@28 | 308 | points */ |
eric@28 | 309 | |
eric@42 | 310 | Rect rect; |
eric@42 | 311 | Bitmap *bitmap; |
eric@42 | 312 | |
eric@32 | 313 | int row; |
eric@10 | 314 | |
eric@28 | 315 | panda_page *page; |
eric@28 | 316 | |
eric@28 | 317 | int tiff_temp_fd; |
eric@44 | 318 | char tiff_temp_fn [] = "/var/tmp/t2p-XXXXXX\0"; |
eric@28 | 319 | TIFF *tiff_temp; |
eric@28 | 320 | |
eric@28 | 321 | char pagesize [26]; /* Needs to hold two ints of four characters (0..3420), |
eric@28 | 322 | two zeros, three spaces, two brackets, and a NULL. |
eric@28 | 323 | Added an extra ten characters just in case. */ |
eric@28 | 324 | |
eric@10 | 325 | if (! TIFFSetDirectory (in, image - 1)) |
eric@10 | 326 | { |
eric@10 | 327 | fprintf (stderr, "can't find page %d of input file\n", image); |
eric@10 | 328 | goto fail; |
eric@10 | 329 | } |
eric@10 | 330 | if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length)) |
eric@10 | 331 | { |
eric@10 | 332 | fprintf (stderr, "can't get image length\n"); |
eric@10 | 333 | goto fail; |
eric@10 | 334 | } |
eric@10 | 335 | if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width)) |
eric@10 | 336 | { |
eric@10 | 337 | fprintf (stderr, "can't get image width\n"); |
eric@10 | 338 | goto fail; |
eric@10 | 339 | } |
eric@29 | 340 | |
eric@29 | 341 | if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel)) |
eric@29 | 342 | { |
eric@29 | 343 | fprintf (stderr, "can't get samples per pixel\n"); |
eric@29 | 344 | goto fail; |
eric@29 | 345 | } |
eric@29 | 346 | |
eric@10 | 347 | #ifdef CHECK_DEPTH |
eric@10 | 348 | if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth)) |
eric@10 | 349 | { |
eric@10 | 350 | fprintf (stderr, "can't get image depth\n"); |
eric@10 | 351 | goto fail; |
eric@10 | 352 | } |
eric@10 | 353 | #endif |
eric@10 | 354 | |
eric@10 | 355 | if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample)) |
eric@10 | 356 | { |
eric@10 | 357 | fprintf (stderr, "can't get bits per sample\n"); |
eric@10 | 358 | goto fail; |
eric@10 | 359 | } |
eric@10 | 360 | |
eric@10 | 361 | if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config)) |
eric@10 | 362 | planar_config = 1; |
eric@10 | 363 | |
eric@10 | 364 | if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit)) |
eric@10 | 365 | resolution_unit = 2; |
eric@10 | 366 | if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution)) |
eric@10 | 367 | x_resolution = 300; |
eric@10 | 368 | if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution)) |
eric@10 | 369 | y_resolution = 300; |
eric@10 | 370 | |
eric@29 | 371 | if (samples_per_pixel != 1) |
eric@29 | 372 | { |
eric@29 | 373 | fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel); |
eric@29 | 374 | goto fail; |
eric@29 | 375 | } |
eric@29 | 376 | |
eric@10 | 377 | #ifdef CHECK_DEPTH |
eric@10 | 378 | if (image_depth != 1) |
eric@10 | 379 | { |
eric@10 | 380 | fprintf (stderr, "image depth %u, must be 1\n", image_depth); |
eric@10 | 381 | goto fail; |
eric@10 | 382 | } |
eric@10 | 383 | #endif |
eric@10 | 384 | |
eric@10 | 385 | if (bits_per_sample != 1) |
eric@10 | 386 | { |
eric@10 | 387 | fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample); |
eric@10 | 388 | goto fail; |
eric@10 | 389 | } |
eric@10 | 390 | |
eric@10 | 391 | if (planar_config != 1) |
eric@10 | 392 | { |
eric@10 | 393 | fprintf (stderr, "planar config %u, must be 1\n", planar_config); |
eric@10 | 394 | goto fail; |
eric@10 | 395 | } |
eric@10 | 396 | |
eric@36 | 397 | if (input_attributes.has_resolution) |
eric@32 | 398 | { |
eric@36 | 399 | x_resolution = input_attributes.x_resolution; |
eric@36 | 400 | y_resolution = input_attributes.y_resolution; |
eric@32 | 401 | } |
eric@32 | 402 | |
eric@32 | 403 | if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270)) |
eric@32 | 404 | { |
eric@32 | 405 | dest_image_width = image_length; |
eric@32 | 406 | dest_image_length = image_width; |
eric@32 | 407 | dest_x_resolution = y_resolution; |
eric@32 | 408 | dest_y_resolution = x_resolution; |
eric@32 | 409 | SWAP (int, width_points, height_points); |
eric@32 | 410 | } |
eric@32 | 411 | else |
eric@32 | 412 | { |
eric@32 | 413 | dest_image_width = image_width; |
eric@32 | 414 | dest_image_length = image_length; |
eric@32 | 415 | dest_x_resolution = x_resolution; |
eric@32 | 416 | dest_y_resolution = y_resolution; |
eric@32 | 417 | } |
eric@32 | 418 | |
eric@42 | 419 | rect.min.x = 0; |
eric@42 | 420 | rect.min.y = 0; |
eric@42 | 421 | rect.max.x = image_width; |
eric@42 | 422 | rect.max.y = image_length; |
eric@42 | 423 | |
eric@42 | 424 | bitmap = create_bitmap (& rect); |
eric@42 | 425 | |
eric@42 | 426 | if (! bitmap) |
eric@10 | 427 | { |
eric@32 | 428 | fprintf (stderr, "can't allocate bitmap\n"); |
eric@10 | 429 | goto fail; |
eric@10 | 430 | } |
eric@10 | 431 | |
eric@10 | 432 | for (row = 0; row < image_length; row++) |
eric@32 | 433 | if (1 != TIFFReadScanline (in, |
eric@43 | 434 | bitmap->bits + row * bitmap->row_words, |
eric@32 | 435 | row, |
eric@32 | 436 | 0)) |
eric@32 | 437 | { |
eric@32 | 438 | fprintf (stderr, "can't read TIFF scanline\n"); |
eric@32 | 439 | goto fail; |
eric@32 | 440 | } |
eric@28 | 441 | |
eric@47 | 442 | #ifdef TIFF_REVERSE_BITS |
eric@48 | 443 | reverse_bits ((uint8_t *) bitmap->bits, |
eric@47 | 444 | image_length * bitmap->row_words * sizeof (word_type)); |
eric@47 | 445 | #endif /* TIFF_REVERSE_BITS */ |
eric@47 | 446 | |
eric@46 | 447 | if (input_attributes.has_page_size) |
eric@46 | 448 | bitmap = resize_bitmap (bitmap, |
eric@46 | 449 | x_resolution, |
eric@46 | 450 | y_resolution, |
eric@46 | 451 | input_attributes); |
eric@42 | 452 | |
eric@42 | 453 | rotate_bitmap (bitmap, |
eric@42 | 454 | input_attributes); |
eric@28 | 455 | |
eric@36 | 456 | tiff_temp_fd = mkstemp (tiff_temp_fn); |
eric@36 | 457 | if (tiff_temp_fd < 0) |
eric@36 | 458 | { |
eric@36 | 459 | fprintf (stderr, "can't create temporary TIFF file\n"); |
eric@36 | 460 | goto fail; |
eric@36 | 461 | } |
eric@36 | 462 | |
eric@36 | 463 | tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w"); |
eric@36 | 464 | if (! out) |
eric@36 | 465 | { |
eric@36 | 466 | fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn); |
eric@36 | 467 | goto fail; |
eric@36 | 468 | } |
eric@36 | 469 | |
eric@42 | 470 | TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect)); |
eric@42 | 471 | TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect)); |
eric@36 | 472 | TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config); |
eric@36 | 473 | |
eric@42 | 474 | TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect)); |
eric@36 | 475 | |
eric@36 | 476 | TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit); |
eric@36 | 477 | TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution); |
eric@36 | 478 | TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution); |
eric@36 | 479 | |
eric@36 | 480 | TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel); |
eric@36 | 481 | TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample); |
eric@36 | 482 | TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
eric@36 | 483 | TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); |
eric@36 | 484 | |
eric@47 | 485 | #ifdef TIFF_REVERSE_BITS |
eric@48 | 486 | reverse_bits ((uint8_t *) bitmap->bits, |
eric@47 | 487 | image_length * bitmap->row_words * sizeof (word_type)); |
eric@47 | 488 | #endif /* TIFF_REVERSE_BITS */ |
eric@47 | 489 | |
eric@42 | 490 | for (row = 0; row < rect_height (& bitmap->rect); row++) |
eric@32 | 491 | if (1 != TIFFWriteScanline (tiff_temp, |
eric@43 | 492 | bitmap->bits + row * bitmap->row_words, |
eric@32 | 493 | row, |
eric@32 | 494 | 0)) |
eric@32 | 495 | { |
eric@32 | 496 | fprintf (stderr, "can't write TIFF scanline\n"); |
eric@32 | 497 | goto fail; |
eric@32 | 498 | } |
eric@32 | 499 | |
eric@32 | 500 | TIFFClose (tiff_temp); |
eric@32 | 501 | |
eric@42 | 502 | width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH; |
eric@42 | 503 | height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH; |
eric@36 | 504 | |
eric@42 | 505 | free_bitmap (bitmap); |
eric@28 | 506 | |
eric@36 | 507 | if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS)) |
eric@36 | 508 | { |
eric@36 | 509 | fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES); |
eric@36 | 510 | goto fail; |
eric@36 | 511 | } |
eric@36 | 512 | |
eric@28 | 513 | sprintf (pagesize, "[0 0 %d %d]", width_points, height_points); |
eric@28 | 514 | |
eric@28 | 515 | page = panda_newpage (out->pdf, pagesize); |
eric@28 | 516 | panda_imagebox (out->pdf, |
eric@28 | 517 | page, |
eric@28 | 518 | 0, /* top */ |
eric@28 | 519 | 0, /* left */ |
eric@28 | 520 | height_points, /* bottom */ |
eric@28 | 521 | width_points, /* right */ |
eric@28 | 522 | tiff_temp_fn, |
eric@28 | 523 | panda_image_tiff); |
eric@28 | 524 | |
eric@29 | 525 | result = 1; |
eric@10 | 526 | |
eric@10 | 527 | fail: |
eric@29 | 528 | if (tiff_temp_fd) |
eric@29 | 529 | unlink (tiff_temp_fn); |
eric@29 | 530 | return (result); |
eric@10 | 531 | } |
eric@10 | 532 | |
eric@10 | 533 | |
eric@49 | 534 | void main_args (char *out_fn, int inf_count, char **in_fn) |
eric@49 | 535 | { |
eric@49 | 536 | int i, ip; |
eric@49 | 537 | input_attributes_t input_attributes; |
eric@49 | 538 | pdf_file_attributes_t output_attributes; |
eric@49 | 539 | |
eric@49 | 540 | memset (& input_attributes, 0, sizeof (input_attributes)); |
eric@49 | 541 | memset (& output_attributes, 0, sizeof (output_attributes)); |
eric@49 | 542 | |
eric@49 | 543 | if (! open_pdf_output_file (out_fn, & output_attributes)) |
eric@49 | 544 | fatal (3, "error opening output file \"%s\"\n", out_fn); |
eric@49 | 545 | for (i = 0; i < inf_count; i++) |
eric@49 | 546 | { |
eric@49 | 547 | if (! open_tiff_input_file (in_fn [i])) |
eric@49 | 548 | fatal (3, "error opening input file \"%s\"\n", in_fn [i]); |
eric@49 | 549 | for (ip = 1;; ip++) |
eric@49 | 550 | { |
eric@49 | 551 | if (! process_page (ip, input_attributes, NULL)) |
eric@49 | 552 | fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]); |
eric@49 | 553 | if (last_tiff_page ()) |
eric@49 | 554 | break; |
eric@49 | 555 | } |
eric@49 | 556 | if (verbose) |
eric@49 | 557 | fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]); |
eric@49 | 558 | if (! close_tiff_input_file ()) |
eric@49 | 559 | fatal (3, "error closing input file \"%s\"\n", in_fn [i]); |
eric@49 | 560 | } |
eric@49 | 561 | if (! close_pdf_output_files ()) |
eric@49 | 562 | fatal (3, "error closing output file \"%s\"\n", out_fn); |
eric@49 | 563 | } |
eric@49 | 564 | |
eric@49 | 565 | |
eric@49 | 566 | void main_spec (char *spec_fn) |
eric@49 | 567 | { |
eric@49 | 568 | if (! parse_spec_file (spec_fn)) |
eric@49 | 569 | fatal (2, "error parsing spec file\n"); |
eric@49 | 570 | if (! process_specs ()) |
eric@49 | 571 | fatal (3, "error processing spec file\n"); |
eric@49 | 572 | } |
eric@49 | 573 | |
eric@49 | 574 | |
eric@10 | 575 | int main (int argc, char *argv[]) |
eric@10 | 576 | { |
eric@49 | 577 | char *spec_fn = NULL; |
eric@49 | 578 | char *out_fn = NULL; |
eric@49 | 579 | int inf_count = 0; |
eric@49 | 580 | char *in_fn [MAX_INPUT_FILES]; |
eric@49 | 581 | |
eric@49 | 582 | progname = argv [0]; |
eric@10 | 583 | |
eric@10 | 584 | panda_init (); |
eric@10 | 585 | |
eric@49 | 586 | while (--argc) |
eric@10 | 587 | { |
eric@49 | 588 | if (argv [1][0] == '-') |
eric@49 | 589 | { |
eric@49 | 590 | if (strcmp (argv [1], "-v") == 0) |
eric@49 | 591 | verbose++; |
eric@49 | 592 | else if (strcmp (argv [1], "-o") == 0) |
eric@49 | 593 | { |
eric@49 | 594 | if (argc) |
eric@49 | 595 | { |
eric@49 | 596 | argc--; |
eric@49 | 597 | argv++; |
eric@49 | 598 | out_fn = argv [1]; |
eric@49 | 599 | } |
eric@49 | 600 | else |
eric@49 | 601 | fatal (1, "missing filename after \"-o\" option\n"); |
eric@49 | 602 | } |
eric@49 | 603 | else if (strcmp (argv [1], "-s") == 0) |
eric@49 | 604 | { |
eric@49 | 605 | if (argc) |
eric@49 | 606 | { |
eric@49 | 607 | argc--; |
eric@49 | 608 | argv++; |
eric@49 | 609 | spec_fn = argv [1]; |
eric@49 | 610 | } |
eric@49 | 611 | else |
eric@49 | 612 | fatal (1, "missing filename after \"-s\" option\n"); |
eric@49 | 613 | } |
eric@49 | 614 | else |
eric@49 | 615 | fatal (1, "unrecognized option \"%s\"\n", argv [1]); |
eric@49 | 616 | } |
eric@49 | 617 | else if (inf_count < MAX_INPUT_FILES) |
eric@49 | 618 | in_fn [inf_count++] = argv [1]; |
eric@49 | 619 | else |
eric@49 | 620 | fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES); |
eric@49 | 621 | argv++; |
eric@10 | 622 | } |
eric@10 | 623 | |
eric@49 | 624 | if (! ((! out_fn) ^ (! spec_fn))) |
eric@49 | 625 | fatal (1, "either a spec file or an output file (but not both) must be specified\n"); |
eric@49 | 626 | |
eric@49 | 627 | if (out_fn && ! inf_count) |
eric@49 | 628 | fatal (1, "no input files specified\n"); |
eric@26 | 629 | |
eric@49 | 630 | if (spec_fn && inf_count) |
eric@49 | 631 | fatal (1, "if spec file is provided, input files can't be specified as arguments\n"); |
eric@49 | 632 | |
eric@49 | 633 | if (spec_fn) |
eric@49 | 634 | main_spec (spec_fn); |
eric@49 | 635 | else |
eric@49 | 636 | main_args (out_fn, inf_count, in_fn); |
eric@17 | 637 | |
eric@10 | 638 | close_tiff_input_file (); |
eric@26 | 639 | close_pdf_output_files (); |
eric@49 | 640 | exit (0); |
eric@10 | 641 | } |