Coverage Report

Created: 2026-05-16 07:21

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