1
0
mirror of synced 2026-03-04 10:45:02 +00:00

more JPEG support. added input file handler API.

This commit is contained in:
Eric Smith
2003-03-20 06:54:08 +00:00
parent e68a6fb9a0
commit e08fd46185
9 changed files with 774 additions and 451 deletions

View File

@@ -1,6 +1,6 @@
# tumble: build a PDF file from image files
# Makefile
# $Id: Makefile,v 1.34 2003/03/16 07:40:00 eric Exp $
# $Id: Makefile,v 1.35 2003/03/19 22:54:07 eric Exp $
# Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
#
# This program is free software; you can redistribute it and/or modify
@@ -23,7 +23,7 @@
# "0" value is considered true by make, so to disable conditionals comment
# them out or set them to a null string.
#DEBUG=1
DEBUG=1
#EFENCE=1
#STATIC=1
@@ -57,19 +57,20 @@ YFLAGS = -d -v
# let me know why so I can improve this Makefile.
# -----------------------------------------------------------------------------
VERSION = 0.28
VERSION = 0.29
PACKAGE = tumble
TARGETS = tumble
CSRCS = tumble.c semantics.c \
tumble_input.c tumble_tiff.c tumble_jpeg.c \
bitblt.c bitblt_table_gen.c bitblt_g4.c g4_table_gen.c \
pdf.c pdf_util.c pdf_prim.c pdf_name_tree.c \
pdf_bookmark.c pdf_page_label.c \
pdf_text.c pdf_g4.c pdf_jpeg.c
OSRCS = scanner.l parser.y
HDRS = tumble.h semantics.h bitblt.h bitblt_tables.h \
HDRS = tumble.h tumble_input.h semantics.h bitblt.h bitblt_tables.h \
pdf.h pdf_private.h pdf_util.h pdf_prim.h pdf_name_tree.h
MISC = COPYING README INSTALL Makefile
@@ -90,7 +91,9 @@ AUTO_MISC = parser.output
all: $(TARGETS) $(TEST_TARGETS)
tumble: tumble.o scanner.o semantics.o parser.tab.o \
tumble: tumble.o semantics.o \
tumble_input.o tumble_tiff.o tumble_jpeg.o \
scanner.o parser.tab.o \
bitblt.o bitblt_g4.o bitblt_tables.o g4_tables.o \
pdf.o pdf_util.o pdf_prim.o pdf_name_tree.o \
pdf_bookmark.o pdf_page_label.o \

17
README
View File

@@ -1,15 +1,14 @@
tumble: build a PDF file from image files
Copyright 2003 Eric Smith <eric@brouhaha.com>
$Id: README,v 1.1 2003/03/16 07:37:16 eric Exp $
$Id: README,v 1.2 2003/03/19 22:54:07 eric Exp $
Tumble is a utility to construct PDF files from one or more image
files. Currently the only input image format supported is black &
white TIFF files, which may be either single- or multi-page files. In
the future additional file formats will be supported, including gray
scale and color. Black and white images will be encoded in the PDF
output using lossless Group 4 fax compression (ITU-T recommendation
T.6). This provides a very good compression ratio for text and line
art.
files. Supported input image file formats are JPEG, and black and
white TIFF (single- or multi-page). Black and white images will be
encoded in the PDF output using lossless Group 4 fax compression
(ITU-T recommendation T.6). This provides a very good compression
ratio for text and line art. JPEG images will be preserved with the
original coding.
The current version of Tumble will only work on little-endian systems,
such as x86, VAX, and Alpha. The byte order dependencies will be fixed
@@ -21,7 +20,7 @@ used which allows for more control over the files and options.
The general command line syntax is:
tumble [options] <input.tif>... -o <output.pdf>
tumble [options] <input>... -o <output.pdf>
The options in this mode are:

9
pdf.h
View File

@@ -2,7 +2,7 @@
* tumble: build a PDF file from image files
*
* PDF routines
* $Id: pdf.h,v 1.11 2003/03/14 00:57:40 eric Exp $
* $Id: pdf.h,v 1.12 2003/03/19 22:54:07 eric Exp $
* Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
@@ -22,6 +22,13 @@
*/
#define POINTS_PER_INCH 72
/* page size limited by Acrobat Reader to 45 inches on a side */
#define PAGE_MAX_INCHES 45
#define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
typedef struct pdf_file *pdf_file_handle;
typedef struct pdf_page *pdf_page_handle;

450
tumble.c
View File

@@ -2,7 +2,7 @@
* tumble: build a PDF file from image files
*
* Main program
* $Id: tumble.c,v 1.37 2003/03/19 07:39:55 eric Exp $
* $Id: tumble.c,v 1.38 2003/03/19 22:54:07 eric Exp $
* Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
@@ -30,25 +30,17 @@
#include <string.h>
#include <unistd.h>
#include <tiffio.h>
#define TIFF_REVERSE_BITS
#include "bitblt.h"
#include "semantics.h"
#include "parser.tab.h"
#include "tumble.h"
#include "bitblt.h"
#include "pdf.h"
#include "tumble_input.h"
#define MAX_INPUT_FILES 5000
#define POINTS_PER_INCH 72
/* page size limited by Acrobat Reader to 45 inches on a side */
#define PAGE_MAX_INCHES 45
#define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
typedef struct output_file_t
{
struct output_file_t *next;
@@ -60,18 +52,6 @@ typedef struct output_file_t
int verbose;
typedef enum
{
INPUT_FILE_TYPE_NONE,
INPUT_FILE_TYPE_TIFF,
INPUT_FILE_TYPE_JPEG
} input_file_type_t;
char *in_filename;
input_file_type_t in_type;
FILE *in;
TIFF *tiff_in;
output_file_t *output_files;
output_file_t *out;
@@ -79,7 +59,6 @@ output_file_t *out;
char *progname;
bool close_tiff_input_file (void);
bool close_pdf_output_files (void);
@@ -126,142 +105,6 @@ void fatal (int ret, char *format, ...)
}
bool close_tiff_input_file (void)
{
TIFFClose (tiff_in);
return (1);
}
bool open_tiff_input_file (FILE *f, char *name)
{
tiff_in = TIFFFdOpen (fileno (f), name, "r");
if (! tiff_in)
{
fprintf (stderr, "can't open input file '%s'\n", name);
free (in_filename);
return (0);
}
in_type = INPUT_FILE_TYPE_TIFF;
return (1);
}
bool close_jpeg_input_file (void)
{
return (1);
}
bool open_jpeg_input_file (FILE *f, char *name)
{
in_type = INPUT_FILE_TYPE_JPEG;
return (1);
}
bool open_input_file (char *name)
{
bool result;
uint8_t buf [2];
size_t l;
if (in)
{
if (strcmp (name, in_filename) == 0)
return (1);
close_input_file ();
}
in_filename = strdup (name);
if (! in_filename)
{
fprintf (stderr, "can't strdup input filename '%s'\n", name);
return (0);
}
in = fopen (name, "rb");
if (! in)
return (0);
l = fread (& buf [0], 1, sizeof (buf), in);
if (l != sizeof (buf))
return (0);
rewind (in);
if ((buf [0] == 0x49) && (buf [1] == 0x49))
result = open_tiff_input_file (in, name);
else if ((buf [0] == 0xff) && (buf [1] == 0xd8))
result = open_jpeg_input_file (in, name);
else
{
fprintf (stderr, "unrecognized file header in file '%s'\n", name);
result = 0;
}
if (! result)
{
if (in)
fclose (in);
in = NULL;
in_type = INPUT_FILE_TYPE_NONE;
}
return (result);
}
bool close_input_file (void)
{
bool result;
switch (in_type)
{
case INPUT_FILE_TYPE_NONE:
return (1);
case INPUT_FILE_TYPE_TIFF:
result = close_tiff_input_file ();
break;
case INPUT_FILE_TYPE_JPEG:
result = close_jpeg_input_file ();
break;
default:
fatal (3, "internal error: bad input file type\n");
}
if (in_filename)
free (in_filename);
fclose (in);
in = NULL;
return (result);
}
bool last_tiff_input_page (void)
{
return (TIFFLastDirectory (tiff_in));
}
bool last_jpeg_input_page (void)
{
return (1);
}
bool last_input_page (void)
{
switch (in_type)
{
case INPUT_FILE_TYPE_TIFF:
return (last_tiff_input_page ());
case INPUT_FILE_TYPE_JPEG:
return (last_jpeg_input_page ());
default:
fatal (3, "internal error: bad input file type\n");
}
}
bool close_pdf_output_files (void)
{
output_file_t *o, *n;
@@ -335,287 +178,23 @@ bool open_pdf_output_file (char *name,
}
/* frees original! */
static Bitmap *resize_bitmap (Bitmap *src,
double x_resolution,
double y_resolution,
input_attributes_t input_attributes)
{
Rect src_rect;
Point dest_min;
Bitmap *dest;
int width_pixels = input_attributes.page_size.width * x_resolution;
int height_pixels = input_attributes.page_size.height * y_resolution;
src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
src_rect.max.x = src_rect.min.x + width_pixels;
src_rect.max.y = src_rect.min.y + height_pixels;
dest_min.x = 0;
dest_min.y = 0;
dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
free_bitmap (src);
return (dest);
}
/* "in place" rotation */
static void rotate_bitmap (Bitmap *src,
input_attributes_t input_attributes)
{
switch (input_attributes.rotation)
{
case 0: break;
case 90: rot_90 (src); break;
case 180: rot_180 (src); break;
case 270: rot_270 (src); break;
default:
fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
}
}
#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
static pdf_page_handle process_tiff_page (int image, /* range 1 .. n */
input_attributes_t input_attributes)
{
uint32_t image_length, image_width;
uint32_t dest_image_length, dest_image_width;
#ifdef CHECK_DEPTH
uint32_t image_depth;
#endif
uint16_t samples_per_pixel;
uint16_t bits_per_sample;
uint16_t planar_config;
uint16_t resolution_unit;
float x_resolution, y_resolution;
double dest_x_resolution, dest_y_resolution;
double width_points, height_points; /* really 1/72 inch units rather than
points */
Rect rect;
Bitmap *bitmap = NULL;
int row;
pdf_page_handle page = NULL;
if (! TIFFSetDirectory (tiff_in, image - 1))
{
fprintf (stderr, "can't find page %d of input file\n", image);
goto fail;
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGELENGTH, & image_length))
{
fprintf (stderr, "can't get image length\n");
goto fail;
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGEWIDTH, & image_width))
{
fprintf (stderr, "can't get image width\n");
goto fail;
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
{
fprintf (stderr, "can't get samples per pixel\n");
goto fail;
}
#ifdef CHECK_DEPTH
if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGEDEPTH, & image_depth))
{
fprintf (stderr, "can't get image depth\n");
goto fail;
}
#endif
if (1 != TIFFGetField (tiff_in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
{
fprintf (stderr, "can't get bits per sample\n");
goto fail;
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_PLANARCONFIG, & planar_config))
planar_config = 1;
if (1 != TIFFGetField (tiff_in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
resolution_unit = 2;
if (1 != TIFFGetField (tiff_in, TIFFTAG_XRESOLUTION, & x_resolution))
x_resolution = 300;
if (1 != TIFFGetField (tiff_in, TIFFTAG_YRESOLUTION, & y_resolution))
y_resolution = 300;
if (samples_per_pixel != 1)
{
fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
goto fail;
}
#ifdef CHECK_DEPTH
if (image_depth != 1)
{
fprintf (stderr, "image depth %u, must be 1\n", image_depth);
goto fail;
}
#endif
if (bits_per_sample != 1)
{
fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
goto fail;
}
if (planar_config != 1)
{
fprintf (stderr, "planar config %u, must be 1\n", planar_config);
goto fail;
}
if (input_attributes.has_resolution)
{
x_resolution = input_attributes.x_resolution;
y_resolution = input_attributes.y_resolution;
}
if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
{
dest_image_width = image_length;
dest_image_length = image_width;
dest_x_resolution = y_resolution;
dest_y_resolution = x_resolution;
SWAP (double, width_points, height_points); /* $$$ not yet set!!! */
}
else
{
dest_image_width = image_width;
dest_image_length = image_length;
dest_x_resolution = x_resolution;
dest_y_resolution = y_resolution;
}
rect.min.x = 0;
rect.min.y = 0;
rect.max.x = image_width;
rect.max.y = image_length;
bitmap = create_bitmap (& rect);
if (! bitmap)
{
fprintf (stderr, "can't allocate bitmap\n");
goto fail;
}
for (row = 0; row < image_length; row++)
if (1 != TIFFReadScanline (tiff_in,
bitmap->bits + row * bitmap->row_words,
row,
0))
{
fprintf (stderr, "can't read TIFF scanline\n");
goto fail;
}
#ifdef TIFF_REVERSE_BITS
reverse_bits ((uint8_t *) bitmap->bits,
image_length * bitmap->row_words * sizeof (word_t));
#endif /* TIFF_REVERSE_BITS */
#if 0
if (input_attributes.has_page_size)
bitmap = resize_bitmap (bitmap,
x_resolution,
y_resolution,
input_attributes);
#endif
rotate_bitmap (bitmap,
input_attributes);
width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH;
height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH;
if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
{
fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
goto fail;
}
page = pdf_new_page (out->pdf, width_points, height_points);
#if 0
pdf_write_text (page);
#else
pdf_write_g4_fax_image (page,
0, 0, /* x, y */
width_points, height_points,
bitmap,
0, /* ImageMask */
0, 0, 0, /* r, g, b */
0); /* BlackIs1 */
#endif
if (bitmap)
free_bitmap (bitmap);
return (page);
fail:
if (bitmap)
free_bitmap (bitmap);
return (NULL);
}
pdf_page_handle process_jpeg_page (int image, /* range 1 .. n */
input_attributes_t input_attributes)
{
pdf_page_handle page;
double width_points, height_points; /* really 1/72 inch units rather than
points */
/* $$$ need to get these from somewhere else, hardcoded for now */
width_points = 4 * 72.0;
height_points = 4 * 72.0;
page = pdf_new_page (out->pdf, width_points, height_points);
pdf_write_jpeg_image (page,
0, 0, /* x, y */
width_points, height_points,
in);
return (page);
}
bool process_page (int image, /* range 1 .. n */
input_attributes_t input_attributes,
bookmark_t *bookmarks,
page_label_t *page_label)
{
pdf_page_handle page;
image_info_t image_info;
if (! get_image_info (image, input_attributes, & image_info))
return (0);
switch (in_type)
{
case INPUT_FILE_TYPE_TIFF:
page = process_tiff_page (image, input_attributes);
break;
case INPUT_FILE_TYPE_JPEG:
page = process_jpeg_page (image, input_attributes);
break;
default:
fatal (3, "internal error: bad input file type\n");
}
page = pdf_new_page (out->pdf,
image_info.width_points,
image_info.height_points);
if (! process_image (image, input_attributes, & image_info, page))
return (0);
while (bookmarks)
{
@@ -769,6 +348,9 @@ int main (int argc, char *argv[])
pdf_init ();
init_tiff_handler ();
init_jpeg_handler ();
while (--argc)
{
if (argv [1][0] == '-')

View File

@@ -1,7 +1,7 @@
/*
* tumble: build a PDF file from image files
*
* $Id: tumble.h,v 1.16 2003/03/19 07:39:55 eric Exp $
* $Id: tumble.h,v 1.17 2003/03/19 22:54:07 eric Exp $
* Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
@@ -17,7 +17,8 @@
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA */
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
extern int verbose;

140
tumble_input.c Normal file
View File

@@ -0,0 +1,140 @@
/*
* tumble: build a PDF file from image files
*
* Input handler dispatch
* $Id: tumble_input.c,v 1.1 2003/03/19 22:54:08 eric Exp $
* Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "semantics.h"
#include "tumble.h"
#include "bitblt.h"
#include "pdf.h"
#include "tumble_input.h"
#define MAX_INPUT_HANDLERS 10
static int input_handler_count = 0;
static input_handler_t *input_handlers [MAX_INPUT_HANDLERS];
static char *in_filename;
static FILE *in;
static input_handler_t *current_input_handler;
void install_input_handler (input_handler_t *handler)
{
if (input_handler_count >= MAX_INPUT_HANDLERS)
fprintf (stderr, "Too many input handlers, table only has room for %d\n", MAX_INPUT_HANDLERS);
else
input_handlers [input_handler_count++] = handler;
}
bool open_input_file (char *name)
{
int i;
if (in)
{
if (strcmp (name, in_filename) == 0)
return (1);
close_input_file ();
}
in_filename = strdup (name);
if (! in_filename)
{
fprintf (stderr, "can't strdup input filename '%s'\n", name);
goto fail;
}
in = fopen (name, "rb");
if (! in)
goto fail;
for (i = 0; i < input_handler_count; i++)
{
if (input_handlers [i]->open_input_file (in, name))
break;
}
if (i >= input_handler_count)
{
fprintf (stderr, "unrecognized format for input file '%s'\n", name);
goto fail;
}
current_input_handler = input_handlers [i];
return (1);
fail:
if (in)
fclose (in);
in = NULL;
return (0);
}
bool close_input_file (void)
{
bool result;
result = current_input_handler->close_input_file ();
if (in_filename)
free (in_filename);
fclose (in);
in = NULL;
return (result);
}
bool last_input_page (void)
{
return (current_input_handler->last_input_page ());
}
bool get_image_info (int image,
input_attributes_t input_attributes,
image_info_t *image_info)
{
return (current_input_handler->get_image_info (image,
input_attributes,
image_info));
}
bool process_image (int image,
input_attributes_t input_attributes,
image_info_t *image_info,
pdf_page_handle page)
{
return (current_input_handler->process_image (image,
input_attributes,
image_info,
page));
}

64
tumble_input.h Normal file
View File

@@ -0,0 +1,64 @@
/*
* tumble: build a PDF file from image files
*
* $Id: tumble_input.h,v 1.1 2003/03/19 22:54:08 eric Exp $
* Copyright 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
typedef struct
{
bool color;
uint32_t width_samples, height_samples;
double width_points, height_points;
double x_resolution, y_resolution;
} image_info_t;
typedef struct
{
bool (*open_input_file) (FILE *f, char *name);
bool (*close_input_file) (void);
bool (*last_input_page) (void);
bool (*get_image_info) (int image,
input_attributes_t input_attributes,
image_info_t *image_info);
bool (*process_image) (int image,
input_attributes_t input_attributes,
image_info_t *image_info,
pdf_page_handle page);
} input_handler_t;
void install_input_handler (input_handler_t *handler);
bool open_input_file (char *name);
bool close_input_file (void);
bool last_input_page (void);
bool get_image_info (int image,
input_attributes_t input_attributes,
image_info_t *image_info);
bool process_image (int image,
input_attributes_t input_attributes,
image_info_t *image_info,
pdf_page_handle page);
void init_tiff_handler (void);
void init_jpeg_handler (void);

176
tumble_jpeg.c Normal file
View File

@@ -0,0 +1,176 @@
/*
* tumble: build a PDF file from image files
*
* $Id: tumble_jpeg.c,v 1.1 2003/03/19 22:54:08 eric Exp $
* Copyright 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <jpeglib.h>
#include "semantics.h"
#include "tumble.h"
#include "bitblt.h"
#include "pdf.h"
#include "tumble_input.h"
static FILE *jpeg_f;
static struct jpeg_decompress_struct cinfo;
static struct jpeg_error_mgr jerr;
bool close_jpeg_input_file (void)
{
return (1);
}
bool open_jpeg_input_file (FILE *f, char *name)
{
uint8_t buf [2];
size_t l;
l = fread (& buf [0], 1, sizeof (buf), f);
if (l != sizeof (buf))
return (0);
rewind (f);
if ((buf [0] != 0xff) || (buf [1] != 0xd8))
return (0);
cinfo.err = jpeg_std_error (& jerr);
jpeg_create_decompress (& cinfo);
jpeg_stdio_src (& cinfo, f);
jpeg_read_header (& cinfo, TRUE);
rewind (f);
jpeg_f = f;
return (1);
}
bool last_jpeg_input_page (void)
{
return (1);
}
bool get_jpeg_image_info (int image,
input_attributes_t input_attributes,
image_info_t *image_info)
{
double unit;
switch (cinfo.jpeg_color_space)
{
case JCS_GRAYSCALE:
if (cinfo.num_components != 1)
{
fprintf (stderr, "JPEG grayscale image has %d components, should have 1\n",
cinfo.num_components);
return (0);
}
image_info->color = 0;
break;
case JCS_RGB:
case JCS_YCbCr:
if (cinfo.num_components != 3)
{
fprintf (stderr, "JPEG RGB or YCbCr image has %d components, should have 3\n",
cinfo.num_components);
return (0);
}
image_info->color = 1;
break;
default:
fprintf (stderr, "JPEG color space %d not supported\n", cinfo.jpeg_color_space);
return (0);
}
image_info->width_samples = cinfo.image_width;
image_info->height_samples = cinfo.image_height;
if (cinfo.saw_JFIF_marker & cinfo.density_unit)
{
switch (cinfo.density_unit)
{
case 1: /* samples per inch */
unit = 1.0;
break;
case 2: /* samples per cm */
unit = 2.54;
break;
default:
fprintf (stderr, "JFIF density unit %d not supported\n", cinfo.density_unit);
return (0);
}
image_info->width_points = ((image_info->width_samples * POINTS_PER_INCH) /
(cinfo.X_density * unit));
image_info->height_points = ((image_info->height_samples * POINTS_PER_INCH) /
(cinfo.Y_density * unit));
}
else
{
/* assume 300 DPI - not great, but what else can we do? */
image_info->width_points = (image_info->width_samples * POINTS_PER_INCH) / 300.0;
image_info->height_points = (image_info->height_samples * POINTS_PER_INCH) / 300.0;
}
return (1);
}
bool process_jpeg_image (int image, /* range 1 .. n */
input_attributes_t input_attributes,
image_info_t *image_info,
pdf_page_handle page)
{
pdf_write_jpeg_image (page,
0, 0, /* x, y */
image_info->width_points,
image_info->height_points,
jpeg_f);
return (page);
}
input_handler_t jpeg_handler =
{
open_jpeg_input_file,
close_jpeg_input_file,
last_jpeg_input_page,
get_jpeg_image_info,
process_jpeg_image
};
void init_jpeg_handler (void)
{
install_input_handler (& jpeg_handler);
}

351
tumble_tiff.c Normal file
View File

@@ -0,0 +1,351 @@
/*
* tumble: build a PDF file from image files
*
* $Id: tumble_tiff.c,v 1.1 2003/03/19 22:54:08 eric Exp $
* Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tiffio.h>
#define TIFF_REVERSE_BITS
#include "semantics.h"
#include "tumble.h"
#include "bitblt.h"
#include "pdf.h"
#include "tumble_input.h"
TIFF *tiff_in;
#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
bool close_tiff_input_file (void)
{
TIFFClose (tiff_in);
return (1);
}
bool open_tiff_input_file (FILE *f, char *name)
{
uint8_t buf [2];
size_t l;
l = fread (& buf [0], 1, sizeof (buf), f);
if (l != sizeof (buf))
return (0);
rewind (f);
if ((buf [0] != 0x49) || (buf [1] != 0x49))
return (0);
/* $$$ should we dup the file descriptor here, so that later closing f
won't cause problems? */
tiff_in = TIFFFdOpen (fileno (f), name, "r");
if (! tiff_in)
{
fprintf (stderr, "can't open input file '%s'\n", name);
return (0);
}
return (1);
}
bool last_tiff_input_page (void)
{
return (TIFFLastDirectory (tiff_in));
}
bool get_tiff_image_info (int image,
input_attributes_t input_attributes,
image_info_t *image_info)
{
uint32_t image_height, image_width;
uint16_t samples_per_pixel;
uint16_t bits_per_sample;
uint16_t planar_config;
uint16_t resolution_unit;
float x_resolution, y_resolution;
double dest_x_resolution, dest_y_resolution;
#ifdef CHECK_DEPTH
uint32_t image_depth;
#endif
if (! TIFFSetDirectory (tiff_in, image - 1))
{
fprintf (stderr, "can't find page %d of input file\n", image);
return (0);
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGELENGTH, & image_height))
{
fprintf (stderr, "can't get image height\n");
return (0);
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGEWIDTH, & image_width))
{
fprintf (stderr, "can't get image width\n");
return (0);
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel))
{
fprintf (stderr, "can't get samples per pixel\n");
return (0);
}
#ifdef CHECK_DEPTH
if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGEDEPTH, & image_depth))
{
fprintf (stderr, "can't get image depth\n");
return (0);
}
#endif
if (1 != TIFFGetField (tiff_in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample))
{
fprintf (stderr, "can't get bits per sample\n");
return (0);
}
if (1 != TIFFGetField (tiff_in, TIFFTAG_PLANARCONFIG, & planar_config))
planar_config = 1;
if (1 != TIFFGetField (tiff_in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit))
resolution_unit = 2;
if (1 != TIFFGetField (tiff_in, TIFFTAG_XRESOLUTION, & x_resolution))
x_resolution = 300;
if (1 != TIFFGetField (tiff_in, TIFFTAG_YRESOLUTION, & y_resolution))
y_resolution = 300;
if (samples_per_pixel != 1)
{
fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel);
return (0);
}
#ifdef CHECK_DEPTH
if (image_depth != 1)
{
fprintf (stderr, "image depth %u, must be 1\n", image_depth);
return (0);
}
#endif
if (bits_per_sample != 1)
{
fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample);
return (0);
}
if (planar_config != 1)
{
fprintf (stderr, "planar config %u, must be 1\n", planar_config);
return (0);
}
if (input_attributes.has_resolution)
{
x_resolution = input_attributes.x_resolution;
y_resolution = input_attributes.y_resolution;
}
if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
{
image_info->width_samples = image_height;
image_info->height_samples = image_width;
dest_x_resolution = y_resolution;
dest_y_resolution = x_resolution;
SWAP (double, image_info->width_points, image_info->height_points);
}
else
{
image_info->width_samples = image_width;
image_info->height_samples = image_height;
dest_x_resolution = x_resolution;
dest_y_resolution = y_resolution;
}
image_info->width_points = (image_info->width_samples / dest_x_resolution) * POINTS_PER_INCH;
image_info->height_points = (image_info->height_samples / dest_y_resolution) * POINTS_PER_INCH;
if ((image_info->height_points > PAGE_MAX_POINTS) ||
(image_info->width_points > PAGE_MAX_POINTS))
{
fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
return (0);
}
return (1);
}
/* frees original! */
static Bitmap *resize_bitmap (Bitmap *src,
double x_resolution,
double y_resolution,
input_attributes_t input_attributes)
{
Rect src_rect;
Point dest_min;
Bitmap *dest;
int width_pixels = input_attributes.page_size.width * x_resolution;
int height_pixels = input_attributes.page_size.height * y_resolution;
src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2;
src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2;
src_rect.max.x = src_rect.min.x + width_pixels;
src_rect.max.y = src_rect.min.y + height_pixels;
dest_min.x = 0;
dest_min.y = 0;
dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0);
free_bitmap (src);
return (dest);
}
/* "in place" rotation */
static void rotate_bitmap (Bitmap *src,
input_attributes_t input_attributes)
{
switch (input_attributes.rotation)
{
case 0: break;
case 90: rot_90 (src); break;
case 180: rot_180 (src); break;
case 270: rot_270 (src); break;
default:
fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
}
}
bool process_tiff_image (int image, /* range 1 .. n */
input_attributes_t input_attributes,
image_info_t *image_info,
pdf_page_handle page)
{
Rect rect;
Bitmap *bitmap = NULL;
int row;
rect.min.x = 0;
rect.min.y = 0;
if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
{
rect.max.x = image_info->height_samples;
rect.max.y = image_info->width_samples;
}
else
{
rect.max.x = image_info->width_samples;
rect.max.y = image_info->height_samples;
}
bitmap = create_bitmap (& rect);
if (! bitmap)
{
fprintf (stderr, "can't allocate bitmap\n");
fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples);
goto fail;
}
for (row = 0; row < rect.max.y; row++)
if (1 != TIFFReadScanline (tiff_in,
bitmap->bits + row * bitmap->row_words,
row,
0))
{
fprintf (stderr, "can't read TIFF scanline\n");
goto fail;
}
#ifdef TIFF_REVERSE_BITS
reverse_bits ((uint8_t *) bitmap->bits,
rect.max.y * bitmap->row_words * sizeof (word_t));
#endif /* TIFF_REVERSE_BITS */
#if 0
if (input_attributes.has_page_size)
bitmap = resize_bitmap (bitmap,
x_resolution,
y_resolution,
input_attributes);
#endif
rotate_bitmap (bitmap,
input_attributes);
#if 0
pdf_write_text (page);
#else
pdf_write_g4_fax_image (page,
0, 0, /* x, y */
image_info->width_points, image_info->height_points,
bitmap,
0, /* ImageMask */
0, 0, 0, /* r, g, b */
0); /* BlackIs1 */
#endif
if (bitmap)
free_bitmap (bitmap);
return (page);
fail:
if (bitmap)
free_bitmap (bitmap);
return (NULL);
}
input_handler_t tiff_handler =
{
open_tiff_input_file,
close_tiff_input_file,
last_tiff_input_page,
get_tiff_image_info,
process_tiff_image
};
void init_tiff_handler (void)
{
install_input_handler (& tiff_handler);
}