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