Tue, 01 Jan 2002 05:41:03 +0000
fix bugs. copy samples per pixel tag.
1 /*
2 * tiff2pdf: Create a PDF file from the contents of one or more
3 * TIFF bilevel image files. The images in the resulting
4 * PDF file will be compressed using ITU-T T.6 (G4) fax
5 * encoding.
6 *
7 * Main program
8 * $Id: tumble.c,v 1.10 2001/12/31 21:41:03 eric Exp $
9 * Copyright 2001 Eric Smith <eric@brouhaha.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation. Note that permission is
14 * not granted to redistribute this program under the terms of any
15 * other version of the General Public License.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
25 */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <tiffio.h>
32 #include <panda/functions.h>
33 #include <panda/constants.h>
35 #include "type.h"
36 #include "bitblt.h"
37 #include "semantics.h"
38 #include "parser.tab.h"
39 #include "tiff2pdf.h"
42 #define POINTS_PER_INCH 72
44 /* page size limited by Acrobat Reader to 45 inches on a side */
45 #define PAGE_MAX_INCHES 45
46 #define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
49 typedef struct output_file_t
50 {
51 struct output_file_t *next;
52 char *name;
53 panda_pdf *pdf;
54 } output_file_t;
57 char *in_filename;
58 TIFF *in;
59 output_file_t *output_files;
60 output_file_t *out;
61 /* panda_pdf *out; */
64 boolean close_tiff_input_file (void)
65 {
66 if (in)
67 {
68 free (in_filename);
69 TIFFClose (in);
70 }
71 in = NULL;
72 in_filename = NULL;
73 return (1);
74 }
76 boolean open_tiff_input_file (char *name)
77 {
78 if (in)
79 {
80 if (strcmp (name, in_filename) == 0)
81 return (1);
82 close_tiff_input_file ();
83 }
84 in_filename = strdup (name);
85 if (! in_filename)
86 {
87 fprintf (stderr, "can't strdup input filename '%s'\n", name);
88 return (0);
89 }
90 in = TIFFOpen (name, "r");
91 if (! in)
92 {
93 fprintf (stderr, "can't open input file '%s'\n", name);
94 free (in_filename);
95 return (0);
96 }
97 return (1);
98 }
101 boolean close_pdf_output_files (void)
102 {
103 output_file_t *o, *n;
105 for (o = output_files; o; o = n)
106 {
107 n = o->next;
108 panda_close (o->pdf);
109 free (o->name);
110 free (o);
111 }
112 out = NULL;
113 output_files = NULL;
114 return (1);
115 }
117 boolean open_pdf_output_file (char *name)
118 {
119 output_file_t *o;
121 if (out && (strcmp (name, out->name) == 0))
122 return (1);
123 for (o = output_files; o; o = o->next)
124 if (strcmp (name, o->name) == 0)
125 {
126 out = o;
127 return (1);
128 }
129 o = calloc (1, sizeof (output_file_t));
130 if (! o)
131 {
132 fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
133 return (0);
134 }
136 o->name = strdup (name);
137 if (! o->name)
138 {
139 fprintf (stderr, "can't strdup output filename '%s'\n", name);
140 free (o);
141 return (0);
142 }
144 o->pdf = panda_open (name, "w");
145 if (! o->pdf)
146 {
147 fprintf (stderr, "can't open output file '%s'\n", name);
148 free (o->name);
149 free (o);
150 return (0);
151 }
153 /* prepend new output file onto list */
154 o->next = output_files;
155 output_files = o;
157 out = o;
158 return (1);
159 }
162 void process_page_numbers (int page_index,
163 int count,
164 int base,
165 page_label_t *page_label)
166 {
167 }
170 boolean process_page (int image, /* range 1 .. n */
171 input_attributes_t input_attributes,
172 bookmark_t *bookmarks)
173 {
174 int result = 0;
176 u32 image_length, image_width;
177 #ifdef CHECK_DEPTH
178 u32 image_depth;
179 #endif
181 u16 samples_per_pixel;
182 u16 bits_per_sample;
183 u16 planar_config;
184 u16 resolution_unit;
185 float x_resolution, y_resolution;
186 int width_points, height_points; /* really 1/72 inch units rather than
187 points */
190 char *buffer;
191 u32 row;
193 panda_page *page;
195 int tiff_temp_fd;
196 char tiff_temp_fn [] = "/var/tmp/tiff2pdf-XXXXXX\0";
197 TIFF *tiff_temp;
199 char pagesize [26]; /* Needs to hold two ints of four characters (0..3420),
200 two zeros, three spaces, two brackets, and a NULL.
201 Added an extra ten characters just in case. */
203 if (! TIFFSetDirectory (in, image - 1))
204 {
205 fprintf (stderr, "can't find page %d of input file\n", image);
206 goto fail;
207 }
208 if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
209 {
210 fprintf (stderr, "can't get image length\n");
211 goto fail;
212 }
213 if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
214 {
215 fprintf (stderr, "can't get image width\n");
216 goto fail;
217 }
219 if (1 != TIFFGetField (in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
220 {
221 fprintf (stderr, "can't get samples per pixel\n");
222 goto fail;
223 }
225 #ifdef CHECK_DEPTH
226 if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
227 {
228 fprintf (stderr, "can't get image depth\n");
229 goto fail;
230 }
231 #endif
233 if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
234 {
235 fprintf (stderr, "can't get bits per sample\n");
236 goto fail;
237 }
239 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
240 planar_config = 1;
242 printf ("image length %u width %u, "
243 #ifdef CHECK_DEPTH
244 "depth %u, "
245 #endif
246 "planar config %u\n",
247 image_length, image_width,
248 #ifdef CHECK_DEPTH
249 image_depth,
250 #endif
251 planar_config);
253 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
254 resolution_unit = 2;
255 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
256 x_resolution = 300;
257 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
258 y_resolution = 300;
260 printf ("resolution unit %u, x resolution %f, y resolution %f\n",
261 resolution_unit, x_resolution, y_resolution);
263 if (samples_per_pixel != 1)
264 {
265 fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
266 goto fail;
267 }
269 #ifdef CHECK_DEPTH
270 if (image_depth != 1)
271 {
272 fprintf (stderr, "image depth %u, must be 1\n", image_depth);
273 goto fail;
274 }
275 #endif
277 if (bits_per_sample != 1)
278 {
279 fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
280 goto fail;
281 }
283 if (planar_config != 1)
284 {
285 fprintf (stderr, "planar config %u, must be 1\n", planar_config);
286 goto fail;
287 }
289 tiff_temp_fd = mkstemp (tiff_temp_fn);
290 if (tiff_temp_fd < 0)
291 {
292 fprintf (stderr, "can't create temporary TIFF file\n");
293 goto fail;
294 }
296 tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
297 if (! out)
298 {
299 fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
300 goto fail;
301 }
303 TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, image_length);
304 TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, image_width);
305 TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
307 TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, image_length);
309 TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
310 TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, x_resolution);
311 TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, y_resolution);
313 TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
314 TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
315 TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
316 TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
318 buffer = _TIFFmalloc (TIFFScanlineSize (in));
319 if (! buffer)
320 {
321 fprintf (stderr, "failed to allocate buffer\n");
322 goto fail;
323 }
325 for (row = 0; row < image_length; row++)
326 {
327 TIFFReadScanline (in, buffer, row, 0);
328 TIFFWriteScanline (tiff_temp, buffer, row, 0);
329 }
331 _TIFFfree (buffer);
332 TIFFClose (tiff_temp);
334 width_points = (image_width / x_resolution) * POINTS_PER_INCH;
335 height_points = (image_length / y_resolution) * POINTS_PER_INCH;
337 if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
338 {
339 fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
340 goto fail;
341 }
343 printf ("height_points %d, width_points %d\n", height_points, width_points);
345 sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
347 page = panda_newpage (out->pdf, pagesize);
348 panda_imagebox (out->pdf,
349 page,
350 0, /* top */
351 0, /* left */
352 height_points, /* bottom */
353 width_points, /* right */
354 tiff_temp_fn,
355 panda_image_tiff);
357 result = 1;
359 fail:
360 if (tiff_temp_fd)
361 unlink (tiff_temp_fn);
362 return (result);
363 }
366 int main (int argc, char *argv[])
367 {
368 int result = 0;
370 panda_init ();
372 if (argc != 2)
373 {
374 fprintf (stderr, "usage: %s spec\n", argv [0]);
375 result = 1;
376 goto fail;
377 }
379 if (! parse_spec_file (argv [1]))
380 {
381 result = 2;
382 goto fail;
383 }
385 if (! process_specs ())
386 {
387 result = 3;
388 goto fail;
389 }
391 fail:
392 close_tiff_input_file ();
393 close_pdf_output_files ();
394 return (result);
395 }