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