tumble.c

Mon, 31 Dec 2001 08:25:04 +0000

author
eric
date
Mon, 31 Dec 2001 08:25:04 +0000
changeset 23
1fe64161aaac
parent 18
3de372e4b230
child 25
139b91f9a224
permissions
-rw-r--r--

added input and output attribute structures as arguments to process_page().

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@23 4 * $Id: tumble.c,v 1.6 2001/12/31 00:25:04 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@23 83 boolean process_page (int image, /* range 1 .. n */
eric@23 84 input_attributes_t input_attributes,
eric@23 85 output_attributes_t output_attributes)
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@10 212 int main (int argc, char *argv[])
eric@10 213 {
eric@10 214 int result = 0;
eric@10 215
eric@10 216 panda_init ();
eric@10 217
eric@10 218 if (argc != 2)
eric@10 219 {
eric@10 220 fprintf (stderr, "usage: %s spec\n", argv [0]);
eric@10 221 result = 1;
eric@10 222 goto fail;
eric@10 223 }
eric@10 224
eric@17 225 if (! parse_spec_file (argv [1]))
eric@17 226 goto fail;
eric@17 227
eric@10 228 fail:
eric@10 229 close_tiff_input_file ();
eric@10 230 close_pdf_output_file ();
eric@10 231 return (result);
eric@10 232 }