t2p.c

changeset 49
be20d7e8466f
parent 48
3d0be1c1c1b2
child 50
efbc5e0957df
     1.1 --- a/t2p.c	Mon Aug 26 06:03:55 2002 +0000
     1.2 +++ b/t2p.c	Tue Jan 21 18:30:49 2003 +0000
     1.3 @@ -4,8 +4,8 @@
     1.4   *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.5   *
     1.6   * Main program
     1.7 - * $Id: t2p.c,v 1.19 2002/08/25 22:02:31 eric Exp $
     1.8 - * Copyright 2001 Eric Smith <eric@brouhaha.com>
     1.9 + * $Id: t2p.c,v 1.20 2003/01/21 10:30:49 eric Exp $
    1.10 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.11   *
    1.12   * This program is free software; you can redistribute it and/or modify
    1.13   * it under the terms of the GNU General Public License version 2 as
    1.14 @@ -23,6 +23,7 @@
    1.15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */
    1.16  
    1.17  
    1.18 +#include <stdarg.h>
    1.19  #include <stdbool.h>
    1.20  #include <stdint.h>
    1.21  #include <stdio.h>
    1.22 @@ -41,6 +42,8 @@
    1.23  #include "t2p.h"
    1.24  
    1.25  
    1.26 +#define MAX_INPUT_FILES 5000
    1.27 +
    1.28  #define POINTS_PER_INCH 72
    1.29  
    1.30  /* page size limited by Acrobat Reader to 45 inches on a side */
    1.31 @@ -56,6 +59,9 @@
    1.32  } output_file_t;
    1.33  
    1.34  
    1.35 +int verbose;
    1.36 +
    1.37 +
    1.38  char *in_filename;
    1.39  TIFF *in;
    1.40  output_file_t *output_files;
    1.41 @@ -63,6 +69,46 @@
    1.42  /* panda_pdf *out; */
    1.43  
    1.44  
    1.45 +char *progname;
    1.46 +
    1.47 +
    1.48 +bool close_tiff_input_file (void);
    1.49 +bool close_pdf_output_files (void);
    1.50 +
    1.51 +
    1.52 +void usage (void)
    1.53 +{
    1.54 +  fprintf (stderr, "usage:\n");
    1.55 +  fprintf (stderr, "    %s [options] -s spec\n", progname);
    1.56 +  fprintf (stderr, "    %s [options] <input.tif>... -o <output.pdf>\n", progname);
    1.57 +  fprintf (stderr, "options:\n");
    1.58 +  fprintf (stderr, "    -v   verbose\n");
    1.59 +}
    1.60 +
    1.61 +
    1.62 +/* generate fatal error message to stderr, doesn't return */
    1.63 +void fatal (int ret, char *format, ...)
    1.64 +{
    1.65 +  va_list ap;
    1.66 +
    1.67 +  fprintf (stderr, "fatal error");
    1.68 +  if (format)
    1.69 +    {
    1.70 +      fprintf (stderr, ": ");
    1.71 +      va_start (ap, format);
    1.72 +      vfprintf (stderr, format, ap);
    1.73 +      va_end (ap);
    1.74 +    }
    1.75 +  else
    1.76 +    fprintf (stderr, "\n");
    1.77 +  if (ret == 1)
    1.78 +    usage ();
    1.79 +  close_tiff_input_file ();
    1.80 +  close_pdf_output_files ();
    1.81 +  exit (ret);
    1.82 +}
    1.83 +
    1.84 +
    1.85  bool close_tiff_input_file (void)
    1.86  {
    1.87    if (in)
    1.88 @@ -75,6 +121,7 @@
    1.89    return (1);
    1.90  }
    1.91  
    1.92 +
    1.93  bool open_tiff_input_file (char *name)
    1.94  {
    1.95    if (in)
    1.96 @@ -226,6 +273,13 @@
    1.97  
    1.98  #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
    1.99  
   1.100 +
   1.101 +bool last_tiff_page (void)
   1.102 +{
   1.103 +  return (TIFFLastDirectory (in));
   1.104 +}
   1.105 +
   1.106 +
   1.107  bool process_page (int image,  /* range 1 .. n */
   1.108  		   input_attributes_t input_attributes,
   1.109  		   bookmark_t *bookmarks)
   1.110 @@ -473,33 +527,111 @@
   1.111  }
   1.112  
   1.113  
   1.114 +void main_args (char *out_fn, int inf_count, char **in_fn)
   1.115 +{
   1.116 +  int i, ip;
   1.117 +  input_attributes_t input_attributes;
   1.118 +  pdf_file_attributes_t output_attributes;
   1.119 +
   1.120 +  memset (& input_attributes, 0, sizeof (input_attributes));
   1.121 +  memset (& output_attributes, 0, sizeof (output_attributes));
   1.122 +
   1.123 +  if (! open_pdf_output_file (out_fn, & output_attributes))
   1.124 +    fatal (3, "error opening output file \"%s\"\n", out_fn);
   1.125 +  for (i = 0; i < inf_count; i++)
   1.126 +    {
   1.127 +      if (! open_tiff_input_file (in_fn [i]))
   1.128 +	fatal (3, "error opening input file \"%s\"\n", in_fn [i]);
   1.129 +      for (ip = 1;; ip++)
   1.130 +	{
   1.131 +	  if (! process_page (ip, input_attributes, NULL))
   1.132 +	    fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
   1.133 +	  if (last_tiff_page ())
   1.134 +	    break;
   1.135 +	}
   1.136 +      if (verbose)
   1.137 +	fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]);
   1.138 +      if (! close_tiff_input_file ())
   1.139 +	fatal (3, "error closing input file \"%s\"\n", in_fn [i]);
   1.140 +    }
   1.141 +  if (! close_pdf_output_files ())
   1.142 +    fatal (3, "error closing output file \"%s\"\n", out_fn);
   1.143 +}
   1.144 +
   1.145 +
   1.146 +void main_spec (char *spec_fn)
   1.147 +{
   1.148 +  if (! parse_spec_file (spec_fn))
   1.149 +    fatal (2, "error parsing spec file\n");
   1.150 +  if (! process_specs ())
   1.151 +    fatal (3, "error processing spec file\n");
   1.152 +}
   1.153 +
   1.154 +
   1.155  int main (int argc, char *argv[])
   1.156  {
   1.157 -  int result = 0;
   1.158 +  char *spec_fn = NULL;
   1.159 +  char *out_fn = NULL;
   1.160 +  int inf_count = 0;
   1.161 +  char *in_fn [MAX_INPUT_FILES];
   1.162 +
   1.163 +  progname = argv [0];
   1.164  
   1.165    panda_init ();
   1.166  
   1.167 -  if (argc != 2)
   1.168 +  while (--argc)
   1.169      {
   1.170 -      fprintf (stderr, "usage: %s spec\n", argv [0]);
   1.171 -      result = 1;
   1.172 -      goto fail;
   1.173 +      if (argv [1][0] == '-')
   1.174 +	{
   1.175 +	  if (strcmp (argv [1], "-v") == 0)
   1.176 +	    verbose++;
   1.177 +	  else if (strcmp (argv [1], "-o") == 0)
   1.178 +	    {
   1.179 +	      if (argc)
   1.180 +		{
   1.181 +		  argc--;
   1.182 +		  argv++;
   1.183 +		  out_fn = argv [1];
   1.184 +		}
   1.185 +	      else
   1.186 +		fatal (1, "missing filename after \"-o\" option\n");
   1.187 +	    }
   1.188 +	  else if (strcmp (argv [1], "-s") == 0)
   1.189 +	    {
   1.190 +	      if (argc)
   1.191 +		{
   1.192 +		  argc--;
   1.193 +		  argv++;
   1.194 +		  spec_fn = argv [1];
   1.195 +		}
   1.196 +	      else
   1.197 +		fatal (1, "missing filename after \"-s\" option\n");
   1.198 +	    }
   1.199 +	  else
   1.200 +	    fatal (1, "unrecognized option \"%s\"\n", argv [1]);
   1.201 +	}
   1.202 +      else if (inf_count < MAX_INPUT_FILES)
   1.203 +	in_fn [inf_count++] = argv [1];
   1.204 +      else
   1.205 +	fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES);
   1.206 +      argv++;
   1.207      }
   1.208  
   1.209 -  if (! parse_spec_file (argv [1]))
   1.210 -    {
   1.211 -      result = 2;
   1.212 -      goto fail;
   1.213 -    }
   1.214 +  if (! ((! out_fn) ^ (! spec_fn)))
   1.215 +    fatal (1, "either a spec file or an output file (but not both) must be specified\n");
   1.216 +
   1.217 +  if (out_fn && ! inf_count)
   1.218 +    fatal (1, "no input files specified\n");
   1.219  
   1.220 -  if (! process_specs ())
   1.221 -    {
   1.222 -      result = 3;
   1.223 -      goto fail;
   1.224 -    }
   1.225 +  if (spec_fn && inf_count)
   1.226 +    fatal (1, "if spec file is provided, input files can't be specified as arguments\n");
   1.227 +
   1.228 +  if (spec_fn)
   1.229 +    main_spec (spec_fn);
   1.230 +  else
   1.231 +    main_args (out_fn, inf_count, in_fn);
   1.232    
   1.233 - fail:
   1.234    close_tiff_input_file ();
   1.235    close_pdf_output_files ();
   1.236 -  return (result);
   1.237 +  exit (0);
   1.238  }