Sun, 30 Dec 2001 04:16:46 +0000
..
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@14 | 4 | * $Id: tumble.c,v 1.3 2001/12/29 20:16:46 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@10 | 31 | #include "parser.tab.h" |
eric@10 | 32 | #include "tiff2pdf.h" |
eric@10 | 33 | |
eric@10 | 34 | |
eric@14 | 35 | int line; |
eric@14 | 36 | |
eric@12 | 37 | FILE *yyin; |
eric@10 | 38 | TIFF *in; |
eric@10 | 39 | panda_pdf *out; |
eric@10 | 40 | |
eric@10 | 41 | |
eric@10 | 42 | boolean close_tiff_input_file (void) |
eric@10 | 43 | { |
eric@10 | 44 | if (in) |
eric@10 | 45 | TIFFClose (in); |
eric@10 | 46 | in = NULL; |
eric@10 | 47 | return (1); |
eric@10 | 48 | } |
eric@10 | 49 | |
eric@10 | 50 | boolean open_tiff_input_file (char *name) |
eric@10 | 51 | { |
eric@10 | 52 | if (in) |
eric@10 | 53 | close_tiff_input_file (); |
eric@10 | 54 | in = TIFFOpen (name, "r"); |
eric@10 | 55 | if (! in) |
eric@10 | 56 | { |
eric@10 | 57 | fprintf (stderr, "can't open input file '%s'\n", name); |
eric@10 | 58 | return (0); |
eric@10 | 59 | } |
eric@10 | 60 | return (1); |
eric@10 | 61 | } |
eric@10 | 62 | |
eric@10 | 63 | |
eric@10 | 64 | boolean close_pdf_output_file (void) |
eric@10 | 65 | { |
eric@10 | 66 | if (out) |
eric@10 | 67 | panda_close (out); |
eric@10 | 68 | out = NULL; |
eric@10 | 69 | return (1); |
eric@10 | 70 | } |
eric@10 | 71 | |
eric@10 | 72 | boolean open_pdf_output_file (char *name) |
eric@10 | 73 | { |
eric@10 | 74 | if (out) |
eric@10 | 75 | close_pdf_output_file (); |
eric@10 | 76 | out = panda_open (name, "w"); |
eric@10 | 77 | if (! out) |
eric@10 | 78 | { |
eric@10 | 79 | return (0); |
eric@10 | 80 | } |
eric@10 | 81 | return (1); |
eric@10 | 82 | } |
eric@10 | 83 | |
eric@10 | 84 | |
eric@10 | 85 | boolean process_page (int image) /* range 1 .. n */ |
eric@10 | 86 | { |
eric@10 | 87 | u32 image_length, image_width; |
eric@10 | 88 | #ifdef CHECK_DEPTH |
eric@10 | 89 | u32 image_depth; |
eric@10 | 90 | #endif |
eric@10 | 91 | u16 bits_per_sample; |
eric@10 | 92 | u16 planar_config; |
eric@10 | 93 | u16 resolution_unit; |
eric@10 | 94 | float x_resolution, y_resolution; |
eric@10 | 95 | |
eric@10 | 96 | char *buffer; |
eric@10 | 97 | u32 row; |
eric@10 | 98 | |
eric@10 | 99 | if (! TIFFSetDirectory (in, image - 1)) |
eric@10 | 100 | { |
eric@10 | 101 | fprintf (stderr, "can't find page %d of input file\n", image); |
eric@10 | 102 | goto fail; |
eric@10 | 103 | } |
eric@10 | 104 | if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length)) |
eric@10 | 105 | { |
eric@10 | 106 | fprintf (stderr, "can't get image length\n"); |
eric@10 | 107 | goto fail; |
eric@10 | 108 | } |
eric@10 | 109 | if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width)) |
eric@10 | 110 | { |
eric@10 | 111 | fprintf (stderr, "can't get image width\n"); |
eric@10 | 112 | goto fail; |
eric@10 | 113 | } |
eric@10 | 114 | #ifdef CHECK_DEPTH |
eric@10 | 115 | if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth)) |
eric@10 | 116 | { |
eric@10 | 117 | fprintf (stderr, "can't get image depth\n"); |
eric@10 | 118 | goto fail; |
eric@10 | 119 | } |
eric@10 | 120 | #endif |
eric@10 | 121 | |
eric@10 | 122 | if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample)) |
eric@10 | 123 | { |
eric@10 | 124 | fprintf (stderr, "can't get bits per sample\n"); |
eric@10 | 125 | goto fail; |
eric@10 | 126 | } |
eric@10 | 127 | |
eric@10 | 128 | if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config)) |
eric@10 | 129 | planar_config = 1; |
eric@10 | 130 | |
eric@10 | 131 | printf ("image length %u width %u, " |
eric@10 | 132 | #ifdef CHECK_DEPTH |
eric@10 | 133 | "depth %u, " |
eric@10 | 134 | #endif |
eric@10 | 135 | "planar config %u\n", |
eric@10 | 136 | image_length, image_width, |
eric@10 | 137 | #ifdef CHECK_DEPTH |
eric@10 | 138 | image_depth, |
eric@10 | 139 | #endif |
eric@10 | 140 | planar_config); |
eric@10 | 141 | |
eric@10 | 142 | if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit)) |
eric@10 | 143 | resolution_unit = 2; |
eric@10 | 144 | if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution)) |
eric@10 | 145 | x_resolution = 300; |
eric@10 | 146 | if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution)) |
eric@10 | 147 | y_resolution = 300; |
eric@10 | 148 | |
eric@10 | 149 | printf ("resolution unit %u, x resolution %f, y resolution %f\n", |
eric@10 | 150 | resolution_unit, x_resolution, y_resolution); |
eric@10 | 151 | |
eric@10 | 152 | #ifdef CHECK_DEPTH |
eric@10 | 153 | if (image_depth != 1) |
eric@10 | 154 | { |
eric@10 | 155 | fprintf (stderr, "image depth %u, must be 1\n", image_depth); |
eric@10 | 156 | goto fail; |
eric@10 | 157 | } |
eric@10 | 158 | #endif |
eric@10 | 159 | |
eric@10 | 160 | if (bits_per_sample != 1) |
eric@10 | 161 | { |
eric@10 | 162 | fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample); |
eric@10 | 163 | goto fail; |
eric@10 | 164 | } |
eric@10 | 165 | |
eric@10 | 166 | if (planar_config != 1) |
eric@10 | 167 | { |
eric@10 | 168 | fprintf (stderr, "planar config %u, must be 1\n", planar_config); |
eric@10 | 169 | goto fail; |
eric@10 | 170 | } |
eric@10 | 171 | |
eric@10 | 172 | #if 0 |
eric@10 | 173 | TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length); |
eric@10 | 174 | TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width); |
eric@10 | 175 | TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config); |
eric@10 | 176 | |
eric@10 | 177 | TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length); |
eric@10 | 178 | |
eric@10 | 179 | TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit); |
eric@10 | 180 | TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution); |
eric@10 | 181 | TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution); |
eric@10 | 182 | |
eric@10 | 183 | TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample); |
eric@10 | 184 | TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
eric@10 | 185 | TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); |
eric@10 | 186 | #endif |
eric@10 | 187 | |
eric@10 | 188 | buffer = _TIFFmalloc (TIFFScanlineSize (in)); |
eric@10 | 189 | if (! buffer) |
eric@10 | 190 | { |
eric@10 | 191 | fprintf (stderr, "failed to allocate buffer\n"); |
eric@10 | 192 | goto fail; |
eric@10 | 193 | } |
eric@10 | 194 | |
eric@10 | 195 | for (row = 0; row < image_length; row++) |
eric@10 | 196 | { |
eric@10 | 197 | TIFFReadScanline (in, buffer, row, 0); |
eric@10 | 198 | #if 0 |
eric@10 | 199 | TIFFWriteScanline (out, buffer, row, 0); |
eric@10 | 200 | #endif |
eric@10 | 201 | } |
eric@10 | 202 | |
eric@10 | 203 | _TIFFfree (buffer); |
eric@10 | 204 | |
eric@10 | 205 | return (1); |
eric@10 | 206 | |
eric@10 | 207 | fail: |
eric@10 | 208 | return (0); |
eric@10 | 209 | } |
eric@10 | 210 | |
eric@10 | 211 | |
eric@14 | 212 | void input_images (int first, int last) |
eric@14 | 213 | { |
eric@14 | 214 | if (first == last) |
eric@14 | 215 | printf ("image %d\n", first); |
eric@14 | 216 | else |
eric@14 | 217 | printf ("iamges %d..%d\n", first, last); |
eric@14 | 218 | } |
eric@14 | 219 | |
eric@14 | 220 | void output_pages (int first, int last) |
eric@14 | 221 | { |
eric@14 | 222 | if (first == last) |
eric@14 | 223 | printf ("page %d\n", first); |
eric@14 | 224 | else |
eric@14 | 225 | printf ("pages %d..%d\n", first, last); |
eric@14 | 226 | } |
eric@14 | 227 | |
eric@14 | 228 | |
eric@12 | 229 | void yyerror (char *s) |
eric@12 | 230 | { |
eric@14 | 231 | fprintf (stderr, "%d: %s\n", line, s); |
eric@12 | 232 | } |
eric@12 | 233 | |
eric@10 | 234 | |
eric@10 | 235 | int main (int argc, char *argv[]) |
eric@10 | 236 | { |
eric@10 | 237 | int result = 0; |
eric@10 | 238 | |
eric@10 | 239 | panda_init (); |
eric@10 | 240 | |
eric@10 | 241 | if (argc != 2) |
eric@10 | 242 | { |
eric@10 | 243 | fprintf (stderr, "usage: %s spec\n", argv [0]); |
eric@10 | 244 | result = 1; |
eric@10 | 245 | goto fail; |
eric@10 | 246 | } |
eric@10 | 247 | |
eric@14 | 248 | yyin = fopen (argv [1], "r"); |
eric@12 | 249 | if (! yyin) |
eric@10 | 250 | { |
eric@14 | 251 | fprintf (stderr, "can't open spec file '%s'\n", argv [1]); |
eric@10 | 252 | result = 3; |
eric@10 | 253 | goto fail; |
eric@10 | 254 | } |
eric@10 | 255 | |
eric@14 | 256 | line = 1; |
eric@14 | 257 | |
eric@10 | 258 | yyparse (); |
eric@10 | 259 | |
eric@10 | 260 | fail: |
eric@14 | 261 | if (yyin) |
eric@14 | 262 | fclose (yyin); |
eric@10 | 263 | close_tiff_input_file (); |
eric@10 | 264 | close_pdf_output_file (); |
eric@10 | 265 | return (result); |
eric@10 | 266 | } |