Sun, 30 Dec 2001 01:54:43 +0000
*** empty log message ***
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.2 2001/12/29 10:59:47 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 FILE *yyin;
36 TIFF *in;
37 panda_pdf *out;
40 boolean close_tiff_input_file (void)
41 {
42 if (in)
43 TIFFClose (in);
44 in = NULL;
45 return (1);
46 }
48 boolean open_tiff_input_file (char *name)
49 {
50 if (in)
51 close_tiff_input_file ();
52 in = TIFFOpen (name, "r");
53 if (! in)
54 {
55 fprintf (stderr, "can't open input file '%s'\n", name);
56 return (0);
57 }
58 return (1);
59 }
62 boolean close_pdf_output_file (void)
63 {
64 if (out)
65 panda_close (out);
66 out = NULL;
67 return (1);
68 }
70 boolean open_pdf_output_file (char *name)
71 {
72 if (out)
73 close_pdf_output_file ();
74 out = panda_open (name, "w");
75 if (! out)
76 {
77 return (0);
78 }
79 return (1);
80 }
83 boolean process_page (int image) /* range 1 .. n */
84 {
85 u32 image_length, image_width;
86 #ifdef CHECK_DEPTH
87 u32 image_depth;
88 #endif
89 u16 bits_per_sample;
90 u16 planar_config;
91 u16 resolution_unit;
92 float x_resolution, y_resolution;
94 char *buffer;
95 u32 row;
97 if (! TIFFSetDirectory (in, image - 1))
98 {
99 fprintf (stderr, "can't find page %d of input file\n", image);
100 goto fail;
101 }
102 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
103 {
104 fprintf (stderr, "can't get image length\n");
105 goto fail;
106 }
107 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
108 {
109 fprintf (stderr, "can't get image width\n");
110 goto fail;
111 }
112 #ifdef CHECK_DEPTH
113 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
114 {
115 fprintf (stderr, "can't get image depth\n");
116 goto fail;
117 }
118 #endif
120 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
121 {
122 fprintf (stderr, "can't get bits per sample\n");
123 goto fail;
124 }
126 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
127 planar_config = 1;
129 printf ("image length %u width %u, "
130 #ifdef CHECK_DEPTH
131 "depth %u, "
132 #endif
133 "planar config %u\n",
134 image_length, image_width,
135 #ifdef CHECK_DEPTH
136 image_depth,
137 #endif
138 planar_config);
140 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
141 resolution_unit = 2;
142 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
143 x_resolution = 300;
144 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
145 y_resolution = 300;
147 printf ("resolution unit %u, x resolution %f, y resolution %f\n",
148 resolution_unit, x_resolution, y_resolution);
150 #ifdef CHECK_DEPTH
151 if (image_depth != 1)
152 {
153 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
154 goto fail;
155 }
156 #endif
158 if (bits_per_sample != 1)
159 {
160 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
161 goto fail;
162 }
164 if (planar_config != 1)
165 {
166 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
167 goto fail;
168 }
170 #if 0
171 TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
172 TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
173 TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
175 TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
177 TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
178 TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
179 TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
181 TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
182 TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
183 TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
184 #endif
186 buffer = _TIFFmalloc (TIFFScanlineSize (in));
187 if (! buffer)
188 {
189 fprintf (stderr, "failed to allocate buffer\n");
190 goto fail;
191 }
193 for (row = 0; row < image_length; row++)
194 {
195 TIFFReadScanline (in, buffer, row, 0);
196 #if 0
197 TIFFWriteScanline (out, buffer, row, 0);
198 #endif
199 }
201 _TIFFfree (buffer);
203 return (1);
205 fail:
206 return (0);
207 }
210 void yyerror (char *s)
211 {
212 fprintf (stderr, "%s\n", s);
213 }
216 int main (int argc, char *argv[])
217 {
218 int result = 0;
220 panda_init ();
222 if (argc != 2)
223 {
224 fprintf (stderr, "usage: %s spec\n", argv [0]);
225 result = 1;
226 goto fail;
227 }
229 yyin = fopen (argv [2], "r");
230 if (! yyin)
231 {
232 fprintf (stderr, "can't open spec file '%s'\n", argv [2]);
233 result = 3;
234 goto fail;
235 }
237 yyparse ();
239 fail:
240 fclose (yyin);
241 close_tiff_input_file ();
242 close_pdf_output_file ();
243 return (result);
244 }