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