tumble.c

Tue, 01 Jan 2002 03:44:40 +0000

author
eric
date
Tue, 01 Jan 2002 03:44:40 +0000
changeset 26
4368c5fd9242
parent 25
139b91f9a224
child 28
ba8313d18bd3
permissions
-rw-r--r--

handle input and output files properly.

     1 /*
     2  * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
     3  * Main program
     4  * $Id: tumble.c,v 1.8 2001/12/31 19:44:40 eric Exp $
     5  * Copyright 2001 Eric Smith <eric@brouhaha.com>
     6  *
     7  * This program is free software; you can redistribute it and/or modify
     8  * it under the terms of the GNU General Public License version 2 as
     9  * published by the Free Software Foundation.  Note that permission is
    10  * not granted to redistribute this program under the terms of any
    11  * other version of the General Public License.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  *
    18  * You should have received a copy of the GNU General Public License
    19  * along with this program; if not, write to the Free Software
    20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111  USA
    21  */
    24 #include <stdio.h>
    25 #include <tiffio.h>
    26 #include <panda/functions.h>
    27 #include <panda/constants.h>
    29 #include "type.h"
    30 #include "bitblt.h"
    31 #include "semantics.h"
    32 #include "parser.tab.h"
    33 #include "tiff2pdf.h"
    36 typedef struct output_file_t
    37 {
    38   struct output_file_t *next;
    39   char *name;
    40   panda_pdf *pdf;
    41 } output_file_t;
    44 char *in_filename;
    45 TIFF *in;
    46 output_file_t *output_files;
    47 output_file_t *out;
    48 /* panda_pdf *out; */
    51 boolean close_tiff_input_file (void)
    52 {
    53   if (in)
    54     {
    55       free (in_filename);
    56       TIFFClose (in);
    57     }
    58   in = NULL;
    59   in_filename = NULL;
    60   return (1);
    61 }
    63 boolean open_tiff_input_file (char *name)
    64 {
    65   if (in)
    66     {
    67       if (strcmp (name, in_filename) == 0)
    68 	return (1);
    69       close_tiff_input_file ();
    70     }
    71   in_filename = strdup (name);
    72   if (! in_filename)
    73     {
    74       fprintf (stderr, "can't strdup input filename '%s'\n", name);
    75       return (0);
    76     }
    77   in = TIFFOpen (name, "r");
    78   if (! in)
    79     {
    80       fprintf (stderr, "can't open input file '%s'\n", name);
    81       free (in_filename);
    82       return (0);
    83     }
    84   return (1);
    85 }
    88 boolean close_pdf_output_files (void)
    89 {
    90   output_file_t *o, *n;
    92   for (o = output_files; o; o = n)
    93     {
    94       n = o->next;
    95       panda_close (o->pdf);
    96       free (o->name);
    97       free (o);
    98     }
    99   out = NULL;
   100   output_files = NULL;
   101   return (1);
   102 }
   104 boolean open_pdf_output_file (char *name)
   105 {
   106   output_file_t *o;
   108   if (out && (strcmp (name, out->name) == 0))
   109     return (1);
   110   for (o = output_files; o; o = o->next)
   111     if (strcmp (name, o->name) == 0)
   112       {
   113 	out = o;
   114 	return (1);
   115       }
   116   o = calloc (1, sizeof (output_file_t));
   117   if (! 0)
   118     {
   119       fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   120       return (0);
   121    }
   123   o->name = strdup (name);
   124   if (! o->name)
   125     {
   126       fprintf (stderr, "can't strdup output filename '%s'\n", name);
   127       free (o);
   128       return (0);
   129     }
   131   o->pdf = panda_open (name, "w");
   132   if (! o->pdf)
   133     {
   134       fprintf (stderr, "can't open output file '%s'\n", name);
   135       free (o->name);
   136       free (o);
   137       return (0);
   138     }
   140   /* prepend new output file onto list */
   141   o->next = output_files;
   142   output_files = o;
   144   out = o;
   145   return (1);
   146 }
   149 void process_page_numbers (int page_index,
   150 			   int count,
   151 			   int base,
   152 			   page_label_t *page_label)
   153 {
   154 }
   157 boolean process_page (int image,  /* range 1 .. n */
   158 		      input_attributes_t input_attributes,
   159 		      bookmark_t *bookmarks)
   160 {
   161   u32 image_length, image_width;
   162 #ifdef CHECK_DEPTH
   163   u32 image_depth;
   164 #endif
   165   u16 bits_per_sample;
   166   u16 planar_config;
   167   u16 resolution_unit;
   168   float x_resolution, y_resolution;
   170   char *buffer;
   171   u32 row;
   173   if (! TIFFSetDirectory (in, image - 1))
   174     {
   175       fprintf (stderr, "can't find page %d of input file\n", image);
   176       goto fail;
   177     }
   178   if (1 != TIFFGetField (in, TIFFTAG_IMAGELENGTH, & image_length))
   179     {
   180       fprintf (stderr, "can't get image length\n");
   181       goto fail;
   182     }
   183   if (1 != TIFFGetField (in, TIFFTAG_IMAGEWIDTH, & image_width))
   184     {
   185       fprintf (stderr, "can't get image width\n");
   186       goto fail;
   187     }
   188 #ifdef CHECK_DEPTH
   189   if (1 != TIFFGetField (in, TIFFTAG_IMAGEDEPTH, & image_depth))
   190     {
   191       fprintf (stderr, "can't get image depth\n");
   192       goto fail;
   193     }
   194 #endif
   196   if (1 != TIFFGetField (in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
   197     {
   198       fprintf (stderr, "can't get bits per sample\n");
   199       goto fail;
   200     }
   202   if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config))
   203     planar_config = 1;
   205   printf ("image length %u width %u, "
   206 #ifdef CHECK_DEPTH
   207           "depth %u, "
   208 #endif
   209           "planar config %u\n",
   210 	  image_length, image_width,
   211 #ifdef CHECK_DEPTH
   212 	  image_depth,
   213 #endif
   214 	  planar_config);
   216   if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
   217     resolution_unit = 2;
   218   if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution))
   219     x_resolution = 300;
   220   if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution))
   221     y_resolution = 300;
   223   printf ("resolution unit %u, x resolution %f, y resolution %f\n",
   224 	  resolution_unit, x_resolution, y_resolution);
   226 #ifdef CHECK_DEPTH
   227   if (image_depth != 1)
   228     {
   229       fprintf (stderr, "image depth %u, must be 1\n", image_depth);
   230       goto fail;
   231     }
   232 #endif
   234   if (bits_per_sample != 1)
   235     {
   236       fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
   237       goto fail;
   238     }
   240   if (planar_config != 1)
   241     {
   242       fprintf (stderr, "planar config %u, must be 1\n", planar_config);
   243       goto fail;
   244     }
   246 #if 0
   247   TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length);
   248   TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width);
   249   TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config);
   251   TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length);
   253   TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   254   TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution);
   255   TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution);
   257   TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   258   TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   259   TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   260 #endif
   262   buffer = _TIFFmalloc (TIFFScanlineSize (in));
   263   if (! buffer)
   264     {
   265       fprintf (stderr, "failed to allocate buffer\n");
   266       goto fail;
   267     }
   269   for (row = 0; row < image_length; row++)
   270     {
   271       TIFFReadScanline (in, buffer, row, 0);
   272 #if 0
   273       TIFFWriteScanline (out->pdf, buffer, row, 0);
   274 #endif
   275     }
   277   _TIFFfree (buffer);
   279   return (1);
   281  fail:
   282   return (0);
   283 }
   286 int main (int argc, char *argv[])
   287 {
   288   int result = 0;
   290   panda_init ();
   292   if (argc != 2)
   293     {
   294       fprintf (stderr, "usage: %s spec\n", argv [0]);
   295       result = 1;
   296       goto fail;
   297     }
   299   if (! parse_spec_file (argv [1]))
   300     {
   301       result = 2;
   302       goto fail;
   303     }
   305   if (! process_specs ())
   306     {
   307       result = 3;
   308       goto fail;
   309     }
   311  fail:
   312   close_tiff_input_file ();
   313   close_pdf_output_files ();
   314   return (result);
   315 }