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_g4.c,v 1.14 2003/03/12 23:59:35 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_g4_image
42 {
43 double width, height;
44 double x, y;
45 double r, g, b; /* fill color, only for ImageMask */
46 unsigned long Columns;
47 unsigned long Rows;
48 bool ImageMask;
49 bool BlackIs1;
50 Bitmap *bitmap;
51 char XObject_name [4];
52 };
55 static void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
56 struct pdf_obj *stream,
57 void *app_data)
58 {
59 struct pdf_g4_image *image = app_data;
61 /* transformation matrix is: width 0 0 height x y cm */
62 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
63 image->width, image->height,
64 image->x, image->y);
65 if (image->ImageMask)
66 pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
67 image->r, image->g, image->b);
69 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
70 image->XObject_name);
71 }
74 static void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
75 struct pdf_obj *stream,
76 void *app_data)
77 {
78 struct pdf_g4_image *image = app_data;
80 bitblt_write_g4 (image->bitmap, pdf_file->f);
81 pdf_stream_printf (pdf_file, stream, "\r\n");
82 }
85 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
86 double x,
87 double y,
88 double width,
89 double height,
90 Bitmap *bitmap,
91 bool ImageMask,
92 double r, /* RGB fill color, only for ImageMask */
93 double g,
94 double b,
95 bool BlackIs1) /* boolean, typ. false */
96 {
97 struct pdf_g4_image *image;
99 struct pdf_obj *stream;
100 struct pdf_obj *stream_dict;
101 struct pdf_obj *decode_parms;
103 struct pdf_obj *content_stream;
105 pdf_add_array_elem_unique (pdf_page->procset, pdf_new_name ("ImageB"));
107 image = pdf_calloc (1, sizeof (struct pdf_g4_image));
109 image->width = width;
110 image->height = height;
111 image->x = x;
112 image->y = y;
113 image->r = r;
114 image->g = g;
115 image->b = b;
117 image->bitmap = bitmap;
118 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
119 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
120 image->ImageMask = ImageMask;
121 image->BlackIs1 = BlackIs1;
123 stream_dict = pdf_new_obj (PT_DICTIONARY);
125 stream = pdf_new_ind_ref (pdf_page->pdf_file,
126 pdf_new_stream (pdf_page->pdf_file,
127 stream_dict,
128 & pdf_write_g4_fax_image_callback,
129 image));
131 strcpy (& image->XObject_name [0], "Im ");
132 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
134 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
135 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
136 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
137 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
138 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
139 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
140 if (ImageMask)
141 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
142 else
143 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
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 if (BlackIs1)
160 pdf_set_dict_entry (decode_parms,
161 "BlackIs1",
162 pdf_new_bool (BlackIs1));
164 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
166 /* the following will write the stream, using our callback function to
167 get the actual data */
168 pdf_write_ind_obj (pdf_page->pdf_file, stream);
170 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
171 pdf_new_stream (pdf_page->pdf_file,
172 pdf_new_obj (PT_DICTIONARY),
173 & pdf_write_g4_content_callback,
174 image));
176 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
178 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
179 }