handle input and output files properly.

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 27
7a28031fe457

handle input and output files properly.

semantics.c file | annotate | diff | revisions
semantics.h file | annotate | diff | revisions
t2p.c file | annotate | diff | revisions
t2p.h file | annotate | diff | revisions
tumble.c file | annotate | diff | revisions
tumble.h file | annotate | diff | revisions
     1.1 --- a/semantics.c	Mon Dec 31 16:44:24 2001 +0000
     1.2 +++ b/semantics.c	Tue Jan 01 03:44:40 2002 +0000
     1.3 @@ -478,60 +478,6 @@
     1.4  }
     1.5  
     1.6  
     1.7 -void doit (void)
     1.8 -{
     1.9 -  input_image_t *image = NULL;
    1.10 -  output_page_t *page = NULL;
    1.11 -  int i = 0;
    1.12 -  int p = 0;
    1.13 -  int page_index = 0;
    1.14 -  input_attributes_t input_attributes;
    1.15 -  input_modifier_type_t parity;
    1.16 -  page_label_t *page_label;
    1.17 -
    1.18 -  for (;;)
    1.19 -    {
    1.20 -      if ((! image) || (i >= range_count (image->range)))
    1.21 -	{
    1.22 -	  if (image)
    1.23 -	    image = image->next;
    1.24 -	  else
    1.25 -	    image = first_input_image;
    1.26 -	  if (! image)
    1.27 -	    return;
    1.28 -	  i = 0;
    1.29 -	}
    1.30 -
    1.31 -      if ((! page) || (p >= range_count (page->range)))
    1.32 -	{
    1.33 -	  if (page)
    1.34 -	    page = page->next;
    1.35 -	  else
    1.36 -	    page = first_output_page;
    1.37 -	  p = 0;
    1.38 -	  page_label = get_output_page_label (page->output_context);
    1.39 -	  process_page_numbers (page_index,
    1.40 -				range_count (page->range),
    1.41 -				page->range.first,
    1.42 -				page_label);
    1.43 -	}
    1.44 -
    1.45 -      parity = ((image->range.first + i) % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN;
    1.46 -
    1.47 -      memset (& input_attributes, 0, sizeof (input_attributes));
    1.48 -      input_attributes.rotation = get_input_rotation (image->input_context,
    1.49 -						      parity);;
    1.50 -
    1.51 -      process_page (image->range.first + i,
    1.52 -		    input_attributes,
    1.53 -		    page->bookmark_list);
    1.54 -      i++;
    1.55 -      p++;
    1.56 -      page_index++;
    1.57 -    }
    1.58 -}
    1.59 -
    1.60 -
    1.61  boolean parse_spec_file (char *fn)
    1.62  {
    1.63    boolean result = 0;
    1.64 @@ -576,3 +522,63 @@
    1.65  
    1.66    return (result);
    1.67  }
    1.68 +
    1.69 +
    1.70 +boolean process_specs (void)
    1.71 +{
    1.72 +  input_image_t *image = NULL;
    1.73 +  output_page_t *page = NULL;
    1.74 +  int i = 0;
    1.75 +  int p = 0;
    1.76 +  int page_index = 0;
    1.77 +  input_attributes_t input_attributes;
    1.78 +  input_modifier_type_t parity;
    1.79 +  page_label_t *page_label;
    1.80 +
    1.81 +  for (;;)
    1.82 +    {
    1.83 +      if ((! image) || (i >= range_count (image->range)))
    1.84 +	{
    1.85 +	  if (image)
    1.86 +	    image = image->next;
    1.87 +	  else
    1.88 +	    image = first_input_image;
    1.89 +	  if (! image)
    1.90 +	    return (0);
    1.91 +	  i = 0;
    1.92 +	  if (! open_tiff_input_file (get_input_file (image->input_context)))
    1.93 +	    return (0);
    1.94 +	}
    1.95 +
    1.96 +      if ((! page) || (p >= range_count (page->range)))
    1.97 +	{
    1.98 +	  if (page)
    1.99 +	    page = page->next;
   1.100 +	  else
   1.101 +	    page = first_output_page;
   1.102 +	  p = 0;
   1.103 +	  if (! open_pdf_output_file (get_output_file (page->output_context)))
   1.104 +	    return (0);
   1.105 +	  page_label = get_output_page_label (page->output_context);
   1.106 +	  process_page_numbers (page_index,
   1.107 +				range_count (page->range),
   1.108 +				page->range.first,
   1.109 +				page_label);
   1.110 +	}
   1.111 +
   1.112 +      parity = ((image->range.first + i) % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN;
   1.113 +
   1.114 +      memset (& input_attributes, 0, sizeof (input_attributes));
   1.115 +      input_attributes.rotation = get_input_rotation (image->input_context,
   1.116 +						      parity);;
   1.117 +
   1.118 +      process_page (image->range.first + i,
   1.119 +		    input_attributes,
   1.120 +		    page->bookmark_list);
   1.121 +      i++;
   1.122 +      p++;
   1.123 +      page_index++;
   1.124 +    }
   1.125 +
   1.126 +  return (1);
   1.127 +}
     2.1 --- a/semantics.h	Mon Dec 31 16:44:24 2001 +0000
     2.2 +++ b/semantics.h	Tue Jan 01 03:44:40 2002 +0000
     2.3 @@ -46,9 +46,6 @@
     2.4  extern int bookmark_level;
     2.5  
     2.6  
     2.7 -boolean parse_spec_file (char *fn);
     2.8 -
     2.9 -
    2.10  /* semantic routines for input statements */
    2.11  void input_push_context (void);
    2.12  void input_pop_context (void);
    2.13 @@ -64,3 +61,8 @@
    2.14  void output_set_bookmark (char *name);
    2.15  void output_set_page_label (page_label_t label);
    2.16  void output_pages (range_t range);
    2.17 +
    2.18 +
    2.19 +/* functions to be called from main program: */
    2.20 +boolean parse_spec_file (char *fn);
    2.21 +boolean process_specs (void);
     3.1 --- a/t2p.c	Mon Dec 31 16:44:24 2001 +0000
     3.2 +++ b/t2p.c	Tue Jan 01 03:44:40 2002 +0000
     3.3 @@ -1,7 +1,7 @@
     3.4  /*
     3.5   * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
     3.6   * Main program
     3.7 - * $Id: t2p.c,v 1.7 2001/12/31 08:44:24 eric Exp $
     3.8 + * $Id: t2p.c,v 1.8 2001/12/31 19:44:40 eric Exp $
     3.9   * Copyright 2001 Eric Smith <eric@brouhaha.com>
    3.10   *
    3.11   * This program is free software; you can redistribute it and/or modify
    3.12 @@ -33,49 +33,115 @@
    3.13  #include "tiff2pdf.h"
    3.14  
    3.15  
    3.16 +typedef struct output_file_t
    3.17 +{
    3.18 +  struct output_file_t *next;
    3.19 +  char *name;
    3.20 +  panda_pdf *pdf;
    3.21 +} output_file_t;
    3.22 +
    3.23 +
    3.24 +char *in_filename;
    3.25  TIFF *in;
    3.26 -panda_pdf *out;
    3.27 +output_file_t *output_files;
    3.28 +output_file_t *out;
    3.29 +/* panda_pdf *out; */
    3.30  
    3.31  
    3.32  boolean close_tiff_input_file (void)
    3.33  {
    3.34    if (in)
    3.35 -    TIFFClose (in);
    3.36 +    {
    3.37 +      free (in_filename);
    3.38 +      TIFFClose (in);
    3.39 +    }
    3.40    in = NULL;
    3.41 +  in_filename = NULL;
    3.42    return (1);
    3.43  }
    3.44  
    3.45  boolean open_tiff_input_file (char *name)
    3.46  {
    3.47    if (in)
    3.48 -    close_tiff_input_file ();
    3.49 +    {
    3.50 +      if (strcmp (name, in_filename) == 0)
    3.51 +	return (1);
    3.52 +      close_tiff_input_file ();
    3.53 +    }
    3.54 +  in_filename = strdup (name);
    3.55 +  if (! in_filename)
    3.56 +    {
    3.57 +      fprintf (stderr, "can't strdup input filename '%s'\n", name);
    3.58 +      return (0);
    3.59 +    }
    3.60    in = TIFFOpen (name, "r");
    3.61    if (! in)
    3.62      {
    3.63        fprintf (stderr, "can't open input file '%s'\n", name);
    3.64 +      free (in_filename);
    3.65        return (0);
    3.66      }
    3.67    return (1);
    3.68  }
    3.69  
    3.70  
    3.71 -boolean close_pdf_output_file (void)
    3.72 +boolean close_pdf_output_files (void)
    3.73  {
    3.74 -  if (out)
    3.75 -    panda_close (out);
    3.76 +  output_file_t *o, *n;
    3.77 +
    3.78 +  for (o = output_files; o; o = n)
    3.79 +    {
    3.80 +      n = o->next;
    3.81 +      panda_close (o->pdf);
    3.82 +      free (o->name);
    3.83 +      free (o);
    3.84 +    }
    3.85    out = NULL;
    3.86 +  output_files = NULL;
    3.87    return (1);
    3.88  }
    3.89  
    3.90  boolean open_pdf_output_file (char *name)
    3.91  {
    3.92 -  if (out)
    3.93 -    close_pdf_output_file ();
    3.94 -  out = panda_open (name, "w");
    3.95 -  if (! out)
    3.96 +  output_file_t *o;
    3.97 +
    3.98 +  if (out && (strcmp (name, out->name) == 0))
    3.99 +    return (1);
   3.100 +  for (o = output_files; o; o = o->next)
   3.101 +    if (strcmp (name, o->name) == 0)
   3.102 +      {
   3.103 +	out = o;
   3.104 +	return (1);
   3.105 +      }
   3.106 +  o = calloc (1, sizeof (output_file_t));
   3.107 +  if (! 0)
   3.108      {
   3.109 +      fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   3.110 +      return (0);
   3.111 +   }
   3.112 +
   3.113 +  o->name = strdup (name);
   3.114 +  if (! o->name)
   3.115 +    {
   3.116 +      fprintf (stderr, "can't strdup output filename '%s'\n", name);
   3.117 +      free (o);
   3.118        return (0);
   3.119      }
   3.120 +
   3.121 +  o->pdf = panda_open (name, "w");
   3.122 +  if (! o->pdf)
   3.123 +    {
   3.124 +      fprintf (stderr, "can't open output file '%s'\n", name);
   3.125 +      free (o->name);
   3.126 +      free (o);
   3.127 +      return (0);
   3.128 +    }
   3.129 +
   3.130 +  /* prepend new output file onto list */
   3.131 +  o->next = output_files;
   3.132 +  output_files = o;
   3.133 +
   3.134 +  out = o;
   3.135    return (1);
   3.136  }
   3.137  
   3.138 @@ -178,19 +244,19 @@
   3.139      }
   3.140  
   3.141  #if 0
   3.142 -  TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
   3.143 -  TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
   3.144 -  TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
   3.145 +  TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length);
   3.146 +  TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width);
   3.147 +  TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config);
   3.148  
   3.149 -  TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
   3.150 +  TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length);
   3.151  
   3.152 -  TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   3.153 -  TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
   3.154 -  TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
   3.155 +  TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   3.156 +  TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution);
   3.157 +  TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution);
   3.158  
   3.159 -  TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   3.160 -  TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   3.161 -  TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   3.162 +  TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   3.163 +  TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   3.164 +  TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   3.165  #endif
   3.166  
   3.167    buffer = _TIFFmalloc (TIFFScanlineSize (in));
   3.168 @@ -204,7 +270,7 @@
   3.169      {
   3.170        TIFFReadScanline (in, buffer, row, 0);
   3.171  #if 0
   3.172 -      TIFFWriteScanline (out, buffer, row, 0);
   3.173 +      TIFFWriteScanline (out->pdf, buffer, row, 0);
   3.174  #endif
   3.175      }
   3.176  
   3.177 @@ -231,10 +297,19 @@
   3.178      }
   3.179  
   3.180    if (! parse_spec_file (argv [1]))
   3.181 -    goto fail;
   3.182 +    {
   3.183 +      result = 2;
   3.184 +      goto fail;
   3.185 +    }
   3.186 +
   3.187 +  if (! process_specs ())
   3.188 +    {
   3.189 +      result = 3;
   3.190 +      goto fail;
   3.191 +    }
   3.192    
   3.193   fail:
   3.194    close_tiff_input_file ();
   3.195 -  close_pdf_output_file ();
   3.196 +  close_pdf_output_files ();
   3.197    return (result);
   3.198  }
     4.1 --- a/t2p.h	Mon Dec 31 16:44:24 2001 +0000
     4.2 +++ b/t2p.h	Tue Jan 01 03:44:40 2002 +0000
     4.3 @@ -9,7 +9,6 @@
     4.4  boolean close_tiff_input_file (void);
     4.5  
     4.6  boolean open_pdf_output_file (char *name);
     4.7 -boolean close_pdf_output_file (void);
     4.8  
     4.9  void process_page_numbers (int page_index,
    4.10  			   int count,
     5.1 --- a/tumble.c	Mon Dec 31 16:44:24 2001 +0000
     5.2 +++ b/tumble.c	Tue Jan 01 03:44:40 2002 +0000
     5.3 @@ -1,7 +1,7 @@
     5.4  /*
     5.5   * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
     5.6   * Main program
     5.7 - * $Id: tumble.c,v 1.7 2001/12/31 08:44:24 eric Exp $
     5.8 + * $Id: tumble.c,v 1.8 2001/12/31 19:44:40 eric Exp $
     5.9   * Copyright 2001 Eric Smith <eric@brouhaha.com>
    5.10   *
    5.11   * This program is free software; you can redistribute it and/or modify
    5.12 @@ -33,49 +33,115 @@
    5.13  #include "tiff2pdf.h"
    5.14  
    5.15  
    5.16 +typedef struct output_file_t
    5.17 +{
    5.18 +  struct output_file_t *next;
    5.19 +  char *name;
    5.20 +  panda_pdf *pdf;
    5.21 +} output_file_t;
    5.22 +
    5.23 +
    5.24 +char *in_filename;
    5.25  TIFF *in;
    5.26 -panda_pdf *out;
    5.27 +output_file_t *output_files;
    5.28 +output_file_t *out;
    5.29 +/* panda_pdf *out; */
    5.30  
    5.31  
    5.32  boolean close_tiff_input_file (void)
    5.33  {
    5.34    if (in)
    5.35 -    TIFFClose (in);
    5.36 +    {
    5.37 +      free (in_filename);
    5.38 +      TIFFClose (in);
    5.39 +    }
    5.40    in = NULL;
    5.41 +  in_filename = NULL;
    5.42    return (1);
    5.43  }
    5.44  
    5.45  boolean open_tiff_input_file (char *name)
    5.46  {
    5.47    if (in)
    5.48 -    close_tiff_input_file ();
    5.49 +    {
    5.50 +      if (strcmp (name, in_filename) == 0)
    5.51 +	return (1);
    5.52 +      close_tiff_input_file ();
    5.53 +    }
    5.54 +  in_filename = strdup (name);
    5.55 +  if (! in_filename)
    5.56 +    {
    5.57 +      fprintf (stderr, "can't strdup input filename '%s'\n", name);
    5.58 +      return (0);
    5.59 +    }
    5.60    in = TIFFOpen (name, "r");
    5.61    if (! in)
    5.62      {
    5.63        fprintf (stderr, "can't open input file '%s'\n", name);
    5.64 +      free (in_filename);
    5.65        return (0);
    5.66      }
    5.67    return (1);
    5.68  }
    5.69  
    5.70  
    5.71 -boolean close_pdf_output_file (void)
    5.72 +boolean close_pdf_output_files (void)
    5.73  {
    5.74 -  if (out)
    5.75 -    panda_close (out);
    5.76 +  output_file_t *o, *n;
    5.77 +
    5.78 +  for (o = output_files; o; o = n)
    5.79 +    {
    5.80 +      n = o->next;
    5.81 +      panda_close (o->pdf);
    5.82 +      free (o->name);
    5.83 +      free (o);
    5.84 +    }
    5.85    out = NULL;
    5.86 +  output_files = NULL;
    5.87    return (1);
    5.88  }
    5.89  
    5.90  boolean open_pdf_output_file (char *name)
    5.91  {
    5.92 -  if (out)
    5.93 -    close_pdf_output_file ();
    5.94 -  out = panda_open (name, "w");
    5.95 -  if (! out)
    5.96 +  output_file_t *o;
    5.97 +
    5.98 +  if (out && (strcmp (name, out->name) == 0))
    5.99 +    return (1);
   5.100 +  for (o = output_files; o; o = o->next)
   5.101 +    if (strcmp (name, o->name) == 0)
   5.102 +      {
   5.103 +	out = o;
   5.104 +	return (1);
   5.105 +      }
   5.106 +  o = calloc (1, sizeof (output_file_t));
   5.107 +  if (! 0)
   5.108      {
   5.109 +      fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
   5.110 +      return (0);
   5.111 +   }
   5.112 +
   5.113 +  o->name = strdup (name);
   5.114 +  if (! o->name)
   5.115 +    {
   5.116 +      fprintf (stderr, "can't strdup output filename '%s'\n", name);
   5.117 +      free (o);
   5.118        return (0);
   5.119      }
   5.120 +
   5.121 +  o->pdf = panda_open (name, "w");
   5.122 +  if (! o->pdf)
   5.123 +    {
   5.124 +      fprintf (stderr, "can't open output file '%s'\n", name);
   5.125 +      free (o->name);
   5.126 +      free (o);
   5.127 +      return (0);
   5.128 +    }
   5.129 +
   5.130 +  /* prepend new output file onto list */
   5.131 +  o->next = output_files;
   5.132 +  output_files = o;
   5.133 +
   5.134 +  out = o;
   5.135    return (1);
   5.136  }
   5.137  
   5.138 @@ -178,19 +244,19 @@
   5.139      }
   5.140  
   5.141  #if 0
   5.142 -  TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
   5.143 -  TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
   5.144 -  TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
   5.145 +  TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length);
   5.146 +  TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width);
   5.147 +  TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config);
   5.148  
   5.149 -  TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
   5.150 +  TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length);
   5.151  
   5.152 -  TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   5.153 -  TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
   5.154 -  TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
   5.155 +  TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   5.156 +  TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution);
   5.157 +  TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution);
   5.158  
   5.159 -  TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   5.160 -  TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   5.161 -  TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   5.162 +  TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   5.163 +  TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   5.164 +  TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   5.165  #endif
   5.166  
   5.167    buffer = _TIFFmalloc (TIFFScanlineSize (in));
   5.168 @@ -204,7 +270,7 @@
   5.169      {
   5.170        TIFFReadScanline (in, buffer, row, 0);
   5.171  #if 0
   5.172 -      TIFFWriteScanline (out, buffer, row, 0);
   5.173 +      TIFFWriteScanline (out->pdf, buffer, row, 0);
   5.174  #endif
   5.175      }
   5.176  
   5.177 @@ -231,10 +297,19 @@
   5.178      }
   5.179  
   5.180    if (! parse_spec_file (argv [1]))
   5.181 -    goto fail;
   5.182 +    {
   5.183 +      result = 2;
   5.184 +      goto fail;
   5.185 +    }
   5.186 +
   5.187 +  if (! process_specs ())
   5.188 +    {
   5.189 +      result = 3;
   5.190 +      goto fail;
   5.191 +    }
   5.192    
   5.193   fail:
   5.194    close_tiff_input_file ();
   5.195 -  close_pdf_output_file ();
   5.196 +  close_pdf_output_files ();
   5.197    return (result);
   5.198  }
     6.1 --- a/tumble.h	Mon Dec 31 16:44:24 2001 +0000
     6.2 +++ b/tumble.h	Tue Jan 01 03:44:40 2002 +0000
     6.3 @@ -9,7 +9,6 @@
     6.4  boolean close_tiff_input_file (void);
     6.5  
     6.6  boolean open_pdf_output_file (char *name);
     6.7 -boolean close_pdf_output_file (void);
     6.8  
     6.9  void process_page_numbers (int page_index,
    6.10  			   int count,