• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(opj_jp2_t * jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)1612 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
1613                         opj_stream_private_t *p_stream,
1614                         opj_image_t* p_image,
1615                         opj_event_mgr_t * p_manager)
1616 {
1617     if (!p_image) {
1618         return OPJ_FALSE;
1619     }
1620 
1621     /* J2K decoding */
1622     if (! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager)) {
1623         opj_event_msg(p_manager, EVT_ERROR,
1624                       "Failed to decode the codestream in the JP2 file\n");
1625         return OPJ_FALSE;
1626     }
1627 
1628     if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
1629         /* Bypass all JP2 component transforms */
1630         return OPJ_TRUE;
1631     }
1632 
1633     if (!jp2->ignore_pclr_cmap_cdef) {
1634         if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
1635             return OPJ_FALSE;
1636         }
1637 
1638         /* Set Image Color Space */
1639         if (jp2->enumcs == 16) {
1640             p_image->color_space = OPJ_CLRSPC_SRGB;
1641         } else if (jp2->enumcs == 17) {
1642             p_image->color_space = OPJ_CLRSPC_GRAY;
1643         } else if (jp2->enumcs == 18) {
1644             p_image->color_space = OPJ_CLRSPC_SYCC;
1645         } else if (jp2->enumcs == 24) {
1646             p_image->color_space = OPJ_CLRSPC_EYCC;
1647         } else if (jp2->enumcs == 12) {
1648             p_image->color_space = OPJ_CLRSPC_CMYK;
1649         } else {
1650             p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1651         }
1652 
1653         if (jp2->color.jp2_pclr) {
1654             /* Part 1, I.5.3.4: Either both or none : */
1655             if (!jp2->color.jp2_pclr->cmap) {
1656                 opj_jp2_free_pclr(&(jp2->color));
1657             } else {
1658                 if (!opj_jp2_apply_pclr(p_image, &(jp2->color), p_manager)) {
1659                     return OPJ_FALSE;
1660                 }
1661             }
1662         }
1663 
1664         /* Apply the color space if needed */
1665         if (jp2->color.jp2_cdef) {
1666             opj_jp2_apply_cdef(p_image, &(jp2->color), p_manager);
1667         }
1668 
1669         if (jp2->color.icc_profile_buf) {
1670             p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1671             p_image->icc_profile_len = jp2->color.icc_profile_len;
1672             jp2->color.icc_profile_buf = NULL;
1673         }
1674     }
1675 
1676     return OPJ_TRUE;
1677 }
1678 
opj_jp2_write_jp2h(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)1679 static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1680                                    opj_stream_private_t *stream,
1681                                    opj_event_mgr_t * p_manager
1682                                   )
1683 {
1684     opj_jp2_img_header_writer_handler_t l_writers [4];
1685     opj_jp2_img_header_writer_handler_t * l_current_writer;
1686 
1687     OPJ_INT32 i, l_nb_pass;
1688     /* size of data for super box*/
1689     OPJ_UINT32 l_jp2h_size = 8;
1690     OPJ_BOOL l_result = OPJ_TRUE;
1691 
1692     /* to store the data of the super box */
1693     OPJ_BYTE l_jp2h_data [8];
1694 
1695     /* preconditions */
1696     assert(stream != 00);
1697     assert(jp2 != 00);
1698     assert(p_manager != 00);
1699 
1700     memset(l_writers, 0, sizeof(l_writers));
1701 
1702     if (jp2->bpc == 255) {
1703         l_nb_pass = 3;
1704         l_writers[0].handler = opj_jp2_write_ihdr;
1705         l_writers[1].handler = opj_jp2_write_bpcc;
1706         l_writers[2].handler = opj_jp2_write_colr;
1707     } else {
1708         l_nb_pass = 2;
1709         l_writers[0].handler = opj_jp2_write_ihdr;
1710         l_writers[1].handler = opj_jp2_write_colr;
1711     }
1712 
1713     if (jp2->color.jp2_cdef != NULL) {
1714         l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1715         l_nb_pass++;
1716     }
1717 
1718     /* write box header */
1719     /* write JP2H type */
1720     opj_write_bytes(l_jp2h_data + 4, JP2_JP2H, 4);
1721 
1722     l_current_writer = l_writers;
1723     for (i = 0; i < l_nb_pass; ++i) {
1724         l_current_writer->m_data = l_current_writer->handler(jp2,
1725                                    &(l_current_writer->m_size));
1726         if (l_current_writer->m_data == 00) {
1727             opj_event_msg(p_manager, EVT_ERROR,
1728                           "Not enough memory to hold JP2 Header data\n");
1729             l_result = OPJ_FALSE;
1730             break;
1731         }
1732 
1733         l_jp2h_size += l_current_writer->m_size;
1734         ++l_current_writer;
1735     }
1736 
1737     if (! l_result) {
1738         l_current_writer = l_writers;
1739         for (i = 0; i < l_nb_pass; ++i) {
1740             if (l_current_writer->m_data != 00) {
1741                 opj_free(l_current_writer->m_data);
1742             }
1743             ++l_current_writer;
1744         }
1745 
1746         return OPJ_FALSE;
1747     }
1748 
1749     /* write super box size */
1750     opj_write_bytes(l_jp2h_data, l_jp2h_size, 4);
1751 
1752     /* write super box data on stream */
1753     if (opj_stream_write_data(stream, l_jp2h_data, 8, p_manager) != 8) {
1754         opj_event_msg(p_manager, EVT_ERROR,
1755                       "Stream error while writing JP2 Header box\n");
1756         l_result = OPJ_FALSE;
1757     }
1758 
1759     if (l_result) {
1760         l_current_writer = l_writers;
1761         for (i = 0; i < l_nb_pass; ++i) {
1762             if (opj_stream_write_data(stream, l_current_writer->m_data,
1763                                       l_current_writer->m_size, p_manager) != l_current_writer->m_size) {
1764                 opj_event_msg(p_manager, EVT_ERROR,
1765                               "Stream error while writing JP2 Header box\n");
1766                 l_result = OPJ_FALSE;
1767                 break;
1768             }
1769             ++l_current_writer;
1770         }
1771     }
1772 
1773     l_current_writer = l_writers;
1774 
1775     /* cleanup */
1776     for (i = 0; i < l_nb_pass; ++i) {
1777         if (l_current_writer->m_data != 00) {
1778             opj_free(l_current_writer->m_data);
1779         }
1780         ++l_current_writer;
1781     }
1782 
1783     return l_result;
1784 }
1785 
opj_jp2_write_ftyp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1786 static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
1787                                    opj_stream_private_t *cio,
1788                                    opj_event_mgr_t * p_manager)
1789 {
1790     OPJ_UINT32 i;
1791     OPJ_UINT32 l_ftyp_size;
1792     OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1793     OPJ_BOOL l_result;
1794 
1795     /* preconditions */
1796     assert(cio != 00);
1797     assert(jp2 != 00);
1798     assert(p_manager != 00);
1799     l_ftyp_size = 16 + 4 * jp2->numcl;
1800 
1801     l_ftyp_data = (OPJ_BYTE *) opj_calloc(1, l_ftyp_size);
1802 
1803     if (l_ftyp_data == 00) {
1804         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1805         return OPJ_FALSE;
1806     }
1807 
1808     l_current_data_ptr = l_ftyp_data;
1809 
1810     opj_write_bytes(l_current_data_ptr, l_ftyp_size, 4); /* box size */
1811     l_current_data_ptr += 4;
1812 
1813     opj_write_bytes(l_current_data_ptr, JP2_FTYP, 4); /* FTYP */
1814     l_current_data_ptr += 4;
1815 
1816     opj_write_bytes(l_current_data_ptr, jp2->brand, 4); /* BR */
1817     l_current_data_ptr += 4;
1818 
1819     opj_write_bytes(l_current_data_ptr, jp2->minversion, 4); /* MinV */
1820     l_current_data_ptr += 4;
1821 
1822     for (i = 0; i < jp2->numcl; i++)  {
1823         opj_write_bytes(l_current_data_ptr, jp2->cl[i], 4); /* CL */
1824     }
1825 
1826     l_result = (opj_stream_write_data(cio, l_ftyp_data, l_ftyp_size,
1827                                       p_manager) == l_ftyp_size);
1828     if (! l_result) {
1829         opj_event_msg(p_manager, EVT_ERROR,
1830                       "Error while writing ftyp data to stream\n");
1831     }
1832 
1833     opj_free(l_ftyp_data);
1834 
1835     return l_result;
1836 }
1837 
opj_jp2_write_jp2c(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1838 static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
1839                                    opj_stream_private_t *cio,
1840                                    opj_event_mgr_t * p_manager)
1841 {
1842     OPJ_OFF_T j2k_codestream_exit;
1843     OPJ_BYTE l_data_header [8];
1844 
1845     /* preconditions */
1846     assert(jp2 != 00);
1847     assert(cio != 00);
1848     assert(p_manager != 00);
1849     assert(opj_stream_has_seek(cio));
1850 
1851     j2k_codestream_exit = opj_stream_tell(cio);
1852     opj_write_bytes(l_data_header,
1853                     (OPJ_UINT32)(j2k_codestream_exit - jp2->j2k_codestream_offset),
1854                     4); /* size of codestream */
1855     opj_write_bytes(l_data_header + 4, JP2_JP2C,
1856                     4);                                     /* JP2C */
1857 
1858     if (! opj_stream_seek(cio, jp2->j2k_codestream_offset, p_manager)) {
1859         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1860         return OPJ_FALSE;
1861     }
1862 
1863     if (opj_stream_write_data(cio, l_data_header, 8, p_manager) != 8) {
1864         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1865         return OPJ_FALSE;
1866     }
1867 
1868     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
1869         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1870         return OPJ_FALSE;
1871     }
1872 
1873     return OPJ_TRUE;
1874 }
1875 
opj_jp2_write_jp(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)1876 static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
1877                                  opj_stream_private_t *cio,
1878                                  opj_event_mgr_t * p_manager)
1879 {
1880     /* 12 bytes will be read */
1881     OPJ_BYTE l_signature_data [12];
1882 
1883     /* preconditions */
1884     assert(cio != 00);
1885     assert(jp2 != 00);
1886     assert(p_manager != 00);
1887 
1888     OPJ_UNUSED(jp2);
1889 
1890     /* write box length */
1891     opj_write_bytes(l_signature_data, 12, 4);
1892     /* writes box type */
1893     opj_write_bytes(l_signature_data + 4, JP2_JP, 4);
1894     /* writes magic number*/
1895     opj_write_bytes(l_signature_data + 8, 0x0d0a870a, 4);
1896 
1897     if (opj_stream_write_data(cio, l_signature_data, 12, p_manager) != 12) {
1898         return OPJ_FALSE;
1899     }
1900 
1901     return OPJ_TRUE;
1902 }
1903 
1904 /* ----------------------------------------------------------------------- */
1905 /* JP2 decoder interface                                             */
1906 /* ----------------------------------------------------------------------- */
1907 
opj_jp2_setup_decoder(opj_jp2_t * jp2,opj_dparameters_t * parameters)1908 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1909 {
1910     /* setup the J2K codec */
1911     opj_j2k_setup_decoder(jp2->j2k, parameters);
1912 
1913     /* further JP2 initializations go here */
1914     jp2->color.jp2_has_colr = 0;
1915     jp2->comps = NULL;
1916     jp2->ignore_pclr_cmap_cdef = parameters->flags &
1917                                  OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1918 }
1919 
opj_jp2_decoder_set_strict_mode(opj_jp2_t * jp2,OPJ_BOOL strict)1920 void opj_jp2_decoder_set_strict_mode(opj_jp2_t *jp2, OPJ_BOOL strict)
1921 {
1922     opj_j2k_decoder_set_strict_mode(jp2->j2k, strict);
1923 }
1924 
opj_jp2_set_threads(opj_jp2_t * jp2,OPJ_UINT32 num_threads)1925 OPJ_BOOL opj_jp2_set_threads(opj_jp2_t *jp2, OPJ_UINT32 num_threads)
1926 {
1927     return opj_j2k_set_threads(jp2->j2k, num_threads);
1928 }
1929 
1930 /* ----------------------------------------------------------------------- */
1931 /* JP2 encoder interface                                             */
1932 /* ----------------------------------------------------------------------- */
1933 
opj_jp2_setup_encoder(opj_jp2_t * jp2,opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)1934 OPJ_BOOL opj_jp2_setup_encoder(opj_jp2_t *jp2,
1935                                opj_cparameters_t *parameters,
1936                                opj_image_t *image,
1937                                opj_event_mgr_t * p_manager)
1938 {
1939     OPJ_UINT32 i;
1940     OPJ_UINT32 depth_0;
1941     OPJ_UINT32 sign;
1942     OPJ_UINT32 alpha_count;
1943     OPJ_UINT32 color_channels = 0U;
1944     OPJ_UINT32 alpha_channel = 0U;
1945 
1946 
1947     if (!jp2 || !parameters || !image) {
1948         return OPJ_FALSE;
1949     }
1950 
1951     /* setup the J2K codec */
1952     /* ------------------- */
1953 
1954     /* Check if number of components respects standard */
1955     if (image->numcomps < 1 || image->numcomps > 16384) {
1956         opj_event_msg(p_manager, EVT_ERROR,
1957                       "Invalid number of components specified while setting up JP2 encoder\n");
1958         return OPJ_FALSE;
1959     }
1960 
1961     if (opj_j2k_setup_encoder(jp2->j2k, parameters, image,
1962                               p_manager) == OPJ_FALSE) {
1963         return OPJ_FALSE;
1964     }
1965 
1966     /* setup the JP2 codec */
1967     /* ------------------- */
1968 
1969     /* Profile box */
1970 
1971     jp2->brand = JP2_JP2;   /* BR */
1972     jp2->minversion = 0;    /* MinV */
1973     jp2->numcl = 1;
1974     jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1975     if (!jp2->cl) {
1976         opj_event_msg(p_manager, EVT_ERROR,
1977                       "Not enough memory when setup the JP2 encoder\n");
1978         return OPJ_FALSE;
1979     }
1980     jp2->cl[0] = JP2_JP2;   /* CL0 : JP2 */
1981 
1982     /* Image Header box */
1983 
1984     jp2->numcomps = image->numcomps;    /* NC */
1985     jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(
1986                      opj_jp2_comps_t));
1987     if (!jp2->comps) {
1988         opj_event_msg(p_manager, EVT_ERROR,
1989                       "Not enough memory when setup the JP2 encoder\n");
1990         /* Memory of jp2->cl will be freed by opj_jp2_destroy */
1991         return OPJ_FALSE;
1992     }
1993 
1994     jp2->h = image->y1 - image->y0;     /* HEIGHT */
1995     jp2->w = image->x1 - image->x0;     /* WIDTH */
1996     /* BPC */
1997     depth_0 = image->comps[0].prec - 1;
1998     sign = image->comps[0].sgnd;
1999     jp2->bpc = depth_0 + (sign << 7);
2000     for (i = 1; i < image->numcomps; i++) {
2001         OPJ_UINT32 depth = image->comps[i].prec - 1;
2002         sign = image->comps[i].sgnd;
2003         if (depth_0 != depth) {
2004             jp2->bpc = 255;
2005         }
2006     }
2007     jp2->C = 7;         /* C : Always 7 */
2008     jp2->UnkC = 0;      /* UnkC, colorspace specified in colr box */
2009     jp2->IPR = 0;       /* IPR, no intellectual property */
2010 
2011     /* BitsPerComponent box */
2012     for (i = 0; i < image->numcomps; i++) {
2013         jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
2014     }
2015 
2016     /* Colour Specification box */
2017     if (image->icc_profile_len) {
2018         jp2->meth = 2;
2019         jp2->enumcs = 0;
2020     } else {
2021         jp2->meth = 1;
2022         if (image->color_space == 1) {
2023             jp2->enumcs = 16;    /* sRGB as defined by IEC 61966-2-1 */
2024         } else if (image->color_space == 2) {
2025             jp2->enumcs = 17;    /* greyscale */
2026         } else if (image->color_space == 3) {
2027             jp2->enumcs = 18;    /* YUV */
2028         }
2029     }
2030 
2031     /* Channel Definition box */
2032     /* FIXME not provided by parameters */
2033     /* We try to do what we can... */
2034     alpha_count = 0U;
2035     for (i = 0; i < image->numcomps; i++) {
2036         if (image->comps[i].alpha != 0) {
2037             alpha_count++;
2038             alpha_channel = i;
2039         }
2040     }
2041     if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channel */
2042         switch (jp2->enumcs) {
2043         case 16:
2044         case 18:
2045             color_channels = 3;
2046             break;
2047         case 17:
2048             color_channels = 1;
2049             break;
2050         default:
2051             alpha_count = 0U;
2052             break;
2053         }
2054         if (alpha_count == 0U) {
2055             opj_event_msg(p_manager, EVT_WARNING,
2056                           "Alpha channel specified but unknown enumcs. No cdef box will be created.\n");
2057         } else if (image->numcomps < (color_channels + 1)) {
2058             opj_event_msg(p_manager, EVT_WARNING,
2059                           "Alpha channel specified but not enough image components for an automatic cdef box creation.\n");
2060             alpha_count = 0U;
2061         } else if ((OPJ_UINT32)alpha_channel < color_channels) {
2062             opj_event_msg(p_manager, EVT_WARNING,
2063                           "Alpha channel position conflicts with color channel. No cdef box will be created.\n");
2064             alpha_count = 0U;
2065         }
2066     } else if (alpha_count > 1) {
2067         opj_event_msg(p_manager, EVT_WARNING,
2068                       "Multiple alpha channels specified. No cdef box will be created.\n");
2069     }
2070     if (alpha_count == 1U) { /* if here, we know what we can do */
2071         jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
2072         if (!jp2->color.jp2_cdef) {
2073             opj_event_msg(p_manager, EVT_ERROR,
2074                           "Not enough memory to setup the JP2 encoder\n");
2075             return OPJ_FALSE;
2076         }
2077         /* no memset needed, all values will be overwritten except if jp2->color.jp2_cdef->info allocation fails, */
2078         /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
2079         jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(
2080                                         image->numcomps * sizeof(opj_jp2_cdef_info_t));
2081         if (!jp2->color.jp2_cdef->info) {
2082             /* memory will be freed by opj_jp2_destroy */
2083             opj_event_msg(p_manager, EVT_ERROR,
2084                           "Not enough memory to setup the JP2 encoder\n");
2085             return OPJ_FALSE;
2086         }
2087         jp2->color.jp2_cdef->n = (OPJ_UINT16)
2088                                  image->numcomps; /* cast is valid : image->numcomps [1,16384] */
2089         for (i = 0U; i < color_channels; i++) {
2090             jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2091                                               i; /* cast is valid : image->numcomps [1,16384] */
2092             jp2->color.jp2_cdef->info[i].typ = 0U;
2093             jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i +
2094                                                 1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
2095         }
2096         for (; i < image->numcomps; i++) {
2097             if (image->comps[i].alpha != 0) { /* we'll be here exactly once */
2098                 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2099                                                   i; /* cast is valid : image->numcomps [1,16384] */
2100                 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacity channel */
2101                 jp2->color.jp2_cdef->info[i].asoc =
2102                     0U; /* Apply alpha channel to the whole image */
2103             } else {
2104                 /* Unknown channel */
2105                 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)
2106                                                   i; /* cast is valid : image->numcomps [1,16384] */
2107                 jp2->color.jp2_cdef->info[i].typ = 65535U;
2108                 jp2->color.jp2_cdef->info[i].asoc = 65535U;
2109             }
2110         }
2111     }
2112 
2113     jp2->precedence = 0;    /* PRECEDENCE */
2114     jp2->approx = 0;        /* APPROX */
2115 
2116     jp2->jpip_on = parameters->jpip_on;
2117 
2118     return OPJ_TRUE;
2119 }
2120 
opj_jp2_encode(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2121 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
2122                         opj_stream_private_t *stream,
2123                         opj_event_mgr_t * p_manager)
2124 {
2125     return opj_j2k_encode(jp2->j2k, stream, p_manager);
2126 }
2127 
opj_jp2_end_decompress(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2128 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2,
2129                                 opj_stream_private_t *cio,
2130                                 opj_event_mgr_t * p_manager
2131                                )
2132 {
2133     /* preconditions */
2134     assert(jp2 != 00);
2135     assert(cio != 00);
2136     assert(p_manager != 00);
2137 
2138     /* customization of the end encoding */
2139     if (! opj_jp2_setup_end_header_reading(jp2, p_manager)) {
2140         return OPJ_FALSE;
2141     }
2142 
2143     /* write header */
2144     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager)) {
2145         return OPJ_FALSE;
2146     }
2147 
2148     return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
2149 }
2150 
opj_jp2_end_compress(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2151 OPJ_BOOL opj_jp2_end_compress(opj_jp2_t *jp2,
2152                               opj_stream_private_t *cio,
2153                               opj_event_mgr_t * p_manager
2154                              )
2155 {
2156     /* preconditions */
2157     assert(jp2 != 00);
2158     assert(cio != 00);
2159     assert(p_manager != 00);
2160 
2161     /* customization of the end encoding */
2162     if (! opj_jp2_setup_end_header_writing(jp2, p_manager)) {
2163         return OPJ_FALSE;
2164     }
2165 
2166     if (! opj_j2k_end_compress(jp2->j2k, cio, p_manager)) {
2167         return OPJ_FALSE;
2168     }
2169 
2170     /* write header */
2171     return opj_jp2_exec(jp2, jp2->m_procedure_list, cio, p_manager);
2172 }
2173 
opj_jp2_setup_end_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2174 static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
2175         opj_event_mgr_t * p_manager)
2176 {
2177     /* preconditions */
2178     assert(jp2 != 00);
2179     assert(p_manager != 00);
2180 
2181 #ifdef USE_JPIP
2182     if (jp2->jpip_on) {
2183         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2184                                                (opj_procedure)opj_jpip_write_iptr, p_manager)) {
2185             return OPJ_FALSE;
2186         }
2187     }
2188 #endif
2189     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2190                                            (opj_procedure)opj_jp2_write_jp2c, p_manager)) {
2191         return OPJ_FALSE;
2192     }
2193     /* DEVELOPER CORNER, add your custom procedures */
2194 #ifdef USE_JPIP
2195     if (jp2->jpip_on) {
2196         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2197                                                (opj_procedure)opj_jpip_write_cidx, p_manager)) {
2198             return OPJ_FALSE;
2199         }
2200         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2201                                                (opj_procedure)opj_jpip_write_fidx, p_manager)) {
2202             return OPJ_FALSE;
2203         }
2204     }
2205 #endif
2206     return OPJ_TRUE;
2207 }
2208 
opj_jp2_setup_end_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2209 static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
2210         opj_event_mgr_t * p_manager)
2211 {
2212     /* preconditions */
2213     assert(jp2 != 00);
2214     assert(p_manager != 00);
2215 
2216     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2217                                            (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2218         return OPJ_FALSE;
2219     }
2220     /* DEVELOPER CORNER, add your custom procedures */
2221 
2222     return OPJ_TRUE;
2223 }
2224 
opj_jp2_default_validation(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)2225 static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
2226         opj_stream_private_t *cio,
2227         opj_event_mgr_t * p_manager
2228                                           )
2229 {
2230     OPJ_BOOL l_is_valid = OPJ_TRUE;
2231     OPJ_UINT32 i;
2232 
2233     /* preconditions */
2234     assert(jp2 != 00);
2235     assert(cio != 00);
2236     assert(p_manager != 00);
2237 
2238     OPJ_UNUSED(p_manager);
2239 
2240     /* JPEG2000 codec validation */
2241 
2242     /* STATE checking */
2243     /* make sure the state is at 0 */
2244     l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
2245 
2246     /* make sure not reading a jp2h ???? WEIRD */
2247     l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
2248 
2249     /* POINTER validation */
2250     /* make sure a j2k codec is present */
2251     l_is_valid &= (jp2->j2k != 00);
2252 
2253     /* make sure a procedure list is present */
2254     l_is_valid &= (jp2->m_procedure_list != 00);
2255 
2256     /* make sure a validation list is present */
2257     l_is_valid &= (jp2->m_validation_list != 00);
2258 
2259     /* PARAMETER VALIDATION */
2260     /* number of components */
2261     l_is_valid &= (jp2->numcl > 0);
2262     /* width */
2263     l_is_valid &= (jp2->h > 0);
2264     /* height */
2265     l_is_valid &= (jp2->w > 0);
2266     /* precision */
2267     for (i = 0; i < jp2->numcomps; ++i) {
2268         l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) <
2269                        38U); /* 0 is valid, ignore sign for check */
2270     }
2271 
2272     /* METH */
2273     l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
2274 
2275     /* stream validation */
2276     /* back and forth is needed */
2277     l_is_valid &= opj_stream_has_seek(cio);
2278 
2279     return l_is_valid;
2280 }
2281 
opj_jp2_read_header_procedure(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2282 static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
2283         opj_stream_private_t *stream,
2284         opj_event_mgr_t * p_manager
2285                                              )
2286 {
2287     opj_jp2_box_t box;
2288     OPJ_UINT32 l_nb_bytes_read;
2289     const opj_jp2_header_handler_t * l_current_handler;
2290     const opj_jp2_header_handler_t * l_current_handler_misplaced;
2291     OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
2292     OPJ_UINT32 l_current_data_size;
2293     OPJ_BYTE * l_current_data = 00;
2294 
2295     /* preconditions */
2296     assert(stream != 00);
2297     assert(jp2 != 00);
2298     assert(p_manager != 00);
2299 
2300     l_current_data = (OPJ_BYTE*)opj_calloc(1, l_last_data_size);
2301 
2302     if (l_current_data == 00) {
2303         opj_event_msg(p_manager, EVT_ERROR,
2304                       "Not enough memory to handle jpeg2000 file header\n");
2305         return OPJ_FALSE;
2306     }
2307 
2308     while (opj_jp2_read_boxhdr(&box, &l_nb_bytes_read, stream, p_manager)) {
2309         /* is it the codestream box ? */
2310         if (box.type == JP2_JP2C) {
2311             if (jp2->jp2_state & JP2_STATE_HEADER) {
2312                 jp2->jp2_state |= JP2_STATE_CODESTREAM;
2313                 opj_free(l_current_data);
2314                 return OPJ_TRUE;
2315             } else {
2316                 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
2317                 opj_free(l_current_data);
2318                 return OPJ_FALSE;
2319             }
2320         } else if (box.length == 0) {
2321             opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2322             opj_free(l_current_data);
2323             return OPJ_FALSE;
2324         }
2325         /* testcase 1851.pdf.SIGSEGV.ce9.948 */
2326         else if (box.length < l_nb_bytes_read) {
2327             opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d (%x)\n", box.length,
2328                           box.type);
2329             opj_free(l_current_data);
2330             return OPJ_FALSE;
2331         }
2332 
2333         l_current_handler = opj_jp2_find_handler(box.type);
2334         l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
2335         l_current_data_size = box.length - l_nb_bytes_read;
2336 
2337         if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
2338             if (l_current_handler == 00) {
2339                 opj_event_msg(p_manager, EVT_WARNING,
2340                               "Found a misplaced '%c%c%c%c' box outside jp2h box\n",
2341                               (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2342                               (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2343                 if (jp2->jp2_state & JP2_STATE_HEADER) {
2344                     /* read anyway, we already have jp2h */
2345                     l_current_handler = l_current_handler_misplaced;
2346                 } else {
2347                     opj_event_msg(p_manager, EVT_WARNING,
2348                                   "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n",
2349                                   (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2350                                   (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0));
2351                     jp2->jp2_state |= JP2_STATE_UNKNOWN;
2352                     if (opj_stream_skip(stream, l_current_data_size,
2353                                         p_manager) != l_current_data_size) {
2354                         opj_event_msg(p_manager, EVT_ERROR,
2355                                       "Problem with skipping JPEG2000 box, stream error\n");
2356                         opj_free(l_current_data);
2357                         return OPJ_FALSE;
2358                     }
2359                     continue;
2360                 }
2361             }
2362             if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
2363                 /* do not even try to malloc if we can't read */
2364                 opj_event_msg(p_manager, EVT_ERROR,
2365                               "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n",
2366                               box.length, (OPJ_BYTE)(box.type >> 24), (OPJ_BYTE)(box.type >> 16),
2367                               (OPJ_BYTE)(box.type >> 8), (OPJ_BYTE)(box.type >> 0), l_current_data_size,
2368                               (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
2369                 opj_free(l_current_data);
2370                 return OPJ_FALSE;
2371             }
2372             if (l_current_data_size > l_last_data_size) {
2373                 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,
2374                                              l_current_data_size);
2375                 if (!new_current_data) {
2376                     opj_free(l_current_data);
2377                     opj_event_msg(p_manager, EVT_ERROR,
2378                                   "Not enough memory to handle jpeg2000 box\n");
2379                     return OPJ_FALSE;
2380                 }
2381                 l_current_data = new_current_data;
2382                 l_last_data_size = l_current_data_size;
2383             }
2384 
2385             l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(stream, l_current_data,
2386                               l_current_data_size, p_manager);
2387             if (l_nb_bytes_read != l_current_data_size) {
2388                 opj_event_msg(p_manager, EVT_ERROR,
2389                               "Problem with reading JPEG2000 box, stream error\n");
2390                 opj_free(l_current_data);
2391                 return OPJ_FALSE;
2392             }
2393 
2394             if (! l_current_handler->handler(jp2, l_current_data, l_current_data_size,
2395                                              p_manager)) {
2396                 opj_free(l_current_data);
2397                 return OPJ_FALSE;
2398             }
2399         } else {
2400             if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) {
2401                 opj_event_msg(p_manager, EVT_ERROR,
2402                               "Malformed JP2 file format: first box must be JPEG 2000 signature box\n");
2403                 opj_free(l_current_data);
2404                 return OPJ_FALSE;
2405             }
2406             if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) {
2407                 opj_event_msg(p_manager, EVT_ERROR,
2408                               "Malformed JP2 file format: second box must be file type box\n");
2409                 opj_free(l_current_data);
2410                 return OPJ_FALSE;
2411             }
2412             jp2->jp2_state |= JP2_STATE_UNKNOWN;
2413             if (opj_stream_skip(stream, l_current_data_size,
2414                                 p_manager) != l_current_data_size) {
2415                 if (jp2->jp2_state & JP2_STATE_CODESTREAM) {
2416                     /* If we already read the codestream, do not error out */
2417                     /* Needed for data/input/nonregression/issue254.jp2 */
2418                     opj_event_msg(p_manager, EVT_WARNING,
2419                                   "Problem with skipping JPEG2000 box, stream error\n");
2420                     opj_free(l_current_data);
2421                     return OPJ_TRUE;
2422                 } else {
2423                     opj_event_msg(p_manager, EVT_ERROR,
2424                                   "Problem with skipping JPEG2000 box, stream error\n");
2425                     opj_free(l_current_data);
2426                     return OPJ_FALSE;
2427                 }
2428             }
2429         }
2430     }
2431 
2432     opj_free(l_current_data);
2433 
2434     return OPJ_TRUE;
2435 }
2436 
2437 /**
2438  * Executes the given procedures on the given codec.
2439  *
2440  * @param   p_procedure_list    the list of procedures to execute
2441  * @param   jp2                 the jpeg2000 file codec to execute the procedures on.
2442  * @param   stream                  the stream to execute the procedures on.
2443  * @param   p_manager           the user manager.
2444  *
2445  * @return  true                if all the procedures were successfully executed.
2446  */
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)2447 static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
2448                              opj_procedure_list_t * p_procedure_list,
2449                              opj_stream_private_t *stream,
2450                              opj_event_mgr_t * p_manager
2451                             )
2452 
2453 {
2454     OPJ_BOOL(** l_procedure)(opj_jp2_t * jp2, opj_stream_private_t *,
2455                              opj_event_mgr_t *) = 00;
2456     OPJ_BOOL l_result = OPJ_TRUE;
2457     OPJ_UINT32 l_nb_proc, i;
2458 
2459     /* preconditions */
2460     assert(p_procedure_list != 00);
2461     assert(jp2 != 00);
2462     assert(stream != 00);
2463     assert(p_manager != 00);
2464 
2465     l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
2466     l_procedure = (OPJ_BOOL(**)(opj_jp2_t * jp2, opj_stream_private_t *,
2467                                 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
2468 
2469     for (i = 0; i < l_nb_proc; ++i) {
2470         l_result = l_result && (*l_procedure)(jp2, stream, p_manager);
2471         ++l_procedure;
2472     }
2473 
2474     /* and clear the procedure list at the end. */
2475     opj_procedure_list_clear(p_procedure_list);
2476     return l_result;
2477 }
2478 
opj_jp2_start_compress(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)2479 OPJ_BOOL opj_jp2_start_compress(opj_jp2_t *jp2,
2480                                 opj_stream_private_t *stream,
2481                                 opj_image_t * p_image,
2482                                 opj_event_mgr_t * p_manager
2483                                )
2484 {
2485     /* preconditions */
2486     assert(jp2 != 00);
2487     assert(stream != 00);
2488     assert(p_manager != 00);
2489 
2490     /* customization of the validation */
2491     if (! opj_jp2_setup_encoding_validation(jp2, p_manager)) {
2492         return OPJ_FALSE;
2493     }
2494 
2495     /* validation of the parameters codec */
2496     if (! opj_jp2_exec(jp2, jp2->m_validation_list, stream, p_manager)) {
2497         return OPJ_FALSE;
2498     }
2499 
2500     /* customization of the encoding */
2501     if (! opj_jp2_setup_header_writing(jp2, p_manager)) {
2502         return OPJ_FALSE;
2503     }
2504 
2505     /* write header */
2506     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, stream, p_manager)) {
2507         return OPJ_FALSE;
2508     }
2509 
2510     return opj_j2k_start_compress(jp2->j2k, stream, p_image, p_manager);
2511 }
2512 
opj_jp2_find_handler(OPJ_UINT32 p_id)2513 static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id)
2514 {
2515     OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(
2516                                        opj_jp2_header_handler_t);
2517 
2518     for (i = 0; i < l_handler_size; ++i) {
2519         if (jp2_header[i].id == p_id) {
2520             return &jp2_header[i];
2521         }
2522     }
2523     return NULL;
2524 }
2525 
2526 /**
2527  * Finds the image execution function related to the given box id.
2528  *
2529  * @param   p_id    the id of the handler to fetch.
2530  *
2531  * @return  the given handler or 00 if it could not be found.
2532  */
opj_jp2_img_find_handler(OPJ_UINT32 p_id)2533 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
2534     OPJ_UINT32 p_id)
2535 {
2536     OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(
2537                                        opj_jp2_header_handler_t);
2538     for (i = 0; i < l_handler_size; ++i) {
2539         if (jp2_img_header[i].id == p_id) {
2540             return &jp2_img_header[i];
2541         }
2542     }
2543 
2544     return NULL;
2545 }
2546 
2547 /**
2548  * Reads a jpeg2000 file signature box.
2549  *
2550  * @param   p_header_data   the data contained in the signature box.
2551  * @param   jp2             the jpeg2000 file codec.
2552  * @param   p_header_size   the size of the data contained in the signature box.
2553  * @param   p_manager       the user event manager.
2554  *
2555  * @return true if the file signature box is valid.
2556  */
opj_jp2_read_jp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2557 static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
2558                                 OPJ_BYTE * p_header_data,
2559                                 OPJ_UINT32 p_header_size,
2560                                 opj_event_mgr_t * p_manager
2561                                )
2562 
2563 {
2564     OPJ_UINT32 l_magic_number;
2565 
2566     /* preconditions */
2567     assert(p_header_data != 00);
2568     assert(jp2 != 00);
2569     assert(p_manager != 00);
2570 
2571     if (jp2->jp2_state != JP2_STATE_NONE) {
2572         opj_event_msg(p_manager, EVT_ERROR,
2573                       "The signature box must be the first box in the file.\n");
2574         return OPJ_FALSE;
2575     }
2576 
2577     /* assure length of data is correct (4 -> magic number) */
2578     if (p_header_size != 4) {
2579         opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
2580         return OPJ_FALSE;
2581     }
2582 
2583     /* rearrange data */
2584     opj_read_bytes(p_header_data, &l_magic_number, 4);
2585     if (l_magic_number != 0x0d0a870a) {
2586         opj_event_msg(p_manager, EVT_ERROR,
2587                       "Error with JP Signature : bad magic number\n");
2588         return OPJ_FALSE;
2589     }
2590 
2591     jp2->jp2_state |= JP2_STATE_SIGNATURE;
2592 
2593     return OPJ_TRUE;
2594 }
2595 
2596 /**
2597  * Reads a a FTYP box - File type box
2598  *
2599  * @param   p_header_data   the data contained in the FTYP box.
2600  * @param   jp2             the jpeg2000 file codec.
2601  * @param   p_header_size   the size of the data contained in the FTYP box.
2602  * @param   p_manager       the user event manager.
2603  *
2604  * @return true if the FTYP box is valid.
2605  */
opj_jp2_read_ftyp(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2606 static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
2607                                   OPJ_BYTE * p_header_data,
2608                                   OPJ_UINT32 p_header_size,
2609                                   opj_event_mgr_t * p_manager
2610                                  )
2611 {
2612     OPJ_UINT32 i, l_remaining_bytes;
2613 
2614     /* preconditions */
2615     assert(p_header_data != 00);
2616     assert(jp2 != 00);
2617     assert(p_manager != 00);
2618 
2619     if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
2620         opj_event_msg(p_manager, EVT_ERROR,
2621                       "The ftyp box must be the second box in the file.\n");
2622         return OPJ_FALSE;
2623     }
2624 
2625     /* assure length of data is correct */
2626     if (p_header_size < 8) {
2627         opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2628         return OPJ_FALSE;
2629     }
2630 
2631     opj_read_bytes(p_header_data, &jp2->brand, 4);      /* BR */
2632     p_header_data += 4;
2633 
2634     opj_read_bytes(p_header_data, &jp2->minversion, 4);     /* MinV */
2635     p_header_data += 4;
2636 
2637     l_remaining_bytes = p_header_size - 8;
2638 
2639     /* the number of remaining bytes should be a multiple of 4 */
2640     if ((l_remaining_bytes & 0x3) != 0) {
2641         opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
2642         return OPJ_FALSE;
2643     }
2644 
2645     /* div by 4 */
2646     jp2->numcl = l_remaining_bytes >> 2;
2647     if (jp2->numcl) {
2648         jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT32));
2649         if (jp2->cl == 00) {
2650             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
2651             return OPJ_FALSE;
2652         }
2653     }
2654 
2655     for (i = 0; i < jp2->numcl; ++i) {
2656         opj_read_bytes(p_header_data, &jp2->cl[i], 4);      /* CLi */
2657         p_header_data += 4;
2658     }
2659 
2660     jp2->jp2_state |= JP2_STATE_FILE_TYPE;
2661 
2662     return OPJ_TRUE;
2663 }
2664 
opj_jp2_skip_jp2c(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2665 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
2666                                   opj_stream_private_t *stream,
2667                                   opj_event_mgr_t * p_manager)
2668 {
2669     /* preconditions */
2670     assert(jp2 != 00);
2671     assert(stream != 00);
2672     assert(p_manager != 00);
2673 
2674     jp2->j2k_codestream_offset = opj_stream_tell(stream);
2675 
2676     if (opj_stream_skip(stream, 8, p_manager) != 8) {
2677         return OPJ_FALSE;
2678     }
2679 
2680     return OPJ_TRUE;
2681 }
2682 
opj_jpip_skip_iptr(opj_jp2_t * jp2,opj_stream_private_t * stream,opj_event_mgr_t * p_manager)2683 static OPJ_BOOL opj_jpip_skip_iptr(opj_jp2_t *jp2,
2684                                    opj_stream_private_t *stream,
2685                                    opj_event_mgr_t * p_manager)
2686 {
2687     /* preconditions */
2688     assert(jp2 != 00);
2689     assert(stream != 00);
2690     assert(p_manager != 00);
2691 
2692     jp2->jpip_iptr_offset = opj_stream_tell(stream);
2693 
2694     if (opj_stream_skip(stream, 24, p_manager) != 24) {
2695         return OPJ_FALSE;
2696     }
2697 
2698     return OPJ_TRUE;
2699 }
2700 
2701 /**
2702  * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
2703  *
2704  * @param   p_header_data   the data contained in the file header box.
2705  * @param   jp2             the jpeg2000 file codec.
2706  * @param   p_header_size   the size of the data contained in the file header box.
2707  * @param   p_manager       the user event manager.
2708  *
2709  * @return true if the JP2 Header box was successfully recognized.
2710 */
opj_jp2_read_jp2h(opj_jp2_t * jp2,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2711 static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
2712                                   OPJ_BYTE *p_header_data,
2713                                   OPJ_UINT32 p_header_size,
2714                                   opj_event_mgr_t * p_manager
2715                                  )
2716 {
2717     OPJ_UINT32 l_box_size = 0, l_current_data_size = 0;
2718     opj_jp2_box_t box;
2719     const opj_jp2_header_handler_t * l_current_handler;
2720     OPJ_BOOL l_has_ihdr = 0;
2721 
2722     /* preconditions */
2723     assert(p_header_data != 00);
2724     assert(jp2 != 00);
2725     assert(p_manager != 00);
2726 
2727     /* make sure the box is well placed */
2728     if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE) {
2729         opj_event_msg(p_manager, EVT_ERROR,
2730                       "The  box must be the first box in the file.\n");
2731         return OPJ_FALSE;
2732     }
2733 
2734     jp2->jp2_img_state = JP2_IMG_STATE_NONE;
2735 
2736     /* iterate while remaining data */
2737     while (p_header_size > 0) {
2738 
2739         if (! opj_jp2_read_boxhdr_char(&box, p_header_data, &l_box_size, p_header_size,
2740                                        p_manager)) {
2741             opj_event_msg(p_manager, EVT_ERROR,
2742                           "Stream error while reading JP2 Header box\n");
2743             return OPJ_FALSE;
2744         }
2745 
2746         if (box.length > p_header_size) {
2747             opj_event_msg(p_manager, EVT_ERROR,
2748                           "Stream error while reading JP2 Header box: box length is inconsistent.\n");
2749             return OPJ_FALSE;
2750         }
2751 
2752         l_current_handler = opj_jp2_img_find_handler(box.type);
2753         l_current_data_size = box.length - l_box_size;
2754         p_header_data += l_box_size;
2755 
2756         if (l_current_handler != 00) {
2757             if (! l_current_handler->handler(jp2, p_header_data, l_current_data_size,
2758                                              p_manager)) {
2759                 return OPJ_FALSE;
2760             }
2761         } else {
2762             jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
2763         }
2764 
2765         if (box.type == JP2_IHDR) {
2766             l_has_ihdr = 1;
2767         }
2768 
2769         p_header_data += l_current_data_size;
2770         p_header_size -= box.length;
2771     }
2772 
2773     if (l_has_ihdr == 0) {
2774         opj_event_msg(p_manager, EVT_ERROR,
2775                       "Stream error while reading JP2 Header box: no 'ihdr' box.\n");
2776         return OPJ_FALSE;
2777     }
2778 
2779     jp2->jp2_state |= JP2_STATE_HEADER;
2780     jp2->has_jp2h = 1;
2781 
2782     return OPJ_TRUE;
2783 }
2784 
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)2785 static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
2786         OPJ_BYTE * p_data,
2787         OPJ_UINT32 * p_number_bytes_read,
2788         OPJ_UINT32 p_box_max_size,
2789         opj_event_mgr_t * p_manager
2790                                         )
2791 {
2792     OPJ_UINT32 l_value;
2793 
2794     /* preconditions */
2795     assert(p_data != 00);
2796     assert(box != 00);
2797     assert(p_number_bytes_read != 00);
2798     assert(p_manager != 00);
2799 
2800     if (p_box_max_size < 8) {
2801         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2802         return OPJ_FALSE;
2803     }
2804 
2805     /* process read data */
2806     opj_read_bytes(p_data, &l_value, 4);
2807     p_data += 4;
2808     box->length = (OPJ_UINT32)(l_value);
2809 
2810     opj_read_bytes(p_data, &l_value, 4);
2811     p_data += 4;
2812     box->type = (OPJ_UINT32)(l_value);
2813 
2814     *p_number_bytes_read = 8;
2815 
2816     /* do we have a "special very large box ?" */
2817     /* read then the XLBox */
2818     if (box->length == 1) {
2819         OPJ_UINT32 l_xl_part_size;
2820 
2821         if (p_box_max_size < 16) {
2822             opj_event_msg(p_manager, EVT_ERROR,
2823                           "Cannot handle XL box of less than 16 bytes\n");
2824             return OPJ_FALSE;
2825         }
2826 
2827         opj_read_bytes(p_data, &l_xl_part_size, 4);
2828         p_data += 4;
2829         *p_number_bytes_read += 4;
2830 
2831         if (l_xl_part_size != 0) {
2832             opj_event_msg(p_manager, EVT_ERROR,
2833                           "Cannot handle box sizes higher than 2^32\n");
2834             return OPJ_FALSE;
2835         }
2836 
2837         opj_read_bytes(p_data, &l_value, 4);
2838         *p_number_bytes_read += 4;
2839         box->length = (OPJ_UINT32)(l_value);
2840 
2841         if (box->length == 0) {
2842             opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2843             return OPJ_FALSE;
2844         }
2845     } else if (box->length == 0) {
2846         opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2847         return OPJ_FALSE;
2848     }
2849     if (box->length < *p_number_bytes_read) {
2850         opj_event_msg(p_manager, EVT_ERROR, "Box length is inconsistent.\n");
2851         return OPJ_FALSE;
2852     }
2853     return OPJ_TRUE;
2854 }
2855 
opj_jp2_read_header(opj_stream_private_t * p_stream,opj_jp2_t * jp2,opj_image_t ** p_image,opj_event_mgr_t * p_manager)2856 OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
2857                              opj_jp2_t *jp2,
2858                              opj_image_t ** p_image,
2859                              opj_event_mgr_t * p_manager
2860                             )
2861 {
2862     /* preconditions */
2863     assert(jp2 != 00);
2864     assert(p_stream != 00);
2865     assert(p_manager != 00);
2866 
2867     /* customization of the validation */
2868     if (! opj_jp2_setup_decoding_validation(jp2, p_manager)) {
2869         return OPJ_FALSE;
2870     }
2871 
2872     /* customization of the encoding */
2873     if (! opj_jp2_setup_header_reading(jp2, p_manager)) {
2874         return OPJ_FALSE;
2875     }
2876 
2877     /* validation of the parameters codec */
2878     if (! opj_jp2_exec(jp2, jp2->m_validation_list, p_stream, p_manager)) {
2879         return OPJ_FALSE;
2880     }
2881 
2882     /* read header */
2883     if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
2884         return OPJ_FALSE;
2885     }
2886     if (jp2->has_jp2h == 0) {
2887         opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
2888         return OPJ_FALSE;
2889     }
2890     if (jp2->has_ihdr == 0) {
2891         opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
2892         return OPJ_FALSE;
2893     }
2894 
2895     return opj_j2k_read_header(p_stream,
2896                                jp2->j2k,
2897                                p_image,
2898                                p_manager);
2899 }
2900 
opj_jp2_setup_encoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2901 static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
2902         opj_event_mgr_t * p_manager)
2903 {
2904     /* preconditions */
2905     assert(jp2 != 00);
2906     assert(p_manager != 00);
2907 
2908     if (! opj_procedure_list_add_procedure(jp2->m_validation_list,
2909                                            (opj_procedure)opj_jp2_default_validation, p_manager)) {
2910         return OPJ_FALSE;
2911     }
2912     /* DEVELOPER CORNER, add your custom validation procedure */
2913 
2914     return OPJ_TRUE;
2915 }
2916 
opj_jp2_setup_decoding_validation(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2917 static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
2918         opj_event_mgr_t * p_manager)
2919 {
2920     /* preconditions */
2921     assert(jp2 != 00);
2922     assert(p_manager != 00);
2923 
2924     OPJ_UNUSED(jp2);
2925     OPJ_UNUSED(p_manager);
2926 
2927     /* DEVELOPER CORNER, add your custom validation procedure */
2928 
2929     return OPJ_TRUE;
2930 }
2931 
opj_jp2_setup_header_writing(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2932 static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
2933         opj_event_mgr_t * p_manager)
2934 {
2935     /* preconditions */
2936     assert(jp2 != 00);
2937     assert(p_manager != 00);
2938 
2939     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2940                                            (opj_procedure)opj_jp2_write_jp, p_manager)) {
2941         return OPJ_FALSE;
2942     }
2943     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2944                                            (opj_procedure)opj_jp2_write_ftyp, p_manager)) {
2945         return OPJ_FALSE;
2946     }
2947     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2948                                            (opj_procedure)opj_jp2_write_jp2h, p_manager)) {
2949         return OPJ_FALSE;
2950     }
2951     if (jp2->jpip_on) {
2952         if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2953                                                (opj_procedure)opj_jpip_skip_iptr, p_manager)) {
2954             return OPJ_FALSE;
2955         }
2956     }
2957     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2958                                            (opj_procedure)opj_jp2_skip_jp2c, p_manager)) {
2959         return OPJ_FALSE;
2960     }
2961 
2962     /* DEVELOPER CORNER, insert your custom procedures */
2963 
2964     return OPJ_TRUE;
2965 }
2966 
opj_jp2_setup_header_reading(opj_jp2_t * jp2,opj_event_mgr_t * p_manager)2967 static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
2968         opj_event_mgr_t * p_manager)
2969 {
2970     /* preconditions */
2971     assert(jp2 != 00);
2972     assert(p_manager != 00);
2973 
2974     if (! opj_procedure_list_add_procedure(jp2->m_procedure_list,
2975                                            (opj_procedure)opj_jp2_read_header_procedure, p_manager)) {
2976         return OPJ_FALSE;
2977     }
2978 
2979     /* DEVELOPER CORNER, add your custom procedures */
2980 
2981     return OPJ_TRUE;
2982 }
2983 
opj_jp2_read_tile_header(opj_jp2_t * 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)2984 OPJ_BOOL opj_jp2_read_tile_header(opj_jp2_t * p_jp2,
2985                                   OPJ_UINT32 * p_tile_index,
2986                                   OPJ_UINT32 * p_data_size,
2987                                   OPJ_INT32 * p_tile_x0,
2988                                   OPJ_INT32 * p_tile_y0,
2989                                   OPJ_INT32 * p_tile_x1,
2990                                   OPJ_INT32 * p_tile_y1,
2991                                   OPJ_UINT32 * p_nb_comps,
2992                                   OPJ_BOOL * p_go_on,
2993                                   opj_stream_private_t *p_stream,
2994                                   opj_event_mgr_t * p_manager
2995                                  )
2996 {
2997     return opj_j2k_read_tile_header(p_jp2->j2k,
2998                                     p_tile_index,
2999                                     p_data_size,
3000                                     p_tile_x0, p_tile_y0,
3001                                     p_tile_x1, p_tile_y1,
3002                                     p_nb_comps,
3003                                     p_go_on,
3004                                     p_stream,
3005                                     p_manager);
3006 }
3007 
opj_jp2_write_tile(opj_jp2_t * 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)3008 OPJ_BOOL opj_jp2_write_tile(opj_jp2_t *p_jp2,
3009                             OPJ_UINT32 p_tile_index,
3010                             OPJ_BYTE * p_data,
3011                             OPJ_UINT32 p_data_size,
3012                             opj_stream_private_t *p_stream,
3013                             opj_event_mgr_t * p_manager
3014                            )
3015 
3016 {
3017     return opj_j2k_write_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3018                               p_stream, p_manager);
3019 }
3020 
opj_jp2_decode_tile(opj_jp2_t * 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)3021 OPJ_BOOL opj_jp2_decode_tile(opj_jp2_t * p_jp2,
3022                              OPJ_UINT32 p_tile_index,
3023                              OPJ_BYTE * p_data,
3024                              OPJ_UINT32 p_data_size,
3025                              opj_stream_private_t *p_stream,
3026                              opj_event_mgr_t * p_manager
3027                             )
3028 {
3029     return opj_j2k_decode_tile(p_jp2->j2k, p_tile_index, p_data, p_data_size,
3030                                p_stream, p_manager);
3031 }
3032 
opj_jp2_destroy(opj_jp2_t * jp2)3033 void opj_jp2_destroy(opj_jp2_t *jp2)
3034 {
3035     if (jp2) {
3036         /* destroy the J2K codec */
3037         opj_j2k_destroy(jp2->j2k);
3038         jp2->j2k = 00;
3039 
3040         if (jp2->comps) {
3041             opj_free(jp2->comps);
3042             jp2->comps = 00;
3043         }
3044 
3045         if (jp2->cl) {
3046             opj_free(jp2->cl);
3047             jp2->cl = 00;
3048         }
3049 
3050         if (jp2->color.icc_profile_buf) {
3051             opj_free(jp2->color.icc_profile_buf);
3052             jp2->color.icc_profile_buf = 00;
3053         }
3054 
3055         if (jp2->color.jp2_cdef) {
3056             if (jp2->color.jp2_cdef->info) {
3057                 opj_free(jp2->color.jp2_cdef->info);
3058                 jp2->color.jp2_cdef->info = NULL;
3059             }
3060 
3061             opj_free(jp2->color.jp2_cdef);
3062             jp2->color.jp2_cdef = 00;
3063         }
3064 
3065         if (jp2->color.jp2_pclr) {
3066             if (jp2->color.jp2_pclr->cmap) {
3067                 opj_free(jp2->color.jp2_pclr->cmap);
3068                 jp2->color.jp2_pclr->cmap = NULL;
3069             }
3070             if (jp2->color.jp2_pclr->channel_sign) {
3071                 opj_free(jp2->color.jp2_pclr->channel_sign);
3072                 jp2->color.jp2_pclr->channel_sign = NULL;
3073             }
3074             if (jp2->color.jp2_pclr->channel_size) {
3075                 opj_free(jp2->color.jp2_pclr->channel_size);
3076                 jp2->color.jp2_pclr->channel_size = NULL;
3077             }
3078             if (jp2->color.jp2_pclr->entries) {
3079                 opj_free(jp2->color.jp2_pclr->entries);
3080                 jp2->color.jp2_pclr->entries = NULL;
3081             }
3082 
3083             opj_free(jp2->color.jp2_pclr);
3084             jp2->color.jp2_pclr = 00;
3085         }
3086 
3087         if (jp2->m_validation_list) {
3088             opj_procedure_list_destroy(jp2->m_validation_list);
3089             jp2->m_validation_list = 00;
3090         }
3091 
3092         if (jp2->m_procedure_list) {
3093             opj_procedure_list_destroy(jp2->m_procedure_list);
3094             jp2->m_procedure_list = 00;
3095         }
3096 
3097         opj_free(jp2);
3098     }
3099 }
3100 
opj_jp2_set_decoded_components(opj_jp2_t * p_jp2,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,opj_event_mgr_t * p_manager)3101 OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *p_jp2,
3102                                         OPJ_UINT32 numcomps,
3103                                         const OPJ_UINT32* comps_indices,
3104                                         opj_event_mgr_t * p_manager)
3105 {
3106     return opj_j2k_set_decoded_components(p_jp2->j2k,
3107                                           numcomps, comps_indices,
3108                                           p_manager);
3109 }
3110 
opj_jp2_set_decode_area(opj_jp2_t * 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)3111 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
3112                                  opj_image_t* p_image,
3113                                  OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
3114                                  OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
3115                                  opj_event_mgr_t * p_manager
3116                                 )
3117 {
3118     return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y,
3119                                    p_end_x, p_end_y, p_manager);
3120 }
3121 
opj_jp2_get_tile(opj_jp2_t * p_jp2,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager,OPJ_UINT32 tile_index)3122 OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
3123                           opj_stream_private_t *p_stream,
3124                           opj_image_t* p_image,
3125                           opj_event_mgr_t * p_manager,
3126                           OPJ_UINT32 tile_index
3127                          )
3128 {
3129     if (!p_image) {
3130         return OPJ_FALSE;
3131     }
3132 
3133     opj_event_msg(p_manager, EVT_WARNING,
3134                   "JP2 box which are after the codestream will not be read by this function.\n");
3135 
3136     if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index)) {
3137         opj_event_msg(p_manager, EVT_ERROR,
3138                       "Failed to decode the codestream in the JP2 file\n");
3139         return OPJ_FALSE;
3140     }
3141 
3142     if (p_jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
3143         /* Bypass all JP2 component transforms */
3144         return OPJ_TRUE;
3145     }
3146 
3147     if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
3148         return OPJ_FALSE;
3149     }
3150 
3151     /* Set Image Color Space */
3152     if (p_jp2->enumcs == 16) {
3153         p_image->color_space = OPJ_CLRSPC_SRGB;
3154     } else if (p_jp2->enumcs == 17) {
3155         p_image->color_space = OPJ_CLRSPC_GRAY;
3156     } else if (p_jp2->enumcs == 18) {
3157         p_image->color_space = OPJ_CLRSPC_SYCC;
3158     } else if (p_jp2->enumcs == 24) {
3159         p_image->color_space = OPJ_CLRSPC_EYCC;
3160     } else if (p_jp2->enumcs == 12) {
3161         p_image->color_space = OPJ_CLRSPC_CMYK;
3162     } else {
3163         p_image->color_space = OPJ_CLRSPC_UNKNOWN;
3164     }
3165 
3166     if (p_jp2->color.jp2_pclr) {
3167         /* Part 1, I.5.3.4: Either both or none : */
3168         if (!p_jp2->color.jp2_pclr->cmap) {
3169             opj_jp2_free_pclr(&(p_jp2->color));
3170         } else {
3171             if (!opj_jp2_apply_pclr(p_image, &(p_jp2->color), p_manager)) {
3172                 return OPJ_FALSE;
3173             }
3174         }
3175     }
3176 
3177     /* Apply the color space if needed */
3178     if (p_jp2->color.jp2_cdef) {
3179         opj_jp2_apply_cdef(p_image, &(p_jp2->color), p_manager);
3180     }
3181 
3182     if (p_jp2->color.icc_profile_buf) {
3183         p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
3184         p_image->icc_profile_len = p_jp2->color.icc_profile_len;
3185         p_jp2->color.icc_profile_buf = NULL;
3186     }
3187 
3188     return OPJ_TRUE;
3189 }
3190 
3191 /* ----------------------------------------------------------------------- */
3192 /* JP2 encoder interface                                             */
3193 /* ----------------------------------------------------------------------- */
3194 
opj_jp2_create(OPJ_BOOL p_is_decoder)3195 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder)
3196 {
3197     opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1, sizeof(opj_jp2_t));
3198     if (jp2) {
3199 
3200         /* create the J2K codec */
3201         if (! p_is_decoder) {
3202             jp2->j2k = opj_j2k_create_compress();
3203         } else {
3204             jp2->j2k = opj_j2k_create_decompress();
3205         }
3206 
3207         if (jp2->j2k == 00) {
3208             opj_jp2_destroy(jp2);
3209             return 00;
3210         }
3211 
3212         /* Color structure */
3213         jp2->color.icc_profile_buf = NULL;
3214         jp2->color.icc_profile_len = 0;
3215         jp2->color.jp2_cdef = NULL;
3216         jp2->color.jp2_pclr = NULL;
3217         jp2->color.jp2_has_colr = 0;
3218 
3219         /* validation list creation */
3220         jp2->m_validation_list = opj_procedure_list_create();
3221         if (! jp2->m_validation_list) {
3222             opj_jp2_destroy(jp2);
3223             return 00;
3224         }
3225 
3226         /* execution list creation */
3227         jp2->m_procedure_list = opj_procedure_list_create();
3228         if (! jp2->m_procedure_list) {
3229             opj_jp2_destroy(jp2);
3230             return 00;
3231         }
3232     }
3233 
3234     return jp2;
3235 }
3236 
jp2_dump(opj_jp2_t * p_jp2,OPJ_INT32 flag,FILE * out_stream)3237 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
3238 {
3239     /* preconditions */
3240     assert(p_jp2 != 00);
3241 
3242     j2k_dump(p_jp2->j2k,
3243              flag,
3244              out_stream);
3245 }
3246 
jp2_get_cstr_index(opj_jp2_t * p_jp2)3247 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
3248 {
3249     return j2k_get_cstr_index(p_jp2->j2k);
3250 }
3251 
jp2_get_cstr_info(opj_jp2_t * p_jp2)3252 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
3253 {
3254     return j2k_get_cstr_info(p_jp2->j2k);
3255 }
3256 
opj_jp2_set_decoded_resolution_factor(opj_jp2_t * p_jp2,OPJ_UINT32 res_factor,opj_event_mgr_t * p_manager)3257 OPJ_BOOL opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
3258         OPJ_UINT32 res_factor,
3259         opj_event_mgr_t * p_manager)
3260 {
3261     return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
3262 }
3263 
3264 /* ----------------------------------------------------------------------- */
3265 
opj_jp2_encoder_set_extra_options(opj_jp2_t * p_jp2,const char * const * p_options,opj_event_mgr_t * p_manager)3266 OPJ_BOOL opj_jp2_encoder_set_extra_options(
3267     opj_jp2_t *p_jp2,
3268     const char* const* p_options,
3269     opj_event_mgr_t * p_manager)
3270 {
3271     return opj_j2k_encoder_set_extra_options(p_jp2->j2k, p_options, p_manager);
3272 }
3273 
3274 /* ----------------------------------------------------------------------- */
3275 
3276 /* JPIP specific */
3277 
3278 #ifdef USE_JPIP
opj_jpip_write_iptr(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3279 static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
3280                                     opj_stream_private_t *cio,
3281                                     opj_event_mgr_t * p_manager)
3282 {
3283     OPJ_OFF_T j2k_codestream_exit;
3284     OPJ_BYTE l_data_header [24];
3285 
3286     /* preconditions */
3287     assert(jp2 != 00);
3288     assert(cio != 00);
3289     assert(p_manager != 00);
3290     assert(opj_stream_has_seek(cio));
3291 
3292     j2k_codestream_exit = opj_stream_tell(cio);
3293     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3294     opj_write_bytes(l_data_header + 4, JPIP_IPTR,
3295                     4);                                      /* IPTR */
3296 #if 0
3297     opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3298     opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3299 #else
3300     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3301     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3302 #endif
3303 
3304     if (! opj_stream_seek(cio, jp2->jpip_iptr_offset, p_manager)) {
3305         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3306         return OPJ_FALSE;
3307     }
3308 
3309     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3310         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3311         return OPJ_FALSE;
3312     }
3313 
3314     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3315         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3316         return OPJ_FALSE;
3317     }
3318 
3319     return OPJ_TRUE;
3320 }
3321 
opj_jpip_write_fidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3322 static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
3323                                     opj_stream_private_t *cio,
3324                                     opj_event_mgr_t * p_manager)
3325 {
3326     OPJ_OFF_T j2k_codestream_exit;
3327     OPJ_BYTE l_data_header [24];
3328 
3329     OPJ_UNUSED(jp2);
3330 
3331     /* preconditions */
3332     assert(jp2 != 00);
3333     assert(cio != 00);
3334     assert(p_manager != 00);
3335     assert(opj_stream_has_seek(cio));
3336 
3337     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3338     opj_write_bytes(l_data_header + 4, JPIP_FIDX,
3339                     4);                                      /* IPTR */
3340     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3341     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3342 
3343     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3344         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3345         return OPJ_FALSE;
3346     }
3347 
3348     j2k_codestream_exit = opj_stream_tell(cio);
3349     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3350         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3351         return OPJ_FALSE;
3352     }
3353 
3354     return OPJ_TRUE;
3355 }
3356 
opj_jpip_write_cidx(opj_jp2_t * jp2,opj_stream_private_t * cio,opj_event_mgr_t * p_manager)3357 static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
3358                                     opj_stream_private_t *cio,
3359                                     opj_event_mgr_t * p_manager)
3360 {
3361     OPJ_OFF_T j2k_codestream_exit;
3362     OPJ_BYTE l_data_header [24];
3363 
3364     OPJ_UNUSED(jp2);
3365 
3366     /* preconditions */
3367     assert(jp2 != 00);
3368     assert(cio != 00);
3369     assert(p_manager != 00);
3370     assert(opj_stream_has_seek(cio));
3371 
3372     j2k_codestream_exit = opj_stream_tell(cio);
3373     opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
3374     opj_write_bytes(l_data_header + 4, JPIP_CIDX,
3375                     4);                                      /* IPTR */
3376 #if 0
3377     opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
3378     opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
3379 #else
3380     opj_write_double(l_data_header + 4 + 4, 0); /* offset */
3381     opj_write_double(l_data_header + 8 + 8, 0); /* length */
3382 #endif
3383 
3384     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3385         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3386         return OPJ_FALSE;
3387     }
3388 
3389     if (opj_stream_write_data(cio, l_data_header, 24, p_manager) != 24) {
3390         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3391         return OPJ_FALSE;
3392     }
3393 
3394     j2k_codestream_exit = opj_stream_tell(cio);
3395     if (! opj_stream_seek(cio, j2k_codestream_exit, p_manager)) {
3396         opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
3397         return OPJ_FALSE;
3398     }
3399 
3400     return OPJ_TRUE;
3401 }
3402 
3403 #if 0
3404 static void write_prxy(int offset_jp2c, int length_jp2c, int offset_idx,
3405                        int length_idx, opj_stream_private_t *cio,
3406                        opj_event_mgr_t * p_manager)
3407 {
3408     OPJ_BYTE l_data_header [8];
3409     OPJ_OFF_T len, lenp;
3410 
3411     lenp = opj_stream_tell(cio);
3412     opj_stream_skip(cio, 4, p_manager);         /* L [at the end] */
3413     opj_write_bytes(l_data_header, JPIP_PRXY, 4); /* IPTR           */
3414     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3415 
3416     opj_write_bytes(l_data_header, offset_jp2c, 8);  /* OOFF           */
3417     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3418     opj_write_bytes(l_data_header, length_jp2c, 4);  /* OBH part 1     */
3419     opj_write_bytes(l_data_header + 4, JP2_JP2C, 4); /* OBH part 2     */
3420     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3421 
3422     opj_write_bytes(l_data_header, 1, 1); /* NI             */
3423     opj_stream_write_data(cio, l_data_header, 1, p_manager);
3424 
3425     opj_write_bytes(l_data_header, offset_idx, 8);   /* IOFF           */
3426     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3427     opj_write_bytes(l_data_header, length_idx, 4);   /* IBH part 1     */
3428     opj_write_bytes(l_data_header + 4, JPIP_CIDX, 4);  /* IBH part 2     */
3429     opj_stream_write_data(cio, l_data_header, 8, p_manager);
3430 
3431     len = opj_stream_tell(cio) - lenp;
3432     opj_stream_skip(cio, lenp, p_manager);
3433     opj_write_bytes(l_data_header, len, 4); /* L              */
3434     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3435     opj_stream_seek(cio, lenp + len, p_manager);
3436 }
3437 #endif
3438 
3439 
3440 #if 0
3441 static int write_fidx(int offset_jp2c, int length_jp2c, int offset_idx,
3442                       int length_idx, opj_stream_private_t *cio,
3443                       opj_event_mgr_t * p_manager)
3444 {
3445     OPJ_BYTE l_data_header [4];
3446     OPJ_OFF_T len, lenp;
3447 
3448     lenp = opj_stream_tell(cio);
3449     opj_stream_skip(cio, 4, p_manager);
3450     opj_write_bytes(l_data_header, JPIP_FIDX, 4); /* FIDX */
3451     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3452 
3453     write_prxy(offset_jp2c, length_jp2c, offset_idx, length_idx, cio, p_manager);
3454 
3455     len = opj_stream_tell(cio) - lenp;
3456     opj_stream_skip(cio, lenp, p_manager);
3457     opj_write_bytes(l_data_header, len, 4); /* L              */
3458     opj_stream_write_data(cio, l_data_header, 4, p_manager);
3459     opj_stream_seek(cio, lenp + len, p_manager);
3460 
3461     return len;
3462 }
3463 #endif
3464 #endif /* USE_JPIP */
3465