1 /*
2 * tumble: build a PDF file from image files
3 *
4 * PDF routines
5 * Copyright 2003, 2017 Eric Smith <spacewar@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. Note that permission is
10 * not granted to redistribute this program under the terms of any
11 * other version of the General Public License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
21 *
22 * 2008-12-30 [JDB] Fixed bug wherein "pdf_write_jpeg_content_callback" called
23 * "pdf_stream_printf" to write XObject name without escaping
24 * restricted characters. Now calls "pdf_write_name".
25 */
26
27
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34
35 #include "bitblt.h"
36 #include "pdf.h"
37 #include "pdf_util.h"
38 #include "pdf_prim.h"
39 #include "pdf_private.h"
40
41
42 struct pdf_jpeg_image
43 {
44 double width, height;
45 double x, y;
46 bool color; /* false for grayscale */
47 uint32_t width_samples, height_samples;
48 FILE *f;
49 char XObject_name [4];
50 };
51
52
pdf_write_jpeg_content_callback(pdf_file_handle pdf_file,struct pdf_obj * stream,void * app_data)53 static void pdf_write_jpeg_content_callback (pdf_file_handle pdf_file,
54 struct pdf_obj *stream,
55 void *app_data)
56 {
57 struct pdf_jpeg_image *image = app_data;
58
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 pdf_write_name (pdf_file, image->XObject_name);
64 pdf_stream_printf (pdf_file, stream, "Do Q\r\n");
65 }
66
67
68 #define JPEG_BUFFER_SIZE 8192
69
pdf_write_jpeg_image_callback(pdf_file_handle pdf_file,struct pdf_obj * stream,void * app_data)70 static void pdf_write_jpeg_image_callback (pdf_file_handle pdf_file,
71 struct pdf_obj *stream,
72 void *app_data)
73 {
74 struct pdf_jpeg_image *image = app_data;
75 int rlen, wlen;
76 uint8_t *wp;
77 uint8_t buffer [8192];
78
79 while (! feof (image->f))
80 {
81 rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, image->f);
82 wp = & buffer [0];
83 while (rlen)
84 {
85 wlen = fwrite (wp, 1, rlen, pdf_file->f);
86 if (feof (pdf_file->f))
87 pdf_fatal ("unexpected EOF on output file\n");
88 if (ferror (pdf_file->f))
89 pdf_fatal ("error on output file\n");
90 rlen -= wlen;
91 wp += wlen;
92 }
93 if (ferror (image->f))
94 pdf_fatal ("error on input file\n");
95 }
96 }
97
98
pdf_write_jpeg_image(pdf_page_handle pdf_page,double x,double y,double width,double height,bool color,uint32_t width_samples,uint32_t height_samples,rgb_range_t * transparency,FILE * f)99 void pdf_write_jpeg_image (pdf_page_handle pdf_page,
100 double x,
101 double y,
102 double width,
103 double height,
104 bool color,
105 uint32_t width_samples,
106 uint32_t height_samples,
107 rgb_range_t *transparency,
108 FILE *f)
109 {
110 struct pdf_jpeg_image *image;
111
112 struct pdf_obj *stream;
113 struct pdf_obj *stream_dict;
114
115 struct pdf_obj *content_stream;
116
117 struct pdf_obj *contents;
118 struct pdf_obj *mask;
119
120 image = pdf_calloc (1, sizeof (struct pdf_jpeg_image));
121
122 image->width = width;
123 image->height = height;
124 image->x = x;
125 image->y = y;
126
127 image->f = f;
128
129 image->color = color;
130 image->width_samples = width_samples;
131 image->height_samples = height_samples;
132
133 pdf_add_array_elem_unique (pdf_page->procset,
134 pdf_new_name (image->color ? "ImageC" : "ImageB"));
135
136 stream_dict = pdf_new_obj (PT_DICTIONARY);
137
138 stream = pdf_new_ind_ref (pdf_page->pdf_file,
139 pdf_new_stream (pdf_page->pdf_file,
140 stream_dict,
141 & pdf_write_jpeg_image_callback,
142 image));
143
144 strcpy (& image->XObject_name [0], "Im ");
145 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
146
147 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
148 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
149 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples));
150 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples));
151 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name (image->color ? "DeviceRGB" : "DeviceGray"));
152 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8));
153
154 if (transparency)
155 {
156 mask = pdf_new_obj (PT_ARRAY);
157
158 pdf_add_array_elem (mask, pdf_new_integer (transparency->red.first));
159 pdf_add_array_elem (mask, pdf_new_integer (transparency->red.last));
160
161 if (image->color) {
162 pdf_add_array_elem (mask, pdf_new_integer (transparency->green.first));
163 pdf_add_array_elem (mask, pdf_new_integer (transparency->green.last));
164 pdf_add_array_elem (mask, pdf_new_integer (transparency->blue.first));
165 pdf_add_array_elem (mask, pdf_new_integer (transparency->blue.last));
166 }
167
168 pdf_set_dict_entry (stream_dict, "Mask", mask);
169 }
170
171 pdf_stream_add_filter (stream, "DCTDecode", NULL);
172
173 /* the following will write the stream, using our callback function to
174 get the actual data */
175 pdf_write_ind_obj (pdf_page->pdf_file, stream);
176
177 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
178 pdf_new_stream (pdf_page->pdf_file,
179 pdf_new_obj (PT_DICTIONARY),
180 & pdf_write_jpeg_content_callback,
181 image));
182
183 contents = pdf_get_dict_entry (pdf_page->page_dict, "Contents");
184
185 if (! contents)
186 contents = pdf_new_obj (PT_ARRAY);
187
188 pdf_add_array_elem (contents, content_stream);
189 pdf_set_dict_entry (pdf_page->page_dict, "Contents", contents);
190
191 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
192 }
193