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