Coverage Report

Created: 2026-09-13 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openjpeg/src/lib/openjp2/pi.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) 2006-2007, Parvatha Elangovan
15
 * All rights reserved.
16
 *
17
 * Redistribution and use in source and binary forms, with or without
18
 * modification, are permitted provided that the following conditions
19
 * are met:
20
 * 1. Redistributions of source code must retain the above copyright
21
 *    notice, this list of conditions and the following disclaimer.
22
 * 2. Redistributions in binary form must reproduce the above copyright
23
 *    notice, this list of conditions and the following disclaimer in the
24
 *    documentation and/or other materials provided with the distribution.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 * POSSIBILITY OF SUCH DAMAGE.
37
 */
38
39
#define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32
40
41
#include "opj_includes.h"
42
43
/** @defgroup PI PI - Implementation of a packet iterator */
44
/*@{*/
45
46
/** @name Local static functions */
47
/*@{*/
48
49
/**
50
Get next packet in layer-resolution-component-precinct order.
51
@param pi packet iterator to modify
52
@return returns false if pi pointed to the last packet or else returns true
53
*/
54
static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
55
/**
56
Get next packet in resolution-layer-component-precinct order.
57
@param pi packet iterator to modify
58
@return returns false if pi pointed to the last packet or else returns true
59
*/
60
static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
61
/**
62
Get next packet in resolution-precinct-component-layer order.
63
@param pi packet iterator to modify
64
@return returns false if pi pointed to the last packet or else returns true
65
*/
66
static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
67
/**
68
Get next packet in precinct-component-resolution-layer order.
69
@param pi packet iterator to modify
70
@return returns false if pi pointed to the last packet or else returns true
71
*/
72
static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
73
/**
74
Get next packet in component-precinct-resolution-layer order.
75
@param pi packet iterator to modify
76
@return returns false if pi pointed to the last packet or else returns true
77
*/
78
static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
79
80
/**
81
 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
82
 *
83
 * @param   p_cp        the coding parameters to modify
84
 * @param   p_tileno    the tile index being concerned.
85
 * @param   p_tx0       X0 parameter for the tile
86
 * @param   p_tx1       X1 parameter for the tile
87
 * @param   p_ty0       Y0 parameter for the tile
88
 * @param   p_ty1       Y1 parameter for the tile
89
 * @param   p_max_prec  the maximum precision for all the bands of the tile
90
 * @param   p_max_res   the maximum number of resolutions for all the poc inside the tile.
91
 * @param   p_dx_min        the minimum dx of all the components of all the resolutions for the tile.
92
 * @param   p_dy_min        the minimum dy of all the components of all the resolutions for the tile.
93
 */
94
static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
95
        OPJ_UINT32 p_tileno,
96
        OPJ_UINT32 p_tx0,
97
        OPJ_UINT32 p_tx1,
98
        OPJ_UINT32 p_ty0,
99
        OPJ_UINT32 p_ty1,
100
        OPJ_UINT32 p_max_prec,
101
        OPJ_UINT32 p_max_res,
102
        OPJ_UINT32 p_dx_min,
103
        OPJ_UINT32 p_dy_min);
104
105
/**
106
 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
107
 *
108
 * @param   p_cp        the coding parameters to modify
109
 * @param   p_num_comps     the number of components
110
 * @param   p_tileno    the tile index being concerned.
111
 * @param   p_tx0       X0 parameter for the tile
112
 * @param   p_tx1       X1 parameter for the tile
113
 * @param   p_ty0       Y0 parameter for the tile
114
 * @param   p_ty1       Y1 parameter for the tile
115
 * @param   p_max_prec  the maximum precision for all the bands of the tile
116
 * @param   p_max_res   the maximum number of resolutions for all the poc inside the tile.
117
 * @param   p_dx_min        the minimum dx of all the components of all the resolutions for the tile.
118
 * @param   p_dy_min        the minimum dy of all the components of all the resolutions for the tile.
119
 */
120
static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
121
        OPJ_UINT32 p_num_comps,
122
        OPJ_UINT32 p_tileno,
123
        OPJ_UINT32 p_tx0,
124
        OPJ_UINT32 p_tx1,
125
        OPJ_UINT32 p_ty0,
126
        OPJ_UINT32 p_ty1,
127
        OPJ_UINT32 p_max_prec,
128
        OPJ_UINT32 p_max_res,
129
        OPJ_UINT32 p_dx_min,
130
        OPJ_UINT32 p_dy_min);
131
/**
132
 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
133
 *
134
 * @param   p_image         the image being encoded.
135
 * @param   p_cp            the coding parameters.
136
 * @param   tileno          the tile index of the tile being encoded.
137
 * @param   p_tx0           pointer that will hold the X0 parameter for the tile
138
 * @param   p_tx1           pointer that will hold the X1 parameter for the tile
139
 * @param   p_ty0           pointer that will hold the Y0 parameter for the tile
140
 * @param   p_ty1           pointer that will hold the Y1 parameter for the tile
141
 * @param   p_max_prec      pointer that will hold the maximum precision for all the bands of the tile
142
 * @param   p_max_res       pointer that will hold the maximum number of resolutions for all the poc inside the tile.
143
 * @param   p_dx_min            pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
144
 * @param   p_dy_min            pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
145
 */
146
static void opj_get_encoding_parameters(const opj_image_t *p_image,
147
                                        const opj_cp_t *p_cp,
148
                                        OPJ_UINT32  tileno,
149
                                        OPJ_UINT32 * p_tx0,
150
                                        OPJ_UINT32 * p_tx1,
151
                                        OPJ_UINT32 * p_ty0,
152
                                        OPJ_UINT32 * p_ty1,
153
                                        OPJ_UINT32 * p_dx_min,
154
                                        OPJ_UINT32 * p_dy_min,
155
                                        OPJ_UINT32 * p_max_prec,
156
                                        OPJ_UINT32 * p_max_res);
157
158
/**
159
 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
160
 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
161
 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
162
 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
163
 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
164
 *
165
 * @param   p_image         the image being encoded.
166
 * @param   p_cp            the coding parameters.
167
 * @param   tileno          the tile index of the tile being encoded.
168
 * @param   p_tx0           pointer that will hold the X0 parameter for the tile
169
 * @param   p_tx1           pointer that will hold the X1 parameter for the tile
170
 * @param   p_ty0           pointer that will hold the Y0 parameter for the tile
171
 * @param   p_ty1           pointer that will hold the Y1 parameter for the tile
172
 * @param   p_max_prec      pointer that will hold the maximum precision for all the bands of the tile
173
 * @param   p_max_res       pointer that will hold the maximum number of resolutions for all the poc inside the tile.
174
 * @param   p_dx_min        pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
175
 * @param   p_dy_min        pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
176
 * @param   p_resolutions   pointer to an area corresponding to the one described above.
177
 */
178
static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
179
        const opj_cp_t *p_cp,
180
        OPJ_UINT32 tileno,
181
        OPJ_UINT32 * p_tx0,
182
        OPJ_UINT32 * p_tx1,
183
        OPJ_UINT32 * p_ty0,
184
        OPJ_UINT32 * p_ty1,
185
        OPJ_UINT32 * p_dx_min,
186
        OPJ_UINT32 * p_dy_min,
187
        OPJ_UINT32 * p_max_prec,
188
        OPJ_UINT32 * p_max_res,
189
        OPJ_UINT32 ** p_resolutions);
190
/**
191
 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
192
 * No other data is set. The include section of the packet  iterator is not allocated.
193
 *
194
 * @param   p_image     the image used to initialize the packet iterator (in fact only the number of components is relevant.
195
 * @param   p_cp        the coding parameters.
196
 * @param   tileno  the index of the tile from which creating the packet iterator.
197
 * @param   manager Event manager
198
 */
199
static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
200
        const opj_cp_t *p_cp,
201
        OPJ_UINT32 tileno,
202
        opj_event_mgr_t* manager);
203
/**
204
 * FIXME DOC
205
 */
206
static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
207
        opj_tcp_t * p_tcp,
208
        OPJ_UINT32 p_max_precision,
209
        OPJ_UINT32 p_max_res);
210
/**
211
 * FIXME DOC
212
 */
213
static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
214
                                     opj_tcp_t * p_tcp,
215
                                     OPJ_UINT32 p_max_precision,
216
                                     OPJ_UINT32 p_max_res);
217
218
/**
219
 * FIXME DOC
220
 */
221
static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
222
                                        opj_cp_t *cp,
223
                                        OPJ_UINT32 tileno,
224
                                        OPJ_UINT32 pino,
225
                                        const OPJ_CHAR *prog);
226
227
/*@}*/
228
229
/*@}*/
230
231
/*
232
==========================================================
233
   local functions
234
==========================================================
235
*/
236
237
static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
238
67.7M
{
239
67.7M
    opj_pi_comp_t *comp = NULL;
240
67.7M
    opj_pi_resolution_t *res = NULL;
241
67.7M
    OPJ_UINT32 index = 0;
242
243
67.7M
    if (pi->poc.compno0 >= pi->numcomps ||
244
67.7M
            pi->poc.compno1 >= pi->numcomps + 1) {
245
2.93k
        opj_event_msg(pi->manager, EVT_ERROR,
246
2.93k
                      "opj_pi_next_lrcp(): invalid compno0/compno1\n");
247
2.93k
        return OPJ_FALSE;
248
2.93k
    }
249
250
67.7M
    if (!pi->first) {
251
67.7M
        comp = &pi->comps[pi->compno];
252
67.7M
        res = &comp->resolutions[pi->resno];
253
67.7M
        goto LABEL_SKIP;
254
67.7M
    } else {
255
21.1k
        pi->first = 0;
256
21.1k
    }
257
258
82.6k
    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
259
267k
        for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
260
205k
                pi->resno++) {
261
645k
            for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
262
440k
                comp = &pi->comps[pi->compno];
263
440k
                if (pi->resno >= comp->numresolutions) {
264
56.8k
                    continue;
265
56.8k
                }
266
383k
                res = &comp->resolutions[pi->resno];
267
383k
                if (!pi->tp_on) {
268
383k
                    pi->poc.precno1 = res->pw * res->ph;
269
383k
                }
270
68.1M
                for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
271
67.7M
                    index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
272
67.7M
                            pi->step_c + pi->precno * pi->step_p;
273
                    /* Avoids index out of bounds access with */
274
                    /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
275
                    /* https://github.com/uclouvain/openjpeg/issues/938 */
276
                    /* Not sure if this is the most clever fix. Perhaps */
277
                    /* include should be resized when a POC arises, or */
278
                    /* the POC should be rejected */
279
67.7M
                    if (index >= pi->include_size) {
280
0
                        opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
281
0
                        return OPJ_FALSE;
282
0
                    }
283
67.7M
                    if (!pi->include[index]) {
284
67.7M
                        pi->include[index] = 1;
285
67.7M
                        return OPJ_TRUE;
286
67.7M
                    }
287
67.7M
LABEL_SKIP:
288
67.7M
                    ;
289
67.7M
                }
290
383k
            }
291
205k
        }
292
61.7k
    }
293
294
20.9k
    return OPJ_FALSE;
295
21.1k
}
296
297
static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
298
17.5M
{
299
17.5M
    opj_pi_comp_t *comp = NULL;
300
17.5M
    opj_pi_resolution_t *res = NULL;
301
17.5M
    OPJ_UINT32 index = 0;
302
303
17.5M
    if (pi->poc.compno0 >= pi->numcomps ||
304
17.5M
            pi->poc.compno1 >= pi->numcomps + 1) {
305
2.11k
        opj_event_msg(pi->manager, EVT_ERROR,
306
2.11k
                      "opj_pi_next_rlcp(): invalid compno0/compno1\n");
307
2.11k
        return OPJ_FALSE;
308
2.11k
    }
309
310
17.5M
    if (!pi->first) {
311
17.5M
        comp = &pi->comps[pi->compno];
312
17.5M
        res = &comp->resolutions[pi->resno];
313
17.5M
        goto LABEL_SKIP;
314
17.5M
    } else {
315
11.1k
        pi->first = 0;
316
11.1k
    }
317
318
73.5k
    for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
319
319k
        for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
320
2.21M
            for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
321
1.96M
                comp = &pi->comps[pi->compno];
322
1.96M
                if (pi->resno >= comp->numresolutions) {
323
56.8k
                    continue;
324
56.8k
                }
325
1.90M
                res = &comp->resolutions[pi->resno];
326
1.90M
                if (!pi->tp_on) {
327
1.90M
                    pi->poc.precno1 = res->pw * res->ph;
328
1.90M
                }
329
19.4M
                for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
330
17.5M
                    index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
331
17.5M
                            pi->step_c + pi->precno * pi->step_p;
332
17.5M
                    if (index >= pi->include_size) {
333
0
                        opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
334
0
                        return OPJ_FALSE;
335
0
                    }
336
17.5M
                    if (!pi->include[index]) {
337
17.5M
                        pi->include[index] = 1;
338
17.5M
                        return OPJ_TRUE;
339
17.5M
                    }
340
17.5M
LABEL_SKIP:
341
17.5M
                    ;
342
17.5M
                }
343
1.90M
            }
344
256k
        }
345
62.5k
    }
346
347
11.0k
    return OPJ_FALSE;
348
11.1k
}
349
350
static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
351
9.38M
{
352
9.38M
    opj_pi_comp_t *comp = NULL;
353
9.38M
    opj_pi_resolution_t *res = NULL;
354
9.38M
    OPJ_UINT32 index = 0;
355
356
9.38M
    if (pi->poc.compno0 >= pi->numcomps ||
357
9.37M
            pi->poc.compno1 >= pi->numcomps + 1) {
358
2.80k
        opj_event_msg(pi->manager, EVT_ERROR,
359
2.80k
                      "opj_pi_next_rpcl(): invalid compno0/compno1\n");
360
2.80k
        return OPJ_FALSE;
361
2.80k
    }
362
363
9.37M
    if (!pi->first) {
364
9.37M
        goto LABEL_SKIP;
365
9.37M
    } else {
366
2.71k
        OPJ_UINT32 compno, resno;
367
2.71k
        pi->first = 0;
368
2.71k
        pi->dx = 0;
369
2.71k
        pi->dy = 0;
370
43.6k
        for (compno = 0; compno < pi->numcomps; compno++) {
371
40.9k
            comp = &pi->comps[compno];
372
361k
            for (resno = 0; resno < comp->numresolutions; resno++) {
373
320k
                OPJ_UINT32 dx, dy;
374
320k
                res = &comp->resolutions[resno];
375
320k
                if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
376
294k
                        comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
377
280k
                    dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
378
280k
                    pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
379
280k
                }
380
320k
                if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
381
294k
                        comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
382
278k
                    dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
383
278k
                    pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
384
278k
                }
385
320k
            }
386
40.9k
        }
387
2.71k
        if (pi->dx == 0 || pi->dy == 0) {
388
0
            return OPJ_FALSE;
389
0
        }
390
2.71k
    }
391
2.71k
    if (!pi->tp_on) {
392
2.71k
        pi->poc.ty0 = pi->ty0;
393
2.71k
        pi->poc.tx0 = pi->tx0;
394
2.71k
        pi->poc.ty1 = pi->ty1;
395
2.71k
        pi->poc.tx1 = pi->tx1;
396
2.71k
    }
397
90.4k
    for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
398
324k
        for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
399
236k
                pi->y += (pi->dy - (pi->y % pi->dy))) {
400
6.12M
            for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
401
5.88M
                    pi->x += (pi->dx - (pi->x % pi->dx))) {
402
22.7M
                for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
403
16.8M
                    OPJ_UINT32 levelno;
404
16.8M
                    OPJ_UINT32 trx0, try0;
405
16.8M
                    OPJ_UINT32  trx1, try1;
406
16.8M
                    OPJ_UINT32  rpx, rpy;
407
16.8M
                    OPJ_UINT32  prci, prcj;
408
16.8M
                    comp = &pi->comps[pi->compno];
409
16.8M
                    if (pi->resno >= comp->numresolutions) {
410
8.18M
                        continue;
411
8.18M
                    }
412
8.66M
                    res = &comp->resolutions[pi->resno];
413
8.66M
                    levelno = comp->numresolutions - 1 - pi->resno;
414
415
8.66M
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
416
8.66M
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
417
0
                        continue;
418
0
                    }
419
420
8.66M
                    trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
421
8.66M
                                                         ((OPJ_UINT64)comp->dx << levelno));
422
8.66M
                    try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
423
8.66M
                                                         ((OPJ_UINT64)comp->dy << levelno));
424
8.66M
                    trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
425
8.66M
                                                         ((OPJ_UINT64)comp->dx << levelno));
426
8.66M
                    try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
427
8.66M
                                                         ((OPJ_UINT64)comp->dy << levelno));
428
8.66M
                    rpx = res->pdx + levelno;
429
8.66M
                    rpy = res->pdy + levelno;
430
431
8.66M
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
432
8.66M
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
433
0
                        continue;
434
0
                    }
435
436
                    /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
437
8.66M
                    if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
438
5.80M
                            ((pi->y == pi->ty0) &&
439
5.64M
                             (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
440
5.64M
                        continue;
441
5.64M
                    }
442
3.02M
                    if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
443
782k
                            ((pi->x == pi->tx0) &&
444
658k
                             (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
445
658k
                        continue;
446
658k
                    }
447
448
2.36M
                    if ((res->pw == 0) || (res->ph == 0)) {
449
134k
                        continue;
450
134k
                    }
451
452
2.22M
                    if ((trx0 == trx1) || (try0 == try1)) {
453
0
                        continue;
454
0
                    }
455
456
2.22M
                    prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
457
2.22M
                                                 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
458
2.22M
                           - opj_uint_floordivpow2(trx0, res->pdx);
459
2.22M
                    prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
460
2.22M
                                                 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
461
2.22M
                           - opj_uint_floordivpow2(try0, res->pdy);
462
2.22M
                    pi->precno = prci + prcj * res->pw;
463
11.6M
                    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
464
9.37M
                        index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
465
9.37M
                                pi->step_c + pi->precno * pi->step_p;
466
9.37M
                        if (index >= pi->include_size) {
467
0
                            opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
468
0
                            return OPJ_FALSE;
469
0
                        }
470
9.37M
                        if (!pi->include[index]) {
471
9.37M
                            pi->include[index] = 1;
472
9.37M
                            return OPJ_TRUE;
473
9.37M
                        }
474
9.37M
LABEL_SKIP:
475
9.37M
                        ;
476
9.37M
                    }
477
2.22M
                }
478
5.88M
            }
479
236k
        }
480
87.7k
    }
481
482
2.63k
    return OPJ_FALSE;
483
2.71k
}
484
485
static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
486
13.3M
{
487
13.3M
    opj_pi_comp_t *comp = NULL;
488
13.3M
    opj_pi_resolution_t *res = NULL;
489
13.3M
    OPJ_UINT32 index = 0;
490
491
13.3M
    if (pi->poc.compno0 >= pi->numcomps ||
492
13.3M
            pi->poc.compno1 >= pi->numcomps + 1) {
493
2.85k
        opj_event_msg(pi->manager, EVT_ERROR,
494
2.85k
                      "opj_pi_next_pcrl(): invalid compno0/compno1\n");
495
2.85k
        return OPJ_FALSE;
496
2.85k
    }
497
498
13.3M
    if (!pi->first) {
499
13.3M
        comp = &pi->comps[pi->compno];
500
13.3M
        goto LABEL_SKIP;
501
13.3M
    } else {
502
5.07k
        OPJ_UINT32 compno, resno;
503
5.07k
        pi->first = 0;
504
5.07k
        pi->dx = 0;
505
5.07k
        pi->dy = 0;
506
29.7k
        for (compno = 0; compno < pi->numcomps; compno++) {
507
24.6k
            comp = &pi->comps[compno];
508
407k
            for (resno = 0; resno < comp->numresolutions; resno++) {
509
383k
                OPJ_UINT32 dx, dy;
510
383k
                res = &comp->resolutions[resno];
511
383k
                if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
512
286k
                        comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
513
259k
                    dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
514
259k
                    pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
515
259k
                }
516
383k
                if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
517
286k
                        comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
518
259k
                    dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
519
259k
                    pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
520
259k
                }
521
383k
            }
522
24.6k
        }
523
5.07k
        if (pi->dx == 0 || pi->dy == 0) {
524
0
            return OPJ_FALSE;
525
0
        }
526
5.07k
    }
527
5.07k
    if (!pi->tp_on) {
528
5.07k
        pi->poc.ty0 = pi->ty0;
529
5.07k
        pi->poc.tx0 = pi->tx0;
530
5.07k
        pi->poc.ty1 = pi->ty1;
531
5.07k
        pi->poc.tx1 = pi->tx1;
532
5.07k
    }
533
33.4k
    for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
534
28.6k
            pi->y += (pi->dy - (pi->y % pi->dy))) {
535
3.28M
        for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
536
3.25M
                pi->x += (pi->dx - (pi->x % pi->dx))) {
537
12.1M
            for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
538
8.91M
                comp = &pi->comps[pi->compno];
539
8.91M
                for (pi->resno = pi->poc.resno0;
540
27.2M
                        pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
541
18.3M
                    OPJ_UINT32 levelno;
542
18.3M
                    OPJ_UINT32 trx0, try0;
543
18.3M
                    OPJ_UINT32 trx1, try1;
544
18.3M
                    OPJ_UINT32 rpx, rpy;
545
18.3M
                    OPJ_UINT32 prci, prcj;
546
18.3M
                    res = &comp->resolutions[pi->resno];
547
18.3M
                    levelno = comp->numresolutions - 1 - pi->resno;
548
549
18.3M
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
550
18.3M
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
551
0
                        continue;
552
0
                    }
553
554
18.3M
                    trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
555
18.3M
                                                         ((OPJ_UINT64)comp->dx << levelno));
556
18.3M
                    try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
557
18.3M
                                                         ((OPJ_UINT64)comp->dy << levelno));
558
18.3M
                    trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
559
18.3M
                                                         ((OPJ_UINT64)comp->dx << levelno));
560
18.3M
                    try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
561
18.3M
                                                         ((OPJ_UINT64)comp->dy << levelno));
562
18.3M
                    rpx = res->pdx + levelno;
563
18.3M
                    rpy = res->pdy + levelno;
564
565
18.3M
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
566
18.3M
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
567
0
                        continue;
568
0
                    }
569
570
                    /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
571
18.3M
                    if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
572
13.9M
                            ((pi->y == pi->ty0) &&
573
13.5M
                             (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
574
13.5M
                        continue;
575
13.5M
                    }
576
4.74M
                    if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
577
1.84M
                            ((pi->x == pi->tx0) &&
578
1.78M
                             (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
579
1.78M
                        continue;
580
1.78M
                    }
581
582
2.96M
                    if ((res->pw == 0) || (res->ph == 0)) {
583
213k
                        continue;
584
213k
                    }
585
586
2.74M
                    if ((trx0 == trx1) || (try0 == try1)) {
587
0
                        continue;
588
0
                    }
589
590
2.74M
                    prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
591
2.74M
                                                 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
592
2.74M
                           - opj_uint_floordivpow2(trx0, res->pdx);
593
2.74M
                    prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
594
2.74M
                                                 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
595
2.74M
                           - opj_uint_floordivpow2(try0, res->pdy);
596
2.74M
                    pi->precno = prci + prcj * res->pw;
597
16.1M
                    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
598
13.3M
                        index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
599
13.3M
                                pi->step_c + pi->precno * pi->step_p;
600
13.3M
                        if (index >= pi->include_size) {
601
0
                            opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
602
0
                            return OPJ_FALSE;
603
0
                        }
604
13.3M
                        if (!pi->include[index]) {
605
13.3M
                            pi->include[index] = 1;
606
13.3M
                            return OPJ_TRUE;
607
13.3M
                        }
608
13.3M
LABEL_SKIP:
609
13.3M
                        ;
610
13.3M
                    }
611
2.74M
                }
612
8.91M
            }
613
3.25M
        }
614
28.6k
    }
615
616
4.84k
    return OPJ_FALSE;
617
5.07k
}
618
619
static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
620
22.5M
{
621
22.5M
    opj_pi_comp_t *comp = NULL;
622
22.5M
    opj_pi_resolution_t *res = NULL;
623
22.5M
    OPJ_UINT32 index = 0;
624
625
22.5M
    if (pi->poc.compno0 >= pi->numcomps ||
626
22.5M
            pi->poc.compno1 >= pi->numcomps + 1) {
627
1.03k
        opj_event_msg(pi->manager, EVT_ERROR,
628
1.03k
                      "opj_pi_next_cprl(): invalid compno0/compno1\n");
629
1.03k
        return OPJ_FALSE;
630
1.03k
    }
631
632
22.5M
    if (!pi->first) {
633
22.5M
        comp = &pi->comps[pi->compno];
634
22.5M
        goto LABEL_SKIP;
635
22.5M
    } else {
636
29.1k
        pi->first = 0;
637
29.1k
    }
638
639
66.6k
    for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
640
37.7k
        OPJ_UINT32 resno;
641
37.7k
        comp = &pi->comps[pi->compno];
642
37.7k
        pi->dx = 0;
643
37.7k
        pi->dy = 0;
644
364k
        for (resno = 0; resno < comp->numresolutions; resno++) {
645
326k
            OPJ_UINT32 dx, dy;
646
326k
            res = &comp->resolutions[resno];
647
326k
            if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
648
256k
                    comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
649
245k
                dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
650
245k
                pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
651
245k
            }
652
326k
            if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
653
256k
                    comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
654
243k
                dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
655
243k
                pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
656
243k
            }
657
326k
        }
658
37.7k
        if (pi->dx == 0 || pi->dy == 0) {
659
0
            return OPJ_FALSE;
660
0
        }
661
37.7k
        if (!pi->tp_on) {
662
16.3k
            pi->poc.ty0 = pi->ty0;
663
16.3k
            pi->poc.tx0 = pi->tx0;
664
16.3k
            pi->poc.ty1 = pi->ty1;
665
16.3k
            pi->poc.tx1 = pi->tx1;
666
16.3k
        }
667
237k
        for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
668
200k
                pi->y += (pi->dy - (pi->y % pi->dy))) {
669
4.70M
            for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
670
4.50M
                    pi->x += (pi->dx - (pi->x % pi->dx))) {
671
4.50M
                for (pi->resno = pi->poc.resno0;
672
13.7M
                        pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
673
9.22M
                    OPJ_UINT32 levelno;
674
9.22M
                    OPJ_UINT32 trx0, try0;
675
9.22M
                    OPJ_UINT32 trx1, try1;
676
9.22M
                    OPJ_UINT32 rpx, rpy;
677
9.22M
                    OPJ_UINT32 prci, prcj;
678
9.22M
                    res = &comp->resolutions[pi->resno];
679
9.22M
                    levelno = comp->numresolutions - 1 - pi->resno;
680
681
9.22M
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
682
9.22M
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
683
0
                        continue;
684
0
                    }
685
686
9.22M
                    trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
687
9.22M
                                                         ((OPJ_UINT64)comp->dx << levelno));
688
9.22M
                    try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
689
9.22M
                                                         ((OPJ_UINT64)comp->dy << levelno));
690
9.22M
                    trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
691
9.22M
                                                         ((OPJ_UINT64)comp->dx << levelno));
692
9.22M
                    try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
693
9.22M
                                                         ((OPJ_UINT64)comp->dy << levelno));
694
9.22M
                    rpx = res->pdx + levelno;
695
9.22M
                    rpy = res->pdy + levelno;
696
697
9.22M
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
698
9.22M
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
699
0
                        continue;
700
0
                    }
701
702
                    /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
703
9.22M
                    if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
704
3.78M
                            ((pi->y == pi->ty0) &&
705
3.58M
                             (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
706
3.58M
                        continue;
707
3.58M
                    }
708
5.63M
                    if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
709
942k
                            ((pi->x == pi->tx0) &&
710
806k
                             (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
711
806k
                        continue;
712
806k
                    }
713
714
4.83M
                    if ((res->pw == 0) || (res->ph == 0)) {
715
126k
                        continue;
716
126k
                    }
717
718
4.70M
                    if ((trx0 == trx1) || (try0 == try1)) {
719
0
                        continue;
720
0
                    }
721
722
4.70M
                    prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
723
4.70M
                                                 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
724
4.70M
                           - opj_uint_floordivpow2(trx0, res->pdx);
725
4.70M
                    prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
726
4.70M
                                                 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
727
4.70M
                           - opj_uint_floordivpow2(try0, res->pdy);
728
4.70M
                    pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
729
27.2M
                    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
730
22.5M
                        index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
731
22.5M
                                pi->step_c + pi->precno * pi->step_p;
732
22.5M
                        if (index >= pi->include_size) {
733
0
                            opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
734
0
                            return OPJ_FALSE;
735
0
                        }
736
22.5M
                        if (!pi->include[index]) {
737
22.5M
                            pi->include[index] = 1;
738
22.5M
                            return OPJ_TRUE;
739
22.5M
                        }
740
22.5M
LABEL_SKIP:
741
22.5M
                        ;
742
22.5M
                    }
743
4.70M
                }
744
4.50M
            }
745
200k
        }
746
37.7k
    }
747
748
28.9k
    return OPJ_FALSE;
749
29.1k
}
750
751
static void opj_get_encoding_parameters(const opj_image_t *p_image,
752
                                        const opj_cp_t *p_cp,
753
                                        OPJ_UINT32 p_tileno,
754
                                        OPJ_UINT32 * p_tx0,
755
                                        OPJ_UINT32  * p_tx1,
756
                                        OPJ_UINT32  * p_ty0,
757
                                        OPJ_UINT32  * p_ty1,
758
                                        OPJ_UINT32 * p_dx_min,
759
                                        OPJ_UINT32 * p_dy_min,
760
                                        OPJ_UINT32 * p_max_prec,
761
                                        OPJ_UINT32 * p_max_res)
762
9.02k
{
763
    /* loop */
764
9.02k
    OPJ_UINT32  compno, resno;
765
    /* pointers */
766
9.02k
    const opj_tcp_t *l_tcp = 00;
767
9.02k
    const opj_tccp_t * l_tccp = 00;
768
9.02k
    const opj_image_comp_t * l_img_comp = 00;
769
770
    /* position in x and y of tile */
771
9.02k
    OPJ_UINT32 p, q;
772
773
    /* non-corrected (in regard to image offset) tile offset */
774
9.02k
    OPJ_UINT32 l_tx0, l_ty0;
775
776
    /* preconditions */
777
9.02k
    assert(p_cp != 00);
778
9.02k
    assert(p_image != 00);
779
9.02k
    assert(p_tileno < p_cp->tw * p_cp->th);
780
781
    /* initializations */
782
9.02k
    l_tcp = &p_cp->tcps [p_tileno];
783
9.02k
    l_img_comp = p_image->comps;
784
9.02k
    l_tccp = l_tcp->tccps;
785
786
    /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
787
9.02k
    p = p_tileno % p_cp->tw;
788
9.02k
    q = p_tileno / p_cp->tw;
789
790
    /* find extent of tile */
791
9.02k
    l_tx0 = p_cp->tx0 + p *
792
9.02k
            p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
793
9.02k
    *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
794
9.02k
    *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
795
9.02k
    l_ty0 = p_cp->ty0 + q *
796
9.02k
            p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
797
9.02k
    *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
798
9.02k
    *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
799
800
    /* max precision is 0 (can only grow) */
801
9.02k
    *p_max_prec = 0;
802
9.02k
    *p_max_res = 0;
803
804
    /* take the largest value for dx_min and dy_min */
805
9.02k
    *p_dx_min = 0x7fffffff;
806
9.02k
    *p_dy_min  = 0x7fffffff;
807
808
19.3k
    for (compno = 0; compno < p_image->numcomps; ++compno) {
809
        /* arithmetic variables to calculate */
810
10.3k
        OPJ_UINT32 l_level_no;
811
10.3k
        OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
812
10.3k
        OPJ_UINT32 l_px0, l_py0, l_px1, py1;
813
10.3k
        OPJ_UINT32 l_pdx, l_pdy;
814
10.3k
        OPJ_UINT32 l_pw, l_ph;
815
10.3k
        OPJ_UINT32 l_product;
816
10.3k
        OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
817
818
10.3k
        l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
819
10.3k
        l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
820
10.3k
        l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
821
10.3k
        l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
822
823
10.3k
        if (l_tccp->numresolutions > *p_max_res) {
824
9.02k
            *p_max_res = l_tccp->numresolutions;
825
9.02k
        }
826
827
        /* use custom size for precincts */
828
58.7k
        for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
829
48.4k
            OPJ_UINT64 l_dx, l_dy;
830
831
            /* precinct width and height */
832
48.4k
            l_pdx = l_tccp->prcw[resno];
833
48.4k
            l_pdy = l_tccp->prch[resno];
834
835
48.4k
            l_dx = l_img_comp->dx * ((OPJ_UINT64)1u << (l_pdx + l_tccp->numresolutions - 1 -
836
48.4k
                                     resno));
837
48.4k
            l_dy = l_img_comp->dy * ((OPJ_UINT64)1u << (l_pdy + l_tccp->numresolutions - 1 -
838
48.4k
                                     resno));
839
840
            /* take the minimum size for dx for each comp and resolution */
841
48.4k
            if (l_dx <= UINT_MAX) {
842
48.4k
                *p_dx_min = opj_uint_min(*p_dx_min, (OPJ_UINT32)l_dx);
843
48.4k
            }
844
48.4k
            if (l_dy <= UINT_MAX) {
845
48.4k
                *p_dy_min = opj_uint_min(*p_dy_min, (OPJ_UINT32)l_dy);
846
48.4k
            }
847
848
            /* various calculations of extents */
849
48.4k
            l_level_no = l_tccp->numresolutions - 1 - resno;
850
851
48.4k
            l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
852
48.4k
            l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
853
48.4k
            l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
854
48.4k
            l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
855
856
48.4k
            l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
857
48.4k
            l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
858
48.4k
            l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
859
860
48.4k
            py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
861
862
48.4k
            l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
863
48.4k
            l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
864
865
48.4k
            l_product = l_pw * l_ph;
866
867
            /* update precision */
868
48.4k
            if (l_product > *p_max_prec) {
869
11.4k
                *p_max_prec = l_product;
870
11.4k
            }
871
48.4k
        }
872
10.3k
        ++l_img_comp;
873
10.3k
        ++l_tccp;
874
10.3k
    }
875
9.02k
}
876
877
878
static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
879
        const opj_cp_t *p_cp,
880
        OPJ_UINT32 tileno,
881
        OPJ_UINT32 * p_tx0,
882
        OPJ_UINT32 * p_tx1,
883
        OPJ_UINT32 * p_ty0,
884
        OPJ_UINT32 * p_ty1,
885
        OPJ_UINT32 * p_dx_min,
886
        OPJ_UINT32 * p_dy_min,
887
        OPJ_UINT32 * p_max_prec,
888
        OPJ_UINT32 * p_max_res,
889
        OPJ_UINT32 ** p_resolutions)
890
64.3k
{
891
    /* loop*/
892
64.3k
    OPJ_UINT32 compno, resno;
893
894
    /* pointers*/
895
64.3k
    const opj_tcp_t *tcp = 00;
896
64.3k
    const opj_tccp_t * l_tccp = 00;
897
64.3k
    const opj_image_comp_t * l_img_comp = 00;
898
899
    /* to store l_dx, l_dy, w and h for each resolution and component.*/
900
64.3k
    OPJ_UINT32 * lResolutionPtr;
901
902
    /* position in x and y of tile*/
903
64.3k
    OPJ_UINT32 p, q;
904
905
    /* non-corrected (in regard to image offset) tile offset */
906
64.3k
    OPJ_UINT32 l_tx0, l_ty0;
907
908
    /* preconditions in debug*/
909
64.3k
    assert(p_cp != 00);
910
64.3k
    assert(p_image != 00);
911
64.3k
    assert(tileno < p_cp->tw * p_cp->th);
912
913
    /* initializations*/
914
64.3k
    tcp = &p_cp->tcps [tileno];
915
64.3k
    l_tccp = tcp->tccps;
916
64.3k
    l_img_comp = p_image->comps;
917
918
    /* position in x and y of tile*/
919
64.3k
    p = tileno % p_cp->tw;
920
64.3k
    q = tileno / p_cp->tw;
921
922
    /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
923
64.3k
    l_tx0 = p_cp->tx0 + p *
924
64.3k
            p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
925
64.3k
    *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
926
64.3k
    *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
927
64.3k
    l_ty0 = p_cp->ty0 + q *
928
64.3k
            p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
929
64.3k
    *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
930
64.3k
    *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
931
932
    /* max precision and resolution is 0 (can only grow)*/
933
64.3k
    *p_max_prec = 0;
934
64.3k
    *p_max_res = 0;
935
936
    /* take the largest value for dx_min and dy_min*/
937
64.3k
    *p_dx_min = 0x7fffffff;
938
64.3k
    *p_dy_min = 0x7fffffff;
939
940
273k
    for (compno = 0; compno < p_image->numcomps; ++compno) {
941
        /* arithmetic variables to calculate*/
942
209k
        OPJ_UINT32 l_level_no;
943
209k
        OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
944
209k
        OPJ_UINT32 l_px0, l_py0, l_px1, py1;
945
209k
        OPJ_UINT32 l_product;
946
209k
        OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
947
209k
        OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
948
949
209k
        lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
950
951
209k
        l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
952
209k
        l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
953
209k
        l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
954
209k
        l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
955
956
209k
        if (l_tccp->numresolutions > *p_max_res) {
957
64.3k
            *p_max_res = l_tccp->numresolutions;
958
64.3k
        }
959
960
        /* use custom size for precincts*/
961
209k
        l_level_no = l_tccp->numresolutions;
962
2.10M
        for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
963
1.89M
            OPJ_UINT32 l_dx, l_dy;
964
965
1.89M
            --l_level_no;
966
967
            /* precinct width and height*/
968
1.89M
            l_pdx = l_tccp->prcw[resno];
969
1.89M
            l_pdy = l_tccp->prch[resno];
970
1.89M
            if (lResolutionPtr) {
971
1.89M
                *lResolutionPtr++ = l_pdx;
972
1.89M
                *lResolutionPtr++ = l_pdy;
973
1.89M
            }
974
1.89M
            if (l_pdx + l_level_no < 32 &&
975
1.45M
                    l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
976
1.36M
                l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
977
                /* take the minimum size for l_dx for each comp and resolution*/
978
1.36M
                *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
979
1.36M
            }
980
1.89M
            if (l_pdy + l_level_no < 32 &&
981
1.45M
                    l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
982
1.36M
                l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
983
1.36M
                *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
984
1.36M
            }
985
986
            /* various calculations of extents*/
987
1.89M
            l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
988
1.89M
            l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
989
1.89M
            l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
990
1.89M
            l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
991
1.89M
            l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
992
1.89M
            l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
993
1.89M
            l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
994
1.89M
            py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
995
1.89M
            l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
996
1.89M
            l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
997
1.89M
            if (lResolutionPtr) {
998
1.89M
                *lResolutionPtr++ = l_pw;
999
1.89M
                *lResolutionPtr++ = l_ph;
1000
1.89M
            }
1001
1.89M
            l_product = l_pw * l_ph;
1002
1003
            /* update precision*/
1004
1.89M
            if (l_product > *p_max_prec) {
1005
129k
                *p_max_prec = l_product;
1006
129k
            }
1007
1008
1.89M
        }
1009
209k
        ++l_tccp;
1010
209k
        ++l_img_comp;
1011
209k
    }
1012
64.3k
}
1013
1014
static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
1015
        const opj_cp_t *cp,
1016
        OPJ_UINT32 tileno,
1017
        opj_event_mgr_t* manager)
1018
64.3k
{
1019
    /* loop*/
1020
64.3k
    OPJ_UINT32 pino, compno;
1021
    /* number of poc in the p_pi*/
1022
64.3k
    OPJ_UINT32 l_poc_bound;
1023
1024
    /* pointers to tile coding parameters and components.*/
1025
64.3k
    opj_pi_iterator_t *l_pi = 00;
1026
64.3k
    opj_tcp_t *tcp = 00;
1027
64.3k
    const opj_tccp_t *tccp = 00;
1028
1029
    /* current packet iterator being allocated*/
1030
64.3k
    opj_pi_iterator_t *l_current_pi = 00;
1031
1032
    /* preconditions in debug*/
1033
64.3k
    assert(cp != 00);
1034
64.3k
    assert(image != 00);
1035
64.3k
    assert(tileno < cp->tw * cp->th);
1036
1037
    /* initializations*/
1038
64.3k
    tcp = &cp->tcps[tileno];
1039
64.3k
    l_poc_bound = tcp->numpocs + 1;
1040
1041
    /* memory allocations*/
1042
64.3k
    l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1043
64.3k
                                           sizeof(opj_pi_iterator_t));
1044
64.3k
    if (!l_pi) {
1045
0
        return NULL;
1046
0
    }
1047
1048
64.3k
    l_current_pi = l_pi;
1049
153k
    for (pino = 0; pino < l_poc_bound ; ++pino) {
1050
1051
89.3k
        l_current_pi->manager = manager;
1052
1053
89.3k
        l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1054
89.3k
                              sizeof(opj_pi_comp_t));
1055
89.3k
        if (! l_current_pi->comps) {
1056
0
            opj_pi_destroy(l_pi, l_poc_bound);
1057
0
            return NULL;
1058
0
        }
1059
1060
89.3k
        l_current_pi->numcomps = image->numcomps;
1061
1062
396k
        for (compno = 0; compno < image->numcomps; ++compno) {
1063
306k
            opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1064
1065
306k
            tccp = &tcp->tccps[compno];
1066
1067
306k
            comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1068
306k
                                sizeof(opj_pi_resolution_t));
1069
306k
            if (!comp->resolutions) {
1070
0
                opj_pi_destroy(l_pi, l_poc_bound);
1071
0
                return 00;
1072
0
            }
1073
1074
306k
            comp->numresolutions = tccp->numresolutions;
1075
306k
        }
1076
89.3k
        ++l_current_pi;
1077
89.3k
    }
1078
64.3k
    return l_pi;
1079
64.3k
}
1080
1081
static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1082
        OPJ_UINT32 p_tileno,
1083
        OPJ_UINT32 p_tx0,
1084
        OPJ_UINT32 p_tx1,
1085
        OPJ_UINT32 p_ty0,
1086
        OPJ_UINT32 p_ty1,
1087
        OPJ_UINT32 p_max_prec,
1088
        OPJ_UINT32 p_max_res,
1089
        OPJ_UINT32 p_dx_min,
1090
        OPJ_UINT32 p_dy_min)
1091
0
{
1092
    /* loop*/
1093
0
    OPJ_UINT32 pino;
1094
    /* tile coding parameter*/
1095
0
    opj_tcp_t *l_tcp = 00;
1096
    /* current poc being updated*/
1097
0
    opj_poc_t * l_current_poc = 00;
1098
1099
    /* number of pocs*/
1100
0
    OPJ_UINT32 l_poc_bound;
1101
1102
0
    OPJ_ARG_NOT_USED(p_max_res);
1103
1104
    /* preconditions in debug*/
1105
0
    assert(p_cp != 00);
1106
0
    assert(p_tileno < p_cp->tw * p_cp->th);
1107
1108
    /* initializations*/
1109
0
    l_tcp = &p_cp->tcps [p_tileno];
1110
    /* number of iterations in the loop */
1111
0
    l_poc_bound = l_tcp->numpocs + 1;
1112
1113
    /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1114
       store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1115
0
    l_current_poc = l_tcp->pocs;
1116
1117
0
    l_current_poc->compS = l_current_poc->compno0;
1118
0
    l_current_poc->compE = l_current_poc->compno1;
1119
0
    l_current_poc->resS = l_current_poc->resno0;
1120
0
    l_current_poc->resE = l_current_poc->resno1;
1121
0
    l_current_poc->layE = l_current_poc->layno1;
1122
1123
    /* special treatment for the first element*/
1124
0
    l_current_poc->layS = 0;
1125
0
    l_current_poc->prg  = l_current_poc->prg1;
1126
0
    l_current_poc->prcS = 0;
1127
1128
0
    l_current_poc->prcE = p_max_prec;
1129
0
    l_current_poc->txS = (OPJ_UINT32)p_tx0;
1130
0
    l_current_poc->txE = (OPJ_UINT32)p_tx1;
1131
0
    l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1132
0
    l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1133
0
    l_current_poc->dx = p_dx_min;
1134
0
    l_current_poc->dy = p_dy_min;
1135
1136
0
    ++ l_current_poc;
1137
0
    for (pino = 1; pino < l_poc_bound ; ++pino) {
1138
0
        l_current_poc->compS = l_current_poc->compno0;
1139
0
        l_current_poc->compE = l_current_poc->compno1;
1140
0
        l_current_poc->resS = l_current_poc->resno0;
1141
0
        l_current_poc->resE = l_current_poc->resno1;
1142
0
        l_current_poc->layE = l_current_poc->layno1;
1143
0
        l_current_poc->prg  = l_current_poc->prg1;
1144
0
        l_current_poc->prcS = 0;
1145
        /* special treatment here different from the first element*/
1146
0
        l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1147
0
                              l_current_poc->layE : 0;
1148
1149
0
        l_current_poc->prcE = p_max_prec;
1150
0
        l_current_poc->txS = (OPJ_UINT32)p_tx0;
1151
0
        l_current_poc->txE = (OPJ_UINT32)p_tx1;
1152
0
        l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1153
0
        l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1154
0
        l_current_poc->dx = p_dx_min;
1155
0
        l_current_poc->dy = p_dy_min;
1156
0
        ++ l_current_poc;
1157
0
    }
1158
0
}
1159
1160
static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1161
        OPJ_UINT32 p_num_comps,
1162
        OPJ_UINT32 p_tileno,
1163
        OPJ_UINT32 p_tx0,
1164
        OPJ_UINT32 p_tx1,
1165
        OPJ_UINT32 p_ty0,
1166
        OPJ_UINT32 p_ty1,
1167
        OPJ_UINT32 p_max_prec,
1168
        OPJ_UINT32 p_max_res,
1169
        OPJ_UINT32 p_dx_min,
1170
        OPJ_UINT32 p_dy_min)
1171
38.6k
{
1172
    /* loop*/
1173
38.6k
    OPJ_UINT32 pino;
1174
    /* tile coding parameter*/
1175
38.6k
    opj_tcp_t *l_tcp = 00;
1176
    /* current poc being updated*/
1177
38.6k
    opj_poc_t * l_current_poc = 00;
1178
    /* number of pocs*/
1179
38.6k
    OPJ_UINT32 l_poc_bound;
1180
1181
    /* preconditions in debug*/
1182
38.6k
    assert(p_cp != 00);
1183
38.6k
    assert(p_tileno < p_cp->tw * p_cp->th);
1184
1185
    /* initializations*/
1186
38.6k
    l_tcp = &p_cp->tcps [p_tileno];
1187
1188
    /* number of iterations in the loop */
1189
38.6k
    l_poc_bound = l_tcp->numpocs + 1;
1190
1191
    /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1192
       store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1193
38.6k
    l_current_poc = l_tcp->pocs;
1194
1195
77.2k
    for (pino = 0; pino < l_poc_bound ; ++pino) {
1196
38.6k
        l_current_poc->compS = 0;
1197
38.6k
        l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1198
38.6k
        l_current_poc->resS = 0;
1199
38.6k
        l_current_poc->resE = p_max_res;
1200
38.6k
        l_current_poc->layS = 0;
1201
38.6k
        l_current_poc->layE = l_tcp->numlayers;
1202
38.6k
        l_current_poc->prg  = l_tcp->prg;
1203
38.6k
        l_current_poc->prcS = 0;
1204
38.6k
        l_current_poc->prcE = p_max_prec;
1205
38.6k
        l_current_poc->txS = p_tx0;
1206
38.6k
        l_current_poc->txE = p_tx1;
1207
38.6k
        l_current_poc->tyS = p_ty0;
1208
38.6k
        l_current_poc->tyE = p_ty1;
1209
38.6k
        l_current_poc->dx = p_dx_min;
1210
38.6k
        l_current_poc->dy = p_dy_min;
1211
38.6k
        ++ l_current_poc;
1212
38.6k
    }
1213
38.6k
}
1214
1215
static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1216
                                     opj_tcp_t * p_tcp,
1217
                                     OPJ_UINT32 p_max_precision,
1218
                                     OPJ_UINT32 p_max_res)
1219
4.47k
{
1220
    /* loop*/
1221
4.47k
    OPJ_UINT32 pino;
1222
1223
    /* encoding parameters to set*/
1224
4.47k
    OPJ_UINT32 l_bound;
1225
1226
4.47k
    opj_pi_iterator_t * l_current_pi = 00;
1227
4.47k
    opj_poc_t* l_current_poc = 0;
1228
1229
4.47k
    OPJ_ARG_NOT_USED(p_max_res);
1230
1231
    /* preconditions in debug*/
1232
4.47k
    assert(p_pi != 00);
1233
4.47k
    assert(p_tcp != 00);
1234
1235
    /* initializations*/
1236
4.47k
    l_bound = p_tcp->numpocs + 1;
1237
4.47k
    l_current_pi = p_pi;
1238
4.47k
    l_current_poc = p_tcp->pocs;
1239
1240
33.7k
    for (pino = 0; pino < l_bound; ++pino) {
1241
29.2k
        l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1242
29.2k
        l_current_pi->first = 1;
1243
1244
29.2k
        l_current_pi->poc.resno0 =
1245
29.2k
            l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1246
29.2k
        l_current_pi->poc.compno0 =
1247
29.2k
            l_current_poc->compno0; /* Component Index #0 (Start) */
1248
29.2k
        l_current_pi->poc.layno0 = 0;
1249
29.2k
        l_current_pi->poc.precno0 = 0;
1250
29.2k
        l_current_pi->poc.resno1 =
1251
29.2k
            l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1252
29.2k
        l_current_pi->poc.compno1 =
1253
29.2k
            l_current_poc->compno1; /* Component Index #0 (End) */
1254
29.2k
        l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1255
29.2k
                                                p_tcp->numlayers); /* Layer Index #0 (End) */
1256
29.2k
        l_current_pi->poc.precno1 = p_max_precision;
1257
29.2k
        ++l_current_pi;
1258
29.2k
        ++l_current_poc;
1259
29.2k
    }
1260
4.47k
}
1261
1262
static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1263
        opj_tcp_t * p_tcp,
1264
        OPJ_UINT32 p_max_precision,
1265
        OPJ_UINT32 p_max_res)
1266
30.1k
{
1267
    /* loop*/
1268
30.1k
    OPJ_UINT32 pino;
1269
1270
    /* encoding parameters to set*/
1271
30.1k
    OPJ_UINT32 l_bound;
1272
1273
30.1k
    opj_pi_iterator_t * l_current_pi = 00;
1274
    /* preconditions in debug*/
1275
30.1k
    assert(p_tcp != 00);
1276
30.1k
    assert(p_pi != 00);
1277
1278
    /* initializations*/
1279
30.1k
    l_bound = p_tcp->numpocs + 1;
1280
30.1k
    l_current_pi = p_pi;
1281
1282
60.3k
    for (pino = 0; pino < l_bound; ++pino) {
1283
30.1k
        l_current_pi->poc.prg = p_tcp->prg;
1284
30.1k
        l_current_pi->first = 1;
1285
30.1k
        l_current_pi->poc.resno0 = 0;
1286
30.1k
        l_current_pi->poc.compno0 = 0;
1287
30.1k
        l_current_pi->poc.layno0 = 0;
1288
30.1k
        l_current_pi->poc.precno0 = 0;
1289
30.1k
        l_current_pi->poc.resno1 = p_max_res;
1290
30.1k
        l_current_pi->poc.compno1 = l_current_pi->numcomps;
1291
30.1k
        l_current_pi->poc.layno1 = p_tcp->numlayers;
1292
30.1k
        l_current_pi->poc.precno1 = p_max_precision;
1293
30.1k
        ++l_current_pi;
1294
30.1k
    }
1295
30.1k
}
1296
1297
1298
1299
static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1300
                                        opj_cp_t *cp,
1301
                                        OPJ_UINT32 tileno,
1302
                                        OPJ_UINT32 pino,
1303
                                        const OPJ_CHAR *prog)
1304
0
{
1305
0
    OPJ_INT32 i;
1306
0
    opj_tcp_t *tcps = &cp->tcps[tileno];
1307
0
    opj_poc_t *tcp = &tcps->pocs[pino];
1308
1309
0
    if (pos >= 0) {
1310
0
        for (i = pos; i >= 0; i--) {
1311
0
            switch (prog[i]) {
1312
0
            case 'R':
1313
0
                if (tcp->res_t == tcp->resE) {
1314
0
                    if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1315
0
                        return OPJ_TRUE;
1316
0
                    } else {
1317
0
                        return OPJ_FALSE;
1318
0
                    }
1319
0
                } else {
1320
0
                    return OPJ_TRUE;
1321
0
                }
1322
0
                break;
1323
0
            case 'C':
1324
0
                if (tcp->comp_t == tcp->compE) {
1325
0
                    if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1326
0
                        return OPJ_TRUE;
1327
0
                    } else {
1328
0
                        return OPJ_FALSE;
1329
0
                    }
1330
0
                } else {
1331
0
                    return OPJ_TRUE;
1332
0
                }
1333
0
                break;
1334
0
            case 'L':
1335
0
                if (tcp->lay_t == tcp->layE) {
1336
0
                    if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1337
0
                        return OPJ_TRUE;
1338
0
                    } else {
1339
0
                        return OPJ_FALSE;
1340
0
                    }
1341
0
                } else {
1342
0
                    return OPJ_TRUE;
1343
0
                }
1344
0
                break;
1345
0
            case 'P':
1346
0
                switch (tcp->prg) {
1347
0
                case OPJ_LRCP: /* fall through */
1348
0
                case OPJ_RLCP:
1349
0
                    if (tcp->prc_t == tcp->prcE) {
1350
0
                        if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1351
0
                            return OPJ_TRUE;
1352
0
                        } else {
1353
0
                            return OPJ_FALSE;
1354
0
                        }
1355
0
                    } else {
1356
0
                        return OPJ_TRUE;
1357
0
                    }
1358
0
                    break;
1359
0
                default:
1360
0
                    if (tcp->tx0_t == tcp->txE) {
1361
                        /*TY*/
1362
0
                        if (tcp->ty0_t == tcp->tyE) {
1363
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1364
0
                                return OPJ_TRUE;
1365
0
                            } else {
1366
0
                                return OPJ_FALSE;
1367
0
                            }
1368
0
                        } else {
1369
0
                            return OPJ_TRUE;
1370
0
                        }/*TY*/
1371
0
                    } else {
1372
0
                        return OPJ_TRUE;
1373
0
                    }
1374
0
                    break;
1375
0
                }/*end case P*/
1376
0
            }/*end switch*/
1377
0
        }/*end for*/
1378
0
    }/*end if*/
1379
0
    return OPJ_FALSE;
1380
0
}
1381
1382
1383
/*
1384
==========================================================
1385
   Packet iterator interface
1386
==========================================================
1387
*/
1388
opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1389
                                        opj_cp_t *p_cp,
1390
                                        OPJ_UINT32 p_tile_no,
1391
                                        opj_event_mgr_t* manager)
1392
34.7k
{
1393
34.7k
    OPJ_UINT32 numcomps = p_image->numcomps;
1394
1395
    /* loop */
1396
34.7k
    OPJ_UINT32 pino;
1397
34.7k
    OPJ_UINT32 compno, resno;
1398
1399
    /* to store w, h, dx and dy for all components and resolutions */
1400
34.7k
    OPJ_UINT32 * l_tmp_data;
1401
34.7k
    OPJ_UINT32 ** l_tmp_ptr;
1402
1403
    /* encoding parameters to set */
1404
34.7k
    OPJ_UINT32 l_max_res;
1405
34.7k
    OPJ_UINT32 l_max_prec;
1406
34.7k
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1407
34.7k
    OPJ_UINT32 l_dx_min, l_dy_min;
1408
34.7k
    OPJ_UINT32 l_bound;
1409
34.7k
    OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1410
34.7k
    OPJ_UINT32 l_data_stride;
1411
1412
    /* pointers */
1413
34.7k
    opj_pi_iterator_t *l_pi = 00;
1414
34.7k
    opj_tcp_t *l_tcp = 00;
1415
34.7k
    opj_pi_comp_t *l_current_comp = 00;
1416
34.7k
    opj_image_comp_t * l_img_comp = 00;
1417
34.7k
    opj_pi_iterator_t * l_current_pi = 00;
1418
34.7k
    OPJ_UINT32 * l_encoding_value_ptr = 00;
1419
1420
    /* preconditions in debug */
1421
34.7k
    assert(p_cp != 00);
1422
34.7k
    assert(p_image != 00);
1423
34.7k
    assert(p_tile_no < p_cp->tw * p_cp->th);
1424
1425
    /* initializations */
1426
34.7k
    l_tcp = &p_cp->tcps[p_tile_no];
1427
34.7k
    l_bound = l_tcp->numpocs + 1;
1428
1429
34.7k
    l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1430
34.7k
    l_tmp_data = (OPJ_UINT32*)opj_malloc(
1431
34.7k
                     l_data_stride * numcomps * sizeof(OPJ_UINT32));
1432
34.7k
    if
1433
34.7k
    (! l_tmp_data) {
1434
0
        return 00;
1435
0
    }
1436
34.7k
    l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1437
34.7k
                    numcomps * sizeof(OPJ_UINT32 *));
1438
34.7k
    if
1439
34.7k
    (! l_tmp_ptr) {
1440
0
        opj_free(l_tmp_data);
1441
0
        return 00;
1442
0
    }
1443
1444
    /* memory allocation for pi */
1445
34.7k
    l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1446
34.7k
    if (!l_pi) {
1447
0
        opj_free(l_tmp_data);
1448
0
        opj_free(l_tmp_ptr);
1449
0
        return 00;
1450
0
    }
1451
1452
34.7k
    l_encoding_value_ptr = l_tmp_data;
1453
    /* update pointer array */
1454
34.7k
    for
1455
213k
    (compno = 0; compno < numcomps; ++compno) {
1456
178k
        l_tmp_ptr[compno] = l_encoding_value_ptr;
1457
178k
        l_encoding_value_ptr += l_data_stride;
1458
178k
    }
1459
    /* get encoding parameters */
1460
34.7k
    opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1461
34.7k
                                    &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1462
1463
    /* step calculations */
1464
34.7k
    l_step_p = 1;
1465
34.7k
    l_step_c = l_max_prec * l_step_p;
1466
34.7k
    l_step_r = numcomps * l_step_c;
1467
34.7k
    l_step_l = l_max_res * l_step_r;
1468
1469
    /* set values for first packet iterator */
1470
34.7k
    l_current_pi = l_pi;
1471
1472
    /* memory allocation for include */
1473
    /* prevent an integer overflow issue */
1474
    /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1475
34.7k
    l_current_pi->include = 00;
1476
34.7k
    if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1477
34.7k
        l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1478
34.7k
        l_current_pi->include = (OPJ_INT16*) opj_calloc(
1479
34.7k
                                    l_current_pi->include_size, sizeof(OPJ_INT16));
1480
34.7k
    }
1481
1482
34.7k
    if (!l_current_pi->include) {
1483
50
        opj_free(l_tmp_data);
1484
50
        opj_free(l_tmp_ptr);
1485
50
        opj_pi_destroy(l_pi, l_bound);
1486
50
        return 00;
1487
50
    }
1488
1489
    /* special treatment for the first packet iterator */
1490
34.6k
    l_current_comp = l_current_pi->comps;
1491
34.6k
    l_img_comp = p_image->comps;
1492
1493
34.6k
    l_current_pi->tx0 = l_tx0;
1494
34.6k
    l_current_pi->ty0 = l_ty0;
1495
34.6k
    l_current_pi->tx1 = l_tx1;
1496
34.6k
    l_current_pi->ty1 = l_ty1;
1497
1498
    /*l_current_pi->dx = l_img_comp->dx;*/
1499
    /*l_current_pi->dy = l_img_comp->dy;*/
1500
1501
34.6k
    l_current_pi->step_p = l_step_p;
1502
34.6k
    l_current_pi->step_c = l_step_c;
1503
34.6k
    l_current_pi->step_r = l_step_r;
1504
34.6k
    l_current_pi->step_l = l_step_l;
1505
1506
    /* allocation for components and number of components has already been calculated by opj_pi_create */
1507
34.6k
    for
1508
213k
    (compno = 0; compno < numcomps; ++compno) {
1509
178k
        opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1510
178k
        l_encoding_value_ptr = l_tmp_ptr[compno];
1511
1512
178k
        l_current_comp->dx = l_img_comp->dx;
1513
178k
        l_current_comp->dy = l_img_comp->dy;
1514
        /* resolutions have already been initialized */
1515
178k
        for
1516
1.89M
        (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1517
1.72M
            l_res->pdx = *(l_encoding_value_ptr++);
1518
1.72M
            l_res->pdy = *(l_encoding_value_ptr++);
1519
1.72M
            l_res->pw =  *(l_encoding_value_ptr++);
1520
1.72M
            l_res->ph =  *(l_encoding_value_ptr++);
1521
1.72M
            ++l_res;
1522
1.72M
        }
1523
178k
        ++l_current_comp;
1524
178k
        ++l_img_comp;
1525
178k
    }
1526
34.6k
    ++l_current_pi;
1527
1528
59.4k
    for (pino = 1 ; pino < l_bound ; ++pino) {
1529
24.8k
        l_current_comp = l_current_pi->comps;
1530
24.8k
        l_img_comp = p_image->comps;
1531
1532
24.8k
        l_current_pi->tx0 = l_tx0;
1533
24.8k
        l_current_pi->ty0 = l_ty0;
1534
24.8k
        l_current_pi->tx1 = l_tx1;
1535
24.8k
        l_current_pi->ty1 = l_ty1;
1536
        /*l_current_pi->dx = l_dx_min;*/
1537
        /*l_current_pi->dy = l_dy_min;*/
1538
24.8k
        l_current_pi->step_p = l_step_p;
1539
24.8k
        l_current_pi->step_c = l_step_c;
1540
24.8k
        l_current_pi->step_r = l_step_r;
1541
24.8k
        l_current_pi->step_l = l_step_l;
1542
1543
        /* allocation for components and number of components has already been calculated by opj_pi_create */
1544
24.8k
        for
1545
120k
        (compno = 0; compno < numcomps; ++compno) {
1546
95.7k
            opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1547
95.7k
            l_encoding_value_ptr = l_tmp_ptr[compno];
1548
1549
95.7k
            l_current_comp->dx = l_img_comp->dx;
1550
95.7k
            l_current_comp->dy = l_img_comp->dy;
1551
            /* resolutions have already been initialized */
1552
95.7k
            for
1553
1.25M
            (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1554
1.16M
                l_res->pdx = *(l_encoding_value_ptr++);
1555
1.16M
                l_res->pdy = *(l_encoding_value_ptr++);
1556
1.16M
                l_res->pw =  *(l_encoding_value_ptr++);
1557
1.16M
                l_res->ph =  *(l_encoding_value_ptr++);
1558
1.16M
                ++l_res;
1559
1.16M
            }
1560
95.7k
            ++l_current_comp;
1561
95.7k
            ++l_img_comp;
1562
95.7k
        }
1563
        /* special treatment*/
1564
24.8k
        l_current_pi->include = (l_current_pi - 1)->include;
1565
24.8k
        l_current_pi->include_size = (l_current_pi - 1)->include_size;
1566
24.8k
        ++l_current_pi;
1567
24.8k
    }
1568
34.6k
    opj_free(l_tmp_data);
1569
34.6k
    l_tmp_data = 00;
1570
34.6k
    opj_free(l_tmp_ptr);
1571
34.6k
    l_tmp_ptr = 00;
1572
34.6k
    if
1573
34.6k
    (l_tcp->POC) {
1574
4.47k
        opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1575
30.1k
    } else {
1576
30.1k
        opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1577
30.1k
    }
1578
34.6k
    return l_pi;
1579
34.7k
}
1580
1581
1582
OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
1583
        const opj_cp_t *p_cp,
1584
        OPJ_UINT32 p_tile_no)
1585
0
{
1586
0
    OPJ_UINT32 l_max_res;
1587
0
    OPJ_UINT32 l_max_prec;
1588
0
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1589
0
    OPJ_UINT32 l_dx_min, l_dy_min;
1590
1591
    /* preconditions in debug*/
1592
0
    assert(p_cp != 00);
1593
0
    assert(p_image != 00);
1594
0
    assert(p_tile_no < p_cp->tw * p_cp->th);
1595
1596
    /* get encoding parameters*/
1597
0
    opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1598
0
                                    &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
1599
1600
0
    return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
1601
0
           l_max_res;
1602
0
}
1603
1604
1605
opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1606
        opj_cp_t *p_cp,
1607
        OPJ_UINT32 p_tile_no,
1608
        J2K_T2_MODE p_t2_mode,
1609
        opj_event_mgr_t* manager)
1610
29.5k
{
1611
29.5k
    OPJ_UINT32 numcomps = p_image->numcomps;
1612
1613
    /* loop*/
1614
29.5k
    OPJ_UINT32 pino;
1615
29.5k
    OPJ_UINT32 compno, resno;
1616
1617
    /* to store w, h, dx and dy for all components and resolutions*/
1618
29.5k
    OPJ_UINT32 * l_tmp_data;
1619
29.5k
    OPJ_UINT32 ** l_tmp_ptr;
1620
1621
    /* encoding parameters to set*/
1622
29.5k
    OPJ_UINT32 l_max_res;
1623
29.5k
    OPJ_UINT32 l_max_prec;
1624
29.5k
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1625
29.5k
    OPJ_UINT32 l_dx_min, l_dy_min;
1626
29.5k
    OPJ_UINT32 l_bound;
1627
29.5k
    OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1628
29.5k
    OPJ_UINT32 l_data_stride;
1629
1630
    /* pointers*/
1631
29.5k
    opj_pi_iterator_t *l_pi = 00;
1632
29.5k
    opj_tcp_t *l_tcp = 00;
1633
29.5k
    opj_pi_comp_t *l_current_comp = 00;
1634
29.5k
    opj_image_comp_t * l_img_comp = 00;
1635
29.5k
    opj_pi_iterator_t * l_current_pi = 00;
1636
29.5k
    OPJ_UINT32 * l_encoding_value_ptr = 00;
1637
1638
    /* preconditions in debug*/
1639
29.5k
    assert(p_cp != 00);
1640
29.5k
    assert(p_image != 00);
1641
29.5k
    assert(p_tile_no < p_cp->tw * p_cp->th);
1642
1643
    /* initializations*/
1644
29.5k
    l_tcp = &p_cp->tcps[p_tile_no];
1645
29.5k
    l_bound = l_tcp->numpocs + 1;
1646
1647
29.5k
    l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1648
29.5k
    l_tmp_data = (OPJ_UINT32*)opj_malloc(
1649
29.5k
                     l_data_stride * numcomps * sizeof(OPJ_UINT32));
1650
29.5k
    if (! l_tmp_data) {
1651
0
        return 00;
1652
0
    }
1653
1654
29.5k
    l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1655
29.5k
                    numcomps * sizeof(OPJ_UINT32 *));
1656
29.5k
    if (! l_tmp_ptr) {
1657
0
        opj_free(l_tmp_data);
1658
0
        return 00;
1659
0
    }
1660
1661
    /* memory allocation for pi*/
1662
29.5k
    l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1663
29.5k
    if (!l_pi) {
1664
0
        opj_free(l_tmp_data);
1665
0
        opj_free(l_tmp_ptr);
1666
0
        return 00;
1667
0
    }
1668
1669
29.5k
    l_encoding_value_ptr = l_tmp_data;
1670
    /* update pointer array*/
1671
60.5k
    for (compno = 0; compno < numcomps; ++compno) {
1672
30.9k
        l_tmp_ptr[compno] = l_encoding_value_ptr;
1673
30.9k
        l_encoding_value_ptr += l_data_stride;
1674
30.9k
    }
1675
1676
    /* get encoding parameters*/
1677
29.5k
    opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1678
29.5k
                                    &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1679
1680
    /* step calculations*/
1681
29.5k
    l_step_p = 1;
1682
29.5k
    l_step_c = l_max_prec * l_step_p;
1683
29.5k
    l_step_r = numcomps * l_step_c;
1684
29.5k
    l_step_l = l_max_res * l_step_r;
1685
1686
    /* set values for first packet iterator*/
1687
29.5k
    l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1688
29.5k
    l_current_pi = l_pi;
1689
1690
    /* memory allocation for include*/
1691
29.5k
    l_current_pi->include = NULL;
1692
29.5k
    if (l_step_l <= UINT_MAX / l_tcp->numlayers) {
1693
29.5k
        l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1694
29.5k
        l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1695
29.5k
                                sizeof(OPJ_INT16));
1696
29.5k
    }
1697
29.5k
    if (!l_current_pi->include) {
1698
0
        opj_free(l_tmp_data);
1699
0
        opj_free(l_tmp_ptr);
1700
0
        opj_pi_destroy(l_pi, l_bound);
1701
0
        return 00;
1702
0
    }
1703
1704
    /* special treatment for the first packet iterator*/
1705
29.5k
    l_current_comp = l_current_pi->comps;
1706
29.5k
    l_img_comp = p_image->comps;
1707
29.5k
    l_current_pi->tx0 = l_tx0;
1708
29.5k
    l_current_pi->ty0 = l_ty0;
1709
29.5k
    l_current_pi->tx1 = l_tx1;
1710
29.5k
    l_current_pi->ty1 = l_ty1;
1711
29.5k
    l_current_pi->dx = l_dx_min;
1712
29.5k
    l_current_pi->dy = l_dy_min;
1713
29.5k
    l_current_pi->step_p = l_step_p;
1714
29.5k
    l_current_pi->step_c = l_step_c;
1715
29.5k
    l_current_pi->step_r = l_step_r;
1716
29.5k
    l_current_pi->step_l = l_step_l;
1717
1718
    /* allocation for components and number of components has already been calculated by opj_pi_create */
1719
60.5k
    for (compno = 0; compno < numcomps; ++compno) {
1720
30.9k
        opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1721
30.9k
        l_encoding_value_ptr = l_tmp_ptr[compno];
1722
1723
30.9k
        l_current_comp->dx = l_img_comp->dx;
1724
30.9k
        l_current_comp->dy = l_img_comp->dy;
1725
1726
        /* resolutions have already been initialized */
1727
202k
        for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1728
171k
            l_res->pdx = *(l_encoding_value_ptr++);
1729
171k
            l_res->pdy = *(l_encoding_value_ptr++);
1730
171k
            l_res->pw =  *(l_encoding_value_ptr++);
1731
171k
            l_res->ph =  *(l_encoding_value_ptr++);
1732
171k
            ++l_res;
1733
171k
        }
1734
1735
30.9k
        ++l_current_comp;
1736
30.9k
        ++l_img_comp;
1737
30.9k
    }
1738
29.5k
    ++l_current_pi;
1739
1740
29.5k
    for (pino = 1 ; pino < l_bound ; ++pino) {
1741
0
        l_current_comp = l_current_pi->comps;
1742
0
        l_img_comp = p_image->comps;
1743
1744
0
        l_current_pi->tx0 = l_tx0;
1745
0
        l_current_pi->ty0 = l_ty0;
1746
0
        l_current_pi->tx1 = l_tx1;
1747
0
        l_current_pi->ty1 = l_ty1;
1748
0
        l_current_pi->dx = l_dx_min;
1749
0
        l_current_pi->dy = l_dy_min;
1750
0
        l_current_pi->step_p = l_step_p;
1751
0
        l_current_pi->step_c = l_step_c;
1752
0
        l_current_pi->step_r = l_step_r;
1753
0
        l_current_pi->step_l = l_step_l;
1754
1755
        /* allocation for components and number of components has already been calculated by opj_pi_create */
1756
0
        for (compno = 0; compno < numcomps; ++compno) {
1757
0
            opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1758
0
            l_encoding_value_ptr = l_tmp_ptr[compno];
1759
1760
0
            l_current_comp->dx = l_img_comp->dx;
1761
0
            l_current_comp->dy = l_img_comp->dy;
1762
            /* resolutions have already been initialized */
1763
0
            for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1764
0
                l_res->pdx = *(l_encoding_value_ptr++);
1765
0
                l_res->pdy = *(l_encoding_value_ptr++);
1766
0
                l_res->pw =  *(l_encoding_value_ptr++);
1767
0
                l_res->ph =  *(l_encoding_value_ptr++);
1768
0
                ++l_res;
1769
0
            }
1770
0
            ++l_current_comp;
1771
0
            ++l_img_comp;
1772
0
        }
1773
1774
        /* special treatment*/
1775
0
        l_current_pi->include = (l_current_pi - 1)->include;
1776
0
        l_current_pi->include_size = (l_current_pi - 1)->include_size;
1777
0
        ++l_current_pi;
1778
0
    }
1779
1780
29.5k
    opj_free(l_tmp_data);
1781
29.5k
    l_tmp_data = 00;
1782
29.5k
    opj_free(l_tmp_ptr);
1783
29.5k
    l_tmp_ptr = 00;
1784
1785
29.5k
    if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1786
0
        opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1787
0
                                           l_max_prec, l_max_res, l_dx_min, l_dy_min);
1788
29.5k
    } else {
1789
29.5k
        opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1790
29.5k
                                     l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1791
29.5k
    }
1792
1793
29.5k
    return l_pi;
1794
29.5k
}
1795
1796
void opj_pi_create_encode(opj_pi_iterator_t *pi,
1797
                          opj_cp_t *cp,
1798
                          OPJ_UINT32 tileno,
1799
                          OPJ_UINT32 pino,
1800
                          OPJ_UINT32 tpnum,
1801
                          OPJ_INT32 tppos,
1802
                          J2K_T2_MODE t2_mode)
1803
29.5k
{
1804
29.5k
    const OPJ_CHAR *prog;
1805
29.5k
    OPJ_INT32 i;
1806
29.5k
    OPJ_UINT32 incr_top = 1, resetX = 0;
1807
29.5k
    opj_tcp_t *tcps = &cp->tcps[tileno];
1808
29.5k
    opj_poc_t *tcp = &tcps->pocs[pino];
1809
1810
29.5k
    prog = opj_j2k_convert_progression_order(tcp->prg);
1811
1812
29.5k
    pi[pino].first = 1;
1813
29.5k
    pi[pino].poc.prg = tcp->prg;
1814
1815
29.5k
    if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1816
21.4k
            !OPJ_IS_IMF(cp->rsiz) &&
1817
28.7k
            (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
1818
28.7k
        pi[pino].poc.resno0 = tcp->resS;
1819
28.7k
        pi[pino].poc.resno1 = tcp->resE;
1820
28.7k
        pi[pino].poc.compno0 = tcp->compS;
1821
28.7k
        pi[pino].poc.compno1 = tcp->compE;
1822
28.7k
        pi[pino].poc.layno0 = tcp->layS;
1823
28.7k
        pi[pino].poc.layno1 = tcp->layE;
1824
28.7k
        pi[pino].poc.precno0 = tcp->prcS;
1825
28.7k
        pi[pino].poc.precno1 = tcp->prcE;
1826
28.7k
        pi[pino].poc.tx0 = tcp->txS;
1827
28.7k
        pi[pino].poc.ty0 = tcp->tyS;
1828
28.7k
        pi[pino].poc.tx1 = tcp->txE;
1829
28.7k
        pi[pino].poc.ty1 = tcp->tyE;
1830
28.7k
    } else {
1831
3.38k
        for (i = tppos + 1; i < 4; i++) {
1832
2.54k
            switch (prog[i]) {
1833
847
            case 'R':
1834
847
                pi[pino].poc.resno0 = tcp->resS;
1835
847
                pi[pino].poc.resno1 = tcp->resE;
1836
847
                break;
1837
0
            case 'C':
1838
0
                pi[pino].poc.compno0 = tcp->compS;
1839
0
                pi[pino].poc.compno1 = tcp->compE;
1840
0
                break;
1841
847
            case 'L':
1842
847
                pi[pino].poc.layno0 = tcp->layS;
1843
847
                pi[pino].poc.layno1 = tcp->layE;
1844
847
                break;
1845
847
            case 'P':
1846
847
                switch (tcp->prg) {
1847
0
                case OPJ_LRCP:
1848
0
                case OPJ_RLCP:
1849
0
                    pi[pino].poc.precno0 = tcp->prcS;
1850
0
                    pi[pino].poc.precno1 = tcp->prcE;
1851
0
                    break;
1852
847
                default:
1853
847
                    pi[pino].poc.tx0 = tcp->txS;
1854
847
                    pi[pino].poc.ty0 = tcp->tyS;
1855
847
                    pi[pino].poc.tx1 = tcp->txE;
1856
847
                    pi[pino].poc.ty1 = tcp->tyE;
1857
847
                    break;
1858
847
                }
1859
847
                break;
1860
2.54k
            }
1861
2.54k
        }
1862
1863
847
        if (tpnum == 0) {
1864
1.69k
            for (i = tppos; i >= 0; i--) {
1865
847
                switch (prog[i]) {
1866
847
                case 'C':
1867
847
                    tcp->comp_t = tcp->compS;
1868
847
                    pi[pino].poc.compno0 = tcp->comp_t;
1869
847
                    pi[pino].poc.compno1 = tcp->comp_t + 1;
1870
847
                    tcp->comp_t += 1;
1871
847
                    break;
1872
0
                case 'R':
1873
0
                    tcp->res_t = tcp->resS;
1874
0
                    pi[pino].poc.resno0 = tcp->res_t;
1875
0
                    pi[pino].poc.resno1 = tcp->res_t + 1;
1876
0
                    tcp->res_t += 1;
1877
0
                    break;
1878
0
                case 'L':
1879
0
                    tcp->lay_t = tcp->layS;
1880
0
                    pi[pino].poc.layno0 = tcp->lay_t;
1881
0
                    pi[pino].poc.layno1 = tcp->lay_t + 1;
1882
0
                    tcp->lay_t += 1;
1883
0
                    break;
1884
0
                case 'P':
1885
0
                    switch (tcp->prg) {
1886
0
                    case OPJ_LRCP:
1887
0
                    case OPJ_RLCP:
1888
0
                        tcp->prc_t = tcp->prcS;
1889
0
                        pi[pino].poc.precno0 = tcp->prc_t;
1890
0
                        pi[pino].poc.precno1 = tcp->prc_t + 1;
1891
0
                        tcp->prc_t += 1;
1892
0
                        break;
1893
0
                    default:
1894
0
                        tcp->tx0_t = tcp->txS;
1895
0
                        tcp->ty0_t = tcp->tyS;
1896
0
                        pi[pino].poc.tx0 = tcp->tx0_t;
1897
0
                        pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1898
0
                        pi[pino].poc.ty0 = tcp->ty0_t;
1899
0
                        pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1900
0
                        tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1901
0
                        tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1902
0
                        break;
1903
0
                    }
1904
0
                    break;
1905
847
                }
1906
847
            }
1907
847
            incr_top = 1;
1908
847
        } else {
1909
0
            for (i = tppos; i >= 0; i--) {
1910
0
                switch (prog[i]) {
1911
0
                case 'C':
1912
0
                    pi[pino].poc.compno0 = tcp->comp_t - 1;
1913
0
                    pi[pino].poc.compno1 = tcp->comp_t;
1914
0
                    break;
1915
0
                case 'R':
1916
0
                    pi[pino].poc.resno0 = tcp->res_t - 1;
1917
0
                    pi[pino].poc.resno1 = tcp->res_t;
1918
0
                    break;
1919
0
                case 'L':
1920
0
                    pi[pino].poc.layno0 = tcp->lay_t - 1;
1921
0
                    pi[pino].poc.layno1 = tcp->lay_t;
1922
0
                    break;
1923
0
                case 'P':
1924
0
                    switch (tcp->prg) {
1925
0
                    case OPJ_LRCP:
1926
0
                    case OPJ_RLCP:
1927
0
                        pi[pino].poc.precno0 = tcp->prc_t - 1;
1928
0
                        pi[pino].poc.precno1 = tcp->prc_t;
1929
0
                        break;
1930
0
                    default:
1931
0
                        pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1932
0
                        pi[pino].poc.tx1 = tcp->tx0_t ;
1933
0
                        pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1934
0
                        pi[pino].poc.ty1 = tcp->ty0_t ;
1935
0
                        break;
1936
0
                    }
1937
0
                    break;
1938
0
                }
1939
0
                if (incr_top == 1) {
1940
0
                    switch (prog[i]) {
1941
0
                    case 'R':
1942
0
                        if (tcp->res_t == tcp->resE) {
1943
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1944
0
                                tcp->res_t = tcp->resS;
1945
0
                                pi[pino].poc.resno0 = tcp->res_t;
1946
0
                                pi[pino].poc.resno1 = tcp->res_t + 1;
1947
0
                                tcp->res_t += 1;
1948
0
                                incr_top = 1;
1949
0
                            } else {
1950
0
                                incr_top = 0;
1951
0
                            }
1952
0
                        } else {
1953
0
                            pi[pino].poc.resno0 = tcp->res_t;
1954
0
                            pi[pino].poc.resno1 = tcp->res_t + 1;
1955
0
                            tcp->res_t += 1;
1956
0
                            incr_top = 0;
1957
0
                        }
1958
0
                        break;
1959
0
                    case 'C':
1960
0
                        if (tcp->comp_t == tcp->compE) {
1961
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1962
0
                                tcp->comp_t = tcp->compS;
1963
0
                                pi[pino].poc.compno0 = tcp->comp_t;
1964
0
                                pi[pino].poc.compno1 = tcp->comp_t + 1;
1965
0
                                tcp->comp_t += 1;
1966
0
                                incr_top = 1;
1967
0
                            } else {
1968
0
                                incr_top = 0;
1969
0
                            }
1970
0
                        } else {
1971
0
                            pi[pino].poc.compno0 = tcp->comp_t;
1972
0
                            pi[pino].poc.compno1 = tcp->comp_t + 1;
1973
0
                            tcp->comp_t += 1;
1974
0
                            incr_top = 0;
1975
0
                        }
1976
0
                        break;
1977
0
                    case 'L':
1978
0
                        if (tcp->lay_t == tcp->layE) {
1979
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1980
0
                                tcp->lay_t = tcp->layS;
1981
0
                                pi[pino].poc.layno0 = tcp->lay_t;
1982
0
                                pi[pino].poc.layno1 = tcp->lay_t + 1;
1983
0
                                tcp->lay_t += 1;
1984
0
                                incr_top = 1;
1985
0
                            } else {
1986
0
                                incr_top = 0;
1987
0
                            }
1988
0
                        } else {
1989
0
                            pi[pino].poc.layno0 = tcp->lay_t;
1990
0
                            pi[pino].poc.layno1 = tcp->lay_t + 1;
1991
0
                            tcp->lay_t += 1;
1992
0
                            incr_top = 0;
1993
0
                        }
1994
0
                        break;
1995
0
                    case 'P':
1996
0
                        switch (tcp->prg) {
1997
0
                        case OPJ_LRCP:
1998
0
                        case OPJ_RLCP:
1999
0
                            if (tcp->prc_t == tcp->prcE) {
2000
0
                                if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2001
0
                                    tcp->prc_t = tcp->prcS;
2002
0
                                    pi[pino].poc.precno0 = tcp->prc_t;
2003
0
                                    pi[pino].poc.precno1 = tcp->prc_t + 1;
2004
0
                                    tcp->prc_t += 1;
2005
0
                                    incr_top = 1;
2006
0
                                } else {
2007
0
                                    incr_top = 0;
2008
0
                                }
2009
0
                            } else {
2010
0
                                pi[pino].poc.precno0 = tcp->prc_t;
2011
0
                                pi[pino].poc.precno1 = tcp->prc_t + 1;
2012
0
                                tcp->prc_t += 1;
2013
0
                                incr_top = 0;
2014
0
                            }
2015
0
                            break;
2016
0
                        default:
2017
0
                            if (tcp->tx0_t >= tcp->txE) {
2018
0
                                if (tcp->ty0_t >= tcp->tyE) {
2019
0
                                    if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2020
0
                                        tcp->ty0_t = tcp->tyS;
2021
0
                                        pi[pino].poc.ty0 = tcp->ty0_t;
2022
0
                                        pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2023
0
                                        tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2024
0
                                        incr_top = 1;
2025
0
                                        resetX = 1;
2026
0
                                    } else {
2027
0
                                        incr_top = 0;
2028
0
                                        resetX = 0;
2029
0
                                    }
2030
0
                                } else {
2031
0
                                    pi[pino].poc.ty0 = tcp->ty0_t;
2032
0
                                    pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2033
0
                                    tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2034
0
                                    incr_top = 0;
2035
0
                                    resetX = 1;
2036
0
                                }
2037
0
                                if (resetX == 1) {
2038
0
                                    tcp->tx0_t = tcp->txS;
2039
0
                                    pi[pino].poc.tx0 = tcp->tx0_t;
2040
0
                                    pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2041
0
                                    tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2042
0
                                }
2043
0
                            } else {
2044
0
                                pi[pino].poc.tx0 = tcp->tx0_t;
2045
0
                                pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2046
0
                                tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2047
0
                                incr_top = 0;
2048
0
                            }
2049
0
                            break;
2050
0
                        }
2051
0
                        break;
2052
0
                    }
2053
0
                }
2054
0
            }
2055
0
        }
2056
847
    }
2057
29.5k
}
2058
2059
void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2060
                    OPJ_UINT32 p_nb_elements)
2061
64.3k
{
2062
64.3k
    OPJ_UINT32 compno, pino;
2063
64.3k
    opj_pi_iterator_t *l_current_pi = p_pi;
2064
64.3k
    if (p_pi) {
2065
64.3k
        if (p_pi->include) {
2066
64.2k
            opj_free(p_pi->include);
2067
64.2k
            p_pi->include = 00;
2068
64.2k
        }
2069
153k
        for (pino = 0; pino < p_nb_elements; ++pino) {
2070
89.3k
            if (l_current_pi->comps) {
2071
89.3k
                opj_pi_comp_t *l_current_component = l_current_pi->comps;
2072
396k
                for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2073
306k
                    if (l_current_component->resolutions) {
2074
306k
                        opj_free(l_current_component->resolutions);
2075
306k
                        l_current_component->resolutions = 00;
2076
306k
                    }
2077
2078
306k
                    ++l_current_component;
2079
306k
                }
2080
89.3k
                opj_free(l_current_pi->comps);
2081
89.3k
                l_current_pi->comps = 0;
2082
89.3k
            }
2083
89.3k
            ++l_current_pi;
2084
89.3k
        }
2085
64.3k
        opj_free(p_pi);
2086
64.3k
    }
2087
64.3k
}
2088
2089
2090
2091
void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2092
                                       opj_cp_t *p_cp,
2093
                                       OPJ_UINT32 p_tile_no)
2094
9.02k
{
2095
    /* encoding parameters to set */
2096
9.02k
    OPJ_UINT32 l_max_res;
2097
9.02k
    OPJ_UINT32 l_max_prec;
2098
9.02k
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
2099
9.02k
    OPJ_UINT32 l_dx_min, l_dy_min;
2100
2101
    /* pointers */
2102
9.02k
    opj_tcp_t *l_tcp = 00;
2103
2104
    /* preconditions */
2105
9.02k
    assert(p_cp != 00);
2106
9.02k
    assert(p_image != 00);
2107
9.02k
    assert(p_tile_no < p_cp->tw * p_cp->th);
2108
2109
9.02k
    l_tcp = &(p_cp->tcps[p_tile_no]);
2110
2111
    /* get encoding parameters */
2112
9.02k
    opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2113
9.02k
                                &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2114
2115
9.02k
    if (l_tcp->POC) {
2116
0
        opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2117
0
                                           l_max_prec, l_max_res, l_dx_min, l_dy_min);
2118
9.02k
    } else {
2119
9.02k
        opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2120
9.02k
                                     l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2121
9.02k
    }
2122
9.02k
}
2123
2124
OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2125
130M
{
2126
130M
    switch (pi->poc.prg) {
2127
67.7M
    case OPJ_LRCP:
2128
67.7M
        return opj_pi_next_lrcp(pi);
2129
17.5M
    case OPJ_RLCP:
2130
17.5M
        return opj_pi_next_rlcp(pi);
2131
9.38M
    case OPJ_RPCL:
2132
9.38M
        return opj_pi_next_rpcl(pi);
2133
13.3M
    case OPJ_PCRL:
2134
13.3M
        return opj_pi_next_pcrl(pi);
2135
22.5M
    case OPJ_CPRL:
2136
22.5M
        return opj_pi_next_cprl(pi);
2137
0
    case OPJ_PROG_UNKNOWN:
2138
0
        return OPJ_FALSE;
2139
130M
    }
2140
2141
8.04k
    return OPJ_FALSE;
2142
130M
}