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