Tue, 01 Jan 2002 03:44:40 +0000
handle input and output files properly.
eric@10 | 1 | /* |
eric@10 | 2 | * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4 |
eric@10 | 3 | * Main program |
eric@26 | 4 | * $Id: tumble.c,v 1.8 2001/12/31 19:44:40 eric Exp $ |
eric@10 | 5 | * Copyright 2001 Eric Smith <eric@brouhaha.com> |
eric@10 | 6 | * |
eric@10 | 7 | * This program is free software; you can redistribute it and/or modify |
eric@10 | 8 | * it under the terms of the GNU General Public License version 2 as |
eric@10 | 9 | * published by the Free Software Foundation. Note that permission is |
eric@10 | 10 | * not granted to redistribute this program under the terms of any |
eric@10 | 11 | * other version of the General Public License. |
eric@10 | 12 | * |
eric@10 | 13 | * This program is distributed in the hope that it will be useful, |
eric@10 | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@10 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@10 | 16 | * GNU General Public License for more details. |
eric@10 | 17 | * |
eric@10 | 18 | * You should have received a copy of the GNU General Public License |
eric@10 | 19 | * along with this program; if not, write to the Free Software |
eric@10 | 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@10 | 21 | */ |
eric@10 | 22 | |
eric@10 | 23 | |
eric@10 | 24 | #include <stdio.h> |
eric@10 | 25 | #include <tiffio.h> |
eric@10 | 26 | #include <panda/functions.h> |
eric@10 | 27 | #include <panda/constants.h> |
eric@10 | 28 | |
eric@10 | 29 | #include "type.h" |
eric@10 | 30 | #include "bitblt.h" |
eric@18 | 31 | #include "semantics.h" |
eric@10 | 32 | #include "parser.tab.h" |
eric@10 | 33 | #include "tiff2pdf.h" |
eric@10 | 34 | |
eric@10 | 35 | |
eric@26 | 36 | typedef struct output_file_t |
eric@26 | 37 | { |
eric@26 | 38 | struct output_file_t *next; |
eric@26 | 39 | char *name; |
eric@26 | 40 | panda_pdf *pdf; |
eric@26 | 41 | } output_file_t; |
eric@26 | 42 | |
eric@26 | 43 | |
eric@26 | 44 | char *in_filename; |
eric@10 | 45 | TIFF *in; |
eric@26 | 46 | output_file_t *output_files; |
eric@26 | 47 | output_file_t *out; |
eric@26 | 48 | /* panda_pdf *out; */ |
eric@10 | 49 | |
eric@10 | 50 | |
eric@10 | 51 | boolean close_tiff_input_file (void) |
eric@10 | 52 | { |
eric@10 | 53 | if (in) |
eric@26 | 54 | { |
eric@26 | 55 | free (in_filename); |
eric@26 | 56 | TIFFClose (in); |
eric@26 | 57 | } |
eric@10 | 58 | in = NULL; |
eric@26 | 59 | in_filename = NULL; |
eric@10 | 60 | return (1); |
eric@10 | 61 | } |
eric@10 | 62 | |
eric@10 | 63 | boolean open_tiff_input_file (char *name) |
eric@10 | 64 | { |
eric@10 | 65 | if (in) |
eric@26 | 66 | { |
eric@26 | 67 | if (strcmp (name, in_filename) == 0) |
eric@26 | 68 | return (1); |
eric@26 | 69 | close_tiff_input_file (); |
eric@26 | 70 | } |
eric@26 | 71 | in_filename = strdup (name); |
eric@26 | 72 | if (! in_filename) |
eric@26 | 73 | { |
eric@26 | 74 | fprintf (stderr, "can't strdup input filename '%s'\n", name); |
eric@26 | 75 | return (0); |
eric@26 | 76 | } |
eric@10 | 77 | in = TIFFOpen (name, "r"); |
eric@10 | 78 | if (! in) |
eric@10 | 79 | { |
eric@10 | 80 | fprintf (stderr, "can't open input file '%s'\n", name); |
eric@26 | 81 | free (in_filename); |
eric@10 | 82 | return (0); |
eric@10 | 83 | } |
eric@10 | 84 | return (1); |
eric@10 | 85 | } |
eric@10 | 86 | |
eric@10 | 87 | |
eric@26 | 88 | boolean close_pdf_output_files (void) |
eric@10 | 89 | { |
eric@26 | 90 | output_file_t *o, *n; |
eric@26 | 91 | |
eric@26 | 92 | for (o = output_files; o; o = n) |
eric@26 | 93 | { |
eric@26 | 94 | n = o->next; |
eric@26 | 95 | panda_close (o->pdf); |
eric@26 | 96 | free (o->name); |
eric@26 | 97 | free (o); |
eric@26 | 98 | } |
eric@10 | 99 | out = NULL; |
eric@26 | 100 | output_files = NULL; |
eric@10 | 101 | return (1); |
eric@10 | 102 | } |
eric@10 | 103 | |
eric@10 | 104 | boolean open_pdf_output_file (char *name) |
eric@10 | 105 | { |
eric@26 | 106 | output_file_t *o; |
eric@26 | 107 | |
eric@26 | 108 | if (out && (strcmp (name, out->name) == 0)) |
eric@26 | 109 | return (1); |
eric@26 | 110 | for (o = output_files; o; o = o->next) |
eric@26 | 111 | if (strcmp (name, o->name) == 0) |
eric@26 | 112 | { |
eric@26 | 113 | out = o; |
eric@26 | 114 | return (1); |
eric@26 | 115 | } |
eric@26 | 116 | o = calloc (1, sizeof (output_file_t)); |
eric@26 | 117 | if (! 0) |
eric@10 | 118 | { |
eric@26 | 119 | fprintf (stderr, "can't calloc output file struct for '%s'\n", name); |
eric@26 | 120 | return (0); |
eric@26 | 121 | } |
eric@26 | 122 | |
eric@26 | 123 | o->name = strdup (name); |
eric@26 | 124 | if (! o->name) |
eric@26 | 125 | { |
eric@26 | 126 | fprintf (stderr, "can't strdup output filename '%s'\n", name); |
eric@26 | 127 | free (o); |
eric@10 | 128 | return (0); |
eric@10 | 129 | } |
eric@26 | 130 | |
eric@26 | 131 | o->pdf = panda_open (name, "w"); |
eric@26 | 132 | if (! o->pdf) |
eric@26 | 133 | { |
eric@26 | 134 | fprintf (stderr, "can't open output file '%s'\n", name); |
eric@26 | 135 | free (o->name); |
eric@26 | 136 | free (o); |
eric@26 | 137 | return (0); |
eric@26 | 138 | } |
eric@26 | 139 | |
eric@26 | 140 | /* prepend new output file onto list */ |
eric@26 | 141 | o->next = output_files; |
eric@26 | 142 | output_files = o; |
eric@26 | 143 | |
eric@26 | 144 | out = o; |
eric@10 | 145 | return (1); |
eric@10 | 146 | } |
eric@10 | 147 | |
eric@10 | 148 | |
eric@25 | 149 | void process_page_numbers (int page_index, |
eric@25 | 150 | int count, |
eric@25 | 151 | int base, |
eric@25 | 152 | page_label_t *page_label) |
eric@25 | 153 | { |
eric@25 | 154 | } |
eric@25 | 155 | |
eric@25 | 156 | |
eric@23 | 157 | boolean process_page (int image, /* range 1 .. n */ |
eric@23 | 158 | input_attributes_t input_attributes, |
eric@25 | 159 | bookmark_t *bookmarks) |
eric@10 | 160 | { |
eric@10 | 161 | u32 image_length, image_width; |
eric@10 | 162 | #ifdef CHECK_DEPTH |
eric@10 | 163 | u32 image_depth; |
eric@10 | 164 | #endif |
eric@10 | 165 | u16 bits_per_sample; |
eric@10 | 166 | u16 planar_config; |
eric@10 | 167 | u16 resolution_unit; |
eric@10 | 168 | float x_resolution, y_resolution; |
eric@10 | 169 | |
eric@10 | 170 | char *buffer; |
eric@10 | 171 | u32 row; |
eric@10 | 172 | |
eric@10 | 173 | if (! TIFFSetDirectory (in, image - 1)) |
eric@10 | 174 | { |
eric@10 | 175 | fprintf (stderr, "can't find page %d of input file\n", image); |
eric@10 | 176 | goto fail; |
eric@10 | 177 | } |
eric@10 | 178 | if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length)) |
eric@10 | 179 | { |
eric@10 | 180 | fprintf (stderr, "can't get image length\n"); |
eric@10 | 181 | goto fail; |
eric@10 | 182 | } |
eric@10 | 183 | if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width)) |
eric@10 | 184 | { |
eric@10 | 185 | fprintf (stderr, "can't get image width\n"); |
eric@10 | 186 | goto fail; |
eric@10 | 187 | } |
eric@10 | 188 | #ifdef CHECK_DEPTH |
eric@10 | 189 | if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth)) |
eric@10 | 190 | { |
eric@10 | 191 | fprintf (stderr, "can't get image depth\n"); |
eric@10 | 192 | goto fail; |
eric@10 | 193 | } |
eric@10 | 194 | #endif |
eric@10 | 195 | |
eric@10 | 196 | if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample)) |
eric@10 | 197 | { |
eric@10 | 198 | fprintf (stderr, "can't get bits per sample\n"); |
eric@10 | 199 | goto fail; |
eric@10 | 200 | } |
eric@10 | 201 | |
eric@10 | 202 | if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config)) |
eric@10 | 203 | planar_config = 1; |
eric@10 | 204 | |
eric@10 | 205 | printf ("image length %u width %u, " |
eric@10 | 206 | #ifdef CHECK_DEPTH |
eric@10 | 207 | "depth %u, " |
eric@10 | 208 | #endif |
eric@10 | 209 | "planar config %u\n", |
eric@10 | 210 | image_length, image_width, |
eric@10 | 211 | #ifdef CHECK_DEPTH |
eric@10 | 212 | image_depth, |
eric@10 | 213 | #endif |
eric@10 | 214 | planar_config); |
eric@10 | 215 | |
eric@10 | 216 | if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit)) |
eric@10 | 217 | resolution_unit = 2; |
eric@10 | 218 | if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution)) |
eric@10 | 219 | x_resolution = 300; |
eric@10 | 220 | if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution)) |
eric@10 | 221 | y_resolution = 300; |
eric@10 | 222 | |
eric@10 | 223 | printf ("resolution unit %u, x resolution %f, y resolution %f\n", |
eric@10 | 224 | resolution_unit, x_resolution, y_resolution); |
eric@10 | 225 | |
eric@10 | 226 | #ifdef CHECK_DEPTH |
eric@10 | 227 | if (image_depth != 1) |
eric@10 | 228 | { |
eric@10 | 229 | fprintf (stderr, "image depth %u, must be 1\n", image_depth); |
eric@10 | 230 | goto fail; |
eric@10 | 231 | } |
eric@10 | 232 | #endif |
eric@10 | 233 | |
eric@10 | 234 | if (bits_per_sample != 1) |
eric@10 | 235 | { |
eric@10 | 236 | fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample); |
eric@10 | 237 | goto fail; |
eric@10 | 238 | } |
eric@10 | 239 | |
eric@10 | 240 | if (planar_config != 1) |
eric@10 | 241 | { |
eric@10 | 242 | fprintf (stderr, "planar config %u, must be 1\n", planar_config); |
eric@10 | 243 | goto fail; |
eric@10 | 244 | } |
eric@10 | 245 | |
eric@10 | 246 | #if 0 |
eric@26 | 247 | TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length); |
eric@26 | 248 | TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width); |
eric@26 | 249 | TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config); |
eric@10 | 250 | |
eric@26 | 251 | TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length); |
eric@10 | 252 | |
eric@26 | 253 | TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit); |
eric@26 | 254 | TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution); |
eric@26 | 255 | TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution); |
eric@10 | 256 | |
eric@26 | 257 | TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample); |
eric@26 | 258 | TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
eric@26 | 259 | TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); |
eric@10 | 260 | #endif |
eric@10 | 261 | |
eric@10 | 262 | buffer = _TIFFmalloc (TIFFScanlineSize (in)); |
eric@10 | 263 | if (! buffer) |
eric@10 | 264 | { |
eric@10 | 265 | fprintf (stderr, "failed to allocate buffer\n"); |
eric@10 | 266 | goto fail; |
eric@10 | 267 | } |
eric@10 | 268 | |
eric@10 | 269 | for (row = 0; row < image_length; row++) |
eric@10 | 270 | { |
eric@10 | 271 | TIFFReadScanline (in, buffer, row, 0); |
eric@10 | 272 | #if 0 |
eric@26 | 273 | TIFFWriteScanline (out->pdf, buffer, row, 0); |
eric@10 | 274 | #endif |
eric@10 | 275 | } |
eric@10 | 276 | |
eric@10 | 277 | _TIFFfree (buffer); |
eric@10 | 278 | |
eric@10 | 279 | return (1); |
eric@10 | 280 | |
eric@10 | 281 | fail: |
eric@10 | 282 | return (0); |
eric@10 | 283 | } |
eric@10 | 284 | |
eric@10 | 285 | |
eric@10 | 286 | int main (int argc, char *argv[]) |
eric@10 | 287 | { |
eric@10 | 288 | int result = 0; |
eric@10 | 289 | |
eric@10 | 290 | panda_init (); |
eric@10 | 291 | |
eric@10 | 292 | if (argc != 2) |
eric@10 | 293 | { |
eric@10 | 294 | fprintf (stderr, "usage: %s spec\n", argv [0]); |
eric@10 | 295 | result = 1; |
eric@10 | 296 | goto fail; |
eric@10 | 297 | } |
eric@10 | 298 | |
eric@17 | 299 | if (! parse_spec_file (argv [1])) |
eric@26 | 300 | { |
eric@26 | 301 | result = 2; |
eric@26 | 302 | goto fail; |
eric@26 | 303 | } |
eric@26 | 304 | |
eric@26 | 305 | if (! process_specs ()) |
eric@26 | 306 | { |
eric@26 | 307 | result = 3; |
eric@26 | 308 | goto fail; |
eric@26 | 309 | } |
eric@17 | 310 | |
eric@10 | 311 | fail: |
eric@10 | 312 | close_tiff_input_file (); |
eric@26 | 313 | close_pdf_output_files (); |
eric@10 | 314 | return (result); |
eric@10 | 315 | } |