Fri, 21 Feb 2003 12:28:37 +0000
GPL
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: bitblt_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $
8 * Copyright 2001, 2002, 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 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
44 struct pdf_g4_image
45 {
46 double width, height;
47 double x, y;
48 double r, g, b; /* fill color, only for ImageMask */
49 unsigned long Columns;
50 unsigned long Rows;
51 bool ImageMask;
52 bool BlackIs1;
53 Bitmap *bitmap;
54 char XObject_name [4];
55 };
58 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
59 {
60 char XObject_name [4] = "Im ";
62 XObject_name [2] = ++pdf_page->last_XObject_name;
64 if (! pdf_page->XObject_dict)
65 {
66 pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
67 pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
68 }
70 pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
72 return (pdf_page->last_XObject_name);
73 }
76 void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
77 struct pdf_obj *stream,
78 void *app_data)
79 {
80 struct pdf_g4_image *image = app_data;
82 /* transformation matrix is: width 0 0 height x y cm */
83 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
84 image->width, image->height,
85 image->x, image->y);
86 if (image->ImageMask)
87 pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
88 image->r, image->g, image->b);
90 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
91 image->XObject_name);
92 }
95 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
96 struct pdf_obj *stream,
97 void *app_data)
98 {
99 struct pdf_g4_image *image = app_data;
101 uint32_t row;
103 /* reference (previous) row */
104 uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t));
105 uint32_t ref_run_count;
107 /* row being converted */
108 uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t));
109 uint32_t row_run_count;
111 /* initialize reference row - all white */
112 ref_runs [0] = image->Columns;
113 ref_run_count = 0;
115 for (row = image->bitmap->rect.min.y;
116 row < image->bitmap->rect.max.y;
117 row++)
118 {
119 row_run_count = get_row_run_lengths (image->bitmap,
120 row,
121 image->bitmap->rect.min.x,
122 image->bitmap->rect.max.x - 1,
123 image->Columns, /* max_runs */
124 row_runs);
125 pdf_assert (row_run_count > 0);
127 /* $$$ G4 encode the runs here */
129 /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */
131 SWAP (uint32_t *, row_runs, ref_runs);
132 ref_run_count = row_run_count;
133 }
136 /* $$$ generate and write EOFB code */
137 pdf_stream_write_bits (pdf_file, stream, 24, 0x001001);
139 pdf_stream_flush_bits (pdf_file, stream);
141 free (ref_runs);
142 free (row_runs);
143 }
146 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
147 double x,
148 double y,
149 double width,
150 double height,
151 Bitmap *bitmap,
152 bool ImageMask,
153 double r, /* RGB fill color, only for ImageMask */
154 double g,
155 double b,
156 bool BlackIs1) /* boolean, typ. false */
157 {
158 struct pdf_g4_image *image;
160 struct pdf_obj *stream;
161 struct pdf_obj *stream_dict;
162 struct pdf_obj *decode_parms;
164 struct pdf_obj *content_stream;
166 image = pdf_calloc (1, sizeof (struct pdf_g4_image));
168 image->width = width;
169 image->height = height;
170 image->x = x;
171 image->y = y;
172 image->r = r;
173 image->g = g;
174 image->b = b;
176 image->bitmap = bitmap;
177 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
178 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
179 image->ImageMask = ImageMask;
180 image->BlackIs1 = BlackIs1;
182 stream_dict = pdf_new_obj (PT_DICTIONARY);
184 stream = pdf_new_ind_ref (pdf_page->pdf_file,
185 pdf_new_stream (pdf_page->pdf_file,
186 stream_dict,
187 & pdf_write_g4_fax_image_callback,
188 image));
190 strcpy (& image->XObject_name [0], "Im ");
191 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
193 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
194 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
195 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
196 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
197 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
198 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
199 if (ImageMask)
200 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
201 else
202 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
204 decode_parms = pdf_new_obj (PT_DICTIONARY);
206 pdf_set_dict_entry (decode_parms,
207 "K",
208 pdf_new_integer (-1));
210 pdf_set_dict_entry (decode_parms,
211 "Columns",
212 pdf_new_integer (image->Columns));
214 pdf_set_dict_entry (decode_parms,
215 "Rows",
216 pdf_new_integer (image->Rows));
218 if (BlackIs1)
219 pdf_set_dict_entry (decode_parms,
220 "BlackIs1",
221 pdf_new_bool (BlackIs1));
223 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
225 /* the following will write the stream, using our callback function to
226 get the actual data */
227 pdf_write_ind_obj (pdf_page->pdf_file, stream);
229 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
230 pdf_new_stream (pdf_page->pdf_file,
231 pdf_new_obj (PT_DICTIONARY),
232 & pdf_write_g4_content_callback,
233 image));
235 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
237 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
238 }