Thu, 13 Mar 2003 08:03:11 +0000
Acrobat 4.0 fails to optimize PDF files in which the Kids array in the Pages object is an indirect reference.
1 /*
2 * t2p: Create a PDF file from the contents of one or more TIFF
3 * bilevel image files. The images in the resulting PDF file
4 * will be compressed using ITU-T T.6 (G4) fax encoding.
5 *
6 * PDF routines
7 * $Id: pdf_jpeg.c,v 1.1 2003/03/12 02:57:06 eric Exp $
8 * Copyright 2003 Eric Smith <eric@brouhaha.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation. Note that permission is
13 * not granted to redistribute this program under the terms of any
14 * other version of the General Public License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
24 */
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
34 #include "bitblt.h"
35 #include "pdf.h"
36 #include "pdf_util.h"
37 #include "pdf_prim.h"
38 #include "pdf_private.h"
41 struct pdf_jpeg_image
42 {
43 double width, height;
44 double x, y;
45 FILE *f;
46 unsigned long Columns;
47 unsigned long Rows;
48 char XObject_name [4];
49 };
52 static void pdf_write_jpeg_content_callback (pdf_file_handle pdf_file,
53 struct pdf_obj *stream,
54 void *app_data)
55 {
56 struct pdf_jpeg_image *image = app_data;
58 /* transformation matrix is: width 0 0 height x y cm */
59 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
60 image->width, image->height,
61 image->x, image->y);
62 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
63 image->XObject_name);
64 }
67 #define JPEG_BUFFER_SIZE 8192
69 static void pdf_write_jpeg_image_callback (pdf_file_handle pdf_file,
70 struct pdf_obj *stream,
71 void *app_data)
72 {
73 struct pdf_jpeg_image *image = app_data;
74 FILE *f;
75 int rlen, wlen;
76 uint8_t *wp;
77 uint8_t buffer [8192];
79 while (! feof (image->f))
80 {
81 rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, f);
82 wp = & buffer [0];
83 while (rlen)
84 {
85 wlen = fwrite (wp, 1, rlen, pdf_file->f);
86 if (feof (pdf_file->f))
87 pdf_fatal ("unexpected EOF on output file\n");
88 if (ferror (pdf_file->f))
89 pdf_fatal ("error on output file\n");
90 rlen -= wlen;
91 wp += wlen;
92 }
93 if (ferror (f))
94 pdf_fatal ("error on input file\n");
95 }
96 }
99 void pdf_write_jpeg_image (pdf_page_handle pdf_page,
100 double x,
101 double y,
102 double width,
103 double height,
104 FILE *f)
105 {
106 struct pdf_jpeg_image *image;
108 struct pdf_obj *stream;
109 struct pdf_obj *stream_dict;
110 struct pdf_obj *decode_parms;
112 struct pdf_obj *content_stream;
114 image = pdf_calloc (1, sizeof (struct pdf_jpeg_image));
116 image->width = width;
117 image->height = height;
118 image->x = x;
119 image->y = y;
121 image->f = f;
122 #if 0
123 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
124 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
125 #endif
127 stream_dict = pdf_new_obj (PT_DICTIONARY);
129 stream = pdf_new_ind_ref (pdf_page->pdf_file,
130 pdf_new_stream (pdf_page->pdf_file,
131 stream_dict,
132 & pdf_write_jpeg_image_callback,
133 image));
135 strcpy (& image->XObject_name [0], "Im ");
136 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
138 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
139 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
140 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
141 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
142 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
143 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8));
145 decode_parms = pdf_new_obj (PT_DICTIONARY);
147 pdf_set_dict_entry (decode_parms,
148 "K",
149 pdf_new_integer (-1));
151 pdf_set_dict_entry (decode_parms,
152 "Columns",
153 pdf_new_integer (image->Columns));
155 pdf_set_dict_entry (decode_parms,
156 "Rows",
157 pdf_new_integer (image->Rows));
159 pdf_stream_add_filter (stream, "DCTDecode", decode_parms);
161 /* the following will write the stream, using our callback function to
162 get the actual data */
163 pdf_write_ind_obj (pdf_page->pdf_file, stream);
165 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
166 pdf_new_stream (pdf_page->pdf_file,
167 pdf_new_obj (PT_DICTIONARY),
168 & pdf_write_jpeg_content_callback,
169 image));
171 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
173 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
174 }