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