Tue, 21 Jan 2003 18:30:49 +0000
added command-line mode
semantics.c | file | annotate | diff | revisions | |
t2p.c | file | annotate | diff | revisions | |
t2p.h | file | annotate | diff | revisions | |
tumble.c | file | annotate | diff | revisions | |
tumble.h | file | annotate | diff | revisions |
1.1 diff -r 3d0be1c1c1b2 -r be20d7e8466f semantics.c 1.2 --- a/semantics.c Mon Aug 26 06:03:55 2002 +0000 1.3 +++ b/semantics.c Tue Jan 21 18:30:49 2003 +0000 1.4 @@ -1,3 +1,28 @@ 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 + * Semantic routines for spec file parser 1.11 + * $Id: semantics.c,v 1.16 2003/01/21 10:30:49 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 #include <stdbool.h> 1.31 #include <stdint.h> 1.32 #include <stdlib.h> 1.33 @@ -674,9 +699,10 @@ 1.34 parity, 1.35 & input_attributes.page_size); 1.36 1.37 - process_page (image->range.first + i, 1.38 - input_attributes, 1.39 - page->bookmark_list); 1.40 + if (! process_page (image->range.first + i, 1.41 + input_attributes, 1.42 + page->bookmark_list)) 1.43 + return (0); 1.44 i++; 1.45 p++; 1.46 page_index++;
2.1 diff -r 3d0be1c1c1b2 -r be20d7e8466f t2p.c 2.2 --- a/t2p.c Mon Aug 26 06:03:55 2002 +0000 2.3 +++ b/t2p.c Tue Jan 21 18:30:49 2003 +0000 2.4 @@ -4,8 +4,8 @@ 2.5 * will be compressed using ITU-T T.6 (G4) fax encoding. 2.6 * 2.7 * Main program 2.8 - * $Id: t2p.c,v 1.19 2002/08/25 22:02:31 eric Exp $ 2.9 - * Copyright 2001 Eric Smith <eric@brouhaha.com> 2.10 + * $Id: t2p.c,v 1.20 2003/01/21 10:30:49 eric Exp $ 2.11 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 2.12 * 2.13 * This program is free software; you can redistribute it and/or modify 2.14 * it under the terms of the GNU General Public License version 2 as 2.15 @@ -23,6 +23,7 @@ 2.16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */ 2.17 2.18 2.19 +#include <stdarg.h> 2.20 #include <stdbool.h> 2.21 #include <stdint.h> 2.22 #include <stdio.h> 2.23 @@ -41,6 +42,8 @@ 2.24 #include "t2p.h" 2.25 2.26 2.27 +#define MAX_INPUT_FILES 5000 2.28 + 2.29 #define POINTS_PER_INCH 72 2.30 2.31 /* page size limited by Acrobat Reader to 45 inches on a side */ 2.32 @@ -56,6 +59,9 @@ 2.33 } output_file_t; 2.34 2.35 2.36 +int verbose; 2.37 + 2.38 + 2.39 char *in_filename; 2.40 TIFF *in; 2.41 output_file_t *output_files; 2.42 @@ -63,6 +69,46 @@ 2.43 /* panda_pdf *out; */ 2.44 2.45 2.46 +char *progname; 2.47 + 2.48 + 2.49 +bool close_tiff_input_file (void); 2.50 +bool close_pdf_output_files (void); 2.51 + 2.52 + 2.53 +void usage (void) 2.54 +{ 2.55 + fprintf (stderr, "usage:\n"); 2.56 + fprintf (stderr, " %s [options] -s spec\n", progname); 2.57 + fprintf (stderr, " %s [options] <input.tif>... -o <output.pdf>\n", progname); 2.58 + fprintf (stderr, "options:\n"); 2.59 + fprintf (stderr, " -v verbose\n"); 2.60 +} 2.61 + 2.62 + 2.63 +/* generate fatal error message to stderr, doesn't return */ 2.64 +void fatal (int ret, char *format, ...) 2.65 +{ 2.66 + va_list ap; 2.67 + 2.68 + fprintf (stderr, "fatal error"); 2.69 + if (format) 2.70 + { 2.71 + fprintf (stderr, ": "); 2.72 + va_start (ap, format); 2.73 + vfprintf (stderr, format, ap); 2.74 + va_end (ap); 2.75 + } 2.76 + else 2.77 + fprintf (stderr, "\n"); 2.78 + if (ret == 1) 2.79 + usage (); 2.80 + close_tiff_input_file (); 2.81 + close_pdf_output_files (); 2.82 + exit (ret); 2.83 +} 2.84 + 2.85 + 2.86 bool close_tiff_input_file (void) 2.87 { 2.88 if (in) 2.89 @@ -75,6 +121,7 @@ 2.90 return (1); 2.91 } 2.92 2.93 + 2.94 bool open_tiff_input_file (char *name) 2.95 { 2.96 if (in) 2.97 @@ -226,6 +273,13 @@ 2.98 2.99 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 2.100 2.101 + 2.102 +bool last_tiff_page (void) 2.103 +{ 2.104 + return (TIFFLastDirectory (in)); 2.105 +} 2.106 + 2.107 + 2.108 bool process_page (int image, /* range 1 .. n */ 2.109 input_attributes_t input_attributes, 2.110 bookmark_t *bookmarks) 2.111 @@ -473,33 +527,111 @@ 2.112 } 2.113 2.114 2.115 +void main_args (char *out_fn, int inf_count, char **in_fn) 2.116 +{ 2.117 + int i, ip; 2.118 + input_attributes_t input_attributes; 2.119 + pdf_file_attributes_t output_attributes; 2.120 + 2.121 + memset (& input_attributes, 0, sizeof (input_attributes)); 2.122 + memset (& output_attributes, 0, sizeof (output_attributes)); 2.123 + 2.124 + if (! open_pdf_output_file (out_fn, & output_attributes)) 2.125 + fatal (3, "error opening output file \"%s\"\n", out_fn); 2.126 + for (i = 0; i < inf_count; i++) 2.127 + { 2.128 + if (! open_tiff_input_file (in_fn [i])) 2.129 + fatal (3, "error opening input file \"%s\"\n", in_fn [i]); 2.130 + for (ip = 1;; ip++) 2.131 + { 2.132 + if (! process_page (ip, input_attributes, NULL)) 2.133 + fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]); 2.134 + if (last_tiff_page ()) 2.135 + break; 2.136 + } 2.137 + if (verbose) 2.138 + fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]); 2.139 + if (! close_tiff_input_file ()) 2.140 + fatal (3, "error closing input file \"%s\"\n", in_fn [i]); 2.141 + } 2.142 + if (! close_pdf_output_files ()) 2.143 + fatal (3, "error closing output file \"%s\"\n", out_fn); 2.144 +} 2.145 + 2.146 + 2.147 +void main_spec (char *spec_fn) 2.148 +{ 2.149 + if (! parse_spec_file (spec_fn)) 2.150 + fatal (2, "error parsing spec file\n"); 2.151 + if (! process_specs ()) 2.152 + fatal (3, "error processing spec file\n"); 2.153 +} 2.154 + 2.155 + 2.156 int main (int argc, char *argv[]) 2.157 { 2.158 - int result = 0; 2.159 + char *spec_fn = NULL; 2.160 + char *out_fn = NULL; 2.161 + int inf_count = 0; 2.162 + char *in_fn [MAX_INPUT_FILES]; 2.163 + 2.164 + progname = argv [0]; 2.165 2.166 panda_init (); 2.167 2.168 - if (argc != 2) 2.169 + while (--argc) 2.170 { 2.171 - fprintf (stderr, "usage: %s spec\n", argv [0]); 2.172 - result = 1; 2.173 - goto fail; 2.174 + if (argv [1][0] == '-') 2.175 + { 2.176 + if (strcmp (argv [1], "-v") == 0) 2.177 + verbose++; 2.178 + else if (strcmp (argv [1], "-o") == 0) 2.179 + { 2.180 + if (argc) 2.181 + { 2.182 + argc--; 2.183 + argv++; 2.184 + out_fn = argv [1]; 2.185 + } 2.186 + else 2.187 + fatal (1, "missing filename after \"-o\" option\n"); 2.188 + } 2.189 + else if (strcmp (argv [1], "-s") == 0) 2.190 + { 2.191 + if (argc) 2.192 + { 2.193 + argc--; 2.194 + argv++; 2.195 + spec_fn = argv [1]; 2.196 + } 2.197 + else 2.198 + fatal (1, "missing filename after \"-s\" option\n"); 2.199 + } 2.200 + else 2.201 + fatal (1, "unrecognized option \"%s\"\n", argv [1]); 2.202 + } 2.203 + else if (inf_count < MAX_INPUT_FILES) 2.204 + in_fn [inf_count++] = argv [1]; 2.205 + else 2.206 + fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES); 2.207 + argv++; 2.208 } 2.209 2.210 - if (! parse_spec_file (argv [1])) 2.211 - { 2.212 - result = 2; 2.213 - goto fail; 2.214 - } 2.215 + if (! ((! out_fn) ^ (! spec_fn))) 2.216 + fatal (1, "either a spec file or an output file (but not both) must be specified\n"); 2.217 + 2.218 + if (out_fn && ! inf_count) 2.219 + fatal (1, "no input files specified\n"); 2.220 2.221 - if (! process_specs ()) 2.222 - { 2.223 - result = 3; 2.224 - goto fail; 2.225 - } 2.226 + if (spec_fn && inf_count) 2.227 + fatal (1, "if spec file is provided, input files can't be specified as arguments\n"); 2.228 + 2.229 + if (spec_fn) 2.230 + main_spec (spec_fn); 2.231 + else 2.232 + main_args (out_fn, inf_count, in_fn); 2.233 2.234 - fail: 2.235 close_tiff_input_file (); 2.236 close_pdf_output_files (); 2.237 - return (result); 2.238 + exit (0); 2.239 }
3.1 diff -r 3d0be1c1c1b2 -r be20d7e8466f t2p.h 3.2 --- a/t2p.h Mon Aug 26 06:03:55 2002 +0000 3.3 +++ b/t2p.h Tue Jan 21 18:30:49 2003 +0000 3.4 @@ -1,3 +1,30 @@ 3.5 +/* 3.6 + * t2p: Create a PDF file from the contents of one or more TIFF 3.7 + * bilevel image files. The images in the resulting PDF file 3.8 + * will be compressed using ITU-T T.6 (G4) fax encoding. 3.9 + * 3.10 + * $Id: t2p.h,v 1.11 2003/01/21 10:30:49 eric Exp $ 3.11 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 3.12 + * 3.13 + * This program is free software; you can redistribute it and/or modify 3.14 + * it under the terms of the GNU General Public License version 2 as 3.15 + * published by the Free Software Foundation. Note that permission is 3.16 + * not granted to redistribute this program under the terms of any 3.17 + * other version of the General Public License. 3.18 + * 3.19 + * This program is distributed in the hope that it will be useful, 3.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 3.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3.22 + * GNU General Public License for more details. 3.23 + * 3.24 + * You should have received a copy of the GNU General Public License 3.25 + * along with this program; if not, write to the Free Software 3.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */ 3.27 + 3.28 + 3.29 +extern int verbose; 3.30 + 3.31 + 3.32 typedef struct 3.33 { 3.34 bool has_resolution; 3.35 @@ -36,6 +63,8 @@ 3.36 int base, 3.37 page_label_t *page_label); 3.38 3.39 + 3.40 + 3.41 bool process_page (int image, /* range 1 .. n */ 3.42 input_attributes_t input_attributes, 3.43 bookmark_t *bookmarks);
4.1 diff -r 3d0be1c1c1b2 -r be20d7e8466f tumble.c 4.2 --- a/tumble.c Mon Aug 26 06:03:55 2002 +0000 4.3 +++ b/tumble.c Tue Jan 21 18:30:49 2003 +0000 4.4 @@ -4,8 +4,8 @@ 4.5 * will be compressed using ITU-T T.6 (G4) fax encoding. 4.6 * 4.7 * Main program 4.8 - * $Id: tumble.c,v 1.19 2002/08/25 22:02:31 eric Exp $ 4.9 - * Copyright 2001 Eric Smith <eric@brouhaha.com> 4.10 + * $Id: tumble.c,v 1.20 2003/01/21 10:30:49 eric Exp $ 4.11 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 4.12 * 4.13 * This program is free software; you can redistribute it and/or modify 4.14 * it under the terms of the GNU General Public License version 2 as 4.15 @@ -23,6 +23,7 @@ 4.16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */ 4.17 4.18 4.19 +#include <stdarg.h> 4.20 #include <stdbool.h> 4.21 #include <stdint.h> 4.22 #include <stdio.h> 4.23 @@ -41,6 +42,8 @@ 4.24 #include "t2p.h" 4.25 4.26 4.27 +#define MAX_INPUT_FILES 5000 4.28 + 4.29 #define POINTS_PER_INCH 72 4.30 4.31 /* page size limited by Acrobat Reader to 45 inches on a side */ 4.32 @@ -56,6 +59,9 @@ 4.33 } output_file_t; 4.34 4.35 4.36 +int verbose; 4.37 + 4.38 + 4.39 char *in_filename; 4.40 TIFF *in; 4.41 output_file_t *output_files; 4.42 @@ -63,6 +69,46 @@ 4.43 /* panda_pdf *out; */ 4.44 4.45 4.46 +char *progname; 4.47 + 4.48 + 4.49 +bool close_tiff_input_file (void); 4.50 +bool close_pdf_output_files (void); 4.51 + 4.52 + 4.53 +void usage (void) 4.54 +{ 4.55 + fprintf (stderr, "usage:\n"); 4.56 + fprintf (stderr, " %s [options] -s spec\n", progname); 4.57 + fprintf (stderr, " %s [options] <input.tif>... -o <output.pdf>\n", progname); 4.58 + fprintf (stderr, "options:\n"); 4.59 + fprintf (stderr, " -v verbose\n"); 4.60 +} 4.61 + 4.62 + 4.63 +/* generate fatal error message to stderr, doesn't return */ 4.64 +void fatal (int ret, char *format, ...) 4.65 +{ 4.66 + va_list ap; 4.67 + 4.68 + fprintf (stderr, "fatal error"); 4.69 + if (format) 4.70 + { 4.71 + fprintf (stderr, ": "); 4.72 + va_start (ap, format); 4.73 + vfprintf (stderr, format, ap); 4.74 + va_end (ap); 4.75 + } 4.76 + else 4.77 + fprintf (stderr, "\n"); 4.78 + if (ret == 1) 4.79 + usage (); 4.80 + close_tiff_input_file (); 4.81 + close_pdf_output_files (); 4.82 + exit (ret); 4.83 +} 4.84 + 4.85 + 4.86 bool close_tiff_input_file (void) 4.87 { 4.88 if (in) 4.89 @@ -75,6 +121,7 @@ 4.90 return (1); 4.91 } 4.92 4.93 + 4.94 bool open_tiff_input_file (char *name) 4.95 { 4.96 if (in) 4.97 @@ -226,6 +273,13 @@ 4.98 4.99 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 4.100 4.101 + 4.102 +bool last_tiff_page (void) 4.103 +{ 4.104 + return (TIFFLastDirectory (in)); 4.105 +} 4.106 + 4.107 + 4.108 bool process_page (int image, /* range 1 .. n */ 4.109 input_attributes_t input_attributes, 4.110 bookmark_t *bookmarks) 4.111 @@ -473,33 +527,111 @@ 4.112 } 4.113 4.114 4.115 +void main_args (char *out_fn, int inf_count, char **in_fn) 4.116 +{ 4.117 + int i, ip; 4.118 + input_attributes_t input_attributes; 4.119 + pdf_file_attributes_t output_attributes; 4.120 + 4.121 + memset (& input_attributes, 0, sizeof (input_attributes)); 4.122 + memset (& output_attributes, 0, sizeof (output_attributes)); 4.123 + 4.124 + if (! open_pdf_output_file (out_fn, & output_attributes)) 4.125 + fatal (3, "error opening output file \"%s\"\n", out_fn); 4.126 + for (i = 0; i < inf_count; i++) 4.127 + { 4.128 + if (! open_tiff_input_file (in_fn [i])) 4.129 + fatal (3, "error opening input file \"%s\"\n", in_fn [i]); 4.130 + for (ip = 1;; ip++) 4.131 + { 4.132 + if (! process_page (ip, input_attributes, NULL)) 4.133 + fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]); 4.134 + if (last_tiff_page ()) 4.135 + break; 4.136 + } 4.137 + if (verbose) 4.138 + fprintf (stderr, "processed %d pages of input file \"%s\"\n", ip, in_fn [i]); 4.139 + if (! close_tiff_input_file ()) 4.140 + fatal (3, "error closing input file \"%s\"\n", in_fn [i]); 4.141 + } 4.142 + if (! close_pdf_output_files ()) 4.143 + fatal (3, "error closing output file \"%s\"\n", out_fn); 4.144 +} 4.145 + 4.146 + 4.147 +void main_spec (char *spec_fn) 4.148 +{ 4.149 + if (! parse_spec_file (spec_fn)) 4.150 + fatal (2, "error parsing spec file\n"); 4.151 + if (! process_specs ()) 4.152 + fatal (3, "error processing spec file\n"); 4.153 +} 4.154 + 4.155 + 4.156 int main (int argc, char *argv[]) 4.157 { 4.158 - int result = 0; 4.159 + char *spec_fn = NULL; 4.160 + char *out_fn = NULL; 4.161 + int inf_count = 0; 4.162 + char *in_fn [MAX_INPUT_FILES]; 4.163 + 4.164 + progname = argv [0]; 4.165 4.166 panda_init (); 4.167 4.168 - if (argc != 2) 4.169 + while (--argc) 4.170 { 4.171 - fprintf (stderr, "usage: %s spec\n", argv [0]); 4.172 - result = 1; 4.173 - goto fail; 4.174 + if (argv [1][0] == '-') 4.175 + { 4.176 + if (strcmp (argv [1], "-v") == 0) 4.177 + verbose++; 4.178 + else if (strcmp (argv [1], "-o") == 0) 4.179 + { 4.180 + if (argc) 4.181 + { 4.182 + argc--; 4.183 + argv++; 4.184 + out_fn = argv [1]; 4.185 + } 4.186 + else 4.187 + fatal (1, "missing filename after \"-o\" option\n"); 4.188 + } 4.189 + else if (strcmp (argv [1], "-s") == 0) 4.190 + { 4.191 + if (argc) 4.192 + { 4.193 + argc--; 4.194 + argv++; 4.195 + spec_fn = argv [1]; 4.196 + } 4.197 + else 4.198 + fatal (1, "missing filename after \"-s\" option\n"); 4.199 + } 4.200 + else 4.201 + fatal (1, "unrecognized option \"%s\"\n", argv [1]); 4.202 + } 4.203 + else if (inf_count < MAX_INPUT_FILES) 4.204 + in_fn [inf_count++] = argv [1]; 4.205 + else 4.206 + fatal (1, "exceeded maximum of %d input files\n", MAX_INPUT_FILES); 4.207 + argv++; 4.208 } 4.209 4.210 - if (! parse_spec_file (argv [1])) 4.211 - { 4.212 - result = 2; 4.213 - goto fail; 4.214 - } 4.215 + if (! ((! out_fn) ^ (! spec_fn))) 4.216 + fatal (1, "either a spec file or an output file (but not both) must be specified\n"); 4.217 + 4.218 + if (out_fn && ! inf_count) 4.219 + fatal (1, "no input files specified\n"); 4.220 4.221 - if (! process_specs ()) 4.222 - { 4.223 - result = 3; 4.224 - goto fail; 4.225 - } 4.226 + if (spec_fn && inf_count) 4.227 + fatal (1, "if spec file is provided, input files can't be specified as arguments\n"); 4.228 + 4.229 + if (spec_fn) 4.230 + main_spec (spec_fn); 4.231 + else 4.232 + main_args (out_fn, inf_count, in_fn); 4.233 4.234 - fail: 4.235 close_tiff_input_file (); 4.236 close_pdf_output_files (); 4.237 - return (result); 4.238 + exit (0); 4.239 }
5.1 diff -r 3d0be1c1c1b2 -r be20d7e8466f tumble.h 5.2 --- a/tumble.h Mon Aug 26 06:03:55 2002 +0000 5.3 +++ b/tumble.h Tue Jan 21 18:30:49 2003 +0000 5.4 @@ -1,3 +1,30 @@ 5.5 +/* 5.6 + * t2p: Create a PDF file from the contents of one or more TIFF 5.7 + * bilevel image files. The images in the resulting PDF file 5.8 + * will be compressed using ITU-T T.6 (G4) fax encoding. 5.9 + * 5.10 + * $Id: tumble.h,v 1.11 2003/01/21 10:30:49 eric Exp $ 5.11 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 5.12 + * 5.13 + * This program is free software; you can redistribute it and/or modify 5.14 + * it under the terms of the GNU General Public License version 2 as 5.15 + * published by the Free Software Foundation. Note that permission is 5.16 + * not granted to redistribute this program under the terms of any 5.17 + * other version of the General Public License. 5.18 + * 5.19 + * This program is distributed in the hope that it will be useful, 5.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 5.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5.22 + * GNU General Public License for more details. 5.23 + * 5.24 + * You should have received a copy of the GNU General Public License 5.25 + * along with this program; if not, write to the Free Software 5.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */ 5.27 + 5.28 + 5.29 +extern int verbose; 5.30 + 5.31 + 5.32 typedef struct 5.33 { 5.34 bool has_resolution; 5.35 @@ -36,6 +63,8 @@ 5.36 int base, 5.37 page_label_t *page_label); 5.38 5.39 + 5.40 + 5.41 bool process_page (int image, /* range 1 .. n */ 5.42 input_attributes_t input_attributes, 5.43 bookmark_t *bookmarks);