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