Mon, 31 Dec 2001 08:25:04 +0000
added input and output attribute structures as arguments to process_page().
1 /*
2 * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
3 * Main program
4 * $Id: tumble.c,v 1.6 2001/12/31 00:25:04 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 "semantics.h"
32 #include "parser.tab.h"
33 #include "tiff2pdf.h"
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 input_attributes_t input_attributes,
85 output_attributes_t output_attributes)
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 int main (int argc, char *argv[])
213 {
214 int result = 0;
216 panda_init ();
218 if (argc != 2)
219 {
220 fprintf (stderr, "usage: %s spec\n", argv [0]);
221 result = 1;
222 goto fail;
223 }
225 if (! parse_spec_file (argv [1]))
226 goto fail;
228 fail:
229 close_tiff_input_file ();
230 close_pdf_output_file ();
231 return (result);
232 }