From 8aa6b77b28185e8e4221475038e573c6c0b96a9a Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Wed, 12 Mar 2003 10:57:06 +0000 Subject: [PATCH] start of JPEG support. --- Makefile | 7 ++- pdf_jpeg.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 pdf_jpeg.c diff --git a/Makefile b/Makefile index 7830fc9..3a49d40 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # t2p: build a PDF file out of one or more TIFF Class F Group 4 files # Makefile -# $Id: Makefile,v 1.23 2003/03/11 22:57:46 eric Exp $ +# $Id: Makefile,v 1.24 2003/03/12 02:57:06 eric Exp $ # Copyright 2001, 2002, 2003 Eric Smith # # This program is free software; you can redistribute it and/or modify @@ -63,7 +63,8 @@ TARGETS = t2p CSRCS = t2p.c semantics.c \ bitblt.c bitblt_table_gen.c bitblt_g4.c g4_table_gen.c \ - pdf.c pdf_util.c pdf_prim.c pdf_bookmark.c pdf_name_tree.c pdf_g4.c + pdf.c pdf_util.c pdf_prim.c pdf_bookmark.c pdf_name_tree.c \ + pdf_g4.c pdf_jpeg.c OSRCS = scanner.l parser.y HDRS = t2p.h semantics.h bitblt.h bitblt_tables.h \ pdf.h pdf_private.h pdf_util.h pdf_prim.h pdf_name_tree.h @@ -87,7 +88,7 @@ all: $(TARGETS) $(TEST_TARGETS) t2p: t2p.o scanner.o semantics.o parser.tab.o \ bitblt.o bitblt_g4.o bitblt_tables.o g4_tables.o \ pdf.o pdf_util.o pdf_prim.o pdf_bookmark.o pdf_name_tree.o \ - pdf_g4.o + pdf_g4.o pdf_jpeg.o $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ ifndef DEBUG strip $@ diff --git a/pdf_jpeg.c b/pdf_jpeg.c new file mode 100644 index 0000000..bc7474d --- /dev/null +++ b/pdf_jpeg.c @@ -0,0 +1,175 @@ +/* + * t2p: Create a PDF file from the contents of one or more TIFF + * bilevel image files. The images in the resulting PDF file + * will be compressed using ITU-T T.6 (G4) fax encoding. + * + * PDF routines + * $Id: pdf_jpeg.c,v 1.1 2003/03/12 02:57:06 eric Exp $ + * Copyright 2003 Eric Smith + * + * 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 +#include +#include +#include +#include + + +#include "bitblt.h" +#include "pdf.h" +#include "pdf_util.h" +#include "pdf_prim.h" +#include "pdf_private.h" + + +struct pdf_jpeg_image +{ + double width, height; + double x, y; + FILE *f; + unsigned long Columns; + unsigned long Rows; + char XObject_name [4]; +}; + + +static void pdf_write_jpeg_content_callback (pdf_file_handle pdf_file, + struct pdf_obj *stream, + void *app_data) +{ + struct pdf_jpeg_image *image = app_data; + + /* transformation matrix is: width 0 0 height x y cm */ + pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", + image->width, image->height, + image->x, image->y); + pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", + image->XObject_name); +} + + +#define JPEG_BUFFER_SIZE 8192 + +static void pdf_write_jpeg_image_callback (pdf_file_handle pdf_file, + struct pdf_obj *stream, + void *app_data) +{ + struct pdf_jpeg_image *image = app_data; + FILE *f; + int rlen, wlen; + uint8_t *wp; + uint8_t buffer [8192]; + + while (! feof (image->f)) + { + rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, f); + wp = & buffer [0]; + while (rlen) + { + wlen = fwrite (wp, 1, rlen, pdf_file->f); + if (feof (pdf_file->f)) + pdf_fatal ("unexpected EOF on output file\n"); + if (ferror (pdf_file->f)) + pdf_fatal ("error on output file\n"); + rlen -= wlen; + wp += wlen; + } + if (ferror (f)) + pdf_fatal ("error on input file\n"); + } +} + + +void pdf_write_jpeg_image (pdf_page_handle pdf_page, + double x, + double y, + double width, + double height, + FILE *f) +{ + struct pdf_jpeg_image *image; + + struct pdf_obj *stream; + struct pdf_obj *stream_dict; + struct pdf_obj *decode_parms; + + struct pdf_obj *content_stream; + + image = pdf_calloc (1, sizeof (struct pdf_jpeg_image)); + + image->width = width; + image->height = height; + image->x = x; + image->y = y; + + image->f = f; +#if 0 + image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; + image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; +#endif + + stream_dict = pdf_new_obj (PT_DICTIONARY); + + stream = pdf_new_ind_ref (pdf_page->pdf_file, + pdf_new_stream (pdf_page->pdf_file, + stream_dict, + & pdf_write_jpeg_image_callback, + image)); + + strcpy (& image->XObject_name [0], "Im "); + image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); + + pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); + pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); + pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); + pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); + pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); + pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8)); + + decode_parms = pdf_new_obj (PT_DICTIONARY); + + pdf_set_dict_entry (decode_parms, + "K", + pdf_new_integer (-1)); + + pdf_set_dict_entry (decode_parms, + "Columns", + pdf_new_integer (image->Columns)); + + pdf_set_dict_entry (decode_parms, + "Rows", + pdf_new_integer (image->Rows)); + + pdf_stream_add_filter (stream, "DCTDecode", decode_parms); + + /* the following will write the stream, using our callback function to + get the actual data */ + pdf_write_ind_obj (pdf_page->pdf_file, stream); + + content_stream = pdf_new_ind_ref (pdf_page->pdf_file, + pdf_new_stream (pdf_page->pdf_file, + pdf_new_obj (PT_DICTIONARY), + & pdf_write_jpeg_content_callback, + image)); + + pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); + + pdf_write_ind_obj (pdf_page->pdf_file, content_stream); +} +