• 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) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
15  * Copyright (c) 2006-2007, Parvatha Elangovan
16  * Copyright (c) 2010-2011, Kaori Hagihara
17  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
18  * Copyright (c) 2012, CS Systemes d'Information, France
19  * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
32  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  */
43 
44 #include "opj_includes.h"
45 
46 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
47 /*@{*/
48 
49 /** @name Local static functions */
50 /*@{*/
51 
52 /**
53  * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
54  */
55 static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k,
56         opj_event_mgr_t * p_manager);
57 
58 /**
59  * The read header procedure.
60  */
61 static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k,
62         opj_stream_private_t *p_stream,
63         opj_event_mgr_t * p_manager);
64 
65 /**
66  * The default encoding validation procedure without any extension.
67  *
68  * @param       p_j2k                   the jpeg2000 codec to validate.
69  * @param       p_stream                the input stream to validate.
70  * @param       p_manager               the user event manager.
71  *
72  * @return true if the parameters are correct.
73  */
74 static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k,
75         opj_stream_private_t *p_stream,
76         opj_event_mgr_t * p_manager);
77 
78 /**
79  * The default decoding validation procedure without any extension.
80  *
81  * @param       p_j2k                   the jpeg2000 codec to validate.
82  * @param       p_stream                                the input stream to validate.
83  * @param       p_manager               the user event manager.
84  *
85  * @return true if the parameters are correct.
86  */
87 static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t * p_j2k,
88         opj_stream_private_t *p_stream,
89         opj_event_mgr_t * p_manager);
90 
91 /**
92  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
93  * are valid. Developpers wanting to extend the library can add their own validation procedures.
94  */
95 static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k,
96         opj_event_mgr_t * p_manager);
97 
98 /**
99  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
100  * are valid. Developpers wanting to extend the library can add their own validation procedures.
101  */
102 static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k,
103         opj_event_mgr_t * p_manager);
104 
105 /**
106  * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
107  * are valid. Developpers wanting to extend the library can add their own validation procedures.
108  */
109 static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k,
110         opj_event_mgr_t * p_manager);
111 
112 /**
113  * The mct encoding validation procedure.
114  *
115  * @param       p_j2k                   the jpeg2000 codec to validate.
116  * @param       p_stream                                the input stream to validate.
117  * @param       p_manager               the user event manager.
118  *
119  * @return true if the parameters are correct.
120  */
121 static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k,
122                                        opj_stream_private_t *p_stream,
123                                        opj_event_mgr_t * p_manager);
124 
125 /**
126  * Builds the tcd decoder to use to decode tile.
127  */
128 static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k,
129                                       opj_stream_private_t *p_stream,
130                                       opj_event_mgr_t * p_manager);
131 /**
132  * Builds the tcd encoder to use to encode tile.
133  */
134 static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k,
135                                       opj_stream_private_t *p_stream,
136                                       opj_event_mgr_t * p_manager);
137 
138 /**
139  * Creates a tile-coder encoder.
140  *
141  * @param       p_stream                        the stream to write data to.
142  * @param       p_j2k                           J2K codec.
143  * @param       p_manager                   the user event manager.
144 */
145 static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k,
146                                    opj_stream_private_t *p_stream,
147                                    opj_event_mgr_t * p_manager);
148 
149 /**
150  * Excutes the given procedures on the given codec.
151  *
152  * @param       p_procedure_list        the list of procedures to execute
153  * @param       p_j2k                           the jpeg2000 codec to execute the procedures on.
154  * @param       p_stream                        the stream to execute the procedures on.
155  * @param       p_manager                       the user manager.
156  *
157  * @return      true                            if all the procedures were successfully executed.
158  */
159 static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k,
160                              opj_procedure_list_t * p_procedure_list,
161                              opj_stream_private_t *p_stream,
162                              opj_event_mgr_t * p_manager);
163 
164 /**
165  * Updates the rates of the tcp.
166  *
167  * @param       p_stream                                the stream to write data to.
168  * @param       p_j2k                           J2K codec.
169  * @param       p_manager               the user event manager.
170 */
171 static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k,
172                                      opj_stream_private_t *p_stream,
173                                      opj_event_mgr_t * p_manager);
174 
175 /**
176  * Copies the decoding tile parameters onto all the tile parameters.
177  * Creates also the tile decoder.
178  */
179 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,
180         opj_stream_private_t *p_stream,
181         opj_event_mgr_t * p_manager);
182 
183 /**
184  * Destroys the memory associated with the decoding of headers.
185  */
186 static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,
187         opj_stream_private_t *p_stream,
188         opj_event_mgr_t * p_manager);
189 
190 /**
191  * Reads the lookup table containing all the marker, status and action, and returns the handler associated
192  * with the marker value.
193  * @param       p_id            Marker value to look up
194  *
195  * @return      the handler associated with the id.
196 */
197 static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler(
198     OPJ_UINT32 p_id);
199 
200 /**
201  * Destroys a tile coding parameter structure.
202  *
203  * @param       p_tcp           the tile coding parameter to destroy.
204  */
205 static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp);
206 
207 /**
208  * Destroys the data inside a tile coding parameter structure.
209  *
210  * @param       p_tcp           the tile coding parameter which contain data to destroy.
211  */
212 static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp);
213 
214 /**
215  * Destroys a coding parameter structure.
216  *
217  * @param       p_cp            the coding parameter to destroy.
218  */
219 static void opj_j2k_cp_destroy(opj_cp_t *p_cp);
220 
221 /**
222  * Compare 2 a SPCod/ SPCoc elements, i.e. the coding style of a given component of a tile.
223  *
224  * @param       p_j2k            J2K codec.
225  * @param       p_tile_no        Tile number
226  * @param       p_first_comp_no  The 1st component number to compare.
227  * @param       p_second_comp_no The 1st component number to compare.
228  *
229  * @return OPJ_TRUE if SPCdod are equals.
230  */
231 static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k,
232         OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
233 
234 /**
235  * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
236  *
237  * @param       p_j2k           J2K codec.
238  * @param       p_tile_no       FIXME DOC
239  * @param       p_comp_no       the component number to output.
240  * @param       p_data          FIXME DOC
241  * @param       p_header_size   FIXME DOC
242  * @param       p_manager       the user event manager.
243  *
244  * @return FIXME DOC
245 */
246 static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k,
247         OPJ_UINT32 p_tile_no,
248         OPJ_UINT32 p_comp_no,
249         OPJ_BYTE * p_data,
250         OPJ_UINT32 * p_header_size,
251         opj_event_mgr_t * p_manager);
252 
253 /**
254  * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
255  *
256  * @param       p_j2k                   the J2K codec.
257  * @param       p_tile_no               the tile index.
258  * @param       p_comp_no               the component being outputted.
259  *
260  * @return      the number of bytes taken by the SPCod element.
261  */
262 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k,
263         OPJ_UINT32 p_tile_no,
264         OPJ_UINT32 p_comp_no);
265 
266 /**
267  * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
268  * @param       p_j2k           the jpeg2000 codec.
269  * @param       compno          FIXME DOC
270  * @param       p_header_data   the data contained in the COM box.
271  * @param       p_header_size   the size of the data contained in the COM marker.
272  * @param       p_manager       the user event manager.
273 */
274 static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k,
275         OPJ_UINT32 compno,
276         OPJ_BYTE * p_header_data,
277         OPJ_UINT32 * p_header_size,
278         opj_event_mgr_t * p_manager);
279 
280 /**
281  * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
282  *
283  * @param       p_tile_no               the tile index.
284  * @param       p_comp_no               the component being outputted.
285  * @param       p_j2k                   the J2K codec.
286  *
287  * @return      the number of bytes taken by the SPCod element.
288  */
289 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k,
290         OPJ_UINT32 p_tile_no,
291         OPJ_UINT32 p_comp_no);
292 
293 /**
294  * Compares 2 SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
295  *
296  * @param       p_j2k                   J2K codec.
297  * @param       p_tile_no               the tile to output.
298  * @param       p_first_comp_no         the first component number to compare.
299  * @param       p_second_comp_no        the second component number to compare.
300  *
301  * @return OPJ_TRUE if equals.
302  */
303 static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k,
304         OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
305 
306 
307 /**
308  * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
309  *
310  * @param       p_tile_no               the tile to output.
311  * @param       p_comp_no               the component number to output.
312  * @param       p_data                  the data buffer.
313  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
314  * @param       p_j2k                   J2K codec.
315  * @param       p_manager               the user event manager.
316  *
317 */
318 static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
319                                         OPJ_UINT32 p_tile_no,
320                                         OPJ_UINT32 p_comp_no,
321                                         OPJ_BYTE * p_data,
322                                         OPJ_UINT32 * p_header_size,
323                                         opj_event_mgr_t * p_manager);
324 
325 /**
326  * Updates the Tile Length Marker.
327  */
328 static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);
329 
330 /**
331  * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
332  *
333  * @param       p_j2k           J2K codec.
334  * @param       compno          the component number to output.
335  * @param       p_header_data   the data buffer.
336  * @param       p_header_size   pointer to the size of the data buffer, it is changed by the function.
337  * @param       p_manager       the user event manager.
338  *
339 */
340 static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
341                                        OPJ_UINT32 compno,
342                                        OPJ_BYTE * p_header_data,
343                                        OPJ_UINT32 * p_header_size,
344                                        opj_event_mgr_t * p_manager);
345 
346 /**
347  * Copies the tile component parameters of all the component from the first tile component.
348  *
349  * @param               p_j2k           the J2k codec.
350  */
351 static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k);
352 
353 /**
354  * Copies the tile quantization parameters of all the component from the first tile component.
355  *
356  * @param               p_j2k           the J2k codec.
357  */
358 static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k);
359 
360 /**
361  * Reads the tiles.
362  */
363 static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
364                                      opj_stream_private_t *p_stream,
365                                      opj_event_mgr_t * p_manager);
366 
367 static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,
368                                        OPJ_UINT32 p_tile_index,
369                                        opj_stream_private_t *p_stream,
370                                        opj_event_mgr_t * p_manager);
371 
372 static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
373         opj_image_t* p_output_image);
374 
375 static void opj_get_tile_dimensions(opj_image_t * l_image,
376                                     opj_tcd_tilecomp_t * l_tilec,
377                                     opj_image_comp_t * l_img_comp,
378                                     OPJ_UINT32* l_size_comp,
379                                     OPJ_UINT32* l_width,
380                                     OPJ_UINT32* l_height,
381                                     OPJ_UINT32* l_offset_x,
382                                     OPJ_UINT32* l_offset_y,
383                                     OPJ_UINT32* l_image_width,
384                                     OPJ_UINT32* l_stride,
385                                     OPJ_UINT32* l_tile_offset);
386 
387 static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
388 
389 static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k,
390                                         opj_stream_private_t *p_stream,
391                                         opj_event_mgr_t * p_manager);
392 
393 /**
394  * Sets up the procedures to do on writing header.
395  * Developers wanting to extend the library can add their own writing procedures.
396  */
397 static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k,
398         opj_event_mgr_t * p_manager);
399 
400 static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k,
401         OPJ_BYTE * p_data,
402         OPJ_UINT32 * p_data_written,
403         OPJ_UINT32 p_total_data_size,
404         opj_stream_private_t *p_stream,
405         struct opj_event_mgr * p_manager);
406 
407 static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k,
408         OPJ_BYTE * p_data,
409         OPJ_UINT32 * p_data_written,
410         OPJ_UINT32 p_total_data_size,
411         opj_stream_private_t *p_stream,
412         struct opj_event_mgr * p_manager);
413 
414 /**
415  * Gets the offset of the header.
416  *
417  * @param       p_stream                the stream to write data to.
418  * @param       p_j2k                   J2K codec.
419  * @param       p_manager               the user event manager.
420 */
421 static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
422                                        opj_stream_private_t *p_stream,
423                                        opj_event_mgr_t * p_manager);
424 
425 static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
426 
427 /*
428  * -----------------------------------------------------------------------
429  * -----------------------------------------------------------------------
430  * -----------------------------------------------------------------------
431  */
432 
433 /**
434  * Writes the SOC marker (Start Of Codestream)
435  *
436  * @param       p_stream                        the stream to write data to.
437  * @param       p_j2k                   J2K codec.
438  * @param       p_manager       the user event manager.
439 */
440 static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k,
441                                   opj_stream_private_t *p_stream,
442                                   opj_event_mgr_t * p_manager);
443 
444 /**
445  * Reads a SOC marker (Start of Codestream)
446  * @param       p_j2k           the jpeg2000 file codec.
447  * @param       p_stream        XXX needs data
448  * @param       p_manager       the user event manager.
449 */
450 static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k,
451                                  opj_stream_private_t *p_stream,
452                                  opj_event_mgr_t * p_manager);
453 
454 /**
455  * Writes the SIZ marker (image and tile size)
456  *
457  * @param       p_j2k           J2K codec.
458  * @param       p_stream        the stream to write data to.
459  * @param       p_manager       the user event manager.
460 */
461 static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k,
462                                   opj_stream_private_t *p_stream,
463                                   opj_event_mgr_t * p_manager);
464 
465 /**
466  * Reads a SIZ marker (image and tile size)
467  * @param       p_j2k           the jpeg2000 file codec.
468  * @param       p_header_data   the data contained in the SIZ box.
469  * @param       p_header_size   the size of the data contained in the SIZ marker.
470  * @param       p_manager       the user event manager.
471 */
472 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
473                                  OPJ_BYTE * p_header_data,
474                                  OPJ_UINT32 p_header_size,
475                                  opj_event_mgr_t * p_manager);
476 
477 /**
478  * Writes the COM marker (comment)
479  *
480  * @param       p_stream                        the stream to write data to.
481  * @param       p_j2k                   J2K codec.
482  * @param       p_manager       the user event manager.
483 */
484 static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k,
485                                   opj_stream_private_t *p_stream,
486                                   opj_event_mgr_t * p_manager);
487 
488 /**
489  * Reads a COM marker (comments)
490  * @param       p_j2k           the jpeg2000 file codec.
491  * @param       p_header_data   the data contained in the COM box.
492  * @param       p_header_size   the size of the data contained in the COM marker.
493  * @param       p_manager       the user event manager.
494 */
495 static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k,
496                                  OPJ_BYTE * p_header_data,
497                                  OPJ_UINT32 p_header_size,
498                                  opj_event_mgr_t * p_manager);
499 /**
500  * Writes the COD marker (Coding style default)
501  *
502  * @param       p_stream                        the stream to write data to.
503  * @param       p_j2k                   J2K codec.
504  * @param       p_manager       the user event manager.
505 */
506 static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k,
507                                   opj_stream_private_t *p_stream,
508                                   opj_event_mgr_t * p_manager);
509 
510 /**
511  * Reads a COD marker (Coding style defaults)
512  * @param       p_header_data   the data contained in the COD box.
513  * @param       p_j2k                   the jpeg2000 codec.
514  * @param       p_header_size   the size of the data contained in the COD marker.
515  * @param       p_manager               the user event manager.
516 */
517 static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k,
518                                  OPJ_BYTE * p_header_data,
519                                  OPJ_UINT32 p_header_size,
520                                  opj_event_mgr_t * p_manager);
521 
522 /**
523  * Compares 2 COC markers (Coding style component)
524  *
525  * @param       p_j2k            J2K codec.
526  * @param       p_first_comp_no  the index of the first component to compare.
527  * @param       p_second_comp_no the index of the second component to compare.
528  *
529  * @return      OPJ_TRUE if equals
530  */
531 static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k,
532                                     OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
533 
534 /**
535  * Writes the COC marker (Coding style component)
536  *
537  * @param       p_j2k       J2K codec.
538  * @param       p_comp_no   the index of the component to output.
539  * @param       p_stream    the stream to write data to.
540  * @param       p_manager   the user event manager.
541 */
542 static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k,
543                                   OPJ_UINT32 p_comp_no,
544                                   opj_stream_private_t *p_stream,
545                                   opj_event_mgr_t * p_manager);
546 
547 /**
548  * Writes the COC marker (Coding style component)
549  *
550  * @param       p_j2k                   J2K codec.
551  * @param       p_comp_no               the index of the component to output.
552  * @param       p_data          FIXME DOC
553  * @param       p_data_written  FIXME DOC
554  * @param       p_manager               the user event manager.
555 */
556 static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
557                                         OPJ_UINT32 p_comp_no,
558                                         OPJ_BYTE * p_data,
559                                         OPJ_UINT32 * p_data_written,
560                                         opj_event_mgr_t * p_manager);
561 
562 /**
563  * Gets the maximum size taken by a coc.
564  *
565  * @param       p_j2k   the jpeg2000 codec to use.
566  */
567 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
568 
569 /**
570  * Reads a COC marker (Coding Style Component)
571  * @param       p_header_data   the data contained in the COC box.
572  * @param       p_j2k                   the jpeg2000 codec.
573  * @param       p_header_size   the size of the data contained in the COC marker.
574  * @param       p_manager               the user event manager.
575 */
576 static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k,
577                                  OPJ_BYTE * p_header_data,
578                                  OPJ_UINT32 p_header_size,
579                                  opj_event_mgr_t * p_manager);
580 
581 /**
582  * Writes the QCD marker (quantization default)
583  *
584  * @param       p_j2k                   J2K codec.
585  * @param       p_stream                the stream to write data to.
586  * @param       p_manager               the user event manager.
587 */
588 static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k,
589                                   opj_stream_private_t *p_stream,
590                                   opj_event_mgr_t * p_manager);
591 
592 /**
593  * Reads a QCD marker (Quantization defaults)
594  * @param       p_header_data   the data contained in the QCD box.
595  * @param       p_j2k                   the jpeg2000 codec.
596  * @param       p_header_size   the size of the data contained in the QCD marker.
597  * @param       p_manager               the user event manager.
598 */
599 static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k,
600                                  OPJ_BYTE * p_header_data,
601                                  OPJ_UINT32 p_header_size,
602                                  opj_event_mgr_t * p_manager);
603 
604 /**
605  * Compare QCC markers (quantization component)
606  *
607  * @param       p_j2k                 J2K codec.
608  * @param       p_first_comp_no       the index of the first component to compare.
609  * @param       p_second_comp_no      the index of the second component to compare.
610  *
611  * @return OPJ_TRUE if equals.
612  */
613 static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k,
614                                     OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
615 
616 /**
617  * Writes the QCC marker (quantization component)
618  *
619  * @param       p_comp_no       the index of the component to output.
620  * @param       p_stream                the stream to write data to.
621  * @param       p_j2k                   J2K codec.
622  * @param       p_manager               the user event manager.
623 */
624 static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k,
625                                   OPJ_UINT32 p_comp_no,
626                                   opj_stream_private_t *p_stream,
627                                   opj_event_mgr_t * p_manager);
628 
629 /**
630  * Writes the QCC marker (quantization component)
631  *
632  * @param       p_j2k           J2K codec.
633  * @param       p_comp_no       the index of the component to output.
634  * @param       p_data          FIXME DOC
635  * @param       p_data_written  the stream to write data to.
636  * @param       p_manager       the user event manager.
637 */
638 static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
639                                         OPJ_UINT32 p_comp_no,
640                                         OPJ_BYTE * p_data,
641                                         OPJ_UINT32 * p_data_written,
642                                         opj_event_mgr_t * p_manager);
643 
644 /**
645  * Gets the maximum size taken by a qcc.
646  */
647 static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k);
648 
649 /**
650  * Reads a QCC marker (Quantization component)
651  * @param       p_header_data   the data contained in the QCC box.
652  * @param       p_j2k                   the jpeg2000 codec.
653  * @param       p_header_size   the size of the data contained in the QCC marker.
654  * @param       p_manager               the user event manager.
655 */
656 static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k,
657                                  OPJ_BYTE * p_header_data,
658                                  OPJ_UINT32 p_header_size,
659                                  opj_event_mgr_t * p_manager);
660 /**
661  * Writes the POC marker (Progression Order Change)
662  *
663  * @param       p_stream                                the stream to write data to.
664  * @param       p_j2k                           J2K codec.
665  * @param       p_manager               the user event manager.
666 */
667 static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k,
668                                   opj_stream_private_t *p_stream,
669                                   opj_event_mgr_t * p_manager);
670 /**
671  * Writes the POC marker (Progression Order Change)
672  *
673  * @param       p_j2k          J2K codec.
674  * @param       p_data         FIXME DOC
675  * @param       p_data_written the stream to write data to.
676  * @param       p_manager      the user event manager.
677  */
678 static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
679                                         OPJ_BYTE * p_data,
680                                         OPJ_UINT32 * p_data_written,
681                                         opj_event_mgr_t * p_manager);
682 /**
683  * Gets the maximum size taken by the writing of a POC.
684  */
685 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
686 
687 /**
688  * Reads a POC marker (Progression Order Change)
689  *
690  * @param       p_header_data   the data contained in the POC box.
691  * @param       p_j2k                   the jpeg2000 codec.
692  * @param       p_header_size   the size of the data contained in the POC marker.
693  * @param       p_manager               the user event manager.
694 */
695 static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k,
696                                  OPJ_BYTE * p_header_data,
697                                  OPJ_UINT32 p_header_size,
698                                  opj_event_mgr_t * p_manager);
699 
700 /**
701  * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
702  */
703 static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k);
704 
705 /**
706  * Gets the maximum size taken by the headers of the SOT.
707  *
708  * @param       p_j2k   the jpeg2000 codec to use.
709  */
710 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
711 
712 /**
713  * Reads a CRG marker (Component registration)
714  *
715  * @param       p_header_data   the data contained in the TLM box.
716  * @param       p_j2k                   the jpeg2000 codec.
717  * @param       p_header_size   the size of the data contained in the TLM marker.
718  * @param       p_manager               the user event manager.
719 */
720 static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k,
721                                  OPJ_BYTE * p_header_data,
722                                  OPJ_UINT32 p_header_size,
723                                  opj_event_mgr_t * p_manager);
724 /**
725  * Reads a TLM marker (Tile Length Marker)
726  *
727  * @param       p_header_data   the data contained in the TLM box.
728  * @param       p_j2k                   the jpeg2000 codec.
729  * @param       p_header_size   the size of the data contained in the TLM marker.
730  * @param       p_manager               the user event manager.
731 */
732 static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k,
733                                  OPJ_BYTE * p_header_data,
734                                  OPJ_UINT32 p_header_size,
735                                  opj_event_mgr_t * p_manager);
736 
737 /**
738  * Writes the updated tlm.
739  *
740  * @param       p_stream                the stream to write data to.
741  * @param       p_j2k                   J2K codec.
742  * @param       p_manager               the user event manager.
743 */
744 static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k,
745         opj_stream_private_t *p_stream,
746         opj_event_mgr_t * p_manager);
747 
748 /**
749  * Reads a PLM marker (Packet length, main header marker)
750  *
751  * @param       p_header_data   the data contained in the TLM box.
752  * @param       p_j2k                   the jpeg2000 codec.
753  * @param       p_header_size   the size of the data contained in the TLM marker.
754  * @param       p_manager               the user event manager.
755 */
756 static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k,
757                                  OPJ_BYTE * p_header_data,
758                                  OPJ_UINT32 p_header_size,
759                                  opj_event_mgr_t * p_manager);
760 /**
761  * Reads a PLT marker (Packet length, tile-part header)
762  *
763  * @param       p_header_data   the data contained in the PLT box.
764  * @param       p_j2k                   the jpeg2000 codec.
765  * @param       p_header_size   the size of the data contained in the PLT marker.
766  * @param       p_manager               the user event manager.
767 */
768 static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k,
769                                  OPJ_BYTE * p_header_data,
770                                  OPJ_UINT32 p_header_size,
771                                  opj_event_mgr_t * p_manager);
772 
773 /**
774  * Reads a PPM marker (Packed headers, main header)
775  *
776  * @param       p_header_data   the data contained in the POC box.
777  * @param       p_j2k                   the jpeg2000 codec.
778  * @param       p_header_size   the size of the data contained in the POC marker.
779  * @param       p_manager               the user event manager.
780  */
781 
782 static OPJ_BOOL opj_j2k_read_ppm(
783     opj_j2k_t *p_j2k,
784     OPJ_BYTE * p_header_data,
785     OPJ_UINT32 p_header_size,
786     opj_event_mgr_t * p_manager);
787 
788 /**
789  * Merges all PPM markers read (Packed headers, main header)
790  *
791  * @param       p_cp      main coding parameters.
792  * @param       p_manager the user event manager.
793  */
794 static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager);
795 
796 /**
797  * Reads a PPT marker (Packed packet headers, tile-part header)
798  *
799  * @param       p_header_data   the data contained in the PPT box.
800  * @param       p_j2k                   the jpeg2000 codec.
801  * @param       p_header_size   the size of the data contained in the PPT marker.
802  * @param       p_manager               the user event manager.
803 */
804 static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k,
805                                  OPJ_BYTE * p_header_data,
806                                  OPJ_UINT32 p_header_size,
807                                  opj_event_mgr_t * p_manager);
808 
809 /**
810  * Merges all PPT markers read (Packed headers, tile-part header)
811  *
812  * @param       p_tcp   the tile.
813  * @param       p_manager               the user event manager.
814  */
815 static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp,
816                                   opj_event_mgr_t * p_manager);
817 
818 
819 /**
820  * Writes the TLM marker (Tile Length Marker)
821  *
822  * @param       p_stream                                the stream to write data to.
823  * @param       p_j2k                           J2K codec.
824  * @param       p_manager               the user event manager.
825 */
826 static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k,
827                                   opj_stream_private_t *p_stream,
828                                   opj_event_mgr_t * p_manager);
829 
830 /**
831  * Writes the SOT marker (Start of tile-part)
832  *
833  * @param       p_j2k            J2K codec.
834  * @param       p_data           Output buffer
835  * @param       p_total_data_size Output buffer size
836  * @param       p_data_written   Number of bytes written into stream
837  * @param       p_stream         the stream to write data to.
838  * @param       p_manager        the user event manager.
839 */
840 static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
841                                   OPJ_BYTE * p_data,
842                                   OPJ_UINT32 p_total_data_size,
843                                   OPJ_UINT32 * p_data_written,
844                                   const opj_stream_private_t *p_stream,
845                                   opj_event_mgr_t * p_manager);
846 
847 /**
848  * Reads values from a SOT marker (Start of tile-part)
849  *
850  * the j2k decoder state is not affected. No side effects, no checks except for p_header_size.
851  *
852  * @param       p_header_data   the data contained in the SOT marker.
853  * @param       p_header_size   the size of the data contained in the SOT marker.
854  * @param       p_tile_no       Isot.
855  * @param       p_tot_len       Psot.
856  * @param       p_current_part  TPsot.
857  * @param       p_num_parts     TNsot.
858  * @param       p_manager       the user event manager.
859  */
860 static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE *  p_header_data,
861                                        OPJ_UINT32  p_header_size,
862                                        OPJ_UINT32* p_tile_no,
863                                        OPJ_UINT32* p_tot_len,
864                                        OPJ_UINT32* p_current_part,
865                                        OPJ_UINT32* p_num_parts,
866                                        opj_event_mgr_t * p_manager);
867 /**
868  * Reads a SOT marker (Start of tile-part)
869  *
870  * @param       p_header_data   the data contained in the SOT marker.
871  * @param       p_j2k           the jpeg2000 codec.
872  * @param       p_header_size   the size of the data contained in the PPT marker.
873  * @param       p_manager       the user event manager.
874 */
875 static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k,
876                                  OPJ_BYTE * p_header_data,
877                                  OPJ_UINT32 p_header_size,
878                                  opj_event_mgr_t * p_manager);
879 /**
880  * Writes the SOD marker (Start of data)
881  *
882  * @param       p_j2k               J2K codec.
883  * @param       p_tile_coder        FIXME DOC
884  * @param       p_data              FIXME DOC
885  * @param       p_data_written      FIXME DOC
886  * @param       p_total_data_size   FIXME DOC
887  * @param       p_stream            the stream to write data to.
888  * @param       p_manager           the user event manager.
889 */
890 static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
891                                   opj_tcd_t * p_tile_coder,
892                                   OPJ_BYTE * p_data,
893                                   OPJ_UINT32 * p_data_written,
894                                   OPJ_UINT32 p_total_data_size,
895                                   const opj_stream_private_t *p_stream,
896                                   opj_event_mgr_t * p_manager);
897 
898 /**
899  * Reads a SOD marker (Start Of Data)
900  *
901  * @param       p_j2k                   the jpeg2000 codec.
902  * @param       p_stream                FIXME DOC
903  * @param       p_manager               the user event manager.
904 */
905 static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
906                                  opj_stream_private_t *p_stream,
907                                  opj_event_mgr_t * p_manager);
908 
opj_j2k_update_tlm(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_part_size)909 static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size)
910 {
911     opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
912                     p_j2k->m_current_tile_number, 1);           /* PSOT */
913     ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
914 
915     opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
916                     p_tile_part_size, 4);                                       /* PSOT */
917     p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
918 }
919 
920 /**
921  * Writes the RGN marker (Region Of Interest)
922  *
923  * @param       p_tile_no               the tile to output
924  * @param       p_comp_no               the component to output
925  * @param       nb_comps                the number of components
926  * @param       p_stream                the stream to write data to.
927  * @param       p_j2k                   J2K codec.
928  * @param       p_manager               the user event manager.
929 */
930 static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
931                                   OPJ_UINT32 p_tile_no,
932                                   OPJ_UINT32 p_comp_no,
933                                   OPJ_UINT32 nb_comps,
934                                   opj_stream_private_t *p_stream,
935                                   opj_event_mgr_t * p_manager);
936 
937 /**
938  * Reads a RGN marker (Region Of Interest)
939  *
940  * @param       p_header_data   the data contained in the POC box.
941  * @param       p_j2k                   the jpeg2000 codec.
942  * @param       p_header_size   the size of the data contained in the POC marker.
943  * @param       p_manager               the user event manager.
944 */
945 static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k,
946                                  OPJ_BYTE * p_header_data,
947                                  OPJ_UINT32 p_header_size,
948                                  opj_event_mgr_t * p_manager);
949 
950 /**
951  * Writes the EOC marker (End of Codestream)
952  *
953  * @param       p_stream                the stream to write data to.
954  * @param       p_j2k                   J2K codec.
955  * @param       p_manager               the user event manager.
956 */
957 static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k,
958                                   opj_stream_private_t *p_stream,
959                                   opj_event_mgr_t * p_manager);
960 
961 #if 0
962 /**
963  * Reads a EOC marker (End Of Codestream)
964  *
965  * @param       p_j2k                   the jpeg2000 codec.
966  * @param       p_stream                FIXME DOC
967  * @param       p_manager               the user event manager.
968 */
969 static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k,
970                                  opj_stream_private_t *p_stream,
971                                  opj_event_mgr_t * p_manager);
972 #endif
973 
974 /**
975  * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
976  *
977  * @param       p_stream                        the stream to write data to.
978  * @param       p_j2k                   J2K codec.
979  * @param       p_manager       the user event manager.
980 */
981 static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k,
982         opj_stream_private_t *p_stream,
983         opj_event_mgr_t * p_manager);
984 
985 /**
986  * Inits the Info
987  *
988  * @param       p_stream                the stream to write data to.
989  * @param       p_j2k                   J2K codec.
990  * @param       p_manager               the user event manager.
991 */
992 static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k,
993                                   opj_stream_private_t *p_stream,
994                                   opj_event_mgr_t * p_manager);
995 
996 /**
997 Add main header marker information
998 @param cstr_index    Codestream information structure
999 @param type         marker type
1000 @param pos          byte offset of marker segment
1001 @param len          length of marker segment
1002  */
1003 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index,
1004                                      OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1005 /**
1006 Add tile header marker information
1007 @param tileno       tile index number
1008 @param cstr_index   Codestream information structure
1009 @param type         marker type
1010 @param pos          byte offset of marker segment
1011 @param len          length of marker segment
1012  */
1013 static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno,
1014                                      opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos,
1015                                      OPJ_UINT32 len);
1016 
1017 /**
1018  * Reads an unknown marker
1019  *
1020  * @param       p_j2k                   the jpeg2000 codec.
1021  * @param       p_stream                the stream object to read from.
1022  * @param       output_marker           FIXME DOC
1023  * @param       p_manager               the user event manager.
1024  *
1025  * @return      true                    if the marker could be deduced.
1026 */
1027 static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
1028                                  opj_stream_private_t *p_stream,
1029                                  OPJ_UINT32 *output_marker,
1030                                  opj_event_mgr_t * p_manager);
1031 
1032 /**
1033  * Writes the MCT marker (Multiple Component Transform)
1034  *
1035  * @param       p_j2k           J2K codec.
1036  * @param       p_mct_record    FIXME DOC
1037  * @param       p_stream        the stream to write data to.
1038  * @param       p_manager       the user event manager.
1039 */
1040 static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k,
1041         opj_mct_data_t * p_mct_record,
1042         opj_stream_private_t *p_stream,
1043         opj_event_mgr_t * p_manager);
1044 
1045 /**
1046  * Reads a MCT marker (Multiple Component Transform)
1047  *
1048  * @param       p_header_data   the data contained in the MCT box.
1049  * @param       p_j2k                   the jpeg2000 codec.
1050  * @param       p_header_size   the size of the data contained in the MCT marker.
1051  * @param       p_manager               the user event manager.
1052 */
1053 static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k,
1054                                  OPJ_BYTE * p_header_data,
1055                                  OPJ_UINT32 p_header_size,
1056                                  opj_event_mgr_t * p_manager);
1057 
1058 /**
1059  * Writes the MCC marker (Multiple Component Collection)
1060  *
1061  * @param       p_j2k                   J2K codec.
1062  * @param       p_mcc_record            FIXME DOC
1063  * @param       p_stream                the stream to write data to.
1064  * @param       p_manager               the user event manager.
1065 */
1066 static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k,
1067         opj_simple_mcc_decorrelation_data_t * p_mcc_record,
1068         opj_stream_private_t *p_stream,
1069         opj_event_mgr_t * p_manager);
1070 
1071 /**
1072  * Reads a MCC marker (Multiple Component Collection)
1073  *
1074  * @param       p_header_data   the data contained in the MCC box.
1075  * @param       p_j2k                   the jpeg2000 codec.
1076  * @param       p_header_size   the size of the data contained in the MCC marker.
1077  * @param       p_manager               the user event manager.
1078 */
1079 static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k,
1080                                  OPJ_BYTE * p_header_data,
1081                                  OPJ_UINT32 p_header_size,
1082                                  opj_event_mgr_t * p_manager);
1083 
1084 /**
1085  * Writes the MCO marker (Multiple component transformation ordering)
1086  *
1087  * @param       p_stream                                the stream to write data to.
1088  * @param       p_j2k                           J2K codec.
1089  * @param       p_manager               the user event manager.
1090 */
1091 static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k,
1092                                   opj_stream_private_t *p_stream,
1093                                   opj_event_mgr_t * p_manager);
1094 
1095 /**
1096  * Reads a MCO marker (Multiple Component Transform Ordering)
1097  *
1098  * @param       p_header_data   the data contained in the MCO box.
1099  * @param       p_j2k                   the jpeg2000 codec.
1100  * @param       p_header_size   the size of the data contained in the MCO marker.
1101  * @param       p_manager               the user event manager.
1102 */
1103 static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k,
1104                                  OPJ_BYTE * p_header_data,
1105                                  OPJ_UINT32 p_header_size,
1106                                  opj_event_mgr_t * p_manager);
1107 
1108 static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image,
1109                                 OPJ_UINT32 p_index);
1110 
1111 static void  opj_j2k_read_int16_to_float(const void * p_src_data,
1112         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1113 static void  opj_j2k_read_int32_to_float(const void * p_src_data,
1114         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1115 static void  opj_j2k_read_float32_to_float(const void * p_src_data,
1116         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1117 static void  opj_j2k_read_float64_to_float(const void * p_src_data,
1118         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1119 
1120 static void  opj_j2k_read_int16_to_int32(const void * p_src_data,
1121         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1122 static void  opj_j2k_read_int32_to_int32(const void * p_src_data,
1123         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1124 static void  opj_j2k_read_float32_to_int32(const void * p_src_data,
1125         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1126 static void  opj_j2k_read_float64_to_int32(const void * p_src_data,
1127         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1128 
1129 static void  opj_j2k_write_float_to_int16(const void * p_src_data,
1130         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1131 static void  opj_j2k_write_float_to_int32(const void * p_src_data,
1132         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1133 static void  opj_j2k_write_float_to_float(const void * p_src_data,
1134         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1135 static void  opj_j2k_write_float_to_float64(const void * p_src_data,
1136         void * p_dest_data, OPJ_UINT32 p_nb_elem);
1137 
1138 /**
1139  * Ends the encoding, i.e. frees memory.
1140  *
1141  * @param       p_stream                the stream to write data to.
1142  * @param       p_j2k                   J2K codec.
1143  * @param       p_manager               the user event manager.
1144 */
1145 static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k,
1146                                      opj_stream_private_t *p_stream,
1147                                      opj_event_mgr_t * p_manager);
1148 
1149 /**
1150  * Writes the CBD marker (Component bit depth definition)
1151  *
1152  * @param       p_stream                                the stream to write data to.
1153  * @param       p_j2k                           J2K codec.
1154  * @param       p_manager               the user event manager.
1155 */
1156 static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k,
1157                                   opj_stream_private_t *p_stream,
1158                                   opj_event_mgr_t * p_manager);
1159 
1160 /**
1161  * Reads a CBD marker (Component bit depth definition)
1162  * @param       p_header_data   the data contained in the CBD box.
1163  * @param       p_j2k                   the jpeg2000 codec.
1164  * @param       p_header_size   the size of the data contained in the CBD marker.
1165  * @param       p_manager               the user event manager.
1166 */
1167 static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k,
1168                                  OPJ_BYTE * p_header_data,
1169                                  OPJ_UINT32 p_header_size,
1170                                  opj_event_mgr_t * p_manager);
1171 
1172 
1173 /**
1174  * Writes COC marker for each component.
1175  *
1176  * @param       p_stream                the stream to write data to.
1177  * @param       p_j2k                   J2K codec.
1178  * @param       p_manager               the user event manager.
1179 */
1180 static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
1181                                       opj_stream_private_t *p_stream,
1182                                       opj_event_mgr_t * p_manager);
1183 
1184 /**
1185  * Writes QCC marker for each component.
1186  *
1187  * @param       p_stream                the stream to write data to.
1188  * @param       p_j2k                   J2K codec.
1189  * @param       p_manager               the user event manager.
1190 */
1191 static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
1192                                       opj_stream_private_t *p_stream,
1193                                       opj_event_mgr_t * p_manager);
1194 
1195 /**
1196  * Writes regions of interests.
1197  *
1198  * @param       p_stream                the stream to write data to.
1199  * @param       p_j2k                   J2K codec.
1200  * @param       p_manager               the user event manager.
1201 */
1202 static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k,
1203                                       opj_stream_private_t *p_stream,
1204                                       opj_event_mgr_t * p_manager);
1205 
1206 /**
1207  * Writes EPC ????
1208  *
1209  * @param       p_stream                the stream to write data to.
1210  * @param       p_j2k                   J2K codec.
1211  * @param       p_manager               the user event manager.
1212 */
1213 static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k,
1214                                   opj_stream_private_t *p_stream,
1215                                   opj_event_mgr_t * p_manager);
1216 
1217 /**
1218  * Checks the progression order changes values. Tells of the poc given as input are valid.
1219  * A nice message is outputted at errors.
1220  *
1221  * @param       p_pocs                  the progression order changes.
1222  * @param       p_nb_pocs               the number of progression order changes.
1223  * @param       p_nb_resolutions        the number of resolutions.
1224  * @param       numcomps                the number of components
1225  * @param       numlayers               the number of layers.
1226  * @param       p_manager               the user event manager.
1227  *
1228  * @return      true if the pocs are valid.
1229  */
1230 static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs,
1231                                       OPJ_UINT32 p_nb_pocs,
1232                                       OPJ_UINT32 p_nb_resolutions,
1233                                       OPJ_UINT32 numcomps,
1234                                       OPJ_UINT32 numlayers,
1235                                       opj_event_mgr_t * p_manager);
1236 
1237 /**
1238  * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1239  *
1240  * @param               cp                      the coding parameters.
1241  * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1242  * @param               tileno          the given tile.
1243  *
1244  * @return              the number of tile parts.
1245  */
1246 static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino,
1247                                      OPJ_UINT32 tileno);
1248 
1249 /**
1250  * Calculates the total number of tile parts needed by the encoder to
1251  * encode such an image. If not enough memory is available, then the function return false.
1252  *
1253  * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1254  * @param       cp                      the coding parameters for the image.
1255  * @param       image           the image to encode.
1256  * @param       p_j2k                   the p_j2k encoder.
1257  * @param       p_manager       the user event manager.
1258  *
1259  * @return true if the function was successful, false else.
1260  */
1261 static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k,
1262                                      opj_cp_t *cp,
1263                                      OPJ_UINT32 * p_nb_tiles,
1264                                      opj_image_t *image,
1265                                      opj_event_mgr_t * p_manager);
1266 
1267 static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
1268 
1269 static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
1270 
1271 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1272 
1273 static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp);
1274 
1275 static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp);
1276 
1277 static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
1278 
1279 static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters,
1280         opj_image_t *image, opj_event_mgr_t *p_manager);
1281 
1282 static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
1283         opj_event_mgr_t *p_manager);
1284 
1285 /**
1286  * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254.
1287  *
1288  * @param       p_stream            the stream to read data from.
1289  * @param       tile_no             tile number we're looking for.
1290  * @param       p_correction_needed output value. if true, non conformant codestream needs TNsot correction.
1291  * @param       p_manager       the user event manager.
1292  *
1293  * @return true if the function was successful, false else.
1294  */
1295 static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t
1296         *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed,
1297         opj_event_mgr_t * p_manager);
1298 
1299 /*@}*/
1300 
1301 /*@}*/
1302 
1303 /* ----------------------------------------------------------------------- */
1304 typedef struct j2k_prog_order {
1305     OPJ_PROG_ORDER enum_prog;
1306     char str_prog[5];
1307 } j2k_prog_order_t;
1308 
1309 static const j2k_prog_order_t j2k_prog_order_list[] = {
1310     {OPJ_CPRL, "CPRL"},
1311     {OPJ_LRCP, "LRCP"},
1312     {OPJ_PCRL, "PCRL"},
1313     {OPJ_RLCP, "RLCP"},
1314     {OPJ_RPCL, "RPCL"},
1315     {(OPJ_PROG_ORDER) - 1, ""}
1316 };
1317 
1318 /**
1319  * FIXME DOC
1320  */
1321 static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = {
1322     2,
1323     4,
1324     4,
1325     8
1326 };
1327 
1328 typedef void (* opj_j2k_mct_function)(const void * p_src_data,
1329                                       void * p_dest_data, OPJ_UINT32 p_nb_elem);
1330 
1331 static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = {
1332     opj_j2k_read_int16_to_float,
1333     opj_j2k_read_int32_to_float,
1334     opj_j2k_read_float32_to_float,
1335     opj_j2k_read_float64_to_float
1336 };
1337 
1338 static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = {
1339     opj_j2k_read_int16_to_int32,
1340     opj_j2k_read_int32_to_int32,
1341     opj_j2k_read_float32_to_int32,
1342     opj_j2k_read_float64_to_int32
1343 };
1344 
1345 static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = {
1346     opj_j2k_write_float_to_int16,
1347     opj_j2k_write_float_to_int32,
1348     opj_j2k_write_float_to_float,
1349     opj_j2k_write_float_to_float64
1350 };
1351 
1352 typedef struct opj_dec_memory_marker_handler {
1353     /** marker value */
1354     OPJ_UINT32 id;
1355     /** value of the state when the marker can appear */
1356     OPJ_UINT32 states;
1357     /** action linked to the marker */
1358     OPJ_BOOL(*handler)(opj_j2k_t *p_j2k,
1359                        OPJ_BYTE * p_header_data,
1360                        OPJ_UINT32 p_header_size,
1361                        opj_event_mgr_t * p_manager);
1362 }
1363 opj_dec_memory_marker_handler_t;
1364 
1365 static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1366 {
1367     {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1368     {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1369     {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1370     {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1371     {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1372     {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1373     {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1374     {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1375     {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1376     {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1377     {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1378     {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm},
1379     {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1380     {J2K_MS_SOP, 0, 0},
1381     {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1382     {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1383     {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1384     {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd},
1385     {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1386     {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1387 #ifdef USE_JPWL
1388 #ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
1389     {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1390     {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1391     {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1392     {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1393 #endif
1394 #endif /* USE_JPWL */
1395 #ifdef USE_JPSEC
1396     {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1397     {J2K_MS_INSEC, 0, j2k_read_insec}
1398 #endif /* USE_JPSEC */
1399     {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1400 };
1401 
opj_j2k_read_int16_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1402 static void  opj_j2k_read_int16_to_float(const void * p_src_data,
1403         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1404 {
1405     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1406     OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1407     OPJ_UINT32 i;
1408     OPJ_UINT32 l_temp;
1409 
1410     for (i = 0; i < p_nb_elem; ++i) {
1411         opj_read_bytes(l_src_data, &l_temp, 2);
1412 
1413         l_src_data += sizeof(OPJ_INT16);
1414 
1415         *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1416     }
1417 }
1418 
opj_j2k_read_int32_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1419 static void  opj_j2k_read_int32_to_float(const void * p_src_data,
1420         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1421 {
1422     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1423     OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1424     OPJ_UINT32 i;
1425     OPJ_UINT32 l_temp;
1426 
1427     for (i = 0; i < p_nb_elem; ++i) {
1428         opj_read_bytes(l_src_data, &l_temp, 4);
1429 
1430         l_src_data += sizeof(OPJ_INT32);
1431 
1432         *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1433     }
1434 }
1435 
opj_j2k_read_float32_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1436 static void  opj_j2k_read_float32_to_float(const void * p_src_data,
1437         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1438 {
1439     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1440     OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1441     OPJ_UINT32 i;
1442     OPJ_FLOAT32 l_temp;
1443 
1444     for (i = 0; i < p_nb_elem; ++i) {
1445         opj_read_float(l_src_data, &l_temp);
1446 
1447         l_src_data += sizeof(OPJ_FLOAT32);
1448 
1449         *(l_dest_data++) = l_temp;
1450     }
1451 }
1452 
opj_j2k_read_float64_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1453 static void  opj_j2k_read_float64_to_float(const void * p_src_data,
1454         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1455 {
1456     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1457     OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1458     OPJ_UINT32 i;
1459     OPJ_FLOAT64 l_temp;
1460 
1461     for (i = 0; i < p_nb_elem; ++i) {
1462         opj_read_double(l_src_data, &l_temp);
1463 
1464         l_src_data += sizeof(OPJ_FLOAT64);
1465 
1466         *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1467     }
1468 }
1469 
opj_j2k_read_int16_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1470 static void  opj_j2k_read_int16_to_int32(const void * p_src_data,
1471         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1472 {
1473     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1474     OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1475     OPJ_UINT32 i;
1476     OPJ_UINT32 l_temp;
1477 
1478     for (i = 0; i < p_nb_elem; ++i) {
1479         opj_read_bytes(l_src_data, &l_temp, 2);
1480 
1481         l_src_data += sizeof(OPJ_INT16);
1482 
1483         *(l_dest_data++) = (OPJ_INT32) l_temp;
1484     }
1485 }
1486 
opj_j2k_read_int32_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1487 static void  opj_j2k_read_int32_to_int32(const void * p_src_data,
1488         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1489 {
1490     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1491     OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1492     OPJ_UINT32 i;
1493     OPJ_UINT32 l_temp;
1494 
1495     for (i = 0; i < p_nb_elem; ++i) {
1496         opj_read_bytes(l_src_data, &l_temp, 4);
1497 
1498         l_src_data += sizeof(OPJ_INT32);
1499 
1500         *(l_dest_data++) = (OPJ_INT32) l_temp;
1501     }
1502 }
1503 
opj_j2k_read_float32_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1504 static void  opj_j2k_read_float32_to_int32(const void * p_src_data,
1505         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1506 {
1507     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1508     OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1509     OPJ_UINT32 i;
1510     OPJ_FLOAT32 l_temp;
1511 
1512     for (i = 0; i < p_nb_elem; ++i) {
1513         opj_read_float(l_src_data, &l_temp);
1514 
1515         l_src_data += sizeof(OPJ_FLOAT32);
1516 
1517         *(l_dest_data++) = (OPJ_INT32) l_temp;
1518     }
1519 }
1520 
opj_j2k_read_float64_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1521 static void  opj_j2k_read_float64_to_int32(const void * p_src_data,
1522         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1523 {
1524     OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1525     OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1526     OPJ_UINT32 i;
1527     OPJ_FLOAT64 l_temp;
1528 
1529     for (i = 0; i < p_nb_elem; ++i) {
1530         opj_read_double(l_src_data, &l_temp);
1531 
1532         l_src_data += sizeof(OPJ_FLOAT64);
1533 
1534         *(l_dest_data++) = (OPJ_INT32) l_temp;
1535     }
1536 }
1537 
opj_j2k_write_float_to_int16(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1538 static void  opj_j2k_write_float_to_int16(const void * p_src_data,
1539         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1540 {
1541     OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1542     OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1543     OPJ_UINT32 i;
1544     OPJ_UINT32 l_temp;
1545 
1546     for (i = 0; i < p_nb_elem; ++i) {
1547         l_temp = (OPJ_UINT32) * (l_src_data++);
1548 
1549         opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT16));
1550 
1551         l_dest_data += sizeof(OPJ_INT16);
1552     }
1553 }
1554 
opj_j2k_write_float_to_int32(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1555 static void opj_j2k_write_float_to_int32(const void * p_src_data,
1556         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1557 {
1558     OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1559     OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1560     OPJ_UINT32 i;
1561     OPJ_UINT32 l_temp;
1562 
1563     for (i = 0; i < p_nb_elem; ++i) {
1564         l_temp = (OPJ_UINT32) * (l_src_data++);
1565 
1566         opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT32));
1567 
1568         l_dest_data += sizeof(OPJ_INT32);
1569     }
1570 }
1571 
opj_j2k_write_float_to_float(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1572 static void  opj_j2k_write_float_to_float(const void * p_src_data,
1573         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1574 {
1575     OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1576     OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1577     OPJ_UINT32 i;
1578     OPJ_FLOAT32 l_temp;
1579 
1580     for (i = 0; i < p_nb_elem; ++i) {
1581         l_temp = (OPJ_FLOAT32) * (l_src_data++);
1582 
1583         opj_write_float(l_dest_data, l_temp);
1584 
1585         l_dest_data += sizeof(OPJ_FLOAT32);
1586     }
1587 }
1588 
opj_j2k_write_float_to_float64(const void * p_src_data,void * p_dest_data,OPJ_UINT32 p_nb_elem)1589 static void  opj_j2k_write_float_to_float64(const void * p_src_data,
1590         void * p_dest_data, OPJ_UINT32 p_nb_elem)
1591 {
1592     OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1593     OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1594     OPJ_UINT32 i;
1595     OPJ_FLOAT64 l_temp;
1596 
1597     for (i = 0; i < p_nb_elem; ++i) {
1598         l_temp = (OPJ_FLOAT64) * (l_src_data++);
1599 
1600         opj_write_double(l_dest_data, l_temp);
1601 
1602         l_dest_data += sizeof(OPJ_FLOAT64);
1603     }
1604 }
1605 
opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)1606 const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)
1607 {
1608     const j2k_prog_order_t *po;
1609     for (po = j2k_prog_order_list; po->enum_prog != -1; po++) {
1610         if (po->enum_prog == prg_order) {
1611             return po->str_prog;
1612         }
1613     }
1614     return po->str_prog;
1615 }
1616 
opj_j2k_check_poc_val(const opj_poc_t * p_pocs,OPJ_UINT32 p_nb_pocs,OPJ_UINT32 p_nb_resolutions,OPJ_UINT32 p_num_comps,OPJ_UINT32 p_num_layers,opj_event_mgr_t * p_manager)1617 static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs,
1618                                       OPJ_UINT32 p_nb_pocs,
1619                                       OPJ_UINT32 p_nb_resolutions,
1620                                       OPJ_UINT32 p_num_comps,
1621                                       OPJ_UINT32 p_num_layers,
1622                                       opj_event_mgr_t * p_manager)
1623 {
1624     OPJ_UINT32* packet_array;
1625     OPJ_UINT32 index, resno, compno, layno;
1626     OPJ_UINT32 i;
1627     OPJ_UINT32 step_c = 1;
1628     OPJ_UINT32 step_r = p_num_comps * step_c;
1629     OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1630     OPJ_BOOL loss = OPJ_FALSE;
1631     OPJ_UINT32 layno0 = 0;
1632 
1633     packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers,
1634                                             sizeof(OPJ_UINT32));
1635     if (packet_array == 00) {
1636         opj_event_msg(p_manager, EVT_ERROR,
1637                       "Not enough memory for checking the poc values.\n");
1638         return OPJ_FALSE;
1639     }
1640 
1641     if (p_nb_pocs == 0) {
1642         opj_free(packet_array);
1643         return OPJ_TRUE;
1644     }
1645 
1646     index = step_r * p_pocs->resno0;
1647     /* take each resolution for each poc */
1648     for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1649         OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1650 
1651         /* take each comp of each resolution for each poc */
1652         for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1653             OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1654 
1655             /* and finally take each layer of each res of ... */
1656             for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1657                 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1658                 packet_array[comp_index] = 1;
1659                 comp_index += step_l;
1660             }
1661 
1662             res_index += step_c;
1663         }
1664 
1665         index += step_r;
1666     }
1667     ++p_pocs;
1668 
1669     /* iterate through all the pocs */
1670     for (i = 1; i < p_nb_pocs ; ++i) {
1671         OPJ_UINT32 l_last_layno1 = (p_pocs - 1)->layno1 ;
1672 
1673         layno0 = (p_pocs->layno1 > l_last_layno1) ? l_last_layno1 : 0;
1674         index = step_r * p_pocs->resno0;
1675 
1676         /* take each resolution for each poc */
1677         for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1678             OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1679 
1680             /* take each comp of each resolution for each poc */
1681             for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1682                 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1683 
1684                 /* and finally take each layer of each res of ... */
1685                 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1686                     /*index = step_r * resno + step_c * compno + step_l * layno;*/
1687                     packet_array[comp_index] = 1;
1688                     comp_index += step_l;
1689                 }
1690 
1691                 res_index += step_c;
1692             }
1693 
1694             index += step_r;
1695         }
1696 
1697         ++p_pocs;
1698     }
1699 
1700     index = 0;
1701     for (layno = 0; layno < p_num_layers ; ++layno) {
1702         for (resno = 0; resno < p_nb_resolutions; ++resno) {
1703             for (compno = 0; compno < p_num_comps; ++compno) {
1704                 loss |= (packet_array[index] != 1);
1705                 /*index = step_r * resno + step_c * compno + step_l * layno;*/
1706                 index += step_c;
1707             }
1708         }
1709     }
1710 
1711     if (loss) {
1712         opj_event_msg(p_manager, EVT_ERROR, "Missing packets possible loss of data\n");
1713     }
1714 
1715     opj_free(packet_array);
1716 
1717     return !loss;
1718 }
1719 
1720 /* ----------------------------------------------------------------------- */
1721 
opj_j2k_get_num_tp(opj_cp_t * cp,OPJ_UINT32 pino,OPJ_UINT32 tileno)1722 static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino,
1723                                      OPJ_UINT32 tileno)
1724 {
1725     const OPJ_CHAR *prog = 00;
1726     OPJ_INT32 i;
1727     OPJ_UINT32 tpnum = 1;
1728     opj_tcp_t *tcp = 00;
1729     opj_poc_t * l_current_poc = 00;
1730 
1731     /*  preconditions */
1732     assert(tileno < (cp->tw * cp->th));
1733     assert(pino < (cp->tcps[tileno].numpocs + 1));
1734 
1735     /* get the given tile coding parameter */
1736     tcp = &cp->tcps[tileno];
1737     assert(tcp != 00);
1738 
1739     l_current_poc = &(tcp->pocs[pino]);
1740     assert(l_current_poc != 0);
1741 
1742     /* get the progression order as a character string */
1743     prog = opj_j2k_convert_progression_order(tcp->prg);
1744     assert(strlen(prog) > 0);
1745 
1746     if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1747         for (i = 0; i < 4; ++i) {
1748             switch (prog[i]) {
1749             /* component wise */
1750             case 'C':
1751                 tpnum *= l_current_poc->compE;
1752                 break;
1753             /* resolution wise */
1754             case 'R':
1755                 tpnum *= l_current_poc->resE;
1756                 break;
1757             /* precinct wise */
1758             case 'P':
1759                 tpnum *= l_current_poc->prcE;
1760                 break;
1761             /* layer wise */
1762             case 'L':
1763                 tpnum *= l_current_poc->layE;
1764                 break;
1765             }
1766             /* whould we split here ? */
1767             if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) {
1768                 cp->m_specific_param.m_enc.m_tp_pos = i;
1769                 break;
1770             }
1771         }
1772     } else {
1773         tpnum = 1;
1774     }
1775 
1776     return tpnum;
1777 }
1778 
opj_j2k_calculate_tp(opj_j2k_t * p_j2k,opj_cp_t * cp,OPJ_UINT32 * p_nb_tiles,opj_image_t * image,opj_event_mgr_t * p_manager)1779 static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k,
1780                                      opj_cp_t *cp,
1781                                      OPJ_UINT32 * p_nb_tiles,
1782                                      opj_image_t *image,
1783                                      opj_event_mgr_t * p_manager
1784                                     )
1785 {
1786     OPJ_UINT32 pino, tileno;
1787     OPJ_UINT32 l_nb_tiles;
1788     opj_tcp_t *tcp;
1789 
1790     /* preconditions */
1791     assert(p_nb_tiles != 00);
1792     assert(cp != 00);
1793     assert(image != 00);
1794     assert(p_j2k != 00);
1795     assert(p_manager != 00);
1796 
1797     OPJ_UNUSED(p_j2k);
1798     OPJ_UNUSED(p_manager);
1799 
1800     l_nb_tiles = cp->tw * cp->th;
1801     * p_nb_tiles = 0;
1802     tcp = cp->tcps;
1803 
1804     /* INDEX >> */
1805     /* TODO mergeV2: check this part which use cstr_info */
1806     /*if (p_j2k->cstr_info) {
1807             opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1808 
1809             for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1810                     OPJ_UINT32 cur_totnum_tp = 0;
1811 
1812                     opj_pi_update_encoding_parameters(image,cp,tileno);
1813 
1814                     for (pino = 0; pino <= tcp->numpocs; ++pino)
1815                     {
1816                             OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1817 
1818                             *p_nb_tiles = *p_nb_tiles + tp_num;
1819 
1820                             cur_totnum_tp += tp_num;
1821                     }
1822 
1823                     tcp->m_nb_tile_parts = cur_totnum_tp;
1824 
1825                     l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1826                     if (l_info_tile_ptr->tp == 00) {
1827                             return OPJ_FALSE;
1828                     }
1829 
1830                     memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1831 
1832                     l_info_tile_ptr->num_tps = cur_totnum_tp;
1833 
1834                     ++l_info_tile_ptr;
1835                     ++tcp;
1836             }
1837     }
1838     else */{
1839         for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1840             OPJ_UINT32 cur_totnum_tp = 0;
1841 
1842             opj_pi_update_encoding_parameters(image, cp, tileno);
1843 
1844             for (pino = 0; pino <= tcp->numpocs; ++pino) {
1845                 OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp, pino, tileno);
1846 
1847                 *p_nb_tiles = *p_nb_tiles + tp_num;
1848 
1849                 cur_totnum_tp += tp_num;
1850             }
1851             tcp->m_nb_tile_parts = cur_totnum_tp;
1852 
1853             ++tcp;
1854         }
1855     }
1856 
1857     return OPJ_TRUE;
1858 }
1859 
opj_j2k_write_soc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)1860 static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k,
1861                                   opj_stream_private_t *p_stream,
1862                                   opj_event_mgr_t * p_manager)
1863 {
1864     /* 2 bytes will be written */
1865     OPJ_BYTE * l_start_stream = 00;
1866 
1867     /* preconditions */
1868     assert(p_stream != 00);
1869     assert(p_j2k != 00);
1870     assert(p_manager != 00);
1871 
1872     l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1873 
1874     /* write SOC identifier */
1875     opj_write_bytes(l_start_stream, J2K_MS_SOC, 2);
1876 
1877     if (opj_stream_write_data(p_stream, l_start_stream, 2, p_manager) != 2) {
1878         return OPJ_FALSE;
1879     }
1880 
1881     /* UniPG>> */
1882 #ifdef USE_JPWL
1883     /* update markers struct */
1884     /*
1885             OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1886     */
1887     assert(0 && "TODO");
1888 #endif /* USE_JPWL */
1889     /* <<UniPG */
1890 
1891     return OPJ_TRUE;
1892 }
1893 
1894 /**
1895  * Reads a SOC marker (Start of Codestream)
1896  * @param       p_j2k           the jpeg2000 file codec.
1897  * @param       p_stream        FIXME DOC
1898  * @param       p_manager       the user event manager.
1899 */
opj_j2k_read_soc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)1900 static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k,
1901                                  opj_stream_private_t *p_stream,
1902                                  opj_event_mgr_t * p_manager
1903                                 )
1904 {
1905     OPJ_BYTE l_data [2];
1906     OPJ_UINT32 l_marker;
1907 
1908     /* preconditions */
1909     assert(p_j2k != 00);
1910     assert(p_manager != 00);
1911     assert(p_stream != 00);
1912 
1913     if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
1914         return OPJ_FALSE;
1915     }
1916 
1917     opj_read_bytes(l_data, &l_marker, 2);
1918     if (l_marker != J2K_MS_SOC) {
1919         return OPJ_FALSE;
1920     }
1921 
1922     /* Next marker should be a SIZ marker in the main header */
1923     p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1924 
1925     /* FIXME move it in a index structure included in p_j2k*/
1926     p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1927 
1928     opj_event_msg(p_manager, EVT_INFO,
1929                   "Start to read j2k main header (%" PRId64 ").\n",
1930                   p_j2k->cstr_index->main_head_start);
1931 
1932     /* Add the marker to the codestream index*/
1933     if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC,
1934                                           p_j2k->cstr_index->main_head_start, 2)) {
1935         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
1936         return OPJ_FALSE;
1937     }
1938     return OPJ_TRUE;
1939 }
1940 
opj_j2k_write_siz(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)1941 static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k,
1942                                   opj_stream_private_t *p_stream,
1943                                   opj_event_mgr_t * p_manager)
1944 {
1945     OPJ_UINT32 i;
1946     OPJ_UINT32 l_size_len;
1947     OPJ_BYTE * l_current_ptr;
1948     opj_image_t * l_image = 00;
1949     opj_cp_t *cp = 00;
1950     opj_image_comp_t * l_img_comp = 00;
1951 
1952     /* preconditions */
1953     assert(p_stream != 00);
1954     assert(p_j2k != 00);
1955     assert(p_manager != 00);
1956 
1957     l_image = p_j2k->m_private_image;
1958     cp = &(p_j2k->m_cp);
1959     l_size_len = 40 + 3 * l_image->numcomps;
1960     l_img_comp = l_image->comps;
1961 
1962     if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1963 
1964         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
1965                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
1966         if (! new_header_tile_data) {
1967             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
1968             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
1969             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
1970             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
1971             return OPJ_FALSE;
1972         }
1973         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
1974         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1975     }
1976 
1977     l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1978 
1979     /* write SOC identifier */
1980     opj_write_bytes(l_current_ptr, J2K_MS_SIZ, 2);  /* SIZ */
1981     l_current_ptr += 2;
1982 
1983     opj_write_bytes(l_current_ptr, l_size_len - 2, 2); /* L_SIZ */
1984     l_current_ptr += 2;
1985 
1986     opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
1987     l_current_ptr += 2;
1988 
1989     opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1990     l_current_ptr += 4;
1991 
1992     opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1993     l_current_ptr += 4;
1994 
1995     opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1996     l_current_ptr += 4;
1997 
1998     opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1999     l_current_ptr += 4;
2000 
2001     opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
2002     l_current_ptr += 4;
2003 
2004     opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
2005     l_current_ptr += 4;
2006 
2007     opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
2008     l_current_ptr += 4;
2009 
2010     opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
2011     l_current_ptr += 4;
2012 
2013     opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
2014     l_current_ptr += 2;
2015 
2016     for (i = 0; i < l_image->numcomps; ++i) {
2017         /* TODO here with MCT ? */
2018         opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7),
2019                         1);      /* Ssiz_i */
2020         ++l_current_ptr;
2021 
2022         opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
2023         ++l_current_ptr;
2024 
2025         opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
2026         ++l_current_ptr;
2027 
2028         ++l_img_comp;
2029     }
2030 
2031     if (opj_stream_write_data(p_stream,
2032                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len,
2033                               p_manager) != l_size_len) {
2034         return OPJ_FALSE;
2035     }
2036 
2037     return OPJ_TRUE;
2038 }
2039 
2040 /**
2041  * Reads a SIZ marker (image and tile size)
2042  * @param       p_j2k           the jpeg2000 file codec.
2043  * @param       p_header_data   the data contained in the SIZ box.
2044  * @param       p_header_size   the size of the data contained in the SIZ marker.
2045  * @param       p_manager       the user event manager.
2046 */
opj_j2k_read_siz(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2047 static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
2048                                  OPJ_BYTE * p_header_data,
2049                                  OPJ_UINT32 p_header_size,
2050                                  opj_event_mgr_t * p_manager
2051                                 )
2052 {
2053     OPJ_UINT32 i;
2054     OPJ_UINT32 l_nb_comp;
2055     OPJ_UINT32 l_nb_comp_remain;
2056     OPJ_UINT32 l_remaining_size;
2057     OPJ_UINT32 l_nb_tiles;
2058     OPJ_UINT32 l_tmp, l_tx1, l_ty1;
2059     OPJ_UINT32 l_prec0, l_sgnd0;
2060     opj_image_t *l_image = 00;
2061     opj_cp_t *l_cp = 00;
2062     opj_image_comp_t * l_img_comp = 00;
2063     opj_tcp_t * l_current_tile_param = 00;
2064 
2065     /* preconditions */
2066     assert(p_j2k != 00);
2067     assert(p_manager != 00);
2068     assert(p_header_data != 00);
2069 
2070     l_image = p_j2k->m_private_image;
2071     l_cp = &(p_j2k->m_cp);
2072 
2073     /* minimum size == 39 - 3 (= minimum component parameter) */
2074     if (p_header_size < 36) {
2075         opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2076         return OPJ_FALSE;
2077     }
2078 
2079     l_remaining_size = p_header_size - 36;
2080     l_nb_comp = l_remaining_size / 3;
2081     l_nb_comp_remain = l_remaining_size % 3;
2082     if (l_nb_comp_remain != 0) {
2083         opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2084         return OPJ_FALSE;
2085     }
2086 
2087     opj_read_bytes(p_header_data, &l_tmp,
2088                    2);                                                /* Rsiz (capabilities) */
2089     p_header_data += 2;
2090     l_cp->rsiz = (OPJ_UINT16) l_tmp;
2091     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
2092     p_header_data += 4;
2093     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
2094     p_header_data += 4;
2095     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
2096     p_header_data += 4;
2097     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
2098     p_header_data += 4;
2099     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx,
2100                    4);             /* XTsiz */
2101     p_header_data += 4;
2102     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy,
2103                    4);             /* YTsiz */
2104     p_header_data += 4;
2105     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0,
2106                    4);             /* XT0siz */
2107     p_header_data += 4;
2108     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0,
2109                    4);             /* YT0siz */
2110     p_header_data += 4;
2111     opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp,
2112                    2);                 /* Csiz */
2113     p_header_data += 2;
2114     if (l_tmp < 16385) {
2115         l_image->numcomps = (OPJ_UINT16) l_tmp;
2116     } else {
2117         opj_event_msg(p_manager, EVT_ERROR,
2118                       "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2119         return OPJ_FALSE;
2120     }
2121 
2122     if (l_image->numcomps != l_nb_comp) {
2123         opj_event_msg(p_manager, EVT_ERROR,
2124                       "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n",
2125                       l_image->numcomps, l_nb_comp);
2126         return OPJ_FALSE;
2127     }
2128 
2129     /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
2130     /* testcase issue427-null-image-size.jp2 */
2131     if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) {
2132         opj_event_msg(p_manager, EVT_ERROR,
2133                       "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64
2134                       ")\n", (OPJ_INT64)l_image->x1 - l_image->x0,
2135                       (OPJ_INT64)l_image->y1 - l_image->y0);
2136         return OPJ_FALSE;
2137     }
2138     /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
2139     if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) {
2140         opj_event_msg(p_manager, EVT_ERROR,
2141                       "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx,
2142                       l_cp->tdy);
2143         return OPJ_FALSE;
2144     }
2145 
2146     /* testcase issue427-illegal-tile-offset.jp2 */
2147     l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */
2148     l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */
2149     if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) ||
2150             (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) {
2151         opj_event_msg(p_manager, EVT_ERROR,
2152                       "Error with SIZ marker: illegal tile offset\n");
2153         return OPJ_FALSE;
2154     }
2155     if (!p_j2k->dump_state) {
2156         OPJ_UINT32 siz_w, siz_h;
2157 
2158         siz_w = l_image->x1 - l_image->x0;
2159         siz_h = l_image->y1 - l_image->y0;
2160 
2161         if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0
2162                 && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) {
2163             opj_event_msg(p_manager, EVT_ERROR,
2164                           "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w,
2165                           p_j2k->ihdr_h, siz_w, siz_h);
2166             return OPJ_FALSE;
2167         }
2168     }
2169 #ifdef USE_JPWL
2170     if (l_cp->correct) {
2171         /* if JPWL is on, we check whether TX errors have damaged
2172           too much the SIZ parameters */
2173         if (!(l_image->x1 * l_image->y1)) {
2174             opj_event_msg(p_manager, EVT_ERROR,
2175                           "JPWL: bad image size (%d x %d)\n",
2176                           l_image->x1, l_image->y1);
2177             if (!JPWL_ASSUME) {
2178                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2179                 return OPJ_FALSE;
2180             }
2181         }
2182 
2183         /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2184                 if (l_image->numcomps != ((len - 38) / 3)) {
2185                         opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2186                                 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2187                                 l_image->numcomps, ((len - 38) / 3));
2188                         if (!JPWL_ASSUME) {
2189                                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2190                                 return OPJ_FALSE;
2191                         }
2192         */              /* we try to correct */
2193         /*              opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
2194                         if (l_image->numcomps < ((len - 38) / 3)) {
2195                                 len = 38 + 3 * l_image->numcomps;
2196                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2197                                         len);
2198                         } else {
2199                                 l_image->numcomps = ((len - 38) / 3);
2200                                 opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2201                                         l_image->numcomps);
2202                         }
2203                 }
2204         */
2205 
2206         /* update components number in the jpwl_exp_comps filed */
2207         l_cp->exp_comps = l_image->numcomps;
2208     }
2209 #endif /* USE_JPWL */
2210 
2211     /* Allocate the resulting image components */
2212     l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps,
2213                      sizeof(opj_image_comp_t));
2214     if (l_image->comps == 00) {
2215         l_image->numcomps = 0;
2216         opj_event_msg(p_manager, EVT_ERROR,
2217                       "Not enough memory to take in charge SIZ marker\n");
2218         return OPJ_FALSE;
2219     }
2220 
2221     l_img_comp = l_image->comps;
2222 
2223     l_prec0 = 0;
2224     l_sgnd0 = 0;
2225     /* Read the component information */
2226     for (i = 0; i < l_image->numcomps; ++i) {
2227         OPJ_UINT32 tmp;
2228         opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */
2229         ++p_header_data;
2230         l_img_comp->prec = (tmp & 0x7f) + 1;
2231         l_img_comp->sgnd = tmp >> 7;
2232 
2233         if (p_j2k->dump_state == 0) {
2234             if (i == 0) {
2235                 l_prec0 = l_img_comp->prec;
2236                 l_sgnd0 = l_img_comp->sgnd;
2237             } else if (!l_cp->allow_different_bit_depth_sign
2238                        && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) {
2239                 opj_event_msg(p_manager, EVT_WARNING,
2240                               "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n"
2241                               "        [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0,
2242                               i, l_img_comp->prec, l_img_comp->sgnd);
2243             }
2244             /* TODO: we should perhaps also check against JP2 BPCC values */
2245         }
2246         opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */
2247         ++p_header_data;
2248         l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2249         opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */
2250         ++p_header_data;
2251         l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2252         if (l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
2253                 l_img_comp->dy < 1 || l_img_comp->dy > 255) {
2254             opj_event_msg(p_manager, EVT_ERROR,
2255                           "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n",
2256                           i, l_img_comp->dx, l_img_comp->dy);
2257             return OPJ_FALSE;
2258         }
2259         /* Avoids later undefined shift in computation of */
2260         /* p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
2261                     << (l_image->comps[i].prec - 1); */
2262         if (l_img_comp->prec > 31) {
2263             opj_event_msg(p_manager, EVT_ERROR,
2264                           "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n",
2265                           i, l_img_comp->prec);
2266             return OPJ_FALSE;
2267         }
2268 #ifdef USE_JPWL
2269         if (l_cp->correct) {
2270             /* if JPWL is on, we check whether TX errors have damaged
2271                     too much the SIZ parameters, again */
2272             if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2273                 opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2274                               "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2275                               i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2276                 if (!JPWL_ASSUME) {
2277                     opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2278                     return OPJ_FALSE;
2279                 }
2280                 /* we try to correct */
2281                 opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2282                 if (!l_image->comps[i].dx) {
2283                     l_image->comps[i].dx = 1;
2284                     opj_event_msg(p_manager, EVT_WARNING,
2285                                   "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2286                                   i, l_image->comps[i].dx);
2287                 }
2288                 if (!l_image->comps[i].dy) {
2289                     l_image->comps[i].dy = 1;
2290                     opj_event_msg(p_manager, EVT_WARNING,
2291                                   "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2292                                   i, l_image->comps[i].dy);
2293                 }
2294             }
2295         }
2296 #endif /* USE_JPWL */
2297         l_img_comp->resno_decoded =
2298             0;                                                          /* number of resolution decoded */
2299         l_img_comp->factor =
2300             l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2301         ++l_img_comp;
2302     }
2303 
2304     if (l_cp->tdx == 0 || l_cp->tdy == 0) {
2305         return OPJ_FALSE;
2306     }
2307 
2308     /* Compute the number of tiles */
2309     l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2310     l_cp->th = opj_uint_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2311 
2312     /* Check that the number of tiles is valid */
2313     if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
2314         opj_event_msg(p_manager, EVT_ERROR,
2315                       "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
2316                       l_cp->tw, l_cp->th);
2317         return OPJ_FALSE;
2318     }
2319     l_nb_tiles = l_cp->tw * l_cp->th;
2320 
2321     /* Define the tiles which will be decoded */
2322     if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2323         p_j2k->m_specific_param.m_decoder.m_start_tile_x =
2324             (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
2325         p_j2k->m_specific_param.m_decoder.m_start_tile_y =
2326             (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
2327         p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(
2328             p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0, l_cp->tdx);
2329         p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(
2330             p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0, l_cp->tdy);
2331     } else {
2332         p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2333         p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2334         p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2335         p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2336     }
2337 
2338 #ifdef USE_JPWL
2339     if (l_cp->correct) {
2340         /* if JPWL is on, we check whether TX errors have damaged
2341           too much the SIZ parameters */
2342         if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) ||
2343                 (l_cp->th > l_cp->max_tiles)) {
2344             opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2345                           "JPWL: bad number of tiles (%d x %d)\n",
2346                           l_cp->tw, l_cp->th);
2347             if (!JPWL_ASSUME) {
2348                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2349                 return OPJ_FALSE;
2350             }
2351             /* we try to correct */
2352             opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2353             if (l_cp->tw < 1) {
2354                 l_cp->tw = 1;
2355                 opj_event_msg(p_manager, EVT_WARNING,
2356                               "- setting %d tiles in x => HYPOTHESIS!!!\n",
2357                               l_cp->tw);
2358             }
2359             if (l_cp->tw > l_cp->max_tiles) {
2360                 l_cp->tw = 1;
2361                 opj_event_msg(p_manager, EVT_WARNING,
2362                               "- too large x, increase expectance of %d\n"
2363                               "- setting %d tiles in x => HYPOTHESIS!!!\n",
2364                               l_cp->max_tiles, l_cp->tw);
2365             }
2366             if (l_cp->th < 1) {
2367                 l_cp->th = 1;
2368                 opj_event_msg(p_manager, EVT_WARNING,
2369                               "- setting %d tiles in y => HYPOTHESIS!!!\n",
2370                               l_cp->th);
2371             }
2372             if (l_cp->th > l_cp->max_tiles) {
2373                 l_cp->th = 1;
2374                 opj_event_msg(p_manager, EVT_WARNING,
2375                               "- too large y, increase expectance of %d to continue\n",
2376                               "- setting %d tiles in y => HYPOTHESIS!!!\n",
2377                               l_cp->max_tiles, l_cp->th);
2378             }
2379         }
2380     }
2381 #endif /* USE_JPWL */
2382 
2383     /* memory allocations */
2384     l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
2385     if (l_cp->tcps == 00) {
2386         opj_event_msg(p_manager, EVT_ERROR,
2387                       "Not enough memory to take in charge SIZ marker\n");
2388         return OPJ_FALSE;
2389     }
2390 
2391 #ifdef USE_JPWL
2392     if (l_cp->correct) {
2393         if (!l_cp->tcps) {
2394             opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2395                           "JPWL: could not alloc tcps field of cp\n");
2396             if (!JPWL_ASSUME) {
2397                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2398                 return OPJ_FALSE;
2399             }
2400         }
2401     }
2402 #endif /* USE_JPWL */
2403 
2404     p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2405         (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2406     if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2407         opj_event_msg(p_manager, EVT_ERROR,
2408                       "Not enough memory to take in charge SIZ marker\n");
2409         return OPJ_FALSE;
2410     }
2411 
2412     p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2413         (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS,
2414                                     sizeof(opj_mct_data_t));
2415 
2416     if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2417         opj_event_msg(p_manager, EVT_ERROR,
2418                       "Not enough memory to take in charge SIZ marker\n");
2419         return OPJ_FALSE;
2420     }
2421     p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records =
2422         OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
2423 
2424     p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2425         (opj_simple_mcc_decorrelation_data_t*)
2426         opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS,
2427                    sizeof(opj_simple_mcc_decorrelation_data_t));
2428 
2429     if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2430         opj_event_msg(p_manager, EVT_ERROR,
2431                       "Not enough memory to take in charge SIZ marker\n");
2432         return OPJ_FALSE;
2433     }
2434     p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records =
2435         OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
2436 
2437     /* set up default dc level shift */
2438     for (i = 0; i < l_image->numcomps; ++i) {
2439         if (! l_image->comps[i].sgnd) {
2440             p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
2441                     << (l_image->comps[i].prec - 1);
2442         }
2443     }
2444 
2445     l_current_tile_param = l_cp->tcps;
2446     for (i = 0; i < l_nb_tiles; ++i) {
2447         l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps,
2448                                       sizeof(opj_tccp_t));
2449         if (l_current_tile_param->tccps == 00) {
2450             opj_event_msg(p_manager, EVT_ERROR,
2451                           "Not enough memory to take in charge SIZ marker\n");
2452             return OPJ_FALSE;
2453         }
2454 
2455         ++l_current_tile_param;
2456     }
2457 
2458     p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH;
2459     opj_image_comp_header_update(l_image, l_cp);
2460 
2461     return OPJ_TRUE;
2462 }
2463 
opj_j2k_write_com(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2464 static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k,
2465                                   opj_stream_private_t *p_stream,
2466                                   opj_event_mgr_t * p_manager
2467                                  )
2468 {
2469     OPJ_UINT32 l_comment_size;
2470     OPJ_UINT32 l_total_com_size;
2471     const OPJ_CHAR *l_comment;
2472     OPJ_BYTE * l_current_ptr = 00;
2473 
2474     /* preconditions */
2475     assert(p_j2k != 00);
2476     assert(p_stream != 00);
2477     assert(p_manager != 00);
2478 
2479     l_comment = p_j2k->m_cp.comment;
2480     l_comment_size = (OPJ_UINT32)strlen(l_comment);
2481     l_total_com_size = l_comment_size + 6;
2482 
2483     if (l_total_com_size >
2484             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2485         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2486                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
2487         if (! new_header_tile_data) {
2488             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2489             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2490             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2491             opj_event_msg(p_manager, EVT_ERROR,
2492                           "Not enough memory to write the COM marker\n");
2493             return OPJ_FALSE;
2494         }
2495         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2496         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2497     }
2498 
2499     l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2500 
2501     opj_write_bytes(l_current_ptr, J2K_MS_COM, 2);  /* COM */
2502     l_current_ptr += 2;
2503 
2504     opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2);        /* L_COM */
2505     l_current_ptr += 2;
2506 
2507     opj_write_bytes(l_current_ptr, 1,
2508                     2);   /* General use (IS 8859-15:1999 (Latin) values) */
2509     l_current_ptr += 2;
2510 
2511     memcpy(l_current_ptr, l_comment, l_comment_size);
2512 
2513     if (opj_stream_write_data(p_stream,
2514                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size,
2515                               p_manager) != l_total_com_size) {
2516         return OPJ_FALSE;
2517     }
2518 
2519     return OPJ_TRUE;
2520 }
2521 
2522 /**
2523  * Reads a COM marker (comments)
2524  * @param       p_j2k           the jpeg2000 file codec.
2525  * @param       p_header_data   the data contained in the COM box.
2526  * @param       p_header_size   the size of the data contained in the COM marker.
2527  * @param       p_manager               the user event manager.
2528 */
opj_j2k_read_com(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2529 static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k,
2530                                  OPJ_BYTE * p_header_data,
2531                                  OPJ_UINT32 p_header_size,
2532                                  opj_event_mgr_t * p_manager
2533                                 )
2534 {
2535     /* preconditions */
2536     assert(p_j2k != 00);
2537     assert(p_manager != 00);
2538     assert(p_header_data != 00);
2539 
2540     OPJ_UNUSED(p_j2k);
2541     OPJ_UNUSED(p_header_data);
2542     OPJ_UNUSED(p_header_size);
2543     OPJ_UNUSED(p_manager);
2544 
2545     return OPJ_TRUE;
2546 }
2547 
opj_j2k_write_cod(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2548 static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k,
2549                                   opj_stream_private_t *p_stream,
2550                                   opj_event_mgr_t * p_manager)
2551 {
2552     opj_cp_t *l_cp = 00;
2553     opj_tcp_t *l_tcp = 00;
2554     OPJ_UINT32 l_code_size, l_remaining_size;
2555     OPJ_BYTE * l_current_data = 00;
2556 
2557     /* preconditions */
2558     assert(p_j2k != 00);
2559     assert(p_manager != 00);
2560     assert(p_stream != 00);
2561 
2562     l_cp = &(p_j2k->m_cp);
2563     l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2564     l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
2565                   p_j2k->m_current_tile_number, 0);
2566     l_remaining_size = l_code_size;
2567 
2568     if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2569         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2570                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2571         if (! new_header_tile_data) {
2572             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2573             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2574             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2575             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2576             return OPJ_FALSE;
2577         }
2578         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2579         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2580     }
2581 
2582     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2583 
2584     opj_write_bytes(l_current_data, J2K_MS_COD, 2);           /* COD */
2585     l_current_data += 2;
2586 
2587     opj_write_bytes(l_current_data, l_code_size - 2, 2);      /* L_COD */
2588     l_current_data += 2;
2589 
2590     opj_write_bytes(l_current_data, l_tcp->csty, 1);          /* Scod */
2591     ++l_current_data;
2592 
2593     opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */
2594     ++l_current_data;
2595 
2596     opj_write_bytes(l_current_data, l_tcp->numlayers, 2);     /* SGcod (B) */
2597     l_current_data += 2;
2598 
2599     opj_write_bytes(l_current_data, l_tcp->mct, 1);           /* SGcod (C) */
2600     ++l_current_data;
2601 
2602     l_remaining_size -= 9;
2603 
2604     if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
2605                                     l_current_data, &l_remaining_size, p_manager)) {
2606         opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2607         return OPJ_FALSE;
2608     }
2609 
2610     if (l_remaining_size != 0) {
2611         opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2612         return OPJ_FALSE;
2613     }
2614 
2615     if (opj_stream_write_data(p_stream,
2616                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size,
2617                               p_manager) != l_code_size) {
2618         return OPJ_FALSE;
2619     }
2620 
2621     return OPJ_TRUE;
2622 }
2623 
2624 /**
2625  * Reads a COD marker (Coding style defaults)
2626  * @param       p_header_data   the data contained in the COD box.
2627  * @param       p_j2k                   the jpeg2000 codec.
2628  * @param       p_header_size   the size of the data contained in the COD marker.
2629  * @param       p_manager               the user event manager.
2630 */
opj_j2k_read_cod(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2631 static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k,
2632                                  OPJ_BYTE * p_header_data,
2633                                  OPJ_UINT32 p_header_size,
2634                                  opj_event_mgr_t * p_manager
2635                                 )
2636 {
2637     /* loop */
2638     OPJ_UINT32 i;
2639     OPJ_UINT32 l_tmp;
2640     opj_cp_t *l_cp = 00;
2641     opj_tcp_t *l_tcp = 00;
2642     opj_image_t *l_image = 00;
2643 
2644     /* preconditions */
2645     assert(p_header_data != 00);
2646     assert(p_j2k != 00);
2647     assert(p_manager != 00);
2648 
2649     l_image = p_j2k->m_private_image;
2650     l_cp = &(p_j2k->m_cp);
2651 
2652     /* If we are in the first tile-part header of the current tile */
2653     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2654             &l_cp->tcps[p_j2k->m_current_tile_number] :
2655             p_j2k->m_specific_param.m_decoder.m_default_tcp;
2656 
2657 #if 0
2658     /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */
2659     /* but this is no longer necessary to handle issue476.jp2 */
2660     /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */
2661     /* Only one COD per tile */
2662     if (l_tcp->cod) {
2663         opj_event_msg(p_manager, EVT_ERROR,
2664                       "COD marker already read. No more than one COD marker per tile.\n");
2665         return OPJ_FALSE;
2666     }
2667 #endif
2668     l_tcp->cod = 1;
2669 
2670     /* Make sure room is sufficient */
2671     if (p_header_size < 5) {
2672         opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2673         return OPJ_FALSE;
2674     }
2675 
2676     opj_read_bytes(p_header_data, &l_tcp->csty, 1);         /* Scod */
2677     ++p_header_data;
2678     /* Make sure we know how to decode this */
2679     if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP |
2680                                      J2K_CP_CSTY_EPH)) != 0U) {
2681         opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
2682         return OPJ_FALSE;
2683     }
2684     opj_read_bytes(p_header_data, &l_tmp, 1);                       /* SGcod (A) */
2685     ++p_header_data;
2686     l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2687     /* Make sure progression order is valid */
2688     if (l_tcp->prg > OPJ_CPRL) {
2689         opj_event_msg(p_manager, EVT_ERROR,
2690                       "Unknown progression order in COD marker\n");
2691         l_tcp->prg = OPJ_PROG_UNKNOWN;
2692     }
2693     opj_read_bytes(p_header_data, &l_tcp->numlayers, 2);    /* SGcod (B) */
2694     p_header_data += 2;
2695 
2696     if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) {
2697         opj_event_msg(p_manager, EVT_ERROR,
2698                       "Invalid number of layers in COD marker : %d not in range [1-65535]\n",
2699                       l_tcp->numlayers);
2700         return OPJ_FALSE;
2701     }
2702 
2703     /* If user didn't set a number layer to decode take the max specify in the codestream. */
2704     if (l_cp->m_specific_param.m_dec.m_layer) {
2705         l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2706     } else {
2707         l_tcp->num_layers_to_decode = l_tcp->numlayers;
2708     }
2709 
2710     opj_read_bytes(p_header_data, &l_tcp->mct, 1);          /* SGcod (C) */
2711     ++p_header_data;
2712 
2713     p_header_size -= 5;
2714     for (i = 0; i < l_image->numcomps; ++i) {
2715         l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2716     }
2717 
2718     if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size,
2719                                    p_manager)) {
2720         opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2721         return OPJ_FALSE;
2722     }
2723 
2724     if (p_header_size != 0) {
2725         opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2726         return OPJ_FALSE;
2727     }
2728 
2729     /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2730     opj_j2k_copy_tile_component_parameters(p_j2k);
2731 
2732     /* Index */
2733 #ifdef WIP_REMOVE_MSD
2734     if (p_j2k->cstr_info) {
2735         /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2736         p_j2k->cstr_info->prog = l_tcp->prg;
2737         p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2738         p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(
2739                                             l_image->numcomps * sizeof(OPJ_UINT32));
2740         if (!p_j2k->cstr_info->numdecompos) {
2741             return OPJ_FALSE;
2742         }
2743         for (i = 0; i < l_image->numcomps; ++i) {
2744             p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2745         }
2746     }
2747 #endif
2748 
2749     return OPJ_TRUE;
2750 }
2751 
opj_j2k_write_coc(opj_j2k_t * p_j2k,OPJ_UINT32 p_comp_no,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2752 static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k,
2753                                   OPJ_UINT32 p_comp_no,
2754                                   opj_stream_private_t *p_stream,
2755                                   opj_event_mgr_t * p_manager)
2756 {
2757     OPJ_UINT32 l_coc_size, l_remaining_size;
2758     OPJ_UINT32 l_comp_room;
2759 
2760     /* preconditions */
2761     assert(p_j2k != 00);
2762     assert(p_manager != 00);
2763     assert(p_stream != 00);
2764 
2765     l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2766 
2767     l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
2768                  p_j2k->m_current_tile_number, p_comp_no);
2769 
2770     if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2771         OPJ_BYTE *new_header_tile_data;
2772         /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
2773                 = (OPJ_BYTE*)opj_realloc(
2774                         p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2775                         l_coc_size);*/
2776 
2777         new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2778                                    p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2779         if (! new_header_tile_data) {
2780             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2781             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2782             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2783             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2784             return OPJ_FALSE;
2785         }
2786         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2787         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2788     }
2789 
2790     opj_j2k_write_coc_in_memory(p_j2k, p_comp_no,
2791                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
2792                                 p_manager);
2793 
2794     if (opj_stream_write_data(p_stream,
2795                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size,
2796                               p_manager) != l_coc_size) {
2797         return OPJ_FALSE;
2798     }
2799 
2800     return OPJ_TRUE;
2801 }
2802 
opj_j2k_compare_coc(opj_j2k_t * p_j2k,OPJ_UINT32 p_first_comp_no,OPJ_UINT32 p_second_comp_no)2803 static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k,
2804                                     OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
2805 {
2806     opj_cp_t *l_cp = NULL;
2807     opj_tcp_t *l_tcp = NULL;
2808 
2809     /* preconditions */
2810     assert(p_j2k != 00);
2811 
2812     l_cp = &(p_j2k->m_cp);
2813     l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2814 
2815     if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) {
2816         return OPJ_FALSE;
2817     }
2818 
2819 
2820     return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number,
2821                                        p_first_comp_no, p_second_comp_no);
2822 }
2823 
opj_j2k_write_coc_in_memory(opj_j2k_t * p_j2k,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,opj_event_mgr_t * p_manager)2824 static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
2825                                         OPJ_UINT32 p_comp_no,
2826                                         OPJ_BYTE * p_data,
2827                                         OPJ_UINT32 * p_data_written,
2828                                         opj_event_mgr_t * p_manager
2829                                        )
2830 {
2831     opj_cp_t *l_cp = 00;
2832     opj_tcp_t *l_tcp = 00;
2833     OPJ_UINT32 l_coc_size, l_remaining_size;
2834     OPJ_BYTE * l_current_data = 00;
2835     opj_image_t *l_image = 00;
2836     OPJ_UINT32 l_comp_room;
2837 
2838     /* preconditions */
2839     assert(p_j2k != 00);
2840     assert(p_manager != 00);
2841 
2842     l_cp = &(p_j2k->m_cp);
2843     l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2844     l_image = p_j2k->m_private_image;
2845     l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2846 
2847     l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
2848                  p_j2k->m_current_tile_number, p_comp_no);
2849     l_remaining_size = l_coc_size;
2850 
2851     l_current_data = p_data;
2852 
2853     opj_write_bytes(l_current_data, J2K_MS_COC,
2854                     2);                         /* COC */
2855     l_current_data += 2;
2856 
2857     opj_write_bytes(l_current_data, l_coc_size - 2,
2858                     2);                     /* L_COC */
2859     l_current_data += 2;
2860 
2861     opj_write_bytes(l_current_data, p_comp_no, l_comp_room);        /* Ccoc */
2862     l_current_data += l_comp_room;
2863 
2864     opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty,
2865                     1);               /* Scoc */
2866     ++l_current_data;
2867 
2868     l_remaining_size -= (5 + l_comp_room);
2869     opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
2870                               l_current_data, &l_remaining_size, p_manager);
2871     * p_data_written = l_coc_size;
2872 }
2873 
opj_j2k_get_max_coc_size(opj_j2k_t * p_j2k)2874 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
2875 {
2876     OPJ_UINT32 i, j;
2877     OPJ_UINT32 l_nb_comp;
2878     OPJ_UINT32 l_nb_tiles;
2879     OPJ_UINT32 l_max = 0;
2880 
2881     /* preconditions */
2882 
2883     l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2884     l_nb_comp = p_j2k->m_private_image->numcomps;
2885 
2886     for (i = 0; i < l_nb_tiles; ++i) {
2887         for (j = 0; j < l_nb_comp; ++j) {
2888             l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j));
2889         }
2890     }
2891 
2892     return 6 + l_max;
2893 }
2894 
2895 /**
2896  * Reads a COC marker (Coding Style Component)
2897  * @param       p_header_data   the data contained in the COC box.
2898  * @param       p_j2k                   the jpeg2000 codec.
2899  * @param       p_header_size   the size of the data contained in the COC marker.
2900  * @param       p_manager               the user event manager.
2901 */
opj_j2k_read_coc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)2902 static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k,
2903                                  OPJ_BYTE * p_header_data,
2904                                  OPJ_UINT32 p_header_size,
2905                                  opj_event_mgr_t * p_manager
2906                                 )
2907 {
2908     opj_cp_t *l_cp = NULL;
2909     opj_tcp_t *l_tcp = NULL;
2910     opj_image_t *l_image = NULL;
2911     OPJ_UINT32 l_comp_room;
2912     OPJ_UINT32 l_comp_no;
2913 
2914     /* preconditions */
2915     assert(p_header_data != 00);
2916     assert(p_j2k != 00);
2917     assert(p_manager != 00);
2918 
2919     l_cp = &(p_j2k->m_cp);
2920     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
2921             ?
2922             &l_cp->tcps[p_j2k->m_current_tile_number] :
2923             p_j2k->m_specific_param.m_decoder.m_default_tcp;
2924     l_image = p_j2k->m_private_image;
2925 
2926     l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2927 
2928     /* make sure room is sufficient*/
2929     if (p_header_size < l_comp_room + 1) {
2930         opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2931         return OPJ_FALSE;
2932     }
2933     p_header_size -= l_comp_room + 1;
2934 
2935     opj_read_bytes(p_header_data, &l_comp_no,
2936                    l_comp_room);                 /* Ccoc */
2937     p_header_data += l_comp_room;
2938     if (l_comp_no >= l_image->numcomps) {
2939         opj_event_msg(p_manager, EVT_ERROR,
2940                       "Error reading COC marker (bad number of components)\n");
2941         return OPJ_FALSE;
2942     }
2943 
2944     opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty,
2945                    1);                  /* Scoc */
2946     ++p_header_data ;
2947 
2948     if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size,
2949                                    p_manager)) {
2950         opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2951         return OPJ_FALSE;
2952     }
2953 
2954     if (p_header_size != 0) {
2955         opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2956         return OPJ_FALSE;
2957     }
2958     return OPJ_TRUE;
2959 }
2960 
opj_j2k_write_qcd(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)2961 static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k,
2962                                   opj_stream_private_t *p_stream,
2963                                   opj_event_mgr_t * p_manager
2964                                  )
2965 {
2966     OPJ_UINT32 l_qcd_size, l_remaining_size;
2967     OPJ_BYTE * l_current_data = 00;
2968 
2969     /* preconditions */
2970     assert(p_j2k != 00);
2971     assert(p_manager != 00);
2972     assert(p_stream != 00);
2973 
2974     l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
2975                  0);
2976     l_remaining_size = l_qcd_size;
2977 
2978     if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2979         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2980                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
2981         if (! new_header_tile_data) {
2982             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2983             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2984             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2985             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
2986             return OPJ_FALSE;
2987         }
2988         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2989         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2990     }
2991 
2992     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2993 
2994     opj_write_bytes(l_current_data, J2K_MS_QCD, 2);         /* QCD */
2995     l_current_data += 2;
2996 
2997     opj_write_bytes(l_current_data, l_qcd_size - 2, 2);     /* L_QCD */
2998     l_current_data += 2;
2999 
3000     l_remaining_size -= 4;
3001 
3002     if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0,
3003                                   l_current_data, &l_remaining_size, p_manager)) {
3004         opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
3005         return OPJ_FALSE;
3006     }
3007 
3008     if (l_remaining_size != 0) {
3009         opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
3010         return OPJ_FALSE;
3011     }
3012 
3013     if (opj_stream_write_data(p_stream,
3014                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size,
3015                               p_manager) != l_qcd_size) {
3016         return OPJ_FALSE;
3017     }
3018 
3019     return OPJ_TRUE;
3020 }
3021 
3022 /**
3023  * Reads a QCD marker (Quantization defaults)
3024  * @param       p_header_data   the data contained in the QCD box.
3025  * @param       p_j2k                   the jpeg2000 codec.
3026  * @param       p_header_size   the size of the data contained in the QCD marker.
3027  * @param       p_manager               the user event manager.
3028 */
opj_j2k_read_qcd(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3029 static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k,
3030                                  OPJ_BYTE * p_header_data,
3031                                  OPJ_UINT32 p_header_size,
3032                                  opj_event_mgr_t * p_manager
3033                                 )
3034 {
3035     /* preconditions */
3036     assert(p_header_data != 00);
3037     assert(p_j2k != 00);
3038     assert(p_manager != 00);
3039 
3040     if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size,
3041                                  p_manager)) {
3042         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3043         return OPJ_FALSE;
3044     }
3045 
3046     if (p_header_size != 0) {
3047         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3048         return OPJ_FALSE;
3049     }
3050 
3051     /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3052     opj_j2k_copy_tile_quantization_parameters(p_j2k);
3053 
3054     return OPJ_TRUE;
3055 }
3056 
opj_j2k_write_qcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_comp_no,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3057 static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k,
3058                                   OPJ_UINT32 p_comp_no,
3059                                   opj_stream_private_t *p_stream,
3060                                   opj_event_mgr_t * p_manager
3061                                  )
3062 {
3063     OPJ_UINT32 l_qcc_size, l_remaining_size;
3064 
3065     /* preconditions */
3066     assert(p_j2k != 00);
3067     assert(p_manager != 00);
3068     assert(p_stream != 00);
3069 
3070     l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
3071                  p_comp_no);
3072     l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1;
3073     l_remaining_size = l_qcc_size;
3074 
3075     if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3076         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
3077                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
3078         if (! new_header_tile_data) {
3079             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3080             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3081             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3082             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
3083             return OPJ_FALSE;
3084         }
3085         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3086         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3087     }
3088 
3089     opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no,
3090                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
3091                                 p_manager);
3092 
3093     if (opj_stream_write_data(p_stream,
3094                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size,
3095                               p_manager) != l_qcc_size) {
3096         return OPJ_FALSE;
3097     }
3098 
3099     return OPJ_TRUE;
3100 }
3101 
opj_j2k_compare_qcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_first_comp_no,OPJ_UINT32 p_second_comp_no)3102 static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k,
3103                                     OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
3104 {
3105     return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number,
3106                                      p_first_comp_no, p_second_comp_no);
3107 }
3108 
opj_j2k_write_qcc_in_memory(opj_j2k_t * p_j2k,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,opj_event_mgr_t * p_manager)3109 static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
3110                                         OPJ_UINT32 p_comp_no,
3111                                         OPJ_BYTE * p_data,
3112                                         OPJ_UINT32 * p_data_written,
3113                                         opj_event_mgr_t * p_manager
3114                                        )
3115 {
3116     OPJ_UINT32 l_qcc_size, l_remaining_size;
3117     OPJ_BYTE * l_current_data = 00;
3118 
3119     /* preconditions */
3120     assert(p_j2k != 00);
3121     assert(p_manager != 00);
3122 
3123     l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
3124                  p_comp_no);
3125     l_remaining_size = l_qcc_size;
3126 
3127     l_current_data = p_data;
3128 
3129     opj_write_bytes(l_current_data, J2K_MS_QCC, 2);         /* QCC */
3130     l_current_data += 2;
3131 
3132     if (p_j2k->m_private_image->numcomps <= 256) {
3133         --l_qcc_size;
3134 
3135         opj_write_bytes(l_current_data, l_qcc_size - 2, 2);     /* L_QCC */
3136         l_current_data += 2;
3137 
3138         opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
3139         ++l_current_data;
3140 
3141         /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3142         l_remaining_size -= 6;
3143     } else {
3144         opj_write_bytes(l_current_data, l_qcc_size - 2, 2);     /* L_QCC */
3145         l_current_data += 2;
3146 
3147         opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
3148         l_current_data += 2;
3149 
3150         l_remaining_size -= 6;
3151     }
3152 
3153     opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no,
3154                             l_current_data, &l_remaining_size, p_manager);
3155 
3156     *p_data_written = l_qcc_size;
3157 }
3158 
opj_j2k_get_max_qcc_size(opj_j2k_t * p_j2k)3159 static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k)
3160 {
3161     return opj_j2k_get_max_coc_size(p_j2k);
3162 }
3163 
3164 /**
3165  * Reads a QCC marker (Quantization component)
3166  * @param       p_header_data   the data contained in the QCC box.
3167  * @param       p_j2k                   the jpeg2000 codec.
3168  * @param       p_header_size   the size of the data contained in the QCC marker.
3169  * @param       p_manager               the user event manager.
3170 */
opj_j2k_read_qcc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3171 static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k,
3172                                  OPJ_BYTE * p_header_data,
3173                                  OPJ_UINT32 p_header_size,
3174                                  opj_event_mgr_t * p_manager
3175                                 )
3176 {
3177     OPJ_UINT32 l_num_comp, l_comp_no;
3178 
3179     /* preconditions */
3180     assert(p_header_data != 00);
3181     assert(p_j2k != 00);
3182     assert(p_manager != 00);
3183 
3184     l_num_comp = p_j2k->m_private_image->numcomps;
3185 
3186     if (l_num_comp <= 256) {
3187         if (p_header_size < 1) {
3188             opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3189             return OPJ_FALSE;
3190         }
3191         opj_read_bytes(p_header_data, &l_comp_no, 1);
3192         ++p_header_data;
3193         --p_header_size;
3194     } else {
3195         if (p_header_size < 2) {
3196             opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3197             return OPJ_FALSE;
3198         }
3199         opj_read_bytes(p_header_data, &l_comp_no, 2);
3200         p_header_data += 2;
3201         p_header_size -= 2;
3202     }
3203 
3204 #ifdef USE_JPWL
3205     if (p_j2k->m_cp.correct) {
3206 
3207         static OPJ_UINT32 backup_compno = 0;
3208 
3209         /* compno is negative or larger than the number of components!!! */
3210         if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3211             opj_event_msg(p_manager, EVT_ERROR,
3212                           "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3213                           l_comp_no, l_num_comp);
3214             if (!JPWL_ASSUME) {
3215                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
3216                 return OPJ_FALSE;
3217             }
3218             /* we try to correct */
3219             l_comp_no = backup_compno % l_num_comp;
3220             opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
3221                           "- setting component number to %d\n",
3222                           l_comp_no);
3223         }
3224 
3225         /* keep your private count of tiles */
3226         backup_compno++;
3227     };
3228 #endif /* USE_JPWL */
3229 
3230     if (l_comp_no >= p_j2k->m_private_image->numcomps) {
3231         opj_event_msg(p_manager, EVT_ERROR,
3232                       "Invalid component number: %d, regarding the number of components %d\n",
3233                       l_comp_no, p_j2k->m_private_image->numcomps);
3234         return OPJ_FALSE;
3235     }
3236 
3237     if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size,
3238                                  p_manager)) {
3239         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3240         return OPJ_FALSE;
3241     }
3242 
3243     if (p_header_size != 0) {
3244         opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3245         return OPJ_FALSE;
3246     }
3247 
3248     return OPJ_TRUE;
3249 }
3250 
opj_j2k_write_poc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)3251 static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k,
3252                                   opj_stream_private_t *p_stream,
3253                                   opj_event_mgr_t * p_manager
3254                                  )
3255 {
3256     OPJ_UINT32 l_nb_comp;
3257     OPJ_UINT32 l_nb_poc;
3258     OPJ_UINT32 l_poc_size;
3259     OPJ_UINT32 l_written_size = 0;
3260     opj_tcp_t *l_tcp = 00;
3261     OPJ_UINT32 l_poc_room;
3262 
3263     /* preconditions */
3264     assert(p_j2k != 00);
3265     assert(p_manager != 00);
3266     assert(p_stream != 00);
3267 
3268     l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3269     l_nb_comp = p_j2k->m_private_image->numcomps;
3270     l_nb_poc = 1 + l_tcp->numpocs;
3271 
3272     if (l_nb_comp <= 256) {
3273         l_poc_room = 1;
3274     } else {
3275         l_poc_room = 2;
3276     }
3277     l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3278 
3279     if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3280         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
3281                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
3282         if (! new_header_tile_data) {
3283             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3284             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3285             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3286             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
3287             return OPJ_FALSE;
3288         }
3289         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3290         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3291     }
3292 
3293     opj_j2k_write_poc_in_memory(p_j2k,
3294                                 p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size,
3295                                 p_manager);
3296 
3297     if (opj_stream_write_data(p_stream,
3298                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size,
3299                               p_manager) != l_poc_size) {
3300         return OPJ_FALSE;
3301     }
3302 
3303     return OPJ_TRUE;
3304 }
3305 
opj_j2k_write_poc_in_memory(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,opj_event_mgr_t * p_manager)3306 static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
3307                                         OPJ_BYTE * p_data,
3308                                         OPJ_UINT32 * p_data_written,
3309                                         opj_event_mgr_t * p_manager
3310                                        )
3311 {
3312     OPJ_UINT32 i;
3313     OPJ_BYTE * l_current_data = 00;
3314     OPJ_UINT32 l_nb_comp;
3315     OPJ_UINT32 l_nb_poc;
3316     OPJ_UINT32 l_poc_size;
3317     opj_image_t *l_image = 00;
3318     opj_tcp_t *l_tcp = 00;
3319     opj_tccp_t *l_tccp = 00;
3320     opj_poc_t *l_current_poc = 00;
3321     OPJ_UINT32 l_poc_room;
3322 
3323     /* preconditions */
3324     assert(p_j2k != 00);
3325     assert(p_manager != 00);
3326 
3327     OPJ_UNUSED(p_manager);
3328 
3329     l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3330     l_tccp = &l_tcp->tccps[0];
3331     l_image = p_j2k->m_private_image;
3332     l_nb_comp = l_image->numcomps;
3333     l_nb_poc = 1 + l_tcp->numpocs;
3334 
3335     if (l_nb_comp <= 256) {
3336         l_poc_room = 1;
3337     } else {
3338         l_poc_room = 2;
3339     }
3340 
3341     l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3342 
3343     l_current_data = p_data;
3344 
3345     opj_write_bytes(l_current_data, J2K_MS_POC,
3346                     2);                                   /* POC  */
3347     l_current_data += 2;
3348 
3349     opj_write_bytes(l_current_data, l_poc_size - 2,
3350                     2);                                 /* Lpoc */
3351     l_current_data += 2;
3352 
3353     l_current_poc =  l_tcp->pocs;
3354     for (i = 0; i < l_nb_poc; ++i) {
3355         opj_write_bytes(l_current_data, l_current_poc->resno0,
3356                         1);                                /* RSpoc_i */
3357         ++l_current_data;
3358 
3359         opj_write_bytes(l_current_data, l_current_poc->compno0,
3360                         l_poc_room);              /* CSpoc_i */
3361         l_current_data += l_poc_room;
3362 
3363         opj_write_bytes(l_current_data, l_current_poc->layno1,
3364                         2);                                /* LYEpoc_i */
3365         l_current_data += 2;
3366 
3367         opj_write_bytes(l_current_data, l_current_poc->resno1,
3368                         1);                                /* REpoc_i */
3369         ++l_current_data;
3370 
3371         opj_write_bytes(l_current_data, l_current_poc->compno1,
3372                         l_poc_room);              /* CEpoc_i */
3373         l_current_data += l_poc_room;
3374 
3375         opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg,
3376                         1);   /* Ppoc_i */
3377         ++l_current_data;
3378 
3379         /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3380         l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
3381                                 l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
3382         l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
3383                                 l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
3384         l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
3385                                  l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
3386 
3387         ++l_current_poc;
3388     }
3389 
3390     *p_data_written = l_poc_size;
3391 }
3392 
opj_j2k_get_max_poc_size(opj_j2k_t * p_j2k)3393 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
3394 {
3395     opj_tcp_t * l_tcp = 00;
3396     OPJ_UINT32 l_nb_tiles = 0;
3397     OPJ_UINT32 l_max_poc = 0;
3398     OPJ_UINT32 i;
3399 
3400     l_tcp = p_j2k->m_cp.tcps;
3401     l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3402 
3403     for (i = 0; i < l_nb_tiles; ++i) {
3404         l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs);
3405         ++l_tcp;
3406     }
3407 
3408     ++l_max_poc;
3409 
3410     return 4 + 9 * l_max_poc;
3411 }
3412 
opj_j2k_get_max_toc_size(opj_j2k_t * p_j2k)3413 static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k)
3414 {
3415     OPJ_UINT32 i;
3416     OPJ_UINT32 l_nb_tiles;
3417     OPJ_UINT32 l_max = 0;
3418     opj_tcp_t * l_tcp = 00;
3419 
3420     l_tcp = p_j2k->m_cp.tcps;
3421     l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3422 
3423     for (i = 0; i < l_nb_tiles; ++i) {
3424         l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts);
3425 
3426         ++l_tcp;
3427     }
3428 
3429     return 12 * l_max;
3430 }
3431 
opj_j2k_get_specific_header_sizes(opj_j2k_t * p_j2k)3432 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
3433 {
3434     OPJ_UINT32 l_nb_bytes = 0;
3435     OPJ_UINT32 l_nb_comps;
3436     OPJ_UINT32 l_coc_bytes, l_qcc_bytes;
3437 
3438     l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3439     l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3440 
3441     if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
3442         l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3443         l_nb_bytes += l_nb_comps * l_coc_bytes;
3444 
3445         l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3446         l_nb_bytes += l_nb_comps * l_qcc_bytes;
3447     }
3448 
3449     l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3450 
3451     /*** DEVELOPER CORNER, Add room for your headers ***/
3452 
3453     return l_nb_bytes;
3454 }
3455 
3456 /**
3457  * Reads a POC marker (Progression Order Change)
3458  *
3459  * @param       p_header_data   the data contained in the POC box.
3460  * @param       p_j2k                   the jpeg2000 codec.
3461  * @param       p_header_size   the size of the data contained in the POC marker.
3462  * @param       p_manager               the user event manager.
3463 */
opj_j2k_read_poc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3464 static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k,
3465                                  OPJ_BYTE * p_header_data,
3466                                  OPJ_UINT32 p_header_size,
3467                                  opj_event_mgr_t * p_manager
3468                                 )
3469 {
3470     OPJ_UINT32 i, l_nb_comp, l_tmp;
3471     opj_image_t * l_image = 00;
3472     OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3473     OPJ_UINT32 l_chunk_size, l_comp_room;
3474 
3475     opj_cp_t *l_cp = 00;
3476     opj_tcp_t *l_tcp = 00;
3477     opj_poc_t *l_current_poc = 00;
3478 
3479     /* preconditions */
3480     assert(p_header_data != 00);
3481     assert(p_j2k != 00);
3482     assert(p_manager != 00);
3483 
3484     l_image = p_j2k->m_private_image;
3485     l_nb_comp = l_image->numcomps;
3486     if (l_nb_comp <= 256) {
3487         l_comp_room = 1;
3488     } else {
3489         l_comp_room = 2;
3490     }
3491     l_chunk_size = 5 + 2 * l_comp_room;
3492     l_current_poc_nb = p_header_size / l_chunk_size;
3493     l_current_poc_remaining = p_header_size % l_chunk_size;
3494 
3495     if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3496         opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
3497         return OPJ_FALSE;
3498     }
3499 
3500     l_cp = &(p_j2k->m_cp);
3501     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3502             &l_cp->tcps[p_j2k->m_current_tile_number] :
3503             p_j2k->m_specific_param.m_decoder.m_default_tcp;
3504     l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3505     l_current_poc_nb += l_old_poc_nb;
3506 
3507     if (l_current_poc_nb >= J2K_MAX_POCS) {
3508         opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
3509         return OPJ_FALSE;
3510     }
3511 
3512     /* now poc is in use.*/
3513     l_tcp->POC = 1;
3514 
3515     l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3516     for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3517         opj_read_bytes(p_header_data, &(l_current_poc->resno0),
3518                        1);                               /* RSpoc_i */
3519         ++p_header_data;
3520         opj_read_bytes(p_header_data, &(l_current_poc->compno0),
3521                        l_comp_room);  /* CSpoc_i */
3522         p_header_data += l_comp_room;
3523         opj_read_bytes(p_header_data, &(l_current_poc->layno1),
3524                        2);                               /* LYEpoc_i */
3525         /* make sure layer end is in acceptable bounds */
3526         l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
3527         p_header_data += 2;
3528         opj_read_bytes(p_header_data, &(l_current_poc->resno1),
3529                        1);                               /* REpoc_i */
3530         ++p_header_data;
3531         opj_read_bytes(p_header_data, &(l_current_poc->compno1),
3532                        l_comp_room);  /* CEpoc_i */
3533         p_header_data += l_comp_room;
3534         opj_read_bytes(p_header_data, &l_tmp,
3535                        1);                                                                 /* Ppoc_i */
3536         ++p_header_data;
3537         l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3538         /* make sure comp is in acceptable bounds */
3539         l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
3540         ++l_current_poc;
3541     }
3542 
3543     l_tcp->numpocs = l_current_poc_nb - 1;
3544     return OPJ_TRUE;
3545 }
3546 
3547 /**
3548  * Reads a CRG marker (Component registration)
3549  *
3550  * @param       p_header_data   the data contained in the TLM box.
3551  * @param       p_j2k                   the jpeg2000 codec.
3552  * @param       p_header_size   the size of the data contained in the TLM marker.
3553  * @param       p_manager               the user event manager.
3554 */
opj_j2k_read_crg(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3555 static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k,
3556                                  OPJ_BYTE * p_header_data,
3557                                  OPJ_UINT32 p_header_size,
3558                                  opj_event_mgr_t * p_manager
3559                                 )
3560 {
3561     OPJ_UINT32 l_nb_comp;
3562     /* preconditions */
3563     assert(p_header_data != 00);
3564     assert(p_j2k != 00);
3565     assert(p_manager != 00);
3566 
3567     OPJ_UNUSED(p_header_data);
3568 
3569     l_nb_comp = p_j2k->m_private_image->numcomps;
3570 
3571     if (p_header_size != l_nb_comp * 4) {
3572         opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3573         return OPJ_FALSE;
3574     }
3575     /* Do not care of this at the moment since only local variables are set here */
3576     /*
3577     for
3578             (i = 0; i < l_nb_comp; ++i)
3579     {
3580             opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
3581             p_header_data+=2;
3582             opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
3583             p_header_data+=2;
3584     }
3585     */
3586     return OPJ_TRUE;
3587 }
3588 
3589 /**
3590  * Reads a TLM marker (Tile Length Marker)
3591  *
3592  * @param       p_header_data   the data contained in the TLM box.
3593  * @param       p_j2k                   the jpeg2000 codec.
3594  * @param       p_header_size   the size of the data contained in the TLM marker.
3595  * @param       p_manager               the user event manager.
3596 */
opj_j2k_read_tlm(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3597 static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k,
3598                                  OPJ_BYTE * p_header_data,
3599                                  OPJ_UINT32 p_header_size,
3600                                  opj_event_mgr_t * p_manager
3601                                 )
3602 {
3603     OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient,
3604                l_Ptlm_size;
3605     /* preconditions */
3606     assert(p_header_data != 00);
3607     assert(p_j2k != 00);
3608     assert(p_manager != 00);
3609 
3610     OPJ_UNUSED(p_j2k);
3611 
3612     if (p_header_size < 2) {
3613         opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3614         return OPJ_FALSE;
3615     }
3616     p_header_size -= 2;
3617 
3618     opj_read_bytes(p_header_data, &l_Ztlm,
3619                    1);                              /* Ztlm */
3620     ++p_header_data;
3621     opj_read_bytes(p_header_data, &l_Stlm,
3622                    1);                              /* Stlm */
3623     ++p_header_data;
3624 
3625     l_ST = ((l_Stlm >> 4) & 0x3);
3626     l_SP = (l_Stlm >> 6) & 0x1;
3627 
3628     l_Ptlm_size = (l_SP + 1) * 2;
3629     l_quotient = l_Ptlm_size + l_ST;
3630 
3631     l_tot_num_tp_remaining = p_header_size % l_quotient;
3632 
3633     if (l_tot_num_tp_remaining != 0) {
3634         opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3635         return OPJ_FALSE;
3636     }
3637     /* FIXME Do not care of this at the moment since only local variables are set here */
3638     /*
3639     for
3640             (i = 0; i < l_tot_num_tp; ++i)
3641     {
3642             opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
3643             p_header_data += l_ST;
3644             opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
3645             p_header_data += l_Ptlm_size;
3646     }*/
3647     return OPJ_TRUE;
3648 }
3649 
3650 /**
3651  * Reads a PLM marker (Packet length, main header marker)
3652  *
3653  * @param       p_header_data   the data contained in the TLM box.
3654  * @param       p_j2k                   the jpeg2000 codec.
3655  * @param       p_header_size   the size of the data contained in the TLM marker.
3656  * @param       p_manager               the user event manager.
3657 */
opj_j2k_read_plm(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3658 static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k,
3659                                  OPJ_BYTE * p_header_data,
3660                                  OPJ_UINT32 p_header_size,
3661                                  opj_event_mgr_t * p_manager
3662                                 )
3663 {
3664     /* preconditions */
3665     assert(p_header_data != 00);
3666     assert(p_j2k != 00);
3667     assert(p_manager != 00);
3668 
3669     OPJ_UNUSED(p_j2k);
3670     OPJ_UNUSED(p_header_data);
3671 
3672     if (p_header_size < 1) {
3673         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3674         return OPJ_FALSE;
3675     }
3676     /* Do not care of this at the moment since only local variables are set here */
3677     /*
3678     opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
3679     ++p_header_data;
3680     --p_header_size;
3681 
3682     while
3683             (p_header_size > 0)
3684     {
3685             opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
3686             ++p_header_data;
3687             p_header_size -= (1+l_Nplm);
3688             if
3689                     (p_header_size < 0)
3690             {
3691                     opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3692                     return false;
3693             }
3694             for
3695                     (i = 0; i < l_Nplm; ++i)
3696             {
3697                     opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
3698                     ++p_header_data;
3699                     // take only the last seven bytes
3700                     l_packet_len |= (l_tmp & 0x7f);
3701                     if
3702                             (l_tmp & 0x80)
3703                     {
3704                             l_packet_len <<= 7;
3705                     }
3706                     else
3707                     {
3708             // store packet length and proceed to next packet
3709                             l_packet_len = 0;
3710                     }
3711             }
3712             if
3713                     (l_packet_len != 0)
3714             {
3715                     opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3716                     return false;
3717             }
3718     }
3719     */
3720     return OPJ_TRUE;
3721 }
3722 
3723 /**
3724  * Reads a PLT marker (Packet length, tile-part header)
3725  *
3726  * @param       p_header_data   the data contained in the PLT box.
3727  * @param       p_j2k                   the jpeg2000 codec.
3728  * @param       p_header_size   the size of the data contained in the PLT marker.
3729  * @param       p_manager               the user event manager.
3730 */
opj_j2k_read_plt(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3731 static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k,
3732                                  OPJ_BYTE * p_header_data,
3733                                  OPJ_UINT32 p_header_size,
3734                                  opj_event_mgr_t * p_manager
3735                                 )
3736 {
3737     OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3738 
3739     /* preconditions */
3740     assert(p_header_data != 00);
3741     assert(p_j2k != 00);
3742     assert(p_manager != 00);
3743 
3744     OPJ_UNUSED(p_j2k);
3745 
3746     if (p_header_size < 1) {
3747         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3748         return OPJ_FALSE;
3749     }
3750 
3751     opj_read_bytes(p_header_data, &l_Zplt, 1);              /* Zplt */
3752     ++p_header_data;
3753     --p_header_size;
3754 
3755     for (i = 0; i < p_header_size; ++i) {
3756         opj_read_bytes(p_header_data, &l_tmp, 1);       /* Iplt_ij */
3757         ++p_header_data;
3758         /* take only the last seven bytes */
3759         l_packet_len |= (l_tmp & 0x7f);
3760         if (l_tmp & 0x80) {
3761             l_packet_len <<= 7;
3762         } else {
3763             /* store packet length and proceed to next packet */
3764             l_packet_len = 0;
3765         }
3766     }
3767 
3768     if (l_packet_len != 0) {
3769         opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3770         return OPJ_FALSE;
3771     }
3772 
3773     return OPJ_TRUE;
3774 }
3775 
3776 /**
3777  * Reads a PPM marker (Packed packet headers, main header)
3778  *
3779  * @param       p_header_data   the data contained in the POC box.
3780  * @param       p_j2k                   the jpeg2000 codec.
3781  * @param       p_header_size   the size of the data contained in the POC marker.
3782  * @param       p_manager               the user event manager.
3783  */
3784 
opj_j2k_read_ppm(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)3785 static OPJ_BOOL opj_j2k_read_ppm(
3786     opj_j2k_t *p_j2k,
3787     OPJ_BYTE * p_header_data,
3788     OPJ_UINT32 p_header_size,
3789     opj_event_mgr_t * p_manager)
3790 {
3791     opj_cp_t *l_cp = 00;
3792     OPJ_UINT32 l_Z_ppm;
3793 
3794     /* preconditions */
3795     assert(p_header_data != 00);
3796     assert(p_j2k != 00);
3797     assert(p_manager != 00);
3798 
3799     /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */
3800     if (p_header_size < 2) {
3801         opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3802         return OPJ_FALSE;
3803     }
3804 
3805     l_cp = &(p_j2k->m_cp);
3806     l_cp->ppm = 1;
3807 
3808     opj_read_bytes(p_header_data, &l_Z_ppm, 1);             /* Z_ppm */
3809     ++p_header_data;
3810     --p_header_size;
3811 
3812     /* check allocation needed */
3813     if (l_cp->ppm_markers == NULL) { /* first PPM marker */
3814         OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
3815         assert(l_cp->ppm_markers_count == 0U);
3816 
3817         l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
3818         if (l_cp->ppm_markers == NULL) {
3819             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3820             return OPJ_FALSE;
3821         }
3822         l_cp->ppm_markers_count = l_newCount;
3823     } else if (l_cp->ppm_markers_count <= l_Z_ppm) {
3824         OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
3825         opj_ppx *new_ppm_markers;
3826         new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers,
3827                           l_newCount * sizeof(opj_ppx));
3828         if (new_ppm_markers == NULL) {
3829             /* clean up to be done on l_cp destruction */
3830             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3831             return OPJ_FALSE;
3832         }
3833         l_cp->ppm_markers = new_ppm_markers;
3834         memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0,
3835                (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx));
3836         l_cp->ppm_markers_count = l_newCount;
3837     }
3838 
3839     if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) {
3840         /* clean up to be done on l_cp destruction */
3841         opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm);
3842         return OPJ_FALSE;
3843     }
3844 
3845     l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
3846     if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) {
3847         /* clean up to be done on l_cp destruction */
3848         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3849         return OPJ_FALSE;
3850     }
3851     l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size;
3852     memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size);
3853 
3854     return OPJ_TRUE;
3855 }
3856 
3857 /**
3858  * Merges all PPM markers read (Packed headers, main header)
3859  *
3860  * @param       p_cp      main coding parameters.
3861  * @param       p_manager the user event manager.
3862  */
opj_j2k_merge_ppm(opj_cp_t * p_cp,opj_event_mgr_t * p_manager)3863 static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager)
3864 {
3865     OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining;
3866 
3867     /* preconditions */
3868     assert(p_cp != 00);
3869     assert(p_manager != 00);
3870     assert(p_cp->ppm_buffer == NULL);
3871 
3872     if (p_cp->ppm == 0U) {
3873         return OPJ_TRUE;
3874     }
3875 
3876     l_ppm_data_size = 0U;
3877     l_N_ppm_remaining = 0U;
3878     for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
3879         if (p_cp->ppm_markers[i].m_data !=
3880                 NULL) { /* standard doesn't seem to require contiguous Zppm */
3881             OPJ_UINT32 l_N_ppm;
3882             OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
3883             const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
3884 
3885             if (l_N_ppm_remaining >= l_data_size) {
3886                 l_N_ppm_remaining -= l_data_size;
3887                 l_data_size = 0U;
3888             } else {
3889                 l_data += l_N_ppm_remaining;
3890                 l_data_size -= l_N_ppm_remaining;
3891                 l_N_ppm_remaining = 0U;
3892             }
3893 
3894             if (l_data_size > 0U) {
3895                 do {
3896                     /* read Nppm */
3897                     if (l_data_size < 4U) {
3898                         /* clean up to be done on l_cp destruction */
3899                         opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
3900                         return OPJ_FALSE;
3901                     }
3902                     opj_read_bytes(l_data, &l_N_ppm, 4);
3903                     l_data += 4;
3904                     l_data_size -= 4;
3905                     l_ppm_data_size +=
3906                         l_N_ppm; /* can't overflow, max 256 markers of max 65536 bytes, that is when PPM markers are not corrupted which is checked elsewhere */
3907 
3908                     if (l_data_size >= l_N_ppm) {
3909                         l_data_size -= l_N_ppm;
3910                         l_data += l_N_ppm;
3911                     } else {
3912                         l_N_ppm_remaining = l_N_ppm - l_data_size;
3913                         l_data_size = 0U;
3914                     }
3915                 } while (l_data_size > 0U);
3916             }
3917         }
3918     }
3919 
3920     if (l_N_ppm_remaining != 0U) {
3921         /* clean up to be done on l_cp destruction */
3922         opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n");
3923         return OPJ_FALSE;
3924     }
3925 
3926     p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size);
3927     if (p_cp->ppm_buffer == 00) {
3928         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3929         return OPJ_FALSE;
3930     }
3931     p_cp->ppm_len = l_ppm_data_size;
3932     l_ppm_data_size = 0U;
3933     l_N_ppm_remaining = 0U;
3934     for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
3935         if (p_cp->ppm_markers[i].m_data !=
3936                 NULL) { /* standard doesn't seem to require contiguous Zppm */
3937             OPJ_UINT32 l_N_ppm;
3938             OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
3939             const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
3940 
3941             if (l_N_ppm_remaining >= l_data_size) {
3942                 memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
3943                 l_ppm_data_size += l_data_size;
3944                 l_N_ppm_remaining -= l_data_size;
3945                 l_data_size = 0U;
3946             } else {
3947                 memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining);
3948                 l_ppm_data_size += l_N_ppm_remaining;
3949                 l_data += l_N_ppm_remaining;
3950                 l_data_size -= l_N_ppm_remaining;
3951                 l_N_ppm_remaining = 0U;
3952             }
3953 
3954             if (l_data_size > 0U) {
3955                 do {
3956                     /* read Nppm */
3957                     if (l_data_size < 4U) {
3958                         /* clean up to be done on l_cp destruction */
3959                         opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
3960                         return OPJ_FALSE;
3961                     }
3962                     opj_read_bytes(l_data, &l_N_ppm, 4);
3963                     l_data += 4;
3964                     l_data_size -= 4;
3965 
3966                     if (l_data_size >= l_N_ppm) {
3967                         memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm);
3968                         l_ppm_data_size += l_N_ppm;
3969                         l_data_size -= l_N_ppm;
3970                         l_data += l_N_ppm;
3971                     } else {
3972                         memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
3973                         l_ppm_data_size += l_data_size;
3974                         l_N_ppm_remaining = l_N_ppm - l_data_size;
3975                         l_data_size = 0U;
3976                     }
3977                 } while (l_data_size > 0U);
3978             }
3979             opj_free(p_cp->ppm_markers[i].m_data);
3980             p_cp->ppm_markers[i].m_data = NULL;
3981             p_cp->ppm_markers[i].m_data_size = 0U;
3982         }
3983     }
3984 
3985     p_cp->ppm_data = p_cp->ppm_buffer;
3986     p_cp->ppm_data_size = p_cp->ppm_len;
3987 
3988     p_cp->ppm_markers_count = 0U;
3989     opj_free(p_cp->ppm_markers);
3990     p_cp->ppm_markers = NULL;
3991 
3992     return OPJ_TRUE;
3993 }
3994 
3995 /**
3996  * Reads a PPT marker (Packed packet headers, tile-part header)
3997  *
3998  * @param       p_header_data   the data contained in the PPT box.
3999  * @param       p_j2k                   the jpeg2000 codec.
4000  * @param       p_header_size   the size of the data contained in the PPT marker.
4001  * @param       p_manager               the user event manager.
4002 */
opj_j2k_read_ppt(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)4003 static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k,
4004                                  OPJ_BYTE * p_header_data,
4005                                  OPJ_UINT32 p_header_size,
4006                                  opj_event_mgr_t * p_manager
4007                                 )
4008 {
4009     opj_cp_t *l_cp = 00;
4010     opj_tcp_t *l_tcp = 00;
4011     OPJ_UINT32 l_Z_ppt;
4012 
4013     /* preconditions */
4014     assert(p_header_data != 00);
4015     assert(p_j2k != 00);
4016     assert(p_manager != 00);
4017 
4018     /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */
4019     if (p_header_size < 2) {
4020         opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4021         return OPJ_FALSE;
4022     }
4023 
4024     l_cp = &(p_j2k->m_cp);
4025     if (l_cp->ppm) {
4026         opj_event_msg(p_manager, EVT_ERROR,
4027                       "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
4028         return OPJ_FALSE;
4029     }
4030 
4031     l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4032     l_tcp->ppt = 1;
4033 
4034     opj_read_bytes(p_header_data, &l_Z_ppt, 1);             /* Z_ppt */
4035     ++p_header_data;
4036     --p_header_size;
4037 
4038     /* check allocation needed */
4039     if (l_tcp->ppt_markers == NULL) { /* first PPT marker */
4040         OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
4041         assert(l_tcp->ppt_markers_count == 0U);
4042 
4043         l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
4044         if (l_tcp->ppt_markers == NULL) {
4045             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4046             return OPJ_FALSE;
4047         }
4048         l_tcp->ppt_markers_count = l_newCount;
4049     } else if (l_tcp->ppt_markers_count <= l_Z_ppt) {
4050         OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
4051         opj_ppx *new_ppt_markers;
4052         new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers,
4053                           l_newCount * sizeof(opj_ppx));
4054         if (new_ppt_markers == NULL) {
4055             /* clean up to be done on l_tcp destruction */
4056             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4057             return OPJ_FALSE;
4058         }
4059         l_tcp->ppt_markers = new_ppt_markers;
4060         memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0,
4061                (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx));
4062         l_tcp->ppt_markers_count = l_newCount;
4063     }
4064 
4065     if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) {
4066         /* clean up to be done on l_tcp destruction */
4067         opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt);
4068         return OPJ_FALSE;
4069     }
4070 
4071     l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
4072     if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) {
4073         /* clean up to be done on l_tcp destruction */
4074         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4075         return OPJ_FALSE;
4076     }
4077     l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size;
4078     memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size);
4079     return OPJ_TRUE;
4080 }
4081 
4082 /**
4083  * Merges all PPT markers read (Packed packet headers, tile-part header)
4084  *
4085  * @param       p_tcp   the tile.
4086  * @param       p_manager               the user event manager.
4087  */
opj_j2k_merge_ppt(opj_tcp_t * p_tcp,opj_event_mgr_t * p_manager)4088 static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager)
4089 {
4090     OPJ_UINT32 i, l_ppt_data_size;
4091     /* preconditions */
4092     assert(p_tcp != 00);
4093     assert(p_manager != 00);
4094 
4095     if (p_tcp->ppt_buffer != NULL) {
4096         opj_event_msg(p_manager, EVT_ERROR,
4097                       "opj_j2k_merge_ppt() has already been called\n");
4098         return OPJ_FALSE;
4099     }
4100 
4101     if (p_tcp->ppt == 0U) {
4102         return OPJ_TRUE;
4103     }
4104 
4105     l_ppt_data_size = 0U;
4106     for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
4107         l_ppt_data_size +=
4108             p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
4109     }
4110 
4111     p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size);
4112     if (p_tcp->ppt_buffer == 00) {
4113         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4114         return OPJ_FALSE;
4115     }
4116     p_tcp->ppt_len = l_ppt_data_size;
4117     l_ppt_data_size = 0U;
4118     for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
4119         if (p_tcp->ppt_markers[i].m_data !=
4120                 NULL) { /* standard doesn't seem to require contiguous Zppt */
4121             memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data,
4122                    p_tcp->ppt_markers[i].m_data_size);
4123             l_ppt_data_size +=
4124                 p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
4125 
4126             opj_free(p_tcp->ppt_markers[i].m_data);
4127             p_tcp->ppt_markers[i].m_data = NULL;
4128             p_tcp->ppt_markers[i].m_data_size = 0U;
4129         }
4130     }
4131 
4132     p_tcp->ppt_markers_count = 0U;
4133     opj_free(p_tcp->ppt_markers);
4134     p_tcp->ppt_markers = NULL;
4135 
4136     p_tcp->ppt_data = p_tcp->ppt_buffer;
4137     p_tcp->ppt_data_size = p_tcp->ppt_len;
4138     return OPJ_TRUE;
4139 }
4140 
opj_j2k_write_tlm(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4141 static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k,
4142                                   opj_stream_private_t *p_stream,
4143                                   opj_event_mgr_t * p_manager
4144                                  )
4145 {
4146     OPJ_BYTE * l_current_data = 00;
4147     OPJ_UINT32 l_tlm_size;
4148 
4149     /* preconditions */
4150     assert(p_j2k != 00);
4151     assert(p_manager != 00);
4152     assert(p_stream != 00);
4153 
4154     l_tlm_size = 6 + (5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4155 
4156     if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4157         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
4158                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
4159         if (! new_header_tile_data) {
4160             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
4161             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
4162             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
4163             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
4164             return OPJ_FALSE;
4165         }
4166         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
4167         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4168     }
4169 
4170     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4171 
4172     /* change the way data is written to avoid seeking if possible */
4173     /* TODO */
4174     p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4175 
4176     opj_write_bytes(l_current_data, J2K_MS_TLM,
4177                     2);                                   /* TLM */
4178     l_current_data += 2;
4179 
4180     opj_write_bytes(l_current_data, l_tlm_size - 2,
4181                     2);                                 /* Lpoc */
4182     l_current_data += 2;
4183 
4184     opj_write_bytes(l_current_data, 0,
4185                     1);                                                    /* Ztlm=0*/
4186     ++l_current_data;
4187 
4188     opj_write_bytes(l_current_data, 0x50,
4189                     1);                                                 /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4190     ++l_current_data;
4191 
4192     /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4193     if (opj_stream_write_data(p_stream,
4194                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size,
4195                               p_manager) != l_tlm_size) {
4196         return OPJ_FALSE;
4197     }
4198 
4199     return OPJ_TRUE;
4200 }
4201 
opj_j2k_write_sot(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 p_total_data_size,OPJ_UINT32 * p_data_written,const opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4202 static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
4203                                   OPJ_BYTE * p_data,
4204                                   OPJ_UINT32 p_total_data_size,
4205                                   OPJ_UINT32 * p_data_written,
4206                                   const opj_stream_private_t *p_stream,
4207                                   opj_event_mgr_t * p_manager
4208                                  )
4209 {
4210     /* preconditions */
4211     assert(p_j2k != 00);
4212     assert(p_manager != 00);
4213     assert(p_stream != 00);
4214 
4215     OPJ_UNUSED(p_stream);
4216 
4217     if (p_total_data_size < 12) {
4218         opj_event_msg(p_manager, EVT_ERROR,
4219                       "Not enough bytes in output buffer to write SOT marker\n");
4220         return OPJ_FALSE;
4221     }
4222 
4223     opj_write_bytes(p_data, J2K_MS_SOT,
4224                     2);                                 /* SOT */
4225     p_data += 2;
4226 
4227     opj_write_bytes(p_data, 10,
4228                     2);                                                   /* Lsot */
4229     p_data += 2;
4230 
4231     opj_write_bytes(p_data, p_j2k->m_current_tile_number,
4232                     2);                        /* Isot */
4233     p_data += 2;
4234 
4235     /* Psot  */
4236     p_data += 4;
4237 
4238     opj_write_bytes(p_data,
4239                     p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,
4240                     1);                        /* TPsot */
4241     ++p_data;
4242 
4243     opj_write_bytes(p_data,
4244                     p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,
4245                     1);                      /* TNsot */
4246     ++p_data;
4247 
4248     /* UniPG>> */
4249 #ifdef USE_JPWL
4250     /* update markers struct */
4251     /*
4252             OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4253     */
4254     assert(0 && "TODO");
4255 #endif /* USE_JPWL */
4256 
4257     * p_data_written = 12;
4258 
4259     return OPJ_TRUE;
4260 }
4261 
opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,OPJ_UINT32 * p_tile_no,OPJ_UINT32 * p_tot_len,OPJ_UINT32 * p_current_part,OPJ_UINT32 * p_num_parts,opj_event_mgr_t * p_manager)4262 static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE *  p_header_data,
4263                                        OPJ_UINT32  p_header_size,
4264                                        OPJ_UINT32* p_tile_no,
4265                                        OPJ_UINT32* p_tot_len,
4266                                        OPJ_UINT32* p_current_part,
4267                                        OPJ_UINT32* p_num_parts,
4268                                        opj_event_mgr_t * p_manager)
4269 {
4270     /* preconditions */
4271     assert(p_header_data != 00);
4272     assert(p_manager != 00);
4273 
4274     /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4275     if (p_header_size != 8) {
4276         opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4277         return OPJ_FALSE;
4278     }
4279 
4280     opj_read_bytes(p_header_data, p_tile_no, 2);    /* Isot */
4281     p_header_data += 2;
4282     opj_read_bytes(p_header_data, p_tot_len, 4);    /* Psot */
4283     p_header_data += 4;
4284     opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */
4285     ++p_header_data;
4286     opj_read_bytes(p_header_data, p_num_parts, 1);  /* TNsot */
4287     ++p_header_data;
4288     return OPJ_TRUE;
4289 }
4290 
opj_j2k_read_sot(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)4291 static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k,
4292                                  OPJ_BYTE * p_header_data,
4293                                  OPJ_UINT32 p_header_size,
4294                                  opj_event_mgr_t * p_manager)
4295 {
4296     opj_cp_t *l_cp = 00;
4297     opj_tcp_t *l_tcp = 00;
4298     OPJ_UINT32 l_tot_len, l_num_parts = 0;
4299     OPJ_UINT32 l_current_part;
4300     OPJ_UINT32 l_tile_x, l_tile_y;
4301 
4302     /* preconditions */
4303 
4304     assert(p_j2k != 00);
4305     assert(p_manager != 00);
4306 
4307     if (! opj_j2k_get_sot_values(p_header_data, p_header_size,
4308                                  &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts,
4309                                  p_manager)) {
4310         opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4311         return OPJ_FALSE;
4312     }
4313 #ifdef DEBUG_VERBOSE
4314     fprintf(stderr, "SOT %d %d %d %d\n",
4315             p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts);
4316 #endif
4317 
4318     l_cp = &(p_j2k->m_cp);
4319 
4320     /* testcase 2.pdf.SIGFPE.706.1112 */
4321     if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
4322         opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n",
4323                       p_j2k->m_current_tile_number);
4324         return OPJ_FALSE;
4325     }
4326 
4327     l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4328     l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4329     l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4330 
4331     if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 ||
4332             p_j2k->m_current_tile_number == (OPJ_UINT32)
4333             p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) {
4334         /* Do only this check if we decode all tile part headers, or if */
4335         /* we decode one precise tile. Otherwise the m_current_tile_part_number */
4336         /* might not be valid */
4337         /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */
4338         /* of https://github.com/uclouvain/openjpeg/issues/939 */
4339         /* We must avoid reading twice the same tile part number for a given tile */
4340         /* so as to avoid various issues, like opj_j2k_merge_ppt being called */
4341         /* several times. */
4342         /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */
4343         /* should appear in increasing order. */
4344         if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) {
4345             opj_event_msg(p_manager, EVT_ERROR,
4346                           "Invalid tile part index for tile number %d. "
4347                           "Got %d, expected %d\n",
4348                           p_j2k->m_current_tile_number,
4349                           l_current_part,
4350                           l_tcp->m_current_tile_part_number + 1);
4351             return OPJ_FALSE;
4352         }
4353     }
4354 
4355     l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part;
4356 
4357 #ifdef USE_JPWL
4358     if (l_cp->correct) {
4359 
4360         OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
4361         static OPJ_UINT32 backup_tileno = 0;
4362 
4363         /* tileno is negative or larger than the number of tiles!!! */
4364         if (tileno > (l_cp->tw * l_cp->th)) {
4365             opj_event_msg(p_manager, EVT_ERROR,
4366                           "JPWL: bad tile number (%d out of a maximum of %d)\n",
4367                           tileno, (l_cp->tw * l_cp->th));
4368             if (!JPWL_ASSUME) {
4369                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4370                 return OPJ_FALSE;
4371             }
4372             /* we try to correct */
4373             tileno = backup_tileno;
4374             opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
4375                           "- setting tile number to %d\n",
4376                           tileno);
4377         }
4378 
4379         /* keep your private count of tiles */
4380         backup_tileno++;
4381     };
4382 #endif /* USE_JPWL */
4383 
4384     /* look for the tile in the list of already processed tile (in parts). */
4385     /* Optimization possible here with a more complex data structure and with the removing of tiles */
4386     /* since the time taken by this function can only grow at the time */
4387 
4388     /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4389     if ((l_tot_len != 0) && (l_tot_len < 14)) {
4390         if (l_tot_len ==
4391                 12) { /* MSD: Special case for the PHR data which are read by kakadu*/
4392             opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n",
4393                           l_tot_len);
4394         } else {
4395             opj_event_msg(p_manager, EVT_ERROR,
4396                           "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
4397             return OPJ_FALSE;
4398         }
4399     }
4400 
4401 #ifdef USE_JPWL
4402     if (l_cp->correct) {
4403 
4404         /* totlen is negative or larger than the bytes left!!! */
4405         if (/*(l_tot_len < 0) ||*/ (l_tot_len >
4406                                     p_header_size)) {   /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
4407             opj_event_msg(p_manager, EVT_ERROR,
4408                           "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4409                           l_tot_len,
4410                           p_header_size);  /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4411             if (!JPWL_ASSUME) {
4412                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4413                 return OPJ_FALSE;
4414             }
4415             /* we try to correct */
4416             l_tot_len = 0;
4417             opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
4418                           "- setting Psot to %d => assuming it is the last tile\n",
4419                           l_tot_len);
4420         }
4421     };
4422 #endif /* USE_JPWL */
4423 
4424     /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4425     if (!l_tot_len) {
4426         opj_event_msg(p_manager, EVT_INFO,
4427                       "Psot value of the current tile-part is equal to zero, "
4428                       "we assuming it is the last tile-part of the codestream.\n");
4429         p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4430     }
4431 
4432     if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) {
4433         /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */
4434         opj_event_msg(p_manager, EVT_ERROR,
4435                       "In SOT marker, TPSot (%d) is not valid regards to the previous "
4436                       "number of tile-part (%d), giving up\n", l_current_part,
4437                       l_tcp->m_nb_tile_parts);
4438         p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4439         return OPJ_FALSE;
4440     }
4441 
4442     if (l_num_parts !=
4443             0) { /* Number of tile-part header is provided by this tile-part header */
4444         l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction;
4445         /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4446          * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4447         if (l_tcp->m_nb_tile_parts) {
4448             if (l_current_part >= l_tcp->m_nb_tile_parts) {
4449                 opj_event_msg(p_manager, EVT_ERROR,
4450                               "In SOT marker, TPSot (%d) is not valid regards to the current "
4451                               "number of tile-part (%d), giving up\n", l_current_part,
4452                               l_tcp->m_nb_tile_parts);
4453                 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4454                 return OPJ_FALSE;
4455             }
4456         }
4457         if (l_current_part >= l_num_parts) {
4458             /* testcase 451.pdf.SIGSEGV.ce9.3723 */
4459             opj_event_msg(p_manager, EVT_ERROR,
4460                           "In SOT marker, TPSot (%d) is not valid regards to the current "
4461                           "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts);
4462             p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4463             return OPJ_FALSE;
4464         }
4465         l_tcp->m_nb_tile_parts = l_num_parts;
4466     }
4467 
4468     /* If know the number of tile part header we will check if we didn't read the last*/
4469     if (l_tcp->m_nb_tile_parts) {
4470         if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4471             p_j2k->m_specific_param.m_decoder.m_can_decode =
4472                 1; /* Process the last tile-part header*/
4473         }
4474     }
4475 
4476     if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4477         /* Keep the size of data to skip after this marker */
4478         p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len -
4479                 12; /* SOT_marker_size = 12 */
4480     } else {
4481         /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4482         p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4483     }
4484 
4485     p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4486 
4487     /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4488     if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4489         p_j2k->m_specific_param.m_decoder.m_skip_data =
4490             (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4491             || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4492             || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4493             || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4494     } else {
4495         assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0);
4496         p_j2k->m_specific_param.m_decoder.m_skip_data =
4497             (p_j2k->m_current_tile_number != (OPJ_UINT32)
4498              p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4499     }
4500 
4501     /* Index */
4502     if (p_j2k->cstr_index) {
4503         assert(p_j2k->cstr_index->tile_index != 00);
4504         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno =
4505             p_j2k->m_current_tile_number;
4506         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno =
4507             l_current_part;
4508 
4509         if (l_num_parts != 0) {
4510             p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps =
4511                 l_num_parts;
4512             p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
4513                 l_num_parts;
4514 
4515             if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4516                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4517                     (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4518                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4519                     opj_event_msg(p_manager, EVT_ERROR,
4520                                   "Not enough memory to read SOT marker. Tile index allocation failed\n");
4521                     return OPJ_FALSE;
4522                 }
4523             } else {
4524                 opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
4525                                                    p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4526                                                    l_num_parts * sizeof(opj_tp_index_t));
4527                 if (! new_tp_index) {
4528                     opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4529                     p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4530                     opj_event_msg(p_manager, EVT_ERROR,
4531                                   "Not enough memory to read SOT marker. Tile index allocation failed\n");
4532                     return OPJ_FALSE;
4533                 }
4534                 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4535                     new_tp_index;
4536             }
4537         } else {
4538             /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4539 
4540                 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4541                     p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4542                     p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4543                         (opj_tp_index_t*)opj_calloc(
4544                             p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4545                             sizeof(opj_tp_index_t));
4546                     if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4547                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4548                         opj_event_msg(p_manager, EVT_ERROR,
4549                                       "Not enough memory to read SOT marker. Tile index allocation failed\n");
4550                         return OPJ_FALSE;
4551                     }
4552                 }
4553 
4554                 if (l_current_part >=
4555                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) {
4556                     opj_tp_index_t *new_tp_index;
4557                     p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
4558                         l_current_part + 1;
4559                     new_tp_index = (opj_tp_index_t *) opj_realloc(
4560                                        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4561                                        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps *
4562                                        sizeof(opj_tp_index_t));
4563                     if (! new_tp_index) {
4564                         opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4565                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4566                         p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4567                         opj_event_msg(p_manager, EVT_ERROR,
4568                                       "Not enough memory to read SOT marker. Tile index allocation failed\n");
4569                         return OPJ_FALSE;
4570                     }
4571                     p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4572                         new_tp_index;
4573                 }
4574             }
4575 
4576         }
4577 
4578     }
4579 
4580     /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
4581     /* if (p_j2k->cstr_info) {
4582        if (l_tcp->first) {
4583        if (tileno == 0) {
4584        p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4585        }
4586 
4587        p_j2k->cstr_info->tile[tileno].tileno = tileno;
4588        p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4589        p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4590        p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4591 
4592        if (numparts) {
4593        p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4594        }
4595        else {
4596        p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4597        }
4598        }
4599        else {
4600        p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4601        }
4602 
4603        p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4604        p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4605        p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4606        }*/
4607     return OPJ_TRUE;
4608 }
4609 
opj_j2k_write_sod(opj_j2k_t * p_j2k,opj_tcd_t * p_tile_coder,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_total_data_size,const opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4610 static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
4611                                   opj_tcd_t * p_tile_coder,
4612                                   OPJ_BYTE * p_data,
4613                                   OPJ_UINT32 * p_data_written,
4614                                   OPJ_UINT32 p_total_data_size,
4615                                   const opj_stream_private_t *p_stream,
4616                                   opj_event_mgr_t * p_manager
4617                                  )
4618 {
4619     opj_codestream_info_t *l_cstr_info = 00;
4620     OPJ_UINT32 l_remaining_data;
4621 
4622     /* preconditions */
4623     assert(p_j2k != 00);
4624     assert(p_manager != 00);
4625     assert(p_stream != 00);
4626 
4627     OPJ_UNUSED(p_stream);
4628 
4629     if (p_total_data_size < 4) {
4630         opj_event_msg(p_manager, EVT_ERROR,
4631                       "Not enough bytes in output buffer to write SOD marker\n");
4632         return OPJ_FALSE;
4633     }
4634 
4635     opj_write_bytes(p_data, J2K_MS_SOD,
4636                     2);                                 /* SOD */
4637     p_data += 2;
4638 
4639     /* make room for the EOF marker */
4640     l_remaining_data =  p_total_data_size - 4;
4641 
4642     /* update tile coder */
4643     p_tile_coder->tp_num =
4644         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4645     p_tile_coder->cur_tp_num =
4646         p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4647 
4648     /* INDEX >> */
4649     /* TODO mergeV2: check this part which use cstr_info */
4650     /*l_cstr_info = p_j2k->cstr_info;
4651     if (l_cstr_info) {
4652             if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4653                     //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4654                     l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4655             }
4656             else {*/
4657     /*
4658     TODO
4659     if
4660             (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4661     {
4662             cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4663     }*/
4664     /*}*/
4665     /* UniPG>> */
4666 #ifdef USE_JPWL
4667     /* update markers struct */
4668     /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4669     */
4670     assert(0 && "TODO");
4671 #endif /* USE_JPWL */
4672     /* <<UniPG */
4673     /*}*/
4674     /* << INDEX */
4675 
4676     if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4677         p_tile_coder->tcd_image->tiles->packno = 0;
4678 #ifdef deadcode
4679         if (l_cstr_info) {
4680             l_cstr_info->packno = 0;
4681         }
4682 #endif
4683     }
4684 
4685     *p_data_written = 0;
4686 
4687     if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data,
4688                               p_data_written, l_remaining_data, l_cstr_info,
4689                               p_manager)) {
4690         opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
4691         return OPJ_FALSE;
4692     }
4693 
4694     *p_data_written += 2;
4695 
4696     return OPJ_TRUE;
4697 }
4698 
opj_j2k_read_sod(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4699 static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
4700                                  opj_stream_private_t *p_stream,
4701                                  opj_event_mgr_t * p_manager
4702                                 )
4703 {
4704     OPJ_SIZE_T l_current_read_size;
4705     opj_codestream_index_t * l_cstr_index = 00;
4706     OPJ_BYTE ** l_current_data = 00;
4707     opj_tcp_t * l_tcp = 00;
4708     OPJ_UINT32 * l_tile_len = 00;
4709     OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
4710 
4711     /* preconditions */
4712     assert(p_j2k != 00);
4713     assert(p_manager != 00);
4714     assert(p_stream != 00);
4715 
4716     l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4717 
4718     if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4719         /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4720         // but we are in the last tile part,
4721         // so its result will fit on OPJ_UINT32 unless we find
4722         // a file with a single tile part of more than 4 GB...*/
4723         p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(
4724                     opj_stream_get_number_byte_left(p_stream) - 2);
4725     } else {
4726         /* Check to avoid pass the limit of OPJ_UINT32 */
4727         if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) {
4728             p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4729         } else {
4730             /* MSD: case commented to support empty SOT marker (PHR data) */
4731         }
4732     }
4733 
4734     l_current_data = &(l_tcp->m_data);
4735     l_tile_len = &l_tcp->m_data_size;
4736 
4737     /* Patch to support new PHR data */
4738     if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
4739         /* If we are here, we'll try to read the data after allocation */
4740         /* Check enough bytes left in stream before allocation */
4741         if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length >
4742                 opj_stream_get_number_byte_left(p_stream)) {
4743             opj_event_msg(p_manager, EVT_ERROR,
4744                           "Tile part length size inconsistent with stream length\n");
4745             return OPJ_FALSE;
4746         }
4747         if (p_j2k->m_specific_param.m_decoder.m_sot_length >
4748                 UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) {
4749             opj_event_msg(p_manager, EVT_ERROR,
4750                           "p_j2k->m_specific_param.m_decoder.m_sot_length > "
4751                           "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA");
4752             return OPJ_FALSE;
4753         }
4754         /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */
4755         /* do so that opj_mqc_init_dec_common() can safely add a synthetic */
4756         /* 0xFFFF marker. */
4757         if (! *l_current_data) {
4758             /* LH: oddly enough, in this path, l_tile_len!=0.
4759              * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
4760              */
4761             *l_current_data = (OPJ_BYTE*) opj_malloc(
4762                                   p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA);
4763         } else {
4764             OPJ_BYTE *l_new_current_data;
4765             if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA -
4766                     p_j2k->m_specific_param.m_decoder.m_sot_length) {
4767                 opj_event_msg(p_manager, EVT_ERROR,
4768                               "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - "
4769                               "p_j2k->m_specific_param.m_decoder.m_sot_length");
4770                 return OPJ_FALSE;
4771             }
4772 
4773             l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data,
4774                                  *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length +
4775                                  OPJ_COMMON_CBLK_DATA_EXTRA);
4776             if (! l_new_current_data) {
4777                 opj_free(*l_current_data);
4778                 /*nothing more is done as l_current_data will be set to null, and just
4779                   afterward we enter in the error path
4780                   and the actual tile_len is updated (committed) at the end of the
4781                   function. */
4782             }
4783             *l_current_data = l_new_current_data;
4784         }
4785 
4786         if (*l_current_data == 00) {
4787             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
4788             return OPJ_FALSE;
4789         }
4790     } else {
4791         l_sot_length_pb_detected = OPJ_TRUE;
4792     }
4793 
4794     /* Index */
4795     l_cstr_index = p_j2k->cstr_index;
4796     if (l_cstr_index) {
4797         OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4798 
4799         OPJ_UINT32 l_current_tile_part =
4800             l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4801         l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header
4802             =
4803                 l_current_pos;
4804         l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos
4805             =
4806                 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4807 
4808         if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
4809                                               l_cstr_index,
4810                                               J2K_MS_SOD,
4811                                               l_current_pos,
4812                                               p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
4813             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
4814             return OPJ_FALSE;
4815         }
4816 
4817         /*l_cstr_index->packno = 0;*/
4818     }
4819 
4820     /* Patch to support new PHR data */
4821     if (!l_sot_length_pb_detected) {
4822         l_current_read_size = opj_stream_read_data(
4823                                   p_stream,
4824                                   *l_current_data + *l_tile_len,
4825                                   p_j2k->m_specific_param.m_decoder.m_sot_length,
4826                                   p_manager);
4827     } else {
4828         l_current_read_size = 0;
4829     }
4830 
4831     if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4832         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4833     } else {
4834         p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4835     }
4836 
4837     *l_tile_len += (OPJ_UINT32)l_current_read_size;
4838 
4839     return OPJ_TRUE;
4840 }
4841 
opj_j2k_write_rgn(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no,OPJ_UINT32 nb_comps,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4842 static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
4843                                   OPJ_UINT32 p_tile_no,
4844                                   OPJ_UINT32 p_comp_no,
4845                                   OPJ_UINT32 nb_comps,
4846                                   opj_stream_private_t *p_stream,
4847                                   opj_event_mgr_t * p_manager
4848                                  )
4849 {
4850     OPJ_BYTE * l_current_data = 00;
4851     OPJ_UINT32 l_rgn_size;
4852     opj_cp_t *l_cp = 00;
4853     opj_tcp_t *l_tcp = 00;
4854     opj_tccp_t *l_tccp = 00;
4855     OPJ_UINT32 l_comp_room;
4856 
4857     /* preconditions */
4858     assert(p_j2k != 00);
4859     assert(p_manager != 00);
4860     assert(p_stream != 00);
4861 
4862     l_cp = &(p_j2k->m_cp);
4863     l_tcp = &l_cp->tcps[p_tile_no];
4864     l_tccp = &l_tcp->tccps[p_comp_no];
4865 
4866     if (nb_comps <= 256) {
4867         l_comp_room = 1;
4868     } else {
4869         l_comp_room = 2;
4870     }
4871 
4872     l_rgn_size = 6 + l_comp_room;
4873 
4874     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4875 
4876     opj_write_bytes(l_current_data, J2K_MS_RGN,
4877                     2);                                   /* RGN  */
4878     l_current_data += 2;
4879 
4880     opj_write_bytes(l_current_data, l_rgn_size - 2,
4881                     2);                                 /* Lrgn */
4882     l_current_data += 2;
4883 
4884     opj_write_bytes(l_current_data, p_comp_no,
4885                     l_comp_room);                          /* Crgn */
4886     l_current_data += l_comp_room;
4887 
4888     opj_write_bytes(l_current_data, 0,
4889                     1);                                           /* Srgn */
4890     ++l_current_data;
4891 
4892     opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,
4893                     1);                            /* SPrgn */
4894     ++l_current_data;
4895 
4896     if (opj_stream_write_data(p_stream,
4897                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size,
4898                               p_manager) != l_rgn_size) {
4899         return OPJ_FALSE;
4900     }
4901 
4902     return OPJ_TRUE;
4903 }
4904 
opj_j2k_write_eoc(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)4905 static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k,
4906                                   opj_stream_private_t *p_stream,
4907                                   opj_event_mgr_t * p_manager
4908                                  )
4909 {
4910     /* preconditions */
4911     assert(p_j2k != 00);
4912     assert(p_manager != 00);
4913     assert(p_stream != 00);
4914 
4915     opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4916                     J2K_MS_EOC, 2);                                    /* EOC */
4917 
4918     /* UniPG>> */
4919 #ifdef USE_JPWL
4920     /* update markers struct */
4921     /*
4922     OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4923     */
4924 #endif /* USE_JPWL */
4925 
4926     if (opj_stream_write_data(p_stream,
4927                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) {
4928         return OPJ_FALSE;
4929     }
4930 
4931     if (! opj_stream_flush(p_stream, p_manager)) {
4932         return OPJ_FALSE;
4933     }
4934 
4935     return OPJ_TRUE;
4936 }
4937 
4938 /**
4939  * Reads a RGN marker (Region Of Interest)
4940  *
4941  * @param       p_header_data   the data contained in the POC box.
4942  * @param       p_j2k                   the jpeg2000 codec.
4943  * @param       p_header_size   the size of the data contained in the POC marker.
4944  * @param       p_manager               the user event manager.
4945 */
opj_j2k_read_rgn(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)4946 static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k,
4947                                  OPJ_BYTE * p_header_data,
4948                                  OPJ_UINT32 p_header_size,
4949                                  opj_event_mgr_t * p_manager
4950                                 )
4951 {
4952     OPJ_UINT32 l_nb_comp;
4953     opj_image_t * l_image = 00;
4954 
4955     opj_cp_t *l_cp = 00;
4956     opj_tcp_t *l_tcp = 00;
4957     OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4958 
4959     /* preconditions*/
4960     assert(p_header_data != 00);
4961     assert(p_j2k != 00);
4962     assert(p_manager != 00);
4963 
4964     l_image = p_j2k->m_private_image;
4965     l_nb_comp = l_image->numcomps;
4966 
4967     if (l_nb_comp <= 256) {
4968         l_comp_room = 1;
4969     } else {
4970         l_comp_room = 2;
4971     }
4972 
4973     if (p_header_size != 2 + l_comp_room) {
4974         opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4975         return OPJ_FALSE;
4976     }
4977 
4978     l_cp = &(p_j2k->m_cp);
4979     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4980             &l_cp->tcps[p_j2k->m_current_tile_number] :
4981             p_j2k->m_specific_param.m_decoder.m_default_tcp;
4982 
4983     opj_read_bytes(p_header_data, &l_comp_no, l_comp_room);         /* Crgn */
4984     p_header_data += l_comp_room;
4985     opj_read_bytes(p_header_data, &l_roi_sty,
4986                    1);                                     /* Srgn */
4987     ++p_header_data;
4988 
4989 #ifdef USE_JPWL
4990     if (l_cp->correct) {
4991         /* totlen is negative or larger than the bytes left!!! */
4992         if (l_comp_room >= l_nb_comp) {
4993             opj_event_msg(p_manager, EVT_ERROR,
4994                           "JPWL: bad component number in RGN (%d when there are only %d)\n",
4995                           l_comp_room, l_nb_comp);
4996             if (!JPWL_ASSUME) {
4997                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4998                 return OPJ_FALSE;
4999             }
5000         }
5001     };
5002 #endif /* USE_JPWL */
5003 
5004     /* testcase 3635.pdf.asan.77.2930 */
5005     if (l_comp_no >= l_nb_comp) {
5006         opj_event_msg(p_manager, EVT_ERROR,
5007                       "bad component number in RGN (%d when there are only %d)\n",
5008                       l_comp_no, l_nb_comp);
5009         return OPJ_FALSE;
5010     }
5011 
5012     opj_read_bytes(p_header_data,
5013                    (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1);  /* SPrgn */
5014     ++p_header_data;
5015 
5016     return OPJ_TRUE;
5017 
5018 }
5019 
opj_j2k_get_tp_stride(opj_tcp_t * p_tcp)5020 static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp)
5021 {
5022     return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14);
5023 }
5024 
opj_j2k_get_default_stride(opj_tcp_t * p_tcp)5025 static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp)
5026 {
5027     (void)p_tcp;
5028     return 0;
5029 }
5030 
opj_j2k_update_rates(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)5031 static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k,
5032                                      opj_stream_private_t *p_stream,
5033                                      opj_event_mgr_t * p_manager)
5034 {
5035     opj_cp_t * l_cp = 00;
5036     opj_image_t * l_image = 00;
5037     opj_tcp_t * l_tcp = 00;
5038     opj_image_comp_t * l_img_comp = 00;
5039 
5040     OPJ_UINT32 i, j, k;
5041     OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
5042     OPJ_FLOAT32 * l_rates = 0;
5043     OPJ_FLOAT32 l_sot_remove;
5044     OPJ_UINT32 l_bits_empty, l_size_pixel;
5045     OPJ_UINT32 l_tile_size = 0;
5046     OPJ_UINT32 l_last_res;
5047     OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00;
5048 
5049     /* preconditions */
5050     assert(p_j2k != 00);
5051     assert(p_manager != 00);
5052     assert(p_stream != 00);
5053 
5054     OPJ_UNUSED(p_manager);
5055 
5056     l_cp = &(p_j2k->m_cp);
5057     l_image = p_j2k->m_private_image;
5058     l_tcp = l_cp->tcps;
5059 
5060     l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5061     l_size_pixel = l_image->numcomps * l_image->comps->prec;
5062     l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(
5063                        l_cp->th * l_cp->tw);
5064 
5065     if (l_cp->m_specific_param.m_enc.m_tp_on) {
5066         l_tp_stride_func = opj_j2k_get_tp_stride;
5067     } else {
5068         l_tp_stride_func = opj_j2k_get_default_stride;
5069     }
5070 
5071     for (i = 0; i < l_cp->th; ++i) {
5072         for (j = 0; j < l_cp->tw; ++j) {
5073             OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) /
5074                                    (OPJ_FLOAT32)l_tcp->numlayers;
5075 
5076             /* 4 borders of the tile rescale on the image if necessary */
5077             l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx),
5078                                (OPJ_INT32)l_image->x0);
5079             l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy),
5080                                (OPJ_INT32)l_image->y0);
5081             l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx),
5082                                (OPJ_INT32)l_image->x1);
5083             l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy),
5084                                (OPJ_INT32)l_image->y1);
5085 
5086             l_rates = l_tcp->rates;
5087 
5088             /* Modification of the RATE >> */
5089             if (*l_rates > 0.0f) {
5090                 *l_rates = (((OPJ_FLOAT32)(l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) *
5091                                            (OPJ_UINT32)(l_y1 - l_y0)))
5092                             /
5093                             ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
5094                            )
5095                            -
5096                            l_offset;
5097             }
5098 
5099             ++l_rates;
5100 
5101             for (k = 1; k < l_tcp->numlayers; ++k) {
5102                 if (*l_rates > 0.0f) {
5103                     *l_rates = (((OPJ_FLOAT32)(l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) *
5104                                                (OPJ_UINT32)(l_y1 - l_y0)))
5105                                 /
5106                                 ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
5107                                )
5108                                -
5109                                l_offset;
5110                 }
5111 
5112                 ++l_rates;
5113             }
5114 
5115             ++l_tcp;
5116 
5117         }
5118     }
5119 
5120     l_tcp = l_cp->tcps;
5121 
5122     for (i = 0; i < l_cp->th; ++i) {
5123         for (j = 0; j < l_cp->tw; ++j) {
5124             l_rates = l_tcp->rates;
5125 
5126             if (*l_rates > 0.0f) {
5127                 *l_rates -= l_sot_remove;
5128 
5129                 if (*l_rates < 30.0f) {
5130                     *l_rates = 30.0f;
5131                 }
5132             }
5133 
5134             ++l_rates;
5135 
5136             l_last_res = l_tcp->numlayers - 1;
5137 
5138             for (k = 1; k < l_last_res; ++k) {
5139 
5140                 if (*l_rates > 0.0f) {
5141                     *l_rates -= l_sot_remove;
5142 
5143                     if (*l_rates < * (l_rates - 1) + 10.0f) {
5144                         *l_rates  = (*(l_rates - 1)) + 20.0f;
5145                     }
5146                 }
5147 
5148                 ++l_rates;
5149             }
5150 
5151             if (*l_rates > 0.0f) {
5152                 *l_rates -= (l_sot_remove + 2.f);
5153 
5154                 if (*l_rates < * (l_rates - 1) + 10.0f) {
5155                     *l_rates  = (*(l_rates - 1)) + 20.0f;
5156                 }
5157             }
5158 
5159             ++l_tcp;
5160         }
5161     }
5162 
5163     l_img_comp = l_image->comps;
5164     l_tile_size = 0;
5165 
5166     for (i = 0; i < l_image->numcomps; ++i) {
5167         l_tile_size += (opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx)
5168                         *
5169                         opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy)
5170                         *
5171                         l_img_comp->prec
5172                        );
5173 
5174         ++l_img_comp;
5175     }
5176 
5177     /* TODO: where does this magic value come from ? */
5178     /* This used to be 1.3 / 8, but with random data and very small code */
5179     /* block sizes, this is not enough. For example with */
5180     /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */
5181     /* TODO revise this to take into account the overhead linked to the */
5182     /* number of packets and number of code blocks in packets */
5183     l_tile_size = (OPJ_UINT32)(l_tile_size * 1.4 / 8);
5184 
5185     /* Arbitrary amount to make the following work: */
5186     /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */
5187     l_tile_size += 500;
5188 
5189     l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
5190 
5191     p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5192     p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5193         (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5194     if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5195         return OPJ_FALSE;
5196     }
5197 
5198     if (OPJ_IS_CINEMA(l_cp->rsiz)) {
5199         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5200             (OPJ_BYTE *) opj_malloc(5 *
5201                                     p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5202         if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5203             return OPJ_FALSE;
5204         }
5205 
5206         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5207             p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5208     }
5209 
5210     return OPJ_TRUE;
5211 }
5212 
5213 #if 0
5214 static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k,
5215                                  opj_stream_private_t *p_stream,
5216                                  opj_event_mgr_t * p_manager)
5217 {
5218     OPJ_UINT32 i;
5219     opj_tcd_t * l_tcd = 00;
5220     OPJ_UINT32 l_nb_tiles;
5221     opj_tcp_t * l_tcp = 00;
5222     OPJ_BOOL l_success;
5223 
5224     /* preconditions */
5225     assert(p_j2k != 00);
5226     assert(p_manager != 00);
5227     assert(p_stream != 00);
5228 
5229     l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5230     l_tcp = p_j2k->m_cp.tcps;
5231 
5232     l_tcd = opj_tcd_create(OPJ_TRUE);
5233     if (l_tcd == 00) {
5234         opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5235         return OPJ_FALSE;
5236     }
5237 
5238     for (i = 0; i < l_nb_tiles; ++i) {
5239         if (l_tcp->m_data) {
5240             if (! opj_tcd_init_decode_tile(l_tcd, i)) {
5241                 opj_tcd_destroy(l_tcd);
5242                 opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5243                 return OPJ_FALSE;
5244             }
5245 
5246             l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i,
5247                                             p_j2k->cstr_index);
5248             /* cleanup */
5249 
5250             if (! l_success) {
5251                 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5252                 break;
5253             }
5254         }
5255 
5256         opj_j2k_tcp_destroy(l_tcp);
5257         ++l_tcp;
5258     }
5259 
5260     opj_tcd_destroy(l_tcd);
5261     return OPJ_TRUE;
5262 }
5263 #endif
5264 
opj_j2k_get_end_header(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5265 static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
5266                                        struct opj_stream_private *p_stream,
5267                                        struct opj_event_mgr * p_manager)
5268 {
5269     /* preconditions */
5270     assert(p_j2k != 00);
5271     assert(p_manager != 00);
5272     assert(p_stream != 00);
5273 
5274     OPJ_UNUSED(p_manager);
5275 
5276     p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5277 
5278     return OPJ_TRUE;
5279 }
5280 
opj_j2k_write_mct_data_group(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5281 static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k,
5282         struct opj_stream_private *p_stream,
5283         struct opj_event_mgr * p_manager)
5284 {
5285     OPJ_UINT32 i;
5286     opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5287     opj_mct_data_t * l_mct_record;
5288     opj_tcp_t * l_tcp;
5289 
5290     /* preconditions */
5291     assert(p_j2k != 00);
5292     assert(p_stream != 00);
5293     assert(p_manager != 00);
5294 
5295     if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) {
5296         return OPJ_FALSE;
5297     }
5298 
5299     l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5300     l_mct_record = l_tcp->m_mct_records;
5301 
5302     for (i = 0; i < l_tcp->m_nb_mct_records; ++i) {
5303 
5304         if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) {
5305             return OPJ_FALSE;
5306         }
5307 
5308         ++l_mct_record;
5309     }
5310 
5311     l_mcc_record = l_tcp->m_mcc_records;
5312 
5313     for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
5314 
5315         if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) {
5316             return OPJ_FALSE;
5317         }
5318 
5319         ++l_mcc_record;
5320     }
5321 
5322     if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) {
5323         return OPJ_FALSE;
5324     }
5325 
5326     return OPJ_TRUE;
5327 }
5328 
opj_j2k_write_all_coc(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5329 static OPJ_BOOL opj_j2k_write_all_coc(
5330     opj_j2k_t *p_j2k,
5331     struct opj_stream_private *p_stream,
5332     struct opj_event_mgr * p_manager)
5333 {
5334     OPJ_UINT32 compno;
5335 
5336     /* preconditions */
5337     assert(p_j2k != 00);
5338     assert(p_manager != 00);
5339     assert(p_stream != 00);
5340 
5341     for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) {
5342         /* cod is first component of first tile */
5343         if (! opj_j2k_compare_coc(p_j2k, 0, compno)) {
5344             if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) {
5345                 return OPJ_FALSE;
5346             }
5347         }
5348     }
5349 
5350     return OPJ_TRUE;
5351 }
5352 
opj_j2k_write_all_qcc(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5353 static OPJ_BOOL opj_j2k_write_all_qcc(
5354     opj_j2k_t *p_j2k,
5355     struct opj_stream_private *p_stream,
5356     struct opj_event_mgr * p_manager)
5357 {
5358     OPJ_UINT32 compno;
5359 
5360     /* preconditions */
5361     assert(p_j2k != 00);
5362     assert(p_manager != 00);
5363     assert(p_stream != 00);
5364 
5365     for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) {
5366         /* qcd is first component of first tile */
5367         if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) {
5368             if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) {
5369                 return OPJ_FALSE;
5370             }
5371         }
5372     }
5373     return OPJ_TRUE;
5374 }
5375 
opj_j2k_write_regions(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5376 static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k,
5377                                       struct opj_stream_private *p_stream,
5378                                       struct opj_event_mgr * p_manager)
5379 {
5380     OPJ_UINT32 compno;
5381     const opj_tccp_t *l_tccp = 00;
5382 
5383     /* preconditions */
5384     assert(p_j2k != 00);
5385     assert(p_manager != 00);
5386     assert(p_stream != 00);
5387 
5388     l_tccp = p_j2k->m_cp.tcps->tccps;
5389 
5390     for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno)  {
5391         if (l_tccp->roishift) {
5392 
5393             if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps,
5394                                     p_stream, p_manager)) {
5395                 return OPJ_FALSE;
5396             }
5397         }
5398 
5399         ++l_tccp;
5400     }
5401 
5402     return OPJ_TRUE;
5403 }
5404 
opj_j2k_write_epc(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5405 static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k,
5406                                   struct opj_stream_private *p_stream,
5407                                   struct opj_event_mgr * p_manager)
5408 {
5409     opj_codestream_index_t * l_cstr_index = 00;
5410 
5411     /* preconditions */
5412     assert(p_j2k != 00);
5413     assert(p_manager != 00);
5414     assert(p_stream != 00);
5415 
5416     OPJ_UNUSED(p_manager);
5417 
5418     l_cstr_index = p_j2k->cstr_index;
5419     if (l_cstr_index) {
5420         l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
5421         /* UniPG>> */
5422         /* The following adjustment is done to adjust the codestream size */
5423         /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5424         /* the first bunch of bytes is not in the codestream              */
5425         l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
5426         /* <<UniPG */
5427     }
5428 
5429 #ifdef USE_JPWL
5430     /* preparation of JPWL marker segments */
5431 #if 0
5432     if (cp->epc_on) {
5433 
5434         /* encode according to JPWL */
5435         jpwl_encode(p_j2k, p_stream, image);
5436 
5437     }
5438 #endif
5439     assert(0 && "TODO");
5440 #endif /* USE_JPWL */
5441 
5442     return OPJ_TRUE;
5443 }
5444 
opj_j2k_read_unk(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,OPJ_UINT32 * output_marker,opj_event_mgr_t * p_manager)5445 static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
5446                                  opj_stream_private_t *p_stream,
5447                                  OPJ_UINT32 *output_marker,
5448                                  opj_event_mgr_t * p_manager
5449                                 )
5450 {
5451     OPJ_UINT32 l_unknown_marker;
5452     const opj_dec_memory_marker_handler_t * l_marker_handler;
5453     OPJ_UINT32 l_size_unk = 2;
5454 
5455     /* preconditions*/
5456     assert(p_j2k != 00);
5457     assert(p_manager != 00);
5458     assert(p_stream != 00);
5459 
5460     opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");
5461 
5462     for (;;) {
5463         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5464         if (opj_stream_read_data(p_stream,
5465                                  p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
5466             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
5467             return OPJ_FALSE;
5468         }
5469 
5470         /* read 2 bytes as the new marker ID*/
5471         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
5472                        &l_unknown_marker, 2);
5473 
5474         if (!(l_unknown_marker < 0xff00)) {
5475 
5476             /* Get the marker handler from the marker ID*/
5477             l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5478 
5479             if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5480                 opj_event_msg(p_manager, EVT_ERROR,
5481                               "Marker is not compliant with its position\n");
5482                 return OPJ_FALSE;
5483             } else {
5484                 if (l_marker_handler->id != J2K_MS_UNK) {
5485                     /* Add the marker to the codestream index*/
5486                     if (l_marker_handler->id != J2K_MS_SOT) {
5487                         OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
5488                                                             (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5489                                                             l_size_unk);
5490                         if (res == OPJ_FALSE) {
5491                             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
5492                             return OPJ_FALSE;
5493                         }
5494                     }
5495                     break; /* next marker is known and well located */
5496                 } else {
5497                     l_size_unk += 2;
5498                 }
5499             }
5500         }
5501     }
5502 
5503     *output_marker = l_marker_handler->id ;
5504 
5505     return OPJ_TRUE;
5506 }
5507 
opj_j2k_write_mct_record(opj_j2k_t * p_j2k,opj_mct_data_t * p_mct_record,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5508 static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k,
5509         opj_mct_data_t * p_mct_record,
5510         struct opj_stream_private *p_stream,
5511         struct opj_event_mgr * p_manager)
5512 {
5513     OPJ_UINT32 l_mct_size;
5514     OPJ_BYTE * l_current_data = 00;
5515     OPJ_UINT32 l_tmp;
5516 
5517     /* preconditions */
5518     assert(p_j2k != 00);
5519     assert(p_manager != 00);
5520     assert(p_stream != 00);
5521 
5522     l_mct_size = 10 + p_mct_record->m_data_size;
5523 
5524     if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5525         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
5526                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
5527         if (! new_header_tile_data) {
5528             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5529             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5530             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5531             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
5532             return OPJ_FALSE;
5533         }
5534         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5535         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5536     }
5537 
5538     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5539 
5540     opj_write_bytes(l_current_data, J2K_MS_MCT,
5541                     2);                                   /* MCT */
5542     l_current_data += 2;
5543 
5544     opj_write_bytes(l_current_data, l_mct_size - 2,
5545                     2);                                 /* Lmct */
5546     l_current_data += 2;
5547 
5548     opj_write_bytes(l_current_data, 0,
5549                     2);                                                    /* Zmct */
5550     l_current_data += 2;
5551 
5552     /* only one marker atm */
5553     l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) |
5554             (p_mct_record->m_element_type << 10);
5555 
5556     opj_write_bytes(l_current_data, l_tmp, 2);
5557     l_current_data += 2;
5558 
5559     opj_write_bytes(l_current_data, 0,
5560                     2);                                                    /* Ymct */
5561     l_current_data += 2;
5562 
5563     memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size);
5564 
5565     if (opj_stream_write_data(p_stream,
5566                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size,
5567                               p_manager) != l_mct_size) {
5568         return OPJ_FALSE;
5569     }
5570 
5571     return OPJ_TRUE;
5572 }
5573 
5574 /**
5575  * Reads a MCT marker (Multiple Component Transform)
5576  *
5577  * @param       p_header_data   the data contained in the MCT box.
5578  * @param       p_j2k                   the jpeg2000 codec.
5579  * @param       p_header_size   the size of the data contained in the MCT marker.
5580  * @param       p_manager               the user event manager.
5581 */
opj_j2k_read_mct(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)5582 static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k,
5583                                  OPJ_BYTE * p_header_data,
5584                                  OPJ_UINT32 p_header_size,
5585                                  opj_event_mgr_t * p_manager
5586                                 )
5587 {
5588     OPJ_UINT32 i;
5589     opj_tcp_t *l_tcp = 00;
5590     OPJ_UINT32 l_tmp;
5591     OPJ_UINT32 l_indix;
5592     opj_mct_data_t * l_mct_data;
5593     OPJ_BOOL new_mct = OPJ_FALSE;
5594 
5595     /* preconditions */
5596     assert(p_header_data != 00);
5597     assert(p_j2k != 00);
5598 
5599     l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5600             &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5601             p_j2k->m_specific_param.m_decoder.m_default_tcp;
5602 
5603     if (p_header_size < 2) {
5604         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5605         return OPJ_FALSE;
5606     }
5607 
5608     /* first marker */
5609     opj_read_bytes(p_header_data, &l_tmp, 2);                       /* Zmct */
5610     p_header_data += 2;
5611     if (l_tmp != 0) {
5612         opj_event_msg(p_manager, EVT_WARNING,
5613                       "Cannot take in charge mct data within multiple MCT records\n");
5614         return OPJ_TRUE;
5615     }
5616 
5617     if (p_header_size <= 6) {
5618         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5619         return OPJ_FALSE;
5620     }
5621 
5622     /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5623     opj_read_bytes(p_header_data, &l_tmp, 2);                       /* Imct */
5624     p_header_data += 2;
5625 
5626     l_indix = l_tmp & 0xff;
5627     l_mct_data = l_tcp->m_mct_records;
5628 
5629     for (i = 0; i < l_tcp->m_nb_mct_records; ++i) {
5630         if (l_mct_data->m_index == l_indix) {
5631             break;
5632         }
5633         ++l_mct_data;
5634     }
5635 
5636     /* NOT FOUND */
5637     if (i == l_tcp->m_nb_mct_records) {
5638         if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5639             opj_mct_data_t *new_mct_records;
5640             l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
5641 
5642             new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records,
5643                               l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
5644             if (! new_mct_records) {
5645                 opj_free(l_tcp->m_mct_records);
5646                 l_tcp->m_mct_records = NULL;
5647                 l_tcp->m_nb_max_mct_records = 0;
5648                 l_tcp->m_nb_mct_records = 0;
5649                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
5650                 return OPJ_FALSE;
5651             }
5652 
5653             /* Update m_mcc_records[].m_offset_array and m_decorrelation_array
5654              * to point to the new addresses */
5655             if (new_mct_records != l_tcp->m_mct_records) {
5656                 for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
5657                     opj_simple_mcc_decorrelation_data_t* l_mcc_record =
5658                         &(l_tcp->m_mcc_records[i]);
5659                     if (l_mcc_record->m_decorrelation_array) {
5660                         l_mcc_record->m_decorrelation_array =
5661                             new_mct_records +
5662                             (l_mcc_record->m_decorrelation_array -
5663                              l_tcp->m_mct_records);
5664                     }
5665                     if (l_mcc_record->m_offset_array) {
5666                         l_mcc_record->m_offset_array =
5667                             new_mct_records +
5668                             (l_mcc_record->m_offset_array -
5669                              l_tcp->m_mct_records);
5670                     }
5671                 }
5672             }
5673 
5674             l_tcp->m_mct_records = new_mct_records;
5675             l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5676             memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) *
5677                    sizeof(opj_mct_data_t));
5678         }
5679 
5680         l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5681         new_mct = OPJ_TRUE;
5682     }
5683 
5684     if (l_mct_data->m_data) {
5685         opj_free(l_mct_data->m_data);
5686         l_mct_data->m_data = 00;
5687         l_mct_data->m_data_size = 0;
5688     }
5689 
5690     l_mct_data->m_index = l_indix;
5691     l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp  >> 8) & 3);
5692     l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp  >> 10) & 3);
5693 
5694     opj_read_bytes(p_header_data, &l_tmp, 2);                       /* Ymct */
5695     p_header_data += 2;
5696     if (l_tmp != 0) {
5697         opj_event_msg(p_manager, EVT_WARNING,
5698                       "Cannot take in charge multiple MCT markers\n");
5699         return OPJ_TRUE;
5700     }
5701 
5702     p_header_size -= 6;
5703 
5704     l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5705     if (! l_mct_data->m_data) {
5706         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5707         return OPJ_FALSE;
5708     }
5709     memcpy(l_mct_data->m_data, p_header_data, p_header_size);
5710 
5711     l_mct_data->m_data_size = p_header_size;
5712 
5713     if (new_mct) {
5714             ++l_tcp->m_nb_mct_records;
5715     }
5716     return OPJ_TRUE;
5717 }
5718 
opj_j2k_write_mcc_record(opj_j2k_t * p_j2k,struct opj_simple_mcc_decorrelation_data * p_mcc_record,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)5719 static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k,
5720         struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5721         struct opj_stream_private *p_stream,
5722         struct opj_event_mgr * p_manager)
5723 {
5724     OPJ_UINT32 i;
5725     OPJ_UINT32 l_mcc_size;
5726     OPJ_BYTE * l_current_data = 00;
5727     OPJ_UINT32 l_nb_bytes_for_comp;
5728     OPJ_UINT32 l_mask;
5729     OPJ_UINT32 l_tmcc;
5730 
5731     /* preconditions */
5732     assert(p_j2k != 00);
5733     assert(p_manager != 00);
5734     assert(p_stream != 00);
5735 
5736     if (p_mcc_record->m_nb_comps > 255) {
5737         l_nb_bytes_for_comp = 2;
5738         l_mask = 0x8000;
5739     } else {
5740         l_nb_bytes_for_comp = 1;
5741         l_mask = 0;
5742     }
5743 
5744     l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5745     if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5746         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
5747                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
5748         if (! new_header_tile_data) {
5749             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5750             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
5751             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5752             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
5753             return OPJ_FALSE;
5754         }
5755         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
5756         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5757     }
5758 
5759     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5760 
5761     opj_write_bytes(l_current_data, J2K_MS_MCC,
5762                     2);                                   /* MCC */
5763     l_current_data += 2;
5764 
5765     opj_write_bytes(l_current_data, l_mcc_size - 2,
5766                     2);                                 /* Lmcc */
5767     l_current_data += 2;
5768 
5769     /* first marker */
5770     opj_write_bytes(l_current_data, 0,
5771                     2);                                  /* Zmcc */
5772     l_current_data += 2;
5773 
5774     opj_write_bytes(l_current_data, p_mcc_record->m_index,
5775                     1);                                        /* Imcc -> no need for other values, take the first */
5776     ++l_current_data;
5777 
5778     /* only one marker atm */
5779     opj_write_bytes(l_current_data, 0,
5780                     2);                                  /* Ymcc */
5781     l_current_data += 2;
5782 
5783     opj_write_bytes(l_current_data, 1,
5784                     2);                                  /* Qmcc -> number of collections -> 1 */
5785     l_current_data += 2;
5786 
5787     opj_write_bytes(l_current_data, 0x1,
5788                     1);                                /* Xmcci type of component transformation -> array based decorrelation */
5789     ++l_current_data;
5790 
5791     opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask,
5792                     2);  /* Nmcci number of input components involved and size for each component offset = 8 bits */
5793     l_current_data += 2;
5794 
5795     for (i = 0; i < p_mcc_record->m_nb_comps; ++i) {
5796         opj_write_bytes(l_current_data, i,
5797                         l_nb_bytes_for_comp);                          /* Cmccij Component offset*/
5798         l_current_data += l_nb_bytes_for_comp;
5799     }
5800 
5801     opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask,
5802                     2);  /* Mmcci number of output components involved and size for each component offset = 8 bits */
5803     l_current_data += 2;
5804 
5805     for (i = 0; i < p_mcc_record->m_nb_comps; ++i) {
5806         opj_write_bytes(l_current_data, i,
5807                         l_nb_bytes_for_comp);                          /* Wmccij Component offset*/
5808         l_current_data += l_nb_bytes_for_comp;
5809     }
5810 
5811     l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16;
5812 
5813     if (p_mcc_record->m_decorrelation_array) {
5814         l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5815     }
5816 
5817     if (p_mcc_record->m_offset_array) {
5818         l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8);
5819     }
5820 
5821     opj_write_bytes(l_current_data, l_tmcc,
5822                     3);     /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5823     l_current_data += 3;
5824 
5825     if (opj_stream_write_data(p_stream,
5826                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size,
5827                               p_manager) != l_mcc_size) {
5828         return OPJ_FALSE;
5829     }
5830 
5831     return OPJ_TRUE;
5832 }
5833 
opj_j2k_read_mcc(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)5834 static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k,
5835                                  OPJ_BYTE * p_header_data,
5836                                  OPJ_UINT32 p_header_size,
5837                                  opj_event_mgr_t * p_manager)
5838 {
5839     OPJ_UINT32 i, j;
5840     OPJ_UINT32 l_tmp;
5841     OPJ_UINT32 l_indix;
5842     opj_tcp_t * l_tcp;
5843     opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5844     opj_mct_data_t * l_mct_data;
5845     OPJ_UINT32 l_nb_collections;
5846     OPJ_UINT32 l_nb_comps;
5847     OPJ_UINT32 l_nb_bytes_by_comp;
5848     OPJ_BOOL l_new_mcc = OPJ_FALSE;
5849 
5850     /* preconditions */
5851     assert(p_header_data != 00);
5852     assert(p_j2k != 00);
5853     assert(p_manager != 00);
5854 
5855     l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5856             &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5857             p_j2k->m_specific_param.m_decoder.m_default_tcp;
5858 
5859     if (p_header_size < 2) {
5860         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5861         return OPJ_FALSE;
5862     }
5863 
5864     /* first marker */
5865     opj_read_bytes(p_header_data, &l_tmp, 2);                       /* Zmcc */
5866     p_header_data += 2;
5867     if (l_tmp != 0) {
5868         opj_event_msg(p_manager, EVT_WARNING,
5869                       "Cannot take in charge multiple data spanning\n");
5870         return OPJ_TRUE;
5871     }
5872 
5873     if (p_header_size < 7) {
5874         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5875         return OPJ_FALSE;
5876     }
5877 
5878     opj_read_bytes(p_header_data, &l_indix,
5879                    1); /* Imcc -> no need for other values, take the first */
5880     ++p_header_data;
5881 
5882     l_mcc_record = l_tcp->m_mcc_records;
5883 
5884     for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
5885         if (l_mcc_record->m_index == l_indix) {
5886             break;
5887         }
5888         ++l_mcc_record;
5889     }
5890 
5891     /** NOT FOUND */
5892     if (i == l_tcp->m_nb_mcc_records) {
5893         if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5894             opj_simple_mcc_decorrelation_data_t *new_mcc_records;
5895             l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
5896 
5897             new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
5898                                   l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(
5899                                       opj_simple_mcc_decorrelation_data_t));
5900             if (! new_mcc_records) {
5901                 opj_free(l_tcp->m_mcc_records);
5902                 l_tcp->m_mcc_records = NULL;
5903                 l_tcp->m_nb_max_mcc_records = 0;
5904                 l_tcp->m_nb_mcc_records = 0;
5905                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
5906                 return OPJ_FALSE;
5907             }
5908             l_tcp->m_mcc_records = new_mcc_records;
5909             l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5910             memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records)
5911                    * sizeof(opj_simple_mcc_decorrelation_data_t));
5912         }
5913         l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5914         l_new_mcc = OPJ_TRUE;
5915     }
5916     l_mcc_record->m_index = l_indix;
5917 
5918     /* only one marker atm */
5919     opj_read_bytes(p_header_data, &l_tmp, 2);                       /* Ymcc */
5920     p_header_data += 2;
5921     if (l_tmp != 0) {
5922         opj_event_msg(p_manager, EVT_WARNING,
5923                       "Cannot take in charge multiple data spanning\n");
5924         return OPJ_TRUE;
5925     }
5926 
5927     opj_read_bytes(p_header_data, &l_nb_collections,
5928                    2);                              /* Qmcc -> number of collections -> 1 */
5929     p_header_data += 2;
5930 
5931     if (l_nb_collections > 1) {
5932         opj_event_msg(p_manager, EVT_WARNING,
5933                       "Cannot take in charge multiple collections\n");
5934         return OPJ_TRUE;
5935     }
5936 
5937     p_header_size -= 7;
5938 
5939     for (i = 0; i < l_nb_collections; ++i) {
5940         if (p_header_size < 3) {
5941             opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5942             return OPJ_FALSE;
5943         }
5944 
5945         opj_read_bytes(p_header_data, &l_tmp,
5946                        1); /* Xmcci type of component transformation -> array based decorrelation */
5947         ++p_header_data;
5948 
5949         if (l_tmp != 1) {
5950             opj_event_msg(p_manager, EVT_WARNING,
5951                           "Cannot take in charge collections other than array decorrelation\n");
5952             return OPJ_TRUE;
5953         }
5954 
5955         opj_read_bytes(p_header_data, &l_nb_comps, 2);
5956 
5957         p_header_data += 2;
5958         p_header_size -= 3;
5959 
5960         l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15);
5961         l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5962 
5963         if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5964             opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5965             return OPJ_FALSE;
5966         }
5967 
5968         p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5969 
5970         for (j = 0; j < l_mcc_record->m_nb_comps; ++j) {
5971             opj_read_bytes(p_header_data, &l_tmp,
5972                            l_nb_bytes_by_comp);      /* Cmccij Component offset*/
5973             p_header_data += l_nb_bytes_by_comp;
5974 
5975             if (l_tmp != j) {
5976                 opj_event_msg(p_manager, EVT_WARNING,
5977                               "Cannot take in charge collections with indix shuffle\n");
5978                 return OPJ_TRUE;
5979             }
5980         }
5981 
5982         opj_read_bytes(p_header_data, &l_nb_comps, 2);
5983         p_header_data += 2;
5984 
5985         l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15);
5986         l_nb_comps &= 0x7fff;
5987 
5988         if (l_nb_comps != l_mcc_record->m_nb_comps) {
5989             opj_event_msg(p_manager, EVT_WARNING,
5990                           "Cannot take in charge collections without same number of indixes\n");
5991             return OPJ_TRUE;
5992         }
5993 
5994         if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5995             opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5996             return OPJ_FALSE;
5997         }
5998 
5999         p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6000 
6001         for (j = 0; j < l_mcc_record->m_nb_comps; ++j) {
6002             opj_read_bytes(p_header_data, &l_tmp,
6003                            l_nb_bytes_by_comp);      /* Wmccij Component offset*/
6004             p_header_data += l_nb_bytes_by_comp;
6005 
6006             if (l_tmp != j) {
6007                 opj_event_msg(p_manager, EVT_WARNING,
6008                               "Cannot take in charge collections with indix shuffle\n");
6009                 return OPJ_TRUE;
6010             }
6011         }
6012 
6013         opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/
6014         p_header_data += 3;
6015 
6016         l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1);
6017         l_mcc_record->m_decorrelation_array = 00;
6018         l_mcc_record->m_offset_array = 00;
6019 
6020         l_indix = l_tmp & 0xff;
6021         if (l_indix != 0) {
6022             l_mct_data = l_tcp->m_mct_records;
6023             for (j = 0; j < l_tcp->m_nb_mct_records; ++j) {
6024                 if (l_mct_data->m_index == l_indix) {
6025                     l_mcc_record->m_decorrelation_array = l_mct_data;
6026                     break;
6027                 }
6028                 ++l_mct_data;
6029             }
6030 
6031             if (l_mcc_record->m_decorrelation_array == 00) {
6032                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6033                 return OPJ_FALSE;
6034             }
6035         }
6036 
6037         l_indix = (l_tmp >> 8) & 0xff;
6038         if (l_indix != 0) {
6039             l_mct_data = l_tcp->m_mct_records;
6040             for (j = 0; j < l_tcp->m_nb_mct_records; ++j) {
6041                 if (l_mct_data->m_index == l_indix) {
6042                     l_mcc_record->m_offset_array = l_mct_data;
6043                     break;
6044                 }
6045                 ++l_mct_data;
6046             }
6047 
6048             if (l_mcc_record->m_offset_array == 00) {
6049                 opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6050                 return OPJ_FALSE;
6051             }
6052         }
6053     }
6054 
6055     if (p_header_size != 0) {
6056         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6057         return OPJ_FALSE;
6058     }
6059 
6060     if (l_new_mcc) {
6061         ++l_tcp->m_nb_mcc_records;
6062     }
6063 
6064     return OPJ_TRUE;
6065 }
6066 
opj_j2k_write_mco(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)6067 static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k,
6068                                   struct opj_stream_private *p_stream,
6069                                   struct opj_event_mgr * p_manager
6070                                  )
6071 {
6072     OPJ_BYTE * l_current_data = 00;
6073     OPJ_UINT32 l_mco_size;
6074     opj_tcp_t * l_tcp = 00;
6075     opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6076     OPJ_UINT32 i;
6077 
6078     /* preconditions */
6079     assert(p_j2k != 00);
6080     assert(p_manager != 00);
6081     assert(p_stream != 00);
6082 
6083     l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6084 
6085     l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6086     if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6087 
6088         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
6089                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
6090         if (! new_header_tile_data) {
6091             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6092             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
6093             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6094             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
6095             return OPJ_FALSE;
6096         }
6097         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
6098         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6099     }
6100     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6101 
6102 
6103     opj_write_bytes(l_current_data, J2K_MS_MCO, 2);                 /* MCO */
6104     l_current_data += 2;
6105 
6106     opj_write_bytes(l_current_data, l_mco_size - 2, 2);             /* Lmco */
6107     l_current_data += 2;
6108 
6109     opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records,
6110                     1);    /* Nmco : only one transform stage*/
6111     ++l_current_data;
6112 
6113     l_mcc_record = l_tcp->m_mcc_records;
6114     for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
6115         opj_write_bytes(l_current_data, l_mcc_record->m_index,
6116                         1); /* Imco -> use the mcc indicated by 1*/
6117         ++l_current_data;
6118         ++l_mcc_record;
6119     }
6120 
6121     if (opj_stream_write_data(p_stream,
6122                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size,
6123                               p_manager) != l_mco_size) {
6124         return OPJ_FALSE;
6125     }
6126 
6127     return OPJ_TRUE;
6128 }
6129 
6130 /**
6131  * Reads a MCO marker (Multiple Component Transform Ordering)
6132  *
6133  * @param       p_header_data   the data contained in the MCO box.
6134  * @param       p_j2k                   the jpeg2000 codec.
6135  * @param       p_header_size   the size of the data contained in the MCO marker.
6136  * @param       p_manager               the user event manager.
6137 */
opj_j2k_read_mco(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)6138 static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k,
6139                                  OPJ_BYTE * p_header_data,
6140                                  OPJ_UINT32 p_header_size,
6141                                  opj_event_mgr_t * p_manager
6142                                 )
6143 {
6144     OPJ_UINT32 l_tmp, i;
6145     OPJ_UINT32 l_nb_stages;
6146     opj_tcp_t * l_tcp;
6147     opj_tccp_t * l_tccp;
6148     opj_image_t * l_image;
6149 
6150     /* preconditions */
6151     assert(p_header_data != 00);
6152     assert(p_j2k != 00);
6153     assert(p_manager != 00);
6154 
6155     l_image = p_j2k->m_private_image;
6156     l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6157             &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6158             p_j2k->m_specific_param.m_decoder.m_default_tcp;
6159 
6160     if (p_header_size < 1) {
6161         opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6162         return OPJ_FALSE;
6163     }
6164 
6165     opj_read_bytes(p_header_data, &l_nb_stages,
6166                    1);                         /* Nmco : only one transform stage*/
6167     ++p_header_data;
6168 
6169     if (l_nb_stages > 1) {
6170         opj_event_msg(p_manager, EVT_WARNING,
6171                       "Cannot take in charge multiple transformation stages.\n");
6172         return OPJ_TRUE;
6173     }
6174 
6175     if (p_header_size != l_nb_stages + 1) {
6176         opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6177         return OPJ_FALSE;
6178     }
6179 
6180     l_tccp = l_tcp->tccps;
6181 
6182     for (i = 0; i < l_image->numcomps; ++i) {
6183         l_tccp->m_dc_level_shift = 0;
6184         ++l_tccp;
6185     }
6186 
6187     if (l_tcp->m_mct_decoding_matrix) {
6188         opj_free(l_tcp->m_mct_decoding_matrix);
6189         l_tcp->m_mct_decoding_matrix = 00;
6190     }
6191 
6192     for (i = 0; i < l_nb_stages; ++i) {
6193         opj_read_bytes(p_header_data, &l_tmp, 1);
6194         ++p_header_data;
6195 
6196         if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) {
6197             return OPJ_FALSE;
6198         }
6199     }
6200 
6201     return OPJ_TRUE;
6202 }
6203 
opj_j2k_add_mct(opj_tcp_t * p_tcp,opj_image_t * p_image,OPJ_UINT32 p_index)6204 static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image,
6205                                 OPJ_UINT32 p_index)
6206 {
6207     OPJ_UINT32 i;
6208     opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6209     opj_mct_data_t * l_deco_array, * l_offset_array;
6210     OPJ_UINT32 l_data_size, l_mct_size, l_offset_size;
6211     OPJ_UINT32 l_nb_elem;
6212     OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6213     opj_tccp_t * l_tccp;
6214 
6215     /* preconditions */
6216     assert(p_tcp != 00);
6217 
6218     l_mcc_record = p_tcp->m_mcc_records;
6219 
6220     for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) {
6221         if (l_mcc_record->m_index == p_index) {
6222             break;
6223         }
6224     }
6225 
6226     if (i == p_tcp->m_nb_mcc_records) {
6227         /** element discarded **/
6228         return OPJ_TRUE;
6229     }
6230 
6231     if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6232         /** do not support number of comps != image */
6233         return OPJ_TRUE;
6234     }
6235 
6236     l_deco_array = l_mcc_record->m_decorrelation_array;
6237 
6238     if (l_deco_array) {
6239         l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps
6240                       * p_image->numcomps;
6241         if (l_deco_array->m_data_size != l_data_size || ! l_deco_array->m_data) {
6242             return OPJ_FALSE;
6243         }
6244 
6245         l_nb_elem = p_image->numcomps * p_image->numcomps;
6246         l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
6247         p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6248 
6249         if (! p_tcp->m_mct_decoding_matrix) {
6250             return OPJ_FALSE;
6251         }
6252 
6253         j2k_mct_read_functions_to_float[l_deco_array->m_element_type](
6254             l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem);
6255     }
6256 
6257     l_offset_array = l_mcc_record->m_offset_array;
6258 
6259     if (l_offset_array) {
6260         l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] *
6261                       p_image->numcomps;
6262         if (l_offset_array->m_data_size != l_data_size || ! l_offset_array->m_data) {
6263             return OPJ_FALSE;
6264         }
6265 
6266         l_nb_elem = p_image->numcomps;
6267         l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);
6268         l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6269 
6270         if (! l_offset_data) {
6271             return OPJ_FALSE;
6272         }
6273 
6274         j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](
6275             l_offset_array->m_data, l_offset_data, l_nb_elem);
6276 
6277         l_tccp = p_tcp->tccps;
6278         l_current_offset_data = l_offset_data;
6279 
6280         for (i = 0; i < p_image->numcomps; ++i) {
6281             l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++);
6282             ++l_tccp;
6283         }
6284 
6285         opj_free(l_offset_data);
6286     }
6287 
6288     return OPJ_TRUE;
6289 }
6290 
opj_j2k_write_cbd(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)6291 static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k,
6292                                   struct opj_stream_private *p_stream,
6293                                   struct opj_event_mgr * p_manager)
6294 {
6295     OPJ_UINT32 i;
6296     OPJ_UINT32 l_cbd_size;
6297     OPJ_BYTE * l_current_data = 00;
6298     opj_image_t *l_image = 00;
6299     opj_image_comp_t * l_comp = 00;
6300 
6301     /* preconditions */
6302     assert(p_j2k != 00);
6303     assert(p_manager != 00);
6304     assert(p_stream != 00);
6305 
6306     l_image = p_j2k->m_private_image;
6307     l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6308 
6309     if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6310         OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
6311                                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
6312         if (! new_header_tile_data) {
6313             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6314             p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
6315             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6316             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
6317             return OPJ_FALSE;
6318         }
6319         p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
6320         p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6321     }
6322 
6323     l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6324 
6325     opj_write_bytes(l_current_data, J2K_MS_CBD, 2);                 /* CBD */
6326     l_current_data += 2;
6327 
6328     opj_write_bytes(l_current_data, l_cbd_size - 2, 2);             /* L_CBD */
6329     l_current_data += 2;
6330 
6331     opj_write_bytes(l_current_data, l_image->numcomps, 2);          /* Ncbd */
6332     l_current_data += 2;
6333 
6334     l_comp = l_image->comps;
6335 
6336     for (i = 0; i < l_image->numcomps; ++i) {
6337         opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1),
6338                         1);           /* Component bit depth */
6339         ++l_current_data;
6340 
6341         ++l_comp;
6342     }
6343 
6344     if (opj_stream_write_data(p_stream,
6345                               p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size,
6346                               p_manager) != l_cbd_size) {
6347         return OPJ_FALSE;
6348     }
6349 
6350     return OPJ_TRUE;
6351 }
6352 
6353 /**
6354  * Reads a CBD marker (Component bit depth definition)
6355  * @param       p_header_data   the data contained in the CBD box.
6356  * @param       p_j2k                   the jpeg2000 codec.
6357  * @param       p_header_size   the size of the data contained in the CBD marker.
6358  * @param       p_manager               the user event manager.
6359 */
opj_j2k_read_cbd(opj_j2k_t * p_j2k,OPJ_BYTE * p_header_data,OPJ_UINT32 p_header_size,opj_event_mgr_t * p_manager)6360 static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k,
6361                                  OPJ_BYTE * p_header_data,
6362                                  OPJ_UINT32 p_header_size,
6363                                  opj_event_mgr_t * p_manager
6364                                 )
6365 {
6366     OPJ_UINT32 l_nb_comp, l_num_comp;
6367     OPJ_UINT32 l_comp_def;
6368     OPJ_UINT32 i;
6369     opj_image_comp_t * l_comp = 00;
6370 
6371     /* preconditions */
6372     assert(p_header_data != 00);
6373     assert(p_j2k != 00);
6374     assert(p_manager != 00);
6375 
6376     l_num_comp = p_j2k->m_private_image->numcomps;
6377 
6378     if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6379         opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6380         return OPJ_FALSE;
6381     }
6382 
6383     opj_read_bytes(p_header_data, &l_nb_comp,
6384                    2);                           /* Ncbd */
6385     p_header_data += 2;
6386 
6387     if (l_nb_comp != l_num_comp) {
6388         opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6389         return OPJ_FALSE;
6390     }
6391 
6392     l_comp = p_j2k->m_private_image->comps;
6393     for (i = 0; i < l_num_comp; ++i) {
6394         opj_read_bytes(p_header_data, &l_comp_def,
6395                        1);                  /* Component bit depth */
6396         ++p_header_data;
6397         l_comp->sgnd = (l_comp_def >> 7) & 1;
6398         l_comp->prec = (l_comp_def & 0x7f) + 1;
6399 
6400         if (l_comp->prec > 31) {
6401             opj_event_msg(p_manager, EVT_ERROR,
6402                           "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n",
6403                           i, l_comp->prec);
6404             return OPJ_FALSE;
6405         }
6406         ++l_comp;
6407     }
6408 
6409     return OPJ_TRUE;
6410 }
6411 
6412 /* ----------------------------------------------------------------------- */
6413 /* J2K / JPT decoder interface                                             */
6414 /* ----------------------------------------------------------------------- */
6415 
opj_j2k_setup_decoder(opj_j2k_t * j2k,opj_dparameters_t * parameters)6416 void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
6417 {
6418     if (j2k && parameters) {
6419         j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6420         j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6421 
6422         j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG);
6423 #ifdef USE_JPWL
6424         j2k->m_cp.correct = parameters->jpwl_correct;
6425         j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6426         j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6427 #endif /* USE_JPWL */
6428     }
6429 }
6430 
opj_j2k_set_threads(opj_j2k_t * j2k,OPJ_UINT32 num_threads)6431 OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads)
6432 {
6433     /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */
6434     /* afterwards */
6435     if (opj_has_thread_support() && j2k->m_tcd == NULL) {
6436         opj_thread_pool_destroy(j2k->m_tp);
6437         j2k->m_tp = NULL;
6438         if (num_threads <= (OPJ_UINT32)INT_MAX) {
6439             j2k->m_tp = opj_thread_pool_create((int)num_threads);
6440         }
6441         if (j2k->m_tp == NULL) {
6442             j2k->m_tp = opj_thread_pool_create(0);
6443             return OPJ_FALSE;
6444         }
6445         return OPJ_TRUE;
6446     }
6447     return OPJ_FALSE;
6448 }
6449 
opj_j2k_get_default_thread_count()6450 static int opj_j2k_get_default_thread_count()
6451 {
6452     const char* num_threads_str = getenv("OPJ_NUM_THREADS");
6453     int num_cpus;
6454     int num_threads;
6455 
6456     if (num_threads_str == NULL || !opj_has_thread_support()) {
6457         return 0;
6458     }
6459     num_cpus = opj_get_num_cpus();
6460     if (strcmp(num_threads_str, "ALL_CPUS") == 0) {
6461         return num_cpus;
6462     }
6463     if (num_cpus == 0) {
6464         num_cpus = 32;
6465     }
6466     num_threads = atoi(num_threads_str);
6467     if (num_threads < 0) {
6468         num_threads = 0;
6469     } else if (num_threads > 2 * num_cpus) {
6470         num_threads = 2 * num_cpus;
6471     }
6472     return num_threads;
6473 }
6474 
6475 /* ----------------------------------------------------------------------- */
6476 /* J2K encoder interface                                                       */
6477 /* ----------------------------------------------------------------------- */
6478 
opj_j2k_create_compress(void)6479 opj_j2k_t* opj_j2k_create_compress(void)
6480 {
6481     opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6482     if (!l_j2k) {
6483         return NULL;
6484     }
6485 
6486 
6487     l_j2k->m_is_decoder = 0;
6488     l_j2k->m_cp.m_is_decoder = 0;
6489 
6490     l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(
6491                 OPJ_J2K_DEFAULT_HEADER_SIZE);
6492     if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6493         opj_j2k_destroy(l_j2k);
6494         return NULL;
6495     }
6496 
6497     l_j2k->m_specific_param.m_encoder.m_header_tile_data_size =
6498         OPJ_J2K_DEFAULT_HEADER_SIZE;
6499 
6500     /* validation list creation*/
6501     l_j2k->m_validation_list = opj_procedure_list_create();
6502     if (! l_j2k->m_validation_list) {
6503         opj_j2k_destroy(l_j2k);
6504         return NULL;
6505     }
6506 
6507     /* execution list creation*/
6508     l_j2k->m_procedure_list = opj_procedure_list_create();
6509     if (! l_j2k->m_procedure_list) {
6510         opj_j2k_destroy(l_j2k);
6511         return NULL;
6512     }
6513 
6514     l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count());
6515     if (!l_j2k->m_tp) {
6516         l_j2k->m_tp = opj_thread_pool_create(0);
6517     }
6518     if (!l_j2k->m_tp) {
6519         opj_j2k_destroy(l_j2k);
6520         return NULL;
6521     }
6522 
6523     return l_j2k;
6524 }
6525 
opj_j2k_initialise_4K_poc(opj_poc_t * POC,int numres)6526 static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres)
6527 {
6528     POC[0].tile  = 1;
6529     POC[0].resno0  = 0;
6530     POC[0].compno0 = 0;
6531     POC[0].layno1  = 1;
6532     POC[0].resno1  = (OPJ_UINT32)(numres - 1);
6533     POC[0].compno1 = 3;
6534     POC[0].prg1 = OPJ_CPRL;
6535     POC[1].tile  = 1;
6536     POC[1].resno0  = (OPJ_UINT32)(numres - 1);
6537     POC[1].compno0 = 0;
6538     POC[1].layno1  = 1;
6539     POC[1].resno1  = (OPJ_UINT32)numres;
6540     POC[1].compno1 = 3;
6541     POC[1].prg1 = OPJ_CPRL;
6542     return 2;
6543 }
6544 
opj_j2k_set_cinema_parameters(opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)6545 static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters,
6546         opj_image_t *image, opj_event_mgr_t *p_manager)
6547 {
6548     /* Configure cinema parameters */
6549     int i;
6550 
6551     /* No tiling */
6552     parameters->tile_size_on = OPJ_FALSE;
6553     parameters->cp_tdx = 1;
6554     parameters->cp_tdy = 1;
6555 
6556     /* One tile part for each component */
6557     parameters->tp_flag = 'C';
6558     parameters->tp_on = 1;
6559 
6560     /* Tile and Image shall be at (0,0) */
6561     parameters->cp_tx0 = 0;
6562     parameters->cp_ty0 = 0;
6563     parameters->image_offset_x0 = 0;
6564     parameters->image_offset_y0 = 0;
6565 
6566     /* Codeblock size= 32*32 */
6567     parameters->cblockw_init = 32;
6568     parameters->cblockh_init = 32;
6569 
6570     /* Codeblock style: no mode switch enabled */
6571     parameters->mode = 0;
6572 
6573     /* No ROI */
6574     parameters->roi_compno = -1;
6575 
6576     /* No subsampling */
6577     parameters->subsampling_dx = 1;
6578     parameters->subsampling_dy = 1;
6579 
6580     /* 9-7 transform */
6581     parameters->irreversible = 1;
6582 
6583     /* Number of layers */
6584     if (parameters->tcp_numlayers > 1) {
6585         opj_event_msg(p_manager, EVT_WARNING,
6586                       "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6587                       "1 single quality layer"
6588                       "-> Number of layers forced to 1 (rather than %d)\n"
6589                       "-> Rate of the last layer (%3.1f) will be used",
6590                       parameters->tcp_numlayers,
6591                       parameters->tcp_rates[parameters->tcp_numlayers - 1]);
6592         parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1];
6593         parameters->tcp_numlayers = 1;
6594     }
6595 
6596     /* Resolution levels */
6597     switch (parameters->rsiz) {
6598     case OPJ_PROFILE_CINEMA_2K:
6599         if (parameters->numresolution > 6) {
6600             opj_event_msg(p_manager, EVT_WARNING,
6601                           "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6602                           "Number of decomposition levels <= 5\n"
6603                           "-> Number of decomposition levels forced to 5 (rather than %d)\n",
6604                           parameters->numresolution + 1);
6605             parameters->numresolution = 6;
6606         }
6607         break;
6608     case OPJ_PROFILE_CINEMA_4K:
6609         if (parameters->numresolution < 2) {
6610             opj_event_msg(p_manager, EVT_WARNING,
6611                           "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6612                           "Number of decomposition levels >= 1 && <= 6\n"
6613                           "-> Number of decomposition levels forced to 1 (rather than %d)\n",
6614                           parameters->numresolution + 1);
6615             parameters->numresolution = 1;
6616         } else if (parameters->numresolution > 7) {
6617             opj_event_msg(p_manager, EVT_WARNING,
6618                           "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6619                           "Number of decomposition levels >= 1 && <= 6\n"
6620                           "-> Number of decomposition levels forced to 6 (rather than %d)\n",
6621                           parameters->numresolution + 1);
6622             parameters->numresolution = 7;
6623         }
6624         break;
6625     default :
6626         break;
6627     }
6628 
6629     /* Precincts */
6630     parameters->csty |= 0x01;
6631     if (parameters->numresolution == 1) {
6632         parameters->res_spec = 1;
6633         parameters->prcw_init[0] = 128;
6634         parameters->prch_init[0] = 128;
6635     } else {
6636         parameters->res_spec = parameters->numresolution - 1;
6637         for (i = 0; i < parameters->res_spec; i++) {
6638             parameters->prcw_init[i] = 256;
6639             parameters->prch_init[i] = 256;
6640         }
6641     }
6642 
6643     /* The progression order shall be CPRL */
6644     parameters->prog_order = OPJ_CPRL;
6645 
6646     /* Progression order changes for 4K, disallowed for 2K */
6647     if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) {
6648         parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,
6649                               parameters->numresolution);
6650     } else {
6651         parameters->numpocs = 0;
6652     }
6653 
6654     /* Limited bit-rate */
6655     parameters->cp_disto_alloc = 1;
6656     if (parameters->max_cs_size <= 0) {
6657         /* No rate has been introduced, 24 fps is assumed */
6658         parameters->max_cs_size = OPJ_CINEMA_24_CS;
6659         opj_event_msg(p_manager, EVT_WARNING,
6660                       "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6661                       "Maximum 1302083 compressed bytes @ 24fps\n"
6662                       "As no rate has been given, this limit will be used.\n");
6663     } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) {
6664         opj_event_msg(p_manager, EVT_WARNING,
6665                       "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6666                       "Maximum 1302083 compressed bytes @ 24fps\n"
6667                       "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n");
6668         parameters->max_cs_size = OPJ_CINEMA_24_CS;
6669     }
6670 
6671     if (parameters->max_comp_size <= 0) {
6672         /* No rate has been introduced, 24 fps is assumed */
6673         parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6674         opj_event_msg(p_manager, EVT_WARNING,
6675                       "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6676                       "Maximum 1041666 compressed bytes @ 24fps\n"
6677                       "As no rate has been given, this limit will be used.\n");
6678     } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) {
6679         opj_event_msg(p_manager, EVT_WARNING,
6680                       "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
6681                       "Maximum 1041666 compressed bytes @ 24fps\n"
6682                       "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n");
6683         parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6684     }
6685 
6686     parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w *
6687                                image->comps[0].h * image->comps[0].prec) /
6688                                (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx *
6689                                        image->comps[0].dy);
6690 
6691 }
6692 
opj_j2k_is_cinema_compliant(opj_image_t * image,OPJ_UINT16 rsiz,opj_event_mgr_t * p_manager)6693 static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
6694         opj_event_mgr_t *p_manager)
6695 {
6696     OPJ_UINT32 i;
6697 
6698     /* Number of components */
6699     if (image->numcomps != 3) {
6700         opj_event_msg(p_manager, EVT_WARNING,
6701                       "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6702                       "3 components"
6703                       "-> Number of components of input image (%d) is not compliant\n"
6704                       "-> Non-profile-3 codestream will be generated\n",
6705                       image->numcomps);
6706         return OPJ_FALSE;
6707     }
6708 
6709     /* Bitdepth */
6710     for (i = 0; i < image->numcomps; i++) {
6711         if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)) {
6712             char signed_str[] = "signed";
6713             char unsigned_str[] = "unsigned";
6714             char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
6715             opj_event_msg(p_manager, EVT_WARNING,
6716                           "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6717                           "Precision of each component shall be 12 bits unsigned"
6718                           "-> At least component %d of input image (%d bits, %s) is not compliant\n"
6719                           "-> Non-profile-3 codestream will be generated\n",
6720                           i, image->comps[i].bpp, tmp_str);
6721             return OPJ_FALSE;
6722         }
6723     }
6724 
6725     /* Image size */
6726     switch (rsiz) {
6727     case OPJ_PROFILE_CINEMA_2K:
6728         if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) {
6729             opj_event_msg(p_manager, EVT_WARNING,
6730                           "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
6731                           "width <= 2048 and height <= 1080\n"
6732                           "-> Input image size %d x %d is not compliant\n"
6733                           "-> Non-profile-3 codestream will be generated\n",
6734                           image->comps[0].w, image->comps[0].h);
6735             return OPJ_FALSE;
6736         }
6737         break;
6738     case OPJ_PROFILE_CINEMA_4K:
6739         if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) {
6740             opj_event_msg(p_manager, EVT_WARNING,
6741                           "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
6742                           "width <= 4096 and height <= 2160\n"
6743                           "-> Image size %d x %d is not compliant\n"
6744                           "-> Non-profile-4 codestream will be generated\n",
6745                           image->comps[0].w, image->comps[0].h);
6746             return OPJ_FALSE;
6747         }
6748         break;
6749     default :
6750         break;
6751     }
6752 
6753     return OPJ_TRUE;
6754 }
6755 
opj_j2k_setup_encoder(opj_j2k_t * p_j2k,opj_cparameters_t * parameters,opj_image_t * image,opj_event_mgr_t * p_manager)6756 OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
6757                                opj_cparameters_t *parameters,
6758                                opj_image_t *image,
6759                                opj_event_mgr_t * p_manager)
6760 {
6761     OPJ_UINT32 i, j, tileno, numpocs_tile;
6762     opj_cp_t *cp = 00;
6763     OPJ_UINT32 cblkw, cblkh;
6764 
6765     if (!p_j2k || !parameters || ! image) {
6766         return OPJ_FALSE;
6767     }
6768 
6769     if ((parameters->numresolution <= 0) ||
6770             (parameters->numresolution > OPJ_J2K_MAXRLVLS)) {
6771         opj_event_msg(p_manager, EVT_ERROR,
6772                       "Invalid number of resolutions : %d not in range [1,%d]\n",
6773                       parameters->numresolution, OPJ_J2K_MAXRLVLS);
6774         return OPJ_FALSE;
6775     }
6776 
6777     if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) {
6778         opj_event_msg(p_manager, EVT_ERROR,
6779                       "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
6780                       parameters->cblockw_init);
6781         return OPJ_FALSE;
6782     }
6783     if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) {
6784         opj_event_msg(p_manager, EVT_ERROR,
6785                       "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n",
6786                       parameters->cblockh_init);
6787         return OPJ_FALSE;
6788     }
6789     if (parameters->cblockw_init * parameters->cblockh_init > 4096) {
6790         opj_event_msg(p_manager, EVT_ERROR,
6791                       "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n");
6792         return OPJ_FALSE;
6793     }
6794     cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
6795     cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
6796     if (parameters->cblockw_init != (1 << cblkw)) {
6797         opj_event_msg(p_manager, EVT_ERROR,
6798                       "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
6799                       parameters->cblockw_init);
6800         return OPJ_FALSE;
6801     }
6802     if (parameters->cblockh_init != (1 << cblkh)) {
6803         opj_event_msg(p_manager, EVT_ERROR,
6804                       "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
6805                       parameters->cblockh_init);
6806         return OPJ_FALSE;
6807     }
6808 
6809     /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6810     cp = &(p_j2k->m_cp);
6811 
6812     /* set default values for cp */
6813     cp->tw = 1;
6814     cp->th = 1;
6815 
6816     /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
6817     if (parameters->rsiz ==
6818             OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
6819         OPJ_BOOL deprecated_used = OPJ_FALSE;
6820         switch (parameters->cp_cinema) {
6821         case OPJ_CINEMA2K_24:
6822             parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
6823             parameters->max_cs_size = OPJ_CINEMA_24_CS;
6824             parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6825             deprecated_used = OPJ_TRUE;
6826             break;
6827         case OPJ_CINEMA2K_48:
6828             parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
6829             parameters->max_cs_size = OPJ_CINEMA_48_CS;
6830             parameters->max_comp_size = OPJ_CINEMA_48_COMP;
6831             deprecated_used = OPJ_TRUE;
6832             break;
6833         case OPJ_CINEMA4K_24:
6834             parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
6835             parameters->max_cs_size = OPJ_CINEMA_24_CS;
6836             parameters->max_comp_size = OPJ_CINEMA_24_COMP;
6837             deprecated_used = OPJ_TRUE;
6838             break;
6839         case OPJ_OFF:
6840         default:
6841             break;
6842         }
6843         switch (parameters->cp_rsiz) {
6844         case OPJ_CINEMA2K:
6845             parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
6846             deprecated_used = OPJ_TRUE;
6847             break;
6848         case OPJ_CINEMA4K:
6849             parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
6850             deprecated_used = OPJ_TRUE;
6851             break;
6852         case OPJ_MCT:
6853             parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT;
6854             deprecated_used = OPJ_TRUE;
6855         case OPJ_STD_RSIZ:
6856         default:
6857             break;
6858         }
6859         if (deprecated_used) {
6860             opj_event_msg(p_manager, EVT_WARNING,
6861                           "Deprecated fields cp_cinema or cp_rsiz are used\n"
6862                           "Please consider using only the rsiz field\n"
6863                           "See openjpeg.h documentation for more details\n");
6864         }
6865     }
6866 
6867     /* If no explicit layers are provided, use lossless settings */
6868     if (parameters->tcp_numlayers == 0) {
6869         parameters->tcp_numlayers = 1;
6870         parameters->cp_disto_alloc = 1;
6871         parameters->tcp_rates[0] = 0;
6872     }
6873 
6874     if (parameters->cp_disto_alloc) {
6875         /* Emit warnings if tcp_rates are not decreasing */
6876         for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
6877             OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i];
6878             OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1];
6879             if (rate_i_corr <= 1.0) {
6880                 rate_i_corr = 1.0;
6881             }
6882             if (rate_i_m_1_corr <= 1.0) {
6883                 rate_i_m_1_corr = 1.0;
6884             }
6885             if (rate_i_corr >= rate_i_m_1_corr) {
6886                 if (rate_i_corr != parameters->tcp_rates[i] &&
6887                         rate_i_m_1_corr != parameters->tcp_rates[i - 1]) {
6888                     opj_event_msg(p_manager, EVT_WARNING,
6889                                   "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser "
6890                                   "than tcp_rates[%d]=%f (corrected as %f)\n",
6891                                   i, parameters->tcp_rates[i], rate_i_corr,
6892                                   i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr);
6893                 } else if (rate_i_corr != parameters->tcp_rates[i]) {
6894                     opj_event_msg(p_manager, EVT_WARNING,
6895                                   "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser "
6896                                   "than tcp_rates[%d]=%f\n",
6897                                   i, parameters->tcp_rates[i], rate_i_corr,
6898                                   i - 1, parameters->tcp_rates[i - 1]);
6899                 } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) {
6900                     opj_event_msg(p_manager, EVT_WARNING,
6901                                   "tcp_rates[%d]=%f should be strictly lesser "
6902                                   "than tcp_rates[%d]=%f (corrected as %f)\n",
6903                                   i, parameters->tcp_rates[i],
6904                                   i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr);
6905                 } else {
6906                     opj_event_msg(p_manager, EVT_WARNING,
6907                                   "tcp_rates[%d]=%f should be strictly lesser "
6908                                   "than tcp_rates[%d]=%f\n",
6909                                   i, parameters->tcp_rates[i],
6910                                   i - 1, parameters->tcp_rates[i - 1]);
6911                 }
6912             }
6913         }
6914     } else if (parameters->cp_fixed_quality) {
6915         /* Emit warnings if tcp_distoratio are not increasing */
6916         for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
6917             if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] &&
6918                     !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 &&
6919                       parameters->tcp_distoratio[i] == 0)) {
6920                 opj_event_msg(p_manager, EVT_WARNING,
6921                               "tcp_distoratio[%d]=%f should be strictly greater "
6922                               "than tcp_distoratio[%d]=%f\n",
6923                               i, parameters->tcp_distoratio[i], i - 1,
6924                               parameters->tcp_distoratio[i - 1]);
6925             }
6926         }
6927     }
6928 
6929     /* see if max_codestream_size does limit input rate */
6930     if (parameters->max_cs_size <= 0) {
6931         if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) {
6932             OPJ_FLOAT32 temp_size;
6933             temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
6934                                        image->comps[0].h * image->comps[0].prec) /
6935                                       ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 *
6936                                        image->comps[0].dx * image->comps[0].dy));
6937             if (temp_size > INT_MAX) {
6938                 parameters->max_cs_size = INT_MAX;
6939             } else {
6940                 parameters->max_cs_size = (int) floor(temp_size);
6941             }
6942         } else {
6943             parameters->max_cs_size = 0;
6944         }
6945     } else {
6946         OPJ_FLOAT32 temp_rate;
6947         OPJ_BOOL cap = OPJ_FALSE;
6948         temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
6949                                    image->comps[0].h * image->comps[0].prec) /
6950                                   (((double)parameters->max_cs_size) * 8 * image->comps[0].dx *
6951                                    image->comps[0].dy));
6952         for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
6953             if (parameters->tcp_rates[i] < temp_rate) {
6954                 parameters->tcp_rates[i] = temp_rate;
6955                 cap = OPJ_TRUE;
6956             }
6957         }
6958         if (cap) {
6959             opj_event_msg(p_manager, EVT_WARNING,
6960                           "The desired maximum codestream size has limited\n"
6961                           "at least one of the desired quality layers\n");
6962         }
6963     }
6964 
6965     /* Manage profiles and applications and set RSIZ */
6966     /* set cinema parameters if required */
6967     if (OPJ_IS_CINEMA(parameters->rsiz)) {
6968         if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K)
6969                 || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) {
6970             opj_event_msg(p_manager, EVT_WARNING,
6971                           "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n");
6972             parameters->rsiz = OPJ_PROFILE_NONE;
6973         } else {
6974             opj_j2k_set_cinema_parameters(parameters, image, p_manager);
6975             if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) {
6976                 parameters->rsiz = OPJ_PROFILE_NONE;
6977             }
6978         }
6979     } else if (OPJ_IS_STORAGE(parameters->rsiz)) {
6980         opj_event_msg(p_manager, EVT_WARNING,
6981                       "JPEG 2000 Long Term Storage profile not yet supported\n");
6982         parameters->rsiz = OPJ_PROFILE_NONE;
6983     } else if (OPJ_IS_BROADCAST(parameters->rsiz)) {
6984         opj_event_msg(p_manager, EVT_WARNING,
6985                       "JPEG 2000 Broadcast profiles not yet supported\n");
6986         parameters->rsiz = OPJ_PROFILE_NONE;
6987     } else if (OPJ_IS_IMF(parameters->rsiz)) {
6988         opj_event_msg(p_manager, EVT_WARNING,
6989                       "JPEG 2000 IMF profiles not yet supported\n");
6990         parameters->rsiz = OPJ_PROFILE_NONE;
6991     } else if (OPJ_IS_PART2(parameters->rsiz)) {
6992         if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) {
6993             opj_event_msg(p_manager, EVT_WARNING,
6994                           "JPEG 2000 Part-2 profile defined\n"
6995                           "but no Part-2 extension enabled.\n"
6996                           "Profile set to NONE.\n");
6997             parameters->rsiz = OPJ_PROFILE_NONE;
6998         } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) {
6999             opj_event_msg(p_manager, EVT_WARNING,
7000                           "Unsupported Part-2 extension enabled\n"
7001                           "Profile set to NONE.\n");
7002             parameters->rsiz = OPJ_PROFILE_NONE;
7003         }
7004     }
7005 
7006     /*
7007     copy user encoding parameters
7008     */
7009     cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)
7010             parameters->max_comp_size;
7011     cp->rsiz = parameters->rsiz;
7012     cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)
7013             parameters->cp_disto_alloc & 1u;
7014     cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)
7015             parameters->cp_fixed_alloc & 1u;
7016     cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)
7017             parameters->cp_fixed_quality & 1u;
7018 
7019     /* mod fixed_quality */
7020     if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
7021         size_t array_size = (size_t)parameters->tcp_numlayers *
7022                             (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
7023         cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7024         if (!cp->m_specific_param.m_enc.m_matrice) {
7025             opj_event_msg(p_manager, EVT_ERROR,
7026                           "Not enough memory to allocate copy of user encoding parameters matrix \n");
7027             return OPJ_FALSE;
7028         }
7029         memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice,
7030                array_size);
7031     }
7032 
7033     /* tiles */
7034     cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
7035     cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
7036 
7037     /* tile offset */
7038     cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
7039     cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
7040 
7041     /* comment string */
7042     if (parameters->cp_comment) {
7043         cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U);
7044         if (!cp->comment) {
7045             opj_event_msg(p_manager, EVT_ERROR,
7046                           "Not enough memory to allocate copy of comment string\n");
7047             return OPJ_FALSE;
7048         }
7049         strcpy(cp->comment, parameters->cp_comment);
7050     } else {
7051         /* Create default comment for codestream */
7052         const char comment[] = "Created by OpenJPEG version ";
7053         const size_t clen = strlen(comment);
7054         const char *version = opj_version();
7055 
7056         /* UniPG>> */
7057 #ifdef USE_JPWL
7058         cp->comment = (char*)opj_malloc(clen + strlen(version) + 11);
7059         if (!cp->comment) {
7060             opj_event_msg(p_manager, EVT_ERROR,
7061                           "Not enough memory to allocate comment string\n");
7062             return OPJ_FALSE;
7063         }
7064         sprintf(cp->comment, "%s%s with JPWL", comment, version);
7065 #else
7066         cp->comment = (char*)opj_malloc(clen + strlen(version) + 1);
7067         if (!cp->comment) {
7068             opj_event_msg(p_manager, EVT_ERROR,
7069                           "Not enough memory to allocate comment string\n");
7070             return OPJ_FALSE;
7071         }
7072         sprintf(cp->comment, "%s%s", comment, version);
7073 #endif
7074         /* <<UniPG */
7075     }
7076 
7077     /*
7078     calculate other encoding parameters
7079     */
7080 
7081     if (parameters->tile_size_on) {
7082         cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx);
7083         cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy);
7084     } else {
7085         cp->tdx = image->x1 - cp->tx0;
7086         cp->tdy = image->y1 - cp->ty0;
7087     }
7088 
7089     if (parameters->tp_on) {
7090         cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
7091         cp->m_specific_param.m_enc.m_tp_on = 1;
7092     }
7093 
7094 #ifdef USE_JPWL
7095     /*
7096     calculate JPWL encoding parameters
7097     */
7098 
7099     if (parameters->jpwl_epc_on) {
7100         OPJ_INT32 i;
7101 
7102         /* set JPWL on */
7103         cp->epc_on = OPJ_TRUE;
7104         cp->info_on = OPJ_FALSE; /* no informative technique */
7105 
7106         /* set EPB on */
7107         if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7108             cp->epb_on = OPJ_TRUE;
7109 
7110             cp->hprot_MH = parameters->jpwl_hprot_MH;
7111             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7112                 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7113                 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7114             }
7115             /* if tile specs are not specified, copy MH specs */
7116             if (cp->hprot_TPH[0] == -1) {
7117                 cp->hprot_TPH_tileno[0] = 0;
7118                 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7119             }
7120             for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7121                 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7122                 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7123                 cp->pprot[i] = parameters->jpwl_pprot[i];
7124             }
7125         }
7126 
7127         /* set ESD writing */
7128         if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7129             cp->esd_on = OPJ_TRUE;
7130 
7131             cp->sens_size = parameters->jpwl_sens_size;
7132             cp->sens_addr = parameters->jpwl_sens_addr;
7133             cp->sens_range = parameters->jpwl_sens_range;
7134 
7135             cp->sens_MH = parameters->jpwl_sens_MH;
7136             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7137                 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7138                 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7139             }
7140         }
7141 
7142         /* always set RED writing to false: we are at the encoder */
7143         cp->red_on = OPJ_FALSE;
7144 
7145     } else {
7146         cp->epc_on = OPJ_FALSE;
7147     }
7148 #endif /* USE_JPWL */
7149 
7150     /* initialize the mutiple tiles */
7151     /* ---------------------------- */
7152     cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7153     if (!cp->tcps) {
7154         opj_event_msg(p_manager, EVT_ERROR,
7155                       "Not enough memory to allocate tile coding parameters\n");
7156         return OPJ_FALSE;
7157     }
7158     if (parameters->numpocs) {
7159         /* initialisation of POC */
7160         opj_j2k_check_poc_val(parameters->POC, parameters->numpocs,
7161                               (OPJ_UINT32)parameters->numresolution, image->numcomps,
7162                               (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
7163         /* TODO MSD use the return value*/
7164     }
7165 
7166     for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7167         opj_tcp_t *tcp = &cp->tcps[tileno];
7168         tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
7169 
7170         for (j = 0; j < tcp->numlayers; j++) {
7171             if (OPJ_IS_CINEMA(cp->rsiz)) {
7172                 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7173                     tcp->distoratio[j] = parameters->tcp_distoratio[j];
7174                 }
7175                 tcp->rates[j] = parameters->tcp_rates[j];
7176             } else {
7177                 if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* add fixed_quality */
7178                     tcp->distoratio[j] = parameters->tcp_distoratio[j];
7179                 } else {
7180                     tcp->rates[j] = parameters->tcp_rates[j];
7181                 }
7182             }
7183             if (!cp->m_specific_param.m_enc.m_fixed_quality &&
7184                     tcp->rates[j] <= 1.0) {
7185                 tcp->rates[j] = 0.0;    /* force lossless */
7186             }
7187         }
7188 
7189         tcp->csty = (OPJ_UINT32)parameters->csty;
7190         tcp->prg = parameters->prog_order;
7191         tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
7192 
7193         numpocs_tile = 0;
7194         tcp->POC = 0;
7195 
7196         if (parameters->numpocs) {
7197             /* initialisation of POC */
7198             tcp->POC = 1;
7199             for (i = 0; i < parameters->numpocs; i++) {
7200                 if (tileno + 1 == parameters->POC[i].tile)  {
7201                     opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7202 
7203                     tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
7204                     tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
7205                     tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
7206                     tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
7207                     tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
7208                     tcp_poc->prg1           = parameters->POC[numpocs_tile].prg1;
7209                     tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
7210 
7211                     numpocs_tile++;
7212                 }
7213             }
7214 
7215             tcp->numpocs = numpocs_tile - 1 ;
7216         } else {
7217             tcp->numpocs = 0;
7218         }
7219 
7220         tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7221         if (!tcp->tccps) {
7222             opj_event_msg(p_manager, EVT_ERROR,
7223                           "Not enough memory to allocate tile component coding parameters\n");
7224             return OPJ_FALSE;
7225         }
7226         if (parameters->mct_data) {
7227 
7228             OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(
7229                                       OPJ_FLOAT32);
7230             OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7231             OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data +
7232                                                    lMctSize);
7233 
7234             if (!lTmpBuf) {
7235                 opj_event_msg(p_manager, EVT_ERROR,
7236                               "Not enough memory to allocate temp buffer\n");
7237                 return OPJ_FALSE;
7238             }
7239 
7240             tcp->mct = 2;
7241             tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7242             if (! tcp->m_mct_coding_matrix) {
7243                 opj_free(lTmpBuf);
7244                 lTmpBuf = NULL;
7245                 opj_event_msg(p_manager, EVT_ERROR,
7246                               "Not enough memory to allocate encoder MCT coding matrix \n");
7247                 return OPJ_FALSE;
7248             }
7249             memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize);
7250             memcpy(lTmpBuf, parameters->mct_data, lMctSize);
7251 
7252             tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7253             if (! tcp->m_mct_decoding_matrix) {
7254                 opj_free(lTmpBuf);
7255                 lTmpBuf = NULL;
7256                 opj_event_msg(p_manager, EVT_ERROR,
7257                               "Not enough memory to allocate encoder MCT decoding matrix \n");
7258                 return OPJ_FALSE;
7259             }
7260             if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix),
7261                                        image->numcomps) == OPJ_FALSE) {
7262                 opj_free(lTmpBuf);
7263                 lTmpBuf = NULL;
7264                 opj_event_msg(p_manager, EVT_ERROR,
7265                               "Failed to inverse encoder MCT decoding matrix \n");
7266                 return OPJ_FALSE;
7267             }
7268 
7269             tcp->mct_norms = (OPJ_FLOAT64*)
7270                              opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7271             if (! tcp->mct_norms) {
7272                 opj_free(lTmpBuf);
7273                 lTmpBuf = NULL;
7274                 opj_event_msg(p_manager, EVT_ERROR,
7275                               "Not enough memory to allocate encoder MCT norms \n");
7276                 return OPJ_FALSE;
7277             }
7278             opj_calculate_norms(tcp->mct_norms, image->numcomps,
7279                                 tcp->m_mct_decoding_matrix);
7280             opj_free(lTmpBuf);
7281 
7282             for (i = 0; i < image->numcomps; i++) {
7283                 opj_tccp_t *tccp = &tcp->tccps[i];
7284                 tccp->m_dc_level_shift = l_dc_shift[i];
7285             }
7286 
7287             if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) {
7288                 /* free will be handled by opj_j2k_destroy */
7289                 opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n");
7290                 return OPJ_FALSE;
7291             }
7292         } else {
7293             if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */
7294                 if ((image->comps[0].dx != image->comps[1].dx) ||
7295                         (image->comps[0].dx != image->comps[2].dx) ||
7296                         (image->comps[0].dy != image->comps[1].dy) ||
7297                         (image->comps[0].dy != image->comps[2].dy)) {
7298                     opj_event_msg(p_manager, EVT_WARNING,
7299                                   "Cannot perform MCT on components with different sizes. Disabling MCT.\n");
7300                     tcp->mct = 0;
7301                 }
7302             }
7303             for (i = 0; i < image->numcomps; i++) {
7304                 opj_tccp_t *tccp = &tcp->tccps[i];
7305                 opj_image_comp_t * l_comp = &(image->comps[i]);
7306 
7307                 if (! l_comp->sgnd) {
7308                     tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7309                 }
7310             }
7311         }
7312 
7313         for (i = 0; i < image->numcomps; i++) {
7314             opj_tccp_t *tccp = &tcp->tccps[i];
7315 
7316             tccp->csty = parameters->csty &
7317                          0x01;   /* 0 => one precinct || 1 => custom precinct  */
7318             tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
7319             tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
7320             tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
7321             tccp->cblksty = (OPJ_UINT32)parameters->mode;
7322             tccp->qmfbid = parameters->irreversible ? 0 : 1;
7323             tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT :
7324                            J2K_CCP_QNTSTY_NOQNT;
7325             tccp->numgbits = 2;
7326 
7327             if ((OPJ_INT32)i == parameters->roi_compno) {
7328                 tccp->roishift = parameters->roi_shift;
7329             } else {
7330                 tccp->roishift = 0;
7331             }
7332 
7333             if (parameters->csty & J2K_CCP_CSTY_PRT) {
7334                 OPJ_INT32 p = 0, it_res;
7335                 assert(tccp->numresolutions > 0);
7336                 for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
7337                     if (p < parameters->res_spec) {
7338 
7339                         if (parameters->prcw_init[p] < 1) {
7340                             tccp->prcw[it_res] = 1;
7341                         } else {
7342                             tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
7343                         }
7344 
7345                         if (parameters->prch_init[p] < 1) {
7346                             tccp->prch[it_res] = 1;
7347                         } else {
7348                             tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
7349                         }
7350 
7351                     } else {
7352                         OPJ_INT32 res_spec = parameters->res_spec;
7353                         OPJ_INT32 size_prcw = 0;
7354                         OPJ_INT32 size_prch = 0;
7355 
7356                         assert(res_spec > 0); /* issue 189 */
7357                         size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7358                         size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7359 
7360 
7361                         if (size_prcw < 1) {
7362                             tccp->prcw[it_res] = 1;
7363                         } else {
7364                             tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
7365                         }
7366 
7367                         if (size_prch < 1) {
7368                             tccp->prch[it_res] = 1;
7369                         } else {
7370                             tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
7371                         }
7372                     }
7373                     p++;
7374                     /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7375                 }       /*end for*/
7376             } else {
7377                 for (j = 0; j < tccp->numresolutions; j++) {
7378                     tccp->prcw[j] = 15;
7379                     tccp->prch[j] = 15;
7380                 }
7381             }
7382 
7383             opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7384         }
7385     }
7386 
7387     if (parameters->mct_data) {
7388         opj_free(parameters->mct_data);
7389         parameters->mct_data = 00;
7390     }
7391     return OPJ_TRUE;
7392 }
7393 
opj_j2k_add_mhmarker(opj_codestream_index_t * cstr_index,OPJ_UINT32 type,OPJ_OFF_T pos,OPJ_UINT32 len)7394 static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index,
7395                                      OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7396 {
7397     assert(cstr_index != 00);
7398 
7399     /* expand the list? */
7400     if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7401         opj_marker_info_t *new_marker;
7402         cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32)
7403                                               cstr_index->maxmarknum);
7404         new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker,
7405                      cstr_index->maxmarknum * sizeof(opj_marker_info_t));
7406         if (! new_marker) {
7407             opj_free(cstr_index->marker);
7408             cstr_index->marker = NULL;
7409             cstr_index->maxmarknum = 0;
7410             cstr_index->marknum = 0;
7411             /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
7412             return OPJ_FALSE;
7413         }
7414         cstr_index->marker = new_marker;
7415     }
7416 
7417     /* add the marker */
7418     cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7419     cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7420     cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7421     cstr_index->marknum++;
7422     return OPJ_TRUE;
7423 }
7424 
opj_j2k_add_tlmarker(OPJ_UINT32 tileno,opj_codestream_index_t * cstr_index,OPJ_UINT32 type,OPJ_OFF_T pos,OPJ_UINT32 len)7425 static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno,
7426                                      opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos,
7427                                      OPJ_UINT32 len)
7428 {
7429     assert(cstr_index != 00);
7430     assert(cstr_index->tile_index != 00);
7431 
7432     /* expand the list? */
7433     if ((cstr_index->tile_index[tileno].marknum + 1) >
7434             cstr_index->tile_index[tileno].maxmarknum) {
7435         opj_marker_info_t *new_marker;
7436         cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 +
7437                 (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
7438         new_marker = (opj_marker_info_t *) opj_realloc(
7439                          cstr_index->tile_index[tileno].marker,
7440                          cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t));
7441         if (! new_marker) {
7442             opj_free(cstr_index->tile_index[tileno].marker);
7443             cstr_index->tile_index[tileno].marker = NULL;
7444             cstr_index->tile_index[tileno].maxmarknum = 0;
7445             cstr_index->tile_index[tileno].marknum = 0;
7446             /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
7447             return OPJ_FALSE;
7448         }
7449         cstr_index->tile_index[tileno].marker = new_marker;
7450     }
7451 
7452     /* add the marker */
7453     cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type
7454         = (OPJ_UINT16)type;
7455     cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos
7456         = (OPJ_INT32)pos;
7457     cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len
7458         = (OPJ_INT32)len;
7459     cstr_index->tile_index[tileno].marknum++;
7460 
7461     if (type == J2K_MS_SOT) {
7462         OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7463 
7464         if (cstr_index->tile_index[tileno].tp_index) {
7465             cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7466         }
7467 
7468     }
7469     return OPJ_TRUE;
7470 }
7471 
7472 /*
7473  * -----------------------------------------------------------------------
7474  * -----------------------------------------------------------------------
7475  * -----------------------------------------------------------------------
7476  */
7477 
opj_j2k_end_decompress(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7478 OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
7479                                 opj_stream_private_t *p_stream,
7480                                 opj_event_mgr_t * p_manager
7481                                )
7482 {
7483     (void)p_j2k;
7484     (void)p_stream;
7485     (void)p_manager;
7486     return OPJ_TRUE;
7487 }
7488 
opj_j2k_read_header(opj_stream_private_t * p_stream,opj_j2k_t * p_j2k,opj_image_t ** p_image,opj_event_mgr_t * p_manager)7489 OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream,
7490                              opj_j2k_t* p_j2k,
7491                              opj_image_t** p_image,
7492                              opj_event_mgr_t* p_manager)
7493 {
7494     /* preconditions */
7495     assert(p_j2k != 00);
7496     assert(p_stream != 00);
7497     assert(p_manager != 00);
7498 
7499     /* create an empty image header */
7500     p_j2k->m_private_image = opj_image_create0();
7501     if (! p_j2k->m_private_image) {
7502         return OPJ_FALSE;
7503     }
7504 
7505     /* customization of the validation */
7506     if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) {
7507         opj_image_destroy(p_j2k->m_private_image);
7508         p_j2k->m_private_image = NULL;
7509         return OPJ_FALSE;
7510     }
7511 
7512     /* validation of the parameters codec */
7513     if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) {
7514         opj_image_destroy(p_j2k->m_private_image);
7515         p_j2k->m_private_image = NULL;
7516         return OPJ_FALSE;
7517     }
7518 
7519     /* customization of the encoding */
7520     if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) {
7521         opj_image_destroy(p_j2k->m_private_image);
7522         p_j2k->m_private_image = NULL;
7523         return OPJ_FALSE;
7524     }
7525 
7526     /* read header */
7527     if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
7528         opj_image_destroy(p_j2k->m_private_image);
7529         p_j2k->m_private_image = NULL;
7530         return OPJ_FALSE;
7531     }
7532 
7533     *p_image = opj_image_create0();
7534     if (!(*p_image)) {
7535         return OPJ_FALSE;
7536     }
7537 
7538     /* Copy codestream image information to the output image */
7539     opj_copy_image_header(p_j2k->m_private_image, *p_image);
7540 
7541     /*Allocate and initialize some elements of codestrem index*/
7542     if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
7543         return OPJ_FALSE;
7544     }
7545 
7546     return OPJ_TRUE;
7547 }
7548 
opj_j2k_setup_header_reading(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)7549 static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k,
7550         opj_event_mgr_t * p_manager)
7551 {
7552     /* preconditions*/
7553     assert(p_j2k != 00);
7554     assert(p_manager != 00);
7555 
7556     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
7557                                            (opj_procedure)opj_j2k_read_header_procedure, p_manager)) {
7558         return OPJ_FALSE;
7559     }
7560 
7561     /* DEVELOPER CORNER, add your custom procedures */
7562     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
7563                                            (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager))  {
7564         return OPJ_FALSE;
7565     }
7566 
7567     return OPJ_TRUE;
7568 }
7569 
opj_j2k_setup_decoding_validation(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)7570 static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k,
7571         opj_event_mgr_t * p_manager)
7572 {
7573     /* preconditions*/
7574     assert(p_j2k != 00);
7575     assert(p_manager != 00);
7576 
7577     if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
7578                                            (opj_procedure)opj_j2k_build_decoder, p_manager)) {
7579         return OPJ_FALSE;
7580     }
7581     if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
7582                                            (opj_procedure)opj_j2k_decoding_validation, p_manager)) {
7583         return OPJ_FALSE;
7584     }
7585 
7586     /* DEVELOPER CORNER, add your custom validation procedure */
7587     return OPJ_TRUE;
7588 }
7589 
opj_j2k_mct_validation(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7590 static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k,
7591                                        opj_stream_private_t *p_stream,
7592                                        opj_event_mgr_t * p_manager)
7593 {
7594     OPJ_BOOL l_is_valid = OPJ_TRUE;
7595     OPJ_UINT32 i, j;
7596 
7597     /* preconditions */
7598     assert(p_j2k != 00);
7599     assert(p_stream != 00);
7600     assert(p_manager != 00);
7601 
7602     OPJ_UNUSED(p_stream);
7603     OPJ_UNUSED(p_manager);
7604 
7605     if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7606         OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7607         opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
7608 
7609         for (i = 0; i < l_nb_tiles; ++i) {
7610             if (l_tcp->mct == 2) {
7611                 opj_tccp_t * l_tccp = l_tcp->tccps;
7612                 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7613 
7614                 for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) {
7615                     l_is_valid &= !(l_tccp->qmfbid & 1);
7616                     ++l_tccp;
7617                 }
7618             }
7619             ++l_tcp;
7620         }
7621     }
7622 
7623     return l_is_valid;
7624 }
7625 
opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp,opj_image_t * p_image)7626 OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
7627 {
7628     OPJ_UINT32 i;
7629     OPJ_UINT32 l_indix = 1;
7630     opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00;
7631     opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7632     OPJ_UINT32 l_mct_size, l_nb_elem;
7633     OPJ_FLOAT32 * l_data, * l_current_data;
7634     opj_tccp_t * l_tccp;
7635 
7636     /* preconditions */
7637     assert(p_tcp != 00);
7638 
7639     if (p_tcp->mct != 2) {
7640         return OPJ_TRUE;
7641     }
7642 
7643     if (p_tcp->m_mct_decoding_matrix) {
7644         if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7645             opj_mct_data_t *new_mct_records;
7646             p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
7647 
7648             new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records,
7649                               p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
7650             if (! new_mct_records) {
7651                 opj_free(p_tcp->m_mct_records);
7652                 p_tcp->m_mct_records = NULL;
7653                 p_tcp->m_nb_max_mct_records = 0;
7654                 p_tcp->m_nb_mct_records = 0;
7655                 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
7656                 return OPJ_FALSE;
7657             }
7658             p_tcp->m_mct_records = new_mct_records;
7659             l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7660 
7661             memset(l_mct_deco_data, 0,
7662                    (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(
7663                        opj_mct_data_t));
7664         }
7665         l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7666 
7667         if (l_mct_deco_data->m_data) {
7668             opj_free(l_mct_deco_data->m_data);
7669             l_mct_deco_data->m_data = 00;
7670         }
7671 
7672         l_mct_deco_data->m_index = l_indix++;
7673         l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7674         l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7675         l_nb_elem = p_image->numcomps * p_image->numcomps;
7676         l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7677         l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size);
7678 
7679         if (! l_mct_deco_data->m_data) {
7680             return OPJ_FALSE;
7681         }
7682 
7683         j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](
7684             p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem);
7685 
7686         l_mct_deco_data->m_data_size = l_mct_size;
7687         ++p_tcp->m_nb_mct_records;
7688     }
7689 
7690     if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7691         opj_mct_data_t *new_mct_records;
7692         p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
7693         new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records,
7694                           p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
7695         if (! new_mct_records) {
7696             opj_free(p_tcp->m_mct_records);
7697             p_tcp->m_mct_records = NULL;
7698             p_tcp->m_nb_max_mct_records = 0;
7699             p_tcp->m_nb_mct_records = 0;
7700             /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
7701             return OPJ_FALSE;
7702         }
7703         p_tcp->m_mct_records = new_mct_records;
7704         l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7705 
7706         memset(l_mct_offset_data, 0,
7707                (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(
7708                    opj_mct_data_t));
7709 
7710         if (l_mct_deco_data) {
7711             l_mct_deco_data = l_mct_offset_data - 1;
7712         }
7713     }
7714 
7715     l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7716 
7717     if (l_mct_offset_data->m_data) {
7718         opj_free(l_mct_offset_data->m_data);
7719         l_mct_offset_data->m_data = 00;
7720     }
7721 
7722     l_mct_offset_data->m_index = l_indix++;
7723     l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7724     l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7725     l_nb_elem = p_image->numcomps;
7726     l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7727     l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size);
7728 
7729     if (! l_mct_offset_data->m_data) {
7730         return OPJ_FALSE;
7731     }
7732 
7733     l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7734     if (! l_data) {
7735         opj_free(l_mct_offset_data->m_data);
7736         l_mct_offset_data->m_data = 00;
7737         return OPJ_FALSE;
7738     }
7739 
7740     l_tccp = p_tcp->tccps;
7741     l_current_data = l_data;
7742 
7743     for (i = 0; i < l_nb_elem; ++i) {
7744         *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift);
7745         ++l_tccp;
7746     }
7747 
7748     j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,
7749             l_mct_offset_data->m_data, l_nb_elem);
7750 
7751     opj_free(l_data);
7752 
7753     l_mct_offset_data->m_data_size = l_mct_size;
7754 
7755     ++p_tcp->m_nb_mct_records;
7756 
7757     if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7758         opj_simple_mcc_decorrelation_data_t *new_mcc_records;
7759         p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
7760         new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
7761                               p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(
7762                                   opj_simple_mcc_decorrelation_data_t));
7763         if (! new_mcc_records) {
7764             opj_free(p_tcp->m_mcc_records);
7765             p_tcp->m_mcc_records = NULL;
7766             p_tcp->m_nb_max_mcc_records = 0;
7767             p_tcp->m_nb_mcc_records = 0;
7768             /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
7769             return OPJ_FALSE;
7770         }
7771         p_tcp->m_mcc_records = new_mcc_records;
7772         l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7773         memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) *
7774                sizeof(opj_simple_mcc_decorrelation_data_t));
7775 
7776     }
7777 
7778     l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7779     l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7780     l_mcc_data->m_is_irreversible = 1;
7781     l_mcc_data->m_nb_comps = p_image->numcomps;
7782     l_mcc_data->m_index = l_indix++;
7783     l_mcc_data->m_offset_array = l_mct_offset_data;
7784     ++p_tcp->m_nb_mcc_records;
7785 
7786     return OPJ_TRUE;
7787 }
7788 
opj_j2k_build_decoder(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7789 static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k,
7790                                       opj_stream_private_t *p_stream,
7791                                       opj_event_mgr_t * p_manager)
7792 {
7793     /* add here initialization of cp
7794        copy paste of setup_decoder */
7795     (void)p_j2k;
7796     (void)p_stream;
7797     (void)p_manager;
7798     return OPJ_TRUE;
7799 }
7800 
opj_j2k_build_encoder(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7801 static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k,
7802                                       opj_stream_private_t *p_stream,
7803                                       opj_event_mgr_t * p_manager)
7804 {
7805     /* add here initialization of cp
7806        copy paste of setup_encoder */
7807     (void)p_j2k;
7808     (void)p_stream;
7809     (void)p_manager;
7810     return OPJ_TRUE;
7811 }
7812 
opj_j2k_encoding_validation(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7813 static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k,
7814         opj_stream_private_t *p_stream,
7815         opj_event_mgr_t * p_manager)
7816 {
7817     OPJ_BOOL l_is_valid = OPJ_TRUE;
7818 
7819     /* preconditions */
7820     assert(p_j2k != 00);
7821     assert(p_stream != 00);
7822     assert(p_manager != 00);
7823 
7824     OPJ_UNUSED(p_stream);
7825 
7826     /* STATE checking */
7827     /* make sure the state is at 0 */
7828     l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7829 
7830     /* POINTER validation */
7831     /* make sure a p_j2k codec is present */
7832     l_is_valid &= (p_j2k->m_procedure_list != 00);
7833     /* make sure a validation list is present */
7834     l_is_valid &= (p_j2k->m_validation_list != 00);
7835 
7836     /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */
7837     /* 33 (32) would always fail the check below (if a cast to 64bits was done) */
7838     /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */
7839     if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) ||
7840             (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) {
7841         opj_event_msg(p_manager, EVT_ERROR,
7842                       "Number of resolutions is too high in comparison to the size of tiles\n");
7843         return OPJ_FALSE;
7844     }
7845 
7846     if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 <<
7847                                          (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
7848         opj_event_msg(p_manager, EVT_ERROR,
7849                       "Number of resolutions is too high in comparison to the size of tiles\n");
7850         return OPJ_FALSE;
7851     }
7852 
7853     if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 <<
7854                                          (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
7855         opj_event_msg(p_manager, EVT_ERROR,
7856                       "Number of resolutions is too high in comparison to the size of tiles\n");
7857         return OPJ_FALSE;
7858     }
7859 
7860     /* PARAMETER VALIDATION */
7861     return l_is_valid;
7862 }
7863 
opj_j2k_decoding_validation(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7864 static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k,
7865         opj_stream_private_t *p_stream,
7866         opj_event_mgr_t * p_manager
7867                                            )
7868 {
7869     OPJ_BOOL l_is_valid = OPJ_TRUE;
7870 
7871     /* preconditions*/
7872     assert(p_j2k != 00);
7873     assert(p_stream != 00);
7874     assert(p_manager != 00);
7875 
7876     OPJ_UNUSED(p_stream);
7877     OPJ_UNUSED(p_manager);
7878 
7879     /* STATE checking */
7880     /* make sure the state is at 0 */
7881 #ifdef TODO_MSD
7882     l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7883 #endif
7884     l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7885 
7886     /* POINTER validation */
7887     /* make sure a p_j2k codec is present */
7888     /* make sure a procedure list is present */
7889     l_is_valid &= (p_j2k->m_procedure_list != 00);
7890     /* make sure a validation list is present */
7891     l_is_valid &= (p_j2k->m_validation_list != 00);
7892 
7893     /* PARAMETER VALIDATION */
7894     return l_is_valid;
7895 }
7896 
opj_j2k_read_header_procedure(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)7897 static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k,
7898         opj_stream_private_t *p_stream,
7899         opj_event_mgr_t * p_manager)
7900 {
7901     OPJ_UINT32 l_current_marker;
7902     OPJ_UINT32 l_marker_size;
7903     const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7904     OPJ_BOOL l_has_siz = 0;
7905     OPJ_BOOL l_has_cod = 0;
7906     OPJ_BOOL l_has_qcd = 0;
7907 
7908     /* preconditions */
7909     assert(p_stream != 00);
7910     assert(p_j2k != 00);
7911     assert(p_manager != 00);
7912 
7913     /*  We enter in the main header */
7914     p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7915 
7916     /* Try to read the SOC marker, the codestream must begin with SOC marker */
7917     if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) {
7918         opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7919         return OPJ_FALSE;
7920     }
7921 
7922     /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7923     if (opj_stream_read_data(p_stream,
7924                              p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
7925         opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7926         return OPJ_FALSE;
7927     }
7928 
7929     /* Read 2 bytes as the new marker ID */
7930     opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
7931                    &l_current_marker, 2);
7932 
7933     /* Try to read until the SOT is detected */
7934     while (l_current_marker != J2K_MS_SOT) {
7935 
7936         /* Check if the current marker ID is valid */
7937         if (l_current_marker < 0xff00) {
7938             opj_event_msg(p_manager, EVT_ERROR,
7939                           "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker);
7940             return OPJ_FALSE;
7941         }
7942 
7943         /* Get the marker handler from the marker ID */
7944         l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7945 
7946         /* Manage case where marker is unknown */
7947         if (l_marker_handler->id == J2K_MS_UNK) {
7948             if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) {
7949                 opj_event_msg(p_manager, EVT_ERROR,
7950                               "Unknow marker have been detected and generated error.\n");
7951                 return OPJ_FALSE;
7952             }
7953 
7954             if (l_current_marker == J2K_MS_SOT) {
7955                 break;    /* SOT marker is detected main header is completely read */
7956             } else { /* Get the marker handler from the marker ID */
7957                 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7958             }
7959         }
7960 
7961         if (l_marker_handler->id == J2K_MS_SIZ) {
7962             /* Mark required SIZ marker as found */
7963             l_has_siz = 1;
7964         }
7965         if (l_marker_handler->id == J2K_MS_COD) {
7966             /* Mark required COD marker as found */
7967             l_has_cod = 1;
7968         }
7969         if (l_marker_handler->id == J2K_MS_QCD) {
7970             /* Mark required QCD marker as found */
7971             l_has_qcd = 1;
7972         }
7973 
7974         /* Check if the marker is known and if it is the right place to find it */
7975         if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
7976             opj_event_msg(p_manager, EVT_ERROR,
7977                           "Marker is not compliant with its position\n");
7978             return OPJ_FALSE;
7979         }
7980 
7981         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7982         if (opj_stream_read_data(p_stream,
7983                                  p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
7984             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
7985             return OPJ_FALSE;
7986         }
7987 
7988         /* read 2 bytes as the marker size */
7989         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size,
7990                        2);
7991         if (l_marker_size < 2) {
7992             opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n");
7993             return OPJ_FALSE;
7994         }
7995         l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7996 
7997         /* Check if the marker size is compatible with the header data size */
7998         if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7999             OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(
8000                                             p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
8001             if (! new_header_data) {
8002                 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8003                 p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
8004                 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8005                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
8006                 return OPJ_FALSE;
8007             }
8008             p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
8009             p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8010         }
8011 
8012         /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8013         if (opj_stream_read_data(p_stream,
8014                                  p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size,
8015                                  p_manager) != l_marker_size) {
8016             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8017             return OPJ_FALSE;
8018         }
8019 
8020         /* Read the marker segment with the correct marker handler */
8021         if (!(*(l_marker_handler->handler))(p_j2k,
8022                                             p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) {
8023             opj_event_msg(p_manager, EVT_ERROR,
8024                           "Marker handler function failed to read the marker segment\n");
8025             return OPJ_FALSE;
8026         }
8027 
8028         /* Add the marker to the codestream index*/
8029         if (OPJ_FALSE == opj_j2k_add_mhmarker(
8030                     p_j2k->cstr_index,
8031                     l_marker_handler->id,
8032                     (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8033                     l_marker_size + 4)) {
8034             opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
8035             return OPJ_FALSE;
8036         }
8037 
8038         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8039         if (opj_stream_read_data(p_stream,
8040                                  p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
8041             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8042             return OPJ_FALSE;
8043         }
8044 
8045         /* read 2 bytes as the new marker ID */
8046         opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
8047                        &l_current_marker, 2);
8048     }
8049 
8050     if (l_has_siz == 0) {
8051         opj_event_msg(p_manager, EVT_ERROR,
8052                       "required SIZ marker not found in main header\n");
8053         return OPJ_FALSE;
8054     }
8055     if (l_has_cod == 0) {
8056         opj_event_msg(p_manager, EVT_ERROR,
8057                       "required COD marker not found in main header\n");
8058         return OPJ_FALSE;
8059     }
8060     if (l_has_qcd == 0) {
8061         opj_event_msg(p_manager, EVT_ERROR,
8062                       "required QCD marker not found in main header\n");
8063         return OPJ_FALSE;
8064     }
8065 
8066     if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) {
8067         opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n");
8068         return OPJ_FALSE;
8069     }
8070 
8071     opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8072 
8073     /* Position of the last element if the main header */
8074     p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8075 
8076     /* Next step: read a tile-part header */
8077     p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8078 
8079     return OPJ_TRUE;
8080 }
8081 
opj_j2k_exec(opj_j2k_t * p_j2k,opj_procedure_list_t * p_procedure_list,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)8082 static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k,
8083                              opj_procedure_list_t * p_procedure_list,
8084                              opj_stream_private_t *p_stream,
8085                              opj_event_mgr_t * p_manager)
8086 {
8087     OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *,
8088                              opj_event_mgr_t *) = 00;
8089     OPJ_BOOL l_result = OPJ_TRUE;
8090     OPJ_UINT32 l_nb_proc, i;
8091 
8092     /* preconditions*/
8093     assert(p_procedure_list != 00);
8094     assert(p_j2k != 00);
8095     assert(p_stream != 00);
8096     assert(p_manager != 00);
8097 
8098     l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8099     l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *,
8100                                 opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
8101 
8102     for (i = 0; i < l_nb_proc; ++i) {
8103         l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager));
8104         ++l_procedure;
8105     }
8106 
8107     /* and clear the procedure list at the end.*/
8108     opj_procedure_list_clear(p_procedure_list);
8109     return l_result;
8110 }
8111 
8112 /* FIXME DOC*/
opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)8113 static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,
8114         opj_stream_private_t *p_stream,
8115         opj_event_mgr_t * p_manager
8116                                                        )
8117 {
8118     opj_tcp_t * l_tcp = 00;
8119     opj_tcp_t * l_default_tcp = 00;
8120     OPJ_UINT32 l_nb_tiles;
8121     OPJ_UINT32 i, j;
8122     opj_tccp_t *l_current_tccp = 00;
8123     OPJ_UINT32 l_tccp_size;
8124     OPJ_UINT32 l_mct_size;
8125     opj_image_t * l_image;
8126     OPJ_UINT32 l_mcc_records_size, l_mct_records_size;
8127     opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8128     opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8129     OPJ_UINT32 l_offset;
8130 
8131     /* preconditions */
8132     assert(p_j2k != 00);
8133     assert(p_stream != 00);
8134     assert(p_manager != 00);
8135 
8136     OPJ_UNUSED(p_stream);
8137 
8138     l_image = p_j2k->m_private_image;
8139     l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8140     l_tcp = p_j2k->m_cp.tcps;
8141     l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);
8142     l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8143     l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(
8144                      OPJ_FLOAT32);
8145 
8146     /* For each tile */
8147     for (i = 0; i < l_nb_tiles; ++i) {
8148         /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8149         l_current_tccp = l_tcp->tccps;
8150         /*Copy default coding parameters into the current tile coding parameters*/
8151         memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));
8152         /* Initialize some values of the current tile coding parameters*/
8153         l_tcp->cod = 0;
8154         l_tcp->ppt = 0;
8155         l_tcp->ppt_data = 00;
8156         l_tcp->m_current_tile_part_number = -1;
8157         /* Remove memory not owned by this tile in case of early error return. */
8158         l_tcp->m_mct_decoding_matrix = 00;
8159         l_tcp->m_nb_max_mct_records = 0;
8160         l_tcp->m_mct_records = 00;
8161         l_tcp->m_nb_max_mcc_records = 0;
8162         l_tcp->m_mcc_records = 00;
8163         /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8164         l_tcp->tccps = l_current_tccp;
8165 
8166         /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8167         if (l_default_tcp->m_mct_decoding_matrix) {
8168             l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8169             if (! l_tcp->m_mct_decoding_matrix) {
8170                 return OPJ_FALSE;
8171             }
8172             memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix,
8173                    l_mct_size);
8174         }
8175 
8176         /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8177         l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(
8178                                  opj_mct_data_t);
8179         l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8180         if (! l_tcp->m_mct_records) {
8181             return OPJ_FALSE;
8182         }
8183         memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size);
8184 
8185         /* Copy the mct record data from dflt_tile_cp to the current tile*/
8186         l_src_mct_rec = l_default_tcp->m_mct_records;
8187         l_dest_mct_rec = l_tcp->m_mct_records;
8188 
8189         for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) {
8190 
8191             if (l_src_mct_rec->m_data) {
8192 
8193                 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8194                 if (! l_dest_mct_rec->m_data) {
8195                     return OPJ_FALSE;
8196                 }
8197                 memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data,
8198                        l_src_mct_rec->m_data_size);
8199             }
8200 
8201             ++l_src_mct_rec;
8202             ++l_dest_mct_rec;
8203             /* Update with each pass to free exactly what has been allocated on early return. */
8204             l_tcp->m_nb_max_mct_records += 1;
8205         }
8206 
8207         /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8208         l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(
8209                                  opj_simple_mcc_decorrelation_data_t);
8210         l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(
8211                                    l_mcc_records_size);
8212         if (! l_tcp->m_mcc_records) {
8213             return OPJ_FALSE;
8214         }
8215         memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size);
8216         l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records;
8217 
8218         /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8219         l_src_mcc_rec = l_default_tcp->m_mcc_records;
8220         l_dest_mcc_rec = l_tcp->m_mcc_records;
8221 
8222         for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) {
8223 
8224             if (l_src_mcc_rec->m_decorrelation_array) {
8225                 l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array -
8226                                         l_default_tcp->m_mct_records);
8227                 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8228             }
8229 
8230             if (l_src_mcc_rec->m_offset_array) {
8231                 l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array -
8232                                         l_default_tcp->m_mct_records);
8233                 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8234             }
8235 
8236             ++l_src_mcc_rec;
8237             ++l_dest_mcc_rec;
8238         }
8239 
8240         /* Copy all the dflt_tile_compo_cp to the current tile cp */
8241         memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size);
8242 
8243         /* Move to next tile cp*/
8244         ++l_tcp;
8245     }
8246 
8247     /* Create the current tile decoder*/
8248     p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE);
8249     if (! p_j2k->m_tcd) {
8250         return OPJ_FALSE;
8251     }
8252 
8253     if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) {
8254         opj_tcd_destroy(p_j2k->m_tcd);
8255         p_j2k->m_tcd = 00;
8256         opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8257         return OPJ_FALSE;
8258     }
8259 
8260     return OPJ_TRUE;
8261 }
8262 
opj_j2k_get_marker_handler(OPJ_UINT32 p_id)8263 static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler(
8264     OPJ_UINT32 p_id)
8265 {
8266     const opj_dec_memory_marker_handler_t *e;
8267     for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8268         if (e->id == p_id) {
8269             break; /* we find a handler corresponding to the marker ID*/
8270         }
8271     }
8272     return e;
8273 }
8274 
opj_j2k_destroy(opj_j2k_t * p_j2k)8275 void opj_j2k_destroy(opj_j2k_t *p_j2k)
8276 {
8277     if (p_j2k == 00) {
8278         return;
8279     }
8280 
8281     if (p_j2k->m_is_decoder) {
8282 
8283         if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8284             opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8285             opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8286             p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8287         }
8288 
8289         if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8290             opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8291             p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8292             p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8293         }
8294 
8295         opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode);
8296         p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00;
8297         p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0;
8298 
8299     } else {
8300 
8301         if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8302             opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8303             p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8304         }
8305 
8306         if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8307             opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8308             p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8309             p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8310         }
8311 
8312         if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8313             opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8314             p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8315             p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8316         }
8317     }
8318 
8319     opj_tcd_destroy(p_j2k->m_tcd);
8320 
8321     opj_j2k_cp_destroy(&(p_j2k->m_cp));
8322     memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t));
8323 
8324     opj_procedure_list_destroy(p_j2k->m_procedure_list);
8325     p_j2k->m_procedure_list = 00;
8326 
8327     opj_procedure_list_destroy(p_j2k->m_validation_list);
8328     p_j2k->m_procedure_list = 00;
8329 
8330     j2k_destroy_cstr_index(p_j2k->cstr_index);
8331     p_j2k->cstr_index = NULL;
8332 
8333     opj_image_destroy(p_j2k->m_private_image);
8334     p_j2k->m_private_image = NULL;
8335 
8336     opj_image_destroy(p_j2k->m_output_image);
8337     p_j2k->m_output_image = NULL;
8338 
8339     opj_thread_pool_destroy(p_j2k->m_tp);
8340     p_j2k->m_tp = NULL;
8341 
8342     opj_free(p_j2k);
8343 }
8344 
j2k_destroy_cstr_index(opj_codestream_index_t * p_cstr_ind)8345 void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind)
8346 {
8347     if (p_cstr_ind) {
8348 
8349         if (p_cstr_ind->marker) {
8350             opj_free(p_cstr_ind->marker);
8351             p_cstr_ind->marker = NULL;
8352         }
8353 
8354         if (p_cstr_ind->tile_index) {
8355             OPJ_UINT32 it_tile = 0;
8356 
8357             for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8358 
8359                 if (p_cstr_ind->tile_index[it_tile].packet_index) {
8360                     opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8361                     p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8362                 }
8363 
8364                 if (p_cstr_ind->tile_index[it_tile].tp_index) {
8365                     opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8366                     p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8367                 }
8368 
8369                 if (p_cstr_ind->tile_index[it_tile].marker) {
8370                     opj_free(p_cstr_ind->tile_index[it_tile].marker);
8371                     p_cstr_ind->tile_index[it_tile].marker = NULL;
8372 
8373                 }
8374             }
8375 
8376             opj_free(p_cstr_ind->tile_index);
8377             p_cstr_ind->tile_index = NULL;
8378         }
8379 
8380         opj_free(p_cstr_ind);
8381     }
8382 }
8383 
opj_j2k_tcp_destroy(opj_tcp_t * p_tcp)8384 static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp)
8385 {
8386     if (p_tcp == 00) {
8387         return;
8388     }
8389 
8390     if (p_tcp->ppt_markers != 00) {
8391         OPJ_UINT32 i;
8392         for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
8393             if (p_tcp->ppt_markers[i].m_data != NULL) {
8394                 opj_free(p_tcp->ppt_markers[i].m_data);
8395             }
8396         }
8397         p_tcp->ppt_markers_count = 0U;
8398         opj_free(p_tcp->ppt_markers);
8399         p_tcp->ppt_markers = NULL;
8400     }
8401 
8402     if (p_tcp->ppt_buffer != 00) {
8403         opj_free(p_tcp->ppt_buffer);
8404         p_tcp->ppt_buffer = 00;
8405     }
8406 
8407     if (p_tcp->tccps != 00) {
8408         opj_free(p_tcp->tccps);
8409         p_tcp->tccps = 00;
8410     }
8411 
8412     if (p_tcp->m_mct_coding_matrix != 00) {
8413         opj_free(p_tcp->m_mct_coding_matrix);
8414         p_tcp->m_mct_coding_matrix = 00;
8415     }
8416 
8417     if (p_tcp->m_mct_decoding_matrix != 00) {
8418         opj_free(p_tcp->m_mct_decoding_matrix);
8419         p_tcp->m_mct_decoding_matrix = 00;
8420     }
8421 
8422     if (p_tcp->m_mcc_records) {
8423         opj_free(p_tcp->m_mcc_records);
8424         p_tcp->m_mcc_records = 00;
8425         p_tcp->m_nb_max_mcc_records = 0;
8426         p_tcp->m_nb_mcc_records = 0;
8427     }
8428 
8429     if (p_tcp->m_mct_records) {
8430         opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8431         OPJ_UINT32 i;
8432 
8433         for (i = 0; i < p_tcp->m_nb_mct_records; ++i) {
8434             if (l_mct_data->m_data) {
8435                 opj_free(l_mct_data->m_data);
8436                 l_mct_data->m_data = 00;
8437             }
8438 
8439             ++l_mct_data;
8440         }
8441 
8442         opj_free(p_tcp->m_mct_records);
8443         p_tcp->m_mct_records = 00;
8444     }
8445 
8446     if (p_tcp->mct_norms != 00) {
8447         opj_free(p_tcp->mct_norms);
8448         p_tcp->mct_norms = 00;
8449     }
8450 
8451     opj_j2k_tcp_data_destroy(p_tcp);
8452 
8453 }
8454 
opj_j2k_tcp_data_destroy(opj_tcp_t * p_tcp)8455 static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp)
8456 {
8457     if (p_tcp->m_data) {
8458         opj_free(p_tcp->m_data);
8459         p_tcp->m_data = NULL;
8460         p_tcp->m_data_size = 0;
8461     }
8462 }
8463 
opj_j2k_cp_destroy(opj_cp_t * p_cp)8464 static void opj_j2k_cp_destroy(opj_cp_t *p_cp)
8465 {
8466     OPJ_UINT32 l_nb_tiles;
8467     opj_tcp_t * l_current_tile = 00;
8468 
8469     if (p_cp == 00) {
8470         return;
8471     }
8472     if (p_cp->tcps != 00) {
8473         OPJ_UINT32 i;
8474         l_current_tile = p_cp->tcps;
8475         l_nb_tiles = p_cp->th * p_cp->tw;
8476 
8477         for (i = 0U; i < l_nb_tiles; ++i) {
8478             opj_j2k_tcp_destroy(l_current_tile);
8479             ++l_current_tile;
8480         }
8481         opj_free(p_cp->tcps);
8482         p_cp->tcps = 00;
8483     }
8484     if (p_cp->ppm_markers != 00) {
8485         OPJ_UINT32 i;
8486         for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
8487             if (p_cp->ppm_markers[i].m_data != NULL) {
8488                 opj_free(p_cp->ppm_markers[i].m_data);
8489             }
8490         }
8491         p_cp->ppm_markers_count = 0U;
8492         opj_free(p_cp->ppm_markers);
8493         p_cp->ppm_markers = NULL;
8494     }
8495     opj_free(p_cp->ppm_buffer);
8496     p_cp->ppm_buffer = 00;
8497     p_cp->ppm_data =
8498         NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
8499     opj_free(p_cp->comment);
8500     p_cp->comment = 00;
8501     if (! p_cp->m_is_decoder) {
8502         opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8503         p_cp->m_specific_param.m_enc.m_matrice = 00;
8504     }
8505 }
8506 
opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t * p_stream,OPJ_UINT32 tile_no,OPJ_BOOL * p_correction_needed,opj_event_mgr_t * p_manager)8507 static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t
8508         *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed,
8509         opj_event_mgr_t * p_manager)
8510 {
8511     OPJ_BYTE   l_header_data[10];
8512     OPJ_OFF_T  l_stream_pos_backup;
8513     OPJ_UINT32 l_current_marker;
8514     OPJ_UINT32 l_marker_size;
8515     OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts;
8516 
8517     /* initialize to no correction needed */
8518     *p_correction_needed = OPJ_FALSE;
8519 
8520     if (!opj_stream_has_seek(p_stream)) {
8521         /* We can't do much in this case, seek is needed */
8522         return OPJ_TRUE;
8523     }
8524 
8525     l_stream_pos_backup = opj_stream_tell(p_stream);
8526     if (l_stream_pos_backup == -1) {
8527         /* let's do nothing */
8528         return OPJ_TRUE;
8529     }
8530 
8531     for (;;) {
8532         /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8533         if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
8534             /* assume all is OK */
8535             if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
8536                 return OPJ_FALSE;
8537             }
8538             return OPJ_TRUE;
8539         }
8540 
8541         /* Read 2 bytes from buffer as the new marker ID */
8542         opj_read_bytes(l_header_data, &l_current_marker, 2);
8543 
8544         if (l_current_marker != J2K_MS_SOT) {
8545             /* assume all is OK */
8546             if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
8547                 return OPJ_FALSE;
8548             }
8549             return OPJ_TRUE;
8550         }
8551 
8552         /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8553         if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
8554             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8555             return OPJ_FALSE;
8556         }
8557 
8558         /* Read 2 bytes from the buffer as the marker size */
8559         opj_read_bytes(l_header_data, &l_marker_size, 2);
8560 
8561         /* Check marker size for SOT Marker */
8562         if (l_marker_size != 10) {
8563             opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
8564             return OPJ_FALSE;
8565         }
8566         l_marker_size -= 2;
8567 
8568         if (opj_stream_read_data(p_stream, l_header_data, l_marker_size,
8569                                  p_manager) != l_marker_size) {
8570             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8571             return OPJ_FALSE;
8572         }
8573 
8574         if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no,
8575                                      &l_tot_len, &l_current_part, &l_num_parts, p_manager)) {
8576             return OPJ_FALSE;
8577         }
8578 
8579         if (l_tile_no == tile_no) {
8580             /* we found what we were looking for */
8581             break;
8582         }
8583 
8584         if (l_tot_len < 14U) {
8585             /* last SOT until EOC or invalid Psot value */
8586             /* assume all is OK */
8587             if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
8588                 return OPJ_FALSE;
8589             }
8590             return OPJ_TRUE;
8591         }
8592         l_tot_len -= 12U;
8593         /* look for next SOT marker */
8594         if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len),
8595                             p_manager) != (OPJ_OFF_T)(l_tot_len)) {
8596             /* assume all is OK */
8597             if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
8598                 return OPJ_FALSE;
8599             }
8600             return OPJ_TRUE;
8601         }
8602     }
8603 
8604     /* check for correction */
8605     if (l_current_part == l_num_parts) {
8606         *p_correction_needed = OPJ_TRUE;
8607     }
8608 
8609     if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
8610         return OPJ_FALSE;
8611     }
8612     return OPJ_TRUE;
8613 }
8614 
opj_j2k_read_tile_header(opj_j2k_t * p_j2k,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)8615 OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
8616                                   OPJ_UINT32 * p_tile_index,
8617                                   OPJ_UINT32 * p_data_size,
8618                                   OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8619                                   OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8620                                   OPJ_UINT32 * p_nb_comps,
8621                                   OPJ_BOOL * p_go_on,
8622                                   opj_stream_private_t *p_stream,
8623                                   opj_event_mgr_t * p_manager)
8624 {
8625     OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8626     OPJ_UINT32 l_marker_size;
8627     const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8628     opj_tcp_t * l_tcp = NULL;
8629 
8630     /* preconditions */
8631     assert(p_stream != 00);
8632     assert(p_j2k != 00);
8633     assert(p_manager != 00);
8634 
8635     /* Reach the End Of Codestream ?*/
8636     if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) {
8637         l_current_marker = J2K_MS_EOC;
8638     }
8639     /* We need to encounter a SOT marker (a new tile-part header) */
8640     else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) {
8641         return OPJ_FALSE;
8642     }
8643 
8644     /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8645     while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) &&
8646             (l_current_marker != J2K_MS_EOC)) {
8647 
8648         /* Try to read until the Start Of Data is detected */
8649         while (l_current_marker != J2K_MS_SOD) {
8650 
8651             if (opj_stream_get_number_byte_left(p_stream) == 0) {
8652                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
8653                 break;
8654             }
8655 
8656             /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8657             if (opj_stream_read_data(p_stream,
8658                                      p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
8659                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8660                 return OPJ_FALSE;
8661             }
8662 
8663             /* Read 2 bytes from the buffer as the marker size */
8664             opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size,
8665                            2);
8666 
8667             /* Check marker size (does not include marker ID but includes marker size) */
8668             if (l_marker_size < 2) {
8669                 opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
8670                 return OPJ_FALSE;
8671             }
8672 
8673             /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
8674             if (l_current_marker == 0x8080 &&
8675                     opj_stream_get_number_byte_left(p_stream) == 0) {
8676                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
8677                 break;
8678             }
8679 
8680             /* Why this condition? FIXME */
8681             if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) {
8682                 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8683             }
8684             l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8685 
8686             /* Get the marker handler from the marker ID */
8687             l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8688 
8689             /* Check if the marker is known and if it is the right place to find it */
8690             if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
8691                 opj_event_msg(p_manager, EVT_ERROR,
8692                               "Marker is not compliant with its position\n");
8693                 return OPJ_FALSE;
8694             }
8695             /* FIXME manage case of unknown marker as in the main header ? */
8696 
8697             /* Check if the marker size is compatible with the header data size */
8698             if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8699                 OPJ_BYTE *new_header_data = NULL;
8700                 /* If we are here, this means we consider this marker as known & we will read it */
8701                 /* Check enough bytes left in stream before allocation */
8702                 if ((OPJ_OFF_T)l_marker_size >  opj_stream_get_number_byte_left(p_stream)) {
8703                     opj_event_msg(p_manager, EVT_ERROR,
8704                                   "Marker size inconsistent with stream length\n");
8705                     return OPJ_FALSE;
8706                 }
8707                 new_header_data = (OPJ_BYTE *) opj_realloc(
8708                                       p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
8709                 if (! new_header_data) {
8710                     opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8711                     p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
8712                     p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8713                     opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
8714                     return OPJ_FALSE;
8715                 }
8716                 p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
8717                 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8718             }
8719 
8720             /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8721             if (opj_stream_read_data(p_stream,
8722                                      p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size,
8723                                      p_manager) != l_marker_size) {
8724                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8725                 return OPJ_FALSE;
8726             }
8727 
8728             if (!l_marker_handler->handler) {
8729                 /* See issue #175 */
8730                 opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");
8731                 return OPJ_FALSE;
8732             }
8733             /* Read the marker segment with the correct marker handler */
8734             if (!(*(l_marker_handler->handler))(p_j2k,
8735                                                 p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) {
8736                 opj_event_msg(p_manager, EVT_ERROR,
8737                               "Fail to read the current marker segment (%#x)\n", l_current_marker);
8738                 return OPJ_FALSE;
8739             }
8740 
8741             /* Add the marker to the codestream index*/
8742             if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
8743                                                   p_j2k->cstr_index,
8744                                                   l_marker_handler->id,
8745                                                   (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8746                                                   l_marker_size + 4)) {
8747                 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
8748                 return OPJ_FALSE;
8749             }
8750 
8751             /* Keep the position of the last SOT marker read */
8752             if (l_marker_handler->id == J2K_MS_SOT) {
8753                 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4
8754                                      ;
8755                 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) {
8756                     p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8757                 }
8758             }
8759 
8760             if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8761                 /* Skip the rest of the tile part header*/
8762                 if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length,
8763                                     p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
8764                     opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8765                     return OPJ_FALSE;
8766                 }
8767                 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8768             } else {
8769                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8770                 if (opj_stream_read_data(p_stream,
8771                                          p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
8772                     opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8773                     return OPJ_FALSE;
8774                 }
8775                 /* Read 2 bytes from the buffer as the new marker ID */
8776                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
8777                                &l_current_marker, 2);
8778             }
8779         }
8780         if (opj_stream_get_number_byte_left(p_stream) == 0
8781                 && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
8782             break;
8783         }
8784 
8785         /* If we didn't skip data before, we need to read the SOD marker*/
8786         if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8787             /* Try to read the SOD marker and skip data ? FIXME */
8788             if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8789                 return OPJ_FALSE;
8790             }
8791             if (p_j2k->m_specific_param.m_decoder.m_can_decode &&
8792                     !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) {
8793                 /* Issue 254 */
8794                 OPJ_BOOL l_correction_needed;
8795 
8796                 p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
8797                 if (!opj_j2k_need_nb_tile_parts_correction(p_stream,
8798                         p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) {
8799                     opj_event_msg(p_manager, EVT_ERROR,
8800                                   "opj_j2k_apply_nb_tile_parts_correction error\n");
8801                     return OPJ_FALSE;
8802                 }
8803                 if (l_correction_needed) {
8804                     OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
8805                     OPJ_UINT32 l_tile_no;
8806 
8807                     p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8808                     p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1;
8809                     /* correct tiles */
8810                     for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
8811                         if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) {
8812                             p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1;
8813                         }
8814                     }
8815                     opj_event_msg(p_manager, EVT_WARNING,
8816                                   "Non conformant codestream TPsot==TNsot.\n");
8817                 }
8818             }
8819             if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8820                 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8821                 if (opj_stream_read_data(p_stream,
8822                                          p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
8823                     opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8824                     return OPJ_FALSE;
8825                 }
8826 
8827                 /* Read 2 bytes from buffer as the new marker ID */
8828                 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
8829                                &l_current_marker, 2);
8830             }
8831         } else {
8832             /* Indicate we will try to read a new tile-part header*/
8833             p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8834             p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8835             p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8836 
8837             /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8838             if (opj_stream_read_data(p_stream,
8839                                      p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
8840                 opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8841                 return OPJ_FALSE;
8842             }
8843 
8844             /* Read 2 bytes from buffer as the new marker ID */
8845             opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
8846                            &l_current_marker, 2);
8847         }
8848     }
8849 
8850     /* Current marker is the EOC marker ?*/
8851     if (l_current_marker == J2K_MS_EOC) {
8852         if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
8853             p_j2k->m_current_tile_number = 0;
8854             p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8855         }
8856     }
8857 
8858     /* FIXME DOC ???*/
8859     if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8860         OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8861         l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8862 
8863         while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) {
8864             ++p_j2k->m_current_tile_number;
8865             ++l_tcp;
8866         }
8867 
8868         if (p_j2k->m_current_tile_number == l_nb_tiles) {
8869             *p_go_on = OPJ_FALSE;
8870             return OPJ_TRUE;
8871         }
8872     }
8873 
8874     if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number,
8875                             p_manager)) {
8876         opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n");
8877         return OPJ_FALSE;
8878     }
8879     /*FIXME ???*/
8880     if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number,
8881                                    p_manager)) {
8882         opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8883         return OPJ_FALSE;
8884     }
8885 
8886     opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8887                   p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw));
8888 
8889     *p_tile_index = p_j2k->m_current_tile_number;
8890     *p_go_on = OPJ_TRUE;
8891     if (p_data_size) {
8892         /* For internal use in j2k.c, we don't need this */
8893         /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */
8894         *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE);
8895         if (*p_data_size == UINT_MAX) {
8896             return OPJ_FALSE;
8897         }
8898     }
8899     *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8900     *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8901     *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8902     *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8903     *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8904 
8905     p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA;
8906 
8907     return OPJ_TRUE;
8908 }
8909 
opj_j2k_decode_tile(opj_j2k_t * p_j2k,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)8910 OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
8911                              OPJ_UINT32 p_tile_index,
8912                              OPJ_BYTE * p_data,
8913                              OPJ_UINT32 p_data_size,
8914                              opj_stream_private_t *p_stream,
8915                              opj_event_mgr_t * p_manager)
8916 {
8917     OPJ_UINT32 l_current_marker;
8918     OPJ_BYTE l_data [2];
8919     opj_tcp_t * l_tcp;
8920     opj_image_t* l_image_for_bounds;
8921 
8922     /* preconditions */
8923     assert(p_stream != 00);
8924     assert(p_j2k != 00);
8925     assert(p_manager != 00);
8926 
8927     if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA)
8928             || (p_tile_index != p_j2k->m_current_tile_number)) {
8929         return OPJ_FALSE;
8930     }
8931 
8932     l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8933     if (! l_tcp->m_data) {
8934         opj_j2k_tcp_destroy(l_tcp);
8935         return OPJ_FALSE;
8936     }
8937 
8938     /* When using the opj_read_tile_header / opj_decode_tile_data API */
8939     /* such as in test_tile_decoder, m_output_image is NULL, so fall back */
8940     /* to the full image dimension. This is a bit surprising that */
8941     /* opj_set_decode_area() is only used to determinte intersecting tiles, */
8942     /* but full tile decoding is done */
8943     l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image :
8944                          p_j2k->m_private_image;
8945     if (! opj_tcd_decode_tile(p_j2k->m_tcd,
8946                               l_image_for_bounds->x0,
8947                               l_image_for_bounds->y0,
8948                               l_image_for_bounds->x1,
8949                               l_image_for_bounds->y1,
8950                               p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode,
8951                               p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode,
8952                               l_tcp->m_data,
8953                               l_tcp->m_data_size,
8954                               p_tile_index,
8955                               p_j2k->cstr_index, p_manager)) {
8956         opj_j2k_tcp_destroy(l_tcp);
8957         p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
8958         opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
8959         return OPJ_FALSE;
8960     }
8961 
8962     /* p_data can be set to NULL when the call will take care of using */
8963     /* itself the TCD data. This is typically the case for whole single */
8964     /* tile decoding optimization. */
8965     if (p_data != NULL) {
8966         if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) {
8967             return OPJ_FALSE;
8968         }
8969 
8970         /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8971         * we destroy just the data which will be re-read in read_tile_header*/
8972         /*opj_j2k_tcp_destroy(l_tcp);
8973         p_j2k->m_tcd->tcp = 0;*/
8974         opj_j2k_tcp_data_destroy(l_tcp);
8975     }
8976 
8977     p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8978     p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA);
8979 
8980     if (opj_stream_get_number_byte_left(p_stream) == 0
8981             && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
8982         return OPJ_TRUE;
8983     }
8984 
8985     if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
8986         if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
8987             opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
8988             return OPJ_FALSE;
8989         }
8990 
8991         opj_read_bytes(l_data, &l_current_marker, 2);
8992 
8993         if (l_current_marker == J2K_MS_EOC) {
8994             p_j2k->m_current_tile_number = 0;
8995             p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8996         } else if (l_current_marker != J2K_MS_SOT) {
8997             if (opj_stream_get_number_byte_left(p_stream) == 0) {
8998                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
8999                 opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n");
9000                 return OPJ_TRUE;
9001             }
9002             opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9003             return OPJ_FALSE;
9004         }
9005     }
9006 
9007     return OPJ_TRUE;
9008 }
9009 
opj_j2k_update_image_data(opj_tcd_t * p_tcd,opj_image_t * p_output_image)9010 static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
9011         opj_image_t* p_output_image)
9012 {
9013     OPJ_UINT32 i, j;
9014     OPJ_UINT32 l_width_src, l_height_src;
9015     OPJ_UINT32 l_width_dest, l_height_dest;
9016     OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9017     OPJ_SIZE_T l_start_offset_src;
9018     OPJ_UINT32 l_start_x_dest, l_start_y_dest;
9019     OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9020     OPJ_SIZE_T l_start_offset_dest;
9021 
9022     opj_image_comp_t * l_img_comp_src = 00;
9023     opj_image_comp_t * l_img_comp_dest = 00;
9024 
9025     opj_tcd_tilecomp_t * l_tilec = 00;
9026     opj_image_t * l_image_src = 00;
9027     OPJ_INT32 * l_dest_ptr;
9028 
9029     l_tilec = p_tcd->tcd_image->tiles->comps;
9030     l_image_src = p_tcd->image;
9031     l_img_comp_src = l_image_src->comps;
9032 
9033     l_img_comp_dest = p_output_image->comps;
9034 
9035     for (i = 0; i < l_image_src->numcomps;
9036             i++, ++l_img_comp_dest, ++l_img_comp_src,  ++l_tilec) {
9037         OPJ_INT32 res_x0, res_x1, res_y0, res_y1;
9038         OPJ_UINT32 src_data_stride;
9039         const OPJ_INT32* p_src_data;
9040 
9041         /* Copy info from decoded comp image to output image */
9042         l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9043 
9044         if (p_tcd->whole_tile_decoding) {
9045             opj_tcd_resolution_t* l_res = l_tilec->resolutions +
9046                                           l_img_comp_src->resno_decoded;
9047             res_x0 = l_res->x0;
9048             res_y0 = l_res->y0;
9049             res_x1 = l_res->x1;
9050             res_y1 = l_res->y1;
9051             src_data_stride = (OPJ_UINT32)(
9052                                   l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 -
9053                                   l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0);
9054             p_src_data = l_tilec->data;
9055         } else {
9056             opj_tcd_resolution_t* l_res = l_tilec->resolutions +
9057                                           l_img_comp_src->resno_decoded;
9058             res_x0 = (OPJ_INT32)l_res->win_x0;
9059             res_y0 = (OPJ_INT32)l_res->win_y0;
9060             res_x1 = (OPJ_INT32)l_res->win_x1;
9061             res_y1 = (OPJ_INT32)l_res->win_y1;
9062             src_data_stride = l_res->win_x1 - l_res->win_x0;
9063             p_src_data = l_tilec->data_win;
9064         }
9065 
9066         if (p_src_data == NULL) {
9067             /* Happens for partial component decoding */
9068             continue;
9069         }
9070 
9071         l_width_src = (OPJ_UINT32)(res_x1 - res_x0);
9072         l_height_src = (OPJ_UINT32)(res_y1 - res_y0);
9073 
9074 
9075         /* Current tile component size*/
9076         /*if (i == 0) {
9077         fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9078                         res_x0, res_x1, res_y0, res_y1);
9079         }*/
9080 
9081 
9082         /* Border of the current output component*/
9083         l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9084         l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9085         l_x1_dest = l_x0_dest +
9086                     l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */
9087         l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9088 
9089         /*if (i == 0) {
9090         fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9091                         l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9092         }*/
9093 
9094         /*-----*/
9095         /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9096          * of the input buffer (decoded tile component) which will be move
9097          * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9098          * l_start_y_dest, l_width_dest, l_height_dest)  which will be modified
9099          * by this input area.
9100          * */
9101         assert(res_x0 >= 0);
9102         assert(res_x1 >= 0);
9103 
9104         /* Prevent bad casting to unsigned values in the subsequent lines. */
9105         if ( res_x0 < 0 || res_x1 < 0 || res_y0 < 0 || res_y1 < 0 ) {
9106             return OPJ_FALSE;
9107         }
9108 
9109         if (l_x0_dest < (OPJ_UINT32)res_x0) {
9110             l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest;
9111             l_offset_x0_src = 0;
9112 
9113             if (l_x1_dest >= (OPJ_UINT32)res_x1) {
9114                 l_width_dest = l_width_src;
9115                 l_offset_x1_src = 0;
9116             } else {
9117                 l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ;
9118                 l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
9119             }
9120         } else {
9121             l_start_x_dest = 0U;
9122             l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0;
9123 
9124             if (l_x1_dest >= (OPJ_UINT32)res_x1) {
9125                 l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
9126                 l_offset_x1_src = 0;
9127             } else {
9128                 l_width_dest = l_img_comp_dest->w ;
9129                 l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest;
9130             }
9131         }
9132 
9133         if (l_y0_dest < (OPJ_UINT32)res_y0) {
9134             l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest;
9135             l_offset_y0_src = 0;
9136 
9137             if (l_y1_dest >= (OPJ_UINT32)res_y1) {
9138                 l_height_dest = l_height_src;
9139                 l_offset_y1_src = 0;
9140             } else {
9141                 l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ;
9142                 l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest);
9143             }
9144         } else {
9145             l_start_y_dest = 0U;
9146             l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0;
9147 
9148             if (l_y1_dest >= (OPJ_UINT32)res_y1) {
9149                 l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
9150                 l_offset_y1_src = 0;
9151             } else {
9152                 l_height_dest = l_img_comp_dest->h ;
9153                 l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest;
9154             }
9155         }
9156 
9157         if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) ||
9158                 (l_offset_y1_src < 0)) {
9159             return OPJ_FALSE;
9160         }
9161         /* testcase 2977.pdf.asan.67.2198 */
9162         if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
9163             return OPJ_FALSE;
9164         }
9165         /*-----*/
9166 
9167         /* Compute the input buffer offset */
9168         l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src
9169                              * (OPJ_SIZE_T)src_data_stride;
9170 
9171         /* Compute the output buffer offset */
9172         l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest
9173                               * (OPJ_SIZE_T)l_img_comp_dest->w;
9174 
9175         /* Allocate output component buffer if necessary */
9176         if (l_img_comp_dest->data == NULL &&
9177                 l_start_offset_src == 0 && l_start_offset_dest == 0 &&
9178                 src_data_stride == l_img_comp_dest->w &&
9179                 l_width_dest == l_img_comp_dest->w &&
9180                 l_height_dest == l_img_comp_dest->h) {
9181             /* If the final image matches the tile buffer, then borrow it */
9182             /* directly to save a copy */
9183             if (p_tcd->whole_tile_decoding) {
9184                 l_img_comp_dest->data = l_tilec->data;
9185                 l_tilec->data = NULL;
9186             } else {
9187                 l_img_comp_dest->data = l_tilec->data_win;
9188                 l_tilec->data_win = NULL;
9189             }
9190             continue;
9191         } else if (l_img_comp_dest->data == NULL) {
9192             OPJ_SIZE_T l_width = l_img_comp_dest->w;
9193             OPJ_SIZE_T l_height = l_img_comp_dest->h;
9194 
9195             if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) ||
9196                     l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) {
9197                 /* would overflow */
9198                 return OPJ_FALSE;
9199             }
9200             l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height *
9201                                     sizeof(OPJ_INT32));
9202             if (! l_img_comp_dest->data) {
9203                 return OPJ_FALSE;
9204             }
9205 
9206             if (l_img_comp_dest->w != l_width_dest ||
9207                     l_img_comp_dest->h != l_height_dest) {
9208                 memset(l_img_comp_dest->data, 0,
9209                        (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32));
9210             }
9211         }
9212 
9213         /* Move the output buffer to the first place where we will write*/
9214         l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9215 
9216         {
9217             const OPJ_INT32 * l_src_ptr = p_src_data;
9218             l_src_ptr += l_start_offset_src;
9219 
9220             for (j = 0; j < l_height_dest; ++j) {
9221                 memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32));
9222                 l_dest_ptr += l_img_comp_dest->w;
9223                 l_src_ptr += src_data_stride;
9224             }
9225         }
9226 
9227 
9228     }
9229 
9230     return OPJ_TRUE;
9231 }
9232 
opj_j2k_update_image_dimensions(opj_image_t * p_image,opj_event_mgr_t * p_manager)9233 static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image,
9234         opj_event_mgr_t * p_manager)
9235 {
9236     OPJ_UINT32 it_comp;
9237     OPJ_INT32 l_comp_x1, l_comp_y1;
9238     opj_image_comp_t* l_img_comp = NULL;
9239 
9240     l_img_comp = p_image->comps;
9241     for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
9242         l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
9243         l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy);
9244         l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
9245         l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
9246 
9247         OPJ_INT32 l_1 = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor);
9248         OPJ_INT32 l_2 = opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
9249         if (l_1 < l_2) {
9250             opj_event_msg(p_manager, EVT_ERROR,
9251                           "Size x of the decoded component image is incorrect (comp[%d].w<0).\n",
9252                           it_comp);
9253             return OPJ_FALSE;
9254         }
9255         l_img_comp->w = (OPJ_UINT32)(l_1-l_2);
9256 
9257         l_1 = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor);
9258         l_2 = opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
9259         if (l_1 < l_2) {
9260             opj_event_msg(p_manager, EVT_ERROR,
9261                           "Size y of the decoded component image is incorrect (comp[%d].h<0).\n",
9262                           it_comp);
9263             return OPJ_FALSE;
9264         }
9265         l_img_comp->h = (OPJ_UINT32)(l_1-l_2);
9266 
9267         l_img_comp++;
9268     }
9269 
9270     return OPJ_TRUE;
9271 }
9272 
opj_j2k_set_decoded_components(opj_j2k_t * p_j2k,OPJ_UINT32 numcomps,const OPJ_UINT32 * comps_indices,opj_event_mgr_t * p_manager)9273 OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
9274                                         OPJ_UINT32 numcomps,
9275                                         const OPJ_UINT32* comps_indices,
9276                                         opj_event_mgr_t * p_manager)
9277 {
9278     OPJ_UINT32 i;
9279     OPJ_BOOL* already_mapped;
9280 
9281     if (p_j2k->m_private_image == NULL) {
9282         opj_event_msg(p_manager, EVT_ERROR,
9283                       "opj_read_header() should be called before "
9284                       "opj_set_decoded_components().\n");
9285         return OPJ_FALSE;
9286     }
9287 
9288     already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
9289                                             p_j2k->m_private_image->numcomps);
9290     if (already_mapped == NULL) {
9291         return OPJ_FALSE;
9292     }
9293 
9294     for (i = 0; i < numcomps; i++) {
9295         if (comps_indices[i] >= p_j2k->m_private_image->numcomps) {
9296             opj_event_msg(p_manager, EVT_ERROR,
9297                           "Invalid component index: %u\n",
9298                           comps_indices[i]);
9299             opj_free(already_mapped);
9300             return OPJ_FALSE;
9301         }
9302         if (already_mapped[comps_indices[i]]) {
9303             opj_event_msg(p_manager, EVT_ERROR,
9304                           "Component index %u used several times\n",
9305                           comps_indices[i]);
9306             opj_free(already_mapped);
9307             return OPJ_FALSE;
9308         }
9309         already_mapped[comps_indices[i]] = OPJ_TRUE;
9310     }
9311     opj_free(already_mapped);
9312 
9313     opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode);
9314     if (numcomps) {
9315         p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode =
9316             (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32));
9317         if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) {
9318             p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0;
9319             return OPJ_FALSE;
9320         }
9321         memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode,
9322                comps_indices,
9323                numcomps * sizeof(OPJ_UINT32));
9324     } else {
9325         p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL;
9326     }
9327     p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps;
9328 
9329     return OPJ_TRUE;
9330 }
9331 
9332 
opj_j2k_set_decode_area(opj_j2k_t * p_j2k,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)9333 OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k,
9334                                  opj_image_t* p_image,
9335                                  OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9336                                  OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9337                                  opj_event_mgr_t * p_manager)
9338 {
9339     opj_cp_t * l_cp = &(p_j2k->m_cp);
9340     opj_image_t * l_image = p_j2k->m_private_image;
9341     OPJ_BOOL ret;
9342     OPJ_UINT32 it_comp;
9343 
9344     if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
9345             p_j2k->m_cp.tcps[0].m_data != NULL) {
9346         /* In the case of a single-tiled image whose codestream we have already */
9347         /* ingested, go on */
9348     }
9349     /* Check if we are read the main header */
9350     else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) {
9351         opj_event_msg(p_manager, EVT_ERROR,
9352                       "Need to decode the main header before begin to decode the remaining codestream.\n");
9353         return OPJ_FALSE;
9354     }
9355 
9356     /* Update the comps[].factor member of the output image with the one */
9357     /* of m_reduce */
9358     for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
9359         p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce;
9360     }
9361 
9362     if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) {
9363         opj_event_msg(p_manager, EVT_INFO,
9364                       "No decoded area parameters, set the decoded area to the whole image\n");
9365 
9366         p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9367         p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9368         p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9369         p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9370 
9371         p_image->x0 = l_image->x0;
9372         p_image->y0 = l_image->y0;
9373         p_image->x1 = l_image->x1;
9374         p_image->y1 = l_image->y1;
9375 
9376         return opj_j2k_update_image_dimensions(p_image, p_manager);
9377     }
9378 
9379     /* ----- */
9380     /* Check if the positions provided by the user are correct */
9381 
9382     /* Left */
9383     if (p_start_x < 0) {
9384         opj_event_msg(p_manager, EVT_ERROR,
9385                       "Left position of the decoded area (region_x0=%d) should be >= 0.\n",
9386                       p_start_x);
9387         return OPJ_FALSE;
9388     } else if ((OPJ_UINT32)p_start_x > l_image->x1) {
9389         opj_event_msg(p_manager, EVT_ERROR,
9390                       "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9391                       p_start_x, l_image->x1);
9392         return OPJ_FALSE;
9393     } else if ((OPJ_UINT32)p_start_x < l_image->x0) {
9394         opj_event_msg(p_manager, EVT_WARNING,
9395                       "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9396                       p_start_x, l_image->x0);
9397         p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9398         p_image->x0 = l_image->x0;
9399     } else {
9400         p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x -
9401                 l_cp->tx0) / l_cp->tdx;
9402         p_image->x0 = (OPJ_UINT32)p_start_x;
9403     }
9404 
9405     /* Up */
9406     if (p_start_y < 0) {
9407         opj_event_msg(p_manager, EVT_ERROR,
9408                       "Up position of the decoded area (region_y0=%d) should be >= 0.\n",
9409                       p_start_y);
9410         return OPJ_FALSE;
9411     } else if ((OPJ_UINT32)p_start_y > l_image->y1) {
9412         opj_event_msg(p_manager, EVT_ERROR,
9413                       "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9414                       p_start_y, l_image->y1);
9415         return OPJ_FALSE;
9416     } else if ((OPJ_UINT32)p_start_y < l_image->y0) {
9417         opj_event_msg(p_manager, EVT_WARNING,
9418                       "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9419                       p_start_y, l_image->y0);
9420         p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9421         p_image->y0 = l_image->y0;
9422     } else {
9423         p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y -
9424                 l_cp->ty0) / l_cp->tdy;
9425         p_image->y0 = (OPJ_UINT32)p_start_y;
9426     }
9427 
9428     /* Right */
9429     if (p_end_x <= 0) {
9430         opj_event_msg(p_manager, EVT_ERROR,
9431                       "Right position of the decoded area (region_x1=%d) should be > 0.\n",
9432                       p_end_x);
9433         return OPJ_FALSE;
9434     } else if ((OPJ_UINT32)p_end_x < l_image->x0) {
9435         opj_event_msg(p_manager, EVT_ERROR,
9436                       "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9437                       p_end_x, l_image->x0);
9438         return OPJ_FALSE;
9439     } else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9440         opj_event_msg(p_manager, EVT_WARNING,
9441                       "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9442                       p_end_x, l_image->x1);
9443         p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9444         p_image->x1 = l_image->x1;
9445     } else {
9446         p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(
9447                     p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);
9448         p_image->x1 = (OPJ_UINT32)p_end_x;
9449     }
9450 
9451     /* Bottom */
9452     if (p_end_y <= 0) {
9453         opj_event_msg(p_manager, EVT_ERROR,
9454                       "Bottom position of the decoded area (region_y1=%d) should be > 0.\n",
9455                       p_end_y);
9456         return OPJ_FALSE;
9457     } else if ((OPJ_UINT32)p_end_y < l_image->y0) {
9458         opj_event_msg(p_manager, EVT_ERROR,
9459                       "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9460                       p_end_y, l_image->y0);
9461         return OPJ_FALSE;
9462     }
9463     if ((OPJ_UINT32)p_end_y > l_image->y1) {
9464         opj_event_msg(p_manager, EVT_WARNING,
9465                       "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9466                       p_end_y, l_image->y1);
9467         p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9468         p_image->y1 = l_image->y1;
9469     } else {
9470         p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(
9471                     p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);
9472         p_image->y1 = (OPJ_UINT32)p_end_y;
9473     }
9474     /* ----- */
9475 
9476     p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9477 
9478     ret = opj_j2k_update_image_dimensions(p_image, p_manager);
9479 
9480     if (ret) {
9481         opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n",
9482                       p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9483     }
9484 
9485     return ret;
9486 }
9487 
opj_j2k_create_decompress(void)9488 opj_j2k_t* opj_j2k_create_decompress(void)
9489 {
9490     opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
9491     if (!l_j2k) {
9492         return 00;
9493     }
9494 
9495     l_j2k->m_is_decoder = 1;
9496     l_j2k->m_cp.m_is_decoder = 1;
9497     /* in the absence of JP2 boxes, consider different bit depth / sign */
9498     /* per component is allowed */
9499     l_j2k->m_cp.allow_different_bit_depth_sign = 1;
9500 
9501 #ifdef OPJ_DISABLE_TPSOT_FIX
9502     l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
9503 #endif
9504 
9505     l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,
9506             sizeof(opj_tcp_t));
9507     if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9508         opj_j2k_destroy(l_j2k);
9509         return 00;
9510     }
9511 
9512     l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,
9513             OPJ_J2K_DEFAULT_HEADER_SIZE);
9514     if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9515         opj_j2k_destroy(l_j2k);
9516         return 00;
9517     }
9518 
9519     l_j2k->m_specific_param.m_decoder.m_header_data_size =
9520         OPJ_J2K_DEFAULT_HEADER_SIZE;
9521 
9522     l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9523 
9524     l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9525 
9526     /* codestream index creation */
9527     l_j2k->cstr_index = opj_j2k_create_cstr_index();
9528     if (!l_j2k->cstr_index) {
9529         opj_j2k_destroy(l_j2k);
9530         return 00;
9531     }
9532 
9533     /* validation list creation */
9534     l_j2k->m_validation_list = opj_procedure_list_create();
9535     if (! l_j2k->m_validation_list) {
9536         opj_j2k_destroy(l_j2k);
9537         return 00;
9538     }
9539 
9540     /* execution list creation */
9541     l_j2k->m_procedure_list = opj_procedure_list_create();
9542     if (! l_j2k->m_procedure_list) {
9543         opj_j2k_destroy(l_j2k);
9544         return 00;
9545     }
9546 
9547     l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count());
9548     if (!l_j2k->m_tp) {
9549         l_j2k->m_tp = opj_thread_pool_create(0);
9550     }
9551     if (!l_j2k->m_tp) {
9552         opj_j2k_destroy(l_j2k);
9553         return NULL;
9554     }
9555 
9556     return l_j2k;
9557 }
9558 
opj_j2k_create_cstr_index(void)9559 static opj_codestream_index_t* opj_j2k_create_cstr_index(void)
9560 {
9561     opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9562                                          opj_calloc(1, sizeof(opj_codestream_index_t));
9563     if (!cstr_index) {
9564         return NULL;
9565     }
9566 
9567     cstr_index->maxmarknum = 100;
9568     cstr_index->marknum = 0;
9569     cstr_index->marker = (opj_marker_info_t*)
9570                          opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9571     if (!cstr_index-> marker) {
9572         opj_free(cstr_index);
9573         return NULL;
9574     }
9575 
9576     cstr_index->tile_index = NULL;
9577 
9578     return cstr_index;
9579 }
9580 
opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no)9581 static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k,
9582         OPJ_UINT32 p_tile_no,
9583         OPJ_UINT32 p_comp_no)
9584 {
9585     opj_cp_t *l_cp = 00;
9586     opj_tcp_t *l_tcp = 00;
9587     opj_tccp_t *l_tccp = 00;
9588 
9589     /* preconditions */
9590     assert(p_j2k != 00);
9591 
9592     l_cp = &(p_j2k->m_cp);
9593     l_tcp = &l_cp->tcps[p_tile_no];
9594     l_tccp = &l_tcp->tccps[p_comp_no];
9595 
9596     /* preconditions again */
9597     assert(p_tile_no < (l_cp->tw * l_cp->th));
9598     assert(p_comp_no < p_j2k->m_private_image->numcomps);
9599 
9600     if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9601         return 5 + l_tccp->numresolutions;
9602     } else {
9603         return 5;
9604     }
9605 }
9606 
opj_j2k_compare_SPCod_SPCoc(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_first_comp_no,OPJ_UINT32 p_second_comp_no)9607 static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k,
9608         OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
9609 {
9610     OPJ_UINT32 i;
9611     opj_cp_t *l_cp = NULL;
9612     opj_tcp_t *l_tcp = NULL;
9613     opj_tccp_t *l_tccp0 = NULL;
9614     opj_tccp_t *l_tccp1 = NULL;
9615 
9616     /* preconditions */
9617     assert(p_j2k != 00);
9618 
9619     l_cp = &(p_j2k->m_cp);
9620     l_tcp = &l_cp->tcps[p_tile_no];
9621     l_tccp0 = &l_tcp->tccps[p_first_comp_no];
9622     l_tccp1 = &l_tcp->tccps[p_second_comp_no];
9623 
9624     if (l_tccp0->numresolutions != l_tccp1->numresolutions) {
9625         return OPJ_FALSE;
9626     }
9627     if (l_tccp0->cblkw != l_tccp1->cblkw) {
9628         return OPJ_FALSE;
9629     }
9630     if (l_tccp0->cblkh != l_tccp1->cblkh) {
9631         return OPJ_FALSE;
9632     }
9633     if (l_tccp0->cblksty != l_tccp1->cblksty) {
9634         return OPJ_FALSE;
9635     }
9636     if (l_tccp0->qmfbid != l_tccp1->qmfbid) {
9637         return OPJ_FALSE;
9638     }
9639     if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) {
9640         return OPJ_FALSE;
9641     }
9642 
9643     for (i = 0U; i < l_tccp0->numresolutions; ++i) {
9644         if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) {
9645             return OPJ_FALSE;
9646         }
9647         if (l_tccp0->prch[i] != l_tccp1->prch[i]) {
9648             return OPJ_FALSE;
9649         }
9650     }
9651     return OPJ_TRUE;
9652 }
9653 
opj_j2k_write_SPCod_SPCoc(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_data,OPJ_UINT32 * p_header_size,struct opj_event_mgr * p_manager)9654 static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k,
9655         OPJ_UINT32 p_tile_no,
9656         OPJ_UINT32 p_comp_no,
9657         OPJ_BYTE * p_data,
9658         OPJ_UINT32 * p_header_size,
9659         struct opj_event_mgr * p_manager)
9660 {
9661     OPJ_UINT32 i;
9662     opj_cp_t *l_cp = 00;
9663     opj_tcp_t *l_tcp = 00;
9664     opj_tccp_t *l_tccp = 00;
9665 
9666     /* preconditions */
9667     assert(p_j2k != 00);
9668     assert(p_header_size != 00);
9669     assert(p_manager != 00);
9670     assert(p_data != 00);
9671 
9672     l_cp = &(p_j2k->m_cp);
9673     l_tcp = &l_cp->tcps[p_tile_no];
9674     l_tccp = &l_tcp->tccps[p_comp_no];
9675 
9676     /* preconditions again */
9677     assert(p_tile_no < (l_cp->tw * l_cp->th));
9678     assert(p_comp_no < (p_j2k->m_private_image->numcomps));
9679 
9680     if (*p_header_size < 5) {
9681         opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9682         return OPJ_FALSE;
9683     }
9684 
9685     opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9686     ++p_data;
9687 
9688     opj_write_bytes(p_data, l_tccp->cblkw - 2, 1);                  /* SPcoc (E) */
9689     ++p_data;
9690 
9691     opj_write_bytes(p_data, l_tccp->cblkh - 2, 1);                  /* SPcoc (F) */
9692     ++p_data;
9693 
9694     opj_write_bytes(p_data, l_tccp->cblksty,
9695                     1);                            /* SPcoc (G) */
9696     ++p_data;
9697 
9698     opj_write_bytes(p_data, l_tccp->qmfbid,
9699                     1);                             /* SPcoc (H) */
9700     ++p_data;
9701 
9702     *p_header_size = *p_header_size - 5;
9703 
9704     if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9705 
9706         if (*p_header_size < l_tccp->numresolutions) {
9707             opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9708             return OPJ_FALSE;
9709         }
9710 
9711         for (i = 0; i < l_tccp->numresolutions; ++i) {
9712             opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4),
9713                             1);   /* SPcoc (I_i) */
9714             ++p_data;
9715         }
9716 
9717         *p_header_size = *p_header_size - l_tccp->numresolutions;
9718     }
9719 
9720     return OPJ_TRUE;
9721 }
9722 
opj_j2k_read_SPCod_SPCoc(opj_j2k_t * p_j2k,OPJ_UINT32 compno,OPJ_BYTE * p_header_data,OPJ_UINT32 * p_header_size,opj_event_mgr_t * p_manager)9723 static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k,
9724         OPJ_UINT32 compno,
9725         OPJ_BYTE * p_header_data,
9726         OPJ_UINT32 * p_header_size,
9727         opj_event_mgr_t * p_manager)
9728 {
9729     OPJ_UINT32 i, l_tmp;
9730     opj_cp_t *l_cp = NULL;
9731     opj_tcp_t *l_tcp = NULL;
9732     opj_tccp_t *l_tccp = NULL;
9733     OPJ_BYTE * l_current_ptr = NULL;
9734 
9735     /* preconditions */
9736     assert(p_j2k != 00);
9737     assert(p_manager != 00);
9738     assert(p_header_data != 00);
9739 
9740     l_cp = &(p_j2k->m_cp);
9741     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9742             &l_cp->tcps[p_j2k->m_current_tile_number] :
9743             p_j2k->m_specific_param.m_decoder.m_default_tcp;
9744 
9745     /* precondition again */
9746     if (compno >= p_j2k->m_private_image->numcomps) {
9747         return OPJ_FALSE;
9748     }
9749 
9750     assert(compno < p_j2k->m_private_image->numcomps);
9751 
9752     l_tccp = &l_tcp->tccps[compno];
9753     l_current_ptr = p_header_data;
9754 
9755     /* make sure room is sufficient */
9756     if (*p_header_size < 5) {
9757         opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9758         return OPJ_FALSE;
9759     }
9760 
9761     opj_read_bytes(l_current_ptr, &l_tccp->numresolutions,
9762                    1);              /* SPcox (D) */
9763     ++l_tccp->numresolutions;                                                                               /* tccp->numresolutions = read() + 1 */
9764     if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
9765         opj_event_msg(p_manager, EVT_ERROR,
9766                       "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
9767                       l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
9768         return OPJ_FALSE;
9769     }
9770     ++l_current_ptr;
9771 
9772     /* If user wants to remove more resolutions than the codestream contains, return error */
9773     if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9774         opj_event_msg(p_manager, EVT_ERROR,
9775                       "Error decoding component %d.\nThe number of resolutions "
9776                       "to remove (%d) is greater or equal than the number "
9777                       "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n",
9778                       compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions);
9779         p_j2k->m_specific_param.m_decoder.m_state |=
9780             0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9781         return OPJ_FALSE;
9782     }
9783 
9784     opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1);               /* SPcoc (E) */
9785     ++l_current_ptr;
9786     l_tccp->cblkw += 2;
9787 
9788     opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1);               /* SPcoc (F) */
9789     ++l_current_ptr;
9790     l_tccp->cblkh += 2;
9791 
9792     if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) ||
9793             ((l_tccp->cblkw + l_tccp->cblkh) > 12)) {
9794         opj_event_msg(p_manager, EVT_ERROR,
9795                       "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n");
9796         return OPJ_FALSE;
9797     }
9798 
9799 
9800     opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1);             /* SPcoc (G) */
9801     ++l_current_ptr;
9802     if (l_tccp->cblksty & 0xC0U) { /* 2 msb are reserved, assume we can't read */
9803         opj_event_msg(p_manager, EVT_ERROR,
9804                       "Error reading SPCod SPCoc element, Invalid code-block style found\n");
9805         return OPJ_FALSE;
9806     }
9807 
9808     opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1);              /* SPcoc (H) */
9809     ++l_current_ptr;
9810 
9811     *p_header_size = *p_header_size - 5;
9812 
9813     /* use custom precinct size ? */
9814     if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9815         if (*p_header_size < l_tccp->numresolutions) {
9816             opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9817             return OPJ_FALSE;
9818         }
9819 
9820         for (i = 0; i < l_tccp->numresolutions; ++i) {
9821             opj_read_bytes(l_current_ptr, &l_tmp, 1);               /* SPcoc (I_i) */
9822             ++l_current_ptr;
9823             /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */
9824             if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) {
9825                 opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n");
9826                 return OPJ_FALSE;
9827             }
9828             l_tccp->prcw[i] = l_tmp & 0xf;
9829             l_tccp->prch[i] = l_tmp >> 4;
9830         }
9831 
9832         *p_header_size = *p_header_size - l_tccp->numresolutions;
9833     } else {
9834         /* set default size for the precinct width and height */
9835         for (i = 0; i < l_tccp->numresolutions; ++i) {
9836             l_tccp->prcw[i] = 15;
9837             l_tccp->prch[i] = 15;
9838         }
9839     }
9840 
9841 #ifdef WIP_REMOVE_MSD
9842     /* INDEX >> */
9843     if (p_j2k->cstr_info && compno == 0) {
9844         OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9845 
9846         p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh =
9847             l_tccp->cblkh;
9848         p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw =
9849             l_tccp->cblkw;
9850         p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions
9851             = l_tccp->numresolutions;
9852         p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty =
9853             l_tccp->cblksty;
9854         p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid =
9855             l_tccp->qmfbid;
9856 
9857         memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw,
9858                l_data_size);
9859         memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch,
9860                l_data_size);
9861     }
9862     /* << INDEX */
9863 #endif
9864 
9865     return OPJ_TRUE;
9866 }
9867 
opj_j2k_copy_tile_component_parameters(opj_j2k_t * p_j2k)9868 static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k)
9869 {
9870     /* loop */
9871     OPJ_UINT32 i;
9872     opj_cp_t *l_cp = NULL;
9873     opj_tcp_t *l_tcp = NULL;
9874     opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9875     OPJ_UINT32 l_prc_size;
9876 
9877     /* preconditions */
9878     assert(p_j2k != 00);
9879 
9880     l_cp = &(p_j2k->m_cp);
9881     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
9882             ?
9883             &l_cp->tcps[p_j2k->m_current_tile_number] :
9884             p_j2k->m_specific_param.m_decoder.m_default_tcp;
9885 
9886     l_ref_tccp = &l_tcp->tccps[0];
9887     l_copied_tccp = l_ref_tccp + 1;
9888     l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);
9889 
9890     for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) {
9891         l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9892         l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9893         l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9894         l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9895         l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9896         memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size);
9897         memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size);
9898         ++l_copied_tccp;
9899     }
9900 }
9901 
opj_j2k_get_SQcd_SQcc_size(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no)9902 static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k,
9903         OPJ_UINT32 p_tile_no,
9904         OPJ_UINT32 p_comp_no)
9905 {
9906     OPJ_UINT32 l_num_bands;
9907 
9908     opj_cp_t *l_cp = 00;
9909     opj_tcp_t *l_tcp = 00;
9910     opj_tccp_t *l_tccp = 00;
9911 
9912     /* preconditions */
9913     assert(p_j2k != 00);
9914 
9915     l_cp = &(p_j2k->m_cp);
9916     l_tcp = &l_cp->tcps[p_tile_no];
9917     l_tccp = &l_tcp->tccps[p_comp_no];
9918 
9919     /* preconditions again */
9920     assert(p_tile_no < l_cp->tw * l_cp->th);
9921     assert(p_comp_no < p_j2k->m_private_image->numcomps);
9922 
9923     l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
9924                   (l_tccp->numresolutions * 3 - 2);
9925 
9926     if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
9927         return 1 + l_num_bands;
9928     } else {
9929         return 1 + 2 * l_num_bands;
9930     }
9931 }
9932 
opj_j2k_compare_SQcd_SQcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_first_comp_no,OPJ_UINT32 p_second_comp_no)9933 static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k,
9934         OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
9935 {
9936     opj_cp_t *l_cp = NULL;
9937     opj_tcp_t *l_tcp = NULL;
9938     opj_tccp_t *l_tccp0 = NULL;
9939     opj_tccp_t *l_tccp1 = NULL;
9940     OPJ_UINT32 l_band_no, l_num_bands;
9941 
9942     /* preconditions */
9943     assert(p_j2k != 00);
9944 
9945     l_cp = &(p_j2k->m_cp);
9946     l_tcp = &l_cp->tcps[p_tile_no];
9947     l_tccp0 = &l_tcp->tccps[p_first_comp_no];
9948     l_tccp1 = &l_tcp->tccps[p_second_comp_no];
9949 
9950     if (l_tccp0->qntsty != l_tccp1->qntsty) {
9951         return OPJ_FALSE;
9952     }
9953     if (l_tccp0->numgbits != l_tccp1->numgbits) {
9954         return OPJ_FALSE;
9955     }
9956     if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9957         l_num_bands = 1U;
9958     } else {
9959         l_num_bands = l_tccp0->numresolutions * 3U - 2U;
9960         if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) {
9961             return OPJ_FALSE;
9962         }
9963     }
9964 
9965     for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9966         if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) {
9967             return OPJ_FALSE;
9968         }
9969     }
9970     if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) {
9971         for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9972             if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) {
9973                 return OPJ_FALSE;
9974             }
9975         }
9976     }
9977     return OPJ_TRUE;
9978 }
9979 
9980 
opj_j2k_write_SQcd_SQcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_no,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_data,OPJ_UINT32 * p_header_size,struct opj_event_mgr * p_manager)9981 static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
9982                                         OPJ_UINT32 p_tile_no,
9983                                         OPJ_UINT32 p_comp_no,
9984                                         OPJ_BYTE * p_data,
9985                                         OPJ_UINT32 * p_header_size,
9986                                         struct opj_event_mgr * p_manager)
9987 {
9988     OPJ_UINT32 l_header_size;
9989     OPJ_UINT32 l_band_no, l_num_bands;
9990     OPJ_UINT32 l_expn, l_mant;
9991 
9992     opj_cp_t *l_cp = 00;
9993     opj_tcp_t *l_tcp = 00;
9994     opj_tccp_t *l_tccp = 00;
9995 
9996     /* preconditions */
9997     assert(p_j2k != 00);
9998     assert(p_header_size != 00);
9999     assert(p_manager != 00);
10000     assert(p_data != 00);
10001 
10002     l_cp = &(p_j2k->m_cp);
10003     l_tcp = &l_cp->tcps[p_tile_no];
10004     l_tccp = &l_tcp->tccps[p_comp_no];
10005 
10006     /* preconditions again */
10007     assert(p_tile_no < l_cp->tw * l_cp->th);
10008     assert(p_comp_no < p_j2k->m_private_image->numcomps);
10009 
10010     l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
10011                   (l_tccp->numresolutions * 3 - 2);
10012 
10013     if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT)  {
10014         l_header_size = 1 + l_num_bands;
10015 
10016         if (*p_header_size < l_header_size) {
10017             opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10018             return OPJ_FALSE;
10019         }
10020 
10021         opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5),
10022                         1);   /* Sqcx */
10023         ++p_data;
10024 
10025         for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10026             l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
10027             opj_write_bytes(p_data, l_expn << 3, 1);        /* SPqcx_i */
10028             ++p_data;
10029         }
10030     } else {
10031         l_header_size = 1 + 2 * l_num_bands;
10032 
10033         if (*p_header_size < l_header_size) {
10034             opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10035             return OPJ_FALSE;
10036         }
10037 
10038         opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5),
10039                         1);   /* Sqcx */
10040         ++p_data;
10041 
10042         for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10043             l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
10044             l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
10045 
10046             opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2);    /* SPqcx_i */
10047             p_data += 2;
10048         }
10049     }
10050 
10051     *p_header_size = *p_header_size - l_header_size;
10052 
10053     return OPJ_TRUE;
10054 }
10055 
opj_j2k_read_SQcd_SQcc(opj_j2k_t * p_j2k,OPJ_UINT32 p_comp_no,OPJ_BYTE * p_header_data,OPJ_UINT32 * p_header_size,opj_event_mgr_t * p_manager)10056 static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
10057                                        OPJ_UINT32 p_comp_no,
10058                                        OPJ_BYTE* p_header_data,
10059                                        OPJ_UINT32 * p_header_size,
10060                                        opj_event_mgr_t * p_manager
10061                                       )
10062 {
10063     /* loop*/
10064     OPJ_UINT32 l_band_no;
10065     opj_cp_t *l_cp = 00;
10066     opj_tcp_t *l_tcp = 00;
10067     opj_tccp_t *l_tccp = 00;
10068     OPJ_BYTE * l_current_ptr = 00;
10069     OPJ_UINT32 l_tmp, l_num_band;
10070 
10071     /* preconditions*/
10072     assert(p_j2k != 00);
10073     assert(p_manager != 00);
10074     assert(p_header_data != 00);
10075 
10076     l_cp = &(p_j2k->m_cp);
10077     /* come from tile part header or main header ?*/
10078     l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
10079             ?
10080             &l_cp->tcps[p_j2k->m_current_tile_number] :
10081             p_j2k->m_specific_param.m_decoder.m_default_tcp;
10082 
10083     /* precondition again*/
10084     if (p_comp_no >=  p_j2k->m_private_image->numcomps) {
10085         return OPJ_FALSE;
10086     }
10087 
10088     l_tccp = &l_tcp->tccps[p_comp_no];
10089     l_current_ptr = p_header_data;
10090 
10091     if (*p_header_size < 1) {
10092         opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10093         return OPJ_FALSE;
10094     }
10095     *p_header_size -= 1;
10096 
10097     opj_read_bytes(l_current_ptr, &l_tmp, 1);                       /* Sqcx */
10098     ++l_current_ptr;
10099 
10100     l_tccp->qntsty = l_tmp & 0x1f;
10101     l_tccp->numgbits = l_tmp >> 5;
10102     if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10103         l_num_band = 1;
10104     } else {
10105         l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10106                      (*p_header_size) :
10107                      (*p_header_size) / 2;
10108 
10109         if (l_num_band > OPJ_J2K_MAXBANDS) {
10110             opj_event_msg(p_manager, EVT_WARNING,
10111                           "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10112                           "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10113                           "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS,
10114                           OPJ_J2K_MAXBANDS);
10115             /*return OPJ_FALSE;*/
10116         }
10117     }
10118 
10119 #ifdef USE_JPWL
10120     if (l_cp->correct) {
10121 
10122         /* if JPWL is on, we check whether there are too many subbands */
10123         if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) {
10124             opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10125                           "JPWL: bad number of subbands in Sqcx (%d)\n",
10126                           l_num_band);
10127             if (!JPWL_ASSUME) {
10128                 opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
10129                 return OPJ_FALSE;
10130             }
10131             /* we try to correct */
10132             l_num_band = 1;
10133             opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"
10134                           "- setting number of bands to %d => HYPOTHESIS!!!\n",
10135                           l_num_band);
10136         };
10137 
10138     };
10139 #endif /* USE_JPWL */
10140 
10141     if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10142         for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10143             opj_read_bytes(l_current_ptr, &l_tmp, 1);                       /* SPqcx_i */
10144             ++l_current_ptr;
10145             if (l_band_no < OPJ_J2K_MAXBANDS) {
10146                 l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
10147                 l_tccp->stepsizes[l_band_no].mant = 0;
10148             }
10149         }
10150         *p_header_size = *p_header_size - l_num_band;
10151     } else {
10152         for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10153             opj_read_bytes(l_current_ptr, &l_tmp, 2);                       /* SPqcx_i */
10154             l_current_ptr += 2;
10155             if (l_band_no < OPJ_J2K_MAXBANDS) {
10156                 l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
10157                 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10158             }
10159         }
10160         *p_header_size = *p_header_size - 2 * l_num_band;
10161     }
10162 
10163     /* Add Antonin : if scalar_derived -> compute other stepsizes */
10164     if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10165         for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
10166             l_tccp->stepsizes[l_band_no].expn =
10167                 ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0)
10168                 ?
10169                 (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
10170             l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10171         }
10172     }
10173 
10174     return OPJ_TRUE;
10175 }
10176 
opj_j2k_copy_tile_quantization_parameters(opj_j2k_t * p_j2k)10177 static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k)
10178 {
10179     OPJ_UINT32 i;
10180     opj_cp_t *l_cp = NULL;
10181     opj_tcp_t *l_tcp = NULL;
10182     opj_tccp_t *l_ref_tccp = NULL;
10183     opj_tccp_t *l_copied_tccp = NULL;
10184     OPJ_UINT32 l_size;
10185 
10186     /* preconditions */
10187     assert(p_j2k != 00);
10188 
10189     l_cp = &(p_j2k->m_cp);
10190     l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10191             &l_cp->tcps[p_j2k->m_current_tile_number] :
10192             p_j2k->m_specific_param.m_decoder.m_default_tcp;
10193 
10194     l_ref_tccp = &l_tcp->tccps[0];
10195     l_copied_tccp = l_ref_tccp + 1;
10196     l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);
10197 
10198     for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) {
10199         l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10200         l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10201         memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size);
10202         ++l_copied_tccp;
10203     }
10204 }
10205 
opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE * out_stream)10206 static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile,
10207                                    OPJ_INT32 numcomps, FILE* out_stream)
10208 {
10209     if (l_default_tile) {
10210         OPJ_INT32 compno;
10211 
10212         fprintf(out_stream, "\t default tile {\n");
10213         fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10214         fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10215         fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10216         fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10217 
10218         for (compno = 0; compno < numcomps; compno++) {
10219             opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10220             OPJ_UINT32 resno;
10221             OPJ_INT32 bandno, numbands;
10222 
10223             /* coding style*/
10224             fprintf(out_stream, "\t\t comp %d {\n", compno);
10225             fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10226             fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10227             fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10228             fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10229             fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10230             fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10231 
10232             fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10233             for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10234                 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10235             }
10236             fprintf(out_stream, "\n");
10237 
10238             /* quantization style*/
10239             fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10240             fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10241             fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10242             numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
10243                        (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
10244             for (bandno = 0; bandno < numbands; bandno++) {
10245                 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10246                         l_tccp->stepsizes[bandno].expn);
10247             }
10248             fprintf(out_stream, "\n");
10249 
10250             /* RGN value*/
10251             fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10252 
10253             fprintf(out_stream, "\t\t }\n");
10254         } /*end of component of default tile*/
10255         fprintf(out_stream, "\t }\n"); /*end of default tile*/
10256     }
10257 }
10258 
j2k_dump(opj_j2k_t * p_j2k,OPJ_INT32 flag,FILE * out_stream)10259 void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10260 {
10261     /* Check if the flag is compatible with j2k file*/
10262     if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) {
10263         fprintf(out_stream, "Wrong flag\n");
10264         return;
10265     }
10266 
10267     /* Dump the image_header */
10268     if (flag & OPJ_IMG_INFO) {
10269         if (p_j2k->m_private_image) {
10270             j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10271         }
10272     }
10273 
10274     /* Dump the codestream info from main header */
10275     if (flag & OPJ_J2K_MH_INFO) {
10276         if (p_j2k->m_private_image) {
10277             opj_j2k_dump_MH_info(p_j2k, out_stream);
10278         }
10279     }
10280     /* Dump all tile/codestream info */
10281     if (flag & OPJ_J2K_TCH_INFO) {
10282         OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10283         OPJ_UINT32 i;
10284         opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
10285         if (p_j2k->m_private_image) {
10286             for (i = 0; i < l_nb_tiles; ++i) {
10287                 opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps,
10288                                        out_stream);
10289                 ++l_tcp;
10290             }
10291         }
10292     }
10293 
10294     /* Dump the codestream info of the current tile */
10295     if (flag & OPJ_J2K_TH_INFO) {
10296 
10297     }
10298 
10299     /* Dump the codestream index from main header */
10300     if (flag & OPJ_J2K_MH_IND) {
10301         opj_j2k_dump_MH_index(p_j2k, out_stream);
10302     }
10303 
10304     /* Dump the codestream index of the current tile */
10305     if (flag & OPJ_J2K_TH_IND) {
10306 
10307     }
10308 
10309 }
10310 
opj_j2k_dump_MH_index(opj_j2k_t * p_j2k,FILE * out_stream)10311 static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
10312 {
10313     opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10314     OPJ_UINT32 it_marker, it_tile, it_tile_part;
10315 
10316     fprintf(out_stream, "Codestream index from main header: {\n");
10317 
10318     fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10319             "\t Main header end position=%" PRIi64 "\n",
10320             cstr_index->main_head_start, cstr_index->main_head_end);
10321 
10322     fprintf(out_stream, "\t Marker list: {\n");
10323 
10324     if (cstr_index->marker) {
10325         for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) {
10326             fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10327                     cstr_index->marker[it_marker].type,
10328                     cstr_index->marker[it_marker].pos,
10329                     cstr_index->marker[it_marker].len);
10330         }
10331     }
10332 
10333     fprintf(out_stream, "\t }\n");
10334 
10335     if (cstr_index->tile_index) {
10336 
10337         /* Simple test to avoid to write empty information*/
10338         OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10339         for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) {
10340             l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10341         }
10342 
10343         if (l_acc_nb_of_tile_part) {
10344             fprintf(out_stream, "\t Tile index: {\n");
10345 
10346             for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) {
10347                 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10348 
10349                 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile,
10350                         nb_of_tile_part);
10351 
10352                 if (cstr_index->tile_index[it_tile].tp_index) {
10353                     for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) {
10354                         fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%"
10355                                 PRIi64 ", end_pos=%" PRIi64 ".\n",
10356                                 it_tile_part,
10357                                 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10358                                 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10359                                 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10360                     }
10361                 }
10362 
10363                 if (cstr_index->tile_index[it_tile].marker) {
10364                     for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ;
10365                             it_marker++) {
10366                         fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10367                                 cstr_index->tile_index[it_tile].marker[it_marker].type,
10368                                 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10369                                 cstr_index->tile_index[it_tile].marker[it_marker].len);
10370                     }
10371                 }
10372             }
10373             fprintf(out_stream, "\t }\n");
10374         }
10375     }
10376 
10377     fprintf(out_stream, "}\n");
10378 
10379 }
10380 
10381 
opj_j2k_dump_MH_info(opj_j2k_t * p_j2k,FILE * out_stream)10382 static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
10383 {
10384 
10385     fprintf(out_stream, "Codestream info from main header: {\n");
10386 
10387     fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10388     fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10389     fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10390     opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,
10391                            (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
10392     fprintf(out_stream, "}\n");
10393 }
10394 
j2k_dump_image_header(opj_image_t * img_header,OPJ_BOOL dev_dump_flag,FILE * out_stream)10395 void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag,
10396                            FILE* out_stream)
10397 {
10398     char tab[2];
10399 
10400     if (dev_dump_flag) {
10401         fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10402         tab[0] = '\0';
10403     } else {
10404         fprintf(out_stream, "Image info {\n");
10405         tab[0] = '\t';
10406         tab[1] = '\0';
10407     }
10408 
10409     fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10410     fprintf(out_stream,     "%s x1=%d, y1=%d\n", tab, img_header->x1,
10411             img_header->y1);
10412     fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10413 
10414     if (img_header->comps) {
10415         OPJ_UINT32 compno;
10416         for (compno = 0; compno < img_header->numcomps; compno++) {
10417             fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10418             j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag,
10419                                        out_stream);
10420             fprintf(out_stream, "%s}\n", tab);
10421         }
10422     }
10423 
10424     fprintf(out_stream, "}\n");
10425 }
10426 
j2k_dump_image_comp_header(opj_image_comp_t * comp_header,OPJ_BOOL dev_dump_flag,FILE * out_stream)10427 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header,
10428                                 OPJ_BOOL dev_dump_flag, FILE* out_stream)
10429 {
10430     char tab[3];
10431 
10432     if (dev_dump_flag) {
10433         fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10434         tab[0] = '\0';
10435     }       else {
10436         tab[0] = '\t';
10437         tab[1] = '\t';
10438         tab[2] = '\0';
10439     }
10440 
10441     fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10442     fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10443     fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10444 
10445     if (dev_dump_flag) {
10446         fprintf(out_stream, "}\n");
10447     }
10448 }
10449 
j2k_get_cstr_info(opj_j2k_t * p_j2k)10450 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
10451 {
10452     OPJ_UINT32 compno;
10453     OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;
10454     opj_tcp_t *l_default_tile;
10455     opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,
10456                                           sizeof(opj_codestream_info_v2_t));
10457     if (!cstr_info) {
10458         return NULL;
10459     }
10460 
10461     cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10462 
10463     cstr_info->tx0 = p_j2k->m_cp.tx0;
10464     cstr_info->ty0 = p_j2k->m_cp.ty0;
10465     cstr_info->tdx = p_j2k->m_cp.tdx;
10466     cstr_info->tdy = p_j2k->m_cp.tdy;
10467     cstr_info->tw = p_j2k->m_cp.tw;
10468     cstr_info->th = p_j2k->m_cp.th;
10469 
10470     cstr_info->tile_info = NULL; /* Not fill from the main header*/
10471 
10472     l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10473 
10474     cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10475     cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10476     cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10477     cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10478 
10479     cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(
10480                 cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10481     if (!cstr_info->m_default_tile_info.tccp_info) {
10482         opj_destroy_cstr_info(&cstr_info);
10483         return NULL;
10484     }
10485 
10486     for (compno = 0; compno < numcomps; compno++) {
10487         opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10488         opj_tccp_info_t *l_tccp_info = &
10489                                        (cstr_info->m_default_tile_info.tccp_info[compno]);
10490         OPJ_INT32 bandno, numbands;
10491 
10492         /* coding style*/
10493         l_tccp_info->csty = l_tccp->csty;
10494         l_tccp_info->numresolutions = l_tccp->numresolutions;
10495         l_tccp_info->cblkw = l_tccp->cblkw;
10496         l_tccp_info->cblkh = l_tccp->cblkh;
10497         l_tccp_info->cblksty = l_tccp->cblksty;
10498         l_tccp_info->qmfbid = l_tccp->qmfbid;
10499         if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) {
10500             memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10501             memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10502         }
10503 
10504         /* quantization style*/
10505         l_tccp_info->qntsty = l_tccp->qntsty;
10506         l_tccp_info->numgbits = l_tccp->numgbits;
10507 
10508         numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
10509                    (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
10510         if (numbands < OPJ_J2K_MAXBANDS) {
10511             for (bandno = 0; bandno < numbands; bandno++) {
10512                 l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)
10513                                                       l_tccp->stepsizes[bandno].mant;
10514                 l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)
10515                                                       l_tccp->stepsizes[bandno].expn;
10516             }
10517         }
10518 
10519         /* RGN value*/
10520         l_tccp_info->roishift = l_tccp->roishift;
10521     }
10522 
10523     return cstr_info;
10524 }
10525 
j2k_get_cstr_index(opj_j2k_t * p_j2k)10526 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
10527 {
10528     opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10529                                            opj_calloc(1, sizeof(opj_codestream_index_t));
10530     if (!l_cstr_index) {
10531         return NULL;
10532     }
10533 
10534     l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10535     l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10536     l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10537 
10538     l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10539     l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum *
10540                            sizeof(opj_marker_info_t));
10541     if (!l_cstr_index->marker) {
10542         opj_free(l_cstr_index);
10543         return NULL;
10544     }
10545 
10546     if (p_j2k->cstr_index->marker) {
10547         memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker,
10548                l_cstr_index->marknum * sizeof(opj_marker_info_t));
10549     } else {
10550         opj_free(l_cstr_index->marker);
10551         l_cstr_index->marker = NULL;
10552     }
10553 
10554     l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10555     l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(
10556                                    l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10557     if (!l_cstr_index->tile_index) {
10558         opj_free(l_cstr_index->marker);
10559         opj_free(l_cstr_index);
10560         return NULL;
10561     }
10562 
10563     if (!p_j2k->cstr_index->tile_index) {
10564         opj_free(l_cstr_index->tile_index);
10565         l_cstr_index->tile_index = NULL;
10566     } else {
10567         OPJ_UINT32 it_tile = 0;
10568         for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) {
10569 
10570             /* Tile Marker*/
10571             l_cstr_index->tile_index[it_tile].marknum =
10572                 p_j2k->cstr_index->tile_index[it_tile].marknum;
10573 
10574             l_cstr_index->tile_index[it_tile].marker =
10575                 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum *
10576                                                sizeof(opj_marker_info_t));
10577 
10578             if (!l_cstr_index->tile_index[it_tile].marker) {
10579                 OPJ_UINT32 it_tile_free;
10580 
10581                 for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) {
10582                     opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10583                 }
10584 
10585                 opj_free(l_cstr_index->tile_index);
10586                 opj_free(l_cstr_index->marker);
10587                 opj_free(l_cstr_index);
10588                 return NULL;
10589             }
10590 
10591             if (p_j2k->cstr_index->tile_index[it_tile].marker)
10592                 memcpy(l_cstr_index->tile_index[it_tile].marker,
10593                        p_j2k->cstr_index->tile_index[it_tile].marker,
10594                        l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t));
10595             else {
10596                 opj_free(l_cstr_index->tile_index[it_tile].marker);
10597                 l_cstr_index->tile_index[it_tile].marker = NULL;
10598             }
10599 
10600             /* Tile part index*/
10601             l_cstr_index->tile_index[it_tile].nb_tps =
10602                 p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10603 
10604             l_cstr_index->tile_index[it_tile].tp_index =
10605                 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof(
10606                                                 opj_tp_index_t));
10607 
10608             if (!l_cstr_index->tile_index[it_tile].tp_index) {
10609                 OPJ_UINT32 it_tile_free;
10610 
10611                 for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) {
10612                     opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10613                     opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10614                 }
10615 
10616                 opj_free(l_cstr_index->tile_index);
10617                 opj_free(l_cstr_index->marker);
10618                 opj_free(l_cstr_index);
10619                 return NULL;
10620             }
10621 
10622             if (p_j2k->cstr_index->tile_index[it_tile].tp_index) {
10623                 memcpy(l_cstr_index->tile_index[it_tile].tp_index,
10624                        p_j2k->cstr_index->tile_index[it_tile].tp_index,
10625                        l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t));
10626             } else {
10627                 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10628                 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10629             }
10630 
10631             /* Packet index (NOT USED)*/
10632             l_cstr_index->tile_index[it_tile].nb_packet = 0;
10633             l_cstr_index->tile_index[it_tile].packet_index = NULL;
10634 
10635         }
10636     }
10637 
10638     return l_cstr_index;
10639 }
10640 
opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t * p_j2k)10641 static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
10642 {
10643     OPJ_UINT32 it_tile = 0;
10644 
10645     p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10646     p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(
10647                                         p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10648     if (!p_j2k->cstr_index->tile_index) {
10649         return OPJ_FALSE;
10650     }
10651 
10652     for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) {
10653         p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10654         p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10655         p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10656                 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum,
10657                            sizeof(opj_marker_info_t));
10658         if (!p_j2k->cstr_index->tile_index[it_tile].marker) {
10659             return OPJ_FALSE;
10660         }
10661     }
10662 
10663     return OPJ_TRUE;
10664 }
10665 
opj_j2k_decode_tiles(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10666 static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
10667                                      opj_stream_private_t *p_stream,
10668                                      opj_event_mgr_t * p_manager)
10669 {
10670     OPJ_BOOL l_go_on = OPJ_TRUE;
10671     OPJ_UINT32 l_current_tile_no;
10672     OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1;
10673     OPJ_UINT32 l_nb_comps;
10674     OPJ_UINT32 nr_tiles = 0;
10675 
10676     /* Particular case for whole single tile decoding */
10677     /* We can avoid allocating intermediate tile buffers */
10678     if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
10679             p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 &&
10680             p_j2k->m_output_image->x0 == 0 &&
10681             p_j2k->m_output_image->y0 == 0 &&
10682             p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx &&
10683             p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) {
10684         OPJ_UINT32 i;
10685         if (! opj_j2k_read_tile_header(p_j2k,
10686                                        &l_current_tile_no,
10687                                        NULL,
10688                                        &l_tile_x0, &l_tile_y0,
10689                                        &l_tile_x1, &l_tile_y1,
10690                                        &l_nb_comps,
10691                                        &l_go_on,
10692                                        p_stream,
10693                                        p_manager)) {
10694             return OPJ_FALSE;
10695         }
10696 
10697         if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
10698                                   p_stream, p_manager)) {
10699             opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n");
10700             return OPJ_FALSE;
10701         }
10702 
10703         /* Transfer TCD data to output image data */
10704         for (i = 0; i < p_j2k->m_output_image->numcomps; i++) {
10705             opj_image_data_free(p_j2k->m_output_image->comps[i].data);
10706             p_j2k->m_output_image->comps[i].data =
10707                 p_j2k->m_tcd->tcd_image->tiles->comps[i].data;
10708             p_j2k->m_output_image->comps[i].resno_decoded =
10709                 p_j2k->m_tcd->image->comps[i].resno_decoded;
10710             p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL;
10711         }
10712 
10713         return OPJ_TRUE;
10714     }
10715 
10716     for (;;) {
10717         if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
10718                 p_j2k->m_cp.tcps[0].m_data != NULL) {
10719             l_current_tile_no = 0;
10720             p_j2k->m_current_tile_number = 0;
10721             p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA;
10722         } else {
10723             if (! opj_j2k_read_tile_header(p_j2k,
10724                                            &l_current_tile_no,
10725                                            NULL,
10726                                            &l_tile_x0, &l_tile_y0,
10727                                            &l_tile_x1, &l_tile_y1,
10728                                            &l_nb_comps,
10729                                            &l_go_on,
10730                                            p_stream,
10731                                            p_manager)) {
10732                 return OPJ_FALSE;
10733             }
10734 
10735             if (! l_go_on) {
10736                 break;
10737             }
10738         }
10739 
10740         if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
10741                                   p_stream, p_manager)) {
10742             opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n",
10743                           l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
10744             return OPJ_FALSE;
10745         }
10746 
10747         opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n",
10748                       l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
10749 
10750         if (! opj_j2k_update_image_data(p_j2k->m_tcd,
10751                                         p_j2k->m_output_image)) {
10752             return OPJ_FALSE;
10753         }
10754 
10755         if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
10756                 !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 &&
10757                   p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 &&
10758                   p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 &&
10759                   p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) {
10760             /* Keep current tcp data */
10761         } else {
10762             opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]);
10763         }
10764 
10765         opj_event_msg(p_manager, EVT_INFO,
10766                       "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10767 
10768         if (opj_stream_get_number_byte_left(p_stream) == 0
10769                 && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
10770             break;
10771         }
10772         if (++nr_tiles ==  p_j2k->m_cp.th * p_j2k->m_cp.tw) {
10773             break;
10774         }
10775     }
10776 
10777     return OPJ_TRUE;
10778 }
10779 
10780 /**
10781  * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10782  */
opj_j2k_setup_decoding(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)10783 static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k,
10784                                        opj_event_mgr_t * p_manager)
10785 {
10786     /* preconditions*/
10787     assert(p_j2k != 00);
10788     assert(p_manager != 00);
10789 
10790     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
10791                                            (opj_procedure)opj_j2k_decode_tiles, p_manager)) {
10792         return OPJ_FALSE;
10793     }
10794     /* DEVELOPER CORNER, add your custom procedures */
10795 
10796     return OPJ_TRUE;
10797 }
10798 
10799 /*
10800  * Read and decode one tile.
10801  */
opj_j2k_decode_one_tile(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)10802 static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k,
10803                                         opj_stream_private_t *p_stream,
10804                                         opj_event_mgr_t * p_manager)
10805 {
10806     OPJ_BOOL l_go_on = OPJ_TRUE;
10807     OPJ_UINT32 l_current_tile_no;
10808     OPJ_UINT32 l_tile_no_to_dec;
10809     OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1;
10810     OPJ_UINT32 l_nb_comps;
10811     OPJ_UINT32 l_nb_tiles;
10812     OPJ_UINT32 i;
10813 
10814     /*Allocate and initialize some elements of codestrem index if not already done*/
10815     if (!p_j2k->cstr_index->tile_index) {
10816         if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
10817             return OPJ_FALSE;
10818         }
10819     }
10820     /* Move into the codestream to the first SOT used to decode the desired tile */
10821     l_tile_no_to_dec = (OPJ_UINT32)
10822                        p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10823     if (p_j2k->cstr_index->tile_index)
10824         if (p_j2k->cstr_index->tile_index->tp_index) {
10825             if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10826                 /* the index for this tile has not been built,
10827                  *  so move to the last SOT read */
10828                 if (!(opj_stream_read_seek(p_stream,
10829                                            p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) {
10830                     opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
10831                     return OPJ_FALSE;
10832                 }
10833             } else {
10834                 if (!(opj_stream_read_seek(p_stream,
10835                                            p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos + 2,
10836                                            p_manager))) {
10837                     opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
10838                     return OPJ_FALSE;
10839                 }
10840             }
10841             /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10842             if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) {
10843                 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10844             }
10845         }
10846 
10847     /* Reset current tile part number for all tiles, and not only the one */
10848     /* of interest. */
10849     /* Not completely sure this is always correct but required for */
10850     /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */
10851     l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10852     for (i = 0; i < l_nb_tiles; ++i) {
10853         p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1;
10854     }
10855 
10856     for (;;) {
10857         if (! opj_j2k_read_tile_header(p_j2k,
10858                                        &l_current_tile_no,
10859                                        NULL,
10860                                        &l_tile_x0, &l_tile_y0,
10861                                        &l_tile_x1, &l_tile_y1,
10862                                        &l_nb_comps,
10863                                        &l_go_on,
10864                                        p_stream,
10865                                        p_manager)) {
10866             return OPJ_FALSE;
10867         }
10868 
10869         if (! l_go_on) {
10870             break;
10871         }
10872 
10873         if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
10874                                   p_stream, p_manager)) {
10875             return OPJ_FALSE;
10876         }
10877         opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n",
10878                       l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
10879 
10880         if (! opj_j2k_update_image_data(p_j2k->m_tcd,
10881                                         p_j2k->m_output_image)) {
10882             return OPJ_FALSE;
10883         }
10884         opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]);
10885 
10886         opj_event_msg(p_manager, EVT_INFO,
10887                       "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10888 
10889         if (l_current_tile_no == l_tile_no_to_dec) {
10890             /* move into the codestream to the first SOT (FIXME or not move?)*/
10891             if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2,
10892                                        p_manager))) {
10893                 opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
10894                 return OPJ_FALSE;
10895             }
10896             break;
10897         } else {
10898             opj_event_msg(p_manager, EVT_WARNING,
10899                           "Tile read, decoded and updated is not the desired one (%d vs %d).\n",
10900                           l_current_tile_no + 1, l_tile_no_to_dec + 1);
10901         }
10902 
10903     }
10904 
10905     return OPJ_TRUE;
10906 }
10907 
10908 /**
10909  * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10910  */
opj_j2k_setup_decoding_tile(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)10911 static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k,
10912         opj_event_mgr_t * p_manager)
10913 {
10914     /* preconditions*/
10915     assert(p_j2k != 00);
10916     assert(p_manager != 00);
10917 
10918     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
10919                                            (opj_procedure)opj_j2k_decode_one_tile, p_manager)) {
10920         return OPJ_FALSE;
10921     }
10922     /* DEVELOPER CORNER, add your custom procedures */
10923 
10924     return OPJ_TRUE;
10925 }
10926 
opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k,opj_image_t * p_image)10927 static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k,
10928         opj_image_t * p_image)
10929 {
10930     OPJ_UINT32 compno;
10931 
10932     /* Move data and copy one information from codec to output image*/
10933     if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) {
10934         opj_image_comp_t* newcomps =
10935             (opj_image_comp_t*) opj_malloc(
10936                 p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode *
10937                 sizeof(opj_image_comp_t));
10938         if (newcomps == NULL) {
10939             opj_image_destroy(p_j2k->m_private_image);
10940             p_j2k->m_private_image = NULL;
10941             return OPJ_FALSE;
10942         }
10943         for (compno = 0; compno < p_image->numcomps; compno++) {
10944             opj_image_data_free(p_image->comps[compno].data);
10945             p_image->comps[compno].data = NULL;
10946         }
10947         for (compno = 0;
10948                 compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
10949             OPJ_UINT32 src_compno =
10950                 p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
10951             memcpy(&(newcomps[compno]),
10952                    &(p_j2k->m_output_image->comps[src_compno]),
10953                    sizeof(opj_image_comp_t));
10954             newcomps[compno].resno_decoded =
10955                 p_j2k->m_output_image->comps[src_compno].resno_decoded;
10956             newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data;
10957             p_j2k->m_output_image->comps[src_compno].data = NULL;
10958         }
10959         for (compno = 0; compno < p_image->numcomps; compno++) {
10960             assert(p_j2k->m_output_image->comps[compno].data == NULL);
10961             opj_image_data_free(p_j2k->m_output_image->comps[compno].data);
10962             p_j2k->m_output_image->comps[compno].data = NULL;
10963         }
10964         p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode;
10965         opj_free(p_image->comps);
10966         p_image->comps = newcomps;
10967     } else {
10968         for (compno = 0; compno < p_image->numcomps; compno++) {
10969             p_image->comps[compno].resno_decoded =
10970                 p_j2k->m_output_image->comps[compno].resno_decoded;
10971             opj_image_data_free(p_image->comps[compno].data);
10972             p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10973 #if 0
10974             char fn[256];
10975             sprintf(fn, "/tmp/%d.raw", compno);
10976             FILE *debug = fopen(fn, "wb");
10977             fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32),
10978                    p_image->comps[compno].w * p_image->comps[compno].h, debug);
10979             fclose(debug);
10980 #endif
10981             p_j2k->m_output_image->comps[compno].data = NULL;
10982         }
10983     }
10984     return OPJ_TRUE;
10985 }
10986 
opj_j2k_decode(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)10987 OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
10988                         opj_stream_private_t * p_stream,
10989                         opj_image_t * p_image,
10990                         opj_event_mgr_t * p_manager)
10991 {
10992     if (!p_image) {
10993         return OPJ_FALSE;
10994     }
10995 
10996     /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */
10997     /* and finally opj_decode_image() without manual setting of comps[].factor */
10998     /* We could potentially always execute it, if we don't allow people to do */
10999     /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */
11000     if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 &&
11001             p_j2k->m_private_image != NULL &&
11002             p_j2k->m_private_image->numcomps > 0 &&
11003             p_j2k->m_private_image->comps[0].factor ==
11004             p_j2k->m_cp.m_specific_param.m_dec.m_reduce &&
11005             p_image->numcomps > 0 &&
11006             p_image->comps[0].factor == 0 &&
11007             /* Don't mess with image dimension if the user has allocated it */
11008             p_image->comps[0].data == NULL) {
11009         OPJ_UINT32 it_comp;
11010 
11011         /* Update the comps[].factor member of the output image with the one */
11012         /* of m_reduce */
11013         for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
11014             p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce;
11015         }
11016         if (!opj_j2k_update_image_dimensions(p_image, p_manager)) {
11017             return OPJ_FALSE;
11018         }
11019     }
11020 
11021     if (p_j2k->m_output_image == NULL) {
11022         p_j2k->m_output_image = opj_image_create0();
11023         if (!(p_j2k->m_output_image)) {
11024             return OPJ_FALSE;
11025         }
11026     }
11027     opj_copy_image_header(p_image, p_j2k->m_output_image);
11028 
11029     /* customization of the decoding */
11030     if (!opj_j2k_setup_decoding(p_j2k, p_manager)) {
11031         return OPJ_FALSE;
11032     }
11033 
11034     /* Decode the codestream */
11035     if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
11036         opj_image_destroy(p_j2k->m_private_image);
11037         p_j2k->m_private_image = NULL;
11038         return OPJ_FALSE;
11039     }
11040 
11041     /* Move data and copy one information from codec to output image*/
11042     return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image);
11043 }
11044 
opj_j2k_get_tile(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager,OPJ_UINT32 tile_index)11045 OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
11046                           opj_stream_private_t *p_stream,
11047                           opj_image_t* p_image,
11048                           opj_event_mgr_t * p_manager,
11049                           OPJ_UINT32 tile_index)
11050 {
11051     OPJ_UINT32 compno;
11052     OPJ_UINT32 l_tile_x, l_tile_y;
11053     opj_image_comp_t* l_img_comp;
11054 
11055     if (!p_image) {
11056         opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");
11057         return OPJ_FALSE;
11058     }
11059 
11060     if (p_image->numcomps < p_j2k->m_private_image->numcomps) {
11061         opj_event_msg(p_manager, EVT_ERROR,
11062                       "Image has less components than codestream.\n");
11063         return OPJ_FALSE;
11064     }
11065 
11066     if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) {
11067         opj_event_msg(p_manager, EVT_ERROR,
11068                       "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index,
11069                       (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
11070         return OPJ_FALSE;
11071     }
11072 
11073     /* Compute the dimension of the desired tile*/
11074     l_tile_x = tile_index % p_j2k->m_cp.tw;
11075     l_tile_y = tile_index / p_j2k->m_cp.tw;
11076 
11077     p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11078     if (p_image->x0 < p_j2k->m_private_image->x0) {
11079         p_image->x0 = p_j2k->m_private_image->x0;
11080     }
11081     p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11082     if (p_image->x1 > p_j2k->m_private_image->x1) {
11083         p_image->x1 = p_j2k->m_private_image->x1;
11084     }
11085 
11086     p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11087     if (p_image->y0 < p_j2k->m_private_image->y0) {
11088         p_image->y0 = p_j2k->m_private_image->y0;
11089     }
11090     p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11091     if (p_image->y1 > p_j2k->m_private_image->y1) {
11092         p_image->y1 = p_j2k->m_private_image->y1;
11093     }
11094 
11095     l_img_comp = p_image->comps;
11096     for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
11097         OPJ_INT32 l_comp_x1, l_comp_y1;
11098 
11099         l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11100 
11101         l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
11102         l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy);
11103         l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
11104         l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
11105 
11106         l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1,
11107                                      (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0,
11108                                              (OPJ_INT32)l_img_comp->factor));
11109         l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1,
11110                                      (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0,
11111                                              (OPJ_INT32)l_img_comp->factor));
11112 
11113         l_img_comp++;
11114     }
11115 
11116     if (p_image->numcomps > p_j2k->m_private_image->numcomps) {
11117         /* Can happen when calling repeatdly opj_get_decoded_tile() on an
11118          * image with a color palette, where color palette expansion is done
11119          * later in jp2.c */
11120         for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps;
11121                 ++compno) {
11122             opj_image_data_free(p_image->comps[compno].data);
11123             p_image->comps[compno].data = NULL;
11124         }
11125         p_image->numcomps = p_j2k->m_private_image->numcomps;
11126     }
11127 
11128     /* Destroy the previous output image*/
11129     if (p_j2k->m_output_image) {
11130         opj_image_destroy(p_j2k->m_output_image);
11131     }
11132 
11133     /* Create the ouput image from the information previously computed*/
11134     p_j2k->m_output_image = opj_image_create0();
11135     if (!(p_j2k->m_output_image)) {
11136         return OPJ_FALSE;
11137     }
11138     opj_copy_image_header(p_image, p_j2k->m_output_image);
11139 
11140     p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
11141 
11142     /* customization of the decoding */
11143     if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) {
11144         return OPJ_FALSE;
11145     }
11146 
11147     /* Decode the codestream */
11148     if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
11149         opj_image_destroy(p_j2k->m_private_image);
11150         p_j2k->m_private_image = NULL;
11151         return OPJ_FALSE;
11152     }
11153 
11154     /* Move data and copy one information from codec to output image*/
11155     return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image);
11156 }
11157 
opj_j2k_set_decoded_resolution_factor(opj_j2k_t * p_j2k,OPJ_UINT32 res_factor,opj_event_mgr_t * p_manager)11158 OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
11159         OPJ_UINT32 res_factor,
11160         opj_event_mgr_t * p_manager)
11161 {
11162     OPJ_UINT32 it_comp;
11163 
11164     p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11165 
11166     if (p_j2k->m_private_image) {
11167         if (p_j2k->m_private_image->comps) {
11168             if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11169                 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11170                     for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11171                         OPJ_UINT32 max_res =
11172                             p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11173                         if (res_factor >= max_res) {
11174                             opj_event_msg(p_manager, EVT_ERROR,
11175                                           "Resolution factor is greater than the maximum resolution in the component.\n");
11176                             return OPJ_FALSE;
11177                         }
11178                         p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11179                     }
11180                     return OPJ_TRUE;
11181                 }
11182             }
11183         }
11184     }
11185 
11186     return OPJ_FALSE;
11187 }
11188 
opj_j2k_encode(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)11189 OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
11190                         opj_stream_private_t *p_stream,
11191                         opj_event_mgr_t * p_manager)
11192 {
11193     OPJ_UINT32 i, j;
11194     OPJ_UINT32 l_nb_tiles;
11195     OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size;
11196     OPJ_BYTE * l_current_data = 00;
11197     OPJ_BOOL l_reuse_data = OPJ_FALSE;
11198     opj_tcd_t* p_tcd = 00;
11199 
11200     /* preconditions */
11201     assert(p_j2k != 00);
11202     assert(p_stream != 00);
11203     assert(p_manager != 00);
11204 
11205     p_tcd = p_j2k->m_tcd;
11206 
11207     l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11208     if (l_nb_tiles == 1) {
11209         l_reuse_data = OPJ_TRUE;
11210 #ifdef __SSE__
11211         for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
11212             opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
11213             if (((size_t)l_img_comp->data & 0xFU) !=
11214                     0U) { /* tile data shall be aligned on 16 bytes */
11215                 l_reuse_data = OPJ_FALSE;
11216             }
11217         }
11218 #endif
11219     }
11220     for (i = 0; i < l_nb_tiles; ++i) {
11221         if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) {
11222             if (l_current_data) {
11223                 opj_free(l_current_data);
11224             }
11225             return OPJ_FALSE;
11226         }
11227 
11228         /* if we only have one tile, then simply set tile component data equal to image component data */
11229         /* otherwise, allocate the data */
11230         for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
11231             opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j;
11232             if (l_reuse_data) {
11233                 opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
11234                 l_tilec->data  =  l_img_comp->data;
11235                 l_tilec->ownsData = OPJ_FALSE;
11236             } else {
11237                 if (! opj_alloc_tile_component_data(l_tilec)) {
11238                     opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data.");
11239                     if (l_current_data) {
11240                         opj_free(l_current_data);
11241                     }
11242                     return OPJ_FALSE;
11243                 }
11244             }
11245         }
11246         l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
11247         if (!l_reuse_data) {
11248             if (l_current_tile_size > l_max_tile_size) {
11249                 OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data,
11250                                                l_current_tile_size);
11251                 if (! l_new_current_data) {
11252                     if (l_current_data) {
11253                         opj_free(l_current_data);
11254                     }
11255                     opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
11256                     return OPJ_FALSE;
11257                 }
11258                 l_current_data = l_new_current_data;
11259                 l_max_tile_size = l_current_tile_size;
11260             }
11261             if (l_current_data == NULL) {
11262                 /* Should not happen in practice, but will avoid Coverity to */
11263                 /* complain about a null pointer dereference */
11264                 assert(0);
11265                 return OPJ_FALSE;
11266             }
11267 
11268             /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */
11269             /* 32 bit components @ 8 bit precision get converted to 8 bit */
11270             /* 32 bit components @ 16 bit precision get converted to 16 bit */
11271             opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data);
11272 
11273             /* now copy this data into the tile component */
11274             if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data,
11275                                          l_current_tile_size)) {
11276                 opj_event_msg(p_manager, EVT_ERROR,
11277                               "Size mismatch between tile data and sent data.");
11278                 opj_free(l_current_data);
11279                 return OPJ_FALSE;
11280             }
11281         }
11282 
11283         if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) {
11284             if (l_current_data) {
11285                 opj_free(l_current_data);
11286             }
11287             return OPJ_FALSE;
11288         }
11289     }
11290 
11291     if (l_current_data) {
11292         opj_free(l_current_data);
11293     }
11294     return OPJ_TRUE;
11295 }
11296 
opj_j2k_end_compress(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)11297 OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
11298                               opj_stream_private_t *p_stream,
11299                               opj_event_mgr_t * p_manager)
11300 {
11301     /* customization of the encoding */
11302     if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) {
11303         return OPJ_FALSE;
11304     }
11305 
11306     if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
11307         return OPJ_FALSE;
11308     }
11309 
11310     return OPJ_TRUE;
11311 }
11312 
opj_j2k_start_compress(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_image_t * p_image,opj_event_mgr_t * p_manager)11313 OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
11314                                 opj_stream_private_t *p_stream,
11315                                 opj_image_t * p_image,
11316                                 opj_event_mgr_t * p_manager)
11317 {
11318     /* preconditions */
11319     assert(p_j2k != 00);
11320     assert(p_stream != 00);
11321     assert(p_manager != 00);
11322 
11323     p_j2k->m_private_image = opj_image_create0();
11324     if (! p_j2k->m_private_image) {
11325         opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header.");
11326         return OPJ_FALSE;
11327     }
11328     opj_copy_image_header(p_image, p_j2k->m_private_image);
11329 
11330     /* TODO_MSD: Find a better way */
11331     if (p_image->comps) {
11332         OPJ_UINT32 it_comp;
11333         for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11334             if (p_image->comps[it_comp].data) {
11335                 p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data;
11336                 p_image->comps[it_comp].data = NULL;
11337 
11338             }
11339         }
11340     }
11341 
11342     /* customization of the validation */
11343     if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) {
11344         return OPJ_FALSE;
11345     }
11346 
11347     /* validation of the parameters codec */
11348     if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) {
11349         return OPJ_FALSE;
11350     }
11351 
11352     /* customization of the encoding */
11353     if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) {
11354         return OPJ_FALSE;
11355     }
11356 
11357     /* write header */
11358     if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
11359         return OPJ_FALSE;
11360     }
11361 
11362     return OPJ_TRUE;
11363 }
11364 
opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,OPJ_UINT32 p_tile_index,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)11365 static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,
11366                                        OPJ_UINT32 p_tile_index,
11367                                        opj_stream_private_t *p_stream,
11368                                        opj_event_mgr_t * p_manager)
11369 {
11370     (void)p_stream;
11371     if (p_tile_index != p_j2k->m_current_tile_number) {
11372         opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match.");
11373         return OPJ_FALSE;
11374     }
11375 
11376     opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n",
11377                   p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
11378 
11379     p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11380     p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11381     p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11382 
11383     /* initialisation before tile encoding  */
11384     if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number,
11385                                    p_manager)) {
11386         return OPJ_FALSE;
11387     }
11388 
11389     return OPJ_TRUE;
11390 }
11391 
opj_get_tile_dimensions(opj_image_t * l_image,opj_tcd_tilecomp_t * l_tilec,opj_image_comp_t * l_img_comp,OPJ_UINT32 * l_size_comp,OPJ_UINT32 * l_width,OPJ_UINT32 * l_height,OPJ_UINT32 * l_offset_x,OPJ_UINT32 * l_offset_y,OPJ_UINT32 * l_image_width,OPJ_UINT32 * l_stride,OPJ_UINT32 * l_tile_offset)11392 static void opj_get_tile_dimensions(opj_image_t * l_image,
11393                                     opj_tcd_tilecomp_t * l_tilec,
11394                                     opj_image_comp_t * l_img_comp,
11395                                     OPJ_UINT32* l_size_comp,
11396                                     OPJ_UINT32* l_width,
11397                                     OPJ_UINT32* l_height,
11398                                     OPJ_UINT32* l_offset_x,
11399                                     OPJ_UINT32* l_offset_y,
11400                                     OPJ_UINT32* l_image_width,
11401                                     OPJ_UINT32* l_stride,
11402                                     OPJ_UINT32* l_tile_offset)
11403 {
11404     OPJ_UINT32 l_remaining;
11405     *l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11406     l_remaining = l_img_comp->prec & 7;  /* (%8) */
11407     if (l_remaining) {
11408         *l_size_comp += 1;
11409     }
11410 
11411     if (*l_size_comp == 3) {
11412         *l_size_comp = 4;
11413     }
11414 
11415     *l_width  = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
11416     *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
11417     *l_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx);
11418     *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy);
11419     *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 -
11420                      (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
11421     *l_stride = *l_image_width - *l_width;
11422     *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + ((
11423                          OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width;
11424 }
11425 
opj_j2k_get_tile_data(opj_tcd_t * p_tcd,OPJ_BYTE * p_data)11426 static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
11427 {
11428     OPJ_UINT32 i, j, k = 0;
11429 
11430     for (i = 0; i < p_tcd->image->numcomps; ++i) {
11431         opj_image_t * l_image =  p_tcd->image;
11432         OPJ_INT32 * l_src_ptr;
11433         opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i;
11434         opj_image_comp_t * l_img_comp = l_image->comps + i;
11435         OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y,
11436                    l_image_width, l_stride, l_tile_offset;
11437 
11438         opj_get_tile_dimensions(l_image,
11439                                 l_tilec,
11440                                 l_img_comp,
11441                                 &l_size_comp,
11442                                 &l_width,
11443                                 &l_height,
11444                                 &l_offset_x,
11445                                 &l_offset_y,
11446                                 &l_image_width,
11447                                 &l_stride,
11448                                 &l_tile_offset);
11449 
11450         l_src_ptr = l_img_comp->data + l_tile_offset;
11451 
11452         switch (l_size_comp) {
11453         case 1: {
11454             OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11455             if (l_img_comp->sgnd) {
11456                 for (j = 0; j < l_height; ++j) {
11457                     for (k = 0; k < l_width; ++k) {
11458                         *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr);
11459                         ++l_dest_ptr;
11460                         ++l_src_ptr;
11461                     }
11462                     l_src_ptr += l_stride;
11463                 }
11464             } else {
11465                 for (j = 0; j < l_height; ++j) {
11466                     for (k = 0; k < l_width; ++k) {
11467                         *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff);
11468                         ++l_dest_ptr;
11469                         ++l_src_ptr;
11470                     }
11471                     l_src_ptr += l_stride;
11472                 }
11473             }
11474 
11475             p_data = (OPJ_BYTE*) l_dest_ptr;
11476         }
11477         break;
11478         case 2: {
11479             OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11480             if (l_img_comp->sgnd) {
11481                 for (j = 0; j < l_height; ++j) {
11482                     for (k = 0; k < l_width; ++k) {
11483                         *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
11484                     }
11485                     l_src_ptr += l_stride;
11486                 }
11487             } else {
11488                 for (j = 0; j < l_height; ++j) {
11489                     for (k = 0; k < l_width; ++k) {
11490                         *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
11491                     }
11492                     l_src_ptr += l_stride;
11493                 }
11494             }
11495 
11496             p_data = (OPJ_BYTE*) l_dest_ptr;
11497         }
11498         break;
11499         case 4: {
11500             OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11501             for (j = 0; j < l_height; ++j) {
11502                 for (k = 0; k < l_width; ++k) {
11503                     *(l_dest_ptr++) = *(l_src_ptr++);
11504                 }
11505                 l_src_ptr += l_stride;
11506             }
11507 
11508             p_data = (OPJ_BYTE*) l_dest_ptr;
11509         }
11510         break;
11511         }
11512     }
11513 }
11514 
opj_j2k_post_write_tile(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)11515 static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k,
11516                                         opj_stream_private_t *p_stream,
11517                                         opj_event_mgr_t * p_manager)
11518 {
11519     OPJ_UINT32 l_nb_bytes_written;
11520     OPJ_BYTE * l_current_data = 00;
11521     OPJ_UINT32 l_tile_size = 0;
11522     OPJ_UINT32 l_available_data;
11523 
11524     /* preconditions */
11525     assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11526 
11527     l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11528     l_available_data = l_tile_size;
11529     l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11530 
11531     l_nb_bytes_written = 0;
11532     if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written,
11533                                         l_available_data, p_stream, p_manager)) {
11534         return OPJ_FALSE;
11535     }
11536     l_current_data += l_nb_bytes_written;
11537     l_available_data -= l_nb_bytes_written;
11538 
11539     l_nb_bytes_written = 0;
11540     if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written,
11541                                        l_available_data, p_stream, p_manager)) {
11542         return OPJ_FALSE;
11543     }
11544 
11545     l_available_data -= l_nb_bytes_written;
11546     l_nb_bytes_written = l_tile_size - l_available_data;
11547 
11548     if (opj_stream_write_data(p_stream,
11549                               p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11550                               l_nb_bytes_written, p_manager) != l_nb_bytes_written) {
11551         return OPJ_FALSE;
11552     }
11553 
11554     ++p_j2k->m_current_tile_number;
11555 
11556     return OPJ_TRUE;
11557 }
11558 
opj_j2k_setup_end_compress(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)11559 static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k,
11560         opj_event_mgr_t * p_manager)
11561 {
11562     /* preconditions */
11563     assert(p_j2k != 00);
11564     assert(p_manager != 00);
11565 
11566     /* DEVELOPER CORNER, insert your custom procedures */
11567     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11568                                            (opj_procedure)opj_j2k_write_eoc, p_manager)) {
11569         return OPJ_FALSE;
11570     }
11571 
11572     if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
11573         if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11574                                                (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) {
11575             return OPJ_FALSE;
11576         }
11577     }
11578 
11579     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11580                                            (opj_procedure)opj_j2k_write_epc, p_manager)) {
11581         return OPJ_FALSE;
11582     }
11583     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11584                                            (opj_procedure)opj_j2k_end_encoding, p_manager)) {
11585         return OPJ_FALSE;
11586     }
11587     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11588                                            (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) {
11589         return OPJ_FALSE;
11590     }
11591     return OPJ_TRUE;
11592 }
11593 
opj_j2k_setup_encoding_validation(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)11594 static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k,
11595         opj_event_mgr_t * p_manager)
11596 {
11597     /* preconditions */
11598     assert(p_j2k != 00);
11599     assert(p_manager != 00);
11600 
11601     if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
11602                                            (opj_procedure)opj_j2k_build_encoder, p_manager)) {
11603         return OPJ_FALSE;
11604     }
11605     if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
11606                                            (opj_procedure)opj_j2k_encoding_validation, p_manager)) {
11607         return OPJ_FALSE;
11608     }
11609 
11610     /* DEVELOPER CORNER, add your custom validation procedure */
11611     if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
11612                                            (opj_procedure)opj_j2k_mct_validation, p_manager)) {
11613         return OPJ_FALSE;
11614     }
11615 
11616     return OPJ_TRUE;
11617 }
11618 
opj_j2k_setup_header_writing(opj_j2k_t * p_j2k,opj_event_mgr_t * p_manager)11619 static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k,
11620         opj_event_mgr_t * p_manager)
11621 {
11622     /* preconditions */
11623     assert(p_j2k != 00);
11624     assert(p_manager != 00);
11625 
11626     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11627                                            (opj_procedure)opj_j2k_init_info, p_manager)) {
11628         return OPJ_FALSE;
11629     }
11630     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11631                                            (opj_procedure)opj_j2k_write_soc, p_manager)) {
11632         return OPJ_FALSE;
11633     }
11634     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11635                                            (opj_procedure)opj_j2k_write_siz, p_manager)) {
11636         return OPJ_FALSE;
11637     }
11638     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11639                                            (opj_procedure)opj_j2k_write_cod, p_manager)) {
11640         return OPJ_FALSE;
11641     }
11642     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11643                                            (opj_procedure)opj_j2k_write_qcd, p_manager)) {
11644         return OPJ_FALSE;
11645     }
11646     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11647                                            (opj_procedure)opj_j2k_write_all_coc, p_manager)) {
11648         return OPJ_FALSE;
11649     }
11650     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11651                                            (opj_procedure)opj_j2k_write_all_qcc, p_manager)) {
11652         return OPJ_FALSE;
11653     }
11654 
11655     if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
11656         if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11657                                                (opj_procedure)opj_j2k_write_tlm, p_manager)) {
11658             return OPJ_FALSE;
11659         }
11660 
11661         if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) {
11662             if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11663                                                    (opj_procedure)opj_j2k_write_poc, p_manager)) {
11664                 return OPJ_FALSE;
11665             }
11666         }
11667     }
11668 
11669     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11670                                            (opj_procedure)opj_j2k_write_regions, p_manager)) {
11671         return OPJ_FALSE;
11672     }
11673 
11674     if (p_j2k->m_cp.comment != 00)  {
11675         if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11676                                                (opj_procedure)opj_j2k_write_com, p_manager)) {
11677             return OPJ_FALSE;
11678         }
11679     }
11680 
11681     /* DEVELOPER CORNER, insert your custom procedures */
11682     if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) {
11683         if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11684                                                (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) {
11685             return OPJ_FALSE;
11686         }
11687     }
11688     /* End of Developer Corner */
11689 
11690     if (p_j2k->cstr_index) {
11691         if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11692                                                (opj_procedure)opj_j2k_get_end_header, p_manager)) {
11693             return OPJ_FALSE;
11694         }
11695     }
11696 
11697     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11698                                            (opj_procedure)opj_j2k_create_tcd, p_manager)) {
11699         return OPJ_FALSE;
11700     }
11701     if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
11702                                            (opj_procedure)opj_j2k_update_rates, p_manager)) {
11703         return OPJ_FALSE;
11704     }
11705 
11706     return OPJ_TRUE;
11707 }
11708 
opj_j2k_write_first_tile_part(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_total_data_size,opj_stream_private_t * p_stream,struct opj_event_mgr * p_manager)11709 static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k,
11710         OPJ_BYTE * p_data,
11711         OPJ_UINT32 * p_data_written,
11712         OPJ_UINT32 p_total_data_size,
11713         opj_stream_private_t *p_stream,
11714         struct opj_event_mgr * p_manager)
11715 {
11716     OPJ_UINT32 l_nb_bytes_written = 0;
11717     OPJ_UINT32 l_current_nb_bytes_written;
11718     OPJ_BYTE * l_begin_data = 00;
11719 
11720     opj_tcd_t * l_tcd = 00;
11721     opj_cp_t * l_cp = 00;
11722 
11723     l_tcd = p_j2k->m_tcd;
11724     l_cp = &(p_j2k->m_cp);
11725 
11726     l_tcd->cur_pino = 0;
11727 
11728     /*Get number of tile parts*/
11729     p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11730 
11731     /* INDEX >> */
11732     /* << INDEX */
11733 
11734     l_current_nb_bytes_written = 0;
11735     l_begin_data = p_data;
11736     if (! opj_j2k_write_sot(p_j2k, p_data, p_total_data_size,
11737                             &l_current_nb_bytes_written, p_stream,
11738                             p_manager)) {
11739         return OPJ_FALSE;
11740     }
11741 
11742     l_nb_bytes_written += l_current_nb_bytes_written;
11743     p_data += l_current_nb_bytes_written;
11744     p_total_data_size -= l_current_nb_bytes_written;
11745 
11746     if (!OPJ_IS_CINEMA(l_cp->rsiz)) {
11747 #if 0
11748         for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11749             l_current_nb_bytes_written = 0;
11750             opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written,
11751                                         p_manager);
11752             l_nb_bytes_written += l_current_nb_bytes_written;
11753             p_data += l_current_nb_bytes_written;
11754             p_total_data_size -= l_current_nb_bytes_written;
11755 
11756             l_current_nb_bytes_written = 0;
11757             opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written,
11758                                         p_manager);
11759             l_nb_bytes_written += l_current_nb_bytes_written;
11760             p_data += l_current_nb_bytes_written;
11761             p_total_data_size -= l_current_nb_bytes_written;
11762         }
11763 #endif
11764         if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11765             l_current_nb_bytes_written = 0;
11766             opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written,
11767                                         p_manager);
11768             l_nb_bytes_written += l_current_nb_bytes_written;
11769             p_data += l_current_nb_bytes_written;
11770             p_total_data_size -= l_current_nb_bytes_written;
11771         }
11772     }
11773 
11774     l_current_nb_bytes_written = 0;
11775     if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
11776                             p_total_data_size, p_stream, p_manager)) {
11777         return OPJ_FALSE;
11778     }
11779 
11780     l_nb_bytes_written += l_current_nb_bytes_written;
11781     * p_data_written = l_nb_bytes_written;
11782 
11783     /* Writing Psot in SOT marker */
11784     opj_write_bytes(l_begin_data + 6, l_nb_bytes_written,
11785                     4);                                 /* PSOT */
11786 
11787     if (OPJ_IS_CINEMA(l_cp->rsiz)) {
11788         opj_j2k_update_tlm(p_j2k, l_nb_bytes_written);
11789     }
11790 
11791     return OPJ_TRUE;
11792 }
11793 
opj_j2k_write_all_tile_parts(opj_j2k_t * p_j2k,OPJ_BYTE * p_data,OPJ_UINT32 * p_data_written,OPJ_UINT32 p_total_data_size,opj_stream_private_t * p_stream,struct opj_event_mgr * p_manager)11794 static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k,
11795         OPJ_BYTE * p_data,
11796         OPJ_UINT32 * p_data_written,
11797         OPJ_UINT32 p_total_data_size,
11798         opj_stream_private_t *p_stream,
11799         struct opj_event_mgr * p_manager
11800                                             )
11801 {
11802     OPJ_UINT32 tilepartno = 0;
11803     OPJ_UINT32 l_nb_bytes_written = 0;
11804     OPJ_UINT32 l_current_nb_bytes_written;
11805     OPJ_UINT32 l_part_tile_size;
11806     OPJ_UINT32 tot_num_tp;
11807     OPJ_UINT32 pino;
11808 
11809     OPJ_BYTE * l_begin_data;
11810     opj_tcp_t *l_tcp = 00;
11811     opj_tcd_t * l_tcd = 00;
11812     opj_cp_t * l_cp = 00;
11813 
11814     l_tcd = p_j2k->m_tcd;
11815     l_cp = &(p_j2k->m_cp);
11816     l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11817 
11818     /*Get number of tile parts*/
11819     tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number);
11820 
11821     /* start writing remaining tile parts */
11822     ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11823     for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11824         p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11825         l_current_nb_bytes_written = 0;
11826         l_part_tile_size = 0;
11827         l_begin_data = p_data;
11828 
11829         if (! opj_j2k_write_sot(p_j2k, p_data,
11830                                 p_total_data_size,
11831                                 &l_current_nb_bytes_written,
11832                                 p_stream,
11833                                 p_manager)) {
11834             return OPJ_FALSE;
11835         }
11836 
11837         l_nb_bytes_written += l_current_nb_bytes_written;
11838         p_data += l_current_nb_bytes_written;
11839         p_total_data_size -= l_current_nb_bytes_written;
11840         l_part_tile_size += l_current_nb_bytes_written;
11841 
11842         l_current_nb_bytes_written = 0;
11843         if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
11844                                 p_total_data_size, p_stream, p_manager)) {
11845             return OPJ_FALSE;
11846         }
11847 
11848         p_data += l_current_nb_bytes_written;
11849         l_nb_bytes_written += l_current_nb_bytes_written;
11850         p_total_data_size -= l_current_nb_bytes_written;
11851         l_part_tile_size += l_current_nb_bytes_written;
11852 
11853         /* Writing Psot in SOT marker */
11854         opj_write_bytes(l_begin_data + 6, l_part_tile_size,
11855                         4);                                   /* PSOT */
11856 
11857         if (OPJ_IS_CINEMA(l_cp->rsiz)) {
11858             opj_j2k_update_tlm(p_j2k, l_part_tile_size);
11859         }
11860 
11861         ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11862     }
11863 
11864     for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11865         l_tcd->cur_pino = pino;
11866 
11867         /*Get number of tile parts*/
11868         tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number);
11869         for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11870             p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11871             l_current_nb_bytes_written = 0;
11872             l_part_tile_size = 0;
11873             l_begin_data = p_data;
11874 
11875             if (! opj_j2k_write_sot(p_j2k, p_data,
11876                                     p_total_data_size,
11877                                     &l_current_nb_bytes_written, p_stream,
11878                                     p_manager)) {
11879                 return OPJ_FALSE;
11880             }
11881 
11882             l_nb_bytes_written += l_current_nb_bytes_written;
11883             p_data += l_current_nb_bytes_written;
11884             p_total_data_size -= l_current_nb_bytes_written;
11885             l_part_tile_size += l_current_nb_bytes_written;
11886 
11887             l_current_nb_bytes_written = 0;
11888 
11889             if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
11890                                     p_total_data_size, p_stream, p_manager)) {
11891                 return OPJ_FALSE;
11892             }
11893 
11894             l_nb_bytes_written += l_current_nb_bytes_written;
11895             p_data += l_current_nb_bytes_written;
11896             p_total_data_size -= l_current_nb_bytes_written;
11897             l_part_tile_size += l_current_nb_bytes_written;
11898 
11899             /* Writing Psot in SOT marker */
11900             opj_write_bytes(l_begin_data + 6, l_part_tile_size,
11901                             4);                                   /* PSOT */
11902 
11903             if (OPJ_IS_CINEMA(l_cp->rsiz)) {
11904                 opj_j2k_update_tlm(p_j2k, l_part_tile_size);
11905             }
11906 
11907             ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11908         }
11909     }
11910 
11911     *p_data_written = l_nb_bytes_written;
11912 
11913     return OPJ_TRUE;
11914 }
11915 
opj_j2k_write_updated_tlm(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)11916 static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k,
11917         struct opj_stream_private *p_stream,
11918         struct opj_event_mgr * p_manager)
11919 {
11920     OPJ_UINT32 l_tlm_size;
11921     OPJ_OFF_T l_tlm_position, l_current_position;
11922 
11923     /* preconditions */
11924     assert(p_j2k != 00);
11925     assert(p_manager != 00);
11926     assert(p_stream != 00);
11927 
11928     l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11929     l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11930     l_current_position = opj_stream_tell(p_stream);
11931 
11932     if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) {
11933         return OPJ_FALSE;
11934     }
11935 
11936     if (opj_stream_write_data(p_stream,
11937                               p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size,
11938                               p_manager) != l_tlm_size) {
11939         return OPJ_FALSE;
11940     }
11941 
11942     if (! opj_stream_seek(p_stream, l_current_position, p_manager)) {
11943         return OPJ_FALSE;
11944     }
11945 
11946     return OPJ_TRUE;
11947 }
11948 
opj_j2k_end_encoding(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)11949 static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k,
11950                                      struct opj_stream_private *p_stream,
11951                                      struct opj_event_mgr * p_manager)
11952 {
11953     /* preconditions */
11954     assert(p_j2k != 00);
11955     assert(p_manager != 00);
11956     assert(p_stream != 00);
11957 
11958     OPJ_UNUSED(p_stream);
11959     OPJ_UNUSED(p_manager);
11960 
11961     opj_tcd_destroy(p_j2k->m_tcd);
11962     p_j2k->m_tcd = 00;
11963 
11964     if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11965         opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11966         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11967         p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11968     }
11969 
11970     if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11971         opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11972         p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11973     }
11974 
11975     p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11976 
11977     return OPJ_TRUE;
11978 }
11979 
11980 /**
11981  * Destroys the memory associated with the decoding of headers.
11982  */
opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)11983 static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,
11984         opj_stream_private_t *p_stream,
11985         opj_event_mgr_t * p_manager
11986                                              )
11987 {
11988     /* preconditions */
11989     assert(p_j2k != 00);
11990     assert(p_stream != 00);
11991     assert(p_manager != 00);
11992 
11993     OPJ_UNUSED(p_stream);
11994     OPJ_UNUSED(p_manager);
11995 
11996     if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11997         opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11998         p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11999     }
12000 
12001     p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12002 
12003     return OPJ_TRUE;
12004 }
12005 
opj_j2k_init_info(opj_j2k_t * p_j2k,struct opj_stream_private * p_stream,struct opj_event_mgr * p_manager)12006 static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k,
12007                                   struct opj_stream_private *p_stream,
12008                                   struct opj_event_mgr * p_manager)
12009 {
12010     opj_codestream_info_t * l_cstr_info = 00;
12011 
12012     /* preconditions */
12013     assert(p_j2k != 00);
12014     assert(p_manager != 00);
12015     assert(p_stream != 00);
12016     (void)l_cstr_info;
12017 
12018     OPJ_UNUSED(p_stream);
12019 
12020     /* TODO mergeV2: check this part which use cstr_info */
12021     /*l_cstr_info = p_j2k->cstr_info;
12022 
12023     if (l_cstr_info)  {
12024             OPJ_UINT32 compno;
12025             l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
12026 
12027             l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12028             l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12029 
12030             l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12031 
12032             l_cstr_info->tw = p_j2k->m_cp.tw;
12033             l_cstr_info->th = p_j2k->m_cp.th;
12034 
12035             l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/        /* new version parser */
12036     /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/      /* new version parser */
12037     /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/     /* new version parser */
12038     /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/     /* new version parser */
12039 
12040     /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12041 
12042     l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12043 
12044     l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12045 
12046     for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12047             l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12048     }
12049 
12050     l_cstr_info->D_max = 0.0;       */      /* ADD Marcela */
12051 
12052     /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12053 
12054     /*l_cstr_info->maxmarknum = 100;
12055     l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12056     l_cstr_info->marknum = 0;
12057     }*/
12058 
12059     return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp),
12060                                 &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image,
12061                                 p_manager);
12062 }
12063 
12064 /**
12065  * Creates a tile-coder encoder.
12066  *
12067  * @param       p_stream                the stream to write data to.
12068  * @param       p_j2k                   J2K codec.
12069  * @param       p_manager               the user event manager.
12070 */
opj_j2k_create_tcd(opj_j2k_t * p_j2k,opj_stream_private_t * p_stream,opj_event_mgr_t * p_manager)12071 static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k,
12072                                    opj_stream_private_t *p_stream,
12073                                    opj_event_mgr_t * p_manager
12074                                   )
12075 {
12076     /* preconditions */
12077     assert(p_j2k != 00);
12078     assert(p_manager != 00);
12079     assert(p_stream != 00);
12080 
12081     OPJ_UNUSED(p_stream);
12082 
12083     p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
12084 
12085     if (! p_j2k->m_tcd) {
12086         opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12087         return OPJ_FALSE;
12088     }
12089 
12090     if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp,
12091                       p_j2k->m_tp)) {
12092         opj_tcd_destroy(p_j2k->m_tcd);
12093         p_j2k->m_tcd = 00;
12094         return OPJ_FALSE;
12095     }
12096 
12097     return OPJ_TRUE;
12098 }
12099 
opj_j2k_write_tile(opj_j2k_t * p_j2k,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)12100 OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k,
12101                             OPJ_UINT32 p_tile_index,
12102                             OPJ_BYTE * p_data,
12103                             OPJ_UINT32 p_data_size,
12104                             opj_stream_private_t *p_stream,
12105                             opj_event_mgr_t * p_manager)
12106 {
12107     if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) {
12108         opj_event_msg(p_manager, EVT_ERROR,
12109                       "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12110         return OPJ_FALSE;
12111     } else {
12112         OPJ_UINT32 j;
12113         /* Allocate data */
12114         for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
12115             opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j;
12116 
12117             if (! opj_alloc_tile_component_data(l_tilec)) {
12118                 opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data.");
12119                 return OPJ_FALSE;
12120             }
12121         }
12122 
12123         /* now copy data into the tile component */
12124         if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) {
12125             opj_event_msg(p_manager, EVT_ERROR,
12126                           "Size mismatch between tile data and sent data.");
12127             return OPJ_FALSE;
12128         }
12129         if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) {
12130             opj_event_msg(p_manager, EVT_ERROR,
12131                           "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
12132             return OPJ_FALSE;
12133         }
12134     }
12135 
12136     return OPJ_TRUE;
12137 }
12138