Mon, 31 Dec 2001 16:44:24 +0000
replaced page_number_format with page_label matching how PDF names
and stores them.
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.7 2001/12/31 08: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 "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 void process_page_numbers (int page_index,
84 int count,
85 int base,
86 page_label_t *page_label)
87 {
88 }
91 boolean process_page (int image, /* range 1 .. n */
92 input_attributes_t input_attributes,
93 bookmark_t *bookmarks)
94 {
95 u32 image_length, image_width;
96 #ifdef CHECK_DEPTH
97 u32 image_depth;
98 #endif
99 u16 bits_per_sample;
100 u16 planar_config;
101 u16 resolution_unit;
102 float x_resolution, y_resolution;
104 char *buffer;
105 u32 row;
107 if (! TIFFSetDirectory (in, image - 1))
108 {
109 fprintf (stderr, "can't find page %d of input file\n", image);
110 goto fail;
111 }
112 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
113 {
114 fprintf (stderr, "can't get image length\n");
115 goto fail;
116 }
117 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
118 {
119 fprintf (stderr, "can't get image width\n");
120 goto fail;
121 }
122 #ifdef CHECK_DEPTH
123 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
124 {
125 fprintf (stderr, "can't get image depth\n");
126 goto fail;
127 }
128 #endif
130 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
131 {
132 fprintf (stderr, "can't get bits per sample\n");
133 goto fail;
134 }
136 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
137 planar_config = 1;
139 printf ("image length %u width %u, "
140 #ifdef CHECK_DEPTH
141 "depth %u, "
142 #endif
143 "planar config %u\n",
144 image_length, image_width,
145 #ifdef CHECK_DEPTH
146 image_depth,
147 #endif
148 planar_config);
150 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
151 resolution_unit = 2;
152 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
153 x_resolution = 300;
154 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
155 y_resolution = 300;
157 printf ("resolution unit %u, x resolution %f, y resolution %f\n",
158 resolution_unit, x_resolution, y_resolution);
160 #ifdef CHECK_DEPTH
161 if (image_depth != 1)
162 {
163 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
164 goto fail;
165 }
166 #endif
168 if (bits_per_sample != 1)
169 {
170 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
171 goto fail;
172 }
174 if (planar_config != 1)
175 {
176 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
177 goto fail;
178 }
180 #if 0
181 TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
182 TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
183 TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
185 TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
187 TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
188 TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
189 TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
191 TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
192 TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
193 TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
194 #endif
196 buffer = _TIFFmalloc (TIFFScanlineSize (in));
197 if (! buffer)
198 {
199 fprintf (stderr, "failed to allocate buffer\n");
200 goto fail;
201 }
203 for (row = 0; row < image_length; row++)
204 {
205 TIFFReadScanline (in, buffer, row, 0);
206 #if 0
207 TIFFWriteScanline (out, buffer, row, 0);
208 #endif
209 }
211 _TIFFfree (buffer);
213 return (1);
215 fail:
216 return (0);
217 }
220 int main (int argc, char *argv[])
221 {
222 int result = 0;
224 panda_init ();
226 if (argc != 2)
227 {
228 fprintf (stderr, "usage: %s spec\n", argv [0]);
229 result = 1;
230 goto fail;
231 }
233 if (! parse_spec_file (argv [1]))
234 goto fail;
236 fail:
237 close_tiff_input_file ();
238 close_pdf_output_file ();
239 return (result);
240 }