t2p.c

Wed, 30 Jan 2002 08:55:53 +0000

author
eric
date
Wed, 30 Jan 2002 08:55:53 +0000
changeset 44
0f78374addae
parent 43
b80cb5a4282a
child 46
20fda1ec5f17
permissions
-rw-r--r--

changed program name to "t2p"

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