Sat, 29 Dec 2001 17:45: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.1 2001/12/29 09:44:24 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 TIFF *in;
36 panda_pdf *out;
39 boolean close_tiff_input_file (void)
40 {
41 if (in)
42 TIFFClose (in);
43 in = NULL;
44 return (1);
45 }
47 boolean open_tiff_input_file (char *name)
48 {
49 if (in)
50 close_tiff_input_file ();
51 in = TIFFOpen (name, "r");
52 if (! in)
53 {
54 fprintf (stderr, "can't open input file '%s'\n", name);
55 return (0);
56 }
57 return (1);
58 }
61 boolean close_pdf_output_file (void)
62 {
63 if (out)
64 panda_close (out);
65 out = NULL;
66 return (1);
67 }
69 boolean open_pdf_output_file (char *name)
70 {
71 if (out)
72 close_pdf_output_file ();
73 out = panda_open (name, "w");
74 if (! out)
75 {
76 return (0);
77 }
78 return (1);
79 }
82 boolean process_page (int image) /* range 1 .. n */
83 {
84 u32 image_length, image_width;
85 #ifdef CHECK_DEPTH
86 u32 image_depth;
87 #endif
88 u16 bits_per_sample;
89 u16 planar_config;
90 u16 resolution_unit;
91 float x_resolution, y_resolution;
93 char *buffer;
94 u32 row;
96 if (! TIFFSetDirectory (in, image - 1))
97 {
98 fprintf (stderr, "can't find page %d of input file\n", image);
99 goto fail;
100 }
101 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
102 {
103 fprintf (stderr, "can't get image length\n");
104 goto fail;
105 }
106 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
107 {
108 fprintf (stderr, "can't get image width\n");
109 goto fail;
110 }
111 #ifdef CHECK_DEPTH
112 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
113 {
114 fprintf (stderr, "can't get image depth\n");
115 goto fail;
116 }
117 #endif
119 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
120 {
121 fprintf (stderr, "can't get bits per sample\n");
122 goto fail;
123 }
125 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
126 planar_config = 1;
128 printf ("image length %u width %u, "
129 #ifdef CHECK_DEPTH
130 "depth %u, "
131 #endif
132 "planar config %u\n",
133 image_length, image_width,
134 #ifdef CHECK_DEPTH
135 image_depth,
136 #endif
137 planar_config);
139 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
140 resolution_unit = 2;
141 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
142 x_resolution = 300;
143 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
144 y_resolution = 300;
146 printf ("resolution unit %u, x resolution %f, y resolution %f\n",
147 resolution_unit, x_resolution, y_resolution);
149 #ifdef CHECK_DEPTH
150 if (image_depth != 1)
151 {
152 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
153 goto fail;
154 }
155 #endif
157 if (bits_per_sample != 1)
158 {
159 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
160 goto fail;
161 }
163 if (planar_config != 1)
164 {
165 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
166 goto fail;
167 }
169 #if 0
170 TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
171 TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
172 TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
174 TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
176 TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
177 TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
178 TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
180 TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
181 TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
182 TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
183 #endif
185 buffer = _TIFFmalloc (TIFFScanlineSize (in));
186 if (! buffer)
187 {
188 fprintf (stderr, "failed to allocate buffer\n");
189 goto fail;
190 }
192 for (row = 0; row < image_length; row++)
193 {
194 TIFFReadScanline (in, buffer, row, 0);
195 #if 0
196 TIFFWriteScanline (out, buffer, row, 0);
197 #endif
198 }
200 _TIFFfree (buffer);
202 return (1);
204 fail:
205 return (0);
206 }
210 int main (int argc, char *argv[])
211 {
212 FILE *spec;
213 int result = 0;
215 panda_init ();
217 if (argc != 2)
218 {
219 fprintf (stderr, "usage: %s spec\n", argv [0]);
220 result = 1;
221 goto fail;
222 }
224 spec = fopen (argv [2], "r");
225 if (! spec)
226 {
227 fprintf (stderr, "can't open spec file '%s'\n", argv [2]);
228 result = 3;
229 goto fail;
230 }
232 yyparse ();
234 fail:
235 close_tiff_input_file ();
236 close_pdf_output_file ();
237 return (result);
238 }