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