handle input and output files properly.
This commit is contained in:
114
semantics.c
114
semantics.c
@@ -478,60 +478,6 @@ static inline int range_count (range_t range)
|
||||
}
|
||||
|
||||
|
||||
void doit (void)
|
||||
{
|
||||
input_image_t *image = NULL;
|
||||
output_page_t *page = NULL;
|
||||
int i = 0;
|
||||
int p = 0;
|
||||
int page_index = 0;
|
||||
input_attributes_t input_attributes;
|
||||
input_modifier_type_t parity;
|
||||
page_label_t *page_label;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if ((! image) || (i >= range_count (image->range)))
|
||||
{
|
||||
if (image)
|
||||
image = image->next;
|
||||
else
|
||||
image = first_input_image;
|
||||
if (! image)
|
||||
return;
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if ((! page) || (p >= range_count (page->range)))
|
||||
{
|
||||
if (page)
|
||||
page = page->next;
|
||||
else
|
||||
page = first_output_page;
|
||||
p = 0;
|
||||
page_label = get_output_page_label (page->output_context);
|
||||
process_page_numbers (page_index,
|
||||
range_count (page->range),
|
||||
page->range.first,
|
||||
page_label);
|
||||
}
|
||||
|
||||
parity = ((image->range.first + i) % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN;
|
||||
|
||||
memset (& input_attributes, 0, sizeof (input_attributes));
|
||||
input_attributes.rotation = get_input_rotation (image->input_context,
|
||||
parity);;
|
||||
|
||||
process_page (image->range.first + i,
|
||||
input_attributes,
|
||||
page->bookmark_list);
|
||||
i++;
|
||||
p++;
|
||||
page_index++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
boolean parse_spec_file (char *fn)
|
||||
{
|
||||
boolean result = 0;
|
||||
@@ -576,3 +522,63 @@ boolean parse_spec_file (char *fn)
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
||||
boolean process_specs (void)
|
||||
{
|
||||
input_image_t *image = NULL;
|
||||
output_page_t *page = NULL;
|
||||
int i = 0;
|
||||
int p = 0;
|
||||
int page_index = 0;
|
||||
input_attributes_t input_attributes;
|
||||
input_modifier_type_t parity;
|
||||
page_label_t *page_label;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if ((! image) || (i >= range_count (image->range)))
|
||||
{
|
||||
if (image)
|
||||
image = image->next;
|
||||
else
|
||||
image = first_input_image;
|
||||
if (! image)
|
||||
return (0);
|
||||
i = 0;
|
||||
if (! open_tiff_input_file (get_input_file (image->input_context)))
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((! page) || (p >= range_count (page->range)))
|
||||
{
|
||||
if (page)
|
||||
page = page->next;
|
||||
else
|
||||
page = first_output_page;
|
||||
p = 0;
|
||||
if (! open_pdf_output_file (get_output_file (page->output_context)))
|
||||
return (0);
|
||||
page_label = get_output_page_label (page->output_context);
|
||||
process_page_numbers (page_index,
|
||||
range_count (page->range),
|
||||
page->range.first,
|
||||
page_label);
|
||||
}
|
||||
|
||||
parity = ((image->range.first + i) % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN;
|
||||
|
||||
memset (& input_attributes, 0, sizeof (input_attributes));
|
||||
input_attributes.rotation = get_input_rotation (image->input_context,
|
||||
parity);;
|
||||
|
||||
process_page (image->range.first + i,
|
||||
input_attributes,
|
||||
page->bookmark_list);
|
||||
i++;
|
||||
p++;
|
||||
page_index++;
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -46,9 +46,6 @@ extern int line; /* line number in spec file */
|
||||
extern int bookmark_level;
|
||||
|
||||
|
||||
boolean parse_spec_file (char *fn);
|
||||
|
||||
|
||||
/* semantic routines for input statements */
|
||||
void input_push_context (void);
|
||||
void input_pop_context (void);
|
||||
@@ -64,3 +61,8 @@ void output_set_file (char *name);
|
||||
void output_set_bookmark (char *name);
|
||||
void output_set_page_label (page_label_t label);
|
||||
void output_pages (range_t range);
|
||||
|
||||
|
||||
/* functions to be called from main program: */
|
||||
boolean parse_spec_file (char *fn);
|
||||
boolean process_specs (void);
|
||||
|
||||
123
t2p.c
123
t2p.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
|
||||
* Main program
|
||||
* $Id: t2p.c,v 1.7 2001/12/31 08:44:24 eric Exp $
|
||||
* $Id: t2p.c,v 1.8 2001/12/31 19:44:40 eric Exp $
|
||||
* Copyright 2001 Eric Smith <eric@brouhaha.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@@ -33,49 +33,115 @@
|
||||
#include "tiff2pdf.h"
|
||||
|
||||
|
||||
typedef struct output_file_t
|
||||
{
|
||||
struct output_file_t *next;
|
||||
char *name;
|
||||
panda_pdf *pdf;
|
||||
} output_file_t;
|
||||
|
||||
|
||||
char *in_filename;
|
||||
TIFF *in;
|
||||
panda_pdf *out;
|
||||
output_file_t *output_files;
|
||||
output_file_t *out;
|
||||
/* panda_pdf *out; */
|
||||
|
||||
|
||||
boolean close_tiff_input_file (void)
|
||||
{
|
||||
if (in)
|
||||
TIFFClose (in);
|
||||
{
|
||||
free (in_filename);
|
||||
TIFFClose (in);
|
||||
}
|
||||
in = NULL;
|
||||
in_filename = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
boolean open_tiff_input_file (char *name)
|
||||
{
|
||||
if (in)
|
||||
close_tiff_input_file ();
|
||||
{
|
||||
if (strcmp (name, in_filename) == 0)
|
||||
return (1);
|
||||
close_tiff_input_file ();
|
||||
}
|
||||
in_filename = strdup (name);
|
||||
if (! in_filename)
|
||||
{
|
||||
fprintf (stderr, "can't strdup input filename '%s'\n", name);
|
||||
return (0);
|
||||
}
|
||||
in = TIFFOpen (name, "r");
|
||||
if (! in)
|
||||
{
|
||||
fprintf (stderr, "can't open input file '%s'\n", name);
|
||||
free (in_filename);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
boolean close_pdf_output_file (void)
|
||||
boolean close_pdf_output_files (void)
|
||||
{
|
||||
if (out)
|
||||
panda_close (out);
|
||||
output_file_t *o, *n;
|
||||
|
||||
for (o = output_files; o; o = n)
|
||||
{
|
||||
n = o->next;
|
||||
panda_close (o->pdf);
|
||||
free (o->name);
|
||||
free (o);
|
||||
}
|
||||
out = NULL;
|
||||
output_files = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
boolean open_pdf_output_file (char *name)
|
||||
{
|
||||
if (out)
|
||||
close_pdf_output_file ();
|
||||
out = panda_open (name, "w");
|
||||
if (! out)
|
||||
output_file_t *o;
|
||||
|
||||
if (out && (strcmp (name, out->name) == 0))
|
||||
return (1);
|
||||
for (o = output_files; o; o = o->next)
|
||||
if (strcmp (name, o->name) == 0)
|
||||
{
|
||||
out = o;
|
||||
return (1);
|
||||
}
|
||||
o = calloc (1, sizeof (output_file_t));
|
||||
if (! 0)
|
||||
{
|
||||
fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
|
||||
return (0);
|
||||
}
|
||||
|
||||
o->name = strdup (name);
|
||||
if (! o->name)
|
||||
{
|
||||
fprintf (stderr, "can't strdup output filename '%s'\n", name);
|
||||
free (o);
|
||||
return (0);
|
||||
}
|
||||
|
||||
o->pdf = panda_open (name, "w");
|
||||
if (! o->pdf)
|
||||
{
|
||||
fprintf (stderr, "can't open output file '%s'\n", name);
|
||||
free (o->name);
|
||||
free (o);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* prepend new output file onto list */
|
||||
o->next = output_files;
|
||||
output_files = o;
|
||||
|
||||
out = o;
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -178,19 +244,19 @@ boolean process_page (int image, /* range 1 .. n */
|
||||
}
|
||||
|
||||
#if 0
|
||||
TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
|
||||
TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
|
||||
TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
|
||||
TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length);
|
||||
TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width);
|
||||
TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config);
|
||||
|
||||
TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
|
||||
TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length);
|
||||
|
||||
TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
|
||||
TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
|
||||
TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
|
||||
TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
|
||||
TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution);
|
||||
TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution);
|
||||
|
||||
TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
|
||||
TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
|
||||
TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
|
||||
TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
|
||||
TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
|
||||
TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
|
||||
#endif
|
||||
|
||||
buffer = _TIFFmalloc (TIFFScanlineSize (in));
|
||||
@@ -204,7 +270,7 @@ boolean process_page (int image, /* range 1 .. n */
|
||||
{
|
||||
TIFFReadScanline (in, buffer, row, 0);
|
||||
#if 0
|
||||
TIFFWriteScanline (out, buffer, row, 0);
|
||||
TIFFWriteScanline (out->pdf, buffer, row, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -231,10 +297,19 @@ int main (int argc, char *argv[])
|
||||
}
|
||||
|
||||
if (! parse_spec_file (argv [1]))
|
||||
goto fail;
|
||||
{
|
||||
result = 2;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (! process_specs ())
|
||||
{
|
||||
result = 3;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fail:
|
||||
close_tiff_input_file ();
|
||||
close_pdf_output_file ();
|
||||
close_pdf_output_files ();
|
||||
return (result);
|
||||
}
|
||||
|
||||
1
t2p.h
1
t2p.h
@@ -9,7 +9,6 @@ boolean open_tiff_input_file (char *name);
|
||||
boolean close_tiff_input_file (void);
|
||||
|
||||
boolean open_pdf_output_file (char *name);
|
||||
boolean close_pdf_output_file (void);
|
||||
|
||||
void process_page_numbers (int page_index,
|
||||
int count,
|
||||
|
||||
123
tumble.c
123
tumble.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
|
||||
* Main program
|
||||
* $Id: tumble.c,v 1.7 2001/12/31 08:44:24 eric Exp $
|
||||
* $Id: tumble.c,v 1.8 2001/12/31 19:44:40 eric Exp $
|
||||
* Copyright 2001 Eric Smith <eric@brouhaha.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@@ -33,49 +33,115 @@
|
||||
#include "tiff2pdf.h"
|
||||
|
||||
|
||||
typedef struct output_file_t
|
||||
{
|
||||
struct output_file_t *next;
|
||||
char *name;
|
||||
panda_pdf *pdf;
|
||||
} output_file_t;
|
||||
|
||||
|
||||
char *in_filename;
|
||||
TIFF *in;
|
||||
panda_pdf *out;
|
||||
output_file_t *output_files;
|
||||
output_file_t *out;
|
||||
/* panda_pdf *out; */
|
||||
|
||||
|
||||
boolean close_tiff_input_file (void)
|
||||
{
|
||||
if (in)
|
||||
TIFFClose (in);
|
||||
{
|
||||
free (in_filename);
|
||||
TIFFClose (in);
|
||||
}
|
||||
in = NULL;
|
||||
in_filename = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
boolean open_tiff_input_file (char *name)
|
||||
{
|
||||
if (in)
|
||||
close_tiff_input_file ();
|
||||
{
|
||||
if (strcmp (name, in_filename) == 0)
|
||||
return (1);
|
||||
close_tiff_input_file ();
|
||||
}
|
||||
in_filename = strdup (name);
|
||||
if (! in_filename)
|
||||
{
|
||||
fprintf (stderr, "can't strdup input filename '%s'\n", name);
|
||||
return (0);
|
||||
}
|
||||
in = TIFFOpen (name, "r");
|
||||
if (! in)
|
||||
{
|
||||
fprintf (stderr, "can't open input file '%s'\n", name);
|
||||
free (in_filename);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
boolean close_pdf_output_file (void)
|
||||
boolean close_pdf_output_files (void)
|
||||
{
|
||||
if (out)
|
||||
panda_close (out);
|
||||
output_file_t *o, *n;
|
||||
|
||||
for (o = output_files; o; o = n)
|
||||
{
|
||||
n = o->next;
|
||||
panda_close (o->pdf);
|
||||
free (o->name);
|
||||
free (o);
|
||||
}
|
||||
out = NULL;
|
||||
output_files = NULL;
|
||||
return (1);
|
||||
}
|
||||
|
||||
boolean open_pdf_output_file (char *name)
|
||||
{
|
||||
if (out)
|
||||
close_pdf_output_file ();
|
||||
out = panda_open (name, "w");
|
||||
if (! out)
|
||||
output_file_t *o;
|
||||
|
||||
if (out && (strcmp (name, out->name) == 0))
|
||||
return (1);
|
||||
for (o = output_files; o; o = o->next)
|
||||
if (strcmp (name, o->name) == 0)
|
||||
{
|
||||
out = o;
|
||||
return (1);
|
||||
}
|
||||
o = calloc (1, sizeof (output_file_t));
|
||||
if (! 0)
|
||||
{
|
||||
fprintf (stderr, "can't calloc output file struct for '%s'\n", name);
|
||||
return (0);
|
||||
}
|
||||
|
||||
o->name = strdup (name);
|
||||
if (! o->name)
|
||||
{
|
||||
fprintf (stderr, "can't strdup output filename '%s'\n", name);
|
||||
free (o);
|
||||
return (0);
|
||||
}
|
||||
|
||||
o->pdf = panda_open (name, "w");
|
||||
if (! o->pdf)
|
||||
{
|
||||
fprintf (stderr, "can't open output file '%s'\n", name);
|
||||
free (o->name);
|
||||
free (o);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* prepend new output file onto list */
|
||||
o->next = output_files;
|
||||
output_files = o;
|
||||
|
||||
out = o;
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -178,19 +244,19 @@ boolean process_page (int image, /* range 1 .. n */
|
||||
}
|
||||
|
||||
#if 0
|
||||
TIFFSetField (out, TIFFTAG_IMAGELENGTH, image_length);
|
||||
TIFFSetField (out, TIFFTAG_IMAGEWIDTH, image_width);
|
||||
TIFFSetField (out, TIFFTAG_PLANARCONFIG, planar_config);
|
||||
TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length);
|
||||
TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width);
|
||||
TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config);
|
||||
|
||||
TIFFSetField (out, TIFFTAG_ROWSPERSTRIP, image_length);
|
||||
TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length);
|
||||
|
||||
TIFFSetField (out, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
|
||||
TIFFSetField (out, TIFFTAG_XRESOLUTION, x_resolution);
|
||||
TIFFSetField (out, TIFFTAG_YRESOLUTION, y_resolution);
|
||||
TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
|
||||
TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution);
|
||||
TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution);
|
||||
|
||||
TIFFSetField (out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
|
||||
TIFFSetField (out, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
|
||||
TIFFSetField (out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
|
||||
TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
|
||||
TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
|
||||
TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
|
||||
#endif
|
||||
|
||||
buffer = _TIFFmalloc (TIFFScanlineSize (in));
|
||||
@@ -204,7 +270,7 @@ boolean process_page (int image, /* range 1 .. n */
|
||||
{
|
||||
TIFFReadScanline (in, buffer, row, 0);
|
||||
#if 0
|
||||
TIFFWriteScanline (out, buffer, row, 0);
|
||||
TIFFWriteScanline (out->pdf, buffer, row, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -231,10 +297,19 @@ int main (int argc, char *argv[])
|
||||
}
|
||||
|
||||
if (! parse_spec_file (argv [1]))
|
||||
goto fail;
|
||||
{
|
||||
result = 2;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (! process_specs ())
|
||||
{
|
||||
result = 3;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fail:
|
||||
close_tiff_input_file ();
|
||||
close_pdf_output_file ();
|
||||
close_pdf_output_files ();
|
||||
return (result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user