Coverage Report

Created: 2024-05-21 06:09

/src/openjpeg/src/lib/openjp2/j2k.c
Line
Count
Source (jump to first uncovered line)
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. Developers 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 launch to make sure the codec parameters
93
 * are valid. Developers 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 launch to make sure the codec parameters
100
 * are valid. Developers 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 launch to make sure the codec parameters
107
 * are valid. Developers 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
 * Executes 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 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 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       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 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
 * This also writes optional PLT markers (before SOD)
883
 *
884
 * @param       p_j2k               J2K codec.
885
 * @param       p_tile_coder        FIXME DOC
886
 * @param       p_data              FIXME DOC
887
 * @param       p_data_written      FIXME DOC
888
 * @param       total_data_size   FIXME DOC
889
 * @param       p_stream            the stream to write data to.
890
 * @param       p_manager           the user event manager.
891
*/
892
static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
893
                                  opj_tcd_t * p_tile_coder,
894
                                  OPJ_BYTE * p_data,
895
                                  OPJ_UINT32 * p_data_written,
896
                                  OPJ_UINT32 total_data_size,
897
                                  const opj_stream_private_t *p_stream,
898
                                  opj_event_mgr_t * p_manager);
899
900
/**
901
 * Reads a SOD marker (Start Of Data)
902
 *
903
 * @param       p_j2k                   the jpeg2000 codec.
904
 * @param       p_stream                FIXME DOC
905
 * @param       p_manager               the user event manager.
906
*/
907
static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
908
                                 opj_stream_private_t *p_stream,
909
                                 opj_event_mgr_t * p_manager);
910
911
static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size)
912
0
{
913
0
    if (p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte) {
914
0
        opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
915
0
                        p_j2k->m_current_tile_number, 1);
916
0
        p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 1;
917
0
    } else {
918
0
        opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
919
0
                        p_j2k->m_current_tile_number, 2);
920
0
        p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 2;
921
0
    }
922
923
0
    opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
924
0
                    p_tile_part_size, 4);                                       /* PSOT */
925
0
    p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
926
0
}
927
928
/**
929
 * Writes the RGN marker (Region Of Interest)
930
 *
931
 * @param       p_tile_no               the tile to output
932
 * @param       p_comp_no               the component to output
933
 * @param       nb_comps                the number of components
934
 * @param       p_stream                the stream to write data to.
935
 * @param       p_j2k                   J2K codec.
936
 * @param       p_manager               the user event manager.
937
*/
938
static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
939
                                  OPJ_UINT32 p_tile_no,
940
                                  OPJ_UINT32 p_comp_no,
941
                                  OPJ_UINT32 nb_comps,
942
                                  opj_stream_private_t *p_stream,
943
                                  opj_event_mgr_t * p_manager);
944
945
/**
946
 * Reads a RGN marker (Region Of Interest)
947
 *
948
 * @param       p_header_data   the data contained in the POC box.
949
 * @param       p_j2k                   the jpeg2000 codec.
950
 * @param       p_header_size   the size of the data contained in the POC marker.
951
 * @param       p_manager               the user event manager.
952
*/
953
static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k,
954
                                 OPJ_BYTE * p_header_data,
955
                                 OPJ_UINT32 p_header_size,
956
                                 opj_event_mgr_t * p_manager);
957
958
/**
959
 * Writes the EOC marker (End of Codestream)
960
 *
961
 * @param       p_stream                the stream to write data to.
962
 * @param       p_j2k                   J2K codec.
963
 * @param       p_manager               the user event manager.
964
*/
965
static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k,
966
                                  opj_stream_private_t *p_stream,
967
                                  opj_event_mgr_t * p_manager);
968
969
#if 0
970
/**
971
 * Reads a EOC marker (End Of Codestream)
972
 *
973
 * @param       p_j2k                   the jpeg2000 codec.
974
 * @param       p_stream                FIXME DOC
975
 * @param       p_manager               the user event manager.
976
*/
977
static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k,
978
                                 opj_stream_private_t *p_stream,
979
                                 opj_event_mgr_t * p_manager);
980
#endif
981
982
/**
983
 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
984
 *
985
 * @param       p_stream                        the stream to write data to.
986
 * @param       p_j2k                   J2K codec.
987
 * @param       p_manager       the user event manager.
988
*/
989
static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k,
990
        opj_stream_private_t *p_stream,
991
        opj_event_mgr_t * p_manager);
992
993
/**
994
 * Inits the Info
995
 *
996
 * @param       p_stream                the stream to write data to.
997
 * @param       p_j2k                   J2K codec.
998
 * @param       p_manager               the user event manager.
999
*/
1000
static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k,
1001
                                  opj_stream_private_t *p_stream,
1002
                                  opj_event_mgr_t * p_manager);
1003
1004
/**
1005
Add main header marker information
1006
@param cstr_index    Codestream information structure
1007
@param type         marker type
1008
@param pos          byte offset of marker segment
1009
@param len          length of marker segment
1010
 */
1011
static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index,
1012
                                     OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1013
/**
1014
Add tile header marker information
1015
@param tileno       tile index number
1016
@param cstr_index   Codestream information structure
1017
@param type         marker type
1018
@param pos          byte offset of marker segment
1019
@param len          length of marker segment
1020
 */
1021
static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno,
1022
                                     opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos,
1023
                                     OPJ_UINT32 len);
1024
1025
/**
1026
 * Reads an unknown marker
1027
 *
1028
 * @param       p_j2k                   the jpeg2000 codec.
1029
 * @param       p_stream                the stream object to read from.
1030
 * @param       output_marker           FIXME DOC
1031
 * @param       p_manager               the user event manager.
1032
 *
1033
 * @return      true                    if the marker could be deduced.
1034
*/
1035
static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
1036
                                 opj_stream_private_t *p_stream,
1037
                                 OPJ_UINT32 *output_marker,
1038
                                 opj_event_mgr_t * p_manager);
1039
1040
/**
1041
 * Writes the MCT marker (Multiple Component Transform)
1042
 *
1043
 * @param       p_j2k           J2K codec.
1044
 * @param       p_mct_record    FIXME DOC
1045
 * @param       p_stream        the stream to write data to.
1046
 * @param       p_manager       the user event manager.
1047
*/
1048
static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k,
1049
        opj_mct_data_t * p_mct_record,
1050
        opj_stream_private_t *p_stream,
1051
        opj_event_mgr_t * p_manager);
1052
1053
/**
1054
 * Reads a MCT marker (Multiple Component Transform)
1055
 *
1056
 * @param       p_header_data   the data contained in the MCT box.
1057
 * @param       p_j2k                   the jpeg2000 codec.
1058
 * @param       p_header_size   the size of the data contained in the MCT marker.
1059
 * @param       p_manager               the user event manager.
1060
*/
1061
static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k,
1062
                                 OPJ_BYTE * p_header_data,
1063
                                 OPJ_UINT32 p_header_size,
1064
                                 opj_event_mgr_t * p_manager);
1065
1066
/**
1067
 * Writes the MCC marker (Multiple Component Collection)
1068
 *
1069
 * @param       p_j2k                   J2K codec.
1070
 * @param       p_mcc_record            FIXME DOC
1071
 * @param       p_stream                the stream to write data to.
1072
 * @param       p_manager               the user event manager.
1073
*/
1074
static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k,
1075
        opj_simple_mcc_decorrelation_data_t * p_mcc_record,
1076
        opj_stream_private_t *p_stream,
1077
        opj_event_mgr_t * p_manager);
1078
1079
/**
1080
 * Reads a MCC marker (Multiple Component Collection)
1081
 *
1082
 * @param       p_header_data   the data contained in the MCC box.
1083
 * @param       p_j2k                   the jpeg2000 codec.
1084
 * @param       p_header_size   the size of the data contained in the MCC marker.
1085
 * @param       p_manager               the user event manager.
1086
*/
1087
static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k,
1088
                                 OPJ_BYTE * p_header_data,
1089
                                 OPJ_UINT32 p_header_size,
1090
                                 opj_event_mgr_t * p_manager);
1091
1092
/**
1093
 * Writes the MCO marker (Multiple component transformation ordering)
1094
 *
1095
 * @param       p_stream                                the stream to write data to.
1096
 * @param       p_j2k                           J2K codec.
1097
 * @param       p_manager               the user event manager.
1098
*/
1099
static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k,
1100
                                  opj_stream_private_t *p_stream,
1101
                                  opj_event_mgr_t * p_manager);
1102
1103
/**
1104
 * Reads a MCO marker (Multiple Component Transform Ordering)
1105
 *
1106
 * @param       p_header_data   the data contained in the MCO box.
1107
 * @param       p_j2k                   the jpeg2000 codec.
1108
 * @param       p_header_size   the size of the data contained in the MCO marker.
1109
 * @param       p_manager               the user event manager.
1110
*/
1111
static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k,
1112
                                 OPJ_BYTE * p_header_data,
1113
                                 OPJ_UINT32 p_header_size,
1114
                                 opj_event_mgr_t * p_manager);
1115
1116
static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image,
1117
                                OPJ_UINT32 p_index);
1118
1119
static void  opj_j2k_read_int16_to_float(const void * p_src_data,
1120
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1121
static void  opj_j2k_read_int32_to_float(const void * p_src_data,
1122
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1123
static void  opj_j2k_read_float32_to_float(const void * p_src_data,
1124
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1125
static void  opj_j2k_read_float64_to_float(const void * p_src_data,
1126
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1127
1128
static void  opj_j2k_read_int16_to_int32(const void * p_src_data,
1129
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1130
static void  opj_j2k_read_int32_to_int32(const void * p_src_data,
1131
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1132
static void  opj_j2k_read_float32_to_int32(const void * p_src_data,
1133
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1134
static void  opj_j2k_read_float64_to_int32(const void * p_src_data,
1135
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1136
1137
static void  opj_j2k_write_float_to_int16(const void * p_src_data,
1138
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1139
static void  opj_j2k_write_float_to_int32(const void * p_src_data,
1140
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1141
static void  opj_j2k_write_float_to_float(const void * p_src_data,
1142
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1143
static void  opj_j2k_write_float_to_float64(const void * p_src_data,
1144
        void * p_dest_data, OPJ_UINT32 p_nb_elem);
1145
1146
/**
1147
 * Ends the encoding, i.e. frees memory.
1148
 *
1149
 * @param       p_stream                the stream to write data to.
1150
 * @param       p_j2k                   J2K codec.
1151
 * @param       p_manager               the user event manager.
1152
*/
1153
static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k,
1154
                                     opj_stream_private_t *p_stream,
1155
                                     opj_event_mgr_t * p_manager);
1156
1157
/**
1158
 * Writes the CBD marker (Component bit depth definition)
1159
 *
1160
 * @param       p_stream                                the stream to write data to.
1161
 * @param       p_j2k                           J2K codec.
1162
 * @param       p_manager               the user event manager.
1163
*/
1164
static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k,
1165
                                  opj_stream_private_t *p_stream,
1166
                                  opj_event_mgr_t * p_manager);
1167
1168
/**
1169
 * Reads a CBD marker (Component bit depth definition)
1170
 * @param       p_header_data   the data contained in the CBD box.
1171
 * @param       p_j2k                   the jpeg2000 codec.
1172
 * @param       p_header_size   the size of the data contained in the CBD marker.
1173
 * @param       p_manager               the user event manager.
1174
*/
1175
static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k,
1176
                                 OPJ_BYTE * p_header_data,
1177
                                 OPJ_UINT32 p_header_size,
1178
                                 opj_event_mgr_t * p_manager);
1179
1180
/**
1181
 * Reads a CAP marker (extended capabilities definition). Empty implementation.
1182
 * Found in HTJ2K files
1183
 *
1184
 * @param       p_header_data   the data contained in the CAP box.
1185
 * @param       p_j2k                   the jpeg2000 codec.
1186
 * @param       p_header_size   the size of the data contained in the CAP marker.
1187
 * @param       p_manager               the user event manager.
1188
*/
1189
static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k,
1190
                                 OPJ_BYTE * p_header_data,
1191
                                 OPJ_UINT32 p_header_size,
1192
                                 opj_event_mgr_t * p_manager);
1193
1194
/**
1195
 * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files
1196
 * @param       p_header_data   the data contained in the CPF box.
1197
 * @param       p_j2k                   the jpeg2000 codec.
1198
 * @param       p_header_size   the size of the data contained in the CPF marker.
1199
 * @param       p_manager               the user event manager.
1200
*/
1201
static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k,
1202
                                 OPJ_BYTE * p_header_data,
1203
                                 OPJ_UINT32 p_header_size,
1204
                                 opj_event_mgr_t * p_manager);
1205
1206
1207
/**
1208
 * Writes COC marker for each component.
1209
 *
1210
 * @param       p_stream                the stream to write data to.
1211
 * @param       p_j2k                   J2K codec.
1212
 * @param       p_manager               the user event manager.
1213
*/
1214
static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
1215
                                      opj_stream_private_t *p_stream,
1216
                                      opj_event_mgr_t * p_manager);
1217
1218
/**
1219
 * Writes QCC marker for each component.
1220
 *
1221
 * @param       p_stream                the stream to write data to.
1222
 * @param       p_j2k                   J2K codec.
1223
 * @param       p_manager               the user event manager.
1224
*/
1225
static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
1226
                                      opj_stream_private_t *p_stream,
1227
                                      opj_event_mgr_t * p_manager);
1228
1229
/**
1230
 * Writes regions of interests.
1231
 *
1232
 * @param       p_stream                the stream to write data to.
1233
 * @param       p_j2k                   J2K codec.
1234
 * @param       p_manager               the user event manager.
1235
*/
1236
static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k,
1237
                                      opj_stream_private_t *p_stream,
1238
                                      opj_event_mgr_t * p_manager);
1239
1240
/**
1241
 * Writes EPC ????
1242
 *
1243
 * @param       p_stream                the stream to write data to.
1244
 * @param       p_j2k                   J2K codec.
1245
 * @param       p_manager               the user event manager.
1246
*/
1247
static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k,
1248
                                  opj_stream_private_t *p_stream,
1249
                                  opj_event_mgr_t * p_manager);
1250
1251
/**
1252
 * Checks the progression order changes values. Tells of the poc given as input are valid.
1253
 * A nice message is outputted at errors.
1254
 *
1255
 * @param       p_pocs                  the progression order changes.
1256
 * @param       tileno                  the tile number of interest
1257
 * @param       p_nb_pocs               the number of progression order changes.
1258
 * @param       p_nb_resolutions        the number of resolutions.
1259
 * @param       numcomps                the number of components
1260
 * @param       numlayers               the number of layers.
1261
 * @param       p_manager               the user event manager.
1262
 *
1263
 * @return      true if the pocs are valid.
1264
 */
1265
static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs,
1266
                                      OPJ_UINT32 tileno,
1267
                                      OPJ_UINT32 p_nb_pocs,
1268
                                      OPJ_UINT32 p_nb_resolutions,
1269
                                      OPJ_UINT32 numcomps,
1270
                                      OPJ_UINT32 numlayers,
1271
                                      opj_event_mgr_t * p_manager);
1272
1273
/**
1274
 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1275
 *
1276
 * @param               cp                      the coding parameters.
1277
 * @param               pino            the offset of the given poc (i.e. its position in the coding parameter).
1278
 * @param               tileno          the given tile.
1279
 *
1280
 * @return              the number of tile parts.
1281
 */
1282
static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino,
1283
                                     OPJ_UINT32 tileno);
1284
1285
/**
1286
 * Calculates the total number of tile parts needed by the encoder to
1287
 * encode such an image. If not enough memory is available, then the function return false.
1288
 *
1289
 * @param       p_nb_tiles      pointer that will hold the number of tile parts.
1290
 * @param       cp                      the coding parameters for the image.
1291
 * @param       image           the image to encode.
1292
 * @param       p_j2k                   the p_j2k encoder.
1293
 * @param       p_manager       the user event manager.
1294
 *
1295
 * @return true if the function was successful, false else.
1296
 */
1297
static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k,
1298
                                     opj_cp_t *cp,
1299
                                     OPJ_UINT32 * p_nb_tiles,
1300
                                     opj_image_t *image,
1301
                                     opj_event_mgr_t * p_manager);
1302
1303
static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
1304
1305
static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
1306
1307
static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1308
1309
static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp);
1310
1311
static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp);
1312
1313
static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
1314
1315
static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters,
1316
        opj_image_t *image, opj_event_mgr_t *p_manager);
1317
1318
static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
1319
        opj_event_mgr_t *p_manager);
1320
1321
static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters,
1322
                                       opj_image_t *image, opj_event_mgr_t *p_manager);
1323
1324
static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters,
1325
        opj_image_t *image,
1326
        opj_event_mgr_t *p_manager);
1327
1328
/**
1329
 * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254.
1330
 *
1331
 * @param       p_stream            the stream to read data from.
1332
 * @param       tile_no             tile number we're looking for.
1333
 * @param       p_correction_needed output value. if true, non conformant codestream needs TNsot correction.
1334
 * @param       p_manager       the user event manager.
1335
 *
1336
 * @return true if the function was successful, false else.
1337
 */
1338
static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t
1339
        *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed,
1340
        opj_event_mgr_t * p_manager);
1341
1342
/*@}*/
1343
1344
/*@}*/
1345
1346
/* ----------------------------------------------------------------------- */
1347
typedef struct j2k_prog_order {
1348
    OPJ_PROG_ORDER enum_prog;
1349
    char str_prog[5];
1350
} j2k_prog_order_t;
1351
1352
static const j2k_prog_order_t j2k_prog_order_list[] = {
1353
    {OPJ_CPRL, "CPRL"},
1354
    {OPJ_LRCP, "LRCP"},
1355
    {OPJ_PCRL, "PCRL"},
1356
    {OPJ_RLCP, "RLCP"},
1357
    {OPJ_RPCL, "RPCL"},
1358
    {(OPJ_PROG_ORDER) - 1, ""}
1359
};
1360
1361
/**
1362
 * FIXME DOC
1363
 */
1364
static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = {
1365
    2,
1366
    4,
1367
    4,
1368
    8
1369
};
1370
1371
typedef void (* opj_j2k_mct_function)(const void * p_src_data,
1372
                                      void * p_dest_data, OPJ_UINT32 p_nb_elem);
1373
1374
static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = {
1375
    opj_j2k_read_int16_to_float,
1376
    opj_j2k_read_int32_to_float,
1377
    opj_j2k_read_float32_to_float,
1378
    opj_j2k_read_float64_to_float
1379
};
1380
1381
static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = {
1382
    opj_j2k_read_int16_to_int32,
1383
    opj_j2k_read_int32_to_int32,
1384
    opj_j2k_read_float32_to_int32,
1385
    opj_j2k_read_float64_to_int32
1386
};
1387
1388
static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = {
1389
    opj_j2k_write_float_to_int16,
1390
    opj_j2k_write_float_to_int32,
1391
    opj_j2k_write_float_to_float,
1392
    opj_j2k_write_float_to_float64
1393
};
1394
1395
typedef struct opj_dec_memory_marker_handler {
1396
    /** marker value */
1397
    OPJ_UINT32 id;
1398
    /** value of the state when the marker can appear */
1399
    OPJ_UINT32 states;
1400
    /** action linked to the marker */
1401
    OPJ_BOOL(*handler)(opj_j2k_t *p_j2k,
1402
                       OPJ_BYTE * p_header_data,
1403
                       OPJ_UINT32 p_header_size,
1404
                       opj_event_mgr_t * p_manager);
1405
}
1406
opj_dec_memory_marker_handler_t;
1407
1408
static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1409
{
1410
    {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1411
    {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1412
    {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1413
    {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1414
    {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1415
    {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1416
    {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1417
    {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1418
    {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1419
    {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1420
    {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1421
    {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm},
1422
    {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1423
    {J2K_MS_SOP, 0, 0},
1424
    {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1425
    {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1426
    {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1427
    {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd},
1428
    {J2K_MS_CAP, J2K_STATE_MH, opj_j2k_read_cap},
1429
    {J2K_MS_CPF, J2K_STATE_MH, opj_j2k_read_cpf},
1430
    {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1431
    {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1432
#ifdef USE_JPWL
1433
#ifdef TODO_MS /* remove these functions which are not compatible with the v2 API */
1434
    {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1435
    {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1436
    {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1437
    {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1438
#endif
1439
#endif /* USE_JPWL */
1440
#ifdef USE_JPSEC
1441
    {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1442
    {J2K_MS_INSEC, 0, j2k_read_insec}
1443
#endif /* USE_JPSEC */
1444
    {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
1445
};
1446
1447
static void  opj_j2k_read_int16_to_float(const void * p_src_data,
1448
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1449
2.03k
{
1450
2.03k
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1451
2.03k
    OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1452
2.03k
    OPJ_UINT32 i;
1453
2.03k
    OPJ_UINT32 l_temp;
1454
1455
5.80k
    for (i = 0; i < p_nb_elem; ++i) {
1456
3.76k
        opj_read_bytes(l_src_data, &l_temp, 2);
1457
1458
3.76k
        l_src_data += sizeof(OPJ_INT16);
1459
1460
3.76k
        *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1461
3.76k
    }
1462
2.03k
}
1463
1464
static void  opj_j2k_read_int32_to_float(const void * p_src_data,
1465
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1466
529
{
1467
529
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1468
529
    OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1469
529
    OPJ_UINT32 i;
1470
529
    OPJ_UINT32 l_temp;
1471
1472
2.47k
    for (i = 0; i < p_nb_elem; ++i) {
1473
1.94k
        opj_read_bytes(l_src_data, &l_temp, 4);
1474
1475
1.94k
        l_src_data += sizeof(OPJ_INT32);
1476
1477
1.94k
        *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1478
1.94k
    }
1479
529
}
1480
1481
static void  opj_j2k_read_float32_to_float(const void * p_src_data,
1482
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1483
424
{
1484
424
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1485
424
    OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1486
424
    OPJ_UINT32 i;
1487
424
    OPJ_FLOAT32 l_temp;
1488
1489
1.88k
    for (i = 0; i < p_nb_elem; ++i) {
1490
1.46k
        opj_read_float(l_src_data, &l_temp);
1491
1492
1.46k
        l_src_data += sizeof(OPJ_FLOAT32);
1493
1494
1.46k
        *(l_dest_data++) = l_temp;
1495
1.46k
    }
1496
424
}
1497
1498
static void  opj_j2k_read_float64_to_float(const void * p_src_data,
1499
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1500
363
{
1501
363
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1502
363
    OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1503
363
    OPJ_UINT32 i;
1504
363
    OPJ_FLOAT64 l_temp;
1505
1506
1.80k
    for (i = 0; i < p_nb_elem; ++i) {
1507
1.44k
        opj_read_double(l_src_data, &l_temp);
1508
1509
1.44k
        l_src_data += sizeof(OPJ_FLOAT64);
1510
1511
1.44k
        *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1512
1.44k
    }
1513
363
}
1514
1515
static void  opj_j2k_read_int16_to_int32(const void * p_src_data,
1516
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1517
1.51k
{
1518
1.51k
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1519
1.51k
    OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1520
1.51k
    OPJ_UINT32 i;
1521
1.51k
    OPJ_UINT32 l_temp;
1522
1523
3.07k
    for (i = 0; i < p_nb_elem; ++i) {
1524
1.56k
        opj_read_bytes(l_src_data, &l_temp, 2);
1525
1526
1.56k
        l_src_data += sizeof(OPJ_INT16);
1527
1528
1.56k
        *(l_dest_data++) = (OPJ_INT32) l_temp;
1529
1.56k
    }
1530
1.51k
}
1531
1532
static void  opj_j2k_read_int32_to_int32(const void * p_src_data,
1533
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1534
122
{
1535
122
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1536
122
    OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1537
122
    OPJ_UINT32 i;
1538
122
    OPJ_UINT32 l_temp;
1539
1540
315
    for (i = 0; i < p_nb_elem; ++i) {
1541
193
        opj_read_bytes(l_src_data, &l_temp, 4);
1542
1543
193
        l_src_data += sizeof(OPJ_INT32);
1544
1545
193
        *(l_dest_data++) = (OPJ_INT32) l_temp;
1546
193
    }
1547
122
}
1548
1549
static void  opj_j2k_read_float32_to_int32(const void * p_src_data,
1550
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1551
448
{
1552
448
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1553
448
    OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1554
448
    OPJ_UINT32 i;
1555
448
    OPJ_FLOAT32 l_temp;
1556
1557
1.24k
    for (i = 0; i < p_nb_elem; ++i) {
1558
796
        opj_read_float(l_src_data, &l_temp);
1559
1560
796
        l_src_data += sizeof(OPJ_FLOAT32);
1561
1562
796
        *(l_dest_data++) = (OPJ_INT32) l_temp;
1563
796
    }
1564
448
}
1565
1566
static void  opj_j2k_read_float64_to_int32(const void * p_src_data,
1567
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1568
77
{
1569
77
    OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1570
77
    OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1571
77
    OPJ_UINT32 i;
1572
77
    OPJ_FLOAT64 l_temp;
1573
1574
228
    for (i = 0; i < p_nb_elem; ++i) {
1575
151
        opj_read_double(l_src_data, &l_temp);
1576
1577
151
        l_src_data += sizeof(OPJ_FLOAT64);
1578
1579
151
        *(l_dest_data++) = (OPJ_INT32) l_temp;
1580
151
    }
1581
77
}
1582
1583
static void  opj_j2k_write_float_to_int16(const void * p_src_data,
1584
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1585
0
{
1586
0
    OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1587
0
    OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1588
0
    OPJ_UINT32 i;
1589
0
    OPJ_UINT32 l_temp;
1590
1591
0
    for (i = 0; i < p_nb_elem; ++i) {
1592
0
        l_temp = (OPJ_UINT32) * (l_src_data++);
1593
1594
0
        opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT16));
1595
1596
0
        l_dest_data += sizeof(OPJ_INT16);
1597
0
    }
1598
0
}
1599
1600
static void opj_j2k_write_float_to_int32(const void * p_src_data,
1601
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1602
0
{
1603
0
    OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1604
0
    OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1605
0
    OPJ_UINT32 i;
1606
0
    OPJ_UINT32 l_temp;
1607
1608
0
    for (i = 0; i < p_nb_elem; ++i) {
1609
0
        l_temp = (OPJ_UINT32) * (l_src_data++);
1610
1611
0
        opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT32));
1612
1613
0
        l_dest_data += sizeof(OPJ_INT32);
1614
0
    }
1615
0
}
1616
1617
static void  opj_j2k_write_float_to_float(const void * p_src_data,
1618
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1619
0
{
1620
0
    OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1621
0
    OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1622
0
    OPJ_UINT32 i;
1623
0
    OPJ_FLOAT32 l_temp;
1624
1625
0
    for (i = 0; i < p_nb_elem; ++i) {
1626
0
        l_temp = (OPJ_FLOAT32) * (l_src_data++);
1627
1628
0
        opj_write_float(l_dest_data, l_temp);
1629
1630
0
        l_dest_data += sizeof(OPJ_FLOAT32);
1631
0
    }
1632
0
}
1633
1634
static void  opj_j2k_write_float_to_float64(const void * p_src_data,
1635
        void * p_dest_data, OPJ_UINT32 p_nb_elem)
1636
0
{
1637
0
    OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1638
0
    OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1639
0
    OPJ_UINT32 i;
1640
0
    OPJ_FLOAT64 l_temp;
1641
1642
0
    for (i = 0; i < p_nb_elem; ++i) {
1643
0
        l_temp = (OPJ_FLOAT64) * (l_src_data++);
1644
1645
0
        opj_write_double(l_dest_data, l_temp);
1646
1647
0
        l_dest_data += sizeof(OPJ_FLOAT64);
1648
0
    }
1649
0
}
1650
1651
const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)
1652
0
{
1653
0
    const j2k_prog_order_t *po;
1654
0
    for (po = j2k_prog_order_list; po->enum_prog != -1; po++) {
1655
0
        if (po->enum_prog == prg_order) {
1656
0
            return po->str_prog;
1657
0
        }
1658
0
    }
1659
0
    return po->str_prog;
1660
0
}
1661
1662
static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs,
1663
                                      OPJ_UINT32 tileno,
1664
                                      OPJ_UINT32 p_nb_pocs,
1665
                                      OPJ_UINT32 p_nb_resolutions,
1666
                                      OPJ_UINT32 p_num_comps,
1667
                                      OPJ_UINT32 p_num_layers,
1668
                                      opj_event_mgr_t * p_manager)
1669
0
{
1670
0
    OPJ_UINT32* packet_array;
1671
0
    OPJ_UINT32 index, resno, compno, layno;
1672
0
    OPJ_UINT32 i;
1673
0
    OPJ_UINT32 step_c = 1;
1674
0
    OPJ_UINT32 step_r = p_num_comps * step_c;
1675
0
    OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1676
0
    OPJ_BOOL loss = OPJ_FALSE;
1677
1678
0
    assert(p_nb_pocs > 0);
1679
1680
0
    packet_array = (OPJ_UINT32*) opj_calloc((size_t)step_l * p_num_layers,
1681
0
                                            sizeof(OPJ_UINT32));
1682
0
    if (packet_array == 00) {
1683
0
        opj_event_msg(p_manager, EVT_ERROR,
1684
0
                      "Not enough memory for checking the poc values.\n");
1685
0
        return OPJ_FALSE;
1686
0
    }
1687
1688
    /* iterate through all the pocs that match our tile of interest. */
1689
0
    for (i = 0; i < p_nb_pocs; ++i) {
1690
0
        const opj_poc_t *poc = &p_pocs[i];
1691
0
        if (tileno + 1 == poc->tile) {
1692
0
            index = step_r * poc->resno0;
1693
1694
            /* take each resolution for each poc */
1695
0
            for (resno = poc->resno0 ;
1696
0
                    resno < opj_uint_min(poc->resno1, p_nb_resolutions); ++resno) {
1697
0
                OPJ_UINT32 res_index = index + poc->compno0 * step_c;
1698
1699
                /* take each comp of each resolution for each poc */
1700
0
                for (compno = poc->compno0 ;
1701
0
                        compno < opj_uint_min(poc->compno1, p_num_comps); ++compno) {
1702
                    /* The layer index always starts at zero for every progression. */
1703
0
                    const OPJ_UINT32 layno0 = 0;
1704
0
                    OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1705
1706
                    /* and finally take each layer of each res of ... */
1707
0
                    for (layno = layno0; layno < opj_uint_min(poc->layno1, p_num_layers);
1708
0
                            ++layno) {
1709
0
                        packet_array[comp_index] = 1;
1710
0
                        comp_index += step_l;
1711
0
                    }
1712
1713
0
                    res_index += step_c;
1714
0
                }
1715
1716
0
                index += step_r;
1717
0
            }
1718
0
        }
1719
0
    }
1720
1721
0
    index = 0;
1722
0
    for (layno = 0; layno < p_num_layers ; ++layno) {
1723
0
        for (resno = 0; resno < p_nb_resolutions; ++resno) {
1724
0
            for (compno = 0; compno < p_num_comps; ++compno) {
1725
0
                loss |= (packet_array[index] != 1);
1726
#ifdef DEBUG_VERBOSE
1727
                if (packet_array[index] != 1) {
1728
                    fprintf(stderr,
1729
                            "Missing packet in POC: layno=%d resno=%d compno=%d\n",
1730
                            layno, resno, compno);
1731
                }
1732
#endif
1733
0
                index += step_c;
1734
0
            }
1735
0
        }
1736
0
    }
1737
1738
0
    if (loss) {
1739
0
        opj_event_msg(p_manager, EVT_ERROR, "Missing packets possible loss of data\n");
1740
0
    }
1741
1742
0
    opj_free(packet_array);
1743
1744
0
    return !loss;
1745
0
}
1746
1747
/* ----------------------------------------------------------------------- */
1748
1749
static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino,
1750
                                     OPJ_UINT32 tileno)
1751
0
{
1752
0
    const OPJ_CHAR *prog = 00;
1753
0
    OPJ_INT32 i;
1754
0
    OPJ_UINT32 tpnum = 1;
1755
0
    opj_tcp_t *tcp = 00;
1756
0
    opj_poc_t * l_current_poc = 00;
1757
1758
    /*  preconditions */
1759
0
    assert(tileno < (cp->tw * cp->th));
1760
0
    assert(pino < (cp->tcps[tileno].numpocs + 1));
1761
1762
    /* get the given tile coding parameter */
1763
0
    tcp = &cp->tcps[tileno];
1764
0
    assert(tcp != 00);
1765
1766
0
    l_current_poc = &(tcp->pocs[pino]);
1767
0
    assert(l_current_poc != 0);
1768
1769
    /* get the progression order as a character string */
1770
0
    prog = opj_j2k_convert_progression_order(tcp->prg);
1771
0
    assert(strlen(prog) > 0);
1772
1773
0
    if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1774
0
        for (i = 0; i < 4; ++i) {
1775
0
            switch (prog[i]) {
1776
            /* component wise */
1777
0
            case 'C':
1778
0
                tpnum *= l_current_poc->compE;
1779
0
                break;
1780
            /* resolution wise */
1781
0
            case 'R':
1782
0
                tpnum *= l_current_poc->resE;
1783
0
                break;
1784
            /* precinct wise */
1785
0
            case 'P':
1786
0
                tpnum *= l_current_poc->prcE;
1787
0
                break;
1788
            /* layer wise */
1789
0
            case 'L':
1790
0
                tpnum *= l_current_poc->layE;
1791
0
                break;
1792
0
            }
1793
            /* would we split here ? */
1794
0
            if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) {
1795
0
                cp->m_specific_param.m_enc.m_tp_pos = i;
1796
0
                break;
1797
0
            }
1798
0
        }
1799
0
    } else {
1800
0
        tpnum = 1;
1801
0
    }
1802
1803
0
    return tpnum;
1804
0
}
1805
1806
static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k,
1807
                                     opj_cp_t *cp,
1808
                                     OPJ_UINT32 * p_nb_tiles,
1809
                                     opj_image_t *image,
1810
                                     opj_event_mgr_t * p_manager
1811
                                    )
1812
0
{
1813
0
    OPJ_UINT32 pino, tileno;
1814
0
    OPJ_UINT32 l_nb_tiles;
1815
0
    opj_tcp_t *tcp;
1816
1817
    /* preconditions */
1818
0
    assert(p_nb_tiles != 00);
1819
0
    assert(cp != 00);
1820
0
    assert(image != 00);
1821
0
    assert(p_j2k != 00);
1822
0
    assert(p_manager != 00);
1823
1824
0
    OPJ_UNUSED(p_j2k);
1825
0
    OPJ_UNUSED(p_manager);
1826
1827
0
    l_nb_tiles = cp->tw * cp->th;
1828
0
    * p_nb_tiles = 0;
1829
0
    tcp = cp->tcps;
1830
1831
    /* INDEX >> */
1832
    /* TODO mergeV2: check this part which use cstr_info */
1833
    /*if (p_j2k->cstr_info) {
1834
            opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1835
1836
            for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1837
                    OPJ_UINT32 cur_totnum_tp = 0;
1838
1839
                    opj_pi_update_encoding_parameters(image,cp,tileno);
1840
1841
                    for (pino = 0; pino <= tcp->numpocs; ++pino)
1842
                    {
1843
                            OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
1844
1845
                            *p_nb_tiles = *p_nb_tiles + tp_num;
1846
1847
                            cur_totnum_tp += tp_num;
1848
                    }
1849
1850
                    tcp->m_nb_tile_parts = cur_totnum_tp;
1851
1852
                    l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1853
                    if (l_info_tile_ptr->tp == 00) {
1854
                            return OPJ_FALSE;
1855
                    }
1856
1857
                    memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1858
1859
                    l_info_tile_ptr->num_tps = cur_totnum_tp;
1860
1861
                    ++l_info_tile_ptr;
1862
                    ++tcp;
1863
            }
1864
    }
1865
    else */{
1866
0
        for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1867
0
            OPJ_UINT32 cur_totnum_tp = 0;
1868
1869
0
            opj_pi_update_encoding_parameters(image, cp, tileno);
1870
1871
0
            for (pino = 0; pino <= tcp->numpocs; ++pino) {
1872
0
                OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp, pino, tileno);
1873
1874
0
                *p_nb_tiles = *p_nb_tiles + tp_num;
1875
1876
0
                cur_totnum_tp += tp_num;
1877
0
            }
1878
0
            tcp->m_nb_tile_parts = cur_totnum_tp;
1879
1880
0
            ++tcp;
1881
0
        }
1882
0
    }
1883
1884
0
    return OPJ_TRUE;
1885
0
}
1886
1887
static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k,
1888
                                  opj_stream_private_t *p_stream,
1889
                                  opj_event_mgr_t * p_manager)
1890
0
{
1891
    /* 2 bytes will be written */
1892
0
    OPJ_BYTE * l_start_stream = 00;
1893
1894
    /* preconditions */
1895
0
    assert(p_stream != 00);
1896
0
    assert(p_j2k != 00);
1897
0
    assert(p_manager != 00);
1898
1899
0
    l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1900
1901
    /* write SOC identifier */
1902
0
    opj_write_bytes(l_start_stream, J2K_MS_SOC, 2);
1903
1904
0
    if (opj_stream_write_data(p_stream, l_start_stream, 2, p_manager) != 2) {
1905
0
        return OPJ_FALSE;
1906
0
    }
1907
1908
    /* UniPG>> */
1909
#ifdef USE_JPWL
1910
    /* update markers struct */
1911
    /*
1912
            OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1913
    */
1914
    assert(0 && "TODO");
1915
#endif /* USE_JPWL */
1916
    /* <<UniPG */
1917
1918
0
    return OPJ_TRUE;
1919
0
}
1920
1921
/**
1922
 * Reads a SOC marker (Start of Codestream)
1923
 * @param       p_j2k           the jpeg2000 file codec.
1924
 * @param       p_stream        FIXME DOC
1925
 * @param       p_manager       the user event manager.
1926
*/
1927
static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k,
1928
                                 opj_stream_private_t *p_stream,
1929
                                 opj_event_mgr_t * p_manager
1930
                                )
1931
16.6k
{
1932
16.6k
    OPJ_BYTE l_data [2];
1933
16.6k
    OPJ_UINT32 l_marker;
1934
1935
    /* preconditions */
1936
16.6k
    assert(p_j2k != 00);
1937
16.6k
    assert(p_manager != 00);
1938
16.6k
    assert(p_stream != 00);
1939
1940
16.6k
    if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
1941
34
        return OPJ_FALSE;
1942
34
    }
1943
1944
16.6k
    opj_read_bytes(l_data, &l_marker, 2);
1945
16.6k
    if (l_marker != J2K_MS_SOC) {
1946
25
        return OPJ_FALSE;
1947
25
    }
1948
1949
    /* Next marker should be a SIZ marker in the main header */
1950
16.5k
    p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1951
1952
    /* FIXME move it in a index structure included in p_j2k*/
1953
16.5k
    p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1954
1955
16.5k
    opj_event_msg(p_manager, EVT_INFO,
1956
16.5k
                  "Start to read j2k main header (%" PRId64 ").\n",
1957
16.5k
                  p_j2k->cstr_index->main_head_start);
1958
1959
    /* Add the marker to the codestream index*/
1960
16.5k
    if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC,
1961
16.5k
                                          p_j2k->cstr_index->main_head_start, 2)) {
1962
0
        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
1963
0
        return OPJ_FALSE;
1964
0
    }
1965
16.5k
    return OPJ_TRUE;
1966
16.5k
}
1967
1968
static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k,
1969
                                  opj_stream_private_t *p_stream,
1970
                                  opj_event_mgr_t * p_manager)
1971
0
{
1972
0
    OPJ_UINT32 i;
1973
0
    OPJ_UINT32 l_size_len;
1974
0
    OPJ_BYTE * l_current_ptr;
1975
0
    opj_image_t * l_image = 00;
1976
0
    opj_cp_t *cp = 00;
1977
0
    opj_image_comp_t * l_img_comp = 00;
1978
1979
    /* preconditions */
1980
0
    assert(p_stream != 00);
1981
0
    assert(p_j2k != 00);
1982
0
    assert(p_manager != 00);
1983
1984
0
    l_image = p_j2k->m_private_image;
1985
0
    cp = &(p_j2k->m_cp);
1986
0
    l_size_len = 40 + 3 * l_image->numcomps;
1987
0
    l_img_comp = l_image->comps;
1988
1989
0
    if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1990
1991
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
1992
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
1993
0
        if (! new_header_tile_data) {
1994
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
1995
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
1996
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
1997
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
1998
0
            return OPJ_FALSE;
1999
0
        }
2000
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2001
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2002
0
    }
2003
2004
0
    l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2005
2006
    /* write SOC identifier */
2007
0
    opj_write_bytes(l_current_ptr, J2K_MS_SIZ, 2);  /* SIZ */
2008
0
    l_current_ptr += 2;
2009
2010
0
    opj_write_bytes(l_current_ptr, l_size_len - 2, 2); /* L_SIZ */
2011
0
    l_current_ptr += 2;
2012
2013
0
    opj_write_bytes(l_current_ptr, cp->rsiz, 2);    /* Rsiz (capabilities) */
2014
0
    l_current_ptr += 2;
2015
2016
0
    opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2017
0
    l_current_ptr += 4;
2018
2019
0
    opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2020
0
    l_current_ptr += 4;
2021
2022
0
    opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2023
0
    l_current_ptr += 4;
2024
2025
0
    opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2026
0
    l_current_ptr += 4;
2027
2028
0
    opj_write_bytes(l_current_ptr, cp->tdx, 4);             /* XTsiz */
2029
0
    l_current_ptr += 4;
2030
2031
0
    opj_write_bytes(l_current_ptr, cp->tdy, 4);             /* YTsiz */
2032
0
    l_current_ptr += 4;
2033
2034
0
    opj_write_bytes(l_current_ptr, cp->tx0, 4);             /* XT0siz */
2035
0
    l_current_ptr += 4;
2036
2037
0
    opj_write_bytes(l_current_ptr, cp->ty0, 4);             /* YT0siz */
2038
0
    l_current_ptr += 4;
2039
2040
0
    opj_write_bytes(l_current_ptr, l_image->numcomps, 2);   /* Csiz */
2041
0
    l_current_ptr += 2;
2042
2043
0
    for (i = 0; i < l_image->numcomps; ++i) {
2044
        /* TODO here with MCT ? */
2045
0
        opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7),
2046
0
                        1);      /* Ssiz_i */
2047
0
        ++l_current_ptr;
2048
2049
0
        opj_write_bytes(l_current_ptr, l_img_comp->dx, 1);      /* XRsiz_i */
2050
0
        ++l_current_ptr;
2051
2052
0
        opj_write_bytes(l_current_ptr, l_img_comp->dy, 1);      /* YRsiz_i */
2053
0
        ++l_current_ptr;
2054
2055
0
        ++l_img_comp;
2056
0
    }
2057
2058
0
    if (opj_stream_write_data(p_stream,
2059
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len,
2060
0
                              p_manager) != l_size_len) {
2061
0
        return OPJ_FALSE;
2062
0
    }
2063
2064
0
    return OPJ_TRUE;
2065
0
}
2066
2067
/**
2068
 * Reads a SIZ marker (image and tile size)
2069
 * @param       p_j2k           the jpeg2000 file codec.
2070
 * @param       p_header_data   the data contained in the SIZ box.
2071
 * @param       p_header_size   the size of the data contained in the SIZ marker.
2072
 * @param       p_manager       the user event manager.
2073
*/
2074
static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
2075
                                 OPJ_BYTE * p_header_data,
2076
                                 OPJ_UINT32 p_header_size,
2077
                                 opj_event_mgr_t * p_manager
2078
                                )
2079
16.3k
{
2080
16.3k
    OPJ_UINT32 i;
2081
16.3k
    OPJ_UINT32 l_nb_comp;
2082
16.3k
    OPJ_UINT32 l_nb_comp_remain;
2083
16.3k
    OPJ_UINT32 l_remaining_size;
2084
16.3k
    OPJ_UINT32 l_nb_tiles;
2085
16.3k
    OPJ_UINT32 l_tmp, l_tx1, l_ty1;
2086
16.3k
    OPJ_UINT32 l_prec0, l_sgnd0;
2087
16.3k
    opj_image_t *l_image = 00;
2088
16.3k
    opj_cp_t *l_cp = 00;
2089
16.3k
    opj_image_comp_t * l_img_comp = 00;
2090
16.3k
    opj_tcp_t * l_current_tile_param = 00;
2091
2092
    /* preconditions */
2093
16.3k
    assert(p_j2k != 00);
2094
16.3k
    assert(p_manager != 00);
2095
16.3k
    assert(p_header_data != 00);
2096
2097
16.3k
    l_image = p_j2k->m_private_image;
2098
16.3k
    l_cp = &(p_j2k->m_cp);
2099
2100
    /* minimum size == 39 - 3 (= minimum component parameter) */
2101
16.3k
    if (p_header_size < 36) {
2102
12
        opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2103
12
        return OPJ_FALSE;
2104
12
    }
2105
2106
16.3k
    l_remaining_size = p_header_size - 36;
2107
16.3k
    l_nb_comp = l_remaining_size / 3;
2108
16.3k
    l_nb_comp_remain = l_remaining_size % 3;
2109
16.3k
    if (l_nb_comp_remain != 0) {
2110
2
        opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2111
2
        return OPJ_FALSE;
2112
2
    }
2113
2114
16.3k
    opj_read_bytes(p_header_data, &l_tmp,
2115
16.3k
                   2);                                                /* Rsiz (capabilities) */
2116
16.3k
    p_header_data += 2;
2117
16.3k
    l_cp->rsiz = (OPJ_UINT16) l_tmp;
2118
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4);   /* Xsiz */
2119
16.3k
    p_header_data += 4;
2120
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4);   /* Ysiz */
2121
16.3k
    p_header_data += 4;
2122
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4);   /* X0siz */
2123
16.3k
    p_header_data += 4;
2124
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4);   /* Y0siz */
2125
16.3k
    p_header_data += 4;
2126
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx,
2127
16.3k
                   4);             /* XTsiz */
2128
16.3k
    p_header_data += 4;
2129
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy,
2130
16.3k
                   4);             /* YTsiz */
2131
16.3k
    p_header_data += 4;
2132
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0,
2133
16.3k
                   4);             /* XT0siz */
2134
16.3k
    p_header_data += 4;
2135
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0,
2136
16.3k
                   4);             /* YT0siz */
2137
16.3k
    p_header_data += 4;
2138
16.3k
    opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp,
2139
16.3k
                   2);                 /* Csiz */
2140
16.3k
    p_header_data += 2;
2141
16.3k
    if (l_tmp < 16385) {
2142
16.3k
        l_image->numcomps = (OPJ_UINT16) l_tmp;
2143
16.3k
    } else {
2144
11
        opj_event_msg(p_manager, EVT_ERROR,
2145
11
                      "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2146
11
        return OPJ_FALSE;
2147
11
    }
2148
2149
16.3k
    if (l_image->numcomps != l_nb_comp) {
2150
55
        opj_event_msg(p_manager, EVT_ERROR,
2151
55
                      "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n",
2152
55
                      l_image->numcomps, l_nb_comp);
2153
55
        return OPJ_FALSE;
2154
55
    }
2155
2156
    /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
2157
    /* testcase issue427-null-image-size.jp2 */
2158
16.2k
    if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) {
2159
27
        opj_event_msg(p_manager, EVT_ERROR,
2160
27
                      "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64
2161
27
                      ")\n", (OPJ_INT64)l_image->x1 - l_image->x0,
2162
27
                      (OPJ_INT64)l_image->y1 - l_image->y0);
2163
27
        return OPJ_FALSE;
2164
27
    }
2165
    /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
2166
16.2k
    if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) {
2167
7
        opj_event_msg(p_manager, EVT_ERROR,
2168
7
                      "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx,
2169
7
                      l_cp->tdy);
2170
7
        return OPJ_FALSE;
2171
7
    }
2172
2173
    /* testcase issue427-illegal-tile-offset.jp2 */
2174
16.2k
    l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */
2175
16.2k
    l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */
2176
16.2k
    if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) ||
2177
16.2k
            (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) {
2178
140
        opj_event_msg(p_manager, EVT_ERROR,
2179
140
                      "Error with SIZ marker: illegal tile offset\n");
2180
140
        return OPJ_FALSE;
2181
140
    }
2182
16.0k
    if (!p_j2k->dump_state) {
2183
16.0k
        OPJ_UINT32 siz_w, siz_h;
2184
2185
16.0k
        siz_w = l_image->x1 - l_image->x0;
2186
16.0k
        siz_h = l_image->y1 - l_image->y0;
2187
2188
16.0k
        if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0
2189
16.0k
                && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) {
2190
110
            opj_event_msg(p_manager, EVT_ERROR,
2191
110
                          "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w,
2192
110
                          p_j2k->ihdr_h, siz_w, siz_h);
2193
110
            return OPJ_FALSE;
2194
110
        }
2195
16.0k
    }
2196
#ifdef USE_JPWL
2197
    if (l_cp->correct) {
2198
        /* if JPWL is on, we check whether TX errors have damaged
2199
          too much the SIZ parameters */
2200
        if (!(l_image->x1 * l_image->y1)) {
2201
            opj_event_msg(p_manager, EVT_ERROR,
2202
                          "JPWL: bad image size (%d x %d)\n",
2203
                          l_image->x1, l_image->y1);
2204
            if (!JPWL_ASSUME) {
2205
                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2206
                return OPJ_FALSE;
2207
            }
2208
        }
2209
2210
        /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2211
                if (l_image->numcomps != ((len - 38) / 3)) {
2212
                        opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2213
                                "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2214
                                l_image->numcomps, ((len - 38) / 3));
2215
                        if (!JPWL_ASSUME) {
2216
                                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2217
                                return OPJ_FALSE;
2218
                        }
2219
        */              /* we try to correct */
2220
        /*              opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
2221
                        if (l_image->numcomps < ((len - 38) / 3)) {
2222
                                len = 38 + 3 * l_image->numcomps;
2223
                                opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2224
                                        len);
2225
                        } else {
2226
                                l_image->numcomps = ((len - 38) / 3);
2227
                                opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2228
                                        l_image->numcomps);
2229
                        }
2230
                }
2231
        */
2232
2233
        /* update components number in the jpwl_exp_comps filed */
2234
        l_cp->exp_comps = l_image->numcomps;
2235
    }
2236
#endif /* USE_JPWL */
2237
2238
    /* Allocate the resulting image components */
2239
15.9k
    l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps,
2240
15.9k
                     sizeof(opj_image_comp_t));
2241
15.9k
    if (l_image->comps == 00) {
2242
34
        l_image->numcomps = 0;
2243
34
        opj_event_msg(p_manager, EVT_ERROR,
2244
34
                      "Not enough memory to take in charge SIZ marker\n");
2245
34
        return OPJ_FALSE;
2246
34
    }
2247
2248
15.9k
    l_img_comp = l_image->comps;
2249
2250
15.9k
    l_prec0 = 0;
2251
15.9k
    l_sgnd0 = 0;
2252
    /* Read the component information */
2253
73.9k
    for (i = 0; i < l_image->numcomps; ++i) {
2254
58.1k
        OPJ_UINT32 tmp;
2255
58.1k
        opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */
2256
58.1k
        ++p_header_data;
2257
58.1k
        l_img_comp->prec = (tmp & 0x7f) + 1;
2258
58.1k
        l_img_comp->sgnd = tmp >> 7;
2259
2260
58.1k
        if (p_j2k->dump_state == 0) {
2261
58.1k
            if (i == 0) {
2262
15.9k
                l_prec0 = l_img_comp->prec;
2263
15.9k
                l_sgnd0 = l_img_comp->sgnd;
2264
42.1k
            } else if (!l_cp->allow_different_bit_depth_sign
2265
42.1k
                       && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) {
2266
18.6k
                opj_event_msg(p_manager, EVT_WARNING,
2267
18.6k
                              "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n"
2268
18.6k
                              "        [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0,
2269
18.6k
                              i, l_img_comp->prec, l_img_comp->sgnd);
2270
18.6k
            }
2271
            /* TODO: we should perhaps also check against JP2 BPCC values */
2272
58.1k
        }
2273
58.1k
        opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */
2274
58.1k
        ++p_header_data;
2275
58.1k
        l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2276
58.1k
        opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */
2277
58.1k
        ++p_header_data;
2278
58.1k
        l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
2279
58.1k
        if (l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
2280
58.1k
                l_img_comp->dy < 1 || l_img_comp->dy > 255) {
2281
23
            opj_event_msg(p_manager, EVT_ERROR,
2282
23
                          "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n",
2283
23
                          i, l_img_comp->dx, l_img_comp->dy);
2284
23
            return OPJ_FALSE;
2285
23
        }
2286
        /* Avoids later undefined shift in computation of */
2287
        /* p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
2288
                    << (l_image->comps[i].prec - 1); */
2289
58.0k
        if (l_img_comp->prec > 31) {
2290
14
            opj_event_msg(p_manager, EVT_ERROR,
2291
14
                          "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",
2292
14
                          i, l_img_comp->prec);
2293
14
            return OPJ_FALSE;
2294
14
        }
2295
#ifdef USE_JPWL
2296
        if (l_cp->correct) {
2297
            /* if JPWL is on, we check whether TX errors have damaged
2298
                    too much the SIZ parameters, again */
2299
            if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2300
                opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2301
                              "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2302
                              i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2303
                if (!JPWL_ASSUME) {
2304
                    opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2305
                    return OPJ_FALSE;
2306
                }
2307
                /* we try to correct */
2308
                opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2309
                if (!l_image->comps[i].dx) {
2310
                    l_image->comps[i].dx = 1;
2311
                    opj_event_msg(p_manager, EVT_WARNING,
2312
                                  "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2313
                                  i, l_image->comps[i].dx);
2314
                }
2315
                if (!l_image->comps[i].dy) {
2316
                    l_image->comps[i].dy = 1;
2317
                    opj_event_msg(p_manager, EVT_WARNING,
2318
                                  "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2319
                                  i, l_image->comps[i].dy);
2320
                }
2321
            }
2322
        }
2323
#endif /* USE_JPWL */
2324
58.0k
        l_img_comp->resno_decoded =
2325
58.0k
            0;                                                          /* number of resolution decoded */
2326
58.0k
        l_img_comp->factor =
2327
58.0k
            l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2328
58.0k
        ++l_img_comp;
2329
58.0k
    }
2330
2331
15.8k
    if (l_cp->tdx == 0 || l_cp->tdy == 0) {
2332
0
        return OPJ_FALSE;
2333
0
    }
2334
2335
    /* Compute the number of tiles */
2336
15.8k
    l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2337
15.8k
    l_cp->th = opj_uint_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2338
2339
    /* Check that the number of tiles is valid */
2340
15.8k
    if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
2341
22
        opj_event_msg(p_manager, EVT_ERROR,
2342
22
                      "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
2343
22
                      l_cp->tw, l_cp->th);
2344
22
        return OPJ_FALSE;
2345
22
    }
2346
15.8k
    l_nb_tiles = l_cp->tw * l_cp->th;
2347
2348
    /* Define the tiles which will be decoded */
2349
15.8k
    if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2350
0
        p_j2k->m_specific_param.m_decoder.m_start_tile_x =
2351
0
            (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
2352
0
        p_j2k->m_specific_param.m_decoder.m_start_tile_y =
2353
0
            (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
2354
0
        p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(
2355
0
                    p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0,
2356
0
                    l_cp->tdx);
2357
0
        p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(
2358
0
                    p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0,
2359
0
                    l_cp->tdy);
2360
15.8k
    } else {
2361
15.8k
        p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2362
15.8k
        p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2363
15.8k
        p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2364
15.8k
        p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2365
15.8k
    }
2366
2367
#ifdef USE_JPWL
2368
    if (l_cp->correct) {
2369
        /* if JPWL is on, we check whether TX errors have damaged
2370
          too much the SIZ parameters */
2371
        if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) ||
2372
                (l_cp->th > l_cp->max_tiles)) {
2373
            opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2374
                          "JPWL: bad number of tiles (%d x %d)\n",
2375
                          l_cp->tw, l_cp->th);
2376
            if (!JPWL_ASSUME) {
2377
                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2378
                return OPJ_FALSE;
2379
            }
2380
            /* we try to correct */
2381
            opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
2382
            if (l_cp->tw < 1) {
2383
                l_cp->tw = 1;
2384
                opj_event_msg(p_manager, EVT_WARNING,
2385
                              "- setting %d tiles in x => HYPOTHESIS!!!\n",
2386
                              l_cp->tw);
2387
            }
2388
            if (l_cp->tw > l_cp->max_tiles) {
2389
                l_cp->tw = 1;
2390
                opj_event_msg(p_manager, EVT_WARNING,
2391
                              "- too large x, increase expectance of %d\n"
2392
                              "- setting %d tiles in x => HYPOTHESIS!!!\n",
2393
                              l_cp->max_tiles, l_cp->tw);
2394
            }
2395
            if (l_cp->th < 1) {
2396
                l_cp->th = 1;
2397
                opj_event_msg(p_manager, EVT_WARNING,
2398
                              "- setting %d tiles in y => HYPOTHESIS!!!\n",
2399
                              l_cp->th);
2400
            }
2401
            if (l_cp->th > l_cp->max_tiles) {
2402
                l_cp->th = 1;
2403
                opj_event_msg(p_manager, EVT_WARNING,
2404
                              "- too large y, increase expectance of %d to continue\n",
2405
                              "- setting %d tiles in y => HYPOTHESIS!!!\n",
2406
                              l_cp->max_tiles, l_cp->th);
2407
            }
2408
        }
2409
    }
2410
#endif /* USE_JPWL */
2411
2412
    /* memory allocations */
2413
15.8k
    l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
2414
15.8k
    if (l_cp->tcps == 00) {
2415
0
        opj_event_msg(p_manager, EVT_ERROR,
2416
0
                      "Not enough memory to take in charge SIZ marker\n");
2417
0
        return OPJ_FALSE;
2418
0
    }
2419
2420
#ifdef USE_JPWL
2421
    if (l_cp->correct) {
2422
        if (!l_cp->tcps) {
2423
            opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2424
                          "JPWL: could not alloc tcps field of cp\n");
2425
            if (!JPWL_ASSUME) {
2426
                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
2427
                return OPJ_FALSE;
2428
            }
2429
        }
2430
    }
2431
#endif /* USE_JPWL */
2432
2433
15.8k
    p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2434
15.8k
        (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2435
15.8k
    if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps  == 00) {
2436
0
        opj_event_msg(p_manager, EVT_ERROR,
2437
0
                      "Not enough memory to take in charge SIZ marker\n");
2438
0
        return OPJ_FALSE;
2439
0
    }
2440
2441
15.8k
    p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2442
15.8k
        (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS,
2443
15.8k
                                    sizeof(opj_mct_data_t));
2444
2445
15.8k
    if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2446
0
        opj_event_msg(p_manager, EVT_ERROR,
2447
0
                      "Not enough memory to take in charge SIZ marker\n");
2448
0
        return OPJ_FALSE;
2449
0
    }
2450
15.8k
    p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records =
2451
15.8k
        OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
2452
2453
15.8k
    p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2454
15.8k
        (opj_simple_mcc_decorrelation_data_t*)
2455
15.8k
        opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS,
2456
15.8k
                   sizeof(opj_simple_mcc_decorrelation_data_t));
2457
2458
15.8k
    if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2459
0
        opj_event_msg(p_manager, EVT_ERROR,
2460
0
                      "Not enough memory to take in charge SIZ marker\n");
2461
0
        return OPJ_FALSE;
2462
0
    }
2463
15.8k
    p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records =
2464
15.8k
        OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
2465
2466
    /* set up default dc level shift */
2467
73.6k
    for (i = 0; i < l_image->numcomps; ++i) {
2468
57.7k
        if (! l_image->comps[i].sgnd) {
2469
41.0k
            p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
2470
41.0k
                    << (l_image->comps[i].prec - 1);
2471
41.0k
        }
2472
57.7k
    }
2473
2474
15.8k
    l_current_tile_param = l_cp->tcps;
2475
20.3M
    for (i = 0; i < l_nb_tiles; ++i) {
2476
20.3M
        l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps,
2477
20.3M
                                      sizeof(opj_tccp_t));
2478
20.3M
        if (l_current_tile_param->tccps == 00) {
2479
0
            opj_event_msg(p_manager, EVT_ERROR,
2480
0
                          "Not enough memory to take in charge SIZ marker\n");
2481
0
            return OPJ_FALSE;
2482
0
        }
2483
2484
20.3M
        ++l_current_tile_param;
2485
20.3M
    }
2486
2487
15.8k
    p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH;
2488
15.8k
    opj_image_comp_header_update(l_image, l_cp);
2489
2490
15.8k
    return OPJ_TRUE;
2491
15.8k
}
2492
2493
static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k,
2494
                                  opj_stream_private_t *p_stream,
2495
                                  opj_event_mgr_t * p_manager
2496
                                 )
2497
0
{
2498
0
    OPJ_UINT32 l_comment_size;
2499
0
    OPJ_UINT32 l_total_com_size;
2500
0
    const OPJ_CHAR *l_comment;
2501
0
    OPJ_BYTE * l_current_ptr = 00;
2502
2503
    /* preconditions */
2504
0
    assert(p_j2k != 00);
2505
0
    assert(p_stream != 00);
2506
0
    assert(p_manager != 00);
2507
2508
0
    l_comment = p_j2k->m_cp.comment;
2509
0
    l_comment_size = (OPJ_UINT32)strlen(l_comment);
2510
0
    l_total_com_size = l_comment_size + 6;
2511
2512
0
    if (l_total_com_size >
2513
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2514
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2515
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
2516
0
        if (! new_header_tile_data) {
2517
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2518
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2519
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2520
0
            opj_event_msg(p_manager, EVT_ERROR,
2521
0
                          "Not enough memory to write the COM marker\n");
2522
0
            return OPJ_FALSE;
2523
0
        }
2524
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2525
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2526
0
    }
2527
2528
0
    l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2529
2530
0
    opj_write_bytes(l_current_ptr, J2K_MS_COM, 2);  /* COM */
2531
0
    l_current_ptr += 2;
2532
2533
0
    opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2);        /* L_COM */
2534
0
    l_current_ptr += 2;
2535
2536
0
    opj_write_bytes(l_current_ptr, 1,
2537
0
                    2);   /* General use (IS 8859-15:1999 (Latin) values) */
2538
0
    l_current_ptr += 2;
2539
2540
0
    memcpy(l_current_ptr, l_comment, l_comment_size);
2541
2542
0
    if (opj_stream_write_data(p_stream,
2543
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size,
2544
0
                              p_manager) != l_total_com_size) {
2545
0
        return OPJ_FALSE;
2546
0
    }
2547
2548
0
    return OPJ_TRUE;
2549
0
}
2550
2551
/**
2552
 * Reads a COM marker (comments)
2553
 * @param       p_j2k           the jpeg2000 file codec.
2554
 * @param       p_header_data   the data contained in the COM box.
2555
 * @param       p_header_size   the size of the data contained in the COM marker.
2556
 * @param       p_manager               the user event manager.
2557
*/
2558
static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k,
2559
                                 OPJ_BYTE * p_header_data,
2560
                                 OPJ_UINT32 p_header_size,
2561
                                 opj_event_mgr_t * p_manager
2562
                                )
2563
3.01k
{
2564
    /* preconditions */
2565
3.01k
    assert(p_j2k != 00);
2566
3.01k
    assert(p_manager != 00);
2567
3.01k
    assert(p_header_data != 00);
2568
2569
3.01k
    OPJ_UNUSED(p_j2k);
2570
3.01k
    OPJ_UNUSED(p_header_data);
2571
3.01k
    OPJ_UNUSED(p_header_size);
2572
3.01k
    OPJ_UNUSED(p_manager);
2573
2574
3.01k
    return OPJ_TRUE;
2575
3.01k
}
2576
2577
static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k,
2578
                                  opj_stream_private_t *p_stream,
2579
                                  opj_event_mgr_t * p_manager)
2580
0
{
2581
0
    opj_cp_t *l_cp = 00;
2582
0
    opj_tcp_t *l_tcp = 00;
2583
0
    OPJ_UINT32 l_code_size, l_remaining_size;
2584
0
    OPJ_BYTE * l_current_data = 00;
2585
2586
    /* preconditions */
2587
0
    assert(p_j2k != 00);
2588
0
    assert(p_manager != 00);
2589
0
    assert(p_stream != 00);
2590
2591
0
    l_cp = &(p_j2k->m_cp);
2592
0
    l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2593
0
    l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
2594
0
                  p_j2k->m_current_tile_number, 0);
2595
0
    l_remaining_size = l_code_size;
2596
2597
0
    if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2598
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2599
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
2600
0
        if (! new_header_tile_data) {
2601
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2602
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2603
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2604
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
2605
0
            return OPJ_FALSE;
2606
0
        }
2607
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2608
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2609
0
    }
2610
2611
0
    l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2612
2613
0
    opj_write_bytes(l_current_data, J2K_MS_COD, 2);           /* COD */
2614
0
    l_current_data += 2;
2615
2616
0
    opj_write_bytes(l_current_data, l_code_size - 2, 2);      /* L_COD */
2617
0
    l_current_data += 2;
2618
2619
0
    opj_write_bytes(l_current_data, l_tcp->csty, 1);          /* Scod */
2620
0
    ++l_current_data;
2621
2622
0
    opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */
2623
0
    ++l_current_data;
2624
2625
0
    opj_write_bytes(l_current_data, l_tcp->numlayers, 2);     /* SGcod (B) */
2626
0
    l_current_data += 2;
2627
2628
0
    opj_write_bytes(l_current_data, l_tcp->mct, 1);           /* SGcod (C) */
2629
0
    ++l_current_data;
2630
2631
0
    l_remaining_size -= 9;
2632
2633
0
    if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
2634
0
                                    l_current_data, &l_remaining_size, p_manager)) {
2635
0
        opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2636
0
        return OPJ_FALSE;
2637
0
    }
2638
2639
0
    if (l_remaining_size != 0) {
2640
0
        opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
2641
0
        return OPJ_FALSE;
2642
0
    }
2643
2644
0
    if (opj_stream_write_data(p_stream,
2645
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size,
2646
0
                              p_manager) != l_code_size) {
2647
0
        return OPJ_FALSE;
2648
0
    }
2649
2650
0
    return OPJ_TRUE;
2651
0
}
2652
2653
/**
2654
 * Reads a COD marker (Coding style defaults)
2655
 * @param       p_header_data   the data contained in the COD box.
2656
 * @param       p_j2k                   the jpeg2000 codec.
2657
 * @param       p_header_size   the size of the data contained in the COD marker.
2658
 * @param       p_manager               the user event manager.
2659
*/
2660
static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k,
2661
                                 OPJ_BYTE * p_header_data,
2662
                                 OPJ_UINT32 p_header_size,
2663
                                 opj_event_mgr_t * p_manager
2664
                                )
2665
19.6k
{
2666
    /* loop */
2667
19.6k
    OPJ_UINT32 i;
2668
19.6k
    OPJ_UINT32 l_tmp;
2669
19.6k
    opj_cp_t *l_cp = 00;
2670
19.6k
    opj_tcp_t *l_tcp = 00;
2671
19.6k
    opj_image_t *l_image = 00;
2672
2673
    /* preconditions */
2674
19.6k
    assert(p_header_data != 00);
2675
19.6k
    assert(p_j2k != 00);
2676
19.6k
    assert(p_manager != 00);
2677
2678
19.6k
    l_image = p_j2k->m_private_image;
2679
19.6k
    l_cp = &(p_j2k->m_cp);
2680
2681
    /* If we are in the first tile-part header of the current tile */
2682
19.6k
    l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2683
512
            &l_cp->tcps[p_j2k->m_current_tile_number] :
2684
19.6k
            p_j2k->m_specific_param.m_decoder.m_default_tcp;
2685
2686
#if 0
2687
    /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */
2688
    /* but this is no longer necessary to handle issue476.jp2 */
2689
    /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */
2690
    /* Only one COD per tile */
2691
    if (l_tcp->cod) {
2692
        opj_event_msg(p_manager, EVT_ERROR,
2693
                      "COD marker already read. No more than one COD marker per tile.\n");
2694
        return OPJ_FALSE;
2695
    }
2696
#endif
2697
19.6k
    l_tcp->cod = 1;
2698
2699
    /* Make sure room is sufficient */
2700
19.6k
    if (p_header_size < 5) {
2701
8
        opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2702
8
        return OPJ_FALSE;
2703
8
    }
2704
2705
19.6k
    opj_read_bytes(p_header_data, &l_tcp->csty, 1);         /* Scod */
2706
19.6k
    ++p_header_data;
2707
    /* Make sure we know how to decode this */
2708
19.6k
    if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP |
2709
19.6k
                                     J2K_CP_CSTY_EPH)) != 0U) {
2710
7
        opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
2711
7
        return OPJ_FALSE;
2712
7
    }
2713
19.6k
    opj_read_bytes(p_header_data, &l_tmp, 1);                       /* SGcod (A) */
2714
19.6k
    ++p_header_data;
2715
19.6k
    l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2716
    /* Make sure progression order is valid */
2717
19.6k
    if (l_tcp->prg > OPJ_CPRL) {
2718
1.14k
        opj_event_msg(p_manager, EVT_ERROR,
2719
1.14k
                      "Unknown progression order in COD marker\n");
2720
1.14k
        l_tcp->prg = OPJ_PROG_UNKNOWN;
2721
1.14k
    }
2722
19.6k
    opj_read_bytes(p_header_data, &l_tcp->numlayers, 2);    /* SGcod (B) */
2723
19.6k
    p_header_data += 2;
2724
2725
19.6k
    if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) {
2726
3
        opj_event_msg(p_manager, EVT_ERROR,
2727
3
                      "Invalid number of layers in COD marker : %d not in range [1-65535]\n",
2728
3
                      l_tcp->numlayers);
2729
3
        return OPJ_FALSE;
2730
3
    }
2731
2732
    /* If user didn't set a number layer to decode take the max specify in the codestream. */
2733
19.6k
    if (l_cp->m_specific_param.m_dec.m_layer) {
2734
0
        l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2735
19.6k
    } else {
2736
19.6k
        l_tcp->num_layers_to_decode = l_tcp->numlayers;
2737
19.6k
    }
2738
2739
19.6k
    opj_read_bytes(p_header_data, &l_tcp->mct, 1);          /* SGcod (C) */
2740
19.6k
    ++p_header_data;
2741
2742
19.6k
    if (l_tcp->mct > 1) {
2743
19
        opj_event_msg(p_manager, EVT_ERROR,
2744
19
                      "Invalid multiple component transformation\n");
2745
19
        return OPJ_FALSE;
2746
19
    }
2747
2748
19.5k
    p_header_size -= 5;
2749
101k
    for (i = 0; i < l_image->numcomps; ++i) {
2750
82.0k
        l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2751
82.0k
    }
2752
2753
19.5k
    if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size,
2754
19.5k
                                   p_manager)) {
2755
56
        opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2756
56
        return OPJ_FALSE;
2757
56
    }
2758
2759
19.5k
    if (p_header_size != 0) {
2760
17
        opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
2761
17
        return OPJ_FALSE;
2762
17
    }
2763
2764
    /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2765
19.5k
    opj_j2k_copy_tile_component_parameters(p_j2k);
2766
2767
    /* Index */
2768
#ifdef WIP_REMOVE_MSD
2769
    if (p_j2k->cstr_info) {
2770
        /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2771
        p_j2k->cstr_info->prog = l_tcp->prg;
2772
        p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2773
        p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(
2774
                                            l_image->numcomps * sizeof(OPJ_UINT32));
2775
        if (!p_j2k->cstr_info->numdecompos) {
2776
            return OPJ_FALSE;
2777
        }
2778
        for (i = 0; i < l_image->numcomps; ++i) {
2779
            p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2780
        }
2781
    }
2782
#endif
2783
2784
19.5k
    return OPJ_TRUE;
2785
19.5k
}
2786
2787
static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k,
2788
                                  OPJ_UINT32 p_comp_no,
2789
                                  opj_stream_private_t *p_stream,
2790
                                  opj_event_mgr_t * p_manager)
2791
0
{
2792
0
    OPJ_UINT32 l_coc_size, l_remaining_size;
2793
0
    OPJ_UINT32 l_comp_room;
2794
2795
    /* preconditions */
2796
0
    assert(p_j2k != 00);
2797
0
    assert(p_manager != 00);
2798
0
    assert(p_stream != 00);
2799
2800
0
    l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2801
2802
0
    l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
2803
0
                 p_j2k->m_current_tile_number, p_comp_no);
2804
2805
0
    if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2806
0
        OPJ_BYTE *new_header_tile_data;
2807
        /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
2808
                = (OPJ_BYTE*)opj_realloc(
2809
                        p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2810
                        l_coc_size);*/
2811
2812
0
        new_header_tile_data = (OPJ_BYTE *) opj_realloc(
2813
0
                                   p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
2814
0
        if (! new_header_tile_data) {
2815
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
2816
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
2817
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
2818
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
2819
0
            return OPJ_FALSE;
2820
0
        }
2821
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
2822
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2823
0
    }
2824
2825
0
    opj_j2k_write_coc_in_memory(p_j2k, p_comp_no,
2826
0
                                p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
2827
0
                                p_manager);
2828
2829
0
    if (opj_stream_write_data(p_stream,
2830
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size,
2831
0
                              p_manager) != l_coc_size) {
2832
0
        return OPJ_FALSE;
2833
0
    }
2834
2835
0
    return OPJ_TRUE;
2836
0
}
2837
2838
static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k,
2839
                                    OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
2840
0
{
2841
0
    opj_cp_t *l_cp = NULL;
2842
0
    opj_tcp_t *l_tcp = NULL;
2843
2844
    /* preconditions */
2845
0
    assert(p_j2k != 00);
2846
2847
0
    l_cp = &(p_j2k->m_cp);
2848
0
    l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2849
2850
0
    if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) {
2851
0
        return OPJ_FALSE;
2852
0
    }
2853
2854
2855
0
    return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number,
2856
0
                                       p_first_comp_no, p_second_comp_no);
2857
0
}
2858
2859
static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
2860
                                        OPJ_UINT32 p_comp_no,
2861
                                        OPJ_BYTE * p_data,
2862
                                        OPJ_UINT32 * p_data_written,
2863
                                        opj_event_mgr_t * p_manager
2864
                                       )
2865
0
{
2866
0
    opj_cp_t *l_cp = 00;
2867
0
    opj_tcp_t *l_tcp = 00;
2868
0
    OPJ_UINT32 l_coc_size, l_remaining_size;
2869
0
    OPJ_BYTE * l_current_data = 00;
2870
0
    opj_image_t *l_image = 00;
2871
0
    OPJ_UINT32 l_comp_room;
2872
2873
    /* preconditions */
2874
0
    assert(p_j2k != 00);
2875
0
    assert(p_manager != 00);
2876
2877
0
    l_cp = &(p_j2k->m_cp);
2878
0
    l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2879
0
    l_image = p_j2k->m_private_image;
2880
0
    l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2881
2882
0
    l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
2883
0
                 p_j2k->m_current_tile_number, p_comp_no);
2884
0
    l_remaining_size = l_coc_size;
2885
2886
0
    l_current_data = p_data;
2887
2888
0
    opj_write_bytes(l_current_data, J2K_MS_COC,
2889
0
                    2);                         /* COC */
2890
0
    l_current_data += 2;
2891
2892
0
    opj_write_bytes(l_current_data, l_coc_size - 2,
2893
0
                    2);                     /* L_COC */
2894
0
    l_current_data += 2;
2895
2896
0
    opj_write_bytes(l_current_data, p_comp_no, l_comp_room);        /* Ccoc */
2897
0
    l_current_data += l_comp_room;
2898
2899
0
    opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty,
2900
0
                    1);               /* Scoc */
2901
0
    ++l_current_data;
2902
2903
0
    l_remaining_size -= (5 + l_comp_room);
2904
0
    opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
2905
0
                              l_current_data, &l_remaining_size, p_manager);
2906
0
    * p_data_written = l_coc_size;
2907
0
}
2908
2909
static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
2910
0
{
2911
0
    OPJ_UINT32 i, j;
2912
0
    OPJ_UINT32 l_nb_comp;
2913
0
    OPJ_UINT32 l_nb_tiles;
2914
0
    OPJ_UINT32 l_max = 0;
2915
2916
    /* preconditions */
2917
2918
0
    l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2919
0
    l_nb_comp = p_j2k->m_private_image->numcomps;
2920
2921
0
    for (i = 0; i < l_nb_tiles; ++i) {
2922
0
        for (j = 0; j < l_nb_comp; ++j) {
2923
0
            l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j));
2924
0
        }
2925
0
    }
2926
2927
0
    return 6 + l_max;
2928
0
}
2929
2930
/**
2931
 * Reads a COC marker (Coding Style Component)
2932
 * @param       p_header_data   the data contained in the COC box.
2933
 * @param       p_j2k                   the jpeg2000 codec.
2934
 * @param       p_header_size   the size of the data contained in the COC marker.
2935
 * @param       p_manager               the user event manager.
2936
*/
2937
static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k,
2938
                                 OPJ_BYTE * p_header_data,
2939
                                 OPJ_UINT32 p_header_size,
2940
                                 opj_event_mgr_t * p_manager
2941
                                )
2942
797
{
2943
797
    opj_cp_t *l_cp = NULL;
2944
797
    opj_tcp_t *l_tcp = NULL;
2945
797
    opj_image_t *l_image = NULL;
2946
797
    OPJ_UINT32 l_comp_room;
2947
797
    OPJ_UINT32 l_comp_no;
2948
2949
    /* preconditions */
2950
797
    assert(p_header_data != 00);
2951
797
    assert(p_j2k != 00);
2952
797
    assert(p_manager != 00);
2953
2954
797
    l_cp = &(p_j2k->m_cp);
2955
797
    l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
2956
797
            ?
2957
250
            &l_cp->tcps[p_j2k->m_current_tile_number] :
2958
797
            p_j2k->m_specific_param.m_decoder.m_default_tcp;
2959
797
    l_image = p_j2k->m_private_image;
2960
2961
797
    l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2962
2963
    /* make sure room is sufficient*/
2964
797
    if (p_header_size < l_comp_room + 1) {
2965
4
        opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2966
4
        return OPJ_FALSE;
2967
4
    }
2968
793
    p_header_size -= l_comp_room + 1;
2969
2970
793
    opj_read_bytes(p_header_data, &l_comp_no,
2971
793
                   l_comp_room);                 /* Ccoc */
2972
793
    p_header_data += l_comp_room;
2973
793
    if (l_comp_no >= l_image->numcomps) {
2974
12
        opj_event_msg(p_manager, EVT_ERROR,
2975
12
                      "Error reading COC marker (bad number of components)\n");
2976
12
        return OPJ_FALSE;
2977
12
    }
2978
2979
781
    opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty,
2980
781
                   1);                  /* Scoc */
2981
781
    ++p_header_data ;
2982
2983
781
    if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size,
2984
781
                                   p_manager)) {
2985
21
        opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2986
21
        return OPJ_FALSE;
2987
21
    }
2988
2989
760
    if (p_header_size != 0) {
2990
9
        opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
2991
9
        return OPJ_FALSE;
2992
9
    }
2993
751
    return OPJ_TRUE;
2994
760
}
2995
2996
static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k,
2997
                                  opj_stream_private_t *p_stream,
2998
                                  opj_event_mgr_t * p_manager
2999
                                 )
3000
0
{
3001
0
    OPJ_UINT32 l_qcd_size, l_remaining_size;
3002
0
    OPJ_BYTE * l_current_data = 00;
3003
3004
    /* preconditions */
3005
0
    assert(p_j2k != 00);
3006
0
    assert(p_manager != 00);
3007
0
    assert(p_stream != 00);
3008
3009
0
    l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
3010
0
                 0);
3011
0
    l_remaining_size = l_qcd_size;
3012
3013
0
    if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3014
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
3015
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
3016
0
        if (! new_header_tile_data) {
3017
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3018
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3019
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3020
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
3021
0
            return OPJ_FALSE;
3022
0
        }
3023
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3024
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3025
0
    }
3026
3027
0
    l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3028
3029
0
    opj_write_bytes(l_current_data, J2K_MS_QCD, 2);         /* QCD */
3030
0
    l_current_data += 2;
3031
3032
0
    opj_write_bytes(l_current_data, l_qcd_size - 2, 2);     /* L_QCD */
3033
0
    l_current_data += 2;
3034
3035
0
    l_remaining_size -= 4;
3036
3037
0
    if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0,
3038
0
                                  l_current_data, &l_remaining_size, p_manager)) {
3039
0
        opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
3040
0
        return OPJ_FALSE;
3041
0
    }
3042
3043
0
    if (l_remaining_size != 0) {
3044
0
        opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
3045
0
        return OPJ_FALSE;
3046
0
    }
3047
3048
0
    if (opj_stream_write_data(p_stream,
3049
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size,
3050
0
                              p_manager) != l_qcd_size) {
3051
0
        return OPJ_FALSE;
3052
0
    }
3053
3054
0
    return OPJ_TRUE;
3055
0
}
3056
3057
/**
3058
 * Reads a QCD marker (Quantization defaults)
3059
 * @param       p_header_data   the data contained in the QCD box.
3060
 * @param       p_j2k                   the jpeg2000 codec.
3061
 * @param       p_header_size   the size of the data contained in the QCD marker.
3062
 * @param       p_manager               the user event manager.
3063
*/
3064
static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k,
3065
                                 OPJ_BYTE * p_header_data,
3066
                                 OPJ_UINT32 p_header_size,
3067
                                 opj_event_mgr_t * p_manager
3068
                                )
3069
22.9k
{
3070
    /* preconditions */
3071
22.9k
    assert(p_header_data != 00);
3072
22.9k
    assert(p_j2k != 00);
3073
22.9k
    assert(p_manager != 00);
3074
3075
22.9k
    if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size,
3076
22.9k
                                 p_manager)) {
3077
4
        opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3078
4
        return OPJ_FALSE;
3079
4
    }
3080
3081
22.9k
    if (p_header_size != 0) {
3082
18
        opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3083
18
        return OPJ_FALSE;
3084
18
    }
3085
3086
    /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3087
22.8k
    opj_j2k_copy_tile_quantization_parameters(p_j2k);
3088
3089
22.8k
    return OPJ_TRUE;
3090
22.9k
}
3091
3092
static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k,
3093
                                  OPJ_UINT32 p_comp_no,
3094
                                  opj_stream_private_t *p_stream,
3095
                                  opj_event_mgr_t * p_manager
3096
                                 )
3097
0
{
3098
0
    OPJ_UINT32 l_qcc_size, l_remaining_size;
3099
3100
    /* preconditions */
3101
0
    assert(p_j2k != 00);
3102
0
    assert(p_manager != 00);
3103
0
    assert(p_stream != 00);
3104
3105
0
    l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
3106
0
                 p_comp_no);
3107
0
    l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1;
3108
0
    l_remaining_size = l_qcc_size;
3109
3110
0
    if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3111
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
3112
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
3113
0
        if (! new_header_tile_data) {
3114
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3115
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3116
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3117
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
3118
0
            return OPJ_FALSE;
3119
0
        }
3120
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3121
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3122
0
    }
3123
3124
0
    opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no,
3125
0
                                p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
3126
0
                                p_manager);
3127
3128
0
    if (opj_stream_write_data(p_stream,
3129
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size,
3130
0
                              p_manager) != l_qcc_size) {
3131
0
        return OPJ_FALSE;
3132
0
    }
3133
3134
0
    return OPJ_TRUE;
3135
0
}
3136
3137
static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k,
3138
                                    OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
3139
0
{
3140
0
    return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number,
3141
0
                                     p_first_comp_no, p_second_comp_no);
3142
0
}
3143
3144
static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
3145
                                        OPJ_UINT32 p_comp_no,
3146
                                        OPJ_BYTE * p_data,
3147
                                        OPJ_UINT32 * p_data_written,
3148
                                        opj_event_mgr_t * p_manager
3149
                                       )
3150
0
{
3151
0
    OPJ_UINT32 l_qcc_size, l_remaining_size;
3152
0
    OPJ_BYTE * l_current_data = 00;
3153
3154
    /* preconditions */
3155
0
    assert(p_j2k != 00);
3156
0
    assert(p_manager != 00);
3157
3158
0
    l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
3159
0
                 p_comp_no);
3160
0
    l_remaining_size = l_qcc_size;
3161
3162
0
    l_current_data = p_data;
3163
3164
0
    opj_write_bytes(l_current_data, J2K_MS_QCC, 2);         /* QCC */
3165
0
    l_current_data += 2;
3166
3167
0
    if (p_j2k->m_private_image->numcomps <= 256) {
3168
0
        --l_qcc_size;
3169
3170
0
        opj_write_bytes(l_current_data, l_qcc_size - 2, 2);     /* L_QCC */
3171
0
        l_current_data += 2;
3172
3173
0
        opj_write_bytes(l_current_data, p_comp_no, 1);  /* Cqcc */
3174
0
        ++l_current_data;
3175
3176
        /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3177
0
        l_remaining_size -= 6;
3178
0
    } else {
3179
0
        opj_write_bytes(l_current_data, l_qcc_size - 2, 2);     /* L_QCC */
3180
0
        l_current_data += 2;
3181
3182
0
        opj_write_bytes(l_current_data, p_comp_no, 2);  /* Cqcc */
3183
0
        l_current_data += 2;
3184
3185
0
        l_remaining_size -= 6;
3186
0
    }
3187
3188
0
    opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no,
3189
0
                            l_current_data, &l_remaining_size, p_manager);
3190
3191
0
    *p_data_written = l_qcc_size;
3192
0
}
3193
3194
static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k)
3195
0
{
3196
0
    return opj_j2k_get_max_coc_size(p_j2k);
3197
0
}
3198
3199
/**
3200
 * Reads a QCC marker (Quantization component)
3201
 * @param       p_header_data   the data contained in the QCC box.
3202
 * @param       p_j2k                   the jpeg2000 codec.
3203
 * @param       p_header_size   the size of the data contained in the QCC marker.
3204
 * @param       p_manager               the user event manager.
3205
*/
3206
static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k,
3207
                                 OPJ_BYTE * p_header_data,
3208
                                 OPJ_UINT32 p_header_size,
3209
                                 opj_event_mgr_t * p_manager
3210
                                )
3211
2.53k
{
3212
2.53k
    OPJ_UINT32 l_num_comp, l_comp_no;
3213
3214
    /* preconditions */
3215
2.53k
    assert(p_header_data != 00);
3216
2.53k
    assert(p_j2k != 00);
3217
2.53k
    assert(p_manager != 00);
3218
3219
2.53k
    l_num_comp = p_j2k->m_private_image->numcomps;
3220
3221
2.53k
    if (l_num_comp <= 256) {
3222
1.90k
        if (p_header_size < 1) {
3223
2
            opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3224
2
            return OPJ_FALSE;
3225
2
        }
3226
1.89k
        opj_read_bytes(p_header_data, &l_comp_no, 1);
3227
1.89k
        ++p_header_data;
3228
1.89k
        --p_header_size;
3229
1.89k
    } else {
3230
630
        if (p_header_size < 2) {
3231
1
            opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3232
1
            return OPJ_FALSE;
3233
1
        }
3234
629
        opj_read_bytes(p_header_data, &l_comp_no, 2);
3235
629
        p_header_data += 2;
3236
629
        p_header_size -= 2;
3237
629
    }
3238
3239
#ifdef USE_JPWL
3240
    if (p_j2k->m_cp.correct) {
3241
3242
        static OPJ_UINT32 backup_compno = 0;
3243
3244
        /* compno is negative or larger than the number of components!!! */
3245
        if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3246
            opj_event_msg(p_manager, EVT_ERROR,
3247
                          "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3248
                          l_comp_no, l_num_comp);
3249
            if (!JPWL_ASSUME) {
3250
                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
3251
                return OPJ_FALSE;
3252
            }
3253
            /* we try to correct */
3254
            l_comp_no = backup_compno % l_num_comp;
3255
            opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
3256
                          "- setting component number to %d\n",
3257
                          l_comp_no);
3258
        }
3259
3260
        /* keep your private count of tiles */
3261
        backup_compno++;
3262
    };
3263
#endif /* USE_JPWL */
3264
3265
2.52k
    if (l_comp_no >= p_j2k->m_private_image->numcomps) {
3266
14
        opj_event_msg(p_manager, EVT_ERROR,
3267
14
                      "Invalid component number: %d, regarding the number of components %d\n",
3268
14
                      l_comp_no, p_j2k->m_private_image->numcomps);
3269
14
        return OPJ_FALSE;
3270
14
    }
3271
3272
2.51k
    if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size,
3273
2.51k
                                 p_manager)) {
3274
3
        opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3275
3
        return OPJ_FALSE;
3276
3
    }
3277
3278
2.51k
    if (p_header_size != 0) {
3279
6
        opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3280
6
        return OPJ_FALSE;
3281
6
    }
3282
3283
2.50k
    return OPJ_TRUE;
3284
2.51k
}
3285
3286
static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k,
3287
                                  opj_stream_private_t *p_stream,
3288
                                  opj_event_mgr_t * p_manager
3289
                                 )
3290
0
{
3291
0
    OPJ_UINT32 l_nb_comp;
3292
0
    OPJ_UINT32 l_nb_poc;
3293
0
    OPJ_UINT32 l_poc_size;
3294
0
    OPJ_UINT32 l_written_size = 0;
3295
0
    opj_tcp_t *l_tcp = 00;
3296
0
    OPJ_UINT32 l_poc_room;
3297
3298
    /* preconditions */
3299
0
    assert(p_j2k != 00);
3300
0
    assert(p_manager != 00);
3301
0
    assert(p_stream != 00);
3302
3303
0
    l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3304
0
    l_nb_comp = p_j2k->m_private_image->numcomps;
3305
0
    l_nb_poc = 1 + l_tcp->numpocs;
3306
3307
0
    if (l_nb_comp <= 256) {
3308
0
        l_poc_room = 1;
3309
0
    } else {
3310
0
        l_poc_room = 2;
3311
0
    }
3312
0
    l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3313
3314
0
    if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3315
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
3316
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
3317
0
        if (! new_header_tile_data) {
3318
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
3319
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
3320
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
3321
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
3322
0
            return OPJ_FALSE;
3323
0
        }
3324
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
3325
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3326
0
    }
3327
3328
0
    opj_j2k_write_poc_in_memory(p_j2k,
3329
0
                                p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size,
3330
0
                                p_manager);
3331
3332
0
    if (opj_stream_write_data(p_stream,
3333
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size,
3334
0
                              p_manager) != l_poc_size) {
3335
0
        return OPJ_FALSE;
3336
0
    }
3337
3338
0
    return OPJ_TRUE;
3339
0
}
3340
3341
static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
3342
                                        OPJ_BYTE * p_data,
3343
                                        OPJ_UINT32 * p_data_written,
3344
                                        opj_event_mgr_t * p_manager
3345
                                       )
3346
0
{
3347
0
    OPJ_UINT32 i;
3348
0
    OPJ_BYTE * l_current_data = 00;
3349
0
    OPJ_UINT32 l_nb_comp;
3350
0
    OPJ_UINT32 l_nb_poc;
3351
0
    OPJ_UINT32 l_poc_size;
3352
0
    opj_image_t *l_image = 00;
3353
0
    opj_tcp_t *l_tcp = 00;
3354
0
    opj_tccp_t *l_tccp = 00;
3355
0
    opj_poc_t *l_current_poc = 00;
3356
0
    OPJ_UINT32 l_poc_room;
3357
3358
    /* preconditions */
3359
0
    assert(p_j2k != 00);
3360
0
    assert(p_manager != 00);
3361
3362
0
    OPJ_UNUSED(p_manager);
3363
3364
0
    l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3365
0
    l_tccp = &l_tcp->tccps[0];
3366
0
    l_image = p_j2k->m_private_image;
3367
0
    l_nb_comp = l_image->numcomps;
3368
0
    l_nb_poc = 1 + l_tcp->numpocs;
3369
3370
0
    if (l_nb_comp <= 256) {
3371
0
        l_poc_room = 1;
3372
0
    } else {
3373
0
        l_poc_room = 2;
3374
0
    }
3375
3376
0
    l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3377
3378
0
    l_current_data = p_data;
3379
3380
0
    opj_write_bytes(l_current_data, J2K_MS_POC,
3381
0
                    2);                                   /* POC  */
3382
0
    l_current_data += 2;
3383
3384
0
    opj_write_bytes(l_current_data, l_poc_size - 2,
3385
0
                    2);                                 /* Lpoc */
3386
0
    l_current_data += 2;
3387
3388
0
    l_current_poc =  l_tcp->pocs;
3389
0
    for (i = 0; i < l_nb_poc; ++i) {
3390
0
        opj_write_bytes(l_current_data, l_current_poc->resno0,
3391
0
                        1);                                /* RSpoc_i */
3392
0
        ++l_current_data;
3393
3394
0
        opj_write_bytes(l_current_data, l_current_poc->compno0,
3395
0
                        l_poc_room);              /* CSpoc_i */
3396
0
        l_current_data += l_poc_room;
3397
3398
0
        opj_write_bytes(l_current_data, l_current_poc->layno1,
3399
0
                        2);                                /* LYEpoc_i */
3400
0
        l_current_data += 2;
3401
3402
0
        opj_write_bytes(l_current_data, l_current_poc->resno1,
3403
0
                        1);                                /* REpoc_i */
3404
0
        ++l_current_data;
3405
3406
0
        opj_write_bytes(l_current_data, l_current_poc->compno1,
3407
0
                        l_poc_room);              /* CEpoc_i */
3408
0
        l_current_data += l_poc_room;
3409
3410
0
        opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg,
3411
0
                        1);   /* Ppoc_i */
3412
0
        ++l_current_data;
3413
3414
        /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3415
0
        l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
3416
0
                                l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
3417
0
        l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
3418
0
                                l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
3419
0
        l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
3420
0
                                 l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
3421
3422
0
        ++l_current_poc;
3423
0
    }
3424
3425
0
    *p_data_written = l_poc_size;
3426
0
}
3427
3428
static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
3429
0
{
3430
0
    opj_tcp_t * l_tcp = 00;
3431
0
    OPJ_UINT32 l_nb_tiles = 0;
3432
0
    OPJ_UINT32 l_max_poc = 0;
3433
0
    OPJ_UINT32 i;
3434
3435
0
    l_tcp = p_j2k->m_cp.tcps;
3436
0
    l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3437
3438
0
    for (i = 0; i < l_nb_tiles; ++i) {
3439
0
        l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs);
3440
0
        ++l_tcp;
3441
0
    }
3442
3443
0
    ++l_max_poc;
3444
3445
0
    return 4 + 9 * l_max_poc;
3446
0
}
3447
3448
static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k)
3449
0
{
3450
0
    OPJ_UINT32 i;
3451
0
    OPJ_UINT32 l_nb_tiles;
3452
0
    OPJ_UINT32 l_max = 0;
3453
0
    opj_tcp_t * l_tcp = 00;
3454
3455
0
    l_tcp = p_j2k->m_cp.tcps;
3456
0
    l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3457
3458
0
    for (i = 0; i < l_nb_tiles; ++i) {
3459
0
        l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts);
3460
3461
0
        ++l_tcp;
3462
0
    }
3463
3464
0
    return 12 * l_max;
3465
0
}
3466
3467
static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
3468
0
{
3469
0
    OPJ_UINT32 l_nb_bytes = 0;
3470
0
    OPJ_UINT32 l_nb_comps;
3471
0
    OPJ_UINT32 l_coc_bytes, l_qcc_bytes;
3472
3473
0
    l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3474
0
    l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3475
3476
0
    if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
3477
0
        l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3478
0
        l_nb_bytes += l_nb_comps * l_coc_bytes;
3479
3480
0
        l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3481
0
        l_nb_bytes += l_nb_comps * l_qcc_bytes;
3482
0
    }
3483
3484
0
    l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3485
3486
0
    if (p_j2k->m_specific_param.m_encoder.m_PLT) {
3487
        /* Reserve space for PLT markers */
3488
3489
0
        OPJ_UINT32 i;
3490
0
        const opj_cp_t * l_cp = &(p_j2k->m_cp);
3491
0
        OPJ_UINT32 l_max_packet_count = 0;
3492
0
        for (i = 0; i < l_cp->th * l_cp->tw; ++i) {
3493
0
            l_max_packet_count = opj_uint_max(l_max_packet_count,
3494
0
                                              opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i));
3495
0
        }
3496
        /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */
3497
        /* estimate of 4 bytes for a packet size), one can write */
3498
        /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */
3499
0
        p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT =
3500
0
            6 * opj_uint_ceildiv(l_max_packet_count, 16382);
3501
        /* Maximum 5 bytes per packet to encode a full UINT32 */
3502
0
        p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT +=
3503
0
            l_nb_bytes += 5 * l_max_packet_count;
3504
0
        p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1;
3505
0
        l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
3506
0
    }
3507
3508
    /*** DEVELOPER CORNER, Add room for your headers ***/
3509
3510
0
    return l_nb_bytes;
3511
0
}
3512
3513
/**
3514
 * Reads a POC marker (Progression Order Change)
3515
 *
3516
 * @param       p_header_data   the data contained in the POC box.
3517
 * @param       p_j2k                   the jpeg2000 codec.
3518
 * @param       p_header_size   the size of the data contained in the POC marker.
3519
 * @param       p_manager               the user event manager.
3520
*/
3521
static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k,
3522
                                 OPJ_BYTE * p_header_data,
3523
                                 OPJ_UINT32 p_header_size,
3524
                                 opj_event_mgr_t * p_manager
3525
                                )
3526
1.96k
{
3527
1.96k
    OPJ_UINT32 i, l_nb_comp, l_tmp;
3528
1.96k
    opj_image_t * l_image = 00;
3529
1.96k
    OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3530
1.96k
    OPJ_UINT32 l_chunk_size, l_comp_room;
3531
3532
1.96k
    opj_cp_t *l_cp = 00;
3533
1.96k
    opj_tcp_t *l_tcp = 00;
3534
1.96k
    opj_poc_t *l_current_poc = 00;
3535
3536
    /* preconditions */
3537
1.96k
    assert(p_header_data != 00);
3538
1.96k
    assert(p_j2k != 00);
3539
1.96k
    assert(p_manager != 00);
3540
3541
1.96k
    l_image = p_j2k->m_private_image;
3542
1.96k
    l_nb_comp = l_image->numcomps;
3543
1.96k
    if (l_nb_comp <= 256) {
3544
1.92k
        l_comp_room = 1;
3545
1.92k
    } else {
3546
40
        l_comp_room = 2;
3547
40
    }
3548
1.96k
    l_chunk_size = 5 + 2 * l_comp_room;
3549
1.96k
    l_current_poc_nb = p_header_size / l_chunk_size;
3550
1.96k
    l_current_poc_remaining = p_header_size % l_chunk_size;
3551
3552
1.96k
    if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3553
9
        opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
3554
9
        return OPJ_FALSE;
3555
9
    }
3556
3557
1.95k
    l_cp = &(p_j2k->m_cp);
3558
1.95k
    l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3559
71
            &l_cp->tcps[p_j2k->m_current_tile_number] :
3560
1.95k
            p_j2k->m_specific_param.m_decoder.m_default_tcp;
3561
1.95k
    l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3562
1.95k
    l_current_poc_nb += l_old_poc_nb;
3563
3564
1.95k
    if (l_current_poc_nb >= J2K_MAX_POCS) {
3565
5
        opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
3566
5
        return OPJ_FALSE;
3567
5
    }
3568
3569
    /* now poc is in use.*/
3570
1.95k
    l_tcp->POC = 1;
3571
3572
1.95k
    l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3573
9.98k
    for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3574
8.03k
        opj_read_bytes(p_header_data, &(l_current_poc->resno0),
3575
8.03k
                       1);                               /* RSpoc_i */
3576
8.03k
        ++p_header_data;
3577
8.03k
        opj_read_bytes(p_header_data, &(l_current_poc->compno0),
3578
8.03k
                       l_comp_room);  /* CSpoc_i */
3579
8.03k
        p_header_data += l_comp_room;
3580
8.03k
        opj_read_bytes(p_header_data, &(l_current_poc->layno1),
3581
8.03k
                       2);                               /* LYEpoc_i */
3582
        /* make sure layer end is in acceptable bounds */
3583
8.03k
        l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
3584
8.03k
        p_header_data += 2;
3585
8.03k
        opj_read_bytes(p_header_data, &(l_current_poc->resno1),
3586
8.03k
                       1);                               /* REpoc_i */
3587
8.03k
        ++p_header_data;
3588
8.03k
        opj_read_bytes(p_header_data, &(l_current_poc->compno1),
3589
8.03k
                       l_comp_room);  /* CEpoc_i */
3590
8.03k
        p_header_data += l_comp_room;
3591
8.03k
        opj_read_bytes(p_header_data, &l_tmp,
3592
8.03k
                       1);                                                                 /* Ppoc_i */
3593
8.03k
        ++p_header_data;
3594
8.03k
        l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3595
        /* make sure comp is in acceptable bounds */
3596
8.03k
        l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
3597
8.03k
        ++l_current_poc;
3598
8.03k
    }
3599
3600
1.95k
    l_tcp->numpocs = l_current_poc_nb - 1;
3601
1.95k
    return OPJ_TRUE;
3602
1.95k
}
3603
3604
/**
3605
 * Reads a CRG marker (Component registration)
3606
 *
3607
 * @param       p_header_data   the data contained in the TLM box.
3608
 * @param       p_j2k                   the jpeg2000 codec.
3609
 * @param       p_header_size   the size of the data contained in the TLM marker.
3610
 * @param       p_manager               the user event manager.
3611
*/
3612
static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k,
3613
                                 OPJ_BYTE * p_header_data,
3614
                                 OPJ_UINT32 p_header_size,
3615
                                 opj_event_mgr_t * p_manager
3616
                                )
3617
265
{
3618
265
    OPJ_UINT32 l_nb_comp;
3619
    /* preconditions */
3620
265
    assert(p_header_data != 00);
3621
265
    assert(p_j2k != 00);
3622
265
    assert(p_manager != 00);
3623
3624
265
    OPJ_UNUSED(p_header_data);
3625
3626
265
    l_nb_comp = p_j2k->m_private_image->numcomps;
3627
3628
265
    if (p_header_size != l_nb_comp * 4) {
3629
5
        opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3630
5
        return OPJ_FALSE;
3631
5
    }
3632
    /* Do not care of this at the moment since only local variables are set here */
3633
    /*
3634
    for
3635
            (i = 0; i < l_nb_comp; ++i)
3636
    {
3637
            opj_read_bytes(p_header_data,&l_Xcrg_i,2);                              // Xcrg_i
3638
            p_header_data+=2;
3639
            opj_read_bytes(p_header_data,&l_Ycrg_i,2);                              // Xcrg_i
3640
            p_header_data+=2;
3641
    }
3642
    */
3643
260
    return OPJ_TRUE;
3644
265
}
3645
3646
/**
3647
 * Reads a TLM marker (Tile Length Marker)
3648
 *
3649
 * @param       p_header_data   the data contained in the TLM box.
3650
 * @param       p_j2k                   the jpeg2000 codec.
3651
 * @param       p_header_size   the size of the data contained in the TLM marker.
3652
 * @param       p_manager               the user event manager.
3653
*/
3654
static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k,
3655
                                 OPJ_BYTE * p_header_data,
3656
                                 OPJ_UINT32 p_header_size,
3657
                                 opj_event_mgr_t * p_manager
3658
                                )
3659
353
{
3660
353
    OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient,
3661
353
               l_Ptlm_size;
3662
    /* preconditions */
3663
353
    assert(p_header_data != 00);
3664
353
    assert(p_j2k != 00);
3665
353
    assert(p_manager != 00);
3666
3667
353
    OPJ_UNUSED(p_j2k);
3668
3669
353
    if (p_header_size < 2) {
3670
3
        opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3671
3
        return OPJ_FALSE;
3672
3
    }
3673
350
    p_header_size -= 2;
3674
3675
350
    opj_read_bytes(p_header_data, &l_Ztlm,
3676
350
                   1);                              /* Ztlm */
3677
350
    ++p_header_data;
3678
350
    opj_read_bytes(p_header_data, &l_Stlm,
3679
350
                   1);                              /* Stlm */
3680
350
    ++p_header_data;
3681
3682
350
    l_ST = ((l_Stlm >> 4) & 0x3);
3683
350
    l_SP = (l_Stlm >> 6) & 0x1;
3684
3685
350
    l_Ptlm_size = (l_SP + 1) * 2;
3686
350
    l_quotient = l_Ptlm_size + l_ST;
3687
3688
350
    l_tot_num_tp_remaining = p_header_size % l_quotient;
3689
3690
350
    if (l_tot_num_tp_remaining != 0) {
3691
2
        opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3692
2
        return OPJ_FALSE;
3693
2
    }
3694
    /* FIXME Do not care of this at the moment since only local variables are set here */
3695
    /*
3696
    for
3697
            (i = 0; i < l_tot_num_tp; ++i)
3698
    {
3699
            opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST);                           // Ttlm_i
3700
            p_header_data += l_ST;
3701
            opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size);            // Ptlm_i
3702
            p_header_data += l_Ptlm_size;
3703
    }*/
3704
348
    return OPJ_TRUE;
3705
350
}
3706
3707
/**
3708
 * Reads a PLM marker (Packet length, main header marker)
3709
 *
3710
 * @param       p_header_data   the data contained in the TLM box.
3711
 * @param       p_j2k                   the jpeg2000 codec.
3712
 * @param       p_header_size   the size of the data contained in the TLM marker.
3713
 * @param       p_manager               the user event manager.
3714
*/
3715
static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k,
3716
                                 OPJ_BYTE * p_header_data,
3717
                                 OPJ_UINT32 p_header_size,
3718
                                 opj_event_mgr_t * p_manager
3719
                                )
3720
6.23k
{
3721
    /* preconditions */
3722
6.23k
    assert(p_header_data != 00);
3723
6.23k
    assert(p_j2k != 00);
3724
6.23k
    assert(p_manager != 00);
3725
3726
6.23k
    OPJ_UNUSED(p_j2k);
3727
6.23k
    OPJ_UNUSED(p_header_data);
3728
3729
6.23k
    if (p_header_size < 1) {
3730
2
        opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3731
2
        return OPJ_FALSE;
3732
2
    }
3733
    /* Do not care of this at the moment since only local variables are set here */
3734
    /*
3735
    opj_read_bytes(p_header_data,&l_Zplm,1);                                        // Zplm
3736
    ++p_header_data;
3737
    --p_header_size;
3738
3739
    while
3740
            (p_header_size > 0)
3741
    {
3742
            opj_read_bytes(p_header_data,&l_Nplm,1);                                // Nplm
3743
            ++p_header_data;
3744
            p_header_size -= (1+l_Nplm);
3745
            if
3746
                    (p_header_size < 0)
3747
            {
3748
                    opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3749
                    return false;
3750
            }
3751
            for
3752
                    (i = 0; i < l_Nplm; ++i)
3753
            {
3754
                    opj_read_bytes(p_header_data,&l_tmp,1);                         // Iplm_ij
3755
                    ++p_header_data;
3756
                    // take only the last seven bytes
3757
                    l_packet_len |= (l_tmp & 0x7f);
3758
                    if
3759
                            (l_tmp & 0x80)
3760
                    {
3761
                            l_packet_len <<= 7;
3762
                    }
3763
                    else
3764
                    {
3765
            // store packet length and proceed to next packet
3766
                            l_packet_len = 0;
3767
                    }
3768
            }
3769
            if
3770
                    (l_packet_len != 0)
3771
            {
3772
                    opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3773
                    return false;
3774
            }
3775
    }
3776
    */
3777
6.22k
    return OPJ_TRUE;
3778
6.23k
}
3779
3780
/**
3781
 * Reads a PLT marker (Packet length, tile-part header)
3782
 *
3783
 * @param       p_header_data   the data contained in the PLT box.
3784
 * @param       p_j2k                   the jpeg2000 codec.
3785
 * @param       p_header_size   the size of the data contained in the PLT marker.
3786
 * @param       p_manager               the user event manager.
3787
*/
3788
static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k,
3789
                                 OPJ_BYTE * p_header_data,
3790
                                 OPJ_UINT32 p_header_size,
3791
                                 opj_event_mgr_t * p_manager
3792
                                )
3793
668
{
3794
668
    OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3795
3796
    /* preconditions */
3797
668
    assert(p_header_data != 00);
3798
668
    assert(p_j2k != 00);
3799
668
    assert(p_manager != 00);
3800
3801
668
    OPJ_UNUSED(p_j2k);
3802
3803
668
    if (p_header_size < 1) {
3804
2
        opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3805
2
        return OPJ_FALSE;
3806
2
    }
3807
3808
666
    opj_read_bytes(p_header_data, &l_Zplt, 1);              /* Zplt */
3809
666
    ++p_header_data;
3810
666
    --p_header_size;
3811
3812
69.1k
    for (i = 0; i < p_header_size; ++i) {
3813
68.4k
        opj_read_bytes(p_header_data, &l_tmp, 1);       /* Iplt_ij */
3814
68.4k
        ++p_header_data;
3815
        /* take only the last seven bytes */
3816
68.4k
        l_packet_len |= (l_tmp & 0x7f);
3817
68.4k
        if (l_tmp & 0x80) {
3818
20.7k
            l_packet_len <<= 7;
3819
47.6k
        } else {
3820
            /* store packet length and proceed to next packet */
3821
47.6k
            l_packet_len = 0;
3822
47.6k
        }
3823
68.4k
    }
3824
3825
666
    if (l_packet_len != 0) {
3826
28
        opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3827
28
        return OPJ_FALSE;
3828
28
    }
3829
3830
638
    return OPJ_TRUE;
3831
666
}
3832
3833
/**
3834
 * Reads a PPM marker (Packed packet headers, main header)
3835
 *
3836
 * @param       p_header_data   the data contained in the POC box.
3837
 * @param       p_j2k                   the jpeg2000 codec.
3838
 * @param       p_header_size   the size of the data contained in the POC marker.
3839
 * @param       p_manager               the user event manager.
3840
 */
3841
3842
static OPJ_BOOL opj_j2k_read_ppm(
3843
    opj_j2k_t *p_j2k,
3844
    OPJ_BYTE * p_header_data,
3845
    OPJ_UINT32 p_header_size,
3846
    opj_event_mgr_t * p_manager)
3847
741
{
3848
741
    opj_cp_t *l_cp = 00;
3849
741
    OPJ_UINT32 l_Z_ppm;
3850
3851
    /* preconditions */
3852
741
    assert(p_header_data != 00);
3853
741
    assert(p_j2k != 00);
3854
741
    assert(p_manager != 00);
3855
3856
    /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */
3857
741
    if (p_header_size < 2) {
3858
4
        opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3859
4
        return OPJ_FALSE;
3860
4
    }
3861
3862
737
    l_cp = &(p_j2k->m_cp);
3863
737
    l_cp->ppm = 1;
3864
3865
737
    opj_read_bytes(p_header_data, &l_Z_ppm, 1);             /* Z_ppm */
3866
737
    ++p_header_data;
3867
737
    --p_header_size;
3868
3869
    /* check allocation needed */
3870
737
    if (l_cp->ppm_markers == NULL) { /* first PPM marker */
3871
218
        OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
3872
218
        assert(l_cp->ppm_markers_count == 0U);
3873
3874
218
        l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
3875
218
        if (l_cp->ppm_markers == NULL) {
3876
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3877
0
            return OPJ_FALSE;
3878
0
        }
3879
218
        l_cp->ppm_markers_count = l_newCount;
3880
519
    } else if (l_cp->ppm_markers_count <= l_Z_ppm) {
3881
258
        OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
3882
258
        opj_ppx *new_ppm_markers;
3883
258
        new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers,
3884
258
                          l_newCount * sizeof(opj_ppx));
3885
258
        if (new_ppm_markers == NULL) {
3886
            /* clean up to be done on l_cp destruction */
3887
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3888
0
            return OPJ_FALSE;
3889
0
        }
3890
258
        l_cp->ppm_markers = new_ppm_markers;
3891
258
        memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0,
3892
258
               (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx));
3893
258
        l_cp->ppm_markers_count = l_newCount;
3894
258
    }
3895
3896
737
    if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) {
3897
        /* clean up to be done on l_cp destruction */
3898
2
        opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm);
3899
2
        return OPJ_FALSE;
3900
2
    }
3901
3902
735
    l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
3903
735
    if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) {
3904
        /* clean up to be done on l_cp destruction */
3905
0
        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3906
0
        return OPJ_FALSE;
3907
0
    }
3908
735
    l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size;
3909
735
    memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size);
3910
3911
735
    return OPJ_TRUE;
3912
735
}
3913
3914
/**
3915
 * Merges all PPM markers read (Packed headers, main header)
3916
 *
3917
 * @param       p_cp      main coding parameters.
3918
 * @param       p_manager the user event manager.
3919
 */
3920
static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager)
3921
14.2k
{
3922
14.2k
    OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining;
3923
3924
    /* preconditions */
3925
14.2k
    assert(p_cp != 00);
3926
14.2k
    assert(p_manager != 00);
3927
14.2k
    assert(p_cp->ppm_buffer == NULL);
3928
3929
14.2k
    if (p_cp->ppm == 0U) {
3930
14.0k
        return OPJ_TRUE;
3931
14.0k
    }
3932
3933
169
    l_ppm_data_size = 0U;
3934
169
    l_N_ppm_remaining = 0U;
3935
15.9k
    for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
3936
15.8k
        if (p_cp->ppm_markers[i].m_data !=
3937
15.8k
                NULL) { /* standard doesn't seem to require contiguous Zppm */
3938
544
            OPJ_UINT32 l_N_ppm;
3939
544
            OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
3940
544
            const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
3941
3942
544
            if (l_N_ppm_remaining >= l_data_size) {
3943
211
                l_N_ppm_remaining -= l_data_size;
3944
211
                l_data_size = 0U;
3945
333
            } else {
3946
333
                l_data += l_N_ppm_remaining;
3947
333
                l_data_size -= l_N_ppm_remaining;
3948
333
                l_N_ppm_remaining = 0U;
3949
333
            }
3950
3951
544
            if (l_data_size > 0U) {
3952
1.31k
                do {
3953
                    /* read Nppm */
3954
1.31k
                    if (l_data_size < 4U) {
3955
                        /* clean up to be done on l_cp destruction */
3956
4
                        opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
3957
4
                        return OPJ_FALSE;
3958
4
                    }
3959
1.30k
                    opj_read_bytes(l_data, &l_N_ppm, 4);
3960
1.30k
                    l_data += 4;
3961
1.30k
                    l_data_size -= 4;
3962
3963
1.30k
                    if (l_ppm_data_size > UINT_MAX - l_N_ppm) {
3964
2
                        opj_event_msg(p_manager, EVT_ERROR, "Too large value for Nppm\n");
3965
2
                        return OPJ_FALSE;
3966
2
                    }
3967
1.30k
                    l_ppm_data_size += l_N_ppm;
3968
1.30k
                    if (l_data_size >= l_N_ppm) {
3969
1.15k
                        l_data_size -= l_N_ppm;
3970
1.15k
                        l_data += l_N_ppm;
3971
1.15k
                    } else {
3972
150
                        l_N_ppm_remaining = l_N_ppm - l_data_size;
3973
150
                        l_data_size = 0U;
3974
150
                    }
3975
1.30k
                } while (l_data_size > 0U);
3976
333
            }
3977
544
        }
3978
15.8k
    }
3979
3980
163
    if (l_N_ppm_remaining != 0U) {
3981
        /* clean up to be done on l_cp destruction */
3982
114
        opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n");
3983
114
        return OPJ_FALSE;
3984
114
    }
3985
3986
49
    p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size);
3987
49
    if (p_cp->ppm_buffer == 00) {
3988
2
        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
3989
2
        return OPJ_FALSE;
3990
2
    }
3991
47
    p_cp->ppm_len = l_ppm_data_size;
3992
47
    l_ppm_data_size = 0U;
3993
47
    l_N_ppm_remaining = 0U;
3994
3.87k
    for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
3995
3.83k
        if (p_cp->ppm_markers[i].m_data !=
3996
3.83k
                NULL) { /* standard doesn't seem to require contiguous Zppm */
3997
200
            OPJ_UINT32 l_N_ppm;
3998
200
            OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
3999
200
            const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
4000
4001
200
            if (l_N_ppm_remaining >= l_data_size) {
4002
24
                memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
4003
24
                l_ppm_data_size += l_data_size;
4004
24
                l_N_ppm_remaining -= l_data_size;
4005
24
                l_data_size = 0U;
4006
176
            } else {
4007
176
                memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining);
4008
176
                l_ppm_data_size += l_N_ppm_remaining;
4009
176
                l_data += l_N_ppm_remaining;
4010
176
                l_data_size -= l_N_ppm_remaining;
4011
176
                l_N_ppm_remaining = 0U;
4012
176
            }
4013
4014
200
            if (l_data_size > 0U) {
4015
364
                do {
4016
                    /* read Nppm */
4017
364
                    if (l_data_size < 4U) {
4018
                        /* clean up to be done on l_cp destruction */
4019
0
                        opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
4020
0
                        return OPJ_FALSE;
4021
0
                    }
4022
364
                    opj_read_bytes(l_data, &l_N_ppm, 4);
4023
364
                    l_data += 4;
4024
364
                    l_data_size -= 4;
4025
4026
364
                    if (l_data_size >= l_N_ppm) {
4027
345
                        memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm);
4028
345
                        l_ppm_data_size += l_N_ppm;
4029
345
                        l_data_size -= l_N_ppm;
4030
345
                        l_data += l_N_ppm;
4031
345
                    } else {
4032
19
                        memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
4033
19
                        l_ppm_data_size += l_data_size;
4034
19
                        l_N_ppm_remaining = l_N_ppm - l_data_size;
4035
19
                        l_data_size = 0U;
4036
19
                    }
4037
364
                } while (l_data_size > 0U);
4038
176
            }
4039
200
            opj_free(p_cp->ppm_markers[i].m_data);
4040
200
            p_cp->ppm_markers[i].m_data = NULL;
4041
200
            p_cp->ppm_markers[i].m_data_size = 0U;
4042
200
        }
4043
3.83k
    }
4044
4045
47
    p_cp->ppm_data = p_cp->ppm_buffer;
4046
47
    p_cp->ppm_data_size = p_cp->ppm_len;
4047
4048
47
    p_cp->ppm_markers_count = 0U;
4049
47
    opj_free(p_cp->ppm_markers);
4050
47
    p_cp->ppm_markers = NULL;
4051
4052
47
    return OPJ_TRUE;
4053
47
}
4054
4055
/**
4056
 * Reads a PPT marker (Packed packet headers, tile-part header)
4057
 *
4058
 * @param       p_header_data   the data contained in the PPT box.
4059
 * @param       p_j2k                   the jpeg2000 codec.
4060
 * @param       p_header_size   the size of the data contained in the PPT marker.
4061
 * @param       p_manager               the user event manager.
4062
*/
4063
static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k,
4064
                                 OPJ_BYTE * p_header_data,
4065
                                 OPJ_UINT32 p_header_size,
4066
                                 opj_event_mgr_t * p_manager
4067
                                )
4068
224
{
4069
224
    opj_cp_t *l_cp = 00;
4070
224
    opj_tcp_t *l_tcp = 00;
4071
224
    OPJ_UINT32 l_Z_ppt;
4072
4073
    /* preconditions */
4074
224
    assert(p_header_data != 00);
4075
224
    assert(p_j2k != 00);
4076
224
    assert(p_manager != 00);
4077
4078
    /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */
4079
224
    if (p_header_size < 2) {
4080
2
        opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4081
2
        return OPJ_FALSE;
4082
2
    }
4083
4084
222
    l_cp = &(p_j2k->m_cp);
4085
222
    if (l_cp->ppm) {
4086
2
        opj_event_msg(p_manager, EVT_ERROR,
4087
2
                      "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
4088
2
        return OPJ_FALSE;
4089
2
    }
4090
4091
220
    l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4092
220
    l_tcp->ppt = 1;
4093
4094
220
    opj_read_bytes(p_header_data, &l_Z_ppt, 1);             /* Z_ppt */
4095
220
    ++p_header_data;
4096
220
    --p_header_size;
4097
4098
    /* check allocation needed */
4099
220
    if (l_tcp->ppt_markers == NULL) { /* first PPT marker */
4100
172
        OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
4101
172
        assert(l_tcp->ppt_markers_count == 0U);
4102
4103
172
        l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
4104
172
        if (l_tcp->ppt_markers == NULL) {
4105
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4106
0
            return OPJ_FALSE;
4107
0
        }
4108
172
        l_tcp->ppt_markers_count = l_newCount;
4109
172
    } else if (l_tcp->ppt_markers_count <= l_Z_ppt) {
4110
18
        OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
4111
18
        opj_ppx *new_ppt_markers;
4112
18
        new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers,
4113
18
                          l_newCount * sizeof(opj_ppx));
4114
18
        if (new_ppt_markers == NULL) {
4115
            /* clean up to be done on l_tcp destruction */
4116
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4117
0
            return OPJ_FALSE;
4118
0
        }
4119
18
        l_tcp->ppt_markers = new_ppt_markers;
4120
18
        memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0,
4121
18
               (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx));
4122
18
        l_tcp->ppt_markers_count = l_newCount;
4123
18
    }
4124
4125
220
    if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) {
4126
        /* clean up to be done on l_tcp destruction */
4127
2
        opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt);
4128
2
        return OPJ_FALSE;
4129
2
    }
4130
4131
218
    l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
4132
218
    if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) {
4133
        /* clean up to be done on l_tcp destruction */
4134
0
        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4135
0
        return OPJ_FALSE;
4136
0
    }
4137
218
    l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size;
4138
218
    memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size);
4139
218
    return OPJ_TRUE;
4140
218
}
4141
4142
/**
4143
 * Merges all PPT markers read (Packed packet headers, tile-part header)
4144
 *
4145
 * @param       p_tcp   the tile.
4146
 * @param       p_manager               the user event manager.
4147
 */
4148
static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager)
4149
15.1k
{
4150
15.1k
    OPJ_UINT32 i, l_ppt_data_size;
4151
    /* preconditions */
4152
15.1k
    assert(p_tcp != 00);
4153
15.1k
    assert(p_manager != 00);
4154
4155
15.1k
    if (p_tcp->ppt_buffer != NULL) {
4156
0
        opj_event_msg(p_manager, EVT_ERROR,
4157
0
                      "opj_j2k_merge_ppt() has already been called\n");
4158
0
        return OPJ_FALSE;
4159
0
    }
4160
4161
15.1k
    if (p_tcp->ppt == 0U) {
4162
14.9k
        return OPJ_TRUE;
4163
14.9k
    }
4164
4165
106
    l_ppt_data_size = 0U;
4166
4.43k
    for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
4167
4.32k
        l_ppt_data_size +=
4168
4.32k
            p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
4169
4.32k
    }
4170
4171
106
    p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size);
4172
106
    if (p_tcp->ppt_buffer == 00) {
4173
0
        opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
4174
0
        return OPJ_FALSE;
4175
0
    }
4176
106
    p_tcp->ppt_len = l_ppt_data_size;
4177
106
    l_ppt_data_size = 0U;
4178
4.43k
    for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
4179
4.32k
        if (p_tcp->ppt_markers[i].m_data !=
4180
4.32k
                NULL) { /* standard doesn't seem to require contiguous Zppt */
4181
116
            memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data,
4182
116
                   p_tcp->ppt_markers[i].m_data_size);
4183
116
            l_ppt_data_size +=
4184
116
                p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
4185
4186
116
            opj_free(p_tcp->ppt_markers[i].m_data);
4187
116
            p_tcp->ppt_markers[i].m_data = NULL;
4188
116
            p_tcp->ppt_markers[i].m_data_size = 0U;
4189
116
        }
4190
4.32k
    }
4191
4192
106
    p_tcp->ppt_markers_count = 0U;
4193
106
    opj_free(p_tcp->ppt_markers);
4194
106
    p_tcp->ppt_markers = NULL;
4195
4196
106
    p_tcp->ppt_data = p_tcp->ppt_buffer;
4197
106
    p_tcp->ppt_data_size = p_tcp->ppt_len;
4198
106
    return OPJ_TRUE;
4199
106
}
4200
4201
static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k,
4202
                                  opj_stream_private_t *p_stream,
4203
                                  opj_event_mgr_t * p_manager
4204
                                 )
4205
0
{
4206
0
    OPJ_BYTE * l_current_data = 00;
4207
0
    OPJ_UINT32 l_tlm_size;
4208
0
    OPJ_UINT32 size_per_tile_part;
4209
4210
    /* preconditions */
4211
0
    assert(p_j2k != 00);
4212
0
    assert(p_manager != 00);
4213
0
    assert(p_stream != 00);
4214
4215
    /* 10921 = (65535 - header_size) / size_per_tile_part where */
4216
    /* header_size = 4 and size_per_tile_part = 6 */
4217
0
    if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts > 10921) {
4218
        /* We could do more but it would require writing several TLM markers */
4219
0
        opj_event_msg(p_manager, EVT_ERROR,
4220
0
                      "A maximum of 10921 tile-parts are supported currently "
4221
0
                      "when writing TLM marker\n");
4222
0
        return OPJ_FALSE;
4223
0
    }
4224
4225
0
    if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts <= 255) {
4226
0
        size_per_tile_part = 5;
4227
0
        p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_TRUE;
4228
0
    } else {
4229
0
        size_per_tile_part = 6;
4230
0
        p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_FALSE;
4231
0
    }
4232
4233
0
    l_tlm_size = 2 + 4 + (size_per_tile_part *
4234
0
                          p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4235
4236
0
    if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4237
0
        OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
4238
0
                                             p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
4239
0
        if (! new_header_tile_data) {
4240
0
            opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
4241
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
4242
0
            p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
4243
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
4244
0
            return OPJ_FALSE;
4245
0
        }
4246
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
4247
0
        p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4248
0
    }
4249
0
    memset(p_j2k->m_specific_param.m_encoder.m_header_tile_data, 0, l_tlm_size);
4250
4251
0
    l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4252
4253
    /* change the way data is written to avoid seeking if possible */
4254
    /* TODO */
4255
0
    p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4256
4257
0
    opj_write_bytes(l_current_data, J2K_MS_TLM,
4258
0
                    2);                                   /* TLM */
4259
0
    l_current_data += 2;
4260
4261
0
    opj_write_bytes(l_current_data, l_tlm_size - 2,
4262
0
                    2);                                 /* Lpoc */
4263
0
    l_current_data += 2;
4264
4265
0
    opj_write_bytes(l_current_data, 0,
4266
0
                    1);                                                    /* Ztlm=0*/
4267
0
    ++l_current_data;
4268
4269
    /* Stlm 0x50= ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4270
    /* Stlm 0x60= ST=2(16bits-65535 tiles max),SP=1(Ptlm=32bits) */
4271
0
    opj_write_bytes(l_current_data,
4272
0
                    size_per_tile_part == 5 ? 0x50 : 0x60,
4273
0
                    1);
4274
0
    ++l_current_data;
4275
4276
    /* do nothing on the size_per_tile_part * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4277
0
    if (opj_stream_write_data(p_stream,
4278
0
                              p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size,
4279
0
                              p_manager) != l_tlm_size) {
4280
0
        return OPJ_FALSE;
4281
0
    }
4282
4283
0
    return OPJ_TRUE;
4284
0
}
4285
4286
static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
4287
                                  OPJ_BYTE * p_data,
4288
                                  OPJ_UINT32 total_data_size,
4289
                                  OPJ_UINT32 * p_data_written,
4290
                                  const opj_stream_private_t *p_stream,
4291
                                  opj_event_mgr_t * p_manager
4292
                                 )
4293
0
{
4294
    /* preconditions */
4295
0
    assert(p_j2k != 00);
4296
0
    assert(p_manager != 00);
4297
0
    assert(p_stream != 00);
4298
4299
0
    OPJ_UNUSED(p_stream);
4300
4301
0
    if (total_data_size < 12) {
4302
0
        opj_event_msg(p_manager, EVT_ERROR,
4303
0
                      "Not enough bytes in output buffer to write SOT marker\n");
4304
0
        return OPJ_FALSE;
4305
0
    }
4306
4307
0
    opj_write_bytes(p_data, J2K_MS_SOT,
4308
0
                    2);                                 /* SOT */
4309
0
    p_data += 2;
4310
4311
0
    opj_write_bytes(p_data, 10,
4312
0
                    2);                                                   /* Lsot */
4313
0
    p_data += 2;
4314
4315
0
    opj_write_bytes(p_data, p_j2k->m_current_tile_number,
4316
0
                    2);                        /* Isot */
4317
0
    p_data += 2;
4318
4319
    /* Psot  */
4320
0
    p_data += 4;
4321
4322
0
    opj_write_bytes(p_data,
4323
0
                    p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,
4324
0
                    1);                        /* TPsot */
4325
0
    ++p_data;
4326
4327
0
    opj_write_bytes(p_data,
4328
0
                    p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,
4329
0
                    1);                      /* TNsot */
4330
0
    ++p_data;
4331
4332
    /* UniPG>> */
4333
#ifdef USE_JPWL
4334
    /* update markers struct */
4335
    /*
4336
            OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4337
    */
4338
    assert(0 && "TODO");
4339
#endif /* USE_JPWL */
4340
4341
0
    * p_data_written = 12;
4342
4343
0
    return OPJ_TRUE;
4344
0
}
4345
4346
static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE *  p_header_data,
4347
                                       OPJ_UINT32  p_header_size,
4348
                                       OPJ_UINT32* p_tile_no,
4349
                                       OPJ_UINT32* p_tot_len,
4350
                                       OPJ_UINT32* p_current_part,
4351
                                       OPJ_UINT32* p_num_parts,
4352
                                       opj_event_mgr_t * p_manager)
4353
19.7k
{
4354
    /* preconditions */
4355
19.7k
    assert(p_header_data != 00);
4356
19.7k
    assert(p_manager != 00);
4357
4358
    /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4359
19.7k
    if (p_header_size != 8) {
4360
13
        opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4361
13
        return OPJ_FALSE;
4362
13
    }
4363
4364
19.7k
    opj_read_bytes(p_header_data, p_tile_no, 2);    /* Isot */
4365
19.7k
    p_header_data += 2;
4366
19.7k
    opj_read_bytes(p_header_data, p_tot_len, 4);    /* Psot */
4367
19.7k
    p_header_data += 4;
4368
19.7k
    opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */
4369
19.7k
    ++p_header_data;
4370
19.7k
    opj_read_bytes(p_header_data, p_num_parts, 1);  /* TNsot */
4371
19.7k
    ++p_header_data;
4372
19.7k
    return OPJ_TRUE;
4373
19.7k
}
4374
4375
static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k,
4376
                                 OPJ_BYTE * p_header_data,
4377
                                 OPJ_UINT32 p_header_size,
4378
                                 opj_event_mgr_t * p_manager)
4379
16.4k
{
4380
16.4k
    opj_cp_t *l_cp = 00;
4381
16.4k
    opj_tcp_t *l_tcp = 00;
4382
16.4k
    OPJ_UINT32 l_tot_len, l_num_parts = 0;
4383
16.4k
    OPJ_UINT32 l_current_part;
4384
16.4k
    OPJ_UINT32 l_tile_x, l_tile_y;
4385
4386
    /* preconditions */
4387
4388
16.4k
    assert(p_j2k != 00);
4389
16.4k
    assert(p_manager != 00);
4390
4391
16.4k
    if (! opj_j2k_get_sot_values(p_header_data, p_header_size,
4392
16.4k
                                 &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts,
4393
16.4k
                                 p_manager)) {
4394
13
        opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4395
13
        return OPJ_FALSE;
4396
13
    }
4397
#ifdef DEBUG_VERBOSE
4398
    fprintf(stderr, "SOT %d %d %d %d\n",
4399
            p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts);
4400
#endif
4401
4402
16.4k
    l_cp = &(p_j2k->m_cp);
4403
4404
    /* testcase 2.pdf.SIGFPE.706.1112 */
4405
16.4k
    if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
4406
66
        opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n",
4407
66
                      p_j2k->m_current_tile_number);
4408
66
        return OPJ_FALSE;
4409
66
    }
4410
4411
16.4k
    l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4412
16.4k
    l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4413
16.4k
    l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4414
4415
16.4k
    if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 ||
4416
16.4k
            p_j2k->m_current_tile_number == (OPJ_UINT32)
4417
16.4k
            p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) {
4418
        /* Do only this check if we decode all tile part headers, or if */
4419
        /* we decode one precise tile. Otherwise the m_current_tile_part_number */
4420
        /* might not be valid */
4421
        /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */
4422
        /* of https://github.com/uclouvain/openjpeg/issues/939 */
4423
        /* We must avoid reading twice the same tile part number for a given tile */
4424
        /* so as to avoid various issues, like opj_j2k_merge_ppt being called */
4425
        /* several times. */
4426
        /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */
4427
        /* should appear in increasing order. */
4428
16.4k
        if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) {
4429
96
            opj_event_msg(p_manager, EVT_ERROR,
4430
96
                          "Invalid tile part index for tile number %d. "
4431
96
                          "Got %d, expected %d\n",
4432
96
                          p_j2k->m_current_tile_number,
4433
96
                          l_current_part,
4434
96
                          l_tcp->m_current_tile_part_number + 1);
4435
96
            return OPJ_FALSE;
4436
96
        }
4437
16.4k
    }
4438
4439
16.3k
    l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part;
4440
4441
#ifdef USE_JPWL
4442
    if (l_cp->correct) {
4443
4444
        OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
4445
        static OPJ_UINT32 backup_tileno = 0;
4446
4447
        /* tileno is negative or larger than the number of tiles!!! */
4448
        if (tileno > (l_cp->tw * l_cp->th)) {
4449
            opj_event_msg(p_manager, EVT_ERROR,
4450
                          "JPWL: bad tile number (%d out of a maximum of %d)\n",
4451
                          tileno, (l_cp->tw * l_cp->th));
4452
            if (!JPWL_ASSUME) {
4453
                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4454
                return OPJ_FALSE;
4455
            }
4456
            /* we try to correct */
4457
            tileno = backup_tileno;
4458
            opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
4459
                          "- setting tile number to %d\n",
4460
                          tileno);
4461
        }
4462
4463
        /* keep your private count of tiles */
4464
        backup_tileno++;
4465
    };
4466
#endif /* USE_JPWL */
4467
4468
    /* look for the tile in the list of already processed tile (in parts). */
4469
    /* Optimization possible here with a more complex data structure and with the removing of tiles */
4470
    /* since the time taken by this function can only grow at the time */
4471
4472
    /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4473
16.3k
    if ((l_tot_len != 0) && (l_tot_len < 14)) {
4474
8
        if (l_tot_len ==
4475
8
                12) { /* MSD: Special case for the PHR data which are read by kakadu*/
4476
4
            opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n",
4477
4
                          l_tot_len);
4478
4
        } else {
4479
4
            opj_event_msg(p_manager, EVT_ERROR,
4480
4
                          "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
4481
4
            return OPJ_FALSE;
4482
4
        }
4483
8
    }
4484
4485
#ifdef USE_JPWL
4486
    if (l_cp->correct) {
4487
4488
        /* totlen is negative or larger than the bytes left!!! */
4489
        if (/*(l_tot_len < 0) ||*/ (l_tot_len >
4490
                                    p_header_size)) {   /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
4491
            opj_event_msg(p_manager, EVT_ERROR,
4492
                          "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4493
                          l_tot_len,
4494
                          p_header_size);  /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4495
            if (!JPWL_ASSUME) {
4496
                opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
4497
                return OPJ_FALSE;
4498
            }
4499
            /* we try to correct */
4500
            l_tot_len = 0;
4501
            opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
4502
                          "- setting Psot to %d => assuming it is the last tile\n",
4503
                          l_tot_len);
4504
        }
4505
    };
4506
#endif /* USE_JPWL */
4507
4508
    /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4509
16.3k
    if (!l_tot_len) {
4510
12.6k
        opj_event_msg(p_manager, EVT_INFO,
4511
12.6k
                      "Psot value of the current tile-part is equal to zero, "
4512
12.6k
                      "we assuming it is the last tile-part of the codestream.\n");
4513
12.6k
        p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4514
12.6k
    }
4515
4516
16.3k
    if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) {
4517
        /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */
4518
3
        opj_event_msg(p_manager, EVT_ERROR,
4519
3
                      "In SOT marker, TPSot (%d) is not valid regards to the previous "
4520
3
                      "number of tile-part (%d), giving up\n", l_current_part,
4521
3
                      l_tcp->m_nb_tile_parts);
4522
3
        p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4523
3
        return OPJ_FALSE;
4524
3
    }
4525
4526
16.3k
    if (l_num_parts !=
4527
16.3k
            0) { /* Number of tile-part header is provided by this tile-part header */
4528
13.5k
        l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction;
4529
        /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4530
         * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4531
13.5k
        if (l_tcp->m_nb_tile_parts) {
4532
56
            if (l_current_part >= l_tcp->m_nb_tile_parts) {
4533
0
                opj_event_msg(p_manager, EVT_ERROR,
4534
0
                              "In SOT marker, TPSot (%d) is not valid regards to the current "
4535
0
                              "number of tile-part (%d), giving up\n", l_current_part,
4536
0
                              l_tcp->m_nb_tile_parts);
4537
0
                p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4538
0
                return OPJ_FALSE;
4539
0
            }
4540
56
        }
4541
13.5k
        if (l_current_part >= l_num_parts) {
4542
            /* testcase 451.pdf.SIGSEGV.ce9.3723 */
4543
3
            opj_event_msg(p_manager, EVT_ERROR,
4544
3
                          "In SOT marker, TPSot (%d) is not valid regards to the current "
4545
3
                          "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts);
4546
3
            p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4547
3
            return OPJ_FALSE;
4548
3
        }
4549
13.5k
        l_tcp->m_nb_tile_parts = l_num_parts;
4550
13.5k
    }
4551
4552
    /* If know the number of tile part header we will check if we didn't read the last*/
4553
16.3k
    if (l_tcp->m_nb_tile_parts) {
4554
13.5k
        if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4555
3.83k
            p_j2k->m_specific_param.m_decoder.m_can_decode =
4556
3.83k
                1; /* Process the last tile-part header*/
4557
3.83k
        }
4558
13.5k
    }
4559
4560
16.3k
    if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4561
        /* Keep the size of data to skip after this marker */
4562
3.65k
        p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len -
4563
3.65k
                12; /* SOT_marker_size = 12 */
4564
12.6k
    } else {
4565
        /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4566
12.6k
        p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4567
12.6k
    }
4568
4569
16.3k
    p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4570
4571
    /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4572
16.3k
    if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4573
16.3k
        p_j2k->m_specific_param.m_decoder.m_skip_data =
4574
16.3k
            (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4575
16.3k
            || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4576
16.3k
            || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4577
16.3k
            || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4578
16.3k
    } else {
4579
0
        assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0);
4580
0
        p_j2k->m_specific_param.m_decoder.m_skip_data =
4581
0
            (p_j2k->m_current_tile_number != (OPJ_UINT32)
4582
0
             p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4583
0
    }
4584
4585
    /* Index */
4586
16.3k
    if (p_j2k->cstr_index) {
4587
16.3k
        assert(p_j2k->cstr_index->tile_index != 00);
4588
16.3k
        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno =
4589
16.3k
            p_j2k->m_current_tile_number;
4590
16.3k
        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno =
4591
16.3k
            l_current_part;
4592
4593
16.3k
        if (l_num_parts != 0) {
4594
13.5k
            p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps =
4595
13.5k
                l_num_parts;
4596
13.5k
            p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
4597
13.5k
                l_num_parts;
4598
4599
13.5k
            if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4600
13.4k
                p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4601
13.4k
                    (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4602
13.4k
                if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4603
0
                    opj_event_msg(p_manager, EVT_ERROR,
4604
0
                                  "Not enough memory to read SOT marker. Tile index allocation failed\n");
4605
0
                    return OPJ_FALSE;
4606
0
                }
4607
13.4k
            } else {
4608
53
                opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
4609
53
                                                   p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4610
53
                                                   l_num_parts * sizeof(opj_tp_index_t));
4611
53
                if (! new_tp_index) {
4612
0
                    opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4613
0
                    p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4614
0
                    opj_event_msg(p_manager, EVT_ERROR,
4615
0
                                  "Not enough memory to read SOT marker. Tile index allocation failed\n");
4616
0
                    return OPJ_FALSE;
4617
0
                }
4618
53
                p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4619
53
                    new_tp_index;
4620
53
            }
4621
13.5k
        } else {
4622
2.76k
            /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4623
4624
2.76k
                if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4625
2.76k
                    p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4626
2.76k
                    p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4627
2.76k
                        (opj_tp_index_t*)opj_calloc(
4628
2.76k
                            p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4629
2.76k
                            sizeof(opj_tp_index_t));
4630
2.76k
                    if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4631
0
                        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4632
0
                        opj_event_msg(p_manager, EVT_ERROR,
4633
0
                                      "Not enough memory to read SOT marker. Tile index allocation failed\n");
4634
0
                        return OPJ_FALSE;
4635
0
                    }
4636
2.76k
                }
4637
4638
2.76k
                if (l_current_part >=
4639
2.76k
                        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) {
4640
0
                    opj_tp_index_t *new_tp_index;
4641
0
                    p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
4642
0
                        l_current_part + 1;
4643
0
                    new_tp_index = (opj_tp_index_t *) opj_realloc(
4644
0
                                       p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4645
0
                                       p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps *
4646
0
                                       sizeof(opj_tp_index_t));
4647
0
                    if (! new_tp_index) {
4648
0
                        opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
4649
0
                        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
4650
0
                        p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
4651
0
                        opj_event_msg(p_manager, EVT_ERROR,
4652
0
                                      "Not enough memory to read SOT marker. Tile index allocation failed\n");
4653
0
                        return OPJ_FALSE;
4654
0
                    }
4655
0
                    p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4656
0
                        new_tp_index;
4657
0
                }
4658
2.76k
            }
4659
4660
2.76k
        }
4661
4662
16.3k
    }
4663
4664
    /* 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 */
4665
    /* if (p_j2k->cstr_info) {
4666
       if (l_tcp->first) {
4667
       if (tileno == 0) {
4668
       p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4669
       }
4670
4671
       p_j2k->cstr_info->tile[tileno].tileno = tileno;
4672
       p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4673
       p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4674
       p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4675
4676
       if (numparts) {
4677
       p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4678
       }
4679
       else {
4680
       p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4681
       }
4682
       }
4683
       else {
4684
       p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4685
       }
4686
4687
       p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4688
       p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4689
       p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4690
       }*/
4691
16.3k
    return OPJ_TRUE;
4692
16.3k
}
4693
4694
/**
4695
 * Write one or more PLT markers in the provided buffer
4696
 */
4697
static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k,
4698
        opj_tcd_marker_info_t* marker_info,
4699
        OPJ_BYTE * p_data,
4700
        OPJ_UINT32 * p_data_written,
4701
        opj_event_mgr_t * p_manager)
4702
0
{
4703
0
    OPJ_BYTE Zplt = 0;
4704
0
    OPJ_UINT16 Lplt;
4705
0
    OPJ_BYTE* p_data_start = p_data;
4706
0
    OPJ_BYTE* p_data_Lplt = p_data + 2;
4707
0
    OPJ_UINT32 i;
4708
4709
0
    OPJ_UNUSED(p_j2k);
4710
4711
0
    opj_write_bytes(p_data, J2K_MS_PLT, 2);
4712
0
    p_data += 2;
4713
4714
    /* Reserve space for Lplt */
4715
0
    p_data += 2;
4716
4717
0
    opj_write_bytes(p_data, Zplt, 1);
4718
0
    p_data += 1;
4719
4720
0
    Lplt = 3;
4721
4722
0
    for (i = 0; i < marker_info->packet_count; i++) {
4723
0
        OPJ_BYTE var_bytes[5];
4724
0
        OPJ_UINT8 var_bytes_size = 0;
4725
0
        OPJ_UINT32 packet_size = marker_info->p_packet_size[i];
4726
4727
        /* Packet size written in variable-length way, starting with LSB */
4728
0
        var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f);
4729
0
        var_bytes_size ++;
4730
0
        packet_size >>= 7;
4731
0
        while (packet_size > 0) {
4732
0
            var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80);
4733
0
            var_bytes_size ++;
4734
0
            packet_size >>= 7;
4735
0
        }
4736
4737
        /* Check if that can fit in the current PLT marker. If not, finish */
4738
        /* current one, and start a new one */
4739
0
        if (Lplt + var_bytes_size > 65535) {
4740
0
            if (Zplt == 255) {
4741
0
                opj_event_msg(p_manager, EVT_ERROR,
4742
0
                              "More than 255 PLT markers would be needed for current tile-part !\n");
4743
0
                return OPJ_FALSE;
4744
0
            }
4745
4746
            /* Patch Lplt */
4747
0
            opj_write_bytes(p_data_Lplt, Lplt, 2);
4748
4749
            /* Start new segment */
4750
0
            opj_write_bytes(p_data, J2K_MS_PLT, 2);
4751
0
            p_data += 2;
4752
4753
            /* Reserve space for Lplt */
4754
0
            p_data_Lplt = p_data;
4755
0
            p_data += 2;
4756
4757
0
            Zplt ++;
4758
0
            opj_write_bytes(p_data, Zplt, 1);
4759
0
            p_data += 1;
4760
4761
0
            Lplt = 3;
4762
0
        }
4763
4764
0
        Lplt = (OPJ_UINT16)(Lplt + var_bytes_size);
4765
4766
        /* Serialize variable-length packet size, starting with MSB */
4767
0
        for (; var_bytes_size > 0; --var_bytes_size) {
4768
0
            opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1);
4769
0
            p_data += 1;
4770
0
        }
4771
0
    }
4772
4773
0
    *p_data_written = (OPJ_UINT32)(p_data - p_data_start);
4774
4775
    /* Patch Lplt */
4776
0
    opj_write_bytes(p_data_Lplt, Lplt, 2);
4777
4778
0
    return OPJ_TRUE;
4779
0
}
4780
4781
static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
4782
                                  opj_tcd_t * p_tile_coder,
4783
                                  OPJ_BYTE * p_data,
4784
                                  OPJ_UINT32 * p_data_written,
4785
                                  OPJ_UINT32 total_data_size,
4786
                                  const opj_stream_private_t *p_stream,
4787
                                  opj_event_mgr_t * p_manager
4788
                                 )
4789
0
{
4790
0
    opj_codestream_info_t *l_cstr_info = 00;
4791
0
    OPJ_UINT32 l_remaining_data;
4792
0
    opj_tcd_marker_info_t* marker_info = NULL;
4793
4794
    /* preconditions */
4795
0
    assert(p_j2k != 00);
4796
0
    assert(p_manager != 00);
4797
0
    assert(p_stream != 00);
4798
4799
0
    OPJ_UNUSED(p_stream);
4800
4801
0
    if (total_data_size < 4) {
4802
0
        opj_event_msg(p_manager, EVT_ERROR,
4803
0
                      "Not enough bytes in output buffer to write SOD marker\n");
4804
0
        return OPJ_FALSE;
4805
0
    }
4806
4807
0
    opj_write_bytes(p_data, J2K_MS_SOD,
4808
0
                    2);                                 /* SOD */
4809
4810
    /* make room for the EOF marker */
4811
0
    l_remaining_data =  total_data_size - 4;
4812
4813
    /* update tile coder */
4814
0
    p_tile_coder->tp_num =
4815
0
        p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4816
0
    p_tile_coder->cur_tp_num =
4817
0
        p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4818
4819
    /* INDEX >> */
4820
    /* TODO mergeV2: check this part which use cstr_info */
4821
    /*l_cstr_info = p_j2k->cstr_info;
4822
    if (l_cstr_info) {
4823
            if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4824
                    //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4825
                    l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4826
            }
4827
            else {*/
4828
    /*
4829
    TODO
4830
    if
4831
            (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4832
    {
4833
            cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4834
    }*/
4835
    /*}*/
4836
    /* UniPG>> */
4837
#ifdef USE_JPWL
4838
    /* update markers struct */
4839
    /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4840
    */
4841
    assert(0 && "TODO");
4842
#endif /* USE_JPWL */
4843
    /* <<UniPG */
4844
    /*}*/
4845
    /* << INDEX */
4846
4847
0
    if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4848
0
        p_tile_coder->tcd_image->tiles->packno = 0;
4849
#ifdef deadcode
4850
        if (l_cstr_info) {
4851
            l_cstr_info->packno = 0;
4852
        }
4853
#endif
4854
0
    }
4855
4856
0
    *p_data_written = 0;
4857
4858
0
    if (p_j2k->m_specific_param.m_encoder.m_PLT) {
4859
0
        marker_info = opj_tcd_marker_info_create(
4860
0
                          p_j2k->m_specific_param.m_encoder.m_PLT);
4861
0
        if (marker_info == NULL) {
4862
0
            opj_event_msg(p_manager, EVT_ERROR,
4863
0
                          "Cannot encode tile: opj_tcd_marker_info_create() failed\n");
4864
0
            return OPJ_FALSE;
4865
0
        }
4866
0
    }
4867
4868
0
    if (l_remaining_data <
4869
0
            p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) {
4870
0
        opj_event_msg(p_manager, EVT_ERROR,
4871
0
                      "Not enough bytes in output buffer to write SOD marker\n");
4872
0
        opj_tcd_marker_info_destroy(marker_info);
4873
0
        return OPJ_FALSE;
4874
0
    }
4875
0
    l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
4876
4877
0
    if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number,
4878
0
                              p_data + 2,
4879
0
                              p_data_written, l_remaining_data, l_cstr_info,
4880
0
                              marker_info,
4881
0
                              p_manager)) {
4882
0
        opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
4883
0
        opj_tcd_marker_info_destroy(marker_info);
4884
0
        return OPJ_FALSE;
4885
0
    }
4886
4887
    /* For SOD */
4888
0
    *p_data_written += 2;
4889
4890
0
    if (p_j2k->m_specific_param.m_encoder.m_PLT) {
4891
0
        OPJ_UINT32 l_data_written_PLT = 0;
4892
0
        OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc(
4893
0
                                     p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
4894
0
        if (!p_PLT_buffer) {
4895
0
            opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n");
4896
0
            opj_tcd_marker_info_destroy(marker_info);
4897
0
            return OPJ_FALSE;
4898
0
        }
4899
0
        if (!opj_j2k_write_plt_in_memory(p_j2k,
4900
0
                                         marker_info,
4901
0
                                         p_PLT_buffer,
4902
0
                                         &l_data_written_PLT,
4903
0
                                         p_manager)) {
4904
0
            opj_tcd_marker_info_destroy(marker_info);
4905
0
            opj_free(p_PLT_buffer);
4906
0
            return OPJ_FALSE;
4907
0
        }
4908
4909
0
        assert(l_data_written_PLT <=
4910
0
               p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
4911
4912
        /* Move PLT marker(s) before SOD */
4913
0
        memmove(p_data + l_data_written_PLT, p_data, *p_data_written);
4914
0
        memcpy(p_data, p_PLT_buffer, l_data_written_PLT);
4915
0
        opj_free(p_PLT_buffer);
4916
0
        *p_data_written += l_data_written_PLT;
4917
0
    }
4918
4919
0
    opj_tcd_marker_info_destroy(marker_info);
4920
4921
0
    return OPJ_TRUE;
4922
0
}
4923
4924
static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
4925
                                 opj_stream_private_t *p_stream,
4926
                                 opj_event_mgr_t * p_manager
4927
                                )
4928
17.2k
{
4929
17.2k
    OPJ_SIZE_T l_current_read_size;
4930
17.2k
    opj_codestream_index_t * l_cstr_index = 00;
4931
17.2k
    OPJ_BYTE ** l_current_data = 00;
4932
17.2k
    opj_tcp_t * l_tcp = 00;
4933
17.2k
    OPJ_UINT32 * l_tile_len = 00;
4934
17.2k
    OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
4935
4936
    /* preconditions */
4937
17.2k
    assert(p_j2k != 00);
4938
17.2k
    assert(p_manager != 00);
4939
17.2k
    assert(p_stream != 00);
4940
4941
17.2k
    l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4942
4943
17.2k
    if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4944
        /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4945
        // but we are in the last tile part,
4946
        // so its result will fit on OPJ_UINT32 unless we find
4947
        // a file with a single tile part of more than 4 GB...*/
4948
12.4k
        p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(
4949
12.4k
                    opj_stream_get_number_byte_left(p_stream) - 2);
4950
12.4k
    } else {
4951
        /* Check to avoid pass the limit of OPJ_UINT32 */
4952
4.78k
        if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) {
4953
3.22k
            p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4954
3.22k
        } else {
4955
            /* MSD: case commented to support empty SOT marker (PHR data) */
4956
1.56k
        }
4957
4.78k
    }
4958
4959
17.2k
    l_current_data = &(l_tcp->m_data);
4960
17.2k
    l_tile_len = &l_tcp->m_data_size;
4961
4962
    /* Patch to support new PHR data */
4963
17.2k
    if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
4964
        /* If we are here, we'll try to read the data after allocation */
4965
        /* Check enough bytes left in stream before allocation */
4966
15.6k
        if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length >
4967
15.6k
                opj_stream_get_number_byte_left(p_stream)) {
4968
164
            if (p_j2k->m_cp.strict) {
4969
164
                opj_event_msg(p_manager, EVT_ERROR,
4970
164
                              "Tile part length size inconsistent with stream length\n");
4971
164
                return OPJ_FALSE;
4972
164
            } else {
4973
0
                opj_event_msg(p_manager, EVT_WARNING,
4974
0
                              "Tile part length size inconsistent with stream length\n");
4975
0
            }
4976
164
        }
4977
15.4k
        if (p_j2k->m_specific_param.m_decoder.m_sot_length >
4978
15.4k
                UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) {
4979
0
            opj_event_msg(p_manager, EVT_ERROR,
4980
0
                          "p_j2k->m_specific_param.m_decoder.m_sot_length > "
4981
0
                          "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA");
4982
0
            return OPJ_FALSE;
4983
0
        }
4984
        /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */
4985
        /* do so that opj_mqc_init_dec_common() can safely add a synthetic */
4986
        /* 0xFFFF marker. */
4987
15.4k
        if (! *l_current_data) {
4988
            /* LH: oddly enough, in this path, l_tile_len!=0.
4989
             * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
4990
             */
4991
15.3k
            *l_current_data = (OPJ_BYTE*) opj_malloc(
4992
15.3k
                                  p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA);
4993
15.3k
        } else {
4994
181
            OPJ_BYTE *l_new_current_data;
4995
181
            if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA -
4996
181
                    p_j2k->m_specific_param.m_decoder.m_sot_length) {
4997
0
                opj_event_msg(p_manager, EVT_ERROR,
4998
0
                              "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - "
4999
0
                              "p_j2k->m_specific_param.m_decoder.m_sot_length");
5000
0
                return OPJ_FALSE;
5001
0
            }
5002
5003
181
            l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data,
5004
181
                                 *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length +
5005
181
                                 OPJ_COMMON_CBLK_DATA_EXTRA);
5006
181
            if (! l_new_current_data) {
5007
0
                opj_free(*l_current_data);
5008
                /*nothing more is done as l_current_data will be set to null, and just
5009
                  afterward we enter in the error path
5010
                  and the actual tile_len is updated (committed) at the end of the
5011
                  function. */
5012
0
            }
5013
181
            *l_current_data = l_new_current_data;
5014
181
        }
5015
5016
15.4k
        if (*l_current_data == 00) {
5017
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
5018
0
            return OPJ_FALSE;
5019
0
        }
5020
15.4k
    } else {
5021
1.59k
        l_sot_length_pb_detected = OPJ_TRUE;
5022
1.59k
    }
5023
5024
    /* Index */
5025
17.0k
    l_cstr_index = p_j2k->cstr_index;
5026
17.0k
    if (l_cstr_index) {
5027
17.0k
        OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5028
5029
17.0k
        OPJ_UINT32 l_current_tile_part =
5030
17.0k
            l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5031
17.0k
        l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header
5032
17.0k
            =
5033
17.0k
                l_current_pos;
5034
17.0k
        l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos
5035
17.0k
            =
5036
17.0k
                l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5037
5038
17.0k
        if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
5039
17.0k
                                              l_cstr_index,
5040
17.0k
                                              J2K_MS_SOD,
5041
17.0k
                                              l_current_pos,
5042
17.0k
                                              p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
5043
0
            opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
5044
0
            return OPJ_FALSE;
5045
0
        }
5046
5047
        /*l_cstr_index->packno = 0;*/
5048
17.0k
    }
5049
5050
    /* Patch to support new PHR data */
5051
17.0k
    if (!l_sot_length_pb_detected) {
5052
15.4k
        l_current_read_size = opj_stream_read_data(
5053
15.4k
                                  p_stream,
5054
15.4k
                                  *l_current_data + *l_tile_len,
5055
15.4k
                                  p_j2k->m_specific_param.m_decoder.m_sot_length,
5056
15.4k
                                  p_manager);
5057
15.4k
    } else {
5058
1.59k
        l_current_read_size = 0;
5059
1.59k
    }
5060
5061
17.0k
    if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5062
0
        p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5063
17.0k
    } else {
5064
17.0k
        p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5065
17.0k
    }
5066
5067
17.0k
    *l_tile_len += (OPJ_UINT32)l_current_read_size;
5068
5069
17.0k
    return OPJ_TRUE;
5070
17.0k
}
5071
5072
static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
5073
                                  OPJ_UINT32 p_tile_no,
5074
                                  OPJ_UINT32 p_comp_no,
5075
                                  OPJ_UINT32 nb_comps,
5076
                                  opj_stream_private_t *p_stream,
5077
                                  opj_event_mgr_t * p_manager
5078
                                 )
5079
0
{
5080
0
    OPJ_BYTE * l_current_data = 00;
5081
0
    OPJ_UINT32 l_rgn_size;
5082
0
    opj_cp_t *l_cp = 00;
5083
0
    opj_tcp_t *l_tcp = 00;
5084
0
    opj_tccp_t *l_tccp = 00;
5085
0
    OPJ_UINT32 l_comp_room;
5086
5087
    /* preconditions */
5088
0
    assert(p_j2k != 00);
5089
0
    assert(p_manager != 00);
5090
0
    assert(p_stream != 00);
5091
5092
0
    l_cp = &(p_j2k->m_cp);
5093
0
    l_tcp = &l_cp->tcps[p_tile_no];
5094
0
    l_tccp = &l_tcp->tccps[p_comp_no];
5095
5096
0
    if (nb_comps <= 256) {
5097
0
        l_comp_room = 1;
5098
0
    } else {
5099
0
        l_comp_room = 2;
5100
0
    }
5101