tumble.c

Sat, 29 Dec 2001 17:44:57 +0000

author
eric
date
Sat, 29 Dec 2001 17:44:57 +0000
changeset 10
3184d26a9596
child 12
c3e2c2344560
permissions
-rw-r--r--

Initial revision

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