Debian Codegraph @

texk/web2c/luatexdir/image/writejp2.c

raw
1 /*
2
3 writejp2.c
4
5 Copyright 2011-2013 Taco Hoekwater <[email protected]>
6 Copyright 2011-2013 Hartmut Henkel <[email protected]>
7
8 This file is part of LuaTeX.
9
10 LuaTeX is free software; you can redistribute it and/or modify it under the terms
11 of the GNU General Public License as published by the Free Software Foundation;
12 either version 2 of the License, or (at your option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 /*tex
24
25 Basic JPEG~2000 image support. Section and Table references below:
26 Information technology --- JPEG~2000 image coding system: Core coding system.
27 ISO/IEC 15444-1, Second edition, 2004-09-15, file |15444-1annexi.pdf|.
28
29 */
30
31 #include "ptexlib.h"
32 #include <math.h>
33 #include <assert.h>
34 #include "image/image.h"
35 #include "image/writejp2.h"
36 #include "image/writejbig2.h"
37
38 /*tex Table 1.2: Defined boxes */
39
40 #define BOX_JP 0x6A502020
41 #define BOX_FTYP 0x66747970
42 #define BOX_JP2H 0x6a703268
43 #define BOX_IHDR 0x69686472
44 #define BOX_BPCC 0x62706363
45 #define BOX_COLR 0x636D6170
46 #define BOX_CDEF 0x63646566
47 #define BOX_RES 0x72657320
48 #define BOX_RESC 0x72657363
49 #define BOX_RESD 0x72657364
50 #define BOX_JP2C 0x6A703263
51
52 /*tex Table 1.4: Box definition */
53
54 typedef struct {
55 uint64_t lbox;
56 unsigned int tbox;
57 } hdr_struct;
58
59 static uint64_t read8bytes(FILE * f)
60 {
61 uint64_t l = read4bytes(f);
62 l = (l << 32) + read4bytes(f);
63 return l;
64 }
65
66 static hdr_struct read_boxhdr(image_dict * idict)
67 {
68 hdr_struct hdr;
69 hdr.lbox = read4bytes(img_file(idict));
70 hdr.tbox = read4bytes(img_file(idict));
71 if (hdr.lbox == 1)
72 hdr.lbox = read8bytes(img_file(idict));
73 if (hdr.lbox == 0 && hdr.tbox != BOX_JP2C) {
74 normal_error("readjp2","LBox == 0");
75 }
76 return hdr;
77 }
78
79 /*tex Table 1.5.3.1: Image Header box */
80
81 static void scan_ihdr(image_dict * idict)
82 {
83 unsigned int height, width;
84 unsigned char bpc;
85 height = read4bytes(img_file(idict));
86 width = read4bytes(img_file(idict));
87 img_ysize(idict) = (int) height;
88 img_xsize(idict) = (int) width;
89 (void) read2bytes(img_file(idict)); /* nc */
90 bpc = (unsigned char) xgetc(img_file(idict));
91 img_colordepth(idict) = bpc + 1;
92 (void) xgetc(img_file(idict)); /* c */
93 (void) xgetc(img_file(idict)); /* unkc */
94 (void) xgetc(img_file(idict)); /* ipr */
95 }
96
97 /*tex Table 1.5.3.7.1: Capture Resolution box */
98
99 /*tex Table 1.5.3.7.2: Default Display Resolution box */
100
101 static void scan_resc_resd(image_dict * idict)
102 {
103 unsigned int vr_n, vr_d, hr_n, hr_d;
104 unsigned char vr_e, hr_e;
105 double hr_, vr_;
106 vr_n = read2bytes(img_file(idict));
107 vr_d = read2bytes(img_file(idict));
108 hr_n = read2bytes(img_file(idict));
109 hr_d = read2bytes(img_file(idict));
110 vr_e = (unsigned char) xgetc(img_file(idict));
111 hr_e = (unsigned char) xgetc(img_file(idict));
112 hr_ = ((double) hr_n / hr_d) * exp(hr_e * log(10.0)) * 0.0254;
113 vr_ = ((double) vr_n / vr_d) * exp(vr_e * log(10.0)) * 0.0254;
114 img_xres(idict) = (int) (hr_ + 0.5);
115 img_yres(idict) = (int) (vr_ + 0.5);
116 }
117
118 /*tex Table 1.5.3.7: Resolution box (superbox) */
119
120 static void scan_res(image_dict * idict, uint64_t epos_s)
121 {
122 hdr_struct hdr;
123 uint64_t spos, epos;
124 epos = xftell(img_file(idict), img_filepath(idict));
125 while (1) {
126 spos = epos;
127 hdr = read_boxhdr(idict);
128 epos = spos + hdr.lbox;
129 switch (hdr.tbox) {
130 case (BOX_RESC):
131 /*tex arbitrary: let BOX_RESD have precedence */
132 if (img_xres(idict) == 0 && img_yres(idict) == 0) {
133 scan_resc_resd(idict);
134 if (xftell(img_file(idict), img_filepath(idict)) != (long)epos)
135 normal_error("readjp2","resc box size inconsistent");
136 }
137 break;
138 case (BOX_RESD):
139 scan_resc_resd(idict);
140 if (xftell(img_file(idict), img_filepath(idict)) != (long)epos)
141 normal_error("readjp2","resd box size inconsistent");
142 break;
143 default:;
144 }
145 if (epos > epos_s)
146 normal_error("readjp2","res box size inconsistent");
147 if (epos == epos_s)
148 break;
149 xfseek(img_file(idict), (long) epos, SEEK_SET, img_filepath(idict));
150 }
151 }
152
153 /*tex Table 1.5.3: JP2 Header box (superbox) */
154
155 static boolean scan_jp2h(image_dict * idict, uint64_t epos_s)
156 {
157 boolean ihdr_found = false;
158 hdr_struct hdr;
159 uint64_t spos, epos;
160 epos = xftell(img_file(idict), img_filepath(idict));
161 while (1) {
162 spos = epos;
163 hdr = read_boxhdr(idict);
164 epos = spos + hdr.lbox;
165 switch (hdr.tbox) {
166 case (BOX_IHDR):
167 scan_ihdr(idict);
168 if (xftell(img_file(idict), img_filepath(idict)) != (long)epos)
169 normal_error("readjp2","ihdr box size inconsistent");
170 ihdr_found = true;
171 break;
172 case (BOX_RES):
173 scan_res(idict, epos);
174 break;
175 default:;
176 }
177 if (epos > epos_s)
178 normal_error("readjp2","jp2h box size inconsistent");
179 if (epos == epos_s)
180 break;
181 xfseek(img_file(idict), (long) epos, SEEK_SET, img_filepath(idict));
182 }
183 return ihdr_found;
184 }
185
186 static void close_and_cleanup_jp2(image_dict * idict)
187 {
188 /*tex If one of then is not NULL we already cleaned up. */
189 if (img_file(idict) != NULL) {
190 xfclose(img_file(idict), img_filepath(idict));
191 img_file(idict) = NULL;
192 }
193 if (img_jp2_ptr(idict) != NULL) {
194 xfree(img_jp2_ptr(idict));
195 }
196 }
197
198 void flush_jp2_info(image_dict * idict)
199 {
200 close_and_cleanup_jp2(idict);
201 }
202
203 void read_jp2_info(image_dict * idict)
204 {
205 boolean ihdr_found = false;
206 hdr_struct hdr;
207 uint64_t spos, epos;
208 if (img_type(idict) != IMG_TYPE_JP2) {
209 normal_error("readjp2","conflicting image dictionary");
210 }
211 if (img_file(idict) != NULL) {
212 normal_error("readjp2","image data already read");
213 }
214 img_totalpages(idict) = 1;
215 img_pagenum(idict) = 1;
216 img_xres(idict) = img_yres(idict) = 0;
217 img_file(idict) = xfopen(img_filepath(idict), FOPEN_RBIN_MODE);
218 img_jp2_ptr(idict) = xtalloc(1, jp2_img_struct);
219 xfseek(img_file(idict), 0, SEEK_END, img_filepath(idict));
220 img_jp2_ptr(idict)->length = (int) xftell(img_file(idict), img_filepath(idict));
221 xfseek(img_file(idict), 0, SEEK_SET, img_filepath(idict));
222 if (sizeof(uint64_t) < 8) {
223 normal_error("readjp2","size problem");
224 }
225 spos = epos = 0;
226 /*tex Table 1.5.1: JPEG 2000 Signature box */
227 hdr = read_boxhdr(idict);
228 epos = spos + hdr.lbox;
229 xfseek(img_file(idict), (long) epos, SEEK_SET, img_filepath(idict));
230 /*tex Table 1.5.2: File Type box */
231 spos = epos;
232 hdr = read_boxhdr(idict);
233 if (hdr.tbox != BOX_FTYP) {
234 normal_error("readjp2","missing ftyp box");
235 }
236 epos = spos + hdr.lbox;
237 xfseek(img_file(idict), (long) epos, SEEK_SET, img_filepath(idict));
238 while (!ihdr_found) {
239 spos = epos;
240 hdr = read_boxhdr(idict);
241 epos = spos + hdr.lbox;
242 switch (hdr.tbox) {
243 case BOX_JP2H:
244 ihdr_found = scan_jp2h(idict, epos);
245 break;
246 case BOX_JP2C:
247 if (!ihdr_found)
248 normal_error("readjp2","no ihdr box found");
249 break;
250 default:;
251 }
252 xfseek(img_file(idict), (long) epos, SEEK_SET, img_filepath(idict));
253 }
254 if (! img_keepopen(idict)) {
255 close_and_cleanup_jp2(idict);
256 }
257 }
258
259 static void reopen_jp2(image_dict * idict)
260 {
261 int width, height, xres, yres;
262 width = img_xsize(idict);
263 height = img_ysize(idict);
264 xres = img_xres(idict);
265 yres = img_yres(idict);
266 /*tex We need to make sure that the file kept open. */
267 img_keepopen(idict) = 1;
268 read_jp2_info(idict);
269 if (width != img_xsize(idict) || height != img_ysize(idict)
270 || xres != img_xres(idict) || yres != img_yres(idict)) {
271 normal_error("writejp2","image dimensions have changed");
272 }
273 }
274
275 void write_jp2(PDF pdf, image_dict * idict)
276 {
277 long unsigned l;
278 if (img_file(idict) == NULL) {
279 reopen_jp2(idict);
280 }
281 pdf_begin_obj(pdf, img_objnum(idict), OBJSTM_NEVER);
282 pdf_begin_dict(pdf);
283 pdf_dict_add_name(pdf, "Type", "XObject");
284 pdf_dict_add_name(pdf, "Subtype", "Image");
285 pdf_dict_add_img_filename(pdf, idict);
286 if (img_attr(idict) != NULL && strlen(img_attr(idict)) > 0)
287 pdf_printf(pdf, "\n%s\n", img_attr(idict));
288 pdf_dict_add_int(pdf, "Width", (int) img_xsize(idict));
289 pdf_dict_add_int(pdf, "Height", (int) img_ysize(idict));
290 pdf_dict_add_int(pdf, "Length", (int) img_jp2_ptr(idict)->length);
291 pdf_dict_add_name(pdf, "Filter", "JPXDecode");
292 pdf_end_dict(pdf);
293 pdf_begin_stream(pdf);
294 l = (long unsigned int) img_jp2_ptr(idict)->length;
295 xfseek(img_file(idict), 0, SEEK_SET, img_filepath(idict));
296 if (read_file_to_buf(pdf, img_file(idict), l) != l)
297 normal_error("writejp2","fread failed");
298 pdf_end_stream(pdf);
299 pdf_end_obj(pdf);
300 /*tex We always:*/
301 close_and_cleanup_jp2(idict);
302 }