Thu, 13 Mar 2003 07:57:21 +0000
added pdf_write_text(), just a placeholder/debugging tool at the moment.
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.13 2003/03/12 22:56:57 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 }
84 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
85 double x,
86 double y,
87 double width,
88 double height,
89 Bitmap *bitmap,
90 bool ImageMask,
91 double r, /* RGB fill color, only for ImageMask */
92 double g,
93 double b,
94 bool BlackIs1) /* boolean, typ. false */
95 {
96 struct pdf_g4_image *image;
98 struct pdf_obj *stream;
99 struct pdf_obj *stream_dict;
100 struct pdf_obj *decode_parms;
102 struct pdf_obj *content_stream;
104 pdf_add_array_elem_unique (pdf_page->procset, pdf_new_name ("ImageB"));
106 image = pdf_calloc (1, sizeof (struct pdf_g4_image));
108 image->width = width;
109 image->height = height;
110 image->x = x;
111 image->y = y;
112 image->r = r;
113 image->g = g;
114 image->b = b;
116 image->bitmap = bitmap;
117 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
118 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
119 image->ImageMask = ImageMask;
120 image->BlackIs1 = BlackIs1;
122 stream_dict = pdf_new_obj (PT_DICTIONARY);
124 stream = pdf_new_ind_ref (pdf_page->pdf_file,
125 pdf_new_stream (pdf_page->pdf_file,
126 stream_dict,
127 & pdf_write_g4_fax_image_callback,
128 image));
130 strcpy (& image->XObject_name [0], "Im ");
131 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
133 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
134 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
135 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
136 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
137 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
138 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
139 if (ImageMask)
140 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
141 else
142 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
144 decode_parms = pdf_new_obj (PT_DICTIONARY);
146 pdf_set_dict_entry (decode_parms,
147 "K",
148 pdf_new_integer (-1));
150 pdf_set_dict_entry (decode_parms,
151 "Columns",
152 pdf_new_integer (image->Columns));
154 pdf_set_dict_entry (decode_parms,
155 "Rows",
156 pdf_new_integer (image->Rows));
158 if (BlackIs1)
159 pdf_set_dict_entry (decode_parms,
160 "BlackIs1",
161 pdf_new_bool (BlackIs1));
163 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
165 /* the following will write the stream, using our callback function to
166 get the actual data */
167 pdf_write_ind_obj (pdf_page->pdf_file, stream);
169 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
170 pdf_new_stream (pdf_page->pdf_file,
171 pdf_new_obj (PT_DICTIONARY),
172 & pdf_write_g4_content_callback,
173 image));
175 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
177 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
178 }