remove deprecated v1 style function j2k_read_cod
[openjpeg.git] / libopenjpeg / pi.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "opj_includes.h"
34
35 /** @defgroup PI PI - Implementation of a packet iterator */
36 /*@{*/
37
38 /** @name Local static functions */
39 /*@{*/
40
41 /**
42 Get next packet in layer-resolution-component-precinct order.
43 @param pi packet iterator to modify
44 @return returns false if pi pointed to the last packet or else returns true 
45 */
46 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi);
47 /**
48 Get next packet in resolution-layer-component-precinct order.
49 @param pi packet iterator to modify
50 @return returns false if pi pointed to the last packet or else returns true 
51 */
52 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi);
53 /**
54 Get next packet in resolution-precinct-component-layer order.
55 @param pi packet iterator to modify
56 @return returns false if pi pointed to the last packet or else returns true 
57 */
58 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi);
59 /**
60 Get next packet in precinct-component-resolution-layer order.
61 @param pi packet iterator to modify
62 @return returns false if pi pointed to the last packet or else returns true 
63 */
64 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi);
65 /**
66 Get next packet in component-precinct-resolution-layer order.
67 @param pi packet iterator to modify
68 @return returns false if pi pointed to the last packet or else returns true 
69 */
70 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi);
71
72 /**
73  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
74  *
75  * @param       p_cp            the coding parameters to modify
76  * @param       p_tileno        the tile index being concerned.
77  * @param       p_tx0           X0 parameter for the tile
78  * @param       p_tx1           X1 parameter for the tile
79  * @param       p_ty0           Y0 parameter for the tile
80  * @param       p_ty1           Y1 parameter for the tile
81  * @param       p_max_prec      the maximum precision for all the bands of the tile
82  * @param       p_max_res       the maximum number of resolutions for all the poc inside the tile.
83  * @param       dx_min          the minimum dx of all the components of all the resolutions for the tile.
84  * @param       dy_min          the minimum dy of all the components of all the resolutions for the tile.
85  */
86 void pi_update_encode_poc_and_final (opj_cp_v2_t *p_cp,
87                                                                          OPJ_UINT32 p_tileno,
88                                                                          OPJ_INT32 p_tx0,
89                                                                          OPJ_INT32 p_tx1,
90                                                                          OPJ_INT32 p_ty0,
91                                                                          OPJ_INT32 p_ty1,
92                                                                          OPJ_UINT32 p_max_prec,
93                                                                          OPJ_UINT32 p_max_res,
94                                      OPJ_UINT32 p_dx_min,
95                                                                          OPJ_UINT32 p_dy_min);
96
97 /**
98  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
99  *
100  * @param       p_cp            the coding parameters to modify
101  * @param       p_tileno        the tile index being concerned.
102  * @param       p_tx0           X0 parameter for the tile
103  * @param       p_tx1           X1 parameter for the tile
104  * @param       p_ty0           Y0 parameter for the tile
105  * @param       p_ty1           Y1 parameter for the tile
106  * @param       p_max_prec      the maximum precision for all the bands of the tile
107  * @param       p_max_res       the maximum number of resolutions for all the poc inside the tile.
108  * @param       dx_min          the minimum dx of all the components of all the resolutions for the tile.
109  * @param       dy_min          the minimum dy of all the components of all the resolutions for the tile.
110  */
111 void pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
112                                                                 OPJ_UINT32 p_num_comps,
113                                                                 OPJ_UINT32 p_tileno,
114                                                                 OPJ_INT32 p_tx0,
115                                                                 OPJ_INT32 p_tx1,
116                                                                 OPJ_INT32 p_ty0,
117                                                                 OPJ_INT32 p_ty1,
118                                                                 OPJ_UINT32 p_max_prec,
119                                                                 OPJ_UINT32 p_max_res,
120                                 OPJ_UINT32 p_dx_min,
121                                                                 OPJ_UINT32 p_dy_min);
122
123 /**
124  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
125  *
126  * @param       p_image                 the image being encoded.
127  * @param       p_cp                    the coding parameters.
128  * @param       tileno                  the tile index of the tile being encoded.
129  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
130  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
131  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
132  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
133  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
134  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
135  * @param       dx_min                  pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
136  * @param       dy_min                  pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
137  */
138 void get_encoding_parameters(   const opj_image_t *p_image,
139                                                                 const opj_cp_v2_t *p_cp,
140                                                                 OPJ_UINT32  tileno,
141                                                                 OPJ_INT32  * p_tx0,
142                                                                 OPJ_INT32 * p_tx1,
143                                                                 OPJ_INT32 * p_ty0,
144                                                                 OPJ_INT32 * p_ty1,
145                                                                 OPJ_UINT32 * p_dx_min,
146                                                                 OPJ_UINT32 * p_dy_min,
147                                                                 OPJ_UINT32 * p_max_prec,
148                                                                 OPJ_UINT32 * p_max_res );
149
150 /**
151  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
152  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
153  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
154  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
155  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
156  *
157  * @param       p_image                 the image being encoded.
158  * @param       p_cp                    the coding parameters.
159  * @param       tileno                  the tile index of the tile being encoded.
160  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
161  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
162  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
163  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
164  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
165  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
166  * @param       p_dx_min                pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
167  * @param       p_dy_min                pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
168  * @param       p_resolutions   pointer to an area corresponding to the one described above.
169  */
170 void get_all_encoding_parameters(
171                                                                 const opj_image_t *p_image,
172                                                                 const opj_cp_v2_t *p_cp,
173                                                                 OPJ_UINT32 tileno,
174                                                                 OPJ_INT32 * p_tx0,
175                                                                 OPJ_INT32 * p_tx1,
176                                                                 OPJ_INT32 * p_ty0,
177                                                                 OPJ_INT32 * p_ty1,
178                                                                 OPJ_UINT32 * p_dx_min,
179                                                                 OPJ_UINT32 * p_dy_min,
180                                                                 OPJ_UINT32 * p_max_prec,
181                                                                 OPJ_UINT32 * p_max_res,
182                                                                 OPJ_UINT32 ** p_resolutions
183                                                         );
184
185
186 /**
187  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
188  * No other data is set. The include section of the packet  iterator is not allocated.
189  *
190  * @param       image           the image used to initialize the packet iterator (only the number of components is relevant).
191  * @param       cp              the coding parameters.
192  * @param       tileno          the index of the tile from which creating the packet iterator.
193  */
194 opj_pi_iterator_t * pi_create(  const opj_image_t *image,
195                                                                 const opj_cp_v2_t *cp,
196                                                                 OPJ_UINT32 tileno );
197
198 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
199 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
200
201
202 OPJ_INT32 pi_check_next_level(  OPJ_INT32 pos,
203                                                                 opj_cp_v2_t *cp,
204                                                                 OPJ_UINT32 tileno,
205                                                                 OPJ_UINT32 pino,
206                                                                 const OPJ_CHAR *prog);
207
208 /*@}*/
209
210 /*@}*/
211
212 /* 
213 ==========================================================
214    local functions
215 ==========================================================
216 */
217
218 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
219         opj_pi_comp_t *comp = NULL;
220         opj_pi_resolution_t *res = NULL;
221         long index = 0;
222         
223         if (!pi->first) {
224                 comp = &pi->comps[pi->compno];
225                 res = &comp->resolutions[pi->resno];
226                 goto LABEL_SKIP;
227         } else {
228                 pi->first = 0;
229         }
230
231         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
232                 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
233                 pi->resno++) {
234                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
235                                 comp = &pi->comps[pi->compno];
236                                 if (pi->resno >= comp->numresolutions) {
237                                         continue;
238                                 }
239                                 res = &comp->resolutions[pi->resno];
240                                 if (!pi->tp_on){
241                                         pi->poc.precno1 = res->pw * res->ph;
242                                 }
243                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
244                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
245                                         if (!pi->include[index]) {
246                                                 pi->include[index] = 1;
247                                                 return OPJ_TRUE;
248                                         }
249 LABEL_SKIP:;
250                                 }
251                         }
252                 }
253         }
254         
255         return OPJ_FALSE;
256 }
257
258 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
259         opj_pi_comp_t *comp = NULL;
260         opj_pi_resolution_t *res = NULL;
261         long index = 0;
262
263         if (!pi->first) {
264                 comp = &pi->comps[pi->compno];
265                 res = &comp->resolutions[pi->resno];
266                 goto LABEL_SKIP;
267         } else {
268                 pi->first = 0;
269         }
270
271         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
272                 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
273                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
274                                 comp = &pi->comps[pi->compno];
275                                 if (pi->resno >= comp->numresolutions) {
276                                         continue;
277                                 }
278                                 res = &comp->resolutions[pi->resno];
279                                 if(!pi->tp_on){
280                                         pi->poc.precno1 = res->pw * res->ph;
281                                 }
282                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
283                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
284                                         if (!pi->include[index]) {
285                                                 pi->include[index] = 1;
286                                                 return OPJ_TRUE;
287                                         }
288 LABEL_SKIP:;
289                                 }
290                         }
291                 }
292         }
293         
294         return OPJ_FALSE;
295 }
296
297 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
298         opj_pi_comp_t *comp = NULL;
299         opj_pi_resolution_t *res = NULL;
300         long index = 0;
301
302         if (!pi->first) {
303                 goto LABEL_SKIP;
304         } else {
305                 int compno, resno;
306                 pi->first = 0;
307                 pi->dx = 0;
308                 pi->dy = 0;
309                 for (compno = 0; compno < pi->numcomps; compno++) {
310                         comp = &pi->comps[compno];
311                         for (resno = 0; resno < comp->numresolutions; resno++) {
312                                 int dx, dy;
313                                 res = &comp->resolutions[resno];
314                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
315                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
316                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
317                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
318                         }
319                 }
320         }
321 if (!pi->tp_on){
322                         pi->poc.ty0 = pi->ty0;
323                         pi->poc.tx0 = pi->tx0;
324                         pi->poc.ty1 = pi->ty1;
325                         pi->poc.tx1 = pi->tx1;
326                 }
327         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
328                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
329                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
330                                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
331                                         int levelno;
332                                         int trx0, try0;
333                                         int trx1, try1;
334                                         int rpx, rpy;
335                                         int prci, prcj;
336                                         comp = &pi->comps[pi->compno];
337                                         if (pi->resno >= comp->numresolutions) {
338                                                 continue;
339                                         }
340                                         res = &comp->resolutions[pi->resno];
341                                         levelno = comp->numresolutions - 1 - pi->resno;
342                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
343                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
344                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
345                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
346                                         rpx = res->pdx + levelno;
347                                         rpy = res->pdy + levelno;
348                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
349                                                 continue;       
350                                         }
351                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
352                                                 continue; 
353                                         }
354                                         
355                                         if ((res->pw==0)||(res->ph==0)) continue;
356                                         
357                                         if ((trx0==trx1)||(try0==try1)) continue;
358                                         
359                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
360                                                  - int_floordivpow2(trx0, res->pdx);
361                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
362                                                  - int_floordivpow2(try0, res->pdy);
363                                         pi->precno = prci + prcj * res->pw;
364                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
365                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
366                                                 if (!pi->include[index]) {
367                                                         pi->include[index] = 1;
368                                                         return OPJ_TRUE;
369                                                 }
370 LABEL_SKIP:;
371                                         }
372                                 }
373                         }
374                 }
375         }
376         
377         return OPJ_FALSE;
378 }
379
380 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
381         opj_pi_comp_t *comp = NULL;
382         opj_pi_resolution_t *res = NULL;
383         long index = 0;
384
385         if (!pi->first) {
386                 comp = &pi->comps[pi->compno];
387                 goto LABEL_SKIP;
388         } else {
389                 int compno, resno;
390                 pi->first = 0;
391                 pi->dx = 0;
392                 pi->dy = 0;
393                 for (compno = 0; compno < pi->numcomps; compno++) {
394                         comp = &pi->comps[compno];
395                         for (resno = 0; resno < comp->numresolutions; resno++) {
396                                 int dx, dy;
397                                 res = &comp->resolutions[resno];
398                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
399                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
400                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
401                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
402                         }
403                 }
404         }
405         if (!pi->tp_on){
406                         pi->poc.ty0 = pi->ty0;
407                         pi->poc.tx0 = pi->tx0;
408                         pi->poc.ty1 = pi->ty1;
409                         pi->poc.tx1 = pi->tx1;
410                 }
411         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
412                 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
413                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
414                                 comp = &pi->comps[pi->compno];
415                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
416                                         int levelno;
417                                         int trx0, try0;
418                                         int trx1, try1;
419                                         int rpx, rpy;
420                                         int prci, prcj;
421                                         res = &comp->resolutions[pi->resno];
422                                         levelno = comp->numresolutions - 1 - pi->resno;
423                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
424                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
425                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
426                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
427                                         rpx = res->pdx + levelno;
428                                         rpy = res->pdy + levelno;
429                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
430                                                 continue;       
431                                         }
432                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
433                                                 continue; 
434                                         }
435                                         
436                                         if ((res->pw==0)||(res->ph==0)) continue;
437                                         
438                                         if ((trx0==trx1)||(try0==try1)) continue;
439                                         
440                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
441                                                  - int_floordivpow2(trx0, res->pdx);
442                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
443                                                  - int_floordivpow2(try0, res->pdy);
444                                         pi->precno = prci + prcj * res->pw;
445                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
446                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
447                                                 if (!pi->include[index]) {
448                                                         pi->include[index] = 1;
449                                                         return OPJ_TRUE;
450                                                 }       
451 LABEL_SKIP:;
452                                         }
453                                 }
454                         }
455                 }
456         }
457         
458         return OPJ_FALSE;
459 }
460
461 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
462         opj_pi_comp_t *comp = NULL;
463         opj_pi_resolution_t *res = NULL;
464         long index = 0;
465
466         if (!pi->first) {
467                 comp = &pi->comps[pi->compno];
468                 goto LABEL_SKIP;
469         } else {
470                 pi->first = 0;
471         }
472
473         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
474                 int resno;
475                 comp = &pi->comps[pi->compno];
476                 pi->dx = 0;
477                 pi->dy = 0;
478                 for (resno = 0; resno < comp->numresolutions; resno++) {
479                         int dx, dy;
480                         res = &comp->resolutions[resno];
481                         dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
482                         dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
483                         pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
484                         pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
485                 }
486                 if (!pi->tp_on){
487                         pi->poc.ty0 = pi->ty0;
488                         pi->poc.tx0 = pi->tx0;
489                         pi->poc.ty1 = pi->ty1;
490                         pi->poc.tx1 = pi->tx1;
491                 }
492                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
493                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
494                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
495                                         int levelno;
496                                         int trx0, try0;
497                                         int trx1, try1;
498                                         int rpx, rpy;
499                                         int prci, prcj;
500                                         res = &comp->resolutions[pi->resno];
501                                         levelno = comp->numresolutions - 1 - pi->resno;
502                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
503                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
504                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
505                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
506                                         rpx = res->pdx + levelno;
507                                         rpy = res->pdy + levelno;
508                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
509                                                 continue;       
510                                         }
511                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
512                                                 continue; 
513                                         }
514                                         
515                                         if ((res->pw==0)||(res->ph==0)) continue;
516                                         
517                                         if ((trx0==trx1)||(try0==try1)) continue;
518                                         
519                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
520                                                  - int_floordivpow2(trx0, res->pdx);
521                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
522                                                  - int_floordivpow2(try0, res->pdy);
523                                         pi->precno = prci + prcj * res->pw;
524                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
525                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
526                                                 if (!pi->include[index]) {
527                                                         pi->include[index] = 1;
528                                                         return OPJ_TRUE;
529                                                 }
530 LABEL_SKIP:;
531                                         }
532                                 }
533                         }
534                 }
535         }
536         
537         return OPJ_FALSE;
538 }
539
540 /* 
541 ==========================================================
542    Packet iterator interface
543 ==========================================================
544 */
545
546 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
547         int p, q;
548         int compno, resno, pino;
549         opj_pi_iterator_t *pi = NULL;
550         opj_tcp_t *tcp = NULL;
551         opj_tccp_t *tccp = NULL;
552
553         tcp = &cp->tcps[tileno];
554
555         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
556         if(!pi) {
557                 /* TODO: throw an error */
558                 return NULL;
559         }
560
561         for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
562                 int maxres = 0;
563                 int maxprec = 0;
564                 p = tileno % cp->tw;
565                 q = tileno / cp->tw;
566
567                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
568                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
569                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
570                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
571                 pi[pino].numcomps = image->numcomps;
572
573                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
574                 if(!pi[pino].comps) {
575                         /* TODO: throw an error */
576                         pi_destroy(pi, cp, tileno);
577                         return NULL;
578                 }
579                 
580                 for (compno = 0; compno < pi->numcomps; compno++) {
581                         int tcx0, tcy0, tcx1, tcy1;
582                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
583                         tccp = &tcp->tccps[compno];
584                         comp->dx = image->comps[compno].dx;
585                         comp->dy = image->comps[compno].dy;
586                         comp->numresolutions = tccp->numresolutions;
587
588                         comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
589                         if(!comp->resolutions) {
590                                 /* TODO: throw an error */
591                                 pi_destroy(pi, cp, tileno);
592                                 return NULL;
593                         }
594
595                         tcx0 = int_ceildiv(pi->tx0, comp->dx);
596                         tcy0 = int_ceildiv(pi->ty0, comp->dy);
597                         tcx1 = int_ceildiv(pi->tx1, comp->dx);
598                         tcy1 = int_ceildiv(pi->ty1, comp->dy);
599                         if (comp->numresolutions > maxres) {
600                                 maxres = comp->numresolutions;
601                         }
602
603                         for (resno = 0; resno < comp->numresolutions; resno++) {
604                                 int levelno;
605                                 int rx0, ry0, rx1, ry1;
606                                 int px0, py0, px1, py1;
607                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
608                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
609                                         res->pdx = tccp->prcw[resno];
610                                         res->pdy = tccp->prch[resno];
611                                 } else {
612                                         res->pdx = 15;
613                                         res->pdy = 15;
614                                 }
615                                 levelno = comp->numresolutions - 1 - resno;
616                                 rx0 = int_ceildivpow2(tcx0, levelno);
617                                 ry0 = int_ceildivpow2(tcy0, levelno);
618                                 rx1 = int_ceildivpow2(tcx1, levelno);
619                                 ry1 = int_ceildivpow2(tcy1, levelno);
620                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
621                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
622                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
623                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
624                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
625                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
626                                 
627                                 if (res->pw*res->ph > maxprec) {
628                                         maxprec = res->pw*res->ph;
629                                 }
630                                 
631                         }
632                 }
633                 
634                 tccp = &tcp->tccps[0];
635                 pi[pino].step_p = 1;
636                 pi[pino].step_c = maxprec * pi[pino].step_p;
637                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
638                 pi[pino].step_l = maxres * pi[pino].step_r;
639                 
640                 if (pino == 0) {
641                         pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
642                         if(!pi[pino].include) {
643                                 /* TODO: throw an error */
644                                 pi_destroy(pi, cp, tileno);
645                                 return NULL;
646                         }
647                 }
648                 else {
649                         pi[pino].include = pi[pino - 1].include;
650                 }
651                 
652                 if (tcp->POC == 0) {
653                         pi[pino].first = 1;
654                         pi[pino].poc.resno0 = 0;
655                         pi[pino].poc.compno0 = 0;
656                         pi[pino].poc.layno1 = tcp->numlayers;
657                         pi[pino].poc.resno1 = maxres;
658                         pi[pino].poc.compno1 = image->numcomps;
659                         pi[pino].poc.prg = tcp->prg;
660                 } else {
661                         pi[pino].first = 1;
662                         pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
663                         pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
664                         pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
665                         pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
666                         pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
667                         pi[pino].poc.prg = tcp->pocs[pino].prg;
668                 }
669                 pi[pino].poc.layno0  = 0;
670                 pi[pino].poc.precno0 = 0; 
671                 pi[pino].poc.precno1 = maxprec;
672                         
673         }
674         
675         return pi;
676 }
677
678
679 opj_pi_iterator_t *pi_create_decode_v2( opj_image_t *p_image,
680                                                                                 opj_cp_v2_t *p_cp,
681                                                                                 OPJ_UINT32 p_tile_no
682                                                                                 )
683 {
684         // loop
685         OPJ_UINT32 pino;
686         OPJ_UINT32 compno, resno;
687
688         // to store w, h, dx and dy fro all components and resolutions
689         OPJ_UINT32 * l_tmp_data;
690         OPJ_UINT32 ** l_tmp_ptr;
691
692         // encoding prameters to set
693         OPJ_UINT32 l_max_res;
694         OPJ_UINT32 l_max_prec;
695         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
696         OPJ_UINT32 l_dx_min,l_dy_min;
697         OPJ_UINT32 l_bound;
698         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
699         OPJ_UINT32 l_data_stride;
700
701         // pointers
702         opj_pi_iterator_t *l_pi = 00;
703         opj_tcp_v2_t *l_tcp = 00;
704         const opj_tccp_t *l_tccp = 00;
705         opj_pi_comp_t *l_current_comp = 00;
706         opj_image_comp_t * l_img_comp = 00;
707         opj_pi_iterator_t * l_current_pi = 00;
708         OPJ_UINT32 * l_encoding_value_ptr = 00;
709
710         // preconditions in debug
711         assert(p_cp != 00);
712         assert(p_image != 00);
713         assert(p_tile_no < p_cp->tw * p_cp->th);
714
715         // initializations
716         l_tcp = &p_cp->tcps[p_tile_no];
717         l_bound = l_tcp->numpocs+1;
718
719         l_data_stride = 4 * J2K_MAXRLVLS;
720         l_tmp_data = (OPJ_UINT32*)opj_malloc(
721                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
722         if
723                 (! l_tmp_data)
724         {
725                 return 00;
726         }
727         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
728                 p_image->numcomps * sizeof(OPJ_UINT32 *));
729         if
730                 (! l_tmp_ptr)
731         {
732                 opj_free(l_tmp_data);
733                 return 00;
734         }
735
736         // memory allocation for pi
737         l_pi = pi_create(p_image, p_cp, p_tile_no);
738         if (!l_pi) {
739                 opj_free(l_tmp_data);
740                 opj_free(l_tmp_ptr);
741                 return 00;
742         }
743
744         l_encoding_value_ptr = l_tmp_data;
745         // update pointer array
746         for
747                 (compno = 0; compno < p_image->numcomps; ++compno)
748         {
749                 l_tmp_ptr[compno] = l_encoding_value_ptr;
750                 l_encoding_value_ptr += l_data_stride;
751         }
752         // get encoding parameters
753         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);
754
755         // step calculations
756         l_step_p = 1;
757         l_step_c = l_max_prec * l_step_p;
758         l_step_r = p_image->numcomps * l_step_c;
759         l_step_l = l_max_res * l_step_r;
760
761         // set values for first packet iterator
762         l_current_pi = l_pi;
763
764         // memory allocation for include
765         l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
766         if
767                 (!l_current_pi->include)
768         {
769                 opj_free(l_tmp_data);
770                 opj_free(l_tmp_ptr);
771                 pi_destroy_v2(l_pi, l_bound);
772                 return 00;
773         }
774         memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
775
776         // special treatment for the first packet iterator
777         l_current_comp = l_current_pi->comps;
778         l_img_comp = p_image->comps;
779         l_tccp = l_tcp->tccps;
780
781         l_current_pi->tx0 = l_tx0;
782         l_current_pi->ty0 = l_ty0;
783         l_current_pi->tx1 = l_tx1;
784         l_current_pi->ty1 = l_ty1;
785
786         //l_current_pi->dx = l_img_comp->dx;
787         //l_current_pi->dy = l_img_comp->dy;
788
789         l_current_pi->step_p = l_step_p;
790         l_current_pi->step_c = l_step_c;
791         l_current_pi->step_r = l_step_r;
792         l_current_pi->step_l = l_step_l;
793
794         /* allocation for components and number of components has already been calculated by pi_create */
795         for
796                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
797         {
798                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
799                 l_encoding_value_ptr = l_tmp_ptr[compno];
800
801                 l_current_comp->dx = l_img_comp->dx;
802                 l_current_comp->dy = l_img_comp->dy;
803                 /* resolutions have already been initialized */
804                 for
805                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
806                 {
807                         l_res->pdx = *(l_encoding_value_ptr++);
808                         l_res->pdy = *(l_encoding_value_ptr++);
809                         l_res->pw =  *(l_encoding_value_ptr++);
810                         l_res->ph =  *(l_encoding_value_ptr++);
811                         ++l_res;
812                 }
813                 ++l_current_comp;
814                 ++l_img_comp;
815                 ++l_tccp;
816         }
817         ++l_current_pi;
818
819         for
820                 (pino = 1 ; pino<l_bound ; ++pino )
821         {
822                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
823                 opj_image_comp_t * l_img_comp = p_image->comps;
824                 l_tccp = l_tcp->tccps;
825
826                 l_current_pi->tx0 = l_tx0;
827                 l_current_pi->ty0 = l_ty0;
828                 l_current_pi->tx1 = l_tx1;
829                 l_current_pi->ty1 = l_ty1;
830                 //l_current_pi->dx = l_dx_min;
831                 //l_current_pi->dy = l_dy_min;
832                 l_current_pi->step_p = l_step_p;
833                 l_current_pi->step_c = l_step_c;
834                 l_current_pi->step_r = l_step_r;
835                 l_current_pi->step_l = l_step_l;
836
837                 /* allocation for components and number of components has already been calculated by pi_create */
838                 for
839                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
840                 {
841                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
842                         l_encoding_value_ptr = l_tmp_ptr[compno];
843
844                         l_current_comp->dx = l_img_comp->dx;
845                         l_current_comp->dy = l_img_comp->dy;
846                         /* resolutions have already been initialized */
847                         for
848                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
849                         {
850                                 l_res->pdx = *(l_encoding_value_ptr++);
851                                 l_res->pdy = *(l_encoding_value_ptr++);
852                                 l_res->pw =  *(l_encoding_value_ptr++);
853                                 l_res->ph =  *(l_encoding_value_ptr++);
854                                 ++l_res;
855                         }
856                         ++l_current_comp;
857                         ++l_img_comp;
858                         ++l_tccp;
859                 }
860                 // special treatment
861                 l_current_pi->include = (l_current_pi-1)->include;
862                 ++l_current_pi;
863         }
864         opj_free(l_tmp_data);
865         l_tmp_data = 00;
866         opj_free(l_tmp_ptr);
867         l_tmp_ptr = 00;
868         if
869                 (l_tcp->POC)
870         {
871                 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
872         }
873         else
874         {
875                 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
876         }
877         return l_pi;
878 }
879
880 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ 
881         int p, q, pino;
882         int compno, resno;
883         int maxres = 0;
884         int maxprec = 0;
885         opj_pi_iterator_t *pi = NULL;
886         opj_tcp_t *tcp = NULL;
887         opj_tccp_t *tccp = NULL;
888         
889         tcp = &cp->tcps[tileno];
890
891         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
892         if(!pi) {       return NULL;}
893         pi->tp_on = cp->tp_on;
894
895         for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
896                 p = tileno % cp->tw;
897                 q = tileno / cp->tw;
898
899                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
900                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
901                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
902                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
903                 pi[pino].numcomps = image->numcomps;
904                 
905                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
906                 if(!pi[pino].comps) {
907                         pi_destroy(pi, cp, tileno);
908                         return NULL;
909                 }
910                 
911                 for (compno = 0; compno < pi[pino].numcomps; compno++) {
912                         int tcx0, tcy0, tcx1, tcy1;
913                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
914                         tccp = &tcp->tccps[compno];
915                         comp->dx = image->comps[compno].dx;
916                         comp->dy = image->comps[compno].dy;
917                         comp->numresolutions = tccp->numresolutions;
918
919                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
920                         if(!comp->resolutions) {
921                                 pi_destroy(pi, cp, tileno);
922                                 return NULL;
923                         }
924
925                         tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
926                         tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
927                         tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
928                         tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
929                         if (comp->numresolutions > maxres) {
930                                 maxres = comp->numresolutions;
931                         }
932
933                         for (resno = 0; resno < comp->numresolutions; resno++) {
934                                 int levelno;
935                                 int rx0, ry0, rx1, ry1;
936                                 int px0, py0, px1, py1;
937                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
938                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
939                                         res->pdx = tccp->prcw[resno];
940                                         res->pdy = tccp->prch[resno];
941                                 } else {
942                                         res->pdx = 15;
943                                         res->pdy = 15;
944                                 }
945                                 levelno = comp->numresolutions - 1 - resno;
946                                 rx0 = int_ceildivpow2(tcx0, levelno);
947                                 ry0 = int_ceildivpow2(tcy0, levelno);
948                                 rx1 = int_ceildivpow2(tcx1, levelno);
949                                 ry1 = int_ceildivpow2(tcy1, levelno);
950                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
951                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
952                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
953                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
954                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
955                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
956
957                                 if (res->pw*res->ph > maxprec) {
958                                         maxprec = res->pw * res->ph;
959                                 }
960                         }
961                 }
962                 
963                 tccp = &tcp->tccps[0];
964                 pi[pino].step_p = 1;
965                 pi[pino].step_c = maxprec * pi[pino].step_p;
966                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
967                 pi[pino].step_l = maxres * pi[pino].step_r;
968                 
969                 for (compno = 0; compno < pi->numcomps; compno++) {
970                         opj_pi_comp_t *comp = &pi->comps[compno];
971                         for (resno = 0; resno < comp->numresolutions; resno++) {
972                                 int dx, dy;
973                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
974                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
975                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
976                                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
977                                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
978                         }
979                 }
980
981                 if (pino == 0) {
982                         pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
983                         if(!pi[pino].include) {
984                                 pi_destroy(pi, cp, tileno);
985                                 return NULL;
986                         }
987                 }
988                 else {
989                         pi[pino].include = pi[pino - 1].include;
990                 }
991                 
992                 /* Generation of boundaries for each prog flag*/
993                         if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
994                                 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
995                                 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
996                                 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
997                                 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
998                                 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
999                                 tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
1000                                 if (pino > 0)
1001                                         tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
1002                         }else {
1003                                 tcp->pocs[pino].compS= 0;
1004                                 tcp->pocs[pino].compE= image->numcomps;
1005                                 tcp->pocs[pino].resS = 0;
1006                                 tcp->pocs[pino].resE = maxres;
1007                                 tcp->pocs[pino].layS = 0;
1008                                 tcp->pocs[pino].layE = tcp->numlayers;
1009                                 tcp->pocs[pino].prg  = tcp->prg;
1010                         }
1011                         tcp->pocs[pino].prcS = 0;
1012                         tcp->pocs[pino].prcE = maxprec;;
1013                         tcp->pocs[pino].txS = pi[pino].tx0;
1014                         tcp->pocs[pino].txE = pi[pino].tx1;
1015                         tcp->pocs[pino].tyS = pi[pino].ty0;
1016                         tcp->pocs[pino].tyE = pi[pino].ty1;
1017                         tcp->pocs[pino].dx = pi[pino].dx;
1018                         tcp->pocs[pino].dy = pi[pino].dy;
1019                 }
1020                         return pi;
1021         }
1022
1023 /**
1024  * Creates a packet iterator for encoding.
1025  *
1026  * @param       p_image         the image being encoded.
1027  * @param       p_cp            the coding parameters.
1028  * @param       p_tile_no       index of the tile being encoded.
1029  * @param       p_t2_mode       the type of pass for generating the packet iterator
1030  * @return      a list of packet iterator that points to the first packet of the tile (not true).
1031 */
1032 opj_pi_iterator_t *pi_initialise_encode_v2(
1033                                                                                 const opj_image_t *p_image,
1034                                                                                 opj_cp_v2_t *p_cp,
1035                                                                                 OPJ_UINT32 p_tile_no,
1036                                                                                 J2K_T2_MODE p_t2_mode
1037                                                                                 )
1038 {
1039         // loop
1040         OPJ_UINT32 pino;
1041         OPJ_UINT32 compno, resno;
1042
1043         // to store w, h, dx and dy fro all components and resolutions
1044         OPJ_UINT32 * l_tmp_data;
1045         OPJ_UINT32 ** l_tmp_ptr;
1046
1047         // encoding prameters to set
1048         OPJ_UINT32 l_max_res;
1049         OPJ_UINT32 l_max_prec;
1050         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1051         OPJ_UINT32 l_dx_min,l_dy_min;
1052         OPJ_UINT32 l_bound;
1053         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1054         OPJ_UINT32 l_data_stride;
1055
1056         // pointers
1057         opj_pi_iterator_t *l_pi = 00;
1058         opj_tcp_v2_t *l_tcp = 00;
1059         const opj_tccp_t *l_tccp = 00;
1060         opj_pi_comp_t *l_current_comp = 00;
1061         opj_image_comp_t * l_img_comp = 00;
1062         opj_pi_iterator_t * l_current_pi = 00;
1063         OPJ_UINT32 * l_encoding_value_ptr = 00;
1064
1065         // preconditions in debug
1066         assert(p_cp != 00);
1067         assert(p_image != 00);
1068         assert(p_tile_no < p_cp->tw * p_cp->th);
1069
1070         // initializations
1071         l_tcp = &p_cp->tcps[p_tile_no];
1072         l_bound = l_tcp->numpocs+1;
1073
1074         l_data_stride = 4 * J2K_MAXRLVLS;
1075         l_tmp_data = (OPJ_UINT32*)opj_malloc(
1076                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1077         if (! l_tmp_data) {
1078                 return 00;
1079         }
1080
1081         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1082                 p_image->numcomps * sizeof(OPJ_UINT32 *));
1083         if (! l_tmp_ptr) {
1084                 opj_free(l_tmp_data);
1085                 return 00;
1086         }
1087
1088         // memory allocation for pi
1089         l_pi = pi_create(p_image,p_cp,p_tile_no);
1090         if (!l_pi) {
1091                 opj_free(l_tmp_data);
1092                 opj_free(l_tmp_ptr);
1093                 return 00;
1094         }
1095
1096         l_encoding_value_ptr = l_tmp_data;
1097         // update pointer array
1098         for (compno = 0; compno < p_image->numcomps; ++compno) {
1099                 l_tmp_ptr[compno] = l_encoding_value_ptr;
1100                 l_encoding_value_ptr += l_data_stride;
1101         }
1102
1103         // get encoding parameters
1104         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);
1105
1106         // step calculations
1107         l_step_p = 1;
1108         l_step_c = l_max_prec * l_step_p;
1109         l_step_r = p_image->numcomps * l_step_c;
1110         l_step_l = l_max_res * l_step_r;
1111
1112         // set values for first packet iterator
1113         l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1114         l_current_pi = l_pi;
1115
1116         // memory allocation for include
1117         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1118         if (!l_current_pi->include) {
1119                 opj_free(l_tmp_data);
1120                 opj_free(l_tmp_ptr);
1121                 pi_destroy_v2(l_pi, l_bound);
1122                 return 00;
1123         }
1124         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
1125
1126         // special treatment for the first packet iterator
1127         l_current_comp = l_current_pi->comps;
1128         l_img_comp = p_image->comps;
1129         l_tccp = l_tcp->tccps;
1130         l_current_pi->tx0 = l_tx0;
1131         l_current_pi->ty0 = l_ty0;
1132         l_current_pi->tx1 = l_tx1;
1133         l_current_pi->ty1 = l_ty1;
1134         l_current_pi->dx = l_dx_min;
1135         l_current_pi->dy = l_dy_min;
1136         l_current_pi->step_p = l_step_p;
1137         l_current_pi->step_c = l_step_c;
1138         l_current_pi->step_r = l_step_r;
1139         l_current_pi->step_l = l_step_l;
1140
1141         /* allocation for components and number of components has already been calculated by pi_create */
1142         for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1143                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1144                 l_encoding_value_ptr = l_tmp_ptr[compno];
1145
1146                 l_current_comp->dx = l_img_comp->dx;
1147                 l_current_comp->dy = l_img_comp->dy;
1148
1149                 /* resolutions have already been initialized */
1150                 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1151                         l_res->pdx = *(l_encoding_value_ptr++);
1152                         l_res->pdy = *(l_encoding_value_ptr++);
1153                         l_res->pw =  *(l_encoding_value_ptr++);
1154                         l_res->ph =  *(l_encoding_value_ptr++);
1155                         ++l_res;
1156                 }
1157
1158                 ++l_current_comp;
1159                 ++l_img_comp;
1160                 ++l_tccp;
1161         }
1162         ++l_current_pi;
1163
1164         for (pino = 1 ; pino<l_bound ; ++pino ) {
1165                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
1166                 opj_image_comp_t * l_img_comp = p_image->comps;
1167                 l_tccp = l_tcp->tccps;
1168
1169                 l_current_pi->tx0 = l_tx0;
1170                 l_current_pi->ty0 = l_ty0;
1171                 l_current_pi->tx1 = l_tx1;
1172                 l_current_pi->ty1 = l_ty1;
1173                 l_current_pi->dx = l_dx_min;
1174                 l_current_pi->dy = l_dy_min;
1175                 l_current_pi->step_p = l_step_p;
1176                 l_current_pi->step_c = l_step_c;
1177                 l_current_pi->step_r = l_step_r;
1178                 l_current_pi->step_l = l_step_l;
1179
1180                 /* allocation for components and number of components has already been calculated by pi_create */
1181                 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1182                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1183                         l_encoding_value_ptr = l_tmp_ptr[compno];
1184
1185                         l_current_comp->dx = l_img_comp->dx;
1186                         l_current_comp->dy = l_img_comp->dy;
1187                         /* resolutions have already been initialized */
1188                         for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1189                                 l_res->pdx = *(l_encoding_value_ptr++);
1190                                 l_res->pdy = *(l_encoding_value_ptr++);
1191                                 l_res->pw =  *(l_encoding_value_ptr++);
1192                                 l_res->ph =  *(l_encoding_value_ptr++);
1193                                 ++l_res;
1194                         }
1195                         ++l_current_comp;
1196                         ++l_img_comp;
1197                         ++l_tccp;
1198                 }
1199
1200                 // special treatment
1201                 l_current_pi->include = (l_current_pi-1)->include;
1202                 ++l_current_pi;
1203         }
1204
1205         opj_free(l_tmp_data);
1206         l_tmp_data = 00;
1207         opj_free(l_tmp_ptr);
1208         l_tmp_ptr = 00;
1209
1210         if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
1211                 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);
1212         }
1213         else {
1214                 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);
1215         }
1216
1217         return l_pi;
1218 }
1219
1220
1221 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
1222         int compno, pino;
1223         opj_tcp_t *tcp = &cp->tcps[tileno];
1224         if(pi) {
1225                 for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
1226                         if(pi[pino].comps) {
1227                                 for (compno = 0; compno < pi->numcomps; compno++) {
1228                                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
1229                                         if(comp->resolutions) {
1230                                                 opj_free(comp->resolutions);
1231                                         }
1232                                 }
1233                                 opj_free(pi[pino].comps);
1234                         }
1235                 }
1236                 if(pi->include) {
1237                         opj_free(pi->include);
1238                 }
1239                 opj_free(pi);
1240         }
1241 }
1242
1243 opj_bool pi_next(opj_pi_iterator_t * pi) {
1244         switch (pi->poc.prg) {
1245                 case LRCP:
1246                         return pi_next_lrcp(pi);
1247                 case RLCP:
1248                         return pi_next_rlcp(pi);
1249                 case RPCL:
1250                         return pi_next_rpcl(pi);
1251                 case PCRL:
1252                         return pi_next_pcrl(pi);
1253                 case CPRL:
1254                         return pi_next_cprl(pi);
1255                 case PROG_UNKNOWN:
1256                         return OPJ_FALSE;
1257         }
1258         
1259         return OPJ_FALSE;
1260 }
1261
1262 opj_bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp){
1263         char prog[4];
1264         int i;
1265         int incr_top=1,resetX=0;
1266         opj_tcp_t *tcps =&cp->tcps[tileno];
1267         opj_poc_t *tcp= &tcps->pocs[pino];
1268
1269         pi[pino].first = 1;
1270         pi[pino].poc.prg = tcp->prg;
1271
1272         switch(tcp->prg){
1273                 case CPRL: strncpy(prog, "CPRL",4);
1274                         break;
1275                 case LRCP: strncpy(prog, "LRCP",4);
1276                         break;
1277                 case PCRL: strncpy(prog, "PCRL",4);
1278                         break;
1279                 case RLCP: strncpy(prog, "RLCP",4);
1280                         break;
1281                 case RPCL: strncpy(prog, "RPCL",4);
1282                         break;
1283                 case PROG_UNKNOWN: 
1284                         return OPJ_TRUE;
1285         }
1286
1287         if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1288                 pi[pino].poc.resno0 = tcp->resS;
1289                 pi[pino].poc.resno1 = tcp->resE;
1290                 pi[pino].poc.compno0 = tcp->compS;
1291                 pi[pino].poc.compno1 = tcp->compE;
1292                 pi[pino].poc.layno0 = tcp->layS;
1293                 pi[pino].poc.layno1 = tcp->layE;
1294                 pi[pino].poc.precno0 = tcp->prcS;
1295                 pi[pino].poc.precno1 = tcp->prcE;
1296                 pi[pino].poc.tx0 = tcp->txS;
1297                 pi[pino].poc.ty0 = tcp->tyS;
1298                 pi[pino].poc.tx1 = tcp->txE;
1299                 pi[pino].poc.ty1 = tcp->tyE;
1300         }else {
1301                 if( tpnum < cur_totnum_tp){
1302                         for(i=3;i>=0;i--){
1303                                 switch(prog[i]){
1304                                 case 'C':
1305                                         if (i > tppos){
1306                                                 pi[pino].poc.compno0 = tcp->compS;
1307                                                 pi[pino].poc.compno1 = tcp->compE;
1308                                         }else{
1309                                                 if (tpnum == 0){
1310                                                         tcp->comp_t = tcp->compS;
1311                                                         pi[pino].poc.compno0 = tcp->comp_t;
1312                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1313                                                         tcp->comp_t+=1;
1314                                                 }else{
1315                                                         if (incr_top == 1){
1316                                                                 if(tcp->comp_t ==tcp->compE){
1317                                                                         tcp->comp_t = tcp->compS;
1318                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1319                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1320                                                                         tcp->comp_t+=1;
1321                                                                         incr_top=1;
1322                                                                 }else{
1323                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1324                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1325                                                                         tcp->comp_t+=1;
1326                                                                         incr_top=0;
1327                                                                 }
1328                                                         }else{
1329                                                                 pi[pino].poc.compno0 = tcp->comp_t-1;
1330                                                                 pi[pino].poc.compno1 = tcp->comp_t;
1331                                                         }
1332                                                 }
1333                                         }
1334                                         break;
1335
1336                                 case 'R':
1337                                         if (i > tppos){
1338                                                 pi[pino].poc.resno0 = tcp->resS;
1339                                                 pi[pino].poc.resno1 = tcp->resE;
1340                                         }else{
1341                                                 if (tpnum == 0){
1342                                                         tcp->res_t = tcp->resS;
1343                                                         pi[pino].poc.resno0 = tcp->res_t;
1344                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1345                                                         tcp->res_t+=1;
1346                                                 }else{
1347                                                         if (incr_top == 1){
1348                                                                 if(tcp->res_t==tcp->resE){
1349                                                                         tcp->res_t = tcp->resS;
1350                                                                         pi[pino].poc.resno0 = tcp->res_t;
1351                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1352                                                                         tcp->res_t+=1;
1353                                                                         incr_top=1;
1354                                                                 }else{
1355                                                                         pi[pino].poc.resno0 = tcp->res_t;
1356                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1357                                                                         tcp->res_t+=1;
1358                                                                         incr_top=0;
1359                                                                 }
1360                                                         }else{
1361                                                                 pi[pino].poc.resno0 = tcp->res_t - 1;
1362                                                                 pi[pino].poc.resno1 = tcp->res_t;
1363                                                         }
1364                                                 }
1365                                         }
1366                                         break;
1367
1368                                 case 'L':
1369                                         if (i > tppos){
1370                                                 pi[pino].poc.layno0 = tcp->layS;
1371                                                 pi[pino].poc.layno1 = tcp->layE;
1372                                         }else{
1373                                                 if (tpnum == 0){
1374                                                         tcp->lay_t = tcp->layS;
1375                                                         pi[pino].poc.layno0 = tcp->lay_t;
1376                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1377                                                         tcp->lay_t+=1;
1378                                                 }else{
1379                                                         if (incr_top == 1){
1380                                                                 if(tcp->lay_t == tcp->layE){
1381                                                                         tcp->lay_t = tcp->layS;
1382                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1383                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1384                                                                         tcp->lay_t+=1;
1385                                                                         incr_top=1;
1386                                                                 }else{
1387                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1388                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1389                                                                         tcp->lay_t+=1;
1390                                                                         incr_top=0;
1391                                                                 }
1392                                                         }else{
1393                                                                 pi[pino].poc.layno0 = tcp->lay_t - 1;
1394                                                                 pi[pino].poc.layno1 = tcp->lay_t;
1395                                                         }
1396                                                 }
1397                                         }
1398                                         break;
1399
1400                                 case 'P':
1401                                         switch(tcp->prg){
1402                                                 case LRCP:
1403                                                 case RLCP:
1404                                                         if (i > tppos){
1405                                                                 pi[pino].poc.precno0 = tcp->prcS;
1406                                                                 pi[pino].poc.precno1 = tcp->prcE;
1407                                                         }else{
1408                                                                 if (tpnum == 0){
1409                                                                         tcp->prc_t = tcp->prcS;
1410                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1411                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1412                                                                         tcp->prc_t+=1; 
1413                                                                 }else{
1414                                                                         if (incr_top == 1){
1415                                                                                 if(tcp->prc_t == tcp->prcE){
1416                                                                                         tcp->prc_t = tcp->prcS;
1417                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1418                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1419                                                                                         tcp->prc_t+=1;
1420                                                                                         incr_top=1;
1421                                                                                 }else{
1422                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1423                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1424                                                                                         tcp->prc_t+=1;
1425                                                                                         incr_top=0;
1426                                                                                 }
1427                                                                         }else{
1428                                                                                 pi[pino].poc.precno0 = tcp->prc_t - 1;
1429                                                                                 pi[pino].poc.precno1 = tcp->prc_t;
1430                                                                         }
1431                                                                 }
1432                                                         }
1433                                                 break;
1434                                                 default:
1435                                                         if (i > tppos){
1436                                                                 pi[pino].poc.tx0 = tcp->txS;
1437                                                                 pi[pino].poc.ty0 = tcp->tyS;
1438                                                                 pi[pino].poc.tx1 = tcp->txE;
1439                                                                 pi[pino].poc.ty1 = tcp->tyE;
1440                                                         }else{
1441                                                                 if (tpnum == 0){
1442                                                                         tcp->tx0_t = tcp->txS;
1443                                                                         tcp->ty0_t = tcp->tyS;
1444                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1445                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1446                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1447                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1448                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1449                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1450                                                                 }else{
1451                                                                         if (incr_top == 1){
1452                                                                                 if(tcp->tx0_t >= tcp->txE){
1453                                                                                         if(tcp->ty0_t >= tcp->tyE){
1454                                                                                                 tcp->ty0_t = tcp->tyS;
1455                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1456                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1457                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1458                                                                                                 incr_top=1;resetX=1;
1459                                                                                         }else{
1460                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1461                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1462                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1463                                                                                                 incr_top=0;resetX=1;
1464                                                                                         }
1465                                                                                         if(resetX==1){
1466                                                                                                 tcp->tx0_t = tcp->txS;
1467                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1468                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1469                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1470                                                                                         }
1471                                                                                 }else{
1472                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1473                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1474                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1475                                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1476                                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1477                                                                                         incr_top=0;
1478                                                                                 }
1479                                                                         }else{
1480                                                                                 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1481                                                                                 pi[pino].poc.tx1 = tcp->tx0_t ;
1482                                                                                 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1483                                                                                 pi[pino].poc.ty1 = tcp->ty0_t ;
1484                                                                         }
1485                                                                 }
1486                                                         }
1487                                                 break;
1488                                                 }
1489                                                 break;
1490                                 }               
1491                         } 
1492                 }
1493         }       
1494         return OPJ_FALSE;
1495 }
1496
1497 /**
1498  * Updates the encoding parameters of the codec.
1499  *
1500  * @param       p_image         the image being encoded.
1501  * @param       p_cp            the coding parameters.
1502  * @param       p_tile_no       index of the tile being encoded.
1503 */
1504 void pi_update_encoding_parameters(     const opj_image_t *p_image,
1505                                                                         opj_cp_v2_t *p_cp,
1506                                                                         OPJ_UINT32 p_tile_no )
1507 {
1508         /* encoding parameters to set */
1509         OPJ_UINT32 l_max_res;
1510         OPJ_UINT32 l_max_prec;
1511         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1512         OPJ_UINT32 l_dx_min,l_dy_min;
1513
1514         /* pointers */
1515         opj_tcp_v2_t *l_tcp = 00;
1516
1517         /* preconditions */
1518         assert(p_cp != 00);
1519         assert(p_image != 00);
1520         assert(p_tile_no < p_cp->tw * p_cp->th);
1521
1522         l_tcp = &(p_cp->tcps[p_tile_no]);
1523
1524         /* get encoding parameters */
1525         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);
1526
1527         if (l_tcp->POC) {
1528                 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);
1529         }
1530         else {
1531                 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);
1532         }
1533
1534 }
1535
1536 /**
1537  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1538  *
1539  * @param       p_image                 the image being encoded.
1540  * @param       p_cp                    the coding parameters.
1541  * @param       p_tileno                        the tile index of the tile being encoded.
1542  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
1543  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
1544  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
1545  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
1546  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
1547  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1548  * @param       dx_min                  pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1549  * @param       dy_min                  pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1550  */
1551 void get_encoding_parameters(   const opj_image_t *p_image,
1552                                                                 const opj_cp_v2_t *p_cp,
1553                                                                 OPJ_UINT32 p_tileno,
1554                                                                 OPJ_INT32 * p_tx0,
1555                                                                 OPJ_INT32  * p_tx1,
1556                                                                 OPJ_INT32  * p_ty0,
1557                                                                 OPJ_INT32  * p_ty1,
1558                                                                 OPJ_UINT32 * p_dx_min,
1559                                                                 OPJ_UINT32 * p_dy_min,
1560                                                                 OPJ_UINT32 * p_max_prec,
1561                                                                 OPJ_UINT32 * p_max_res )
1562 {
1563         /* loop */
1564         OPJ_UINT32  compno, resno;
1565         /* pointers */
1566         const opj_tcp_v2_t *l_tcp = 00;
1567         const opj_tccp_t * l_tccp = 00;
1568         const opj_image_comp_t * l_img_comp = 00;
1569
1570         /* position in x and y of tile */
1571         OPJ_UINT32 p, q;
1572
1573         /* preconditions */
1574         assert(p_cp != 00);
1575         assert(p_image != 00);
1576         assert(p_tileno < p_cp->tw * p_cp->th);
1577
1578         /* initializations */
1579         l_tcp = &p_cp->tcps [p_tileno];
1580         l_img_comp = p_image->comps;
1581         l_tccp = l_tcp->tccps;
1582
1583         /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
1584         p = p_tileno % p_cp->tw;
1585         q = p_tileno / p_cp->tw;
1586
1587         /* find extent of tile */
1588         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1589         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1590         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1591         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1592
1593         /* max precision is 0 (can only grow) */
1594         *p_max_prec = 0;
1595         *p_max_res = 0;
1596
1597         /* take the largest value for dx_min and dy_min */
1598         *p_dx_min = 0x7fffffff;
1599         *p_dy_min  = 0x7fffffff;
1600
1601         for (compno = 0; compno < p_image->numcomps; ++compno) {
1602                 /* arithmetic variables to calculate */
1603                 OPJ_UINT32 l_level_no;
1604                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1605                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1606                 OPJ_UINT32 l_pdx, l_pdy;
1607                 OPJ_UINT32 l_pw, l_ph;
1608                 OPJ_UINT32 l_product;
1609                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1610
1611                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1612                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1613                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1614                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1615
1616                 if (l_tccp->numresolutions > *p_max_res) {
1617                         *p_max_res = l_tccp->numresolutions;
1618                 }
1619
1620                 /* use custom size for precincts */
1621                 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1622                         OPJ_UINT32 l_dx, l_dy;
1623
1624                         /* precinct width and height */
1625                         l_pdx = l_tccp->prcw[resno];
1626                         l_pdy = l_tccp->prch[resno];
1627
1628                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
1629                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
1630
1631                         /* take the minimum size for dx for each comp and resolution */
1632                         *p_dx_min = uint_min(*p_dx_min, l_dx);
1633                         *p_dy_min = uint_min(*p_dy_min, l_dy);
1634
1635                         /* various calculations of extents */
1636                         l_level_no = l_tccp->numresolutions - 1 - resno;
1637
1638                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1639                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1640                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1641                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1642
1643                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1644                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1645                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1646
1647                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1648
1649                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1650                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1651
1652                         l_product = l_pw * l_ph;
1653
1654                         /* update precision */
1655                         if (l_product > *p_max_prec) {
1656                                 *p_max_prec = l_product;
1657                         }
1658                 }
1659                 ++l_img_comp;
1660                 ++l_tccp;
1661         }
1662 }
1663
1664 /**
1665  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1666  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1667  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1668  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1669  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1670  *
1671  * @param       p_image                 the image being encoded.
1672  * @param       p_cp                    the coding parameters.
1673  * @param       tileno                  the tile index of the tile being encoded.
1674  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
1675  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
1676  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
1677  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
1678  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
1679  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1680  * @param       p_dx_min                pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1681  * @param       p_dy_min                pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1682  * @param       p_resolutions   pointer to an area corresponding to the one described above.
1683  */
1684 void get_all_encoding_parameters(
1685                                                                 const opj_image_t *p_image,
1686                                                                 const opj_cp_v2_t *p_cp,
1687                                                                 OPJ_UINT32 tileno,
1688                                                                 OPJ_INT32 * p_tx0,
1689                                                                 OPJ_INT32 * p_tx1,
1690                                                                 OPJ_INT32 * p_ty0,
1691                                                                 OPJ_INT32 * p_ty1,
1692                                                                 OPJ_UINT32 * p_dx_min,
1693                                                                 OPJ_UINT32 * p_dy_min,
1694                                                                 OPJ_UINT32 * p_max_prec,
1695                                                                 OPJ_UINT32 * p_max_res,
1696                                                                 OPJ_UINT32 ** p_resolutions
1697                                                         )
1698 {
1699         // loop
1700         OPJ_UINT32 compno, resno;
1701
1702         // pointers
1703         const opj_tcp_v2_t *tcp = 00;
1704         const opj_tccp_t * l_tccp = 00;
1705         const opj_image_comp_t * l_img_comp = 00;
1706
1707         // to store l_dx, l_dy, w and h for each resolution and component.
1708         OPJ_UINT32 * lResolutionPtr;
1709
1710         // position in x and y of tile
1711         OPJ_UINT32 p, q;
1712
1713         // preconditions in debug
1714         assert(p_cp != 00);
1715         assert(p_image != 00);
1716         assert(tileno < p_cp->tw * p_cp->th);
1717
1718         // initializations
1719         tcp = &p_cp->tcps [tileno];
1720         l_tccp = tcp->tccps;
1721         l_img_comp = p_image->comps;
1722
1723         // position in x and y of tile
1724
1725         p = tileno % p_cp->tw;
1726         q = tileno / p_cp->tw;
1727
1728         /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1729         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1730         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1731         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1732         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1733
1734         // max precision and resolution is 0 (can only grow)
1735         *p_max_prec = 0;
1736         *p_max_res = 0;
1737
1738         // take the largest value for dx_min and dy_min
1739         *p_dx_min = 0x7fffffff;
1740         *p_dy_min  = 0x7fffffff;
1741
1742         for
1743                 (compno = 0; compno < p_image->numcomps; ++compno)
1744         {
1745                 // aritmetic variables to calculate
1746                 OPJ_UINT32 l_level_no;
1747                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1748                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1749                 OPJ_UINT32 l_product;
1750                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1751                 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1752
1753                 lResolutionPtr = p_resolutions[compno];
1754
1755                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1756                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1757                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1758                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1759                 if
1760                         (l_tccp->numresolutions > *p_max_res)
1761                 {
1762                         *p_max_res = l_tccp->numresolutions;
1763                 }
1764
1765                 // use custom size for precincts
1766                 l_level_no = l_tccp->numresolutions - 1;
1767                 for
1768                         (resno = 0; resno < l_tccp->numresolutions; ++resno)
1769                 {
1770                         OPJ_UINT32 l_dx, l_dy;
1771                         // precinct width and height
1772                         l_pdx = l_tccp->prcw[resno];
1773                         l_pdy = l_tccp->prch[resno];
1774                         *lResolutionPtr++ = l_pdx;
1775                         *lResolutionPtr++ = l_pdy;
1776                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1777                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1778                         // take the minimum size for l_dx for each comp and resolution
1779                         *p_dx_min = int_min(*p_dx_min, l_dx);
1780                         *p_dy_min = int_min(*p_dy_min, l_dy);
1781                         // various calculations of extents
1782
1783                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1784                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1785                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1786                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1787                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1788                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1789                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1790                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1791                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1792                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1793                         *lResolutionPtr++ = l_pw;
1794                         *lResolutionPtr++ = l_ph;
1795                         l_product = l_pw * l_ph;
1796                         // update precision
1797                         if
1798                                 (l_product > *p_max_prec)
1799                         {
1800                                 *p_max_prec = l_product;
1801                         }
1802                         --l_level_no;
1803                 }
1804                 ++l_tccp;
1805                 ++l_img_comp;
1806         }
1807 }
1808
1809 /**
1810  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1811  * No other data is set. The include section of the packet  iterator is not allocated.
1812  *
1813  * @param       image           the image used to initialize the packet iterator (only the number of components is relevant.
1814  * @param       cp              the coding parameters.
1815  * @param       tileno          the index of the tile from which creating the packet iterator.
1816  */
1817 opj_pi_iterator_t * pi_create(  const opj_image_t *image,
1818                                                                 const opj_cp_v2_t *cp,
1819                                                                 OPJ_UINT32 tileno )
1820 {
1821         // loop
1822         OPJ_UINT32 pino, compno;
1823         // number of poc in the p_pi
1824         OPJ_UINT32 l_poc_bound;
1825
1826         // pointers to tile coding parameters and components.
1827         opj_pi_iterator_t *l_pi = 00;
1828         opj_tcp_v2_t *tcp = 00;
1829         const opj_tccp_t *tccp = 00;
1830
1831         // current packet iterator being allocated
1832         opj_pi_iterator_t *l_current_pi = 00;
1833
1834         // preconditions in debug
1835         assert(cp != 00);
1836         assert(image != 00);
1837         assert(tileno < cp->tw * cp->th);
1838
1839         // initializations
1840         tcp = &cp->tcps[tileno];
1841         l_poc_bound = tcp->numpocs+1;
1842
1843         // memory allocations
1844         l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1845         if (!l_pi) {
1846                 return NULL;
1847         }
1848         memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1849
1850         l_current_pi = l_pi;
1851         for (pino = 0; pino < l_poc_bound ; ++pino) {
1852
1853                 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1854                 if (! l_current_pi->comps) {
1855                         pi_destroy_v2(l_pi, l_poc_bound);
1856                         return NULL;
1857                 }
1858
1859                 l_current_pi->numcomps = image->numcomps;
1860                 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1861
1862                 for (compno = 0; compno < image->numcomps; ++compno) {
1863                         opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1864
1865                         tccp = &tcp->tccps[compno];
1866
1867                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1868                         if (!comp->resolutions) {
1869                                 pi_destroy_v2(l_pi, l_poc_bound);
1870                                 return 00;
1871                         }
1872
1873                         comp->numresolutions = tccp->numresolutions;
1874                         memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1875                 }
1876                 ++l_current_pi;
1877         }
1878         return l_pi;
1879 }
1880
1881 /**
1882  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
1883  *
1884  * @param       p_cp            the coding parameters to modify
1885  * @param       p_tileno        the tile index being concerned.
1886  * @param       p_tx0           X0 parameter for the tile
1887  * @param       p_tx1           X1 parameter for the tile
1888  * @param       p_ty0           Y0 parameter for the tile
1889  * @param       p_ty1           Y1 parameter for the tile
1890  * @param       p_max_prec      the maximum precision for all the bands of the tile
1891  * @param       p_max_res       the maximum number of resolutions for all the poc inside the tile.
1892  * @param       dx_min          the minimum dx of all the components of all the resolutions for the tile.
1893  * @param       dy_min          the minimum dy of all the components of all the resolutions for the tile.
1894  */
1895 void pi_update_encode_poc_and_final (opj_cp_v2_t *p_cp,
1896                                                                          OPJ_UINT32 p_tileno,
1897                                                                          OPJ_INT32 p_tx0,
1898                                                                          OPJ_INT32 p_tx1,
1899                                                                          OPJ_INT32 p_ty0,
1900                                                                          OPJ_INT32 p_ty1,
1901                                                                          OPJ_UINT32 p_max_prec,
1902                                                                          OPJ_UINT32 p_max_res,
1903                                      OPJ_UINT32 p_dx_min,
1904                                                                          OPJ_UINT32 p_dy_min)
1905 {
1906         // loop
1907         OPJ_UINT32 pino;
1908         // tile coding parameter
1909         opj_tcp_v2_t *l_tcp = 00;
1910         // current poc being updated
1911         opj_poc_t * l_current_poc = 00;
1912
1913         // number of pocs
1914         OPJ_UINT32 l_poc_bound;
1915
1916         // preconditions in debug
1917         assert(p_cp != 00);
1918         assert(p_tileno < p_cp->tw * p_cp->th);
1919
1920         // initializations
1921         l_tcp = &p_cp->tcps [p_tileno];
1922         /* number of iterations in the loop */
1923         l_poc_bound = l_tcp->numpocs+1;
1924
1925         // start at first element, and to make sure the compiler will not make a calculation each time in the loop
1926         // store a pointer to the current element to modify rather than l_tcp->pocs[i]
1927         l_current_poc = l_tcp->pocs;
1928
1929         l_current_poc->compS = l_current_poc->compno0;
1930         l_current_poc->compE = l_current_poc->compno1;
1931         l_current_poc->resS = l_current_poc->resno0;
1932         l_current_poc->resE = l_current_poc->resno1;
1933         l_current_poc->layE = l_current_poc->layno1;
1934
1935         // special treatment for the first element
1936         l_current_poc->layS = 0;
1937         l_current_poc->prg  = l_current_poc->prg1;
1938         l_current_poc->prcS = 0;
1939
1940         l_current_poc->prcE = p_max_prec;
1941         l_current_poc->txS = p_tx0;
1942         l_current_poc->txE = p_tx1;
1943         l_current_poc->tyS = p_ty0;
1944         l_current_poc->tyE = p_ty1;
1945         l_current_poc->dx = p_dx_min;
1946         l_current_poc->dy = p_dy_min;
1947
1948         ++ l_current_poc;
1949         for (pino = 1;pino < l_poc_bound ; ++pino) {
1950                 l_current_poc->compS = l_current_poc->compno0;
1951                 l_current_poc->compE= l_current_poc->compno1;
1952                 l_current_poc->resS = l_current_poc->resno0;
1953                 l_current_poc->resE = l_current_poc->resno1;
1954                 l_current_poc->layE = l_current_poc->layno1;
1955                 l_current_poc->prg  = l_current_poc->prg1;
1956                 l_current_poc->prcS = 0;
1957                 // special treatment here different from the first element
1958                 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
1959
1960                 l_current_poc->prcE = p_max_prec;
1961                 l_current_poc->txS = p_tx0;
1962                 l_current_poc->txE = p_tx1;
1963                 l_current_poc->tyS = p_ty0;
1964                 l_current_poc->tyE = p_ty1;
1965                 l_current_poc->dx = p_dx_min;
1966                 l_current_poc->dy = p_dy_min;
1967                 ++ l_current_poc;
1968         }
1969 }
1970
1971 /**
1972  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
1973  *
1974  * @param       p_cp            the coding parameters to modify
1975  * @param       p_tileno        the tile index being concerned.
1976  * @param       p_tx0           X0 parameter for the tile
1977  * @param       p_tx1           X1 parameter for the tile
1978  * @param       p_ty0           Y0 parameter for the tile
1979  * @param       p_ty1           Y1 parameter for the tile
1980  * @param       p_max_prec      the maximum precision for all the bands of the tile
1981  * @param       p_max_res       the maximum number of resolutions for all the poc inside the tile.
1982  * @param       dx_min          the minimum dx of all the components of all the resolutions for the tile.
1983  * @param       dy_min          the minimum dy of all the components of all the resolutions for the tile.
1984  */
1985 void pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
1986                                                                 OPJ_UINT32 p_num_comps,
1987                                                                 OPJ_UINT32 p_tileno,
1988                                                                 OPJ_INT32 p_tx0,
1989                                                                 OPJ_INT32 p_tx1,
1990                                                                 OPJ_INT32 p_ty0,
1991                                                                 OPJ_INT32 p_ty1,
1992                                                                 OPJ_UINT32 p_max_prec,
1993                                                                 OPJ_UINT32 p_max_res,
1994                                 OPJ_UINT32 p_dx_min,
1995                                                                 OPJ_UINT32 p_dy_min)
1996 {
1997         // loop
1998         OPJ_UINT32 pino;
1999         // tile coding parameter
2000         opj_tcp_v2_t *l_tcp = 00;
2001         // current poc being updated
2002         opj_poc_t * l_current_poc = 00;
2003         // number of pocs
2004         OPJ_UINT32 l_poc_bound;
2005
2006         // preconditions in debug
2007         assert(p_cp != 00);
2008         assert(p_tileno < p_cp->tw * p_cp->th);
2009
2010         // initializations
2011         l_tcp = &p_cp->tcps [p_tileno];
2012
2013         /* number of iterations in the loop */
2014         l_poc_bound = l_tcp->numpocs+1;
2015
2016         // start at first element, and to make sure the compiler will not make a calculation each time in the loop
2017         // store a pointer to the current element to modify rather than l_tcp->pocs[i]
2018         l_current_poc = l_tcp->pocs;
2019
2020         for (pino = 0; pino < l_poc_bound ; ++pino) {
2021                 l_current_poc->compS = 0;
2022                 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
2023                 l_current_poc->resS = 0;
2024                 l_current_poc->resE = p_max_res;
2025                 l_current_poc->layS = 0;
2026                 l_current_poc->layE = l_tcp->numlayers;
2027                 l_current_poc->prg  = l_tcp->prg;
2028                 l_current_poc->prcS = 0;
2029                 l_current_poc->prcE = p_max_prec;
2030                 l_current_poc->txS = p_tx0;
2031                 l_current_poc->txE = p_tx1;
2032                 l_current_poc->tyS = p_ty0;
2033                 l_current_poc->tyE = p_ty1;
2034                 l_current_poc->dx = p_dx_min;
2035                 l_current_poc->dy = p_dy_min;
2036                 ++ l_current_poc;
2037         }
2038 }
2039
2040 /**
2041  * Destroys a packet iterator array.
2042  *
2043  * @param       p_pi                    the packet iterator array to destroy.
2044  * @param       p_nb_elements   the number of elements in the array.
2045  */
2046 void pi_destroy_v2(
2047                                 opj_pi_iterator_t *p_pi,
2048                                 OPJ_UINT32 p_nb_elements)
2049 {
2050         OPJ_UINT32 compno, pino;
2051         opj_pi_iterator_t *l_current_pi = p_pi;
2052         if
2053                 (p_pi)
2054         {
2055                 if
2056                         (p_pi->include)
2057                 {
2058                         opj_free(p_pi->include);
2059                         p_pi->include = 00;
2060                 }
2061                 // TODO
2062                 for
2063                         (pino = 0; pino < p_nb_elements; ++pino)
2064                 {
2065                         if
2066                                 (l_current_pi->comps)
2067                         {
2068                                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2069                                 for
2070                                         (compno = 0; compno < l_current_pi->numcomps; compno++)
2071                                 {
2072                                         if
2073                                                 (l_current_component->resolutions)
2074                                         {
2075                                                 opj_free(l_current_component->resolutions);
2076                                                 l_current_component->resolutions = 00;
2077                                         }
2078                                         ++l_current_component;
2079                                 }
2080                                 opj_free(l_current_pi->comps);
2081                                 l_current_pi->comps = 0;
2082                         }
2083                         ++l_current_pi;
2084                 }
2085                 opj_free(p_pi);
2086         }
2087 }
2088
2089
2090
2091 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
2092 {
2093         // loop
2094         OPJ_UINT32 pino;
2095
2096         // encoding prameters to set
2097         OPJ_UINT32 l_bound;
2098
2099         opj_pi_iterator_t * l_current_pi = 00;
2100         opj_poc_t* l_current_poc = 0;
2101
2102         // preconditions in debug
2103         assert(p_pi != 00);
2104         assert(p_tcp != 00);
2105
2106         // initializations
2107         l_bound = p_tcp->numpocs+1;
2108         l_current_pi = p_pi;
2109         l_current_poc = p_tcp->pocs;
2110
2111         for
2112                 (pino = 0;pino<l_bound;++pino)
2113         {
2114                 l_current_pi->poc.prg = l_current_poc->prg;
2115                 l_current_pi->first = 1;
2116
2117                 l_current_pi->poc.resno0 = l_current_poc->resno0;
2118                 l_current_pi->poc.compno0 = l_current_poc->compno0;
2119                 l_current_pi->poc.layno0 = 0;
2120                 l_current_pi->poc.precno0 = 0;
2121                 l_current_pi->poc.resno1 = l_current_poc->resno1;
2122                 l_current_pi->poc.compno1 = l_current_poc->compno1;
2123                 l_current_pi->poc.layno1 = l_current_poc->layno1;
2124                 l_current_pi->poc.precno1 = p_max_precision;
2125                 ++l_current_pi;
2126                 ++l_current_poc;
2127         }
2128 }
2129
2130
2131 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
2132 {
2133         // loop
2134         OPJ_UINT32 pino;
2135
2136         // encoding prameters to set
2137         OPJ_UINT32 l_bound;
2138
2139         opj_pi_iterator_t * l_current_pi = 00;
2140         // preconditions in debug
2141         assert(p_tcp != 00);
2142         assert(p_pi != 00);
2143
2144         // initializations
2145         l_bound = p_tcp->numpocs+1;
2146         l_current_pi = p_pi;
2147
2148         for
2149                 (pino = 0;pino<l_bound;++pino)
2150         {
2151                 l_current_pi->poc.prg = p_tcp->prg;
2152                 l_current_pi->first = 1;
2153                 l_current_pi->poc.resno0 = 0;
2154                 l_current_pi->poc.compno0 = 0;
2155                 l_current_pi->poc.layno0 = 0;
2156                 l_current_pi->poc.precno0 = 0;
2157                 l_current_pi->poc.resno1 = p_max_res;
2158                 l_current_pi->poc.compno1 = l_current_pi->numcomps;
2159                 l_current_pi->poc.layno1 = p_tcp->numlayers;
2160                 l_current_pi->poc.precno1 = p_max_precision;
2161                 ++l_current_pi;
2162         }
2163 }
2164
2165
2166 void pi_create_encode_v2(       opj_pi_iterator_t *pi,
2167                                                         opj_cp_v2_t *cp,
2168                                                         OPJ_UINT32 tileno,
2169                                                         OPJ_UINT32 pino,
2170                                                         OPJ_UINT32 tpnum,
2171                                                         OPJ_INT32 tppos,
2172                                                         J2K_T2_MODE t2_mode)
2173 {
2174         const OPJ_CHAR *prog;
2175         OPJ_INT32 i,l;
2176         OPJ_UINT32 incr_top=1,resetX=0;
2177         opj_tcp_v2_t *tcps =&cp->tcps[tileno];
2178         opj_poc_t *tcp= &tcps->pocs[pino];
2179
2180         prog = j2k_convert_progression_order(tcp->prg);
2181
2182         pi[pino].first = 1;
2183         pi[pino].poc.prg = tcp->prg;
2184
2185         if(!(cp->m_specific_param.m_enc.m_tp_on&& ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
2186                 pi[pino].poc.resno0 = tcp->resS;
2187                 pi[pino].poc.resno1 = tcp->resE;
2188                 pi[pino].poc.compno0 = tcp->compS;
2189                 pi[pino].poc.compno1 = tcp->compE;
2190                 pi[pino].poc.layno0 = tcp->layS;
2191                 pi[pino].poc.layno1 = tcp->layE;
2192                 pi[pino].poc.precno0 = tcp->prcS;
2193                 pi[pino].poc.precno1 = tcp->prcE;
2194                 pi[pino].poc.tx0 = tcp->txS;
2195                 pi[pino].poc.ty0 = tcp->tyS;
2196                 pi[pino].poc.tx1 = tcp->txE;
2197                 pi[pino].poc.ty1 = tcp->tyE;
2198         }else {
2199                 for(i=tppos+1;i<4;i++){
2200                         switch(prog[i]){
2201                         case 'R':
2202                                 pi[pino].poc.resno0 = tcp->resS;
2203                                 pi[pino].poc.resno1 = tcp->resE;
2204                                 break;
2205                         case 'C':
2206                                 pi[pino].poc.compno0 = tcp->compS;
2207                                 pi[pino].poc.compno1 = tcp->compE;
2208                                 break;
2209                         case 'L':
2210                                 pi[pino].poc.layno0 = tcp->layS;
2211                                 pi[pino].poc.layno1 = tcp->layE;
2212                                 break;
2213                         case 'P':
2214                                 switch(tcp->prg){
2215                                         case LRCP:
2216                                         case RLCP:
2217                                                 pi[pino].poc.precno0 = tcp->prcS;
2218                                                 pi[pino].poc.precno1 = tcp->prcE;
2219                                                 break;
2220                                         default:
2221                                                 pi[pino].poc.tx0 = tcp->txS;
2222                                                 pi[pino].poc.ty0 = tcp->tyS;
2223                                                 pi[pino].poc.tx1 = tcp->txE;
2224                                                 pi[pino].poc.ty1 = tcp->tyE;
2225                                                 break;
2226                                 }
2227                                 break;
2228                         }
2229                 }
2230
2231                 if(tpnum==0){
2232                         for(i=tppos;i>=0;i--){
2233                                 switch(prog[i]){
2234                                                 case 'C':
2235                                                         tcp->comp_t = tcp->compS;
2236                                                         pi[pino].poc.compno0 = tcp->comp_t;
2237                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
2238                                                         tcp->comp_t+=1;
2239                                                         break;
2240                                                 case 'R':
2241                                                         tcp->res_t = tcp->resS;
2242                                                         pi[pino].poc.resno0 = tcp->res_t;
2243                                                         pi[pino].poc.resno1 = tcp->res_t+1;
2244                                                         tcp->res_t+=1;
2245                                                         break;
2246                                                 case 'L':
2247                                                         tcp->lay_t = tcp->layS;
2248                                                         pi[pino].poc.layno0 = tcp->lay_t;
2249                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
2250                                                         tcp->lay_t+=1;
2251                                                         break;
2252                                                 case 'P':
2253                                                         switch(tcp->prg){
2254                                                                 case LRCP:
2255                                                                 case RLCP:
2256                                                                         tcp->prc_t = tcp->prcS;
2257                                                                         pi[pino].poc.precno0 = tcp->prc_t;
2258                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
2259                                                                         tcp->prc_t+=1;
2260                                                                         break;
2261                                                                 default:
2262                                                                         tcp->tx0_t = tcp->txS;
2263                                                                         tcp->ty0_t = tcp->tyS;
2264                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
2265                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2266                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
2267                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2268                                                                         tcp->tx0_t = pi[pino].poc.tx1;
2269                                                                         tcp->ty0_t = pi[pino].poc.ty1;
2270                                                                         break;
2271                                                         }
2272                                                         break;
2273                                 }
2274                         }
2275                         incr_top=1;
2276                 }else{
2277                         for(i=tppos;i>=0;i--){
2278                                 switch(prog[i]){
2279                                                 case 'C':
2280                                                         pi[pino].poc.compno0 = tcp->comp_t-1;
2281                                                         pi[pino].poc.compno1 = tcp->comp_t;
2282                                                         break;
2283                                                 case 'R':
2284                                                         pi[pino].poc.resno0 = tcp->res_t-1;
2285                                                         pi[pino].poc.resno1 = tcp->res_t;
2286                                                         break;
2287                                                 case 'L':
2288                                                         pi[pino].poc.layno0 = tcp->lay_t-1;
2289                                                         pi[pino].poc.layno1 = tcp->lay_t;
2290                                                         break;
2291                                                 case 'P':
2292                                                         switch(tcp->prg){
2293                                                                 case LRCP:
2294                                                                 case RLCP:
2295                                                                         pi[pino].poc.precno0 = tcp->prc_t-1;
2296                                                                         pi[pino].poc.precno1 = tcp->prc_t;
2297                                                                         break;
2298                                                                 default:
2299                                                                         pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
2300                                                                         pi[pino].poc.tx1 = tcp->tx0_t ;
2301                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
2302                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
2303                                                                         break;
2304                                                         }
2305                                                         break;
2306                                 }
2307                                 if(incr_top==1){
2308                                         switch(prog[i]){
2309                                                         case 'R':
2310                                                                 if(tcp->res_t==tcp->resE){
2311                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2312                                                                         if(l==1){
2313                                                                                 tcp->res_t = tcp->resS;
2314                                                                                 pi[pino].poc.resno0 = tcp->res_t;
2315                                                                                 pi[pino].poc.resno1 = tcp->res_t+1;
2316                                                                                 tcp->res_t+=1;
2317                                                                                 incr_top=1;
2318                                                                         }else{
2319                                                                                 incr_top=0;
2320                                                                         }
2321                                                                 }else{
2322                                                                         pi[pino].poc.resno0 = tcp->res_t;
2323                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
2324                                                                         tcp->res_t+=1;
2325                                                                         incr_top=0;
2326                                                                 }
2327                                                                 break;
2328                                                         case 'C':
2329                                                                 if(tcp->comp_t ==tcp->compE){
2330                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2331                                                                         if(l==1){
2332                                                                                 tcp->comp_t = tcp->compS;
2333                                                                                 pi[pino].poc.compno0 = tcp->comp_t;
2334                                                                                 pi[pino].poc.compno1 = tcp->comp_t+1;
2335                                                                                 tcp->comp_t+=1;
2336                                                                                 incr_top=1;
2337                                                                         }else{
2338                                                                                 incr_top=0;
2339                                                                         }
2340                                                                 }else{
2341                                                                         pi[pino].poc.compno0 = tcp->comp_t;
2342                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
2343                                                                         tcp->comp_t+=1;
2344                                                                         incr_top=0;
2345                                                                 }
2346                                                                 break;
2347                                                         case 'L':
2348                                                                 if(tcp->lay_t == tcp->layE){
2349                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2350                                                                         if(l==1){
2351                                                                                 tcp->lay_t = tcp->layS;
2352                                                                                 pi[pino].poc.layno0 = tcp->lay_t;
2353                                                                                 pi[pino].poc.layno1 = tcp->lay_t+1;
2354                                                                                 tcp->lay_t+=1;
2355                                                                                 incr_top=1;
2356                                                                         }else{
2357                                                                                 incr_top=0;
2358                                                                         }
2359                                                                 }else{
2360                                                                         pi[pino].poc.layno0 = tcp->lay_t;
2361                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
2362                                                                         tcp->lay_t+=1;
2363                                                                         incr_top=0;
2364                                                                 }
2365                                                                 break;
2366                                                         case 'P':
2367                                                                 switch(tcp->prg){
2368                                                                         case LRCP:
2369                                                                         case RLCP:
2370                                                                                 if(tcp->prc_t == tcp->prcE){
2371                                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2372                                                                                         if(l==1){
2373                                                                                                 tcp->prc_t = tcp->prcS;
2374                                                                                                 pi[pino].poc.precno0 = tcp->prc_t;
2375                                                                                                 pi[pino].poc.precno1 = tcp->prc_t+1;
2376                                                                                                 tcp->prc_t+=1;
2377                                                                                                 incr_top=1;
2378                                                                                         }else{
2379                                                                                                 incr_top=0;
2380                                                                                         }
2381                                                                                 }else{
2382                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
2383                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
2384                                                                                         tcp->prc_t+=1;
2385                                                                                         incr_top=0;
2386                                                                                 }
2387                                                                                 break;
2388                                                                         default:
2389                                                                                 if(tcp->tx0_t >= tcp->txE){
2390                                                                                         if(tcp->ty0_t >= tcp->tyE){
2391                                                                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2392                                                                                                 if(l==1){
2393                                                                                                         tcp->ty0_t = tcp->tyS;
2394                                                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
2395                                                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2396                                                                                                         tcp->ty0_t = pi[pino].poc.ty1;
2397                                                                                                         incr_top=1;resetX=1;
2398                                                                                                 }else{
2399                                                                                                         incr_top=0;resetX=0;
2400                                                                                                 }
2401                                                                                         }else{
2402                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
2403                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2404                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
2405                                                                                                 incr_top=0;resetX=1;
2406                                                                                         }
2407                                                                                         if(resetX==1){
2408                                                                                                 tcp->tx0_t = tcp->txS;
2409                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
2410                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
2411                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
2412                                                                                         }
2413                                                                                 }else{
2414                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
2415                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
2416                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
2417                                                                                         incr_top=0;
2418                                                                                 }
2419                                                                                 break;
2420                                                                 }
2421                                                                 break;
2422                                         }
2423                                 }
2424                         }
2425                 }
2426         }
2427 }
2428
2429 OPJ_INT32 pi_check_next_level(  OPJ_INT32 pos,
2430                                                                 opj_cp_v2_t *cp,
2431                                                                 OPJ_UINT32 tileno,
2432                                                                 OPJ_UINT32 pino,
2433                                                                 const OPJ_CHAR *prog)
2434 {
2435         OPJ_INT32 i,l;
2436         opj_tcp_v2_t *tcps =&cp->tcps[tileno];
2437         opj_poc_t *tcp = &tcps->pocs[pino];
2438
2439         if(pos>=0){
2440                 for(i=pos;pos>=0;i--){
2441                         switch(prog[i]){
2442                 case 'R':
2443                         if(tcp->res_t==tcp->resE){
2444                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2445                                 if(l==1){
2446                                         return 1;
2447                                 }else{
2448                                         return 0;
2449                                 }
2450                         }else{
2451                                 return 1;
2452                         }
2453                         break;
2454                 case 'C':
2455                         if(tcp->comp_t==tcp->compE){
2456                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2457                                 if(l==1){
2458                                         return 1;
2459                                 }else{
2460                                         return 0;
2461                                 }
2462                         }else{
2463                                 return 1;
2464                         }
2465                         break;
2466                 case 'L':
2467                         if(tcp->lay_t==tcp->layE){
2468                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2469                                 if(l==1){
2470                                         return 1;
2471                                 }else{
2472                                         return 0;
2473                                 }
2474                         }else{
2475                                 return 1;
2476                         }
2477                         break;
2478                 case 'P':
2479                         switch(tcp->prg){
2480                                 case LRCP||RLCP:
2481                                         if(tcp->prc_t == tcp->prcE){
2482                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2483                                                 if(l==1){
2484                                                         return 1;
2485                                                 }else{
2486                                                         return 0;
2487                                                 }
2488                                         }else{
2489                                                 return 1;
2490                                         }
2491                                         break;
2492                         default:
2493                                 if(tcp->tx0_t == tcp->txE){
2494                                         //TY
2495                                         if(tcp->ty0_t == tcp->tyE){
2496                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2497                                                 if(l==1){
2498                                                         return 1;
2499                                                 }else{
2500                                                         return 0;
2501                                                 }
2502                                         }else{
2503                                                 return 1;
2504                                         }//TY
2505                                 }else{
2506                                         return 1;
2507                                 }
2508                                 break;
2509                         }//end case P
2510                 }//end switch
2511                 }//end for
2512         }//end if
2513         return 0;
2514 }