Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/openjpeg/src/lib/openjp2/pi.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The copyright in this software is being made available under the 2-clauses
3
 * BSD License, included below. This software may be subject to other third
4
 * party and contributor rights, including patent rights, and no such rights
5
 * are granted under this license.
6
 *
7
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8
 * Copyright (c) 2002-2014, Professor Benoit Macq
9
 * Copyright (c) 2001-2003, David Janssens
10
 * Copyright (c) 2002-2003, Yannick Verschueren
11
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12
 * Copyright (c) 2003-2014, Antonin Descampe
13
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14
 * Copyright (c) 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
1.50M
{
239
1.50M
    opj_pi_comp_t *comp = NULL;
240
1.50M
    opj_pi_resolution_t *res = NULL;
241
1.50M
    OPJ_UINT32 index = 0;
242
243
1.50M
    if (pi->poc.compno0 >= pi->numcomps ||
244
1.50M
            pi->poc.compno1 >= pi->numcomps + 1) {
245
0
        opj_event_msg(pi->manager, EVT_ERROR,
246
0
                      "opj_pi_next_lrcp(): invalid compno0/compno1\n");
247
0
        return OPJ_FALSE;
248
0
    }
249
250
1.50M
    if (!pi->first) {
251
1.49M
        comp = &pi->comps[pi->compno];
252
1.49M
        res = &comp->resolutions[pi->resno];
253
1.49M
        goto LABEL_SKIP;
254
1.49M
    } else {
255
7.57k
        pi->first = 0;
256
7.57k
    }
257
258
44.2k
    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
259
240k
        for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
260
203k
                pi->resno++) {
261
796k
            for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
262
592k
                comp = &pi->comps[pi->compno];
263
592k
                if (pi->resno >= comp->numresolutions) {
264
0
                    continue;
265
0
                }
266
592k
                res = &comp->resolutions[pi->resno];
267
592k
                if (!pi->tp_on) {
268
592k
                    pi->poc.precno1 = res->pw * res->ph;
269
592k
                }
270
2.08M
                for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
271
1.49M
                    index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
272
1.49M
                            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
1.49M
                    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
1.49M
                    if (!pi->include[index]) {
284
1.49M
                        pi->include[index] = 1;
285
1.49M
                        return OPJ_TRUE;
286
1.49M
                    }
287
1.49M
LABEL_SKIP:
288
1.49M
                    ;
289
1.49M
                }
290
592k
            }
291
203k
        }
292
36.9k
    }
293
294
7.23k
    return OPJ_FALSE;
295
7.57k
}
296
297
static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
298
1.28M
{
299
1.28M
    opj_pi_comp_t *comp = NULL;
300
1.28M
    opj_pi_resolution_t *res = NULL;
301
1.28M
    OPJ_UINT32 index = 0;
302
303
1.28M
    if (pi->poc.compno0 >= pi->numcomps ||
304
1.28M
            pi->poc.compno1 >= pi->numcomps + 1) {
305
0
        opj_event_msg(pi->manager, EVT_ERROR,
306
0
                      "opj_pi_next_rlcp(): invalid compno0/compno1\n");
307
0
        return OPJ_FALSE;
308
0
    }
309
310
1.28M
    if (!pi->first) {
311
1.26M
        comp = &pi->comps[pi->compno];
312
1.26M
        res = &comp->resolutions[pi->resno];
313
1.26M
        goto LABEL_SKIP;
314
1.26M
    } else {
315
18.0k
        pi->first = 0;
316
18.0k
    }
317
318
125k
    for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
319
646k
        for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
320
1.80M
            for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
321
1.26M
                comp = &pi->comps[pi->compno];
322
1.26M
                if (pi->resno >= comp->numresolutions) {
323
0
                    continue;
324
0
                }
325
1.26M
                res = &comp->resolutions[pi->resno];
326
1.26M
                if (!pi->tp_on) {
327
1.26M
                    pi->poc.precno1 = res->pw * res->ph;
328
1.26M
                }
329
2.53M
                for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
330
1.26M
                    index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
331
1.26M
                            pi->step_c + pi->precno * pi->step_p;
332
1.26M
                    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
1.26M
                    if (!pi->include[index]) {
337
1.26M
                        pi->include[index] = 1;
338
1.26M
                        return OPJ_TRUE;
339
1.26M
                    }
340
1.26M
LABEL_SKIP:
341
1.26M
                    ;
342
1.26M
                }
343
1.26M
            }
344
538k
        }
345
108k
    }
346
347
17.5k
    return OPJ_FALSE;
348
18.0k
}
349
350
static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
351
0
{
352
0
    opj_pi_comp_t *comp = NULL;
353
0
    opj_pi_resolution_t *res = NULL;
354
0
    OPJ_UINT32 index = 0;
355
356
0
    if (pi->poc.compno0 >= pi->numcomps ||
357
0
            pi->poc.compno1 >= pi->numcomps + 1) {
358
0
        opj_event_msg(pi->manager, EVT_ERROR,
359
0
                      "opj_pi_next_rpcl(): invalid compno0/compno1\n");
360
0
        return OPJ_FALSE;
361
0
    }
362
363
0
    if (!pi->first) {
364
0
        goto LABEL_SKIP;
365
0
    } else {
366
0
        OPJ_UINT32 compno, resno;
367
0
        pi->first = 0;
368
0
        pi->dx = 0;
369
0
        pi->dy = 0;
370
0
        for (compno = 0; compno < pi->numcomps; compno++) {
371
0
            comp = &pi->comps[compno];
372
0
            for (resno = 0; resno < comp->numresolutions; resno++) {
373
0
                OPJ_UINT32 dx, dy;
374
0
                res = &comp->resolutions[resno];
375
0
                if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
376
0
                        comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
377
0
                    dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
378
0
                    pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
379
0
                }
380
0
                if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
381
0
                        comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
382
0
                    dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
383
0
                    pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
384
0
                }
385
0
            }
386
0
        }
387
0
        if (pi->dx == 0 || pi->dy == 0) {
388
0
            return OPJ_FALSE;
389
0
        }
390
0
    }
391
0
    if (!pi->tp_on) {
392
0
        pi->poc.ty0 = pi->ty0;
393
0
        pi->poc.tx0 = pi->tx0;
394
0
        pi->poc.ty1 = pi->ty1;
395
0
        pi->poc.tx1 = pi->tx1;
396
0
    }
397
0
    for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
398
0
        for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
399
0
                pi->y += (pi->dy - (pi->y % pi->dy))) {
400
0
            for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
401
0
                    pi->x += (pi->dx - (pi->x % pi->dx))) {
402
0
                for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
403
0
                    OPJ_UINT32 levelno;
404
0
                    OPJ_UINT32 trx0, try0;
405
0
                    OPJ_UINT32  trx1, try1;
406
0
                    OPJ_UINT32  rpx, rpy;
407
0
                    OPJ_UINT32  prci, prcj;
408
0
                    comp = &pi->comps[pi->compno];
409
0
                    if (pi->resno >= comp->numresolutions) {
410
0
                        continue;
411
0
                    }
412
0
                    res = &comp->resolutions[pi->resno];
413
0
                    levelno = comp->numresolutions - 1 - pi->resno;
414
415
0
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
416
0
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
417
0
                        continue;
418
0
                    }
419
420
0
                    trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
421
0
                                                         ((OPJ_UINT64)comp->dx << levelno));
422
0
                    try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
423
0
                                                         ((OPJ_UINT64)comp->dy << levelno));
424
0
                    trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
425
0
                                                         ((OPJ_UINT64)comp->dx << levelno));
426
0
                    try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
427
0
                                                         ((OPJ_UINT64)comp->dy << levelno));
428
0
                    rpx = res->pdx + levelno;
429
0
                    rpy = res->pdy + levelno;
430
431
0
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
432
0
                            (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
0
                    if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
438
0
                            ((pi->y == pi->ty0) &&
439
0
                             (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
440
0
                        continue;
441
0
                    }
442
0
                    if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
443
0
                            ((pi->x == pi->tx0) &&
444
0
                             (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
445
0
                        continue;
446
0
                    }
447
448
0
                    if ((res->pw == 0) || (res->ph == 0)) {
449
0
                        continue;
450
0
                    }
451
452
0
                    if ((trx0 == trx1) || (try0 == try1)) {
453
0
                        continue;
454
0
                    }
455
456
0
                    prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
457
0
                                                 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
458
0
                           - opj_uint_floordivpow2(trx0, res->pdx);
459
0
                    prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
460
0
                                                 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
461
0
                           - opj_uint_floordivpow2(try0, res->pdy);
462
0
                    pi->precno = prci + prcj * res->pw;
463
0
                    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
464
0
                        index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
465
0
                                pi->step_c + pi->precno * pi->step_p;
466
0
                        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
0
                        if (!pi->include[index]) {
471
0
                            pi->include[index] = 1;
472
0
                            return OPJ_TRUE;
473
0
                        }
474
0
LABEL_SKIP:
475
0
                        ;
476
0
                    }
477
0
                }
478
0
            }
479
0
        }
480
0
    }
481
482
0
    return OPJ_FALSE;
483
0
}
484
485
static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
486
0
{
487
0
    opj_pi_comp_t *comp = NULL;
488
0
    opj_pi_resolution_t *res = NULL;
489
0
    OPJ_UINT32 index = 0;
490
491
0
    if (pi->poc.compno0 >= pi->numcomps ||
492
0
            pi->poc.compno1 >= pi->numcomps + 1) {
493
0
        opj_event_msg(pi->manager, EVT_ERROR,
494
0
                      "opj_pi_next_pcrl(): invalid compno0/compno1\n");
495
0
        return OPJ_FALSE;
496
0
    }
497
498
0
    if (!pi->first) {
499
0
        comp = &pi->comps[pi->compno];
500
0
        goto LABEL_SKIP;
501
0
    } else {
502
0
        OPJ_UINT32 compno, resno;
503
0
        pi->first = 0;
504
0
        pi->dx = 0;
505
0
        pi->dy = 0;
506
0
        for (compno = 0; compno < pi->numcomps; compno++) {
507
0
            comp = &pi->comps[compno];
508
0
            for (resno = 0; resno < comp->numresolutions; resno++) {
509
0
                OPJ_UINT32 dx, dy;
510
0
                res = &comp->resolutions[resno];
511
0
                if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
512
0
                        comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
513
0
                    dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
514
0
                    pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
515
0
                }
516
0
                if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
517
0
                        comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
518
0
                    dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
519
0
                    pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
520
0
                }
521
0
            }
522
0
        }
523
0
        if (pi->dx == 0 || pi->dy == 0) {
524
0
            return OPJ_FALSE;
525
0
        }
526
0
    }
527
0
    if (!pi->tp_on) {
528
0
        pi->poc.ty0 = pi->ty0;
529
0
        pi->poc.tx0 = pi->tx0;
530
0
        pi->poc.ty1 = pi->ty1;
531
0
        pi->poc.tx1 = pi->tx1;
532
0
    }
533
0
    for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
534
0
            pi->y += (pi->dy - (pi->y % pi->dy))) {
535
0
        for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
536
0
                pi->x += (pi->dx - (pi->x % pi->dx))) {
537
0
            for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
538
0
                comp = &pi->comps[pi->compno];
539
0
                for (pi->resno = pi->poc.resno0;
540
0
                        pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
541
0
                    OPJ_UINT32 levelno;
542
0
                    OPJ_UINT32 trx0, try0;
543
0
                    OPJ_UINT32 trx1, try1;
544
0
                    OPJ_UINT32 rpx, rpy;
545
0
                    OPJ_UINT32 prci, prcj;
546
0
                    res = &comp->resolutions[pi->resno];
547
0
                    levelno = comp->numresolutions - 1 - pi->resno;
548
549
0
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
550
0
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
551
0
                        continue;
552
0
                    }
553
554
0
                    trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
555
0
                                                         ((OPJ_UINT64)comp->dx << levelno));
556
0
                    try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
557
0
                                                         ((OPJ_UINT64)comp->dy << levelno));
558
0
                    trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
559
0
                                                         ((OPJ_UINT64)comp->dx << levelno));
560
0
                    try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
561
0
                                                         ((OPJ_UINT64)comp->dy << levelno));
562
0
                    rpx = res->pdx + levelno;
563
0
                    rpy = res->pdy + levelno;
564
565
0
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
566
0
                            (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
0
                    if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
572
0
                            ((pi->y == pi->ty0) &&
573
0
                             (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
574
0
                        continue;
575
0
                    }
576
0
                    if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
577
0
                            ((pi->x == pi->tx0) &&
578
0
                             (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
579
0
                        continue;
580
0
                    }
581
582
0
                    if ((res->pw == 0) || (res->ph == 0)) {
583
0
                        continue;
584
0
                    }
585
586
0
                    if ((trx0 == trx1) || (try0 == try1)) {
587
0
                        continue;
588
0
                    }
589
590
0
                    prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
591
0
                                                 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
592
0
                           - opj_uint_floordivpow2(trx0, res->pdx);
593
0
                    prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
594
0
                                                 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
595
0
                           - opj_uint_floordivpow2(try0, res->pdy);
596
0
                    pi->precno = prci + prcj * res->pw;
597
0
                    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
598
0
                        index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
599
0
                                pi->step_c + pi->precno * pi->step_p;
600
0
                        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
0
                        if (!pi->include[index]) {
605
0
                            pi->include[index] = 1;
606
0
                            return OPJ_TRUE;
607
0
                        }
608
0
LABEL_SKIP:
609
0
                        ;
610
0
                    }
611
0
                }
612
0
            }
613
0
        }
614
0
    }
615
616
0
    return OPJ_FALSE;
617
0
}
618
619
static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
620
0
{
621
0
    opj_pi_comp_t *comp = NULL;
622
0
    opj_pi_resolution_t *res = NULL;
623
0
    OPJ_UINT32 index = 0;
624
625
0
    if (pi->poc.compno0 >= pi->numcomps ||
626
0
            pi->poc.compno1 >= pi->numcomps + 1) {
627
0
        opj_event_msg(pi->manager, EVT_ERROR,
628
0
                      "opj_pi_next_cprl(): invalid compno0/compno1\n");
629
0
        return OPJ_FALSE;
630
0
    }
631
632
0
    if (!pi->first) {
633
0
        comp = &pi->comps[pi->compno];
634
0
        goto LABEL_SKIP;
635
0
    } else {
636
0
        pi->first = 0;
637
0
    }
638
639
0
    for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
640
0
        OPJ_UINT32 resno;
641
0
        comp = &pi->comps[pi->compno];
642
0
        pi->dx = 0;
643
0
        pi->dy = 0;
644
0
        for (resno = 0; resno < comp->numresolutions; resno++) {
645
0
            OPJ_UINT32 dx, dy;
646
0
            res = &comp->resolutions[resno];
647
0
            if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
648
0
                    comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
649
0
                dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
650
0
                pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
651
0
            }
652
0
            if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
653
0
                    comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
654
0
                dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
655
0
                pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
656
0
            }
657
0
        }
658
0
        if (pi->dx == 0 || pi->dy == 0) {
659
0
            return OPJ_FALSE;
660
0
        }
661
0
        if (!pi->tp_on) {
662
0
            pi->poc.ty0 = pi->ty0;
663
0
            pi->poc.tx0 = pi->tx0;
664
0
            pi->poc.ty1 = pi->ty1;
665
0
            pi->poc.tx1 = pi->tx1;
666
0
        }
667
0
        for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
668
0
                pi->y += (pi->dy - (pi->y % pi->dy))) {
669
0
            for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
670
0
                    pi->x += (pi->dx - (pi->x % pi->dx))) {
671
0
                for (pi->resno = pi->poc.resno0;
672
0
                        pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
673
0
                    OPJ_UINT32 levelno;
674
0
                    OPJ_UINT32 trx0, try0;
675
0
                    OPJ_UINT32 trx1, try1;
676
0
                    OPJ_UINT32 rpx, rpy;
677
0
                    OPJ_UINT32 prci, prcj;
678
0
                    res = &comp->resolutions[pi->resno];
679
0
                    levelno = comp->numresolutions - 1 - pi->resno;
680
681
0
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
682
0
                            (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
683
0
                        continue;
684
0
                    }
685
686
0
                    trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
687
0
                                                         ((OPJ_UINT64)comp->dx << levelno));
688
0
                    try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
689
0
                                                         ((OPJ_UINT64)comp->dy << levelno));
690
0
                    trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
691
0
                                                         ((OPJ_UINT64)comp->dx << levelno));
692
0
                    try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
693
0
                                                         ((OPJ_UINT64)comp->dy << levelno));
694
0
                    rpx = res->pdx + levelno;
695
0
                    rpy = res->pdy + levelno;
696
697
0
                    if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
698
0
                            (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
0
                    if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
704
0
                            ((pi->y == pi->ty0) &&
705
0
                             (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
706
0
                        continue;
707
0
                    }
708
0
                    if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
709
0
                            ((pi->x == pi->tx0) &&
710
0
                             (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
711
0
                        continue;
712
0
                    }
713
714
0
                    if ((res->pw == 0) || (res->ph == 0)) {
715
0
                        continue;
716
0
                    }
717
718
0
                    if ((trx0 == trx1) || (try0 == try1)) {
719
0
                        continue;
720
0
                    }
721
722
0
                    prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
723
0
                                                 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
724
0
                           - opj_uint_floordivpow2(trx0, res->pdx);
725
0
                    prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
726
0
                                                 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
727
0
                           - opj_uint_floordivpow2(try0, res->pdy);
728
0
                    pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
729
0
                    for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
730
0
                        index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
731
0
                                pi->step_c + pi->precno * pi->step_p;
732
0
                        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
0
                        if (!pi->include[index]) {
737
0
                            pi->include[index] = 1;
738
0
                            return OPJ_TRUE;
739
0
                        }
740
0
LABEL_SKIP:
741
0
                        ;
742
0
                    }
743
0
                }
744
0
            }
745
0
        }
746
0
    }
747
748
0
    return OPJ_FALSE;
749
0
}
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
0
{
763
    /* loop */
764
0
    OPJ_UINT32  compno, resno;
765
    /* pointers */
766
0
    const opj_tcp_t *l_tcp = 00;
767
0
    const opj_tccp_t * l_tccp = 00;
768
0
    const opj_image_comp_t * l_img_comp = 00;
769
770
    /* position in x and y of tile */
771
0
    OPJ_UINT32 p, q;
772
773
    /* non-corrected (in regard to image offset) tile offset */
774
0
    OPJ_UINT32 l_tx0, l_ty0;
775
776
    /* preconditions */
777
0
    assert(p_cp != 00);
778
0
    assert(p_image != 00);
779
0
    assert(p_tileno < p_cp->tw * p_cp->th);
780
781
    /* initializations */
782
0
    l_tcp = &p_cp->tcps [p_tileno];
783
0
    l_img_comp = p_image->comps;
784
0
    l_tccp = l_tcp->tccps;
785
786
    /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
787
0
    p = p_tileno % p_cp->tw;
788
0
    q = p_tileno / p_cp->tw;
789
790
    /* find extent of tile */
791
0
    l_tx0 = p_cp->tx0 + p *
792
0
            p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
793
0
    *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
794
0
    *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
795
0
    l_ty0 = p_cp->ty0 + q *
796
0
            p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
797
0
    *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
798
0
    *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
0
    *p_max_prec = 0;
802
0
    *p_max_res = 0;
803
804
    /* take the largest value for dx_min and dy_min */
805
0
    *p_dx_min = 0x7fffffff;
806
0
    *p_dy_min  = 0x7fffffff;
807
808
0
    for (compno = 0; compno < p_image->numcomps; ++compno) {
809
        /* arithmetic variables to calculate */
810
0
        OPJ_UINT32 l_level_no;
811
0
        OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
812
0
        OPJ_UINT32 l_px0, l_py0, l_px1, py1;
813
0
        OPJ_UINT32 l_pdx, l_pdy;
814
0
        OPJ_UINT32 l_pw, l_ph;
815
0
        OPJ_UINT32 l_product;
816
0
        OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
817
818
0
        l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
819
0
        l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
820
0
        l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
821
0
        l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
822
823
0
        if (l_tccp->numresolutions > *p_max_res) {
824
0
            *p_max_res = l_tccp->numresolutions;
825
0
        }
826
827
        /* use custom size for precincts */
828
0
        for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
829
0
            OPJ_UINT64 l_dx, l_dy;
830
831
            /* precinct width and height */
832
0
            l_pdx = l_tccp->prcw[resno];
833
0
            l_pdy = l_tccp->prch[resno];
834
835
0
            l_dx = l_img_comp->dx * ((OPJ_UINT64)1u << (l_pdx + l_tccp->numresolutions - 1 -
836
0
                                     resno));
837
0
            l_dy = l_img_comp->dy * ((OPJ_UINT64)1u << (l_pdy + l_tccp->numresolutions - 1 -
838
0
                                     resno));
839
840
            /* take the minimum size for dx for each comp and resolution */
841
0
            if (l_dx <= UINT_MAX) {
842
0
                *p_dx_min = opj_uint_min(*p_dx_min, (OPJ_UINT32)l_dx);
843
0
            }
844
0
            if (l_dy <= UINT_MAX) {
845
0
                *p_dy_min = opj_uint_min(*p_dy_min, (OPJ_UINT32)l_dy);
846
0
            }
847
848
            /* various calculations of extents */
849
0
            l_level_no = l_tccp->numresolutions - 1 - resno;
850
851
0
            l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
852
0
            l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
853
0
            l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
854
0
            l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
855
856
0
            l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
857
0
            l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
858
0
            l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
859
860
0
            py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
861
862
0
            l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
863
0
            l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
864
865
0
            l_product = l_pw * l_ph;
866
867
            /* update precision */
868
0
            if (l_product > *p_max_prec) {
869
0
                *p_max_prec = l_product;
870
0
            }
871
0
        }
872
0
        ++l_img_comp;
873
0
        ++l_tccp;
874
0
    }
875
0
}
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
25.6k
{
891
    /* loop*/
892
25.6k
    OPJ_UINT32 compno, resno;
893
894
    /* pointers*/
895
25.6k
    const opj_tcp_t *tcp = 00;
896
25.6k
    const opj_tccp_t * l_tccp = 00;
897
25.6k
    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
25.6k
    OPJ_UINT32 * lResolutionPtr;
901
902
    /* position in x and y of tile*/
903
25.6k
    OPJ_UINT32 p, q;
904
905
    /* non-corrected (in regard to image offset) tile offset */
906
25.6k
    OPJ_UINT32 l_tx0, l_ty0;
907
908
    /* preconditions in debug*/
909
25.6k
    assert(p_cp != 00);
910
25.6k
    assert(p_image != 00);
911
25.6k
    assert(tileno < p_cp->tw * p_cp->th);
912
913
    /* initializations*/
914
25.6k
    tcp = &p_cp->tcps [tileno];
915
25.6k
    l_tccp = tcp->tccps;
916
25.6k
    l_img_comp = p_image->comps;
917
918
    /* position in x and y of tile*/
919
25.6k
    p = tileno % p_cp->tw;
920
25.6k
    q = tileno / p_cp->tw;
921
922
    /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
923
25.6k
    l_tx0 = p_cp->tx0 + p *
924
25.6k
            p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
925
25.6k
    *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
926
25.6k
    *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
927
25.6k
    l_ty0 = p_cp->ty0 + q *
928
25.6k
            p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
929
25.6k
    *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
930
25.6k
    *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
25.6k
    *p_max_prec = 0;
934
25.6k
    *p_max_res = 0;
935
936
    /* take the largest value for dx_min and dy_min*/
937
25.6k
    *p_dx_min = 0x7fffffff;
938
25.6k
    *p_dy_min = 0x7fffffff;
939
940
82.7k
    for (compno = 0; compno < p_image->numcomps; ++compno) {
941
        /* arithmetic variables to calculate*/
942
57.0k
        OPJ_UINT32 l_level_no;
943
57.0k
        OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
944
57.0k
        OPJ_UINT32 l_px0, l_py0, l_px1, py1;
945
57.0k
        OPJ_UINT32 l_product;
946
57.0k
        OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
947
57.0k
        OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
948
949
57.0k
        lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
950
951
57.0k
        l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
952
57.0k
        l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
953
57.0k
        l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
954
57.0k
        l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
955
956
57.0k
        if (l_tccp->numresolutions > *p_max_res) {
957
25.6k
            *p_max_res = l_tccp->numresolutions;
958
25.6k
        }
959
960
        /* use custom size for precincts*/
961
57.0k
        l_level_no = l_tccp->numresolutions;
962
383k
        for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
963
326k
            OPJ_UINT32 l_dx, l_dy;
964
965
326k
            --l_level_no;
966
967
            /* precinct width and height*/
968
326k
            l_pdx = l_tccp->prcw[resno];
969
326k
            l_pdy = l_tccp->prch[resno];
970
326k
            if (lResolutionPtr) {
971
326k
                *lResolutionPtr++ = l_pdx;
972
326k
                *lResolutionPtr++ = l_pdy;
973
326k
            }
974
326k
            if (l_pdx + l_level_no < 32 &&
975
326k
                    l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
976
326k
                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
326k
                *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
979
326k
            }
980
326k
            if (l_pdy + l_level_no < 32 &&
981
326k
                    l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
982
326k
                l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
983
326k
                *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
984
326k
            }
985
986
            /* various calculations of extents*/
987
326k
            l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
988
326k
            l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
989
326k
            l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
990
326k
            l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
991
326k
            l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
992
326k
            l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
993
326k
            l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
994
326k
            py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
995
326k
            l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
996
326k
            l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
997
326k
            if (lResolutionPtr) {
998
326k
                *lResolutionPtr++ = l_pw;
999
326k
                *lResolutionPtr++ = l_ph;
1000
326k
            }
1001
326k
            l_product = l_pw * l_ph;
1002
1003
            /* update precision*/
1004
326k
            if (l_product > *p_max_prec) {
1005
31.6k
                *p_max_prec = l_product;
1006
31.6k
            }
1007
1008
326k
        }
1009
57.0k
        ++l_tccp;
1010
57.0k
        ++l_img_comp;
1011
57.0k
    }
1012
25.6k
}
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
25.6k
{
1019
    /* loop*/
1020
25.6k
    OPJ_UINT32 pino, compno;
1021
    /* number of poc in the p_pi*/
1022
25.6k
    OPJ_UINT32 l_poc_bound;
1023
1024
    /* pointers to tile coding parameters and components.*/
1025
25.6k
    opj_pi_iterator_t *l_pi = 00;
1026
25.6k
    opj_tcp_t *tcp = 00;
1027
25.6k
    const opj_tccp_t *tccp = 00;
1028
1029
    /* current packet iterator being allocated*/
1030
25.6k
    opj_pi_iterator_t *l_current_pi = 00;
1031
1032
    /* preconditions in debug*/
1033
25.6k
    assert(cp != 00);
1034
25.6k
    assert(image != 00);
1035
25.6k
    assert(tileno < cp->tw * cp->th);
1036
1037
    /* initializations*/
1038
25.6k
    tcp = &cp->tcps[tileno];
1039
25.6k
    l_poc_bound = tcp->numpocs + 1;
1040
1041
    /* memory allocations*/
1042
25.6k
    l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1043
25.6k
                                           sizeof(opj_pi_iterator_t));
1044
25.6k
    if (!l_pi) {
1045
0
        return NULL;
1046
0
    }
1047
1048
25.6k
    l_current_pi = l_pi;
1049
51.3k
    for (pino = 0; pino < l_poc_bound ; ++pino) {
1050
1051
25.6k
        l_current_pi->manager = manager;
1052
1053
25.6k
        l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1054
25.6k
                              sizeof(opj_pi_comp_t));
1055
25.6k
        if (! l_current_pi->comps) {
1056
0
            opj_pi_destroy(l_pi, l_poc_bound);
1057
0
            return NULL;
1058
0
        }
1059
1060
25.6k
        l_current_pi->numcomps = image->numcomps;
1061
1062
82.7k
        for (compno = 0; compno < image->numcomps; ++compno) {
1063
57.0k
            opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1064
1065
57.0k
            tccp = &tcp->tccps[compno];
1066
1067
57.0k
            comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1068
57.0k
                                sizeof(opj_pi_resolution_t));
1069
57.0k
            if (!comp->resolutions) {
1070
0
                opj_pi_destroy(l_pi, l_poc_bound);
1071
0
                return 00;
1072
0
            }
1073
1074
57.0k
            comp->numresolutions = tccp->numresolutions;
1075
57.0k
        }
1076
25.6k
        ++l_current_pi;
1077
25.6k
    }
1078
25.6k
    return l_pi;
1079
25.6k
}
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
0
{
1172
    /* loop*/
1173
0
    OPJ_UINT32 pino;
1174
    /* tile coding parameter*/
1175
0
    opj_tcp_t *l_tcp = 00;
1176
    /* current poc being updated*/
1177
0
    opj_poc_t * l_current_poc = 00;
1178
    /* number of pocs*/
1179
0
    OPJ_UINT32 l_poc_bound;
1180
1181
    /* preconditions in debug*/
1182
0
    assert(p_cp != 00);
1183
0
    assert(p_tileno < p_cp->tw * p_cp->th);
1184
1185
    /* initializations*/
1186
0
    l_tcp = &p_cp->tcps [p_tileno];
1187
1188
    /* number of iterations in the loop */
1189
0
    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
0
    l_current_poc = l_tcp->pocs;
1194
1195
0
    for (pino = 0; pino < l_poc_bound ; ++pino) {
1196
0
        l_current_poc->compS = 0;
1197
0
        l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1198
0
        l_current_poc->resS = 0;
1199
0
        l_current_poc->resE = p_max_res;
1200
0
        l_current_poc->layS = 0;
1201
0
        l_current_poc->layE = l_tcp->numlayers;
1202
0
        l_current_poc->prg  = l_tcp->prg;
1203
0
        l_current_poc->prcS = 0;
1204
0
        l_current_poc->prcE = p_max_prec;
1205
0
        l_current_poc->txS = p_tx0;
1206
0
        l_current_poc->txE = p_tx1;
1207
0
        l_current_poc->tyS = p_ty0;
1208
0
        l_current_poc->tyE = p_ty1;
1209
0
        l_current_poc->dx = p_dx_min;
1210
0
        l_current_poc->dy = p_dy_min;
1211
0
        ++ l_current_poc;
1212
0
    }
1213
0
}
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
0
{
1220
    /* loop*/
1221
0
    OPJ_UINT32 pino;
1222
1223
    /* encoding parameters to set*/
1224
0
    OPJ_UINT32 l_bound;
1225
1226
0
    opj_pi_iterator_t * l_current_pi = 00;
1227
0
    opj_poc_t* l_current_poc = 0;
1228
1229
0
    OPJ_ARG_NOT_USED(p_max_res);
1230
1231
    /* preconditions in debug*/
1232
0
    assert(p_pi != 00);
1233
0
    assert(p_tcp != 00);
1234
1235
    /* initializations*/
1236
0
    l_bound = p_tcp->numpocs + 1;
1237
0
    l_current_pi = p_pi;
1238
0
    l_current_poc = p_tcp->pocs;
1239
1240
0
    for (pino = 0; pino < l_bound; ++pino) {
1241
0
        l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1242
0
        l_current_pi->first = 1;
1243
1244
0
        l_current_pi->poc.resno0 =
1245
0
            l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1246
0
        l_current_pi->poc.compno0 =
1247
0
            l_current_poc->compno0; /* Component Index #0 (Start) */
1248
0
        l_current_pi->poc.layno0 = 0;
1249
0
        l_current_pi->poc.precno0 = 0;
1250
0
        l_current_pi->poc.resno1 =
1251
0
            l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1252
0
        l_current_pi->poc.compno1 =
1253
0
            l_current_poc->compno1; /* Component Index #0 (End) */
1254
0
        l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1255
0
                                                p_tcp->numlayers); /* Layer Index #0 (End) */
1256
0
        l_current_pi->poc.precno1 = p_max_precision;
1257
0
        ++l_current_pi;
1258
0
        ++l_current_poc;
1259
0
    }
1260
0
}
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
25.6k
{
1267
    /* loop*/
1268
25.6k
    OPJ_UINT32 pino;
1269
1270
    /* encoding parameters to set*/
1271
25.6k
    OPJ_UINT32 l_bound;
1272
1273
25.6k
    opj_pi_iterator_t * l_current_pi = 00;
1274
    /* preconditions in debug*/
1275
25.6k
    assert(p_tcp != 00);
1276
25.6k
    assert(p_pi != 00);
1277
1278
    /* initializations*/
1279
25.6k
    l_bound = p_tcp->numpocs + 1;
1280
25.6k
    l_current_pi = p_pi;
1281
1282
51.3k
    for (pino = 0; pino < l_bound; ++pino) {
1283
25.6k
        l_current_pi->poc.prg = p_tcp->prg;
1284
25.6k
        l_current_pi->first = 1;
1285
25.6k
        l_current_pi->poc.resno0 = 0;
1286
25.6k
        l_current_pi->poc.compno0 = 0;
1287
25.6k
        l_current_pi->poc.layno0 = 0;
1288
25.6k
        l_current_pi->poc.precno0 = 0;
1289
25.6k
        l_current_pi->poc.resno1 = p_max_res;
1290
25.6k
        l_current_pi->poc.compno1 = l_current_pi->numcomps;
1291
25.6k
        l_current_pi->poc.layno1 = p_tcp->numlayers;
1292
25.6k
        l_current_pi->poc.precno1 = p_max_precision;
1293
25.6k
        ++l_current_pi;
1294
25.6k
    }
1295
25.6k
}
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
25.6k
{
1393
25.6k
    OPJ_UINT32 numcomps = p_image->numcomps;
1394
1395
    /* loop */
1396
25.6k
    OPJ_UINT32 pino;
1397
25.6k
    OPJ_UINT32 compno, resno;
1398
1399
    /* to store w, h, dx and dy for all components and resolutions */
1400
25.6k
    OPJ_UINT32 * l_tmp_data;
1401
25.6k
    OPJ_UINT32 ** l_tmp_ptr;
1402
1403
    /* encoding parameters to set */
1404
25.6k
    OPJ_UINT32 l_max_res;
1405
25.6k
    OPJ_UINT32 l_max_prec;
1406
25.6k
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1407
25.6k
    OPJ_UINT32 l_dx_min, l_dy_min;
1408
25.6k
    OPJ_UINT32 l_bound;
1409
25.6k
    OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1410
25.6k
    OPJ_UINT32 l_data_stride;
1411
1412
    /* pointers */
1413
25.6k
    opj_pi_iterator_t *l_pi = 00;
1414
25.6k
    opj_tcp_t *l_tcp = 00;
1415
25.6k
    const opj_tccp_t *l_tccp = 00;
1416
25.6k
    opj_pi_comp_t *l_current_comp = 00;
1417
25.6k
    opj_image_comp_t * l_img_comp = 00;
1418
25.6k
    opj_pi_iterator_t * l_current_pi = 00;
1419
25.6k
    OPJ_UINT32 * l_encoding_value_ptr = 00;
1420
1421
    /* preconditions in debug */
1422
25.6k
    assert(p_cp != 00);
1423
25.6k
    assert(p_image != 00);
1424
25.6k
    assert(p_tile_no < p_cp->tw * p_cp->th);
1425
1426
    /* initializations */
1427
25.6k
    l_tcp = &p_cp->tcps[p_tile_no];
1428
25.6k
    l_bound = l_tcp->numpocs + 1;
1429
1430
25.6k
    l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1431
25.6k
    l_tmp_data = (OPJ_UINT32*)opj_malloc(
1432
25.6k
                     l_data_stride * numcomps * sizeof(OPJ_UINT32));
1433
25.6k
    if
1434
25.6k
    (! l_tmp_data) {
1435
0
        return 00;
1436
0
    }
1437
25.6k
    l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1438
25.6k
                    numcomps * sizeof(OPJ_UINT32 *));
1439
25.6k
    if
1440
25.6k
    (! l_tmp_ptr) {
1441
0
        opj_free(l_tmp_data);
1442
0
        return 00;
1443
0
    }
1444
1445
    /* memory allocation for pi */
1446
25.6k
    l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1447
25.6k
    if (!l_pi) {
1448
0
        opj_free(l_tmp_data);
1449
0
        opj_free(l_tmp_ptr);
1450
0
        return 00;
1451
0
    }
1452
1453
25.6k
    l_encoding_value_ptr = l_tmp_data;
1454
    /* update pointer array */
1455
25.6k
    for
1456
82.7k
    (compno = 0; compno < numcomps; ++compno) {
1457
57.0k
        l_tmp_ptr[compno] = l_encoding_value_ptr;
1458
57.0k
        l_encoding_value_ptr += l_data_stride;
1459
57.0k
    }
1460
    /* get encoding parameters */
1461
25.6k
    opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1462
25.6k
                                    &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1463
1464
    /* step calculations */
1465
25.6k
    l_step_p = 1;
1466
25.6k
    l_step_c = l_max_prec * l_step_p;
1467
25.6k
    l_step_r = numcomps * l_step_c;
1468
25.6k
    l_step_l = l_max_res * l_step_r;
1469
1470
    /* set values for first packet iterator */
1471
25.6k
    l_current_pi = l_pi;
1472
1473
    /* memory allocation for include */
1474
    /* prevent an integer overflow issue */
1475
    /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1476
25.6k
    l_current_pi->include = 00;
1477
25.6k
    if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1478
25.6k
        l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1479
25.6k
        l_current_pi->include = (OPJ_INT16*) opj_calloc(
1480
25.6k
                                    l_current_pi->include_size, sizeof(OPJ_INT16));
1481
25.6k
    }
1482
1483
25.6k
    if (!l_current_pi->include) {
1484
0
        opj_free(l_tmp_data);
1485
0
        opj_free(l_tmp_ptr);
1486
0
        opj_pi_destroy(l_pi, l_bound);
1487
0
        return 00;
1488
0
    }
1489
1490
    /* special treatment for the first packet iterator */
1491
25.6k
    l_current_comp = l_current_pi->comps;
1492
25.6k
    l_img_comp = p_image->comps;
1493
25.6k
    l_tccp = l_tcp->tccps;
1494
1495
25.6k
    l_current_pi->tx0 = l_tx0;
1496
25.6k
    l_current_pi->ty0 = l_ty0;
1497
25.6k
    l_current_pi->tx1 = l_tx1;
1498
25.6k
    l_current_pi->ty1 = l_ty1;
1499
1500
    /*l_current_pi->dx = l_img_comp->dx;*/
1501
    /*l_current_pi->dy = l_img_comp->dy;*/
1502
1503
25.6k
    l_current_pi->step_p = l_step_p;
1504
25.6k
    l_current_pi->step_c = l_step_c;
1505
25.6k
    l_current_pi->step_r = l_step_r;
1506
25.6k
    l_current_pi->step_l = l_step_l;
1507
1508
    /* allocation for components and number of components has already been calculated by opj_pi_create */
1509
25.6k
    for
1510
82.7k
    (compno = 0; compno < numcomps; ++compno) {
1511
57.0k
        opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1512
57.0k
        l_encoding_value_ptr = l_tmp_ptr[compno];
1513
1514
57.0k
        l_current_comp->dx = l_img_comp->dx;
1515
57.0k
        l_current_comp->dy = l_img_comp->dy;
1516
        /* resolutions have already been initialized */
1517
57.0k
        for
1518
383k
        (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1519
326k
            l_res->pdx = *(l_encoding_value_ptr++);
1520
326k
            l_res->pdy = *(l_encoding_value_ptr++);
1521
326k
            l_res->pw =  *(l_encoding_value_ptr++);
1522
326k
            l_res->ph =  *(l_encoding_value_ptr++);
1523
326k
            ++l_res;
1524
326k
        }
1525
57.0k
        ++l_current_comp;
1526
57.0k
        ++l_img_comp;
1527
57.0k
        ++l_tccp;
1528
57.0k
    }
1529
25.6k
    ++l_current_pi;
1530
1531
25.6k
    for (pino = 1 ; pino < l_bound ; ++pino) {
1532
0
        l_current_comp = l_current_pi->comps;
1533
0
        l_img_comp = p_image->comps;
1534
0
        l_tccp = l_tcp->tccps;
1535
1536
0
        l_current_pi->tx0 = l_tx0;
1537
0
        l_current_pi->ty0 = l_ty0;
1538
0
        l_current_pi->tx1 = l_tx1;
1539
0
        l_current_pi->ty1 = l_ty1;
1540
        /*l_current_pi->dx = l_dx_min;*/
1541
        /*l_current_pi->dy = l_dy_min;*/
1542
0
        l_current_pi->step_p = l_step_p;
1543
0
        l_current_pi->step_c = l_step_c;
1544
0
        l_current_pi->step_r = l_step_r;
1545
0
        l_current_pi->step_l = l_step_l;
1546
1547
        /* allocation for components and number of components has already been calculated by opj_pi_create */
1548
0
        for
1549
0
        (compno = 0; compno < numcomps; ++compno) {
1550
0
            opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1551
0
            l_encoding_value_ptr = l_tmp_ptr[compno];
1552
1553
0
            l_current_comp->dx = l_img_comp->dx;
1554
0
            l_current_comp->dy = l_img_comp->dy;
1555
            /* resolutions have already been initialized */
1556
0
            for
1557
0
            (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1558
0
                l_res->pdx = *(l_encoding_value_ptr++);
1559
0
                l_res->pdy = *(l_encoding_value_ptr++);
1560
0
                l_res->pw =  *(l_encoding_value_ptr++);
1561
0
                l_res->ph =  *(l_encoding_value_ptr++);
1562
0
                ++l_res;
1563
0
            }
1564
0
            ++l_current_comp;
1565
0
            ++l_img_comp;
1566
0
            ++l_tccp;
1567
0
        }
1568
        /* special treatment*/
1569
0
        l_current_pi->include = (l_current_pi - 1)->include;
1570
0
        l_current_pi->include_size = (l_current_pi - 1)->include_size;
1571
0
        ++l_current_pi;
1572
0
    }
1573
25.6k
    opj_free(l_tmp_data);
1574
25.6k
    l_tmp_data = 00;
1575
25.6k
    opj_free(l_tmp_ptr);
1576
25.6k
    l_tmp_ptr = 00;
1577
25.6k
    if
1578
25.6k
    (l_tcp->POC) {
1579
0
        opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1580
25.6k
    } else {
1581
25.6k
        opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1582
25.6k
    }
1583
25.6k
    return l_pi;
1584
25.6k
}
1585
1586
1587
OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
1588
        const opj_cp_t *p_cp,
1589
        OPJ_UINT32 p_tile_no)
1590
0
{
1591
0
    OPJ_UINT32 l_max_res;
1592
0
    OPJ_UINT32 l_max_prec;
1593
0
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1594
0
    OPJ_UINT32 l_dx_min, l_dy_min;
1595
1596
    /* preconditions in debug*/
1597
0
    assert(p_cp != 00);
1598
0
    assert(p_image != 00);
1599
0
    assert(p_tile_no < p_cp->tw * p_cp->th);
1600
1601
    /* get encoding parameters*/
1602
0
    opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1603
0
                                    &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
1604
1605
0
    return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
1606
0
           l_max_res;
1607
0
}
1608
1609
1610
opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1611
        opj_cp_t *p_cp,
1612
        OPJ_UINT32 p_tile_no,
1613
        J2K_T2_MODE p_t2_mode,
1614
        opj_event_mgr_t* manager)
1615
0
{
1616
0
    OPJ_UINT32 numcomps = p_image->numcomps;
1617
1618
    /* loop*/
1619
0
    OPJ_UINT32 pino;
1620
0
    OPJ_UINT32 compno, resno;
1621
1622
    /* to store w, h, dx and dy for all components and resolutions*/
1623
0
    OPJ_UINT32 * l_tmp_data;
1624
0
    OPJ_UINT32 ** l_tmp_ptr;
1625
1626
    /* encoding parameters to set*/
1627
0
    OPJ_UINT32 l_max_res;
1628
0
    OPJ_UINT32 l_max_prec;
1629
0
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1630
0
    OPJ_UINT32 l_dx_min, l_dy_min;
1631
0
    OPJ_UINT32 l_bound;
1632
0
    OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1633
0
    OPJ_UINT32 l_data_stride;
1634
1635
    /* pointers*/
1636
0
    opj_pi_iterator_t *l_pi = 00;
1637
0
    opj_tcp_t *l_tcp = 00;
1638
0
    const opj_tccp_t *l_tccp = 00;
1639
0
    opj_pi_comp_t *l_current_comp = 00;
1640
0
    opj_image_comp_t * l_img_comp = 00;
1641
0
    opj_pi_iterator_t * l_current_pi = 00;
1642
0
    OPJ_UINT32 * l_encoding_value_ptr = 00;
1643
1644
    /* preconditions in debug*/
1645
0
    assert(p_cp != 00);
1646
0
    assert(p_image != 00);
1647
0
    assert(p_tile_no < p_cp->tw * p_cp->th);
1648
1649
    /* initializations*/
1650
0
    l_tcp = &p_cp->tcps[p_tile_no];
1651
0
    l_bound = l_tcp->numpocs + 1;
1652
1653
0
    l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1654
0
    l_tmp_data = (OPJ_UINT32*)opj_malloc(
1655
0
                     l_data_stride * numcomps * sizeof(OPJ_UINT32));
1656
0
    if (! l_tmp_data) {
1657
0
        return 00;
1658
0
    }
1659
1660
0
    l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1661
0
                    numcomps * sizeof(OPJ_UINT32 *));
1662
0
    if (! l_tmp_ptr) {
1663
0
        opj_free(l_tmp_data);
1664
0
        return 00;
1665
0
    }
1666
1667
    /* memory allocation for pi*/
1668
0
    l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1669
0
    if (!l_pi) {
1670
0
        opj_free(l_tmp_data);
1671
0
        opj_free(l_tmp_ptr);
1672
0
        return 00;
1673
0
    }
1674
1675
0
    l_encoding_value_ptr = l_tmp_data;
1676
    /* update pointer array*/
1677
0
    for (compno = 0; compno < numcomps; ++compno) {
1678
0
        l_tmp_ptr[compno] = l_encoding_value_ptr;
1679
0
        l_encoding_value_ptr += l_data_stride;
1680
0
    }
1681
1682
    /* get encoding parameters*/
1683
0
    opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1684
0
                                    &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1685
1686
    /* step calculations*/
1687
0
    l_step_p = 1;
1688
0
    l_step_c = l_max_prec * l_step_p;
1689
0
    l_step_r = numcomps * l_step_c;
1690
0
    l_step_l = l_max_res * l_step_r;
1691
1692
    /* set values for first packet iterator*/
1693
0
    l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1694
0
    l_current_pi = l_pi;
1695
1696
    /* memory allocation for include*/
1697
0
    l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1698
0
    l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1699
0
                            sizeof(OPJ_INT16));
1700
0
    if (!l_current_pi->include) {
1701
0
        opj_free(l_tmp_data);
1702
0
        opj_free(l_tmp_ptr);
1703
0
        opj_pi_destroy(l_pi, l_bound);
1704
0
        return 00;
1705
0
    }
1706
1707
    /* special treatment for the first packet iterator*/
1708
0
    l_current_comp = l_current_pi->comps;
1709
0
    l_img_comp = p_image->comps;
1710
0
    l_tccp = l_tcp->tccps;
1711
0
    l_current_pi->tx0 = l_tx0;
1712
0
    l_current_pi->ty0 = l_ty0;
1713
0
    l_current_pi->tx1 = l_tx1;
1714
0
    l_current_pi->ty1 = l_ty1;
1715
0
    l_current_pi->dx = l_dx_min;
1716
0
    l_current_pi->dy = l_dy_min;
1717
0
    l_current_pi->step_p = l_step_p;
1718
0
    l_current_pi->step_c = l_step_c;
1719
0
    l_current_pi->step_r = l_step_r;
1720
0
    l_current_pi->step_l = l_step_l;
1721
1722
    /* allocation for components and number of components has already been calculated by opj_pi_create */
1723
0
    for (compno = 0; compno < numcomps; ++compno) {
1724
0
        opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1725
0
        l_encoding_value_ptr = l_tmp_ptr[compno];
1726
1727
0
        l_current_comp->dx = l_img_comp->dx;
1728
0
        l_current_comp->dy = l_img_comp->dy;
1729
1730
        /* resolutions have already been initialized */
1731
0
        for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1732
0
            l_res->pdx = *(l_encoding_value_ptr++);
1733
0
            l_res->pdy = *(l_encoding_value_ptr++);
1734
0
            l_res->pw =  *(l_encoding_value_ptr++);
1735
0
            l_res->ph =  *(l_encoding_value_ptr++);
1736
0
            ++l_res;
1737
0
        }
1738
1739
0
        ++l_current_comp;
1740
0
        ++l_img_comp;
1741
0
        ++l_tccp;
1742
0
    }
1743
0
    ++l_current_pi;
1744
1745
0
    for (pino = 1 ; pino < l_bound ; ++pino) {
1746
0
        l_current_comp = l_current_pi->comps;
1747
0
        l_img_comp = p_image->comps;
1748
0
        l_tccp = l_tcp->tccps;
1749
1750
0
        l_current_pi->tx0 = l_tx0;
1751
0
        l_current_pi->ty0 = l_ty0;
1752
0
        l_current_pi->tx1 = l_tx1;
1753
0
        l_current_pi->ty1 = l_ty1;
1754
0
        l_current_pi->dx = l_dx_min;
1755
0
        l_current_pi->dy = l_dy_min;
1756
0
        l_current_pi->step_p = l_step_p;
1757
0
        l_current_pi->step_c = l_step_c;
1758
0
        l_current_pi->step_r = l_step_r;
1759
0
        l_current_pi->step_l = l_step_l;
1760
1761
        /* allocation for components and number of components has already been calculated by opj_pi_create */
1762
0
        for (compno = 0; compno < numcomps; ++compno) {
1763
0
            opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1764
0
            l_encoding_value_ptr = l_tmp_ptr[compno];
1765
1766
0
            l_current_comp->dx = l_img_comp->dx;
1767
0
            l_current_comp->dy = l_img_comp->dy;
1768
            /* resolutions have already been initialized */
1769
0
            for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1770
0
                l_res->pdx = *(l_encoding_value_ptr++);
1771
0
                l_res->pdy = *(l_encoding_value_ptr++);
1772
0
                l_res->pw =  *(l_encoding_value_ptr++);
1773
0
                l_res->ph =  *(l_encoding_value_ptr++);
1774
0
                ++l_res;
1775
0
            }
1776
0
            ++l_current_comp;
1777
0
            ++l_img_comp;
1778
0
            ++l_tccp;
1779
0
        }
1780
1781
        /* special treatment*/
1782
0
        l_current_pi->include = (l_current_pi - 1)->include;
1783
0
        l_current_pi->include_size = (l_current_pi - 1)->include_size;
1784
0
        ++l_current_pi;
1785
0
    }
1786
1787
0
    opj_free(l_tmp_data);
1788
0
    l_tmp_data = 00;
1789
0
    opj_free(l_tmp_ptr);
1790
0
    l_tmp_ptr = 00;
1791
1792
0
    if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1793
0
        opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1794
0
                                           l_max_prec, l_max_res, l_dx_min, l_dy_min);
1795
0
    } else {
1796
0
        opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1797
0
                                     l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1798
0
    }
1799
1800
0
    return l_pi;
1801
0
}
1802
1803
void opj_pi_create_encode(opj_pi_iterator_t *pi,
1804
                          opj_cp_t *cp,
1805
                          OPJ_UINT32 tileno,
1806
                          OPJ_UINT32 pino,
1807
                          OPJ_UINT32 tpnum,
1808
                          OPJ_INT32 tppos,
1809
                          J2K_T2_MODE t2_mode)
1810
0
{
1811
0
    const OPJ_CHAR *prog;
1812
0
    OPJ_INT32 i;
1813
0
    OPJ_UINT32 incr_top = 1, resetX = 0;
1814
0
    opj_tcp_t *tcps = &cp->tcps[tileno];
1815
0
    opj_poc_t *tcp = &tcps->pocs[pino];
1816
1817
0
    prog = opj_j2k_convert_progression_order(tcp->prg);
1818
1819
0
    pi[pino].first = 1;
1820
0
    pi[pino].poc.prg = tcp->prg;
1821
1822
0
    if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1823
0
            !OPJ_IS_IMF(cp->rsiz) &&
1824
0
            (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
1825
0
        pi[pino].poc.resno0 = tcp->resS;
1826
0
        pi[pino].poc.resno1 = tcp->resE;
1827
0
        pi[pino].poc.compno0 = tcp->compS;
1828
0
        pi[pino].poc.compno1 = tcp->compE;
1829
0
        pi[pino].poc.layno0 = tcp->layS;
1830
0
        pi[pino].poc.layno1 = tcp->layE;
1831
0
        pi[pino].poc.precno0 = tcp->prcS;
1832
0
        pi[pino].poc.precno1 = tcp->prcE;
1833
0
        pi[pino].poc.tx0 = tcp->txS;
1834
0
        pi[pino].poc.ty0 = tcp->tyS;
1835
0
        pi[pino].poc.tx1 = tcp->txE;
1836
0
        pi[pino].poc.ty1 = tcp->tyE;
1837
0
    } else {
1838
0
        for (i = tppos + 1; i < 4; i++) {
1839
0
            switch (prog[i]) {
1840
0
            case 'R':
1841
0
                pi[pino].poc.resno0 = tcp->resS;
1842
0
                pi[pino].poc.resno1 = tcp->resE;
1843
0
                break;
1844
0
            case 'C':
1845
0
                pi[pino].poc.compno0 = tcp->compS;
1846
0
                pi[pino].poc.compno1 = tcp->compE;
1847
0
                break;
1848
0
            case 'L':
1849
0
                pi[pino].poc.layno0 = tcp->layS;
1850
0
                pi[pino].poc.layno1 = tcp->layE;
1851
0
                break;
1852
0
            case 'P':
1853
0
                switch (tcp->prg) {
1854
0
                case OPJ_LRCP:
1855
0
                case OPJ_RLCP:
1856
0
                    pi[pino].poc.precno0 = tcp->prcS;
1857
0
                    pi[pino].poc.precno1 = tcp->prcE;
1858
0
                    break;
1859
0
                default:
1860
0
                    pi[pino].poc.tx0 = tcp->txS;
1861
0
                    pi[pino].poc.ty0 = tcp->tyS;
1862
0
                    pi[pino].poc.tx1 = tcp->txE;
1863
0
                    pi[pino].poc.ty1 = tcp->tyE;
1864
0
                    break;
1865
0
                }
1866
0
                break;
1867
0
            }
1868
0
        }
1869
1870
0
        if (tpnum == 0) {
1871
0
            for (i = tppos; i >= 0; i--) {
1872
0
                switch (prog[i]) {
1873
0
                case 'C':
1874
0
                    tcp->comp_t = tcp->compS;
1875
0
                    pi[pino].poc.compno0 = tcp->comp_t;
1876
0
                    pi[pino].poc.compno1 = tcp->comp_t + 1;
1877
0
                    tcp->comp_t += 1;
1878
0
                    break;
1879
0
                case 'R':
1880
0
                    tcp->res_t = tcp->resS;
1881
0
                    pi[pino].poc.resno0 = tcp->res_t;
1882
0
                    pi[pino].poc.resno1 = tcp->res_t + 1;
1883
0
                    tcp->res_t += 1;
1884
0
                    break;
1885
0
                case 'L':
1886
0
                    tcp->lay_t = tcp->layS;
1887
0
                    pi[pino].poc.layno0 = tcp->lay_t;
1888
0
                    pi[pino].poc.layno1 = tcp->lay_t + 1;
1889
0
                    tcp->lay_t += 1;
1890
0
                    break;
1891
0
                case 'P':
1892
0
                    switch (tcp->prg) {
1893
0
                    case OPJ_LRCP:
1894
0
                    case OPJ_RLCP:
1895
0
                        tcp->prc_t = tcp->prcS;
1896
0
                        pi[pino].poc.precno0 = tcp->prc_t;
1897
0
                        pi[pino].poc.precno1 = tcp->prc_t + 1;
1898
0
                        tcp->prc_t += 1;
1899
0
                        break;
1900
0
                    default:
1901
0
                        tcp->tx0_t = tcp->txS;
1902
0
                        tcp->ty0_t = tcp->tyS;
1903
0
                        pi[pino].poc.tx0 = tcp->tx0_t;
1904
0
                        pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1905
0
                        pi[pino].poc.ty0 = tcp->ty0_t;
1906
0
                        pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1907
0
                        tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1908
0
                        tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1909
0
                        break;
1910
0
                    }
1911
0
                    break;
1912
0
                }
1913
0
            }
1914
0
            incr_top = 1;
1915
0
        } else {
1916
0
            for (i = tppos; i >= 0; i--) {
1917
0
                switch (prog[i]) {
1918
0
                case 'C':
1919
0
                    pi[pino].poc.compno0 = tcp->comp_t - 1;
1920
0
                    pi[pino].poc.compno1 = tcp->comp_t;
1921
0
                    break;
1922
0
                case 'R':
1923
0
                    pi[pino].poc.resno0 = tcp->res_t - 1;
1924
0
                    pi[pino].poc.resno1 = tcp->res_t;
1925
0
                    break;
1926
0
                case 'L':
1927
0
                    pi[pino].poc.layno0 = tcp->lay_t - 1;
1928
0
                    pi[pino].poc.layno1 = tcp->lay_t;
1929
0
                    break;
1930
0
                case 'P':
1931
0
                    switch (tcp->prg) {
1932
0
                    case OPJ_LRCP:
1933
0
                    case OPJ_RLCP:
1934
0
                        pi[pino].poc.precno0 = tcp->prc_t - 1;
1935
0
                        pi[pino].poc.precno1 = tcp->prc_t;
1936
0
                        break;
1937
0
                    default:
1938
0
                        pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1939
0
                        pi[pino].poc.tx1 = tcp->tx0_t ;
1940
0
                        pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1941
0
                        pi[pino].poc.ty1 = tcp->ty0_t ;
1942
0
                        break;
1943
0
                    }
1944
0
                    break;
1945
0
                }
1946
0
                if (incr_top == 1) {
1947
0
                    switch (prog[i]) {
1948
0
                    case 'R':
1949
0
                        if (tcp->res_t == tcp->resE) {
1950
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1951
0
                                tcp->res_t = tcp->resS;
1952
0
                                pi[pino].poc.resno0 = tcp->res_t;
1953
0
                                pi[pino].poc.resno1 = tcp->res_t + 1;
1954
0
                                tcp->res_t += 1;
1955
0
                                incr_top = 1;
1956
0
                            } else {
1957
0
                                incr_top = 0;
1958
0
                            }
1959
0
                        } else {
1960
0
                            pi[pino].poc.resno0 = tcp->res_t;
1961
0
                            pi[pino].poc.resno1 = tcp->res_t + 1;
1962
0
                            tcp->res_t += 1;
1963
0
                            incr_top = 0;
1964
0
                        }
1965
0
                        break;
1966
0
                    case 'C':
1967
0
                        if (tcp->comp_t == tcp->compE) {
1968
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1969
0
                                tcp->comp_t = tcp->compS;
1970
0
                                pi[pino].poc.compno0 = tcp->comp_t;
1971
0
                                pi[pino].poc.compno1 = tcp->comp_t + 1;
1972
0
                                tcp->comp_t += 1;
1973
0
                                incr_top = 1;
1974
0
                            } else {
1975
0
                                incr_top = 0;
1976
0
                            }
1977
0
                        } else {
1978
0
                            pi[pino].poc.compno0 = tcp->comp_t;
1979
0
                            pi[pino].poc.compno1 = tcp->comp_t + 1;
1980
0
                            tcp->comp_t += 1;
1981
0
                            incr_top = 0;
1982
0
                        }
1983
0
                        break;
1984
0
                    case 'L':
1985
0
                        if (tcp->lay_t == tcp->layE) {
1986
0
                            if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1987
0
                                tcp->lay_t = tcp->layS;
1988
0
                                pi[pino].poc.layno0 = tcp->lay_t;
1989
0
                                pi[pino].poc.layno1 = tcp->lay_t + 1;
1990
0
                                tcp->lay_t += 1;
1991
0
                                incr_top = 1;
1992
0
                            } else {
1993
0
                                incr_top = 0;
1994
0
                            }
1995
0
                        } else {
1996
0
                            pi[pino].poc.layno0 = tcp->lay_t;
1997
0
                            pi[pino].poc.layno1 = tcp->lay_t + 1;
1998
0
                            tcp->lay_t += 1;
1999
0
                            incr_top = 0;
2000
0
                        }
2001
0
                        break;
2002
0
                    case 'P':
2003
0
                        switch (tcp->prg) {
2004
0
                        case OPJ_LRCP:
2005
0
                        case OPJ_RLCP:
2006
0
                            if (tcp->prc_t == tcp->prcE) {
2007
0
                                if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2008
0
                                    tcp->prc_t = tcp->prcS;
2009
0
                                    pi[pino].poc.precno0 = tcp->prc_t;
2010
0
                                    pi[pino].poc.precno1 = tcp->prc_t + 1;
2011
0
                                    tcp->prc_t += 1;
2012
0
                                    incr_top = 1;
2013
0
                                } else {
2014
0
                                    incr_top = 0;
2015
0
                                }
2016
0
                            } else {
2017
0
                                pi[pino].poc.precno0 = tcp->prc_t;
2018
0
                                pi[pino].poc.precno1 = tcp->prc_t + 1;
2019
0
                                tcp->prc_t += 1;
2020
0
                                incr_top = 0;
2021
0
                            }
2022
0
                            break;
2023
0
                        default:
2024
0
                            if (tcp->tx0_t >= tcp->txE) {
2025
0
                                if (tcp->ty0_t >= tcp->tyE) {
2026
0
                                    if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2027
0
                                        tcp->ty0_t = tcp->tyS;
2028
0
                                        pi[pino].poc.ty0 = tcp->ty0_t;
2029
0
                                        pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2030
0
                                        tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2031
0
                                        incr_top = 1;
2032
0
                                        resetX = 1;
2033
0
                                    } else {
2034
0
                                        incr_top = 0;
2035
0
                                        resetX = 0;
2036
0
                                    }
2037
0
                                } else {
2038
0
                                    pi[pino].poc.ty0 = tcp->ty0_t;
2039
0
                                    pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2040
0
                                    tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2041
0
                                    incr_top = 0;
2042
0
                                    resetX = 1;
2043
0
                                }
2044
0
                                if (resetX == 1) {
2045
0
                                    tcp->tx0_t = tcp->txS;
2046
0
                                    pi[pino].poc.tx0 = tcp->tx0_t;
2047
0
                                    pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2048
0
                                    tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2049
0
                                }
2050
0
                            } else {
2051
0
                                pi[pino].poc.tx0 = tcp->tx0_t;
2052
0
                                pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2053
0
                                tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2054
0
                                incr_top = 0;
2055
0
                            }
2056
0
                            break;
2057
0
                        }
2058
0
                        break;
2059
0
                    }
2060
0
                }
2061
0
            }
2062
0
        }
2063
0
    }
2064
0
}
2065
2066
void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2067
                    OPJ_UINT32 p_nb_elements)
2068
25.6k
{
2069
25.6k
    OPJ_UINT32 compno, pino;
2070
25.6k
    opj_pi_iterator_t *l_current_pi = p_pi;
2071
25.6k
    if (p_pi) {
2072
25.6k
        if (p_pi->include) {
2073
25.6k
            opj_free(p_pi->include);
2074
25.6k
            p_pi->include = 00;
2075
25.6k
        }
2076
51.3k
        for (pino = 0; pino < p_nb_elements; ++pino) {
2077
25.6k
            if (l_current_pi->comps) {
2078
25.6k
                opj_pi_comp_t *l_current_component = l_current_pi->comps;
2079
82.7k
                for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2080
57.0k
                    if (l_current_component->resolutions) {
2081
57.0k
                        opj_free(l_current_component->resolutions);
2082
57.0k
                        l_current_component->resolutions = 00;
2083
57.0k
                    }
2084
2085
57.0k
                    ++l_current_component;
2086
57.0k
                }
2087
25.6k
                opj_free(l_current_pi->comps);
2088
25.6k
                l_current_pi->comps = 0;
2089
25.6k
            }
2090
25.6k
            ++l_current_pi;
2091
25.6k
        }
2092
25.6k
        opj_free(p_pi);
2093
25.6k
    }
2094
25.6k
}
2095
2096
2097
2098
void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2099
                                       opj_cp_t *p_cp,
2100
                                       OPJ_UINT32 p_tile_no)
2101
0
{
2102
    /* encoding parameters to set */
2103
0
    OPJ_UINT32 l_max_res;
2104
0
    OPJ_UINT32 l_max_prec;
2105
0
    OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
2106
0
    OPJ_UINT32 l_dx_min, l_dy_min;
2107
2108
    /* pointers */
2109
0
    opj_tcp_t *l_tcp = 00;
2110
2111
    /* preconditions */
2112
0
    assert(p_cp != 00);
2113
0
    assert(p_image != 00);
2114
0
    assert(p_tile_no < p_cp->tw * p_cp->th);
2115
2116
0
    l_tcp = &(p_cp->tcps[p_tile_no]);
2117
2118
    /* get encoding parameters */
2119
0
    opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2120
0
                                &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2121
2122
0
    if (l_tcp->POC) {
2123
0
        opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2124
0
                                           l_max_prec, l_max_res, l_dx_min, l_dy_min);
2125
0
    } else {
2126
0
        opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2127
0
                                     l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2128
0
    }
2129
0
}
2130
2131
OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2132
2.78M
{
2133
2.78M
    switch (pi->poc.prg) {
2134
1.50M
    case OPJ_LRCP:
2135
1.50M
        return opj_pi_next_lrcp(pi);
2136
1.28M
    case OPJ_RLCP:
2137
1.28M
        return opj_pi_next_rlcp(pi);
2138
0
    case OPJ_RPCL:
2139
0
        return opj_pi_next_rpcl(pi);
2140
0
    case OPJ_PCRL:
2141
0
        return opj_pi_next_pcrl(pi);
2142
0
    case OPJ_CPRL:
2143
0
        return opj_pi_next_cprl(pi);
2144
0
    case OPJ_PROG_UNKNOWN:
2145
0
        return OPJ_FALSE;
2146
2.78M
    }
2147
2148
0
    return OPJ_FALSE;
2149
2.78M
}