Sun, 30 Dec 2001 04:16:46 +0000
..
1 /*
2 * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
3 * Main program
4 * $Id: t2p.c,v 1.3 2001/12/29 20:16:46 eric Exp $
5 * Copyright 2001 Eric Smith <eric@brouhaha.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. Note that permission is
10 * not granted to redistribute this program under the terms of any
11 * other version of the General Public License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
21 */
24 #include <stdio.h>
25 #include <tiffio.h>
26 #include <panda/functions.h>
27 #include <panda/constants.h>
29 #include "type.h"
30 #include "bitblt.h"
31 #include "parser.tab.h"
32 #include "tiff2pdf.h"
35 int line;
37 FILE *yyin;
38 TIFF *in;
39 panda_pdf *out;
42 boolean close_tiff_input_file (void)
43 {
44 if (in)
45 TIFFClose (in);
46 in = NULL;
47 return (1);
48 }
50 boolean open_tiff_input_file (char *name)
51 {
52 if (in)
53 close_tiff_input_file ();
54 in = TIFFOpen (name, "r");
55 if (! in)
56 {
57 fprintf (stderr, "can't open input file '%s'\n", name);
58 return (0);
59 }
60 return (1);
61 }
64 boolean close_pdf_output_file (void)
65 {
66 if (out)
67 panda_close (out);
68 out = NULL;
69 return (1);
70 }
72 boolean open_pdf_output_file (char *name)
73 {
74 if (out)
75 close_pdf_output_file ();
76 out = panda_open (name, "w");
77 if (! out)
78 {
79 return (0);
80 }
81 return (1);
82 }
85 boolean process_page (int image) /* range 1 .. n */
86 {
87 u32 image_length, image_width;
88 #ifdef CHECK_DEPTH
89 u32 image_depth;
90 #endif
91 u16 bits_per_sample;
92 u16 planar_config;
93 u16 resolution_unit;
94 float x_resolution, y_resolution;
96 char *buffer;
97 u32 row;
99 if (! TIFFSetDirectory (in, image - 1))
100 {
101 fprintf (stderr, "can't find page %d of input file\n", image);
102 goto fail;
103 }
104 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
105 {
106 fprintf (stderr, "can't get image length\n");
107 goto fail;
108 }
109 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
110 {
111 fprintf (stderr, "can't get image width\n");
112 goto fail;
113 }
114 #ifdef CHECK_DEPTH
115 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
116 {
117 fprintf (stderr, "can't get image depth\n");
118 goto fail;
119 }
120 #endif
122 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
123 {
124 fprintf (stderr, "can't get bits per sample\n");
125 goto fail;
126 }
128 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
129 planar_config = 1;
131 printf ("image length %u width %u, "
132 #ifdef CHECK_DEPTH
133 "depth %u, "
134 #endif
135 "planar config %u\n",
136 image_length, image_width,
137 #ifdef CHECK_DEPTH
138 image_depth,
139 #endif
140 planar_config);
142 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
143 resolution_unit = 2;
144 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
145 x_resolution = 300;
146 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
147 y_resolution = 300;
149 printf ("resolution unit %u, x resolution %f, y resolution %f\n",
150 resolution_unit, x_resolution, y_resolution);
152 #ifdef CHECK_DEPTH
153 if (image_depth != 1)
154 {
155 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
156 goto fail;
157 }
158 #endif
160 if (bits_per_sample != 1)
161 {
162 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
163 goto fail;
164 }
166 if (planar_config != 1)
167 {
168 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
169 goto fail;
170 }
172 #if 0
173 TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
174 TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
175 TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
177 TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
179 TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
180 TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
181 TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
183 TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
184 TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
185 TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
186 #endif
188 buffer = _TIFFmalloc (TIFFScanlineSize (in));
189 if (! buffer)
190 {
191 fprintf (stderr, "failed to allocate buffer\n");
192 goto fail;
193 }
195 for (row = 0; row < image_length; row++)
196 {
197 TIFFReadScanline (in, buffer, row, 0);
198 #if 0
199 TIFFWriteScanline (out, buffer, row, 0);
200 #endif
201 }
203 _TIFFfree (buffer);
205 return (1);
207 fail:
208 return (0);
209 }
212 void input_images (int first, int last)
213 {
214 if (first == last)
215 printf ("image %d\n", first);
216 else
217 printf ("iamges %d..%d\n", first, last);
218 }
220 void output_pages (int first, int last)
221 {
222 if (first == last)
223 printf ("page %d\n", first);
224 else
225 printf ("pages %d..%d\n", first, last);
226 }
229 void yyerror (char *s)
230 {
231 fprintf (stderr, "%d: %s\n", line, s);
232 }
235 int main (int argc, char *argv[])
236 {
237 int result = 0;
239 panda_init ();
241 if (argc != 2)
242 {
243 fprintf (stderr, "usage: %s spec\n", argv [0]);
244 result = 1;
245 goto fail;
246 }
248 yyin = fopen (argv [1], "r");
249 if (! yyin)
250 {
251 fprintf (stderr, "can't open spec file '%s'\n", argv [1]);
252 result = 3;
253 goto fail;
254 }
256 line = 1;
258 yyparse ();
260 fail:
261 if (yyin)
262 fclose (yyin);
263 close_tiff_input_file ();
264 close_pdf_output_file ();
265 return (result);
266 }