Wed, 12 Mar 2003 10:57:06 +0000
start of JPEG support.
Makefile | file | annotate | diff | revisions | |
pdf_jpeg.c | file | annotate | diff | revisions |
1.1 diff -r 8400d97f798b -r 311726c12122 Makefile 1.2 --- a/Makefile Wed Mar 12 09:52:09 2003 +0000 1.3 +++ b/Makefile Wed Mar 12 10:57:06 2003 +0000 1.4 @@ -1,6 +1,6 @@ 1.5 # t2p: build a PDF file out of one or more TIFF Class F Group 4 files 1.6 # Makefile 1.7 -# $Id: Makefile,v 1.23 2003/03/11 22:57:46 eric Exp $ 1.8 +# $Id: Makefile,v 1.24 2003/03/12 02:57:06 eric Exp $ 1.9 # Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 1.10 # 1.11 # This program is free software; you can redistribute it and/or modify 1.12 @@ -63,7 +63,8 @@ 1.13 1.14 CSRCS = t2p.c semantics.c \ 1.15 bitblt.c bitblt_table_gen.c bitblt_g4.c g4_table_gen.c \ 1.16 - pdf.c pdf_util.c pdf_prim.c pdf_bookmark.c pdf_name_tree.c pdf_g4.c 1.17 + pdf.c pdf_util.c pdf_prim.c pdf_bookmark.c pdf_name_tree.c \ 1.18 + pdf_g4.c pdf_jpeg.c 1.19 OSRCS = scanner.l parser.y 1.20 HDRS = t2p.h semantics.h bitblt.h bitblt_tables.h \ 1.21 pdf.h pdf_private.h pdf_util.h pdf_prim.h pdf_name_tree.h 1.22 @@ -87,7 +88,7 @@ 1.23 t2p: t2p.o scanner.o semantics.o parser.tab.o \ 1.24 bitblt.o bitblt_g4.o bitblt_tables.o g4_tables.o \ 1.25 pdf.o pdf_util.o pdf_prim.o pdf_bookmark.o pdf_name_tree.o \ 1.26 - pdf_g4.o 1.27 + pdf_g4.o pdf_jpeg.o 1.28 $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ 1.29 ifndef DEBUG 1.30 strip $@
2.1 diff -r 8400d97f798b -r 311726c12122 pdf_jpeg.c 2.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 +++ b/pdf_jpeg.c Wed Mar 12 10:57:06 2003 +0000 2.4 @@ -0,0 +1,175 @@ 2.5 +/* 2.6 + * t2p: Create a PDF file from the contents of one or more TIFF 2.7 + * bilevel image files. The images in the resulting PDF file 2.8 + * will be compressed using ITU-T T.6 (G4) fax encoding. 2.9 + * 2.10 + * PDF routines 2.11 + * $Id: pdf_jpeg.c,v 1.1 2003/03/12 02:57:06 eric Exp $ 2.12 + * Copyright 2003 Eric Smith <eric@brouhaha.com> 2.13 + * 2.14 + * This program is free software; you can redistribute it and/or modify 2.15 + * it under the terms of the GNU General Public License version 2 as 2.16 + * published by the Free Software Foundation. Note that permission is 2.17 + * not granted to redistribute this program under the terms of any 2.18 + * other version of the General Public License. 2.19 + * 2.20 + * This program is distributed in the hope that it will be useful, 2.21 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 2.22 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2.23 + * GNU General Public License for more details. 2.24 + * 2.25 + * You should have received a copy of the GNU General Public License 2.26 + * along with this program; if not, write to the Free Software 2.27 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 2.28 + */ 2.29 + 2.30 + 2.31 +#include <stdbool.h> 2.32 +#include <stdint.h> 2.33 +#include <stdio.h> 2.34 +#include <stdlib.h> 2.35 +#include <string.h> 2.36 + 2.37 + 2.38 +#include "bitblt.h" 2.39 +#include "pdf.h" 2.40 +#include "pdf_util.h" 2.41 +#include "pdf_prim.h" 2.42 +#include "pdf_private.h" 2.43 + 2.44 + 2.45 +struct pdf_jpeg_image 2.46 +{ 2.47 + double width, height; 2.48 + double x, y; 2.49 + FILE *f; 2.50 + unsigned long Columns; 2.51 + unsigned long Rows; 2.52 + char XObject_name [4]; 2.53 +}; 2.54 + 2.55 + 2.56 +static void pdf_write_jpeg_content_callback (pdf_file_handle pdf_file, 2.57 + struct pdf_obj *stream, 2.58 + void *app_data) 2.59 +{ 2.60 + struct pdf_jpeg_image *image = app_data; 2.61 + 2.62 + /* transformation matrix is: width 0 0 height x y cm */ 2.63 + pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", 2.64 + image->width, image->height, 2.65 + image->x, image->y); 2.66 + pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", 2.67 + image->XObject_name); 2.68 +} 2.69 + 2.70 + 2.71 +#define JPEG_BUFFER_SIZE 8192 2.72 + 2.73 +static void pdf_write_jpeg_image_callback (pdf_file_handle pdf_file, 2.74 + struct pdf_obj *stream, 2.75 + void *app_data) 2.76 +{ 2.77 + struct pdf_jpeg_image *image = app_data; 2.78 + FILE *f; 2.79 + int rlen, wlen; 2.80 + uint8_t *wp; 2.81 + uint8_t buffer [8192]; 2.82 + 2.83 + while (! feof (image->f)) 2.84 + { 2.85 + rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, f); 2.86 + wp = & buffer [0]; 2.87 + while (rlen) 2.88 + { 2.89 + wlen = fwrite (wp, 1, rlen, pdf_file->f); 2.90 + if (feof (pdf_file->f)) 2.91 + pdf_fatal ("unexpected EOF on output file\n"); 2.92 + if (ferror (pdf_file->f)) 2.93 + pdf_fatal ("error on output file\n"); 2.94 + rlen -= wlen; 2.95 + wp += wlen; 2.96 + } 2.97 + if (ferror (f)) 2.98 + pdf_fatal ("error on input file\n"); 2.99 + } 2.100 +} 2.101 + 2.102 + 2.103 +void pdf_write_jpeg_image (pdf_page_handle pdf_page, 2.104 + double x, 2.105 + double y, 2.106 + double width, 2.107 + double height, 2.108 + FILE *f) 2.109 +{ 2.110 + struct pdf_jpeg_image *image; 2.111 + 2.112 + struct pdf_obj *stream; 2.113 + struct pdf_obj *stream_dict; 2.114 + struct pdf_obj *decode_parms; 2.115 + 2.116 + struct pdf_obj *content_stream; 2.117 + 2.118 + image = pdf_calloc (1, sizeof (struct pdf_jpeg_image)); 2.119 + 2.120 + image->width = width; 2.121 + image->height = height; 2.122 + image->x = x; 2.123 + image->y = y; 2.124 + 2.125 + image->f = f; 2.126 +#if 0 2.127 + image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; 2.128 + image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; 2.129 +#endif 2.130 + 2.131 + stream_dict = pdf_new_obj (PT_DICTIONARY); 2.132 + 2.133 + stream = pdf_new_ind_ref (pdf_page->pdf_file, 2.134 + pdf_new_stream (pdf_page->pdf_file, 2.135 + stream_dict, 2.136 + & pdf_write_jpeg_image_callback, 2.137 + image)); 2.138 + 2.139 + strcpy (& image->XObject_name [0], "Im "); 2.140 + image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); 2.141 + 2.142 + pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); 2.143 + pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); 2.144 + pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); 2.145 + pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); 2.146 + pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); 2.147 + pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8)); 2.148 + 2.149 + decode_parms = pdf_new_obj (PT_DICTIONARY); 2.150 + 2.151 + pdf_set_dict_entry (decode_parms, 2.152 + "K", 2.153 + pdf_new_integer (-1)); 2.154 + 2.155 + pdf_set_dict_entry (decode_parms, 2.156 + "Columns", 2.157 + pdf_new_integer (image->Columns)); 2.158 + 2.159 + pdf_set_dict_entry (decode_parms, 2.160 + "Rows", 2.161 + pdf_new_integer (image->Rows)); 2.162 + 2.163 + pdf_stream_add_filter (stream, "DCTDecode", decode_parms); 2.164 + 2.165 + /* the following will write the stream, using our callback function to 2.166 + get the actual data */ 2.167 + pdf_write_ind_obj (pdf_page->pdf_file, stream); 2.168 + 2.169 + content_stream = pdf_new_ind_ref (pdf_page->pdf_file, 2.170 + pdf_new_stream (pdf_page->pdf_file, 2.171 + pdf_new_obj (PT_DICTIONARY), 2.172 + & pdf_write_jpeg_content_callback, 2.173 + image)); 2.174 + 2.175 + pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); 2.176 + 2.177 + pdf_write_ind_obj (pdf_page->pdf_file, content_stream); 2.178 +} 2.179 +