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