t2p.c

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