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