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