t2p.c

changeset 125
e2ef1c2f9eca
parent 124
ba64dfca82e9
child 126
4089ff3b927c
     1.1 diff -r ba64dfca82e9 -r e2ef1c2f9eca t2p.c
     1.2 --- a/t2p.c	Thu Mar 13 08:03:11 2003 +0000
     1.3 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.4 @@ -1,732 +0,0 @@
     1.5 -/*
     1.6 - * t2p: Create a PDF file from the contents of one or more TIFF
     1.7 - *      bilevel image files.  The images in the resulting PDF file
     1.8 - *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.9 - *
    1.10 - * Main program
    1.11 - * $Id: t2p.c,v 1.31 2003/03/12 23:59:10 eric Exp $
    1.12 - * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.13 - *
    1.14 - * This program is free software; you can redistribute it and/or modify
    1.15 - * it under the terms of the GNU General Public License version 2 as
    1.16 - * published by the Free Software Foundation.  Note that permission is
    1.17 - * not granted to redistribute this program under the terms of any
    1.18 - * other version of the General Public License.
    1.19 - *
    1.20 - * This program is distributed in the hope that it will be useful,
    1.21 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.23 - * GNU General Public License for more details.
    1.24 - *
    1.25 - * You should have received a copy of the GNU General Public License
    1.26 - * along with this program; if not, write to the Free Software
    1.27 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    1.28 - */
    1.29 -
    1.30 -
    1.31 -#include <stdarg.h>
    1.32 -#include <stdbool.h>
    1.33 -#include <stdint.h>
    1.34 -#include <stdio.h>
    1.35 -#include <stdlib.h>
    1.36 -#include <string.h>
    1.37 -#include <unistd.h>
    1.38 -
    1.39 -#include <tiffio.h>
    1.40 -#define TIFF_REVERSE_BITS
    1.41 -
    1.42 -#include "bitblt.h"
    1.43 -#include "semantics.h"
    1.44 -#include "parser.tab.h"
    1.45 -#include "t2p.h"
    1.46 -#include "pdf.h"
    1.47 -
    1.48 -
    1.49 -#define MAX_INPUT_FILES 5000
    1.50 -
    1.51 -#define POINTS_PER_INCH 72
    1.52 -
    1.53 -/* page size limited by Acrobat Reader to 45 inches on a side */
    1.54 -#define PAGE_MAX_INCHES 45
    1.55 -#define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
    1.56 -
    1.57 -
    1.58 -typedef struct output_file_t
    1.59 -{
    1.60 -  struct output_file_t *next;
    1.61 -  char *name;
    1.62 -  pdf_file_handle pdf;
    1.63 -} output_file_t;
    1.64 -
    1.65 -
    1.66 -int verbose;
    1.67 -
    1.68 -
    1.69 -char *in_filename;
    1.70 -TIFF *in;
    1.71 -output_file_t *output_files;
    1.72 -output_file_t *out;
    1.73 -
    1.74 -
    1.75 -char *progname;
    1.76 -
    1.77 -
    1.78 -bool close_tiff_input_file (void);
    1.79 -bool close_pdf_output_files (void);
    1.80 -
    1.81 -
    1.82 -void usage (void)
    1.83 -{
    1.84 -  fprintf (stderr, "\n");
    1.85 -  fprintf (stderr, "t2p - Copyright 2001-2003 Eric Smith <eric@brouhaha.com>\n");
    1.86 -  fprintf (stderr, "http://www.brouhaha.com/~eric/software/t2p/\n");
    1.87 -  fprintf (stderr, "\n");
    1.88 -  fprintf (stderr, "usage:\n");
    1.89 -  fprintf (stderr, "    %s [options] -s spec\n", progname);
    1.90 -  fprintf (stderr, "    %s [options] <input.tif>... -o <output.pdf>\n", progname);
    1.91 -  fprintf (stderr, "options:\n");
    1.92 -  fprintf (stderr, "    -v   verbose\n");
    1.93 -  fprintf (stderr, "    -b fmt  create bookmarks\n");
    1.94 -  fprintf (stderr, "bookmark format:\n");
    1.95 -  fprintf (stderr, "    %%F  file name (sans suffix)\n");
    1.96 -  fprintf (stderr, "    %%p  page number\n");
    1.97 -}
    1.98 -
    1.99 -
   1.100 -/* generate fatal error message to stderr, doesn't return */
   1.101 -void fatal (int ret, char *format, ...)
   1.102 -{
   1.103 -  va_list ap;
   1.104 -
   1.105 -  fprintf (stderr, "fatal error");
   1.106 -  if (format)
   1.107 -    {
   1.108 -      fprintf (stderr, ": ");
   1.109 -      va_start (ap, format);
   1.110 -      vfprintf (stderr, format, ap);
   1.111 -      va_end (ap);
   1.112 -    }
   1.113 -  else
   1.114 -    fprintf (stderr, "\n");
   1.115 -  if (ret == 1)
   1.116 -    usage ();
   1.117 -  close_tiff_input_file ();
   1.118 -  close_pdf_output_files ();
   1.119 -  exit (ret);
   1.120 -}
   1.121 -
   1.122 -
   1.123 -bool close_tiff_input_file (void)
   1.124 -{
   1.125 -  if (in)
   1.126 -    {
   1.127 -      free (in_filename);
   1.128 -      TIFFClose (in);
   1.129 -    }
   1.130 -  in = NULL;
   1.131 -  in_filename = NULL;
   1.132 -  return (1);
   1.133 -}
   1.134 -
   1.135 -
   1.136 -bool open_tiff_input_file (char *name)
   1.137 -{
   1.138 -  if (in)
   1.139 -    {
   1.140 -      if (strcmp (name, in_filename) == 0)
   1.141 -	return (1);
   1.142 -      close_tiff_input_file ();
   1.143 -    }
   1.144 -  in_filename = strdup (name);
   1.145 -  if (! in_filename)
   1.146 -    {
   1.147 -      fprintf (stderr, "can't strdup input filename '%s'\n", name);
   1.148 -      return (0);
   1.149 -    }
   1.150 -  in = TIFFOpen (name, "r");
   1.151 -  if (! in)
   1.152 -    {
   1.153 -      fprintf (stderr, "can't open input file '%s'\n", name);
   1.154 -      free (in_filename);
   1.155 -      return (0);
   1.156 -    }
   1.157 -  return (1);
   1.158 -}
   1.159 -
   1.160 -
   1.161 -bool close_pdf_output_files (void)
   1.162 -{
   1.163 -  output_file_t *o, *n;
   1.164 -
   1.165 -  for (o = output_files; o; o = n)
   1.166 -    {
   1.167 -      n = o->next;
   1.168 -      pdf_close (o->pdf);
   1.169 -      free (o->name);
   1.170 -      free (o);
   1.171 -    }
   1.172 -  out = NULL;
   1.173 -  output_files = NULL;
   1.174 -  return (1);
   1.175 -}
   1.176 -
   1.177 -bool open_pdf_output_file (char *name,
   1.178 -			   pdf_file_attributes_t *attributes)
   1.179 -{
   1.180 -  output_file_t *o;
   1.181 -
   1.182 -  if (out && (strcmp (name, out->name) == 0))
   1.183 -    return (1);
   1.184 -  for (o = output_files; o; o = o->next)
   1.185 -    if (strcmp (name, o->name) == 0)
   1.186 -      {
   1.187 -	out = o;
   1.188 -	return (1);
   1.189 -      }
   1.190 -  o = calloc (1, sizeof (output_file_t));
   1.191 -  if (! o)
   1.192 -    {
   1.193 -      fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   1.194 -      return (0);
   1.195 -   }
   1.196 -
   1.197 -  o->name = strdup (name);
   1.198 -  if (! o->name)
   1.199 -    {
   1.200 -      fprintf (stderr, "can't strdup output filename '%s'\n", name);
   1.201 -      free (o);
   1.202 -      return (0);
   1.203 -    }
   1.204 -
   1.205 -  o->pdf = pdf_create (name, (attributes->has_bookmarks ?
   1.206 -			      PDF_PAGE_MODE_USE_OUTLINES :
   1.207 -			      PDF_PAGE_MODE_USE_NONE));
   1.208 -  if (! o->pdf)
   1.209 -    {
   1.210 -      fprintf (stderr, "can't open output file '%s'\n", name);
   1.211 -      free (o->name);
   1.212 -      free (o);
   1.213 -      return (0);
   1.214 -    }
   1.215 -
   1.216 -  if (attributes->author)
   1.217 -    pdf_set_author (o->pdf, attributes->author);
   1.218 -  if (attributes->creator)
   1.219 -    pdf_set_creator (o->pdf, attributes->creator);
   1.220 -  if (attributes->title)
   1.221 -    pdf_set_title (o->pdf, attributes->title);
   1.222 -  if (attributes->subject)
   1.223 -    pdf_set_subject (o->pdf, attributes->subject);
   1.224 -  if (attributes->keywords)
   1.225 -    pdf_set_keywords (o->pdf, attributes->keywords);
   1.226 -
   1.227 -  /* prepend new output file onto list */
   1.228 -  o->next = output_files;
   1.229 -  output_files = o;
   1.230 -
   1.231 -  out = o;
   1.232 -  return (1);
   1.233 -}
   1.234 -
   1.235 -
   1.236 -void process_page_numbers (int page_index,
   1.237 -			   int count,
   1.238 -			   int base,
   1.239 -			   page_label_t *page_label)
   1.240 -{
   1.241 -}
   1.242 -
   1.243 -
   1.244 -/* frees original! */
   1.245 -static Bitmap *resize_bitmap (Bitmap *src,
   1.246 -			      double x_resolution,
   1.247 -			      double y_resolution,
   1.248 -			      input_attributes_t input_attributes)
   1.249 -{
   1.250 -  Rect src_rect;
   1.251 -  Point dest_min;
   1.252 -  Bitmap *dest;
   1.253 -
   1.254 -  int width_pixels = input_attributes.page_size.width * x_resolution;
   1.255 -  int height_pixels = input_attributes.page_size.height * y_resolution;
   1.256 -
   1.257 -  src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
   1.258 -  src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
   1.259 -  src_rect.max.x = src_rect.min.x + width_pixels;
   1.260 -  src_rect.max.y = src_rect.min.y + height_pixels;
   1.261 -
   1.262 -  dest_min.x = 0;
   1.263 -  dest_min.y = 0;
   1.264 -
   1.265 -  dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
   1.266 -  free_bitmap (src);
   1.267 -  return (dest);
   1.268 -}
   1.269 -
   1.270 -
   1.271 -/* "in place" rotation */
   1.272 -static void rotate_bitmap (Bitmap *src,
   1.273 -			   input_attributes_t input_attributes)
   1.274 -{
   1.275 -  switch (input_attributes.rotation)
   1.276 -    {
   1.277 -    case 0: break;
   1.278 -    case 90: rot_90 (src); break;
   1.279 -    case 180: rot_180 (src); break;
   1.280 -    case 270: rot_270 (src); break;
   1.281 -    default:
   1.282 -      fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
   1.283 -    }
   1.284 -}
   1.285 -
   1.286 -
   1.287 -#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
   1.288 -
   1.289 -
   1.290 -bool last_tiff_page (void)
   1.291 -{
   1.292 -  return (TIFFLastDirectory (in));
   1.293 -}
   1.294 -
   1.295 -
   1.296 -bool process_tiff_page (int image,  /* range 1 .. n */
   1.297 -			input_attributes_t input_attributes,
   1.298 -			bookmark_t *bookmarks)
   1.299 -{
   1.300 -  int result = 0;
   1.301 -
   1.302 -  uint32_t image_length, image_width;
   1.303 -  uint32_t dest_image_length, dest_image_width;
   1.304 -#ifdef CHECK_DEPTH
   1.305 -  uint32_t image_depth;
   1.306 -#endif
   1.307 -
   1.308 -  uint16_t samples_per_pixel;
   1.309 -  uint16_t bits_per_sample;
   1.310 -  uint16_t planar_config;
   1.311 -
   1.312 -  uint16_t resolution_unit;
   1.313 -  float x_resolution, y_resolution;
   1.314 -  double dest_x_resolution, dest_y_resolution;
   1.315 -
   1.316 -  double width_points, height_points;  /* really 1/72 inch units rather than
   1.317 -					  points */
   1.318 -
   1.319 -  Rect rect;
   1.320 -  Bitmap *bitmap = NULL;
   1.321 -
   1.322 -  int row;
   1.323 -
   1.324 -  pdf_page_handle page;
   1.325 -
   1.326 -  if (! TIFFSetDirectory (in, image - 1))
   1.327 -    {
   1.328 -      fprintf (stderr, "can't find page %d of input file\n", image);
   1.329 -      goto fail;
   1.330 -    }
   1.331 -  if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   1.332 -    {
   1.333 -      fprintf (stderr, "can't get image length\n");
   1.334 -      goto fail;
   1.335 -    }
   1.336 -  if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   1.337 -    {
   1.338 -      fprintf (stderr, "can't get image width\n");
   1.339 -      goto fail;
   1.340 -    }
   1.341 -
   1.342 -  if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
   1.343 -    {
   1.344 -      fprintf (stderr, "can't get samples per pixel\n");
   1.345 -      goto fail;
   1.346 -    }
   1.347 -
   1.348 -#ifdef CHECK_DEPTH
   1.349 -  if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   1.350 -    {
   1.351 -      fprintf (stderr, "can't get image depth\n");
   1.352 -      goto fail;
   1.353 -    }
   1.354 -#endif
   1.355 -
   1.356 -  if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   1.357 -    {
   1.358 -      fprintf (stderr, "can't get bits per sample\n");
   1.359 -      goto fail;
   1.360 -    }
   1.361 -
   1.362 -  if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   1.363 -    planar_config = 1;
   1.364 -
   1.365 -  if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   1.366 -    resolution_unit = 2;
   1.367 -  if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   1.368 -    x_resolution = 300;
   1.369 -  if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   1.370 -    y_resolution = 300;
   1.371 -
   1.372 -  if (samples_per_pixel != 1)
   1.373 -    {
   1.374 -      fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
   1.375 -      goto fail;
   1.376 -    }
   1.377 -
   1.378 -#ifdef CHECK_DEPTH
   1.379 -  if (image_depth != 1)
   1.380 -    {
   1.381 -      fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   1.382 -      goto fail;
   1.383 -    }
   1.384 -#endif
   1.385 -
   1.386 -  if (bits_per_sample != 1)
   1.387 -    {
   1.388 -      fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   1.389 -      goto fail;
   1.390 -    }
   1.391 -
   1.392 -  if (planar_config != 1)
   1.393 -    {
   1.394 -      fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   1.395 -      goto fail;
   1.396 -    }
   1.397 -
   1.398 -  if (input_attributes.has_resolution)
   1.399 -    {
   1.400 -      x_resolution = input_attributes.x_resolution;
   1.401 -      y_resolution = input_attributes.y_resolution;
   1.402 -    }
   1.403 -
   1.404 -  if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   1.405 -    {
   1.406 -      dest_image_width  = image_length;
   1.407 -      dest_image_length = image_width;
   1.408 -      dest_x_resolution = y_resolution;
   1.409 -      dest_y_resolution = x_resolution;
   1.410 -      SWAP (double, width_points, height_points);  /* $$$ not yet set!!! */
   1.411 -    }
   1.412 -  else
   1.413 -    {
   1.414 -      dest_image_width = image_width;
   1.415 -      dest_image_length = image_length;
   1.416 -      dest_x_resolution = x_resolution;
   1.417 -      dest_y_resolution = y_resolution;
   1.418 -    }
   1.419 -
   1.420 -  rect.min.x = 0;
   1.421 -  rect.min.y = 0;
   1.422 -  rect.max.x = image_width;
   1.423 -  rect.max.y = image_length;
   1.424 -
   1.425 -  bitmap = create_bitmap (& rect);
   1.426 -
   1.427 -  if (! bitmap)
   1.428 -    {
   1.429 -      fprintf (stderr, "can't allocate bitmap\n");
   1.430 -      goto fail;
   1.431 -    }
   1.432 -
   1.433 -  for (row = 0; row < image_length; row++)
   1.434 -    if (1 != TIFFReadScanline (in,
   1.435 -			       bitmap->bits + row * bitmap->row_words,
   1.436 -			       row,
   1.437 -			       0))
   1.438 -      {
   1.439 -	fprintf (stderr, "can't read TIFF scanline\n");
   1.440 -	goto fail;
   1.441 -      }
   1.442 -
   1.443 -#ifdef TIFF_REVERSE_BITS
   1.444 -  reverse_bits ((uint8_t *) bitmap->bits,
   1.445 -		image_length * bitmap->row_words * sizeof (word_t));
   1.446 -#endif /* TIFF_REVERSE_BITS */
   1.447 -
   1.448 -#if 0
   1.449 -  if (input_attributes.has_page_size)
   1.450 -    bitmap = resize_bitmap (bitmap,
   1.451 -			    x_resolution,
   1.452 -			    y_resolution,
   1.453 -			    input_attributes);
   1.454 -#endif
   1.455 -
   1.456 -  rotate_bitmap (bitmap,
   1.457 -		 input_attributes);
   1.458 -
   1.459 -  width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
   1.460 -  height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
   1.461 -
   1.462 -  if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   1.463 -    {
   1.464 -      fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   1.465 -      goto fail;
   1.466 -    }
   1.467 -
   1.468 -  page = pdf_new_page (out->pdf, width_points, height_points);
   1.469 -
   1.470 -#if 0
   1.471 -  pdf_write_text (page);
   1.472 -#else
   1.473 -  pdf_write_g4_fax_image (page,
   1.474 -			  0, 0,  /* x, y */
   1.475 -			  width_points, height_points,
   1.476 -			  bitmap,
   1.477 -			  0, /* ImageMask */
   1.478 -			  0, 0, 0,  /* r, g, b */
   1.479 -			  0); /* BlackIs1 */
   1.480 -#endif
   1.481 -
   1.482 -  while (bookmarks)
   1.483 -    {
   1.484 -      /* $$$ need to handle level here */
   1.485 -      pdf_new_bookmark (NULL, bookmarks->name, 0, page);
   1.486 -      bookmarks = bookmarks->next;
   1.487 -    }
   1.488 -
   1.489 -  result = 1;
   1.490 -
   1.491 - fail:
   1.492 -  if (bitmap)
   1.493 -    free_bitmap (bitmap);
   1.494 -
   1.495 -  return (result);
   1.496 -}
   1.497 -
   1.498 -
   1.499 -#if 0
   1.500 -bool process_jpeg_page (int image,  /* range 1 .. n */
   1.501 -			input_attributes_t input_attributes,
   1.502 -			bookmark_t *bookmarks)
   1.503 -{
   1.504 -  int result = 0;
   1.505 -  FILE *f;
   1.506 -  pdf_page_handle page;
   1.507 -
   1.508 -  f = fopen (filename, "rb");
   1.509 -  if (! f)
   1.510 -    fatal ("error opening input file '%s'\n", filename);
   1.511 -
   1.512 -  page = pdf_new_page (out->pdf, width_points, height_points);
   1.513 -
   1.514 -  pdf_write_jpeg_image (page,
   1.515 -			0, 0,  /* x, y */
   1.516 -			width_points, height_points,
   1.517 -			f);
   1.518 -
   1.519 -  return (result);
   1.520 -}
   1.521 -#endif
   1.522 -
   1.523 -
   1.524 -bool process_page (int image,  /* range 1 .. n */
   1.525 -		   input_attributes_t input_attributes,
   1.526 -		   bookmark_t *bookmarks)
   1.527 -{
   1.528 -  int result = 0;
   1.529 -
   1.530 -  result = process_tiff_page (image, input_attributes, bookmarks);
   1.531 -
   1.532 -  return (result);
   1.533 -}
   1.534 -
   1.535 -
   1.536 -#define MAX_BOOKMARK_NAME_LEN 500
   1.537 -
   1.538 -
   1.539 -static int filename_length_without_suffix (char *in_fn)
   1.540 -{
   1.541 -  char *p;
   1.542 -  int len = strlen (in_fn);
   1.543 -
   1.544 -  p = strrchr (in_fn, '.');
   1.545 -  if (p && ((strcasecmp (p, ".tif") == 0) ||
   1.546 -	    (strcasecmp (p, ".tiff") == 0)))
   1.547 -    return (p - in_fn);
   1.548 -  return (len);
   1.549 -}
   1.550 -
   1.551 -
   1.552 -/* $$$ this function should ensure that it doesn't overflow the name string! */
   1.553 -static void generate_bookmark_name (char *name,
   1.554 -				    char *bookmark_fmt, 
   1.555 -				    char *in_fn,
   1.556 -				    int page)
   1.557 -{
   1.558 -  bool meta = 0;
   1.559 -  int len;
   1.560 -
   1.561 -  while (*bookmark_fmt)
   1.562 -    {
   1.563 -      if (meta)
   1.564 -	{
   1.565 -	  meta = 0;
   1.566 -	  switch (*bookmark_fmt)
   1.567 -	    {
   1.568 -	    case '%':
   1.569 -	      *(name++) = '%';
   1.570 -	      break;
   1.571 -	    case 'F':
   1.572 -	      len = filename_length_without_suffix (in_fn);
   1.573 -	      strncpy (name, in_fn, len);
   1.574 -	      name += len;
   1.575 -	      break;
   1.576 -	    case 'p':
   1.577 -	      sprintf (name, "%d", page);
   1.578 -	      name += strlen (name);
   1.579 -	      break;
   1.580 -	    default:
   1.581 -	      break;
   1.582 -	    }
   1.583 -	}
   1.584 -      else
   1.585 -	switch (*bookmark_fmt)
   1.586 -	  {
   1.587 -	  case '%':
   1.588 -	    meta = 1;
   1.589 -	    break;
   1.590 -	  default:
   1.591 -	    *(name++) = *bookmark_fmt;
   1.592 -	  }
   1.593 -      bookmark_fmt++;
   1.594 -    }
   1.595 -  *name = '\0';
   1.596 -}
   1.597 -
   1.598 -
   1.599 -void main_args (char *out_fn,
   1.600 -		int inf_count,
   1.601 -		char **in_fn,
   1.602 -		char *bookmark_fmt)
   1.603 -{
   1.604 -  int i, ip;
   1.605 -  input_attributes_t input_attributes;
   1.606 -  pdf_file_attributes_t output_attributes;
   1.607 -  bookmark_t bookmark;
   1.608 -  char bookmark_name [MAX_BOOKMARK_NAME_LEN];
   1.609 -
   1.610 -  bookmark.next = NULL;
   1.611 -  bookmark.level = 1;
   1.612 -  bookmark.name = & bookmark_name [0];
   1.613 -
   1.614 -  memset (& input_attributes, 0, sizeof (input_attributes));
   1.615 -  memset (& output_attributes, 0, sizeof (output_attributes));
   1.616 -
   1.617 -  output_attributes.has_bookmarks = (bookmark_fmt != NULL);
   1.618 -
   1.619 -  if (! open_pdf_output_file (out_fn, & output_attributes))
   1.620 -    fatal (3, "error opening output file \"%s\"\n", out_fn);
   1.621 -  for (i = 0; i < inf_count; i++)
   1.622 -    {
   1.623 -      if (! open_tiff_input_file (in_fn [i]))
   1.624 -	fatal (3, "error opening input file \"%s\"\n", in_fn [i]);
   1.625 -      for (ip = 1;; ip++)
   1.626 -	{
   1.627 -	  fprintf (stderr, "processing page %d of file \"%s\"\r", ip, in_fn [i]);
   1.628 -	  if (bookmark_fmt)
   1.629 -	    generate_bookmark_name (& bookmark_name [0],
   1.630 -				    bookmark_fmt, 
   1.631 -				    in_fn [i],
   1.632 -				    ip);
   1.633 -	  if (! process_page (ip, input_attributes,
   1.634 -			      bookmark_fmt ? & bookmark : NULL))
   1.635 -	    fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
   1.636 -	  if (last_tiff_page ())
   1.637 -	    break;
   1.638 -	}
   1.639 -      if (verbose)
   1.640 -	fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]);
   1.641 -      if (! close_tiff_input_file ())
   1.642 -	fatal (3, "error closing input file \"%s\"\n", in_fn [i]);
   1.643 -    }
   1.644 -  if (! close_pdf_output_files ())
   1.645 -    fatal (3, "error closing output file \"%s\"\n", out_fn);
   1.646 -}
   1.647 -
   1.648 -
   1.649 -void main_spec (char *spec_fn)
   1.650 -{
   1.651 -  if (! parse_spec_file (spec_fn))
   1.652 -    fatal (2, "error parsing spec file\n");
   1.653 -  if (! process_specs ())
   1.654 -    fatal (3, "error processing spec file\n");
   1.655 -}
   1.656 -
   1.657 -
   1.658 -int main (int argc, char *argv[])
   1.659 -{
   1.660 -  char *spec_fn = NULL;
   1.661 -  char *out_fn = NULL;
   1.662 -  char *bookmark_fmt = NULL;
   1.663 -  int inf_count = 0;
   1.664 -  char *in_fn [MAX_INPUT_FILES];
   1.665 -
   1.666 -  progname = argv [0];
   1.667 -
   1.668 -  pdf_init ();
   1.669 -
   1.670 -  while (--argc)
   1.671 -    {
   1.672 -      if (argv [1][0] == '-')
   1.673 -	{
   1.674 -	  if (strcmp (argv [1], "-v") == 0)
   1.675 -	    verbose++;
   1.676 -	  else if (strcmp (argv [1], "-o") == 0)
   1.677 -	    {
   1.678 -	      if (argc)
   1.679 -		{
   1.680 -		  argc--;
   1.681 -		  argv++;
   1.682 -		  out_fn = argv [1];
   1.683 -		}
   1.684 -	      else
   1.685 -		fatal (1, "missing filename after \"-o\" option\n");
   1.686 -	    }
   1.687 -	  else if (strcmp (argv [1], "-s") == 0)
   1.688 -	    {
   1.689 -	      if (argc)
   1.690 -		{
   1.691 -		  argc--;
   1.692 -		  argv++;
   1.693 -		  spec_fn = argv [1];
   1.694 -		}
   1.695 -	      else
   1.696 -		fatal (1, "missing filename after \"-s\" option\n");
   1.697 -	    }
   1.698 -	  else if (strcmp (argv [1], "-b") == 0)
   1.699 -	    {
   1.700 -	      if (argc)
   1.701 -		{
   1.702 -		  argc--;
   1.703 -		  argv++;
   1.704 -		  bookmark_fmt = argv [1];
   1.705 -		}
   1.706 -	      else
   1.707 -		fatal (1, "missing format string after \"-b\" option\n");
   1.708 -	    }
   1.709 -	  else
   1.710 -	    fatal (1, "unrecognized option \"%s\"\n", argv [1]);
   1.711 -	}
   1.712 -      else if (inf_count < MAX_INPUT_FILES)
   1.713 -	in_fn [inf_count++] = argv [1];
   1.714 -      else
   1.715 -	fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES);
   1.716 -      argv++;
   1.717 -    }
   1.718 -
   1.719 -  if (! ((! out_fn) ^ (! spec_fn)))
   1.720 -    fatal (1, "either a spec file or an output file (but not both) must be specified\n");
   1.721 -
   1.722 -  if (out_fn && ! inf_count)
   1.723 -    fatal (1, "no input files specified\n");
   1.724 -
   1.725 -  if (spec_fn && inf_count)
   1.726 -    fatal (1, "if spec file is provided, input files can't be specified as arguments\n");
   1.727 -
   1.728 -  if (spec_fn)
   1.729 -    main_spec (spec_fn);
   1.730 -  else
   1.731 -    main_args (out_fn, inf_count, in_fn, bookmark_fmt);
   1.732 -  
   1.733 -  close_tiff_input_file ();
   1.734 -  close_pdf_output_files ();
   1.735 -  exit (0);
   1.736 -}