Wed, 12 Mar 2003 06:02:46 +0000
removed get_row_run_lengths().
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.9 2003/03/10 01:49:50 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 #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 bitblt_write_g4 (image->bitmap, pdf_file->f);
102 }
105 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
106 double x,
107 double y,
108 double width,
109 double height,
110 Bitmap *bitmap,
111 bool ImageMask,
112 double r, /* RGB fill color, only for ImageMask */
113 double g,
114 double b,
115 bool BlackIs1) /* boolean, typ. false */
116 {
117 struct pdf_g4_image *image;
119 struct pdf_obj *stream;
120 struct pdf_obj *stream_dict;
121 struct pdf_obj *decode_parms;
123 struct pdf_obj *content_stream;
125 image = pdf_calloc (1, sizeof (struct pdf_g4_image));
127 image->width = width;
128 image->height = height;
129 image->x = x;
130 image->y = y;
131 image->r = r;
132 image->g = g;
133 image->b = b;
135 image->bitmap = bitmap;
136 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
137 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
138 image->ImageMask = ImageMask;
139 image->BlackIs1 = BlackIs1;
141 stream_dict = pdf_new_obj (PT_DICTIONARY);
143 stream = pdf_new_ind_ref (pdf_page->pdf_file,
144 pdf_new_stream (pdf_page->pdf_file,
145 stream_dict,
146 & pdf_write_g4_fax_image_callback,
147 image));
149 strcpy (& image->XObject_name [0], "Im ");
150 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
152 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
153 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
154 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
155 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
156 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
157 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
158 if (ImageMask)
159 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
160 else
161 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
163 decode_parms = pdf_new_obj (PT_DICTIONARY);
165 pdf_set_dict_entry (decode_parms,
166 "K",
167 pdf_new_integer (-1));
169 pdf_set_dict_entry (decode_parms,
170 "Columns",
171 pdf_new_integer (image->Columns));
173 pdf_set_dict_entry (decode_parms,
174 "Rows",
175 pdf_new_integer (image->Rows));
177 if (BlackIs1)
178 pdf_set_dict_entry (decode_parms,
179 "BlackIs1",
180 pdf_new_bool (BlackIs1));
182 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
184 /* the following will write the stream, using our callback function to
185 get the actual data */
186 pdf_write_ind_obj (pdf_page->pdf_file, stream);
188 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
189 pdf_new_stream (pdf_page->pdf_file,
190 pdf_new_obj (PT_DICTIONARY),
191 & pdf_write_g4_content_callback,
192 image));
194 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
196 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
197 }