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