Wed, 12 Mar 2003 07:38:57 +0000
remove unused SWAP macro.
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.10 2003/03/11 23:38: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 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
56 {
57 char XObject_name [4] = "Im ";
59 XObject_name [2] = ++pdf_page->last_XObject_name;
61 if (! pdf_page->XObject_dict)
62 {
63 pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
64 pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
65 }
67 pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
69 return (pdf_page->last_XObject_name);
70 }
73 void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
74 struct pdf_obj *stream,
75 void *app_data)
76 {
77 struct pdf_g4_image *image = app_data;
79 /* transformation matrix is: width 0 0 height x y cm */
80 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
81 image->width, image->height,
82 image->x, image->y);
83 if (image->ImageMask)
84 pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
85 image->r, image->g, image->b);
87 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
88 image->XObject_name);
89 }
92 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
93 struct pdf_obj *stream,
94 void *app_data)
95 {
96 struct pdf_g4_image *image = app_data;
98 bitblt_write_g4 (image->bitmap, pdf_file->f);
99 }
102 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
103 double x,
104 double y,
105 double width,
106 double height,
107 Bitmap *bitmap,
108 bool ImageMask,
109 double r, /* RGB fill color, only for ImageMask */
110 double g,
111 double b,
112 bool BlackIs1) /* boolean, typ. false */
113 {
114 struct pdf_g4_image *image;
116 struct pdf_obj *stream;
117 struct pdf_obj *stream_dict;
118 struct pdf_obj *decode_parms;
120 struct pdf_obj *content_stream;
122 image = pdf_calloc (1, sizeof (struct pdf_g4_image));
124 image->width = width;
125 image->height = height;
126 image->x = x;
127 image->y = y;
128 image->r = r;
129 image->g = g;
130 image->b = b;
132 image->bitmap = bitmap;
133 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
134 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
135 image->ImageMask = ImageMask;
136 image->BlackIs1 = BlackIs1;
138 stream_dict = pdf_new_obj (PT_DICTIONARY);
140 stream = pdf_new_ind_ref (pdf_page->pdf_file,
141 pdf_new_stream (pdf_page->pdf_file,
142 stream_dict,
143 & pdf_write_g4_fax_image_callback,
144 image));
146 strcpy (& image->XObject_name [0], "Im ");
147 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
149 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
150 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
151 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
152 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
153 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
154 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
155 if (ImageMask)
156 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
157 else
158 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
160 decode_parms = pdf_new_obj (PT_DICTIONARY);
162 pdf_set_dict_entry (decode_parms,
163 "K",
164 pdf_new_integer (-1));
166 pdf_set_dict_entry (decode_parms,
167 "Columns",
168 pdf_new_integer (image->Columns));
170 pdf_set_dict_entry (decode_parms,
171 "Rows",
172 pdf_new_integer (image->Rows));
174 if (BlackIs1)
175 pdf_set_dict_entry (decode_parms,
176 "BlackIs1",
177 pdf_new_bool (BlackIs1));
179 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
181 /* the following will write the stream, using our callback function to
182 get the actual data */
183 pdf_write_ind_obj (pdf_page->pdf_file, stream);
185 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
186 pdf_new_stream (pdf_page->pdf_file,
187 pdf_new_obj (PT_DICTIONARY),
188 & pdf_write_g4_content_callback,
189 image));
191 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
193 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
194 }