tumble.c

Mon, 26 Aug 2002 06:03:55 +0000

author
eric
date
Mon, 26 Aug 2002 06:03:55 +0000
changeset 48
3d0be1c1c1b2
parent 47
bfc6aaa089b0
child 49
be20d7e8466f
permissions
-rw-r--r--

now use C99 stdint.h and stdbool.h

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