Fri, 14 Mar 2003 08:56:38 +0000
remove debug output.
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * PDF routines
5 * $Id: pdf_g4.c,v 1.15 2003/03/13 00:57:05 eric Exp $
6 * Copyright 2003 Eric Smith <eric@brouhaha.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. Note that permission is
11 * not granted to redistribute this program under the terms of any
12 * other version of the General Public License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 */
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
32 #include "bitblt.h"
33 #include "pdf.h"
34 #include "pdf_util.h"
35 #include "pdf_prim.h"
36 #include "pdf_private.h"
39 struct pdf_g4_image
40 {
41 double width, height;
42 double x, y;
43 double r, g, b; /* fill color, only for ImageMask */
44 unsigned long Columns;
45 unsigned long Rows;
46 bool ImageMask;
47 bool BlackIs1;
48 Bitmap *bitmap;
49 char XObject_name [4];
50 };
53 static void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
54 struct pdf_obj *stream,
55 void *app_data)
56 {
57 struct pdf_g4_image *image = app_data;
59 /* transformation matrix is: width 0 0 height x y cm */
60 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
61 image->width, image->height,
62 image->x, image->y);
63 if (image->ImageMask)
64 pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
65 image->r, image->g, image->b);
67 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
68 image->XObject_name);
69 }
72 static void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
73 struct pdf_obj *stream,
74 void *app_data)
75 {
76 struct pdf_g4_image *image = app_data;
78 bitblt_write_g4 (image->bitmap, pdf_file->f);
79 pdf_stream_printf (pdf_file, stream, "\r\n");
80 }
83 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
84 double x,
85 double y,
86 double width,
87 double height,
88 Bitmap *bitmap,
89 bool ImageMask,
90 double r, /* RGB fill color, only for ImageMask */
91 double g,
92 double b,
93 bool BlackIs1) /* boolean, typ. false */
94 {
95 struct pdf_g4_image *image;
97 struct pdf_obj *stream;
98 struct pdf_obj *stream_dict;
99 struct pdf_obj *decode_parms;
101 struct pdf_obj *content_stream;
103 pdf_add_array_elem_unique (pdf_page->procset, pdf_new_name ("ImageB"));
105 image = pdf_calloc (1, sizeof (struct pdf_g4_image));
107 image->width = width;
108 image->height = height;
109 image->x = x;
110 image->y = y;
111 image->r = r;
112 image->g = g;
113 image->b = b;
115 image->bitmap = bitmap;
116 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
117 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
118 image->ImageMask = ImageMask;
119 image->BlackIs1 = BlackIs1;
121 stream_dict = pdf_new_obj (PT_DICTIONARY);
123 stream = pdf_new_ind_ref (pdf_page->pdf_file,
124 pdf_new_stream (pdf_page->pdf_file,
125 stream_dict,
126 & pdf_write_g4_fax_image_callback,
127 image));
129 strcpy (& image->XObject_name [0], "Im ");
130 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
132 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
133 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
134 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
135 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
136 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
137 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
138 if (ImageMask)
139 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
140 else
141 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
143 decode_parms = pdf_new_obj (PT_DICTIONARY);
145 pdf_set_dict_entry (decode_parms,
146 "K",
147 pdf_new_integer (-1));
149 pdf_set_dict_entry (decode_parms,
150 "Columns",
151 pdf_new_integer (image->Columns));
153 pdf_set_dict_entry (decode_parms,
154 "Rows",
155 pdf_new_integer (image->Rows));
157 if (BlackIs1)
158 pdf_set_dict_entry (decode_parms,
159 "BlackIs1",
160 pdf_new_bool (BlackIs1));
162 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
164 /* the following will write the stream, using our callback function to
165 get the actual data */
166 pdf_write_ind_obj (pdf_page->pdf_file, stream);
168 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
169 pdf_new_stream (pdf_page->pdf_file,
170 pdf_new_obj (PT_DICTIONARY),
171 & pdf_write_g4_content_callback,
172 image));
174 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
176 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
177 }