1 /*
2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
6 *
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2010-2011, Kaori Hagihara
15 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16 * Copyright (c) 2012, CS Systemes d'Information, France
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40 #include "opj_includes.h"
41
42 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
43 /*@{*/
44
45 #define OPJ_BOX_SIZE 1024
46
47 #define OPJ_UNUSED(x) (void)x
48
49 /** @name Local static functions */
50 /*@{*/
51
52 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
53
54 /**
55 * Reads a IHDR box - Image Header box
56 *
57 * @param p_image_header_data pointer to actual data (already read from file)
58 * @param jp2 the jpeg2000 file codec.
59 * @param p_image_header_size the size of the image header
60 * @param p_manager the user event manager.
61 *
62 * @return true if the image header is valid, false else.
63 */
64 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
65 OPJ_BYTE *p_image_header_data,
66 OPJ_UINT32 p_image_header_size,
67 opj_event_mgr_t * p_manager);
68
69 /**
70 * Writes the Image Header box - Image Header box.
71 *
72 * @param jp2 jpeg2000 file codec.
73 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
74 *
75 * @return the data being copied.
76 */
77 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
78 OPJ_UINT32 * p_nb_bytes_written);
79
80 /**
81 * Writes the Bit per Component box.
82 *
83 * @param jp2 jpeg2000 file codec.
84 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
85 *
86 * @return the data being copied.
87 */
88 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
89 OPJ_UINT32 * p_nb_bytes_written);
90
91 /**
92 * Reads a Bit per Component box.
93 *
94 * @param p_bpc_header_data pointer to actual data (already read from file)
95 * @param jp2 the jpeg2000 file codec.
96 * @param p_bpc_header_size the size of the bpc header
97 * @param p_manager the user event manager.
98 *
99 * @return true if the bpc header is valid, false else.
100 */
101 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
102 OPJ_BYTE * p_bpc_header_data,
103 OPJ_UINT32 p_bpc_header_size,
104 opj_event_mgr_t * p_manager);
105
106 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
107 OPJ_BYTE * p_cdef_header_data,
108 OPJ_UINT32 p_cdef_header_size,
109 opj_event_mgr_t * p_manager);
110
111 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
112 opj_event_mgr_t *);
113
114 /**
115 * Writes the Channel Definition box.
116 *
117 * @param jp2 jpeg2000 file codec.
118 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
119 *
120 * @return the data being copied.
121 */
122 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
123 OPJ_UINT32 * p_nb_bytes_written);
124
125 /**
126 * Writes the Colour Specification box.
127 *
128 * @param jp2 jpeg2000 file codec.
129 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
130 *
131 * @return the data being copied.
132 */
133 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
134 OPJ_UINT32 * p_nb_bytes_written);
135
136 /**
137 * Writes a FTYP box - File type box
138 *
139 * @param cio the stream to write data to.
140 * @param jp2 the jpeg2000 file codec.
141 * @param p_manager the user event manager.
142 *
143 * @return true if writing was successful.
144 */
145 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
146 opj_stream_private_t *cio,
147 opj_event_mgr_t * p_manager);
148
149 /**
150 * Reads a a FTYP box - File type box
151 *
152 * @param p_header_data the data contained in the FTYP box.
153 * @param jp2 the jpeg2000 file codec.
154 * @param p_header_size the size of the data contained in the FTYP box.
155 * @param p_manager the user event manager.
156 *
157 * @return true if the FTYP box is valid.
158 */
159 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
160 OPJ_BYTE * p_header_data,
161 OPJ_UINT32 p_header_size,
162 opj_event_mgr_t * p_manager);
163
164 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
165 opj_stream_private_t *stream,
166 opj_event_mgr_t * p_manager);
167
168 /**
169 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
170 *
171 * @param p_header_data the data contained in the file header box.
172 * @param jp2 the jpeg2000 file codec.
173 * @param p_header_size the size of the data contained in the file header box.
174 * @param p_manager the user event manager.
175 *
176 * @return true if the JP2 Header box was successfully recognized.
177 */
178 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
179 OPJ_BYTE *p_header_data,
180 OPJ_UINT32 p_header_size,
181 opj_event_mgr_t * p_manager);
182
183 /**
184 * Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
185 *
186 * @param jp2 the jpeg2000 file codec.
187 * @param stream the stream to write data to.
188 * @param p_manager user event manager.
189 *
190 * @return true if writing was successful.
191 */
192 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
193 opj_stream_private_t *stream,
194 opj_event_mgr_t * p_manager);
195
196 /**
197 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
198 *
199 * @param cio the stream to write data to.
200 * @param jp2 the jpeg2000 file codec.
201 * @param p_manager user event manager.
202 *
203 * @return true if writing was successful.
204 */
205 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
206 opj_stream_private_t *cio,
207 opj_event_mgr_t * p_manager);
208
209 #ifdef USE_JPIP
210 /**
211 * Write index Finder box
212 * @param cio the stream to write to.
213 * @param jp2 the jpeg2000 file codec.
214 * @param p_manager user event manager.
215 */
216 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
217 opj_stream_private_t *cio,
218 opj_event_mgr_t * p_manager);
219
220 /**
221 * Write index Finder box
222 * @param cio the stream to write to.
223 * @param jp2 the jpeg2000 file codec.
224 * @param p_manager user event manager.
225 */
226 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
227 opj_stream_private_t *cio,
228 opj_event_mgr_t * p_manager);
229
230 /**
231 * Write file Index (superbox)
232 * @param cio the stream to write to.
233 * @param jp2 the jpeg2000 file codec.
234 * @param p_manager user event manager.
235 */
236 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
237 opj_stream_private_t *cio,
238 opj_event_mgr_t * p_manager);
239 #endif /* USE_JPIP */
240
241 /**
242 * Reads a jpeg2000 file signature box.
243 *
244 * @param p_header_data the data contained in the signature box.
245 * @param jp2 the jpeg2000 file codec.
246 * @param p_header_size the size of the data contained in the signature box.
247 * @param p_manager the user event manager.
248 *
249 * @return true if the file signature box is valid.
250 */
251 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
252 OPJ_BYTE * p_header_data,
253 OPJ_UINT32 p_header_size,
254 opj_event_mgr_t * p_manager);
255
256 /**
257 * Writes a jpeg2000 file signature box.
258 *
259 * @param cio the stream to write data to.
260 * @param jp2 the jpeg2000 file codec.
261 * @param p_manager the user event manager.
262 *
263 * @return true if writing was successful.
264 */
265 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
266 opj_stream_private_t *cio,
267 opj_event_mgr_t * p_manager);
268
269 /**
270 Apply collected palette data
271 @param image Image.
272 @param color Collector for profile, cdef and pclr data.
273 @param p_manager the user event manager.
274 @return true in case of success
275 */
276 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
277 opj_jp2_color_t *color,
278 opj_event_mgr_t * p_manager);
279
280 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
281
282 /**
283 * Collect palette data
284 *
285 * @param jp2 JP2 handle
286 * @param p_pclr_header_data FIXME DOC
287 * @param p_pclr_header_size FIXME DOC
288 * @param p_manager
289 *
290 * @return Returns true if successful, returns false otherwise
291 */
292 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
293 OPJ_BYTE * p_pclr_header_data,
294 OPJ_UINT32 p_pclr_header_size,
295 opj_event_mgr_t * p_manager);
296
297 /**
298 * Collect component mapping data
299 *
300 * @param jp2 JP2 handle
301 * @param p_cmap_header_data FIXME DOC
302 * @param p_cmap_header_size FIXME DOC
303 * @param p_manager FIXME DOC
304 *
305 * @return Returns true if successful, returns false otherwise
306 */
307
308 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
309 OPJ_BYTE * p_cmap_header_data,
310 OPJ_UINT32 p_cmap_header_size,
311 opj_event_mgr_t * p_manager);
312
313 /**
314 * Reads the Color Specification box.
315 *
316 * @param p_colr_header_data pointer to actual data (already read from file)
317 * @param jp2 the jpeg2000 file codec.
318 * @param p_colr_header_size the size of the color header
319 * @param p_manager the user event manager.
320 *
321 * @return true if the bpc header is valid, false else.
322 */
323 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
324 OPJ_BYTE * p_colr_header_data,
325 OPJ_UINT32 p_colr_header_size,
326 opj_event_mgr_t * p_manager);
327
328 /*@}*/
329
330 /*@}*/
331
332 /**
333 * Sets up the procedures to do on writing header after the codestream.
334 * Developers wanting to extend the library can add their own writing procedures.
335 */
336 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
337 opj_event_mgr_t * p_manager);
338
339 /**
340 * Sets up the procedures to do on reading header after the codestream.
341 * Developers wanting to extend the library can add their own writing procedures.
342 */
343 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
344 opj_event_mgr_t * p_manager);
345
346 /**
347 * Reads a jpeg2000 file header structure.
348 *
349 * @param jp2 the jpeg2000 file header structure.
350 * @param stream the stream to read data from.
351 * @param p_manager the user event manager.
352 *
353 * @return true if the box is valid.
354 */
355 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
356 opj_stream_private_t *stream,
357 opj_event_mgr_t * p_manager);
358
359 /**
360 * Executes the given procedures on the given codec.
361 *
362 * @param p_procedure_list the list of procedures to execute
363 * @param jp2 the jpeg2000 file codec to execute the procedures on.
364 * @param stream the stream to execute the procedures on.
365 * @param p_manager the user manager.
366 *
367 * @return true if all the procedures were successfully executed.
368 */
369 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
370 opj_procedure_list_t * p_procedure_list,
371 opj_stream_private_t *stream,
372 opj_event_mgr_t * p_manager);
373
374 /**
375 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
376 *
377 * @param cio the input stream to read data from.
378 * @param box the box structure to fill.
379 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
380 * @param p_manager user event manager.
381 *
382 * @return true if the box is recognized, false otherwise
383 */
384 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
385 OPJ_UINT32 * p_number_bytes_read,
386 opj_stream_private_t *cio,
387 opj_event_mgr_t * p_manager);
388
389 /**
390 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
391 * are valid. Developers wanting to extend the library can add their own validation procedures.
392 */
393 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
394 opj_event_mgr_t * p_manager);
395
396 /**
397 * Sets up the procedures to do on writing header. Developers wanting to extend the library can add their own writing procedures.
398 */
399 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
400 opj_event_mgr_t * p_manager);
401
402 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
403 opj_stream_private_t *cio,
404 opj_event_mgr_t * p_manager);
405
406 /**
407 * Finds the image execution function related to the given box id.
408 *
409 * @param p_id the id of the handler to fetch.
410 *
411 * @return the given handler or NULL if it could not be found.
412 */
413 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
414 OPJ_UINT32 p_id);
415
416 /**
417 * Finds the execution function related to the given box id.
418 *
419 * @param p_id the id of the handler to fetch.
420 *
421 * @return the given handler or NULL if it could not be found.
422 */
423 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id);
424
425 static const opj_jp2_header_handler_t jp2_header [] = {
426 {JP2_JP, opj_jp2_read_jp},
427 {JP2_FTYP, opj_jp2_read_ftyp},
428 {JP2_JP2H, opj_jp2_read_jp2h}
429 };
430
431 static const opj_jp2_header_handler_t jp2_img_header [] = {
432 {JP2_IHDR, opj_jp2_read_ihdr},
433 {JP2_COLR, opj_jp2_read_colr},
434 {JP2_BPCC, opj_jp2_read_bpcc},
435 {JP2_PCLR, opj_jp2_read_pclr},
436 {JP2_CMAP, opj_jp2_read_cmap},
437 {JP2_CDEF, opj_jp2_read_cdef}
438
439 };
440
441 /**
442 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
443 *
444 * @param box the box structure to fill.
445 * @param p_data the character string to read data from.
446 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
447 * @param p_box_max_size the maximum number of bytes in the box.
448 * @param p_manager FIXME DOC
449 *
450 * @return true if the box is recognized, false otherwise
451 */
452 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
453 OPJ_BYTE * p_data,
454 OPJ_UINT32 * p_number_bytes_read,
455 OPJ_UINT32 p_box_max_size,
456 opj_event_mgr_t * p_manager);
457
458 /**
459 * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
460 * are valid. Developers wanting to extend the library can add their own validation procedures.
461 */
462 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
463 opj_event_mgr_t * p_manager);
464
465 /**
466 * Sets up the procedures to do on reading header.
467 * Developers wanting to extend the library can add their own writing procedures.
468 */
469 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
470 opj_event_mgr_t * p_manager);
471
472 /* ----------------------------------------------------------------------- */
opj_jp2_read_boxhdr(opj_jp2_box_t * box,OPJ_UINT32 * p_number_bytes_read,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)473 static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
474 OPJ_UINT32 * p_number_bytes_read,
475 opj_stream_private_t *cio,
476 opj_event_mgr_t * p_manager)
477 {
478 /* read header from file */
479 OPJ_BYTE l_data_header [8];
480
481 /* preconditions */
482 assert(cio != 00);
483 assert(box != 00);
484 assert(p_number_bytes_read != 00);
485 assert(p_manager != 00);
486
487 *p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, l_data_header, 8,
488 p_manager);
489 if (*p_number_bytes_read != 8) {
490 return OPJ_FALSE;
491 }
492
493 /* process read data */
494 opj_read_bytes(l_data_header, &(box->length), 4);
495 opj_read_bytes(l_data_header + 4, &(box->type), 4);
496
497 if (box->length == 0) { /* last box */
498 const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
499 if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
500 opj_event_msg(p_manager, EVT_ERROR,
501 "Cannot handle box sizes higher than 2^32\n");
502 return OPJ_FALSE;
503 }
504 box->length = (OPJ_UINT32)bleft + 8U;
505 assert((OPJ_OFF_T)box->length == bleft + 8);
506 return OPJ_TRUE;
507 }
508
509 /* do we have a "special very large box ?" */
510 /* read then the XLBox */
511 if (box->length == 1) {
512 OPJ_UINT32 l_xl_part_size;
513
514 OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,
515 l_data_header, 8, p_manager);
516 if (l_nb_bytes_read != 8) {
517 if (l_nb_bytes_read > 0) {
518 *p_number_bytes_read += l_nb_bytes_read;
519 }
520
521 return OPJ_FALSE;
522 }
523
524 *p_number_bytes_read = 16;
525 opj_read_bytes(l_data_header, &l_xl_part_size, 4);
526 if (l_xl_part_size != 0) {
527 opj_event_msg(p_manager, EVT_ERROR,
528 "Cannot handle box sizes higher than 2^32\n");
529 return OPJ_FALSE;
530 }
531 opj_read_bytes(l_data_header + 4, &(box->length), 4);
532 }
533 return OPJ_TRUE;
534 }
535
536 #if 0
537 static void jp2_write_url(opj_cio_t *cio, char *Idx_file)
538 {
539 OPJ_UINT32 i;
540 opj_jp2_box_t box;
541
542 box.init_pos = cio_tell(cio);
543 cio_skip(cio, 4);
544 cio_write(cio, JP2_URL, 4); /* DBTL */
545 cio_write(cio, 0, 1); /* VERS */
546 cio_write(cio, 0, 3); /* FLAG */
547
548 if (Idx_file) {
549 for (i = 0; i < strlen(Idx_file); i++) {
550 cio_write(cio, Idx_file[i], 1);
551 }
552 }
553
554 box.length = cio_tell(cio) - box.init_pos;
555 cio_seek(cio, box.init_pos);
556 cio_write(cio, box.length, 4); /* L */
557 cio_seek(cio, box.init_pos + box.length);
558 }
559 #endif
560
opj_jp2_read_ihdr(opj_jp2_t * jp2,OPJ_BYTE * p_image_header_data,OPJ_UINT32 p_image_header_size,opj_event_mgr_t * p_manager)561 static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
562 OPJ_BYTE *p_image_header_data,
563 OPJ_UINT32 p_image_header_size,
564 opj_event_mgr_t * p_manager)
565 {
566 /* preconditions */
567 assert(p_image_header_data != 00);
568 assert(jp2 != 00);
569 assert(p_manager != 00);
570
571 if (jp2->comps != NULL) {
572 opj_event_msg(p_manager, EVT_WARNING,
573 "Ignoring ihdr box. First ihdr box already read\n");
574 return OPJ_TRUE;
575 }
576
577 if (p_image_header_size != 14) {
578 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
579 return OPJ_FALSE;
580 }
581
582 opj_read_bytes(p_image_header_data, &(jp2->h), 4); /* HEIGHT */
583 p_image_header_data += 4;
584 opj_read_bytes(p_image_header_data, &(jp2->w), 4); /* WIDTH */
585 p_image_header_data += 4;
586 opj_read_bytes(p_image_header_data, &(jp2->numcomps), 2); /* NC */
587 p_image_header_data += 2;
588
589 if (jp2->h < 1 || jp2->w < 1 || jp2->numcomps < 1) {
590 opj_event_msg(p_manager, EVT_ERROR,
591 "Wrong values for: w(%d) h(%d) numcomps(%d) (ihdr)\n",
592 jp2->w, jp2->h, jp2->numcomps);
593 return OPJ_FALSE;
594 }
595 if ((jp2->numcomps - 1U) >=
596 16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
597 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
598 return OPJ_FALSE;
599 }
600
601 /* allocate memory for components */
602 opj_free(jp2->comps);
603 jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps,
604 sizeof(opj_jp2_comps_t));
605 if (jp2->comps == 0) {
606 opj_event_msg(p_manager, EVT_ERROR,
607 "Not enough memory to handle image header (ihdr)\n");
608 return OPJ_FALSE;
609 }
610
611 opj_read_bytes(p_image_header_data, &(jp2->bpc), 1); /* BPC */
612 ++ p_image_header_data;
613
614 opj_read_bytes(p_image_header_data, &(jp2->C), 1); /* C */
615 ++ p_image_header_data;
616
617 /* Should be equal to 7 cf. chapter about image header box of the norm */
618 if (jp2->C != 7) {
619 opj_event_msg(p_manager, EVT_INFO,
620 "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
621 jp2->C);
622 }
623
624 opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1); /* UnkC */
625 ++ p_image_header_data;
626 opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */
627 ++ p_image_header_data;
628
629 jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255);
630 jp2->j2k->ihdr_w = jp2->w;
631 jp2->j2k->ihdr_h = jp2->h;
632 jp2->has_ihdr = 1;
633
634 return OPJ_TRUE;
635 }
636
opj_jp2_write_ihdr(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)637 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
638 OPJ_UINT32 * p_nb_bytes_written
639 )
640 {
641 OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr;
642
643 /* preconditions */
644 assert(jp2 != 00);
645 assert(p_nb_bytes_written != 00);
646
647 /* default image header is 22 bytes wide */
648 l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22);
649 if (l_ihdr_data == 00) {
650 return 00;
651 }
652
653 l_current_ihdr_ptr = l_ihdr_data;
654
655 opj_write_bytes(l_current_ihdr_ptr, 22, 4); /* write box size */
656 l_current_ihdr_ptr += 4;
657
658 opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4); /* IHDR */
659 l_current_ihdr_ptr += 4;
660
661 opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4); /* HEIGHT */
662 l_current_ihdr_ptr += 4;
663
664 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
665 l_current_ihdr_ptr += 4;
666
667 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
668 l_current_ihdr_ptr += 2;
669
670 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
671 ++l_current_ihdr_ptr;
672
673 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
674 ++l_current_ihdr_ptr;
675
676 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC,
677 1); /* UnkC, colorspace unknown */
678 ++l_current_ihdr_ptr;
679
680 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR,
681 1); /* IPR, no intellectual property */
682 ++l_current_ihdr_ptr;
683
684 *p_nb_bytes_written = 22;
685
686 return l_ihdr_data;
687 }
688
opj_jp2_write_bpcc(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)689 static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
690 OPJ_UINT32 * p_nb_bytes_written
691 )
692 {
693 OPJ_UINT32 i;
694 /* room for 8 bytes for box and 1 byte for each component */
695 OPJ_UINT32 l_bpcc_size;
696 OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr;
697
698 /* preconditions */
699 assert(jp2 != 00);
700 assert(p_nb_bytes_written != 00);
701 l_bpcc_size = 8 + jp2->numcomps;
702
703 l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size);
704 if (l_bpcc_data == 00) {
705 return 00;
706 }
707
708 l_current_bpcc_ptr = l_bpcc_data;
709
710 opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size,
711 4); /* write box size */
712 l_current_bpcc_ptr += 4;
713
714 opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4); /* BPCC */
715 l_current_bpcc_ptr += 4;
716
717 for (i = 0; i < jp2->numcomps; ++i) {
718 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc,
719 1); /* write each component information */
720 ++l_current_bpcc_ptr;
721 }
722
723 *p_nb_bytes_written = l_bpcc_size;
724
725 return l_bpcc_data;
726 }
727
opj_jp2_read_bpcc(opj_jp2_t * jp2,OPJ_BYTE * p_bpc_header_data,OPJ_UINT32 p_bpc_header_size,opj_event_mgr_t * p_manager)728 static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
729 OPJ_BYTE * p_bpc_header_data,
730 OPJ_UINT32 p_bpc_header_size,
731 opj_event_mgr_t * p_manager
732 )
733 {
734 OPJ_UINT32 i;
735
736 /* preconditions */
737 assert(p_bpc_header_data != 00);
738 assert(jp2 != 00);
739 assert(p_manager != 00);
740
741
742 if (jp2->bpc != 255) {
743 opj_event_msg(p_manager, EVT_WARNING,
744 "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",
745 jp2->bpc);
746 }
747
748 /* and length is relevant */
749 if (p_bpc_header_size != jp2->numcomps) {
750 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
751 return OPJ_FALSE;
752 }
753
754 /* read info for each component */
755 for (i = 0; i < jp2->numcomps; ++i) {
756 opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc,
757 1); /* read each BPCC component */
758 ++p_bpc_header_data;
759 }
760
761 return OPJ_TRUE;
762 }
opj_jp2_write_cdef(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)763 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
764 OPJ_UINT32 * p_nb_bytes_written)
765 {
766 /* room for 8 bytes for box, 2 for n */
767 OPJ_UINT32 l_cdef_size = 10;
768 OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr;
769 OPJ_UINT32 l_value;
770 OPJ_UINT16 i;
771
772 /* preconditions */
773 assert(jp2 != 00);
774 assert(p_nb_bytes_written != 00);
775 assert(jp2->color.jp2_cdef != 00);
776 assert(jp2->color.jp2_cdef->info != 00);
777 assert(jp2->color.jp2_cdef->n > 0U);
778
779 l_cdef_size += 6U * jp2->color.jp2_cdef->n;
780
781 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
782 if (l_cdef_data == 00) {
783 return 00;
784 }
785
786 l_current_cdef_ptr = l_cdef_data;
787
788 opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4); /* write box size */
789 l_current_cdef_ptr += 4;
790
791 opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4); /* BPCC */
792 l_current_cdef_ptr += 4;
793
794 l_value = jp2->color.jp2_cdef->n;
795 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* N */
796 l_current_cdef_ptr += 2;
797
798 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
799 l_value = jp2->color.jp2_cdef->info[i].cn;
800 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Cni */
801 l_current_cdef_ptr += 2;
802 l_value = jp2->color.jp2_cdef->info[i].typ;
803 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Typi */
804 l_current_cdef_ptr += 2;
805 l_value = jp2->color.jp2_cdef->info[i].asoc;
806 opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Asoci */
807 l_current_cdef_ptr += 2;
808 }
809 *p_nb_bytes_written = l_cdef_size;
810
811 return l_cdef_data;
812 }
813
opj_jp2_write_colr(opj_jp2_t * jp2,OPJ_UINT32 * p_nb_bytes_written)814 static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
815 OPJ_UINT32 * p_nb_bytes_written
816 )
817 {
818 /* room for 8 bytes for box 3 for common data and variable upon profile*/
819 OPJ_UINT32 l_colr_size = 11;
820 OPJ_BYTE * l_colr_data, * l_current_colr_ptr;
821
822 /* preconditions */
823 assert(jp2 != 00);
824 assert(p_nb_bytes_written != 00);
825 assert(jp2->meth == 1 || jp2->meth == 2);
826
827 switch (jp2->meth) {
828 case 1 :
829 l_colr_size += 4; /* EnumCS */
830 break;
831 case 2 :
832 assert(jp2->color.icc_profile_len); /* ICC profile */
833 l_colr_size += jp2->color.icc_profile_len;
834 break;
835 default :
836 return 00;
837 }
838
839 l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size);
840 if (l_colr_data == 00) {
841 return 00;
842 }
843
844 l_current_colr_ptr = l_colr_data;
845
846 opj_write_bytes(l_current_colr_ptr, l_colr_size,
847 4); /* write box size */
848 l_current_colr_ptr += 4;
849
850 opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4); /* BPCC */
851 l_current_colr_ptr += 4;
852
853 opj_write_bytes(l_current_colr_ptr, jp2->meth, 1); /* METH */
854 ++l_current_colr_ptr;
855
856 opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1); /* PRECEDENCE */
857 ++l_current_colr_ptr;
858
859 opj_write_bytes(l_current_colr_ptr, jp2->approx, 1); /* APPROX */
860 ++l_current_colr_ptr;
861
862 if (jp2->meth ==
863 1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
864 opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4);
865 } /* EnumCS */
866 else {
867 if (jp2->meth == 2) { /* ICC profile */
868 OPJ_UINT32 i;
869 for (i = 0; i < jp2->color.icc_profile_len; ++i) {
870 opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
871 ++l_current_colr_ptr;
872 }
873 }
874 }
875
876 *p_nb_bytes_written = l_colr_size;
877
878 return l_colr_data;
879 }
880
opj_jp2_free_pclr(opj_jp2_color_t * color)881 static void opj_jp2_free_pclr(opj_jp2_color_t *color)
882 {
883 opj_free(color->jp2_pclr->channel_sign);
884 opj_free(color->jp2_pclr->channel_size);
885 opj_free(color->jp2_pclr->entries);
886
887 if (color->jp2_pclr->cmap) {
888 opj_free(color->jp2_pclr->cmap);
889 }
890
891 opj_free(color->jp2_pclr);
892 color->jp2_pclr = NULL;
893 }
894
opj_jp2_check_color(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * p_manager)895 static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
896 opj_event_mgr_t *p_manager)
897 {
898 OPJ_UINT16 i;
899
900 /* testcase 4149.pdf.SIGSEGV.cf7.3501 */
901 if (color->jp2_cdef) {
902 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
903 OPJ_UINT16 n = color->jp2_cdef->n;
904 OPJ_UINT32 nr_channels =
905 image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
906
907 /* cdef applies to cmap channels if any */
908 if (color->jp2_pclr && color->jp2_pclr->cmap) {
909 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
910 }
911
912 for (i = 0; i < n; i++) {
913 if (info[i].cn >= nr_channels) {
914 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
915 info[i].cn, nr_channels);
916 return OPJ_FALSE;
917 }
918 if (info[i].asoc == 65535U) {
919 continue;
920 }
921
922 if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
923 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
924 info[i].asoc - 1, nr_channels);
925 return OPJ_FALSE;
926 }
927 }
928
929 /* issue 397 */
930 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
931 while (nr_channels > 0) {
932 for (i = 0; i < n; ++i) {
933 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
934 break;
935 }
936 }
937 if (i == n) {
938 opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
939 return OPJ_FALSE;
940 }
941 --nr_channels;
942 }
943 }
944
945 /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
946 66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
947 if (color->jp2_pclr && color->jp2_pclr->cmap) {
948 OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
949 opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
950 OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
951
952 /* verify that all original components match an existing one */
953 for (i = 0; i < nr_channels; i++) {
954 if (cmap[i].cmp >= image->numcomps) {
955 opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
956 cmap[i].cmp, image->numcomps);
957 is_sane = OPJ_FALSE;
958 }
959 }
960
961 pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
962 if (!pcol_usage) {
963 opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
964 return OPJ_FALSE;
965 }
966 /* verify that no component is targeted more than once */
967 for (i = 0; i < nr_channels; i++) {
968 OPJ_BYTE mtyp = cmap[i].mtyp;
969 OPJ_BYTE pcol = cmap[i].pcol;
970 /* See ISO 15444-1 Table I.14 – MTYPi field values */
971 if (mtyp != 0 && mtyp != 1) {
972 opj_event_msg(p_manager, EVT_ERROR,
973 "Invalid value for cmap[%d].mtyp = %d.\n", i,
974 mtyp);
975 is_sane = OPJ_FALSE;
976 } else if (pcol >= nr_channels) {
977 opj_event_msg(p_manager, EVT_ERROR,
978 "Invalid component/palette index for direct mapping %d.\n", pcol);
979 is_sane = OPJ_FALSE;
980 } else if (pcol_usage[pcol] && mtyp == 1) {
981 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
982 is_sane = OPJ_FALSE;
983 } else if (mtyp == 0 && pcol != 0) {
984 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
985 * the value of this field shall be 0. */
986 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
987 pcol);
988 is_sane = OPJ_FALSE;
989 } else if (mtyp == 1 && pcol != i) {
990 /* OpenJPEG implementation limitation. See assert(i == pcol); */
991 /* in opj_jp2_apply_pclr() */
992 opj_event_msg(p_manager, EVT_ERROR,
993 "Implementation limitation: for palette mapping, "
994 "pcol[%d] should be equal to %d, but is equal "
995 "to %d.\n", i, i, pcol);
996 is_sane = OPJ_FALSE;
997 } else {
998 pcol_usage[pcol] = OPJ_TRUE;
999 }
1000 }
1001 /* verify that all components are targeted at least once */
1002 for (i = 0; i < nr_channels; i++) {
1003 if (!pcol_usage[i] && cmap[i].mtyp != 0) {
1004 opj_event_msg(p_manager, EVT_ERROR, "Component %d doesn't have a mapping.\n",
1005 i);
1006 is_sane = OPJ_FALSE;
1007 }
1008 }
1009 /* Issue 235/447 weird cmap */
1010 if (1 && is_sane && (image->numcomps == 1U)) {
1011 for (i = 0; i < nr_channels; i++) {
1012 if (!pcol_usage[i]) {
1013 is_sane = 0U;
1014 opj_event_msg(p_manager, EVT_WARNING,
1015 "Component mapping seems wrong. Trying to correct.\n");
1016 break;
1017 }
1018 }
1019 if (!is_sane) {
1020 is_sane = OPJ_TRUE;
1021 for (i = 0; i < nr_channels; i++) {
1022 cmap[i].mtyp = 1U;
1023 cmap[i].pcol = (OPJ_BYTE) i;
1024 }
1025 }
1026 }
1027 opj_free(pcol_usage);
1028 if (!is_sane) {
1029 return OPJ_FALSE;
1030 }
1031 }
1032
1033 return OPJ_TRUE;
1034 }
1035
1036 /* file9.jp2 */
opj_jp2_apply_pclr(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * p_manager)1037 static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
1038 opj_jp2_color_t *color,
1039 opj_event_mgr_t * p_manager)
1040 {
1041 opj_image_comp_t *old_comps, *new_comps;
1042 OPJ_BYTE *channel_size, *channel_sign;
1043 OPJ_UINT32 *entries;
1044 opj_jp2_cmap_comp_t *cmap;
1045 OPJ_INT32 *src, *dst;
1046 OPJ_UINT32 j, max;
1047 OPJ_UINT16 i, nr_channels, cmp, pcol;
1048 OPJ_INT32 k, top_k;
1049
1050 channel_size = color->jp2_pclr->channel_size;
1051 channel_sign = color->jp2_pclr->channel_sign;
1052 entries = color->jp2_pclr->entries;
1053 cmap = color->jp2_pclr->cmap;
1054 nr_channels = color->jp2_pclr->nr_channels;
1055
1056 for (i = 0; i < nr_channels; ++i) {
1057 /* Palette mapping: */
1058 cmp = cmap[i].cmp;
1059 if (image->comps[cmp].data == NULL) {
1060 opj_event_msg(p_manager, EVT_ERROR,
1061 "image->comps[%d].data == NULL in opj_jp2_apply_pclr().\n", i);
1062 return OPJ_FALSE;
1063 }
1064 }
1065
1066 old_comps = image->comps;
1067 /* Overflow check: prevent integer overflow */
1068 for (i = 0; i < nr_channels; ++i) {
1069 cmp = cmap[i].cmp;
1070 if (old_comps[cmp].h == 0 || old_comps[cmp].w > ((OPJ_UINT32)-1) / sizeof(OPJ_INT32) / old_comps[cmp].h) {
1071 return OPJ_FALSE;
1072 }
1073 }
1074
1075 new_comps = (opj_image_comp_t*)
1076 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
1077 if (!new_comps) {
1078 opj_event_msg(p_manager, EVT_ERROR,
1079 "Memory allocation failure in opj_jp2_apply_pclr().\n");
1080 return OPJ_FALSE;
1081 }
1082 for (i = 0; i < nr_channels; ++i) {
1083 pcol = cmap[i].pcol;
1084 cmp = cmap[i].cmp;
1085
1086 /* Direct use */
1087 if (cmap[i].mtyp == 0) {
1088 assert(pcol == 0);
1089 new_comps[i] = old_comps[cmp];
1090 } else {
1091 assert( i == pcol ); // probably wrong?
1092 new_comps[i] = old_comps[cmp];
1093 }
1094
1095 /* Palette mapping: */
1096 new_comps[i].data = (OPJ_INT32*)
1097 opj_image_data_alloc(sizeof(OPJ_INT32) * old_comps[cmp].w * old_comps[cmp].h);
1098 if (!new_comps[i].data) {
1099 while (i > 0) {
1100 -- i;
1101 opj_image_data_free(new_comps[i].data);
1102 }
1103 opj_free(new_comps);
1104 opj_event_msg(p_manager, EVT_ERROR,
1105 "Memory allocation failure in opj_jp2_apply_pclr().\n");
1106 return OPJ_FALSE;
1107 }
1108 new_comps[i].prec = channel_size[i];
1109 new_comps[i].sgnd = channel_sign[i];
1110 }
1111
1112 top_k = color->jp2_pclr->nr_entries - 1;
1113
1114 for (i = 0; i < nr_channels; ++i) {
1115 /* Palette mapping: */
1116 cmp = cmap[i].cmp;
1117 pcol = cmap[i].pcol;
1118 src = old_comps[cmp].data;
1119 dst = new_comps[i].data;
1120 max = new_comps[i].w * new_comps[i].h;
1121
1122 /* Prevent null pointer access */
1123 if (!src || !dst) {
1124 for (j = 0; j < nr_channels; ++j) {
1125 opj_image_data_free(new_comps[j].data);
1126 }
1127 opj_free(new_comps);
1128 new_comps = NULL;
1129 return OPJ_FALSE;
1130 }
1131
1132 /* Direct use: */
1133 if (cmap[i].mtyp == 0) {
1134 for (j = 0; j < max; ++j) {
1135 dst[j] = src[j];
1136 }
1137 } else {
1138 assert( i == pcol ); // probably wrong?
1139 for (j = 0; j < max; ++j) {
1140 /* The index */
1141 if ((k = src[j]) < 0) {
1142 k = 0;
1143 } else if (k > top_k) {
1144 k = top_k;
1145 }
1146
1147 /* The colour */
1148 dst[j] = (OPJ_INT32)entries[k * nr_channels + pcol];
1149 }
1150 }
1151 }
1152
1153 max = image->numcomps;
1154 for (j = 0; j < max; ++j) {
1155 if (old_comps[j].data) {
1156 opj_image_data_free(old_comps[j].data);
1157 }
1158 }
1159
1160 opj_free(old_comps);
1161 image->comps = new_comps;
1162 image->numcomps = nr_channels;
1163
1164 return OPJ_TRUE;
1165 }/* apply_pclr() */
1166
opj_jp2_read_pclr(opj_jp2_t * jp2,OPJ_BYTE * p_pclr_header_data,OPJ_UINT32 p_pclr_header_size,opj_event_mgr_t * p_manager)1167 static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
1168 OPJ_BYTE * p_pclr_header_data,
1169 OPJ_UINT32 p_pclr_header_size,
1170 opj_event_mgr_t * p_manager
1171 )
1172 {
1173 opj_jp2_pclr_t *jp2_pclr;
1174 OPJ_BYTE *channel_size, *channel_sign;
1175 OPJ_UINT32 *entries;
1176 OPJ_UINT16 nr_entries, nr_channels;
1177 OPJ_UINT16 i, j;
1178 OPJ_UINT32 l_value;
1179 OPJ_BYTE *orig_header_data = p_pclr_header_data;
1180
1181 /* preconditions */
1182 assert(p_pclr_header_data != 00);
1183 assert(jp2 != 00);
1184 assert(p_manager != 00);
1185 (void)p_pclr_header_size;
1186
1187 if (jp2->color.jp2_pclr) {
1188 return OPJ_FALSE;
1189 }
1190
1191 if (p_pclr_header_size < 3) {
1192 return OPJ_FALSE;
1193 }
1194
1195 opj_read_bytes(p_pclr_header_data, &l_value, 2); /* NE */
1196 p_pclr_header_data += 2;
1197 nr_entries = (OPJ_UINT16) l_value;
1198 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1199 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports %d entries\n",
1200 (int)nr_entries);
1201 return OPJ_FALSE;
1202 }
1203
1204 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* NPC */
1205 ++p_pclr_header_data;
1206 nr_channels = (OPJ_UINT16) l_value;
1207 if (nr_channels == 0U) {
1208 opj_event_msg(p_manager, EVT_ERROR,
1209 "Invalid PCLR box. Reports 0 palette columns\n");
1210 return OPJ_FALSE;
1211 }
1212
1213 if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels) {
1214 return OPJ_FALSE;
1215 }
1216
1217 entries = (OPJ_UINT32*) opj_malloc(sizeof(OPJ_UINT32) * nr_channels *
1218 nr_entries);
1219 if (!entries) {
1220 return OPJ_FALSE;
1221 }
1222 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
1223 if (!channel_size) {
1224 opj_free(entries);
1225 return OPJ_FALSE;
1226 }
1227 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
1228 if (!channel_sign) {
1229 opj_free(entries);
1230 opj_free(channel_size);
1231 return OPJ_FALSE;
1232 }
1233
1234 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
1235 if (!jp2_pclr) {
1236 opj_free(entries);
1237 opj_free(channel_size);
1238 opj_free(channel_sign);
1239 return OPJ_FALSE;
1240 }
1241
1242 jp2_pclr->channel_sign = channel_sign;
1243 jp2_pclr->channel_size = channel_size;
1244 jp2_pclr->entries = entries;
1245 jp2_pclr->nr_entries = nr_entries;
1246 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
1247 jp2_pclr->cmap = NULL;
1248
1249 jp2->color.jp2_pclr = jp2_pclr;
1250
1251 for (i = 0; i < nr_channels; ++i) {
1252 opj_read_bytes(p_pclr_header_data, &l_value, 1); /* Bi */
1253 ++p_pclr_header_data;
1254
1255 channel_size[i] = (OPJ_BYTE)((l_value & 0x7f) + 1);
1256 channel_sign[i] = (l_value & 0x80) ? 1 : 0;
1257 }
1258
1259 for (j = 0; j < nr_entries; ++j) {
1260 for (i = 0; i < nr_channels; ++i) {
1261 OPJ_UINT32 bytes_to_read = (OPJ_UINT32)((channel_size[i] + 7) >> 3);
1262
1263 if (bytes_to_read > sizeof(OPJ_UINT32)) {
1264 bytes_to_read = sizeof(OPJ_UINT32);
1265 }
1266 if ((ptrdiff_t)p_pclr_header_size < (ptrdiff_t)(p_pclr_header_data -
1267 orig_header_data) + (ptrdiff_t)bytes_to_read) {
1268 return OPJ_FALSE;
1269 }
1270
1271 opj_read_bytes(p_pclr_header_data, &l_value, bytes_to_read); /* Cji */
1272 p_pclr_header_data += bytes_to_read;
1273 *entries = (OPJ_UINT32) l_value;
1274 entries++;
1275 }
1276 }
1277
1278 return OPJ_TRUE;
1279 }
1280
opj_jp2_read_cmap(opj_jp2_t * jp2,OPJ_BYTE * p_cmap_header_data,OPJ_UINT32 p_cmap_header_size,opj_event_mgr_t * p_manager)1281 static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
1282 OPJ_BYTE * p_cmap_header_data,
1283 OPJ_UINT32 p_cmap_header_size,
1284 opj_event_mgr_t * p_manager
1285 )
1286 {
1287 opj_jp2_cmap_comp_t *cmap;
1288 OPJ_BYTE i, nr_channels;
1289 OPJ_UINT32 l_value;
1290
1291 /* preconditions */
1292 assert(jp2 != 00);
1293 assert(p_cmap_header_data != 00);
1294 assert(p_manager != 00);
1295 (void)p_cmap_header_size;
1296
1297 /* Need nr_channels: */
1298 if (jp2->color.jp2_pclr == NULL) {
1299 opj_event_msg(p_manager, EVT_ERROR,
1300 "Need to read a PCLR box before the CMAP box.\n");
1301 return OPJ_FALSE;
1302 }
1303
1304 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
1305 * inside a JP2 Header box' :
1306 */
1307 if (jp2->color.jp2_pclr->cmap) {
1308 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
1309 return OPJ_FALSE;
1310 }
1311
1312 nr_channels = jp2->color.jp2_pclr->nr_channels;
1313 if (p_cmap_header_size < (OPJ_UINT32)nr_channels * 4) {
1314 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CMAP box.\n");
1315 return OPJ_FALSE;
1316 }
1317
1318 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(
1319 opj_jp2_cmap_comp_t));
1320 if (!cmap) {
1321 return OPJ_FALSE;
1322 }
1323
1324
1325 for (i = 0; i < nr_channels; ++i) {
1326 opj_read_bytes_BE(p_cmap_header_data, &l_value, 2); /* CMP^i */
1327 p_cmap_header_data += 2;
1328 cmap[i].cmp = (OPJ_UINT16) l_value;
1329
1330 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
1331 ++p_cmap_header_data;
1332 cmap[i].mtyp = (OPJ_BYTE) l_value;
1333
1334 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
1335 ++p_cmap_header_data;
1336 cmap[i].pcol = (OPJ_BYTE) l_value;
1337 }
1338
1339 jp2->color.jp2_pclr->cmap = cmap;
1340
1341 return OPJ_TRUE;
1342 }
1343
opj_jp2_apply_cdef(opj_image_t * image,opj_jp2_color_t * color,opj_event_mgr_t * manager)1344 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
1345 opj_event_mgr_t *manager)
1346 {
1347 opj_jp2_cdef_info_t *info;
1348 OPJ_UINT16 i, n, cn, asoc, acn;
1349
1350 info = color->jp2_cdef->info;
1351 n = color->jp2_cdef->n;
1352
1353 for (i = 0; i < n; ++i) {
1354 /* WATCH: acn = asoc - 1 ! */
1355 asoc = info[i].asoc;
1356 cn = info[i].cn;
1357
1358 if (cn >= image->numcomps) {
1359 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: cn=%d, numcomps=%d\n",
1360 cn, image->numcomps);
1361 continue;
1362 }
1363 if (asoc == 0 || asoc == 65535) {
1364 image->comps[cn].alpha = info[i].typ;
1365 continue;
1366 }
1367
1368 acn = (OPJ_UINT16)(asoc - 1);
1369 if (acn >= image->numcomps) {
1370 opj_event_msg(manager, EVT_WARNING, "opj_jp2_apply_cdef: acn=%d, numcomps=%d\n",
1371 acn, image->numcomps);
1372 continue;
1373 }
1374
1375 /* Swap only if color channel */
1376 if ((cn != acn) && (info[i].typ == 0)) {
1377 opj_image_comp_t saved;
1378 OPJ_UINT16 j;
1379
1380 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
1381 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
1382 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
1383
1384 /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1385 for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) {
1386 if (info[j].cn == cn) {
1387 info[j].cn = acn;
1388 } else if (info[j].cn == acn) {
1389 info[j].cn = cn;
1390 }
1391 /* asoc is related to color index. Do not update. */
1392 }
1393 }
1394
1395 image->comps[cn].alpha = info[i].typ;
1396 }
1397
1398 if (color->jp2_cdef->info) {
1399 opj_free(color->jp2_cdef->info);
1400 }
1401
1402 opj_free(color->jp2_cdef);
1403 color->jp2_cdef = NULL;
1404
1405 }/* jp2_apply_cdef() */
1406
opj_jp2_read_cdef(opj_jp2_t * jp2,OPJ_BYTE * p_cdef_header_data,OPJ_UINT32 p_cdef_header_size,opj_event_mgr_t * p_manager)1407 static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
1408 OPJ_BYTE * p_cdef_header_data,
1409 OPJ_UINT32 p_cdef_header_size,
1410 opj_event_mgr_t * p_manager
1411 )
1412 {
1413 opj_jp2_cdef_info_t *cdef_info;
1414 OPJ_UINT16 i;
1415 OPJ_UINT32 l_value;
1416
1417 /* preconditions */
1418 assert(jp2 != 00);
1419 assert(p_cdef_header_data != 00);
1420 assert(p_manager != 00);
1421 (void)p_cdef_header_size;
1422
1423 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
1424 * inside a JP2 Header box.'*/
1425 if (jp2->color.jp2_cdef) {
1426 return OPJ_FALSE;
1427 }
1428
1429 if (p_cdef_header_size < 2) {
1430 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1431 return OPJ_FALSE;
1432 }
1433
1434 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* N */
1435 p_cdef_header_data += 2;
1436
1437 if ((OPJ_UINT16)l_value == 0) { /* szukw000: FIXME */
1438 opj_event_msg(p_manager, EVT_ERROR,
1439 "Number of channel description is equal to zero in CDEF box.\n");
1440 return OPJ_FALSE;
1441 }
1442
1443 if (p_cdef_header_size < 2 + (OPJ_UINT32)(OPJ_UINT16)l_value * 6) {
1444 opj_event_msg(p_manager, EVT_ERROR, "Insufficient data for CDEF box.\n");
1445 return OPJ_FALSE;
1446 }
1447
1448 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(
1449 opj_jp2_cdef_info_t));
1450 if (!cdef_info) {
1451 return OPJ_FALSE;
1452 }
1453
1454 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
1455 if (!jp2->color.jp2_cdef) {
1456 opj_free(cdef_info);
1457 return OPJ_FALSE;
1458 }
1459 jp2->color.jp2_cdef->info = cdef_info;
1460 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
1461
1462 for (i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1463 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1464 p_cdef_header_data += 2;
1465 cdef_info[i].cn = (OPJ_UINT16) l_value;
1466
1467 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1468 p_cdef_header_data += 2;
1469 cdef_info[i].typ = (OPJ_UINT16) l_value;
1470
1471 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1472 p_cdef_header_data += 2;
1473 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1474 }
1475
1476 return OPJ_TRUE;
1477 }
1478
opj_jp2_read_colr(opj_jp2_t * jp2,OPJ_BYTE * p_colr_header_data,OPJ_UINT32 p_colr_header_size,opj_event_mgr_t * p_manager)1479 static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
1480 OPJ_BYTE * p_colr_header_data,
1481 OPJ_UINT32 p_colr_header_size,
1482 opj_event_mgr_t * p_manager
1483 )
1484 {
1485 OPJ_UINT32 l_value;
1486
1487 /* preconditions */
1488 assert(jp2 != 00);
1489 assert(p_colr_header_data != 00);
1490 assert(p_manager != 00);
1491
1492 if (p_colr_header_size < 3) {
1493 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1494 return OPJ_FALSE;
1495 }
1496
1497 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1498 * Specification boxes after the first.'
1499 */
1500 if (jp2->color.jp2_has_colr) {
1501 opj_event_msg(p_manager, EVT_INFO,
1502 "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1503 p_colr_header_data += p_colr_header_size;
1504 return OPJ_TRUE;
1505 }
1506
1507 opj_read_bytes(p_colr_header_data, &jp2->meth, 1); /* METH */
1508 ++p_colr_header_data;
1509
1510 opj_read_bytes(p_colr_header_data, &jp2->precedence, 1); /* PRECEDENCE */
1511 ++p_colr_header_data;
1512
1513 opj_read_bytes(p_colr_header_data, &jp2->approx, 1); /* APPROX */
1514 ++p_colr_header_data;
1515
1516 if (jp2->meth == 1) {
1517 if (p_colr_header_size < 7) {
1518 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size: %d)\n",
1519 p_colr_header_size);
1520 return OPJ_FALSE;
1521 }
1522 if ((p_colr_header_size > 7) &&
1523 (jp2->enumcs != 14)) { /* handled below for CIELab) */
1524 /* testcase Altona_Technical_v20_x4.pdf */
1525 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header box (bad size: %d)\n",
1526 p_colr_header_size);
1527 }
1528
1529 opj_read_bytes(p_colr_header_data, &jp2->enumcs, 4); /* EnumCS */
1530
1531 p_colr_header_data += 4;
1532
1533 if (jp2->enumcs == 14) { /* CIELab */
1534 OPJ_UINT32 *cielab;
1535 OPJ_UINT32 rl, ol, ra, oa, rb, ob, il;
1536
1537 cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32));
1538 if (cielab == NULL) {
1539 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for cielab\n");
1540 return OPJ_FALSE;
1541 }
1542 cielab[0] = 14; /* enumcs */
1543
1544 /* default values */
1545 rl = ra = rb = ol = oa = ob = 0;
1546 il = 0x00443530; /* D50 */
1547 cielab[1] = 0x44454600;/* DEF */
1548
1549 if (p_colr_header_size == 35) {
1550 opj_read_bytes(p_colr_header_data, &rl, 4);
1551 p_colr_header_data += 4;
1552 opj_read_bytes(p_colr_header_data, &ol, 4);
1553 p_colr_header_data += 4;
1554 opj_read_bytes(p_colr_header_data, &ra, 4);
1555 p_colr_header_data += 4;
1556 opj_read_bytes(p_colr_header_data, &oa, 4);
1557 p_colr_header_data += 4;
1558 opj_read_bytes(p_colr_header_data, &rb, 4);
1559 p_colr_header_data += 4;
1560 opj_read_bytes(p_colr_header_data, &ob, 4);
1561 p_colr_header_data += 4;
1562 opj_read_bytes(p_colr_header_data, &il, 4);
1563 p_colr_header_data += 4;
1564
1565 cielab[1] = 0;
1566 } else if (p_colr_header_size != 7) {
1567 opj_event_msg(p_manager, EVT_WARNING,
1568 "Bad COLR header box (CIELab, bad size: %d)\n", p_colr_header_size);
1569 }
1570 cielab[2] = rl;
1571 cielab[4] = ra;
1572 cielab[6] = rb;
1573 cielab[3] = ol;
1574 cielab[5] = oa;
1575 cielab[7] = ob;
1576 cielab[8] = il;
1577
1578 jp2->color.icc_profile_buf = (OPJ_BYTE*)cielab;
1579 jp2->color.icc_profile_len = 0;
1580 }
1581 jp2->color.jp2_has_colr = 1;
1582 } else if (jp2->meth == 2) {
1583 /* ICC profile */
1584 OPJ_INT32 it_icc_value = 0;
1585 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3;
1586
1587 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len;
1588 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1, (size_t)icc_len);
1589 if (!jp2->color.icc_profile_buf) {
1590 jp2->color.icc_profile_len = 0;
1591 return OPJ_FALSE;
1592 }
1593
1594 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) {
1595 opj_read_bytes(p_colr_header_data, &l_value, 1); /* icc values */
1596 ++p_colr_header_data;
1597 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1598 }
1599
1600 jp2->color.jp2_has_colr = 1;
1601 } else if (jp2->meth > 2) {
1602 /* ISO/IEC 15444-1:2004 (E), Table I.9 Legal METH values:
1603 conforming JP2 reader shall ignore the entire Colour Specification box.*/
1604 opj_event_msg(p_manager, EVT_INFO,
1605 "COLR BOX meth value is not a regular value (%d), "
1606 "so we will ignore the entire Colour Specification box. \n", jp2->meth);
1607 }
1608
1609 return OPJ_TRUE;
1610 }
1611
opj_jp2_decode(void * p_jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)1612 OPJ_BOOL opj_jp2_decode(void *p_jp2,
1613 opj_stream_private_t *p_stream,
1614 opj_image_t* p_image,
1615 opj_event_mgr_t * p_manager)
1616 {
1617 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
1618 if (!p_image) {
1619 return OPJ_FALSE;
1620 }
1621
1622 /* J2K decoding */
1623 if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1624 opj_event_msg(p_manager, EVT_ERROR,
1625 "Failed to decode the codestream in the JP2 file\n");
1626 return OPJ_FALSE;
1627 }
1628
1629 if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
1630 /* Bypass all JP2 component transforms */
1631 return OPJ_TRUE;
1632 }
1633
1634 if (!jp2->ignore_pclr_cmap_cdef) {
1635 if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1636 return OPJ_FALSE;
1637 }
1638
1639 /* Set Image Color Space */
1640 if (jp2->enumcs == 16) {
1641 p_image->color_space = OPJ_CLRSPC_SRGB;
1642 } else if (jp2->enumcs == 17) {
1643 p_image->color_space = OPJ_CLRSPC_GRAY;
1644 } else if (jp2->enumcs == 18) {
1645 p_image->color_space = OPJ_CLRSPC_SYCC;
1646 } else if (jp2->enumcs == 24) {
1647 p_image->color_space = OPJ_CLRSPC_EYCC;
1648 } else if (jp2->enumcs == 12) {
1649 p_image->color_space = OPJ_CLRSPC_CMYK;
1650 } else {
1651 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1652 }
1653
1654 if (jp2->color.jp2_pclr) {
1655 /* Part 1, I.5.3.4: Either both or none : */
1656 if (!jp2->color.jp2_pclr->cmap) {
1657 opj_jp2_free_pclr(&(jp2->color));
1658 } else {
1659 if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) {
1660 return OPJ_FALSE;
1661 }
1662 }
1663 }
1664
1665 /* Apply the color space if needed */
1666 if (jp2->color.jp2_cdef) {
1667 opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1668 }
1669
1670 if (jp2->color.icc_profile_buf) {
1671 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1672 p_image->icc_profile_len = jp2->color.icc_profile_len;
1673 jp2->color.icc_profile_buf = NULL;
1674 }
1675 }
1676
1677 return OPJ_TRUE;
1678 }
1679
opj_jp2_write_jp2h(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)1680 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1681 opj_stream_private_t *stream,
1682 opj_event_mgr_t * p_manager
1683 )
1684 {
1685 opj_jp2_img_header_writer_handler_t l_writers [4];
1686 opj_jp2_img_header_writer_handler_t * l_current_writer;
1687
1688 OPJ_INT32 i, l_nb_pass;
1689 /* size of data for super box*/
1690 OPJ_UINT32 l_jp2h_size = 8;
1691 OPJ_BOOL l_result = OPJ_TRUE;
1692
1693 /* to store the data of the super box */
1694 OPJ_BYTE l_jp2h_data [8];
1695
1696 /* preconditions */
1697 assert(stream != 00);
1698 assert(jp2 != 00);
1699 assert(p_manager != 00);
1700
1701 memset(l_writers, 0, sizeof(l_writers));
1702
1703 if (jp2->bpc == 255) {
1704 l_nb_pass = 3;
1705 l_writers[0].handler = opj_jp2_write_ihdr;
1706 l_writers[1].handler = opj_jp2_write_bpcc;
1707 l_writers[2].handler = opj_jp2_write_colr;
1708 } else {
1709 l_nb_pass = 2;
1710 l_writers[0].handler = opj_jp2_write_ihdr;
1711 l_writers[1].handler = opj_jp2_write_colr;
1712 }
1713
1714 if (jp2->color.jp2_cdef != NULL) {
1715 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1716 l_nb_pass++;
1717 }
1718
1719 /* write box header */
1720 /* write JP2H type */
1721 opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1722
1723 l_current_writer = l_writers;
1724 for (i = 0; i < l_nb_pass; ++i) {
1725 l_current_writer->m_data = l_current_writer->handler(jp2,
1726 &(l_current_writer->m_size));
1727 if (l_current_writer->m_data == 00) {
1728 opj_event_msg(p_manager, EVT_ERROR,
1729 "Not enough memory to hold JP2 Header data\n");
1730 l_result = OPJ_FALSE;
1731 break;
1732 }
1733
1734 l_jp2h_size += l_current_writer->m_size;
1735 ++l_current_writer;
1736 }
1737
1738 if (! l_result) {
1739 l_current_writer = l_writers;
1740 for (i = 0; i < l_nb_pass; ++i) {
1741 if (l_current_writer->m_data != 00) {
1742 opj_free(l_current_writer->m_data);
1743 }
1744 ++l_current_writer;
1745 }
1746
1747 return OPJ_FALSE;
1748 }
1749
1750 /* write super box size */
1751 opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1752
1753 /* write super box data on stream */
1754 if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1755 opj_event_msg(p_manager, EVT_ERROR,
1756 "Stream error while writing JP2 Header box\n");
1757 l_result = OPJ_FALSE;
1758 }
1759
1760 if (l_result) {
1761 l_current_writer = l_writers;
1762 for (i = 0; i < l_nb_pass; ++i) {
1763 if (opj_stream_write_data(stream, l_current_writer->m_data,
1764 l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1765 opj_event_msg(p_manager, EVT_ERROR,
1766 "Stream error while writing JP2 Header box\n");
1767 l_result = OPJ_FALSE;
1768 break;
1769 }
1770 ++l_current_writer;
1771 }
1772 }
1773
1774 l_current_writer = l_writers;
1775
1776 /* cleanup */
1777 for (i = 0; i < l_nb_pass; ++i) {
1778 if (l_current_writer->m_data != 00) {
1779 opj_free(l_current_writer->m_data);
1780 }
1781 ++l_current_writer;
1782 }
1783
1784 return l_result;
1785 }
1786
opj_jp2_write_ftyp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1787 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1788 opj_stream_private_t *cio,
1789 opj_event_mgr_t * p_manager)
1790 {
1791 OPJ_UINT32 i;
1792 OPJ_UINT32 l_ftyp_size;
1793 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1794 OPJ_BOOL l_result;
1795
1796 /* preconditions */
1797 assert(cio != 00);
1798 assert(jp2 != 00);
1799 assert(p_manager != 00);
1800 l_ftyp_size = 16 + 4 * jp2->numcl;
1801
1802 l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1803
1804 if (l_ftyp_data == 00) {
1805 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1806 return OPJ_FALSE;
1807 }
1808
1809 l_current_data_ptr = l_ftyp_data;
1810
1811 opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1812 l_current_data_ptr += 4;
1813
1814 opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1815 l_current_data_ptr += 4;
1816
1817 opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1818 l_current_data_ptr += 4;
1819
1820 opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1821 l_current_data_ptr += 4;
1822
1823 for (i = 0; i < jp2->numcl; i++) {
1824 opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1825 }
1826
1827 l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1828 p_manager) == l_ftyp_size);
1829 if (! l_result) {
1830 opj_event_msg(p_manager, EVT_ERROR,
1831 "Error while writing ftyp data to stream\n");
1832 }
1833
1834 opj_free(l_ftyp_data);
1835
1836 return l_result;
1837 }
1838
opj_jp2_write_jp2c(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1839 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1840 opj_stream_private_t *cio,
1841 opj_event_mgr_t * p_manager)
1842 {
1843 OPJ_OFF_T j2k_codestream_exit;
1844 OPJ_BYTE l_data_header [8];
1845
1846 /* preconditions */
1847 assert(jp2 != 00);
1848 assert(cio != 00);
1849 assert(p_manager != 00);
1850 assert(opj_stream_has_seek(cio));
1851
1852 j2k_codestream_exit = opj_stream_tell(cio);
1853 opj_write_bytes(l_data_header,
1854 (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1855 4); /* size of codestream */
1856 opj_write_bytes(l_data_header + 4, JP2_JP2C,
1857 4); /* JP2C */
1858
1859 if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1860 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1861 return OPJ_FALSE;
1862 }
1863
1864 if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1865 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1866 return OPJ_FALSE;
1867 }
1868
1869 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1870 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1871 return OPJ_FALSE;
1872 }
1873
1874 return OPJ_TRUE;
1875 }
1876
opj_jp2_write_jp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1877 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1878 opj_stream_private_t *cio,
1879 opj_event_mgr_t * p_manager)
1880 {
1881 /* 12 bytes will be read */
1882 OPJ_BYTE l_signature_data [12];
1883
1884 /* preconditions */
1885 assert(cio != 00);
1886 assert(jp2 != 00);
1887 assert(p_manager != 00);
1888
1889 OPJ_UNUSED(jp2);
1890
1891 /* write box length */
1892 opj_write_bytes(l_signature_data, 12, 4);
1893 /* writes box type */
1894 opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1895 /* writes magic number*/
1896 opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1897
1898 if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1899 return OPJ_FALSE;
1900 }
1901
1902 return OPJ_TRUE;
1903 }
1904
1905 /* ----------------------------------------------------------------------- */
1906 /* JP2 decoder interface */
1907 /* ----------------------------------------------------------------------- */
1908
opj_jp2_setup_decoder(void * p_jp2,opj_dparameters_t * parameters)1909 void opj_jp2_setup_decoder(void *p_jp2, opj_dparameters_t *parameters)
1910 {
1911 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
1912 /* setup the J2K codec */
1913 opj_j2k_setup_decoder(jp2->j2k, parameters);
1914
1915 /* further JP2 initializations go here */
1916 jp2->color.jp2_has_colr = 0;
1917 jp2->comps = NULL;
1918 jp2->ignore_pclr_cmap_cdef = parameters->flags &
1919 OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1920 }
1921
opj_jp2_decoder_set_strict_mode(void * p_jp2,OPJ_BOOL strict)1922 void opj_jp2_decoder_set_strict_mode(void *p_jp2, OPJ_BOOL strict)
1923 {
1924 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
1925 opj_j2k_decoder_set_strict_mode(jp2->j2k, strict);
1926 }
1927
opj_jp2_set_threads(void * p_jp2,OPJ_UINT32 num_threads)1928 OPJ_BOOL opj_jp2_set_threads(void *p_jp2, OPJ_UINT32 num_threads)
1929 {
1930 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
1931 return opj_j2k_set_threads(jp2->j2k, num_threads);
1932 }
1933
1934 /* ----------------------------------------------------------------------- */
1935 /* JP2 encoder interface */
1936 /* ----------------------------------------------------------------------- */
1937
opj_jp2_setup_encoder(void * p_jp2,opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)1938 OPJ_BOOL opj_jp2_setup_encoder(void *p_jp2,
1939 opj_cparameters_t *parameters,
1940 opj_image_t *image,
1941 opj_event_mgr_t * p_manager)
1942 {
1943 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
1944 OPJ_UINT32 i;
1945 OPJ_UINT32 depth_0;
1946 OPJ_UINT32 sign;
1947 OPJ_UINT32 alpha_count;
1948 OPJ_UINT32 color_channels = 0U;
1949 OPJ_UINT32 alpha_channel = 0U;
1950
1951
1952 if (!jp2 || !parameters || !image) {
1953 return OPJ_FALSE;
1954 }
1955
1956 /* setup the J2K codec */
1957 /* ------------------- */
1958
1959 /* Check if number of components respects standard */
1960 if (image->numcomps < 1 || image->numcomps > 16384) {
1961 opj_event_msg(p_manager, EVT_ERROR,
1962 "Invalid number of components specified while setting up JP2 encoder\n");
1963 return OPJ_FALSE;
1964 }
1965
1966 if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1967 p_manager) == OPJ_FALSE) {
1968 return OPJ_FALSE;
1969 }
1970
1971 /* setup the JP2 codec */
1972 /* ------------------- */
1973
1974 /* Profile box */
1975
1976 jp2->brand = JP2_JP2; /* BR */
1977 jp2->minversion = 0; /* MinV */
1978 jp2->numcl = 1;
1979 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1980 if (!jp2->cl) {
1981 opj_event_msg(p_manager, EVT_ERROR,
1982 "Not enough memory when setup the JP2 encoder\n");
1983 return OPJ_FALSE;
1984 }
1985 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1986
1987 /* Image Header box */
1988
1989 jp2->numcomps = image->numcomps; /* NC */
1990 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1991 opj_jp2_comps_t));
1992 if (!jp2->comps) {
1993 opj_event_msg(p_manager, EVT_ERROR,
1994 "Not enough memory when setup the JP2 encoder\n");
1995 /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1996 return OPJ_FALSE;
1997 }
1998
1999 jp2->h = image->y1 - image->y0; /* HEIGHT */
2000 jp2->w = image->x1 - image->x0; /* WIDTH */
2001 /* BPC */
2002 depth_0 = image->comps[0].prec - 1;
2003 sign = image->comps[0].sgnd;
2004 jp2->bpc = depth_0 + (sign << 7);
2005 for (i = 1; i < image->numcomps; i++) {
2006 OPJ_UINT32 depth = image->comps[i].prec - 1;
2007 sign = image->comps[i].sgnd;
2008 if (depth_0 != depth) {
2009 jp2->bpc = 255;
2010 }
2011 }
2012 jp2->C = 7; /* C : Always 7 */
2013 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
2014 jp2->IPR = 0; /* IPR, no intellectual property */
2015
2016 /* BitsPerComponent box */
2017 for (i = 0; i < image->numcomps; i++) {
2018 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2019 }
2020
2021 /* Colour Specification box */
2022 if (image->icc_profile_len) {
2023 jp2->meth = 2;
2024 jp2->enumcs = 0;
2025 } else {
2026 jp2->meth = 1;
2027 if (image->color_space == 1) {
2028 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
2029 } else if (image->color_space == 2) {
2030 jp2->enumcs = 17; /* greyscale */
2031 } else if (image->color_space == 3) {
2032 jp2->enumcs = 18; /* YUV */
2033 }
2034 }
2035
2036 /* Channel Definition box */
2037 /* FIXME not provided by parameters */
2038 /* We try to do what we can... */
2039 alpha_count = 0U;
2040 for (i = 0; i < image->numcomps; i++) {
2041 if (image->comps[i].alpha != 0) {
2042 alpha_count++;
2043 alpha_channel = i;
2044 }
2045 }
2046 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
2047 switch (jp2->enumcs) {
2048 case 16:
2049 case 18:
2050 color_channels = 3;
2051 break;
2052 case 17:
2053 color_channels = 1;
2054 break;
2055 default:
2056 alpha_count = 0U;
2057 break;
2058 }
2059 if (alpha_count == 0U) {
2060 opj_event_msg(p_manager, EVT_WARNING,
2061 "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
2062 } else if (image->numcomps < (color_channels + 1)) {
2063 opj_event_msg(p_manager, EVT_WARNING,
2064 "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
2065 alpha_count = 0U;
2066 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
2067 opj_event_msg(p_manager, EVT_WARNING,
2068 "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
2069 alpha_count = 0U;
2070 }
2071 } else if (alpha_count > 1) {
2072 opj_event_msg(p_manager, EVT_WARNING,
2073 "Multiple alpha channels specified. No cdef box will be created.\n");
2074 }
2075 if (alpha_count == 1U) { /* if here, we know what we can do */
2076 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2077 if (!jp2->color.jp2_cdef) {
2078 opj_event_msg(p_manager, EVT_ERROR,
2079 "Not enough memory to setup the JP2 encoder\n");
2080 return OPJ_FALSE;
2081 }
2082 /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2083 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2084 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2085 image->numcomps * sizeof(opj_jp2_cdef_info_t));
2086 if (!jp2->color.jp2_cdef->info) {
2087 /* memory will be freed by opj_jp2_destroy */
2088 opj_event_msg(p_manager, EVT_ERROR,
2089 "Not enough memory to setup the JP2 encoder\n");
2090 return OPJ_FALSE;
2091 }
2092 jp2->color.jp2_cdef->n = (OPJ_UINT16)
2093 image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2094 for (i = 0U; i < color_channels; i++) {
2095 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2096 i; /* cast is valid : image->numcomps [1,16384] */
2097 jp2->color.jp2_cdef->info[i].typ = 0U;
2098 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2099 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2100 }
2101 for (; i < image->numcomps; i++) {
2102 if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2103 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2104 i; /* cast is valid : image->numcomps [1,16384] */
2105 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2106 jp2->color.jp2_cdef->info[i].asoc =
2107 0U; /* Apply alpha channel to the whole image */
2108 } else {
2109 /* Unknown channel */
2110 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2111 i; /* cast is valid : image->numcomps [1,16384] */
2112 jp2->color.jp2_cdef->info[i].typ = 65535U;
2113 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2114 }
2115 }
2116 }
2117
2118 jp2->precedence = 0; /* PRECEDENCE */
2119 jp2->approx = 0; /* APPROX */
2120
2121 jp2->jpip_on = parameters->jpip_on;
2122
2123 return OPJ_TRUE;
2124 }
2125
opj_jp2_encode(void * p_jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2126 OPJ_BOOL opj_jp2_encode(void *p_jp2,
2127 opj_stream_private_t *stream,
2128 opj_event_mgr_t * p_manager)
2129 {
2130 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
2131 return opj_j2k_encode(jp2->j2k, stream, p_manager);
2132 }
2133
opj_jp2_end_decompress(void * p_jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2134 OPJ_BOOL opj_jp2_end_decompress(void *p_jp2,
2135 opj_stream_private_t *cio,
2136 opj_event_mgr_t * p_manager
2137 )
2138 {
2139 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
2140 /* preconditions */
2141 assert(jp2 != 00);
2142 assert(cio != 00);
2143 assert(p_manager != 00);
2144
2145 /* customization of the end encoding */
2146 if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2147 return OPJ_FALSE;
2148 }
2149
2150 /* write header */
2151 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2152 return OPJ_FALSE;
2153 }
2154
2155 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2156 }
2157
opj_jp2_end_compress(void * p_jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2158 OPJ_BOOL opj_jp2_end_compress(void *p_jp2,
2159 opj_stream_private_t *cio,
2160 opj_event_mgr_t * p_manager
2161 )
2162 {
2163 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
2164 /* preconditions */
2165 assert(jp2 != 00);
2166 assert(cio != 00);
2167 assert(p_manager != 00);
2168
2169 /* customization of the end encoding */
2170 if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2171 return OPJ_FALSE;
2172 }
2173
2174 if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2175 return OPJ_FALSE;
2176 }
2177
2178 /* write header */
2179 return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2180 }
2181
opj_jp2_setup_end_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2182 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2183 opj_event_mgr_t * p_manager)
2184 {
2185 /* preconditions */
2186 assert(jp2 != 00);
2187 assert(p_manager != 00);
2188
2189 #ifdef USE_JPIP
2190 if (jp2->jpip_on) {
2191 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2192 (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2193 return OPJ_FALSE;
2194 }
2195 }
2196 #endif
2197 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2198 (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2199 return OPJ_FALSE;
2200 }
2201 /* DEVELOPER CORNER, add your custom procedures */
2202 #ifdef USE_JPIP
2203 if (jp2->jpip_on) {
2204 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2205 (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2206 return OPJ_FALSE;
2207 }
2208 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2209 (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2210 return OPJ_FALSE;
2211 }
2212 }
2213 #endif
2214 return OPJ_TRUE;
2215 }
2216
opj_jp2_setup_end_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2217 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2218 opj_event_mgr_t * p_manager)
2219 {
2220 /* preconditions */
2221 assert(jp2 != 00);
2222 assert(p_manager != 00);
2223
2224 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2225 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2226 return OPJ_FALSE;
2227 }
2228 /* DEVELOPER CORNER, add your custom procedures */
2229
2230 return OPJ_TRUE;
2231 }
2232
opj_jp2_default_validation(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2233 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2234 opj_stream_private_t *cio,
2235 opj_event_mgr_t * p_manager
2236 )
2237 {
2238 OPJ_BOOL l_is_valid = OPJ_TRUE;
2239 OPJ_UINT32 i;
2240
2241 /* preconditions */
2242 assert(jp2 != 00);
2243 assert(cio != 00);
2244 assert(p_manager != 00);
2245
2246 OPJ_UNUSED(p_manager);
2247
2248 /* JPEG2000 codec validation */
2249
2250 /* STATE checking */
2251 /* make sure the state is at 0 */
2252 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2253
2254 /* make sure not reading a jp2h ???? WEIRD */
2255 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2256
2257 /* POINTER validation */
2258 /* make sure a j2k codec is present */
2259 l_is_valid &= (jp2->j2k != 00);
2260
2261 /* make sure a procedure list is present */
2262 l_is_valid &= (jp2->m_procedure_list != 00);
2263
2264 /* make sure a validation list is present */
2265 l_is_valid &= (jp2->m_validation_list != 00);
2266
2267 /* PARAMETER VALIDATION */
2268 /* number of components */
2269 l_is_valid &= (jp2->numcl > 0);
2270 /* width */
2271 l_is_valid &= (jp2->h > 0);
2272 /* height */
2273 l_is_valid &= (jp2->w > 0);
2274 /* precision */
2275 for (i = 0; i < jp2->numcomps; ++i) {
2276 l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2277 38U); /* 0 is valid, ignore sign for check */
2278 }
2279
2280 /* METH */
2281 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2282
2283 /* stream validation */
2284 /* back and forth is needed */
2285 l_is_valid &= opj_stream_has_seek(cio);
2286
2287 return l_is_valid;
2288 }
2289
opj_jp2_read_header_procedure(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2290 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2291 opj_stream_private_t *stream,
2292 opj_event_mgr_t * p_manager
2293 )
2294 {
2295 opj_jp2_box_t box;
2296 OPJ_UINT32 l_nb_bytes_read;
2297 const opj_jp2_header_handler_t * l_current_handler;
2298 const opj_jp2_header_handler_t * l_current_handler_misplaced;
2299 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2300 OPJ_UINT32 l_current_data_size;
2301 OPJ_BYTE * l_current_data = 00;
2302
2303 /* preconditions */
2304 assert(stream != 00);
2305 assert(jp2 != 00);
2306 assert(p_manager != 00);
2307
2308 l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2309
2310 if (l_current_data == 00) {
2311 opj_event_msg(p_manager, EVT_ERROR,
2312 "Not enough memory to handle jpeg2000 file header\n");
2313 return OPJ_FALSE;
2314 }
2315
2316 while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2317 /* is it the codestream box ? */
2318 if (box.type == JP2_JP2C) {
2319 if (jp2->jp2_state & JP2_STATE_HEADER) {
2320 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2321 opj_free(l_current_data);
2322 return OPJ_TRUE;
2323 } else {
2324 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2325 opj_free(l_current_data);
2326 return OPJ_FALSE;
2327 }
2328 } else if (box.length == 0) {
2329 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2330 opj_free(l_current_data);
2331 return OPJ_FALSE;
2332 }
2333 /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2334 else if (box.length < l_nb_bytes_read) {
2335 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2336 box.type);
2337 opj_free(l_current_data);
2338 return OPJ_FALSE;
2339 }
2340
2341 l_current_handler = opj_jp2_find_handler(box.type);
2342 l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2343 l_current_data_size = box.length - l_nb_bytes_read;
2344
2345 if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2346 if (l_current_handler == 00) {
2347 opj_event_msg(p_manager, EVT_WARNING,
2348 "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2349 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2350 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2351 if (jp2->jp2_state & JP2_STATE_HEADER) {
2352 /* read anyway, we already have jp2h */
2353 l_current_handler = l_current_handler_misplaced;
2354 } else {
2355 opj_event_msg(p_manager, EVT_WARNING,
2356 "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2357 (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2358 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2359 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2360 if (opj_stream_skip(stream, l_current_data_size,
2361 p_manager) != l_current_data_size) {
2362 opj_event_msg(p_manager, EVT_ERROR,
2363 "Problem with skipping JPEG2000 box, stream error\n");
2364 opj_free(l_current_data);
2365 return OPJ_FALSE;
2366 }
2367 continue;
2368 }
2369 }
2370 if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2371 /* do not even try to malloc if we can't read */
2372 opj_event_msg(p_manager, EVT_ERROR,
2373 "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2374 box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2375 (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2376 (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2377 opj_free(l_current_data);
2378 return OPJ_FALSE;
2379 }
2380 if (l_current_data_size > l_last_data_size) {
2381 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2382 l_current_data_size);
2383 if (!new_current_data) {
2384 opj_free(l_current_data);
2385 opj_event_msg(p_manager, EVT_ERROR,
2386 "Not enough memory to handle jpeg2000 box\n");
2387 return OPJ_FALSE;
2388 }
2389 l_current_data = new_current_data;
2390 l_last_data_size = l_current_data_size;
2391 }
2392
2393 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2394 l_current_data_size, p_manager);
2395 if (l_nb_bytes_read != l_current_data_size) {
2396 opj_event_msg(p_manager, EVT_ERROR,
2397 "Problem with reading JPEG2000 box, stream error\n");
2398 opj_free(l_current_data);
2399 return OPJ_FALSE;
2400 }
2401
2402 if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2403 p_manager)) {
2404 opj_free(l_current_data);
2405 return OPJ_FALSE;
2406 }
2407 } else {
2408 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2409 opj_event_msg(p_manager, EVT_ERROR,
2410 "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2411 opj_free(l_current_data);
2412 return OPJ_FALSE;
2413 }
2414 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2415 opj_event_msg(p_manager, EVT_ERROR,
2416 "Malformed JP2 file format: second box must be file type box\n");
2417 opj_free(l_current_data);
2418 return OPJ_FALSE;
2419 }
2420 jp2->jp2_state |= JP2_STATE_UNKNOWN;
2421 if (opj_stream_skip(stream, l_current_data_size,
2422 p_manager) != l_current_data_size) {
2423 if (jp2->jp2_state & JP2_STATE_CODESTREAM) {
2424 /* If we already read the codestream, do not error out */
2425 /* Needed for data/input/nonregression/issue254.jp2 */
2426 opj_event_msg(p_manager, EVT_WARNING,
2427 "Problem with skipping JPEG2000 box, stream error\n");
2428 opj_free(l_current_data);
2429 return OPJ_TRUE;
2430 } else {
2431 opj_event_msg(p_manager, EVT_ERROR,
2432 "Problem with skipping JPEG2000 box, stream error\n");
2433 opj_free(l_current_data);
2434 return OPJ_FALSE;
2435 }
2436 }
2437 }
2438 }
2439
2440 opj_free(l_current_data);
2441
2442 return OPJ_TRUE;
2443 }
2444
2445 /**
2446 * Executes the given procedures on the given codec.
2447 *
2448 * @param p_procedure_list the list of procedures to execute
2449 * @param jp2 the jpeg2000 file codec to execute the procedures on.
2450 * @param stream the stream to execute the procedures on.
2451 * @param p_manager the user manager.
2452 *
2453 * @return true if all the procedures were successfully executed.
2454 */
opj_jp2_exec(opj_jp2_t * jp2,opj_procedure_list_t * p_procedure_list,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2455 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2456 opj_procedure_list_t * p_procedure_list,
2457 opj_stream_private_t *stream,
2458 opj_event_mgr_t * p_manager
2459 )
2460
2461 {
2462 OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2463 opj_event_mgr_t *) = 00;
2464 OPJ_BOOL l_result = OPJ_TRUE;
2465 OPJ_UINT32 l_nb_proc, i;
2466
2467 /* preconditions */
2468 assert(p_procedure_list != 00);
2469 assert(jp2 != 00);
2470 assert(stream != 00);
2471 assert(p_manager != 00);
2472
2473 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2474 l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2475 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2476
2477 for (i = 0; i < l_nb_proc; ++i) {
2478 l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2479 ++l_procedure;
2480 }
2481
2482 /* and clear the procedure list at the end. */
2483 opj_procedure_list_clear(p_procedure_list);
2484 return l_result;
2485 }
2486
opj_jp2_start_compress(void * p_jp2,opj_stream_private_t * stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)2487 OPJ_BOOL opj_jp2_start_compress(void *p_jp2,
2488 opj_stream_private_t *stream,
2489 opj_image_t * p_image,
2490 opj_event_mgr_t * p_manager
2491 )
2492 {
2493 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
2494 /* preconditions */
2495 assert(jp2 != 00);
2496 assert(stream != 00);
2497 assert(p_manager != 00);
2498
2499 /* customization of the validation */
2500 if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2501 return OPJ_FALSE;
2502 }
2503
2504 /* validation of the parameters codec */
2505 if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2506 return OPJ_FALSE;
2507 }
2508
2509 /* customization of the encoding */
2510 if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2511 return OPJ_FALSE;
2512 }
2513
2514 /* write header */
2515 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2516 return OPJ_FALSE;
2517 }
2518
2519 return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2520 }
2521
opj_jp2_find_handler(OPJ_UINT32 p_id)2522 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2523 {
2524 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2525 opj_jp2_header_handler_t);
2526
2527 for (i = 0; i < l_handler_size; ++i) {
2528 if (jp2_header[i].id == p_id) {
2529 return &jp2_header[i];
2530 }
2531 }
2532 return NULL;
2533 }
2534
2535 /**
2536 * Finds the image execution function related to the given box id.
2537 *
2538 * @param p_id the id of the handler to fetch.
2539 *
2540 * @return the given handler or 00 if it could not be found.
2541 */
opj_jp2_img_find_handler(OPJ_UINT32 p_id)2542 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2543 OPJ_UINT32 p_id)
2544 {
2545 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2546 opj_jp2_header_handler_t);
2547 for (i = 0; i < l_handler_size; ++i) {
2548 if (jp2_img_header[i].id == p_id) {
2549 return &jp2_img_header[i];
2550 }
2551 }
2552
2553 return NULL;
2554 }
2555
2556 /**
2557 * Reads a jpeg2000 file signature box.
2558 *
2559 * @param p_header_data the data contained in the signature box.
2560 * @param jp2 the jpeg2000 file codec.
2561 * @param p_header_size the size of the data contained in the signature box.
2562 * @param p_manager the user event manager.
2563 *
2564 * @return true if the file signature box is valid.
2565 */
opj_jp2_read_jp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2566 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2567 OPJ_BYTE * p_header_data,
2568 OPJ_UINT32 p_header_size,
2569 opj_event_mgr_t * p_manager
2570 )
2571
2572 {
2573 OPJ_UINT32 l_magic_number;
2574
2575 /* preconditions */
2576 assert(p_header_data != 00);
2577 assert(jp2 != 00);
2578 assert(p_manager != 00);
2579
2580 if (jp2->jp2_state != JP2_STATE_NONE) {
2581 opj_event_msg(p_manager, EVT_ERROR,
2582 "The signature box must be the first box in the file.\n");
2583 return OPJ_FALSE;
2584 }
2585
2586 /* assure length of data is correct (4 -> magic number) */
2587 if (p_header_size != 4) {
2588 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2589 return OPJ_FALSE;
2590 }
2591
2592 /* rearrange data */
2593 opj_read_bytes(p_header_data, &l_magic_number, 4);
2594 if (l_magic_number != 0x0d0a870a) {
2595 opj_event_msg(p_manager, EVT_ERROR,
2596 "Error with JP Signature : bad magic number\n");
2597 return OPJ_FALSE;
2598 }
2599
2600 jp2->jp2_state |= JP2_STATE_SIGNATURE;
2601
2602 return OPJ_TRUE;
2603 }
2604
2605 /**
2606 * Reads a a FTYP box - File type box
2607 *
2608 * @param p_header_data the data contained in the FTYP box.
2609 * @param jp2 the jpeg2000 file codec.
2610 * @param p_header_size the size of the data contained in the FTYP box.
2611 * @param p_manager the user event manager.
2612 *
2613 * @return true if the FTYP box is valid.
2614 */
opj_jp2_read_ftyp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2615 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2616 OPJ_BYTE * p_header_data,
2617 OPJ_UINT32 p_header_size,
2618 opj_event_mgr_t * p_manager
2619 )
2620 {
2621 OPJ_UINT32 i, l_remaining_bytes;
2622
2623 /* preconditions */
2624 assert(p_header_data != 00);
2625 assert(jp2 != 00);
2626 assert(p_manager != 00);
2627
2628 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2629 opj_event_msg(p_manager, EVT_ERROR,
2630 "The ftyp box must be the second box in the file.\n");
2631 return OPJ_FALSE;
2632 }
2633
2634 /* assure length of data is correct */
2635 if (p_header_size < 8) {
2636 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2637 return OPJ_FALSE;
2638 }
2639
2640 opj_read_bytes(p_header_data, &jp2->brand, 4); /* BR */
2641 p_header_data += 4;
2642
2643 opj_read_bytes(p_header_data, &jp2->minversion, 4); /* MinV */
2644 p_header_data += 4;
2645
2646 l_remaining_bytes = p_header_size - 8;
2647
2648 /* the number of remaining bytes should be a multiple of 4 */
2649 if ((l_remaining_bytes & 0x3) != 0) {
2650 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2651 return OPJ_FALSE;
2652 }
2653
2654 /* div by 4 */
2655 jp2->numcl = l_remaining_bytes >> 2;
2656 if (jp2->numcl) {
2657 jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2658 if (jp2->cl == 00) {
2659 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2660 return OPJ_FALSE;
2661 }
2662 }
2663
2664 for (i = 0; i < jp2->numcl; ++i) {
2665 opj_read_bytes(p_header_data, &jp2->cl[i], 4); /* CLi */
2666 p_header_data += 4;
2667 }
2668
2669 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2670
2671 return OPJ_TRUE;
2672 }
2673
opj_jp2_skip_jp2c(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2674 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2675 opj_stream_private_t *stream,
2676 opj_event_mgr_t * p_manager)
2677 {
2678 /* preconditions */
2679 assert(jp2 != 00);
2680 assert(stream != 00);
2681 assert(p_manager != 00);
2682
2683 jp2->j2k_codestream_offset = opj_stream_tell(stream);
2684
2685 if (opj_stream_skip(stream, 8, p_manager) != 8) {
2686 return OPJ_FALSE;
2687 }
2688
2689 return OPJ_TRUE;
2690 }
2691
opj_jpip_skip_iptr(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2692 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2693 opj_stream_private_t *stream,
2694 opj_event_mgr_t * p_manager)
2695 {
2696 /* preconditions */
2697 assert(jp2 != 00);
2698 assert(stream != 00);
2699 assert(p_manager != 00);
2700
2701 jp2->jpip_iptr_offset = opj_stream_tell(stream);
2702
2703 if (opj_stream_skip(stream, 24, p_manager) != 24) {
2704 return OPJ_FALSE;
2705 }
2706
2707 return OPJ_TRUE;
2708 }
2709
2710 /**
2711 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2712 *
2713 * @param p_header_data the data contained in the file header box.
2714 * @param jp2 the jpeg2000 file codec.
2715 * @param p_header_size the size of the data contained in the file header box.
2716 * @param p_manager the user event manager.
2717 *
2718 * @return true if the JP2 Header box was successfully recognized.
2719 */
opj_jp2_read_jp2h(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2720 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2721 OPJ_BYTE *p_header_data,
2722 OPJ_UINT32 p_header_size,
2723 opj_event_mgr_t * p_manager
2724 )
2725 {
2726 OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2727 opj_jp2_box_t box;
2728 const opj_jp2_header_handler_t * l_current_handler;
2729 OPJ_BOOL l_has_ihdr = 0;
2730
2731 /* preconditions */
2732 assert(p_header_data != 00);
2733 assert(jp2 != 00);
2734 assert(p_manager != 00);
2735
2736 /* make sure the box is well placed */
2737 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2738 opj_event_msg(p_manager, EVT_ERROR,
2739 "The box must be the first box in the file.\n");
2740 return OPJ_FALSE;
2741 }
2742
2743 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2744
2745 /* iterate while remaining data */
2746 while (p_header_size > 0) {
2747
2748 if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2749 p_manager)) {
2750 opj_event_msg(p_manager, EVT_ERROR,
2751 "Stream error while reading JP2 Header box\n");
2752 return OPJ_FALSE;
2753 }
2754
2755 if (box.length > p_header_size) {
2756 opj_event_msg(p_manager, EVT_ERROR,
2757 "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2758 return OPJ_FALSE;
2759 }
2760
2761 l_current_handler = opj_jp2_img_find_handler(box.type);
2762 l_current_data_size = box.length - l_box_size;
2763 p_header_data += l_box_size;
2764
2765 if (l_current_handler != 00) {
2766 if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2767 p_manager)) {
2768 return OPJ_FALSE;
2769 }
2770 } else {
2771 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2772 }
2773
2774 if (box.type == JP2_IHDR) {
2775 l_has_ihdr = 1;
2776 }
2777
2778 p_header_data += l_current_data_size;
2779 p_header_size -= box.length;
2780 }
2781
2782 if (l_has_ihdr == 0) {
2783 opj_event_msg(p_manager, EVT_ERROR,
2784 "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2785 return OPJ_FALSE;
2786 }
2787
2788 jp2->jp2_state |= JP2_STATE_HEADER;
2789 jp2->has_jp2h = 1;
2790
2791 return OPJ_TRUE;
2792 }
2793
opj_jp2_read_boxhdr_char(opj_jp2_box_t * box,OPJ_BYTE * p_data,OPJ_UINT32 * p_number_bytes_read,OPJ_UINT32 p_box_max_size,opj_event_mgr_t * p_manager)2794 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2795 OPJ_BYTE * p_data,
2796 OPJ_UINT32 * p_number_bytes_read,
2797 OPJ_UINT32 p_box_max_size,
2798 opj_event_mgr_t * p_manager
2799 )
2800 {
2801 OPJ_UINT32 l_value;
2802
2803 /* preconditions */
2804 assert(p_data != 00);
2805 assert(box != 00);
2806 assert(p_number_bytes_read != 00);
2807 assert(p_manager != 00);
2808
2809 if (p_box_max_size < 8) {
2810 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2811 return OPJ_FALSE;
2812 }
2813
2814 /* process read data */
2815 opj_read_bytes(p_data, &l_value, 4);
2816 p_data += 4;
2817 box->length = (OPJ_UINT32)(l_value);
2818
2819 opj_read_bytes(p_data, &l_value, 4);
2820 p_data += 4;
2821 box->type = (OPJ_UINT32)(l_value);
2822
2823 *p_number_bytes_read = 8;
2824
2825 /* do we have a "special very large box ?" */
2826 /* read then the XLBox */
2827 if (box->length == 1) {
2828 OPJ_UINT32 l_xl_part_size;
2829
2830 if (p_box_max_size < 16) {
2831 opj_event_msg(p_manager, EVT_ERROR,
2832 "Cannot handle XL box of less than 16 bytes\n");
2833 return OPJ_FALSE;
2834 }
2835
2836 opj_read_bytes(p_data, &l_xl_part_size, 4);
2837 p_data += 4;
2838 *p_number_bytes_read += 4;
2839
2840 if (l_xl_part_size != 0) {
2841 opj_event_msg(p_manager, EVT_ERROR,
2842 "Cannot handle box sizes higher than 2^32\n");
2843 return OPJ_FALSE;
2844 }
2845
2846 opj_read_bytes(p_data, &l_value, 4);
2847 *p_number_bytes_read += 4;
2848 box->length = (OPJ_UINT32)(l_value);
2849
2850 if (box->length == 0) {
2851 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2852 return OPJ_FALSE;
2853 }
2854 } else if (box->length == 0) {
2855 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2856 return OPJ_FALSE;
2857 }
2858 if (box->length < *p_number_bytes_read) {
2859 opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2860 return OPJ_FALSE;
2861 }
2862 return OPJ_TRUE;
2863 }
2864
opj_jp2_read_header(opj_stream_private_t * p_stream,void * p_jp2,opj_image_t ** p_image,opj_event_mgr_t * p_manager)2865 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2866 void *p_jp2,
2867 opj_image_t ** p_image,
2868 opj_event_mgr_t * p_manager
2869 )
2870 {
2871 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
2872 /* preconditions */
2873 assert(jp2 != 00);
2874 assert(p_stream != 00);
2875 assert(p_manager != 00);
2876
2877 /* customization of the validation */
2878 if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2879 return OPJ_FALSE;
2880 }
2881
2882 /* customization of the encoding */
2883 if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2884 return OPJ_FALSE;
2885 }
2886
2887 /* validation of the parameters codec */
2888 if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2889 return OPJ_FALSE;
2890 }
2891
2892 /* read header */
2893 if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2894 return OPJ_FALSE;
2895 }
2896 if (jp2->has_jp2h == 0) {
2897 opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
2898 return OPJ_FALSE;
2899 }
2900 if (jp2->has_ihdr == 0) {
2901 opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
2902 return OPJ_FALSE;
2903 }
2904
2905 return opj_j2k_read_header(p_stream,
2906 jp2->j2k,
2907 p_image,
2908 p_manager);
2909 }
2910
opj_jp2_setup_encoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2911 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2912 opj_event_mgr_t * p_manager)
2913 {
2914 /* preconditions */
2915 assert(jp2 != 00);
2916 assert(p_manager != 00);
2917
2918 if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2919 (opj_procedure)opj_jp2_default_validation, p_manager)) {
2920 return OPJ_FALSE;
2921 }
2922 /* DEVELOPER CORNER, add your custom validation procedure */
2923
2924 return OPJ_TRUE;
2925 }
2926
opj_jp2_setup_decoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2927 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2928 opj_event_mgr_t * p_manager)
2929 {
2930 /* preconditions */
2931 assert(jp2 != 00);
2932 assert(p_manager != 00);
2933
2934 OPJ_UNUSED(jp2);
2935 OPJ_UNUSED(p_manager);
2936
2937 /* DEVELOPER CORNER, add your custom validation procedure */
2938
2939 return OPJ_TRUE;
2940 }
2941
opj_jp2_setup_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2942 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2943 opj_event_mgr_t * p_manager)
2944 {
2945 /* preconditions */
2946 assert(jp2 != 00);
2947 assert(p_manager != 00);
2948
2949 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2950 (opj_procedure)opj_jp2_write_jp, p_manager)) {
2951 return OPJ_FALSE;
2952 }
2953 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2954 (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2955 return OPJ_FALSE;
2956 }
2957 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2958 (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2959 return OPJ_FALSE;
2960 }
2961 if (jp2->jpip_on) {
2962 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2963 (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2964 return OPJ_FALSE;
2965 }
2966 }
2967 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2968 (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2969 return OPJ_FALSE;
2970 }
2971
2972 /* DEVELOPER CORNER, insert your custom procedures */
2973
2974 return OPJ_TRUE;
2975 }
2976
opj_jp2_setup_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2977 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2978 opj_event_mgr_t * p_manager)
2979 {
2980 /* preconditions */
2981 assert(jp2 != 00);
2982 assert(p_manager != 00);
2983
2984 if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2985 (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2986 return OPJ_FALSE;
2987 }
2988
2989 /* DEVELOPER CORNER, add your custom procedures */
2990
2991 return OPJ_TRUE;
2992 }
2993
opj_jp2_read_tile_header(void * p_jp2,OPJ_UINT32 * p_tile_index,OPJ_UINT32 * p_data_size,OPJ_INT32 * p_tile_x0,OPJ_INT32 * p_tile_y0,OPJ_INT32 * p_tile_x1,OPJ_INT32 * p_tile_y1,OPJ_UINT32 * p_nb_comps,OPJ_BOOL * p_go_on,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2994 OPJ_BOOL opj_jp2_read_tile_header(void *p_jp2,
2995 OPJ_UINT32 * p_tile_index,
2996 OPJ_UINT32 * p_data_size,
2997 OPJ_INT32 * p_tile_x0,
2998 OPJ_INT32 * p_tile_y0,
2999 OPJ_INT32 * p_tile_x1,
3000 OPJ_INT32 * p_tile_y1,
3001 OPJ_UINT32 * p_nb_comps,
3002 OPJ_BOOL * p_go_on,
3003 opj_stream_private_t *p_stream,
3004 opj_event_mgr_t * p_manager
3005 )
3006 {
3007 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3008 return opj_j2k_read_tile_header(jp2->j2k,
3009 p_tile_index,
3010 p_data_size,
3011 p_tile_x0, p_tile_y0,
3012 p_tile_x1, p_tile_y1,
3013 p_nb_comps,
3014 p_go_on,
3015 p_stream,
3016 p_manager);
3017 }
3018
opj_jp2_write_tile(void * p_jp2,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3019 OPJ_BOOL opj_jp2_write_tile(void *p_jp2,
3020 OPJ_UINT32 p_tile_index,
3021 OPJ_BYTE * p_data,
3022 OPJ_UINT32 p_data_size,
3023 opj_stream_private_t *p_stream,
3024 opj_event_mgr_t * p_manager
3025 )
3026
3027 {
3028 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3029 return opj_j2k_write_tile(jp2->j2k, p_tile_index, p_data, p_data_size,
3030 p_stream, p_manager);
3031 }
3032
opj_jp2_decode_tile(void * p_jp2,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3033 OPJ_BOOL opj_jp2_decode_tile(void *p_jp2,
3034 OPJ_UINT32 p_tile_index,
3035 OPJ_BYTE * p_data,
3036 OPJ_UINT32 p_data_size,
3037 opj_stream_private_t *p_stream,
3038 opj_event_mgr_t * p_manager
3039 )
3040 {
3041 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3042 return opj_j2k_decode_tile(jp2->j2k, p_tile_index, p_data, p_data_size,
3043 p_stream, p_manager);
3044 }
3045
opj_jp2_destroy(void * p_jp2)3046 void opj_jp2_destroy(void *p_jp2)
3047 {
3048 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3049 if (jp2) {
3050 /* destroy the J2K codec */
3051 opj_j2k_destroy(jp2->j2k);
3052 jp2->j2k = 00;
3053
3054 if (jp2->comps) {
3055 opj_free(jp2->comps);
3056 jp2->comps = 00;
3057 }
3058
3059 if (jp2->cl) {
3060 opj_free(jp2->cl);
3061 jp2->cl = 00;
3062 }
3063
3064 if (jp2->color.icc_profile_buf) {
3065 opj_free(jp2->color.icc_profile_buf);
3066 jp2->color.icc_profile_buf = 00;
3067 }
3068
3069 if (jp2->color.jp2_cdef) {
3070 if (jp2->color.jp2_cdef->info) {
3071 opj_free(jp2->color.jp2_cdef->info);
3072 jp2->color.jp2_cdef->info = NULL;
3073 }
3074
3075 opj_free(jp2->color.jp2_cdef);
3076 jp2->color.jp2_cdef = 00;
3077 }
3078
3079 if (jp2->color.jp2_pclr) {
3080 if (jp2->color.jp2_pclr->cmap) {
3081 opj_free(jp2->color.jp2_pclr->cmap);
3082 jp2->color.jp2_pclr->cmap = NULL;
3083 }
3084 if (jp2->color.jp2_pclr->channel_sign) {
3085 opj_free(jp2->color.jp2_pclr->channel_sign);
3086 jp2->color.jp2_pclr->channel_sign = NULL;
3087 }
3088 if (jp2->color.jp2_pclr->channel_size) {
3089 opj_free(jp2->color.jp2_pclr->channel_size);
3090 jp2->color.jp2_pclr->channel_size = NULL;
3091 }
3092 if (jp2->color.jp2_pclr->entries) {
3093 opj_free(jp2->color.jp2_pclr->entries);
3094 jp2->color.jp2_pclr->entries = NULL;
3095 }
3096
3097 opj_free(jp2->color.jp2_pclr);
3098 jp2->color.jp2_pclr = 00;
3099 }
3100
3101 if (jp2->m_validation_list) {
3102 opj_procedure_list_destroy(jp2->m_validation_list);
3103 jp2->m_validation_list = 00;
3104 }
3105
3106 if (jp2->m_procedure_list) {
3107 opj_procedure_list_destroy(jp2->m_procedure_list);
3108 jp2->m_procedure_list = 00;
3109 }
3110
3111 opj_free(jp2);
3112 }
3113 }
3114
opj_jp2_set_decoded_components(void * p_jp2,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,opj_event_mgr_t * p_manager)3115 OPJ_BOOL opj_jp2_set_decoded_components(void *p_jp2,
3116 OPJ_UINT32 numcomps,
3117 const OPJ_UINT32* comps_indices,
3118 opj_event_mgr_t * p_manager)
3119 {
3120 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3121 return opj_j2k_set_decoded_components(jp2->j2k,
3122 numcomps, comps_indices,
3123 p_manager);
3124 }
3125
opj_jp2_set_decode_area(void * p_jp2,opj_image_t * p_image,OPJ_INT32 p_start_x,OPJ_INT32 p_start_y,OPJ_INT32 p_end_x,OPJ_INT32 p_end_y,opj_event_mgr_t * p_manager)3126 OPJ_BOOL opj_jp2_set_decode_area(void *p_jp2,
3127 opj_image_t* p_image,
3128 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3129 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3130 opj_event_mgr_t * p_manager
3131 )
3132 {
3133 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3134 return opj_j2k_set_decode_area(jp2->j2k, p_image, p_start_x, p_start_y,
3135 p_end_x, p_end_y, p_manager);
3136 }
3137
opj_jp2_get_tile(void * jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager,OPJ_UINT32 tile_index)3138 OPJ_BOOL opj_jp2_get_tile(void *jp2,
3139 opj_stream_private_t *p_stream,
3140 opj_image_t* p_image,
3141 opj_event_mgr_t * p_manager,
3142 OPJ_UINT32 tile_index
3143 )
3144 {
3145 opj_jp2_t *p_jp2 = (opj_jp2_t*)jp2;
3146 if (!p_image) {
3147 return OPJ_FALSE;
3148 }
3149
3150 opj_event_msg(p_manager, EVT_WARNING,
3151 "JP2 box which are after the codestream will not be read by this function.\n");
3152
3153 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3154 opj_event_msg(p_manager, EVT_ERROR,
3155 "Failed to decode the codestream in the JP2 file\n");
3156 return OPJ_FALSE;
3157 }
3158
3159 if (p_jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
3160 /* Bypass all JP2 component transforms */
3161 return OPJ_TRUE;
3162 }
3163
3164 if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
3165 return OPJ_FALSE;
3166 }
3167
3168 /* Set Image Color Space */
3169 if (p_jp2->enumcs == 16) {
3170 p_image->color_space = OPJ_CLRSPC_SRGB;
3171 } else if (p_jp2->enumcs == 17) {
3172 p_image->color_space = OPJ_CLRSPC_GRAY;
3173 } else if (p_jp2->enumcs == 18) {
3174 p_image->color_space = OPJ_CLRSPC_SYCC;
3175 } else if (p_jp2->enumcs == 24) {
3176 p_image->color_space = OPJ_CLRSPC_EYCC;
3177 } else if (p_jp2->enumcs == 12) {
3178 p_image->color_space = OPJ_CLRSPC_CMYK;
3179 } else {
3180 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
3181 }
3182
3183 if (p_jp2->color.jp2_pclr) {
3184 /* Part 1, I.5.3.4: Either both or none : */
3185 if (!p_jp2->color.jp2_pclr->cmap) {
3186 opj_jp2_free_pclr(&(p_jp2->color));
3187 } else {
3188 if (!opj_jp2_apply_pclr(p_image, &(p_jp2->color), p_manager)) {
3189 return OPJ_FALSE;
3190 }
3191 }
3192 }
3193
3194 /* Apply the color space if needed */
3195 if (p_jp2->color.jp2_cdef) {
3196 opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
3197 }
3198
3199 if (p_jp2->color.icc_profile_buf) {
3200 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
3201 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
3202 p_jp2->color.icc_profile_buf = NULL;
3203 }
3204
3205 return OPJ_TRUE;
3206 }
3207
3208 /* ----------------------------------------------------------------------- */
3209 /* JP2 encoder interface */
3210 /* ----------------------------------------------------------------------- */
3211
opj_jp2_create(OPJ_BOOL p_is_decoder)3212 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3213 {
3214 opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3215 if (jp2) {
3216
3217 /* create the J2K codec */
3218 if (! p_is_decoder) {
3219 jp2->j2k = opj_j2k_create_compress();
3220 } else {
3221 jp2->j2k = opj_j2k_create_decompress();
3222 }
3223
3224 if (jp2->j2k == 00) {
3225 opj_jp2_destroy(jp2);
3226 return 00;
3227 }
3228
3229 /* Color structure */
3230 jp2->color.icc_profile_buf = NULL;
3231 jp2->color.icc_profile_len = 0;
3232 jp2->color.jp2_cdef = NULL;
3233 jp2->color.jp2_pclr = NULL;
3234 jp2->color.jp2_has_colr = 0;
3235
3236 /* validation list creation */
3237 jp2->m_validation_list = opj_procedure_list_create();
3238 if (! jp2->m_validation_list) {
3239 opj_jp2_destroy(jp2);
3240 return 00;
3241 }
3242
3243 /* execution list creation */
3244 jp2->m_procedure_list = opj_procedure_list_create();
3245 if (! jp2->m_procedure_list) {
3246 opj_jp2_destroy(jp2);
3247 return 00;
3248 }
3249 }
3250
3251 return jp2;
3252 }
3253
jp2_dump(void * p_jp2,OPJ_INT32 flag,FILE * out_stream)3254 void jp2_dump(void* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3255 {
3256 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3257 /* preconditions */
3258 assert(p_jp2 != 00);
3259
3260 j2k_dump(jp2->j2k,
3261 flag,
3262 out_stream);
3263 }
3264
jp2_get_cstr_index(void * p_jp2)3265 opj_codestream_index_t* jp2_get_cstr_index(void* p_jp2)
3266 {
3267 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3268 return j2k_get_cstr_index(jp2->j2k);
3269 }
3270
jp2_get_cstr_info(void * p_jp2)3271 opj_codestream_info_v2_t* jp2_get_cstr_info(void* p_jp2)
3272 {
3273 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3274 return j2k_get_cstr_info(jp2->j2k);
3275 }
3276
opj_jp2_set_decoded_resolution_factor(void * p_jp2,OPJ_UINT32 res_factor,opj_event_mgr_t * p_manager)3277 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(void *p_jp2,
3278 OPJ_UINT32 res_factor,
3279 opj_event_mgr_t * p_manager)
3280 {
3281 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3282 return opj_j2k_set_decoded_resolution_factor(jp2->j2k, res_factor, p_manager);
3283 }
3284
3285 /* ----------------------------------------------------------------------- */
3286
opj_jp2_encoder_set_extra_options(void * p_jp2,const char * const * p_options,opj_event_mgr_t * p_manager)3287 OPJ_BOOL opj_jp2_encoder_set_extra_options(
3288 void *p_jp2,
3289 const char* const* p_options,
3290 opj_event_mgr_t * p_manager)
3291 {
3292 opj_jp2_t *jp2 = (opj_jp2_t*)p_jp2;
3293 return opj_j2k_encoder_set_extra_options(jp2->j2k, p_options, p_manager);
3294 }
3295
3296 /* ----------------------------------------------------------------------- */
3297
3298 /* JPIP specific */
3299
3300 #ifdef USE_JPIP
opj_jpip_write_iptr(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3301 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3302 opj_stream_private_t *cio,
3303 opj_event_mgr_t * p_manager)
3304 {
3305 OPJ_OFF_T j2k_codestream_exit;
3306 OPJ_BYTE l_data_header [24];
3307
3308 /* preconditions */
3309 assert(jp2 != 00);
3310 assert(cio != 00);
3311 assert(p_manager != 00);
3312 assert(opj_stream_has_seek(cio));
3313
3314 j2k_codestream_exit = opj_stream_tell(cio);
3315 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3316 opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3317 4); /* IPTR */
3318 #if 0
3319 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3320 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3321 #else
3322 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3323 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3324 #endif
3325
3326 if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3327 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3328 return OPJ_FALSE;
3329 }
3330
3331 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3332 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3333 return OPJ_FALSE;
3334 }
3335
3336 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3337 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3338 return OPJ_FALSE;
3339 }
3340
3341 return OPJ_TRUE;
3342 }
3343
opj_jpip_write_fidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3344 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3345 opj_stream_private_t *cio,
3346 opj_event_mgr_t * p_manager)
3347 {
3348 OPJ_OFF_T j2k_codestream_exit;
3349 OPJ_BYTE l_data_header [24];
3350
3351 OPJ_UNUSED(jp2);
3352
3353 /* preconditions */
3354 assert(jp2 != 00);
3355 assert(cio != 00);
3356 assert(p_manager != 00);
3357 assert(opj_stream_has_seek(cio));
3358
3359 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3360 opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3361 4); /* IPTR */
3362 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3363 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3364
3365 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3366 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3367 return OPJ_FALSE;
3368 }
3369
3370 j2k_codestream_exit = opj_stream_tell(cio);
3371 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3372 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3373 return OPJ_FALSE;
3374 }
3375
3376 return OPJ_TRUE;
3377 }
3378
opj_jpip_write_cidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3379 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3380 opj_stream_private_t *cio,
3381 opj_event_mgr_t * p_manager)
3382 {
3383 OPJ_OFF_T j2k_codestream_exit;
3384 OPJ_BYTE l_data_header [24];
3385
3386 OPJ_UNUSED(jp2);
3387
3388 /* preconditions */
3389 assert(jp2 != 00);
3390 assert(cio != 00);
3391 assert(p_manager != 00);
3392 assert(opj_stream_has_seek(cio));
3393
3394 j2k_codestream_exit = opj_stream_tell(cio);
3395 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3396 opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3397 4); /* IPTR */
3398 #if 0
3399 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3400 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3401 #else
3402 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3403 opj_write_double(l_data_header + 8 + 8, 0); /* length */
3404 #endif
3405
3406 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3407 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3408 return OPJ_FALSE;
3409 }
3410
3411 if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3412 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3413 return OPJ_FALSE;
3414 }
3415
3416 j2k_codestream_exit = opj_stream_tell(cio);
3417 if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3418 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3419 return OPJ_FALSE;
3420 }
3421
3422 return OPJ_TRUE;
3423 }
3424
3425 #if 0
3426 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3427 int length_idx, opj_stream_private_t *cio,
3428 opj_event_mgr_t * p_manager)
3429 {
3430 OPJ_BYTE l_data_header [8];
3431 OPJ_OFF_T len, lenp;
3432
3433 lenp = opj_stream_tell(cio);
3434 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
3435 opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR */
3436 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3437
3438 opj_write_bytes(l_data_header, offset_jp2c, 8); /* OOFF */
3439 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3440 opj_write_bytes(l_data_header, length_jp2c, 4); /* OBH part 1 */
3441 opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2 */
3442 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3443
3444 opj_write_bytes(l_data_header, 1, 1); /* NI */
3445 opj_stream_write_data(cio, l_data_header, 1, p_manager);
3446
3447 opj_write_bytes(l_data_header, offset_idx, 8); /* IOFF */
3448 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3449 opj_write_bytes(l_data_header, length_idx, 4); /* IBH part 1 */
3450 opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4); /* IBH part 2 */
3451 opj_stream_write_data(cio, l_data_header, 8, p_manager);
3452
3453 len = opj_stream_tell(cio) - lenp;
3454 opj_stream_skip(cio, lenp, p_manager);
3455 opj_write_bytes(l_data_header, len, 4); /* L */
3456 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3457 opj_stream_seek(cio, lenp + len, p_manager);
3458 }
3459 #endif
3460
3461
3462 #if 0
3463 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3464 int length_idx, opj_stream_private_t *cio,
3465 opj_event_mgr_t * p_manager)
3466 {
3467 OPJ_BYTE l_data_header [4];
3468 OPJ_OFF_T len, lenp;
3469
3470 lenp = opj_stream_tell(cio);
3471 opj_stream_skip(cio, 4, p_manager);
3472 opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3473 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3474
3475 write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3476
3477 len = opj_stream_tell(cio) - lenp;
3478 opj_stream_skip(cio, lenp, p_manager);
3479 opj_write_bytes(l_data_header, len, 4); /* L */
3480 opj_stream_write_data(cio, l_data_header, 4, p_manager);
3481 opj_stream_seek(cio, lenp + len, p_manager);
3482
3483 return len;
3484 }
3485 #endif
3486 #endif /* USE_JPIP */
3487