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