1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/t2p.c Sat Dec 29 17:44:57 2001 +0000 1.3 @@ -0,0 +1,238 @@ 1.4 +/* 1.5 + * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4 1.6 + * Main program 1.7 + * $Id: t2p.c,v 1.1 2001/12/29 09:44:24 eric Exp $ 1.8 + * Copyright 2001 Eric Smith <eric@brouhaha.com> 1.9 + * 1.10 + * This program is free software; you can redistribute it and/or modify 1.11 + * it under the terms of the GNU General Public License version 2 as 1.12 + * published by the Free Software Foundation. Note that permission is 1.13 + * not granted to redistribute this program under the terms of any 1.14 + * other version of the General Public License. 1.15 + * 1.16 + * This program is distributed in the hope that it will be useful, 1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.19 + * GNU General Public License for more details. 1.20 + * 1.21 + * You should have received a copy of the GNU General Public License 1.22 + * along with this program; if not, write to the Free Software 1.23 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 1.24 + */ 1.25 + 1.26 + 1.27 +#include <stdio.h> 1.28 +#include <tiffio.h> 1.29 +#include <panda/functions.h> 1.30 +#include <panda/constants.h> 1.31 + 1.32 +#include "type.h" 1.33 +#include "bitblt.h" 1.34 +#include "parser.tab.h" 1.35 +#include "tiff2pdf.h" 1.36 + 1.37 + 1.38 +TIFF *in; 1.39 +panda_pdf *out; 1.40 + 1.41 + 1.42 +boolean close_tiff_input_file (void) 1.43 +{ 1.44 + if (in) 1.45 + TIFFClose (in); 1.46 + in = NULL; 1.47 + return (1); 1.48 +} 1.49 + 1.50 +boolean open_tiff_input_file (char *name) 1.51 +{ 1.52 + if (in) 1.53 + close_tiff_input_file (); 1.54 + in = TIFFOpen (name, "r"); 1.55 + if (! in) 1.56 + { 1.57 + fprintf (stderr, "can't open input file '%s'\n", name); 1.58 + return (0); 1.59 + } 1.60 + return (1); 1.61 +} 1.62 + 1.63 + 1.64 +boolean close_pdf_output_file (void) 1.65 +{ 1.66 + if (out) 1.67 + panda_close (out); 1.68 + out = NULL; 1.69 + return (1); 1.70 +} 1.71 + 1.72 +boolean open_pdf_output_file (char *name) 1.73 +{ 1.74 + if (out) 1.75 + close_pdf_output_file (); 1.76 + out = panda_open (name, "w"); 1.77 + if (! out) 1.78 + { 1.79 + return (0); 1.80 + } 1.81 + return (1); 1.82 +} 1.83 + 1.84 + 1.85 +boolean process_page (int image) /* range 1 .. n */ 1.86 +{ 1.87 + u32 image_length, image_width; 1.88 +#ifdef CHECK_DEPTH 1.89 + u32 image_depth; 1.90 +#endif 1.91 + u16 bits_per_sample; 1.92 + u16 planar_config; 1.93 + u16 resolution_unit; 1.94 + float x_resolution, y_resolution; 1.95 + 1.96 + char *buffer; 1.97 + u32 row; 1.98 + 1.99 + if (! TIFFSetDirectory (in, image - 1)) 1.100 + { 1.101 + fprintf (stderr, "can't find page %d of input file\n", image); 1.102 + goto fail; 1.103 + } 1.104 + if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length)) 1.105 + { 1.106 + fprintf (stderr, "can't get image length\n"); 1.107 + goto fail; 1.108 + } 1.109 + if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width)) 1.110 + { 1.111 + fprintf (stderr, "can't get image width\n"); 1.112 + goto fail; 1.113 + } 1.114 +#ifdef CHECK_DEPTH 1.115 + if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth)) 1.116 + { 1.117 + fprintf (stderr, "can't get image depth\n"); 1.118 + goto fail; 1.119 + } 1.120 +#endif 1.121 + 1.122 + if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample)) 1.123 + { 1.124 + fprintf (stderr, "can't get bits per sample\n"); 1.125 + goto fail; 1.126 + } 1.127 + 1.128 + if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config)) 1.129 + planar_config = 1; 1.130 + 1.131 + printf ("image length %u width %u, " 1.132 +#ifdef CHECK_DEPTH 1.133 + "depth %u, " 1.134 +#endif 1.135 + "planar config %u\n", 1.136 + image_length, image_width, 1.137 +#ifdef CHECK_DEPTH 1.138 + image_depth, 1.139 +#endif 1.140 + planar_config); 1.141 + 1.142 + if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit)) 1.143 + resolution_unit = 2; 1.144 + if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution)) 1.145 + x_resolution = 300; 1.146 + if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution)) 1.147 + y_resolution = 300; 1.148 + 1.149 + printf ("resolution unit %u, x resolution %f, y resolution %f\n", 1.150 + resolution_unit, x_resolution, y_resolution); 1.151 + 1.152 +#ifdef CHECK_DEPTH 1.153 + if (image_depth != 1) 1.154 + { 1.155 + fprintf (stderr, "image depth %u, must be 1\n", image_depth); 1.156 + goto fail; 1.157 + } 1.158 +#endif 1.159 + 1.160 + if (bits_per_sample != 1) 1.161 + { 1.162 + fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample); 1.163 + goto fail; 1.164 + } 1.165 + 1.166 + if (planar_config != 1) 1.167 + { 1.168 + fprintf (stderr, "planar config %u, must be 1\n", planar_config); 1.169 + goto fail; 1.170 + } 1.171 + 1.172 +#if 0 1.173 + TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length); 1.174 + TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width); 1.175 + TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config); 1.176 + 1.177 + TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length); 1.178 + 1.179 + TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit); 1.180 + TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution); 1.181 + TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution); 1.182 + 1.183 + TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample); 1.184 + TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); 1.185 + TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); 1.186 +#endif 1.187 + 1.188 + buffer = _TIFFmalloc (TIFFScanlineSize (in)); 1.189 + if (! buffer) 1.190 + { 1.191 + fprintf (stderr, "failed to allocate buffer\n"); 1.192 + goto fail; 1.193 + } 1.194 + 1.195 + for (row = 0; row < image_length; row++) 1.196 + { 1.197 + TIFFReadScanline (in, buffer, row, 0); 1.198 +#if 0 1.199 + TIFFWriteScanline (out, buffer, row, 0); 1.200 +#endif 1.201 + } 1.202 + 1.203 + _TIFFfree (buffer); 1.204 + 1.205 + return (1); 1.206 + 1.207 + fail: 1.208 + return (0); 1.209 +} 1.210 + 1.211 + 1.212 + 1.213 +int main (int argc, char *argv[]) 1.214 +{ 1.215 + FILE *spec; 1.216 + int result = 0; 1.217 + 1.218 + panda_init (); 1.219 + 1.220 + if (argc != 2) 1.221 + { 1.222 + fprintf (stderr, "usage: %s spec\n", argv [0]); 1.223 + result = 1; 1.224 + goto fail; 1.225 + } 1.226 + 1.227 + spec = fopen (argv [2], "r"); 1.228 + if (! spec) 1.229 + { 1.230 + fprintf (stderr, "can't open spec file '%s'\n", argv [2]); 1.231 + result = 3; 1.232 + goto fail; 1.233 + } 1.234 + 1.235 + yyparse (); 1.236 + 1.237 + fail: 1.238 + close_tiff_input_file (); 1.239 + close_pdf_output_file (); 1.240 + return (result); 1.241 +}