73bff9cf3a640d84bd2c48fe505e94aa7375d60a
[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_v2_t *p_cp,
87                                                  OPJ_UINT32 p_tileno,
88                                                  OPJ_INT32 p_tx0,
89                                                  OPJ_INT32 p_tx1,
90                                                  OPJ_INT32 p_ty0,
91                                                  OPJ_INT32 p_ty1,
92                                                  OPJ_UINT32 p_max_prec,
93                                                  OPJ_UINT32 p_max_res,
94                                                  OPJ_UINT32 p_dx_min,
95                                                  OPJ_UINT32 p_dy_min);
96
97 /**
98  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
99  *
100  * @param       p_cp            the coding parameters to modify
101  * @param       p_tileno        the tile index being concerned.
102  * @param       p_tx0           X0 parameter for the tile
103  * @param       p_tx1           X1 parameter for the tile
104  * @param       p_ty0           Y0 parameter for the tile
105  * @param       p_ty1           Y1 parameter for the tile
106  * @param       p_max_prec      the maximum precision for all the bands of the tile
107  * @param       p_max_res       the maximum number of resolutions for all the poc inside the tile.
108  * @param       dx_min          the minimum dx of all the components of all the resolutions for the tile.
109  * @param       dy_min          the minimum dy of all the components of all the resolutions for the tile.
110  */
111 static void opj_pi_update_encode_not_poc (  opj_cp_v2_t *p_cp,
112                                             OPJ_UINT32 p_num_comps,
113                                             OPJ_UINT32 p_tileno,
114                                             OPJ_INT32 p_tx0,
115                                             OPJ_INT32 p_tx1,
116                                             OPJ_INT32 p_ty0,
117                                             OPJ_INT32 p_ty1,
118                                             OPJ_UINT32 p_max_prec,
119                                             OPJ_UINT32 p_max_res,
120                                             OPJ_UINT32 p_dx_min,
121                                             OPJ_UINT32 p_dy_min);
122 /**
123  * 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_v2_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_v2_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_v2_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_v2_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_v2_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_v2_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 : int_min(pi->dx, dx);
325                                 pi->dy = !pi->dy ? dy : 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 = int_ceildiv(pi->tx0, comp->dx << levelno);
351                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
352                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
353                                         try1 = 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 = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
368                                                  - int_floordivpow2(trx0, res->pdx);
369                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
370                                                  - 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 : int_min(pi->dx, dx);
409                                 pi->dy = !pi->dy ? dy : 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 < 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 = int_ceildiv(pi->tx0, comp->dx << levelno);
432                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
433                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
434                                         try1 = 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 = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
449                                                  - int_floordivpow2(trx0, res->pdx);
450                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
451                                                  - 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                 int resno;
483                 comp = &pi->comps[pi->compno];
484                 pi->dx = 0;
485                 pi->dy = 0;
486                 for (resno = 0; resno < comp->numresolutions; resno++) {
487                         int 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 : int_min(pi->dx, dx);
492                         pi->dy = !pi->dy ? dy : 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 < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
503                                         int levelno;
504                                         int trx0, try0;
505                                         int trx1, try1;
506                                         int rpx, rpy;
507                                         int prci, prcj;
508                                         res = &comp->resolutions[pi->resno];
509                                         levelno = comp->numresolutions - 1 - pi->resno;
510                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
511                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
512                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
513                                         try1 = 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 = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
528                                                  - int_floordivpow2(trx0, res->pdx);
529                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
530                                                  - 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 /*
549 ==========================================================
550    Packet iterator interface
551 ==========================================================
552 */
553 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
554                                                                                 opj_cp_v2_t *p_cp,
555                                                                                 OPJ_UINT32 p_tile_no)
556 {
557         /* loop */
558         OPJ_UINT32 pino;
559         OPJ_UINT32 compno, resno;
560
561         /* to store w, h, dx and dy fro all components and resolutions */
562         OPJ_UINT32 * l_tmp_data;
563         OPJ_UINT32 ** l_tmp_ptr;
564
565         /* encoding prameters to set */
566         OPJ_UINT32 l_max_res;
567         OPJ_UINT32 l_max_prec;
568         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
569         OPJ_UINT32 l_dx_min,l_dy_min;
570         OPJ_UINT32 l_bound;
571         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
572         OPJ_UINT32 l_data_stride;
573
574         /* pointers */
575         opj_pi_iterator_t *l_pi = 00;
576         opj_tcp_v2_t *l_tcp = 00;
577         const opj_tccp_t *l_tccp = 00;
578         opj_pi_comp_t *l_current_comp = 00;
579         opj_image_comp_t * l_img_comp = 00;
580         opj_pi_iterator_t * l_current_pi = 00;
581         OPJ_UINT32 * l_encoding_value_ptr = 00;
582
583         /* preconditions in debug */
584         assert(p_cp != 00);
585         assert(p_image != 00);
586         assert(p_tile_no < p_cp->tw * p_cp->th);
587
588         /* initializations */
589         l_tcp = &p_cp->tcps[p_tile_no];
590         l_bound = l_tcp->numpocs+1;
591
592         l_data_stride = 4 * J2K_MAXRLVLS;
593         l_tmp_data = (OPJ_UINT32*)opj_malloc(
594                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
595         if
596                 (! l_tmp_data)
597         {
598                 return 00;
599         }
600         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
601                 p_image->numcomps * sizeof(OPJ_UINT32 *));
602         if
603                 (! l_tmp_ptr)
604         {
605                 opj_free(l_tmp_data);
606                 return 00;
607         }
608
609         /* memory allocation for pi */
610         l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
611         if (!l_pi) {
612                 opj_free(l_tmp_data);
613                 opj_free(l_tmp_ptr);
614                 return 00;
615         }
616
617         l_encoding_value_ptr = l_tmp_data;
618         /* update pointer array */
619         for
620                 (compno = 0; compno < p_image->numcomps; ++compno)
621         {
622                 l_tmp_ptr[compno] = l_encoding_value_ptr;
623                 l_encoding_value_ptr += l_data_stride;
624         }
625         /* get encoding parameters */
626         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);
627
628         /* step calculations */
629         l_step_p = 1;
630         l_step_c = l_max_prec * l_step_p;
631         l_step_r = p_image->numcomps * l_step_c;
632         l_step_l = l_max_res * l_step_r;
633
634         /* set values for first packet iterator */
635         l_current_pi = l_pi;
636
637         /* memory allocation for include */
638         l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
639         if
640                 (!l_current_pi->include)
641         {
642                 opj_free(l_tmp_data);
643                 opj_free(l_tmp_ptr);
644                 opj_pi_destroy(l_pi, l_bound);
645                 return 00;
646         }
647         memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
648
649         /* special treatment for the first packet iterator */
650         l_current_comp = l_current_pi->comps;
651         l_img_comp = p_image->comps;
652         l_tccp = l_tcp->tccps;
653
654         l_current_pi->tx0 = l_tx0;
655         l_current_pi->ty0 = l_ty0;
656         l_current_pi->tx1 = l_tx1;
657         l_current_pi->ty1 = l_ty1;
658
659         /*l_current_pi->dx = l_img_comp->dx;*/
660         /*l_current_pi->dy = l_img_comp->dy;*/
661
662         l_current_pi->step_p = l_step_p;
663         l_current_pi->step_c = l_step_c;
664         l_current_pi->step_r = l_step_r;
665         l_current_pi->step_l = l_step_l;
666
667         /* allocation for components and number of components has already been calculated by opj_pi_create */
668         for
669                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
670         {
671                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
672                 l_encoding_value_ptr = l_tmp_ptr[compno];
673
674                 l_current_comp->dx = l_img_comp->dx;
675                 l_current_comp->dy = l_img_comp->dy;
676                 /* resolutions have already been initialized */
677                 for
678                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
679                 {
680                         l_res->pdx = *(l_encoding_value_ptr++);
681                         l_res->pdy = *(l_encoding_value_ptr++);
682                         l_res->pw =  *(l_encoding_value_ptr++);
683                         l_res->ph =  *(l_encoding_value_ptr++);
684                         ++l_res;
685                 }
686                 ++l_current_comp;
687                 ++l_img_comp;
688                 ++l_tccp;
689         }
690         ++l_current_pi;
691
692         for
693                 (pino = 1 ; pino<l_bound ; ++pino )
694         {
695                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
696                 opj_image_comp_t * l_img_comp = p_image->comps;
697                 l_tccp = l_tcp->tccps;
698
699                 l_current_pi->tx0 = l_tx0;
700                 l_current_pi->ty0 = l_ty0;
701                 l_current_pi->tx1 = l_tx1;
702                 l_current_pi->ty1 = l_ty1;
703                 /*l_current_pi->dx = l_dx_min;*/
704                 /*l_current_pi->dy = l_dy_min;*/
705                 l_current_pi->step_p = l_step_p;
706                 l_current_pi->step_c = l_step_c;
707                 l_current_pi->step_r = l_step_r;
708                 l_current_pi->step_l = l_step_l;
709
710                 /* allocation for components and number of components has already been calculated by opj_pi_create */
711                 for
712                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
713                 {
714                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
715                         l_encoding_value_ptr = l_tmp_ptr[compno];
716
717                         l_current_comp->dx = l_img_comp->dx;
718                         l_current_comp->dy = l_img_comp->dy;
719                         /* resolutions have already been initialized */
720                         for
721                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
722                         {
723                                 l_res->pdx = *(l_encoding_value_ptr++);
724                                 l_res->pdy = *(l_encoding_value_ptr++);
725                                 l_res->pw =  *(l_encoding_value_ptr++);
726                                 l_res->ph =  *(l_encoding_value_ptr++);
727                                 ++l_res;
728                         }
729                         ++l_current_comp;
730                         ++l_img_comp;
731                         ++l_tccp;
732                 }
733                 /* special treatment*/
734                 l_current_pi->include = (l_current_pi-1)->include;
735                 ++l_current_pi;
736         }
737         opj_free(l_tmp_data);
738         l_tmp_data = 00;
739         opj_free(l_tmp_ptr);
740         l_tmp_ptr = 00;
741         if
742                 (l_tcp->POC)
743         {
744                 opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
745         }
746         else
747         {
748                 opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
749         }
750         return l_pi;
751 }
752
753
754
755 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
756                                             opj_cp_v2_t *p_cp,
757                                             OPJ_UINT32 p_tile_no,
758                                             J2K_T2_MODE p_t2_mode )
759 {
760         /* loop*/
761         OPJ_UINT32 pino;
762         OPJ_UINT32 compno, resno;
763
764         /* to store w, h, dx and dy fro all components and resolutions*/
765         OPJ_UINT32 * l_tmp_data;
766         OPJ_UINT32 ** l_tmp_ptr;
767
768         /* encoding prameters to set*/
769         OPJ_UINT32 l_max_res;
770         OPJ_UINT32 l_max_prec;
771         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
772         OPJ_UINT32 l_dx_min,l_dy_min;
773         OPJ_UINT32 l_bound;
774         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
775         OPJ_UINT32 l_data_stride;
776
777         /* pointers*/
778         opj_pi_iterator_t *l_pi = 00;
779         opj_tcp_v2_t *l_tcp = 00;
780         const opj_tccp_t *l_tccp = 00;
781         opj_pi_comp_t *l_current_comp = 00;
782         opj_image_comp_t * l_img_comp = 00;
783         opj_pi_iterator_t * l_current_pi = 00;
784         OPJ_UINT32 * l_encoding_value_ptr = 00;
785
786         /* preconditions in debug*/
787         assert(p_cp != 00);
788         assert(p_image != 00);
789         assert(p_tile_no < p_cp->tw * p_cp->th);
790
791         /* initializations*/
792         l_tcp = &p_cp->tcps[p_tile_no];
793         l_bound = l_tcp->numpocs+1;
794
795         l_data_stride = 4 * J2K_MAXRLVLS;
796         l_tmp_data = (OPJ_UINT32*)opj_malloc(
797                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
798         if (! l_tmp_data) {
799                 return 00;
800         }
801
802         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
803                 p_image->numcomps * sizeof(OPJ_UINT32 *));
804         if (! l_tmp_ptr) {
805                 opj_free(l_tmp_data);
806                 return 00;
807         }
808
809         /* memory allocation for pi*/
810         l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
811         if (!l_pi) {
812                 opj_free(l_tmp_data);
813                 opj_free(l_tmp_ptr);
814                 return 00;
815         }
816
817         l_encoding_value_ptr = l_tmp_data;
818         /* update pointer array*/
819         for (compno = 0; compno < p_image->numcomps; ++compno) {
820                 l_tmp_ptr[compno] = l_encoding_value_ptr;
821                 l_encoding_value_ptr += l_data_stride;
822         }
823
824         /* get encoding parameters*/
825         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);
826
827         /* step calculations*/
828         l_step_p = 1;
829         l_step_c = l_max_prec * l_step_p;
830         l_step_r = p_image->numcomps * l_step_c;
831         l_step_l = l_max_res * l_step_r;
832
833         /* set values for first packet iterator*/
834         l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
835         l_current_pi = l_pi;
836
837         /* memory allocation for include*/
838         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
839         if (!l_current_pi->include) {
840                 opj_free(l_tmp_data);
841                 opj_free(l_tmp_ptr);
842                 opj_pi_destroy(l_pi, l_bound);
843                 return 00;
844         }
845         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
846
847         /* special treatment for the first packet iterator*/
848         l_current_comp = l_current_pi->comps;
849         l_img_comp = p_image->comps;
850         l_tccp = l_tcp->tccps;
851         l_current_pi->tx0 = l_tx0;
852         l_current_pi->ty0 = l_ty0;
853         l_current_pi->tx1 = l_tx1;
854         l_current_pi->ty1 = l_ty1;
855         l_current_pi->dx = l_dx_min;
856         l_current_pi->dy = l_dy_min;
857         l_current_pi->step_p = l_step_p;
858         l_current_pi->step_c = l_step_c;
859         l_current_pi->step_r = l_step_r;
860         l_current_pi->step_l = l_step_l;
861
862         /* allocation for components and number of components has already been calculated by opj_pi_create */
863         for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
864                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
865                 l_encoding_value_ptr = l_tmp_ptr[compno];
866
867                 l_current_comp->dx = l_img_comp->dx;
868                 l_current_comp->dy = l_img_comp->dy;
869
870                 /* resolutions have already been initialized */
871                 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
872                         l_res->pdx = *(l_encoding_value_ptr++);
873                         l_res->pdy = *(l_encoding_value_ptr++);
874                         l_res->pw =  *(l_encoding_value_ptr++);
875                         l_res->ph =  *(l_encoding_value_ptr++);
876                         ++l_res;
877                 }
878
879                 ++l_current_comp;
880                 ++l_img_comp;
881                 ++l_tccp;
882         }
883         ++l_current_pi;
884
885         for (pino = 1 ; pino<l_bound ; ++pino ) {
886                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
887                 opj_image_comp_t * l_img_comp = p_image->comps;
888                 l_tccp = l_tcp->tccps;
889
890                 l_current_pi->tx0 = l_tx0;
891                 l_current_pi->ty0 = l_ty0;
892                 l_current_pi->tx1 = l_tx1;
893                 l_current_pi->ty1 = l_ty1;
894                 l_current_pi->dx = l_dx_min;
895                 l_current_pi->dy = l_dy_min;
896                 l_current_pi->step_p = l_step_p;
897                 l_current_pi->step_c = l_step_c;
898                 l_current_pi->step_r = l_step_r;
899                 l_current_pi->step_l = l_step_l;
900
901                 /* allocation for components and number of components has already been calculated by opj_pi_create */
902                 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
903                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
904                         l_encoding_value_ptr = l_tmp_ptr[compno];
905
906                         l_current_comp->dx = l_img_comp->dx;
907                         l_current_comp->dy = l_img_comp->dy;
908                         /* resolutions have already been initialized */
909                         for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
910                                 l_res->pdx = *(l_encoding_value_ptr++);
911                                 l_res->pdy = *(l_encoding_value_ptr++);
912                                 l_res->pw =  *(l_encoding_value_ptr++);
913                                 l_res->ph =  *(l_encoding_value_ptr++);
914                                 ++l_res;
915                         }
916                         ++l_current_comp;
917                         ++l_img_comp;
918                         ++l_tccp;
919                 }
920
921                 /* special treatment*/
922                 l_current_pi->include = (l_current_pi-1)->include;
923                 ++l_current_pi;
924         }
925
926         opj_free(l_tmp_data);
927         l_tmp_data = 00;
928         opj_free(l_tmp_ptr);
929         l_tmp_ptr = 00;
930
931         if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
932                 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);
933         }
934         else {
935                 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);
936         }
937
938         return l_pi;
939 }
940
941 opj_bool opj_pi_next(opj_pi_iterator_t * pi) {
942         switch (pi->poc.prg) {
943                 case LRCP:
944                         return opj_pi_next_lrcp(pi);
945                 case RLCP:
946                         return opj_pi_next_rlcp(pi);
947                 case RPCL:
948                         return opj_pi_next_rpcl(pi);
949                 case PCRL:
950                         return opj_pi_next_pcrl(pi);
951                 case CPRL:
952                         return opj_pi_next_cprl(pi);
953                 case PROG_UNKNOWN:
954                         return OPJ_FALSE;
955         }
956         
957         return OPJ_FALSE;
958 }
959
960
961
962 void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
963                                         opj_cp_v2_t *p_cp,
964                                         OPJ_UINT32 p_tile_no )
965 {
966         /* encoding parameters to set */
967         OPJ_UINT32 l_max_res;
968         OPJ_UINT32 l_max_prec;
969         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
970         OPJ_UINT32 l_dx_min,l_dy_min;
971
972         /* pointers */
973         opj_tcp_v2_t *l_tcp = 00;
974
975         /* preconditions */
976         assert(p_cp != 00);
977         assert(p_image != 00);
978         assert(p_tile_no < p_cp->tw * p_cp->th);
979
980         l_tcp = &(p_cp->tcps[p_tile_no]);
981
982         /* get encoding parameters */
983         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);
984
985         if (l_tcp->POC) {
986                 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);
987         }
988         else {
989                 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);
990         }
991
992 }
993
994 void opj_get_encoding_parameters(       const opj_image_t *p_image,
995                                     const opj_cp_v2_t *p_cp,
996                                     OPJ_UINT32 p_tileno,
997                                     OPJ_INT32 * p_tx0,
998                                     OPJ_INT32  * p_tx1,
999                                     OPJ_INT32  * p_ty0,
1000                                     OPJ_INT32  * p_ty1,
1001                                     OPJ_UINT32 * p_dx_min,
1002                                     OPJ_UINT32 * p_dy_min,
1003                                     OPJ_UINT32 * p_max_prec,
1004                                     OPJ_UINT32 * p_max_res )
1005 {
1006         /* loop */
1007         OPJ_UINT32  compno, resno;
1008         /* pointers */
1009         const opj_tcp_v2_t *l_tcp = 00;
1010         const opj_tccp_t * l_tccp = 00;
1011         const opj_image_comp_t * l_img_comp = 00;
1012
1013         /* position in x and y of tile */
1014         OPJ_UINT32 p, q;
1015
1016         /* preconditions */
1017         assert(p_cp != 00);
1018         assert(p_image != 00);
1019         assert(p_tileno < p_cp->tw * p_cp->th);
1020
1021         /* initializations */
1022         l_tcp = &p_cp->tcps [p_tileno];
1023         l_img_comp = p_image->comps;
1024         l_tccp = l_tcp->tccps;
1025
1026         /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
1027         p = p_tileno % p_cp->tw;
1028         q = p_tileno / p_cp->tw;
1029
1030         /* find extent of tile */
1031         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1032         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1033         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1034         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1035
1036         /* max precision is 0 (can only grow) */
1037         *p_max_prec = 0;
1038         *p_max_res = 0;
1039
1040         /* take the largest value for dx_min and dy_min */
1041         *p_dx_min = 0x7fffffff;
1042         *p_dy_min  = 0x7fffffff;
1043
1044         for (compno = 0; compno < p_image->numcomps; ++compno) {
1045                 /* arithmetic variables to calculate */
1046                 OPJ_UINT32 l_level_no;
1047                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1048                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1049                 OPJ_UINT32 l_pdx, l_pdy;
1050                 OPJ_UINT32 l_pw, l_ph;
1051                 OPJ_UINT32 l_product;
1052                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1053
1054                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1055                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1056                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1057                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1058
1059                 if (l_tccp->numresolutions > *p_max_res) {
1060                         *p_max_res = l_tccp->numresolutions;
1061                 }
1062
1063                 /* use custom size for precincts */
1064                 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1065                         OPJ_UINT32 l_dx, l_dy;
1066
1067                         /* precinct width and height */
1068                         l_pdx = l_tccp->prcw[resno];
1069                         l_pdy = l_tccp->prch[resno];
1070
1071                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
1072                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
1073
1074                         /* take the minimum size for dx for each comp and resolution */
1075                         *p_dx_min = uint_min(*p_dx_min, l_dx);
1076                         *p_dy_min = uint_min(*p_dy_min, l_dy);
1077
1078                         /* various calculations of extents */
1079                         l_level_no = l_tccp->numresolutions - 1 - resno;
1080
1081                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1082                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1083                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1084                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1085
1086                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1087                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1088                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1089
1090                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1091
1092                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1093                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1094
1095                         l_product = l_pw * l_ph;
1096
1097                         /* update precision */
1098                         if (l_product > *p_max_prec) {
1099                                 *p_max_prec = l_product;
1100                         }
1101                 }
1102                 ++l_img_comp;
1103                 ++l_tccp;
1104         }
1105 }
1106
1107
1108 void opj_get_all_encoding_parameters(   const opj_image_t *p_image,
1109                                         const opj_cp_v2_t *p_cp,
1110                                         OPJ_UINT32 tileno,
1111                                         OPJ_INT32 * p_tx0,
1112                                         OPJ_INT32 * p_tx1,
1113                                         OPJ_INT32 * p_ty0,
1114                                         OPJ_INT32 * p_ty1,
1115                                         OPJ_UINT32 * p_dx_min,
1116                                         OPJ_UINT32 * p_dy_min,
1117                                         OPJ_UINT32 * p_max_prec,
1118                                         OPJ_UINT32 * p_max_res,
1119                                         OPJ_UINT32 ** p_resolutions )
1120 {
1121         /* loop*/
1122         OPJ_UINT32 compno, resno;
1123
1124         /* pointers*/
1125         const opj_tcp_v2_t *tcp = 00;
1126         const opj_tccp_t * l_tccp = 00;
1127         const opj_image_comp_t * l_img_comp = 00;
1128
1129         /* to store l_dx, l_dy, w and h for each resolution and component.*/
1130         OPJ_UINT32 * lResolutionPtr;
1131
1132         /* position in x and y of tile*/
1133         OPJ_UINT32 p, q;
1134
1135         /* preconditions in debug*/
1136         assert(p_cp != 00);
1137         assert(p_image != 00);
1138         assert(tileno < p_cp->tw * p_cp->th);
1139
1140         /* initializations*/
1141         tcp = &p_cp->tcps [tileno];
1142         l_tccp = tcp->tccps;
1143         l_img_comp = p_image->comps;
1144
1145         /* position in x and y of tile*/
1146         p = tileno % p_cp->tw;
1147         q = tileno / p_cp->tw;
1148
1149         /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1150         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1151         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1152         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1153         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1154
1155         /* max precision and resolution is 0 (can only grow)*/
1156         *p_max_prec = 0;
1157         *p_max_res = 0;
1158
1159         /* take the largest value for dx_min and dy_min*/
1160         *p_dx_min = 0x7fffffff;
1161         *p_dy_min = 0x7fffffff;
1162
1163         for (compno = 0; compno < p_image->numcomps; ++compno) {
1164                 /* aritmetic variables to calculate*/
1165                 OPJ_UINT32 l_level_no;
1166                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1167                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1168                 OPJ_UINT32 l_product;
1169                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1170                 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1171
1172                 lResolutionPtr = p_resolutions[compno];
1173
1174                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1175                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1176                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1177                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1178
1179                 if (l_tccp->numresolutions > *p_max_res) {
1180                         *p_max_res = l_tccp->numresolutions;
1181                 }
1182
1183                 /* use custom size for precincts*/
1184                 l_level_no = l_tccp->numresolutions - 1;
1185                 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1186                         OPJ_UINT32 l_dx, l_dy;
1187
1188                         /* precinct width and height*/
1189                         l_pdx = l_tccp->prcw[resno];
1190                         l_pdy = l_tccp->prch[resno];
1191                         *lResolutionPtr++ = l_pdx;
1192                         *lResolutionPtr++ = l_pdy;
1193                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1194                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1195                         /* take the minimum size for l_dx for each comp and resolution*/
1196                         *p_dx_min = int_min(*p_dx_min, l_dx);
1197                         *p_dy_min = int_min(*p_dy_min, l_dy);
1198
1199                         /* various calculations of extents*/
1200                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1201                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1202                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1203                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1204                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1205                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1206                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1207                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1208                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1209                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1210                         *lResolutionPtr++ = l_pw;
1211                         *lResolutionPtr++ = l_ph;
1212                         l_product = l_pw * l_ph;
1213                         
1214             /* update precision*/
1215                         if (l_product > *p_max_prec) {
1216                                 *p_max_prec = l_product;
1217                         }
1218
1219                         --l_level_no;
1220                 }
1221                 ++l_tccp;
1222                 ++l_img_comp;
1223         }
1224 }
1225
1226 opj_pi_iterator_t * opj_pi_create(      const opj_image_t *image,
1227                                     const opj_cp_v2_t *cp,
1228                                     OPJ_UINT32 tileno )
1229 {
1230         /* loop*/
1231         OPJ_UINT32 pino, compno;
1232         /* number of poc in the p_pi*/
1233         OPJ_UINT32 l_poc_bound;
1234
1235         /* pointers to tile coding parameters and components.*/
1236         opj_pi_iterator_t *l_pi = 00;
1237         opj_tcp_v2_t *tcp = 00;
1238         const opj_tccp_t *tccp = 00;
1239
1240         /* current packet iterator being allocated*/
1241         opj_pi_iterator_t *l_current_pi = 00;
1242
1243         /* preconditions in debug*/
1244         assert(cp != 00);
1245         assert(image != 00);
1246         assert(tileno < cp->tw * cp->th);
1247
1248         /* initializations*/
1249         tcp = &cp->tcps[tileno];
1250         l_poc_bound = tcp->numpocs+1;
1251
1252         /* memory allocations*/
1253         l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1254         if (!l_pi) {
1255                 return NULL;
1256         }
1257         memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1258
1259         l_current_pi = l_pi;
1260         for (pino = 0; pino < l_poc_bound ; ++pino) {
1261
1262                 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1263                 if (! l_current_pi->comps) {
1264                         opj_pi_destroy(l_pi, l_poc_bound);
1265                         return NULL;
1266                 }
1267
1268                 l_current_pi->numcomps = image->numcomps;
1269                 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1270
1271                 for (compno = 0; compno < image->numcomps; ++compno) {
1272                         opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1273
1274                         tccp = &tcp->tccps[compno];
1275
1276                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1277                         if (!comp->resolutions) {
1278                                 opj_pi_destroy(l_pi, l_poc_bound);
1279                                 return 00;
1280                         }
1281
1282                         comp->numresolutions = tccp->numresolutions;
1283                         memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1284                 }
1285                 ++l_current_pi;
1286         }
1287         return l_pi;
1288 }
1289
1290 void opj_pi_update_encode_poc_and_final (   opj_cp_v2_t *p_cp,
1291                                             OPJ_UINT32 p_tileno,
1292                                             OPJ_INT32 p_tx0,
1293                                             OPJ_INT32 p_tx1,
1294                                             OPJ_INT32 p_ty0,
1295                                             OPJ_INT32 p_ty1,
1296                                             OPJ_UINT32 p_max_prec,
1297                                             OPJ_UINT32 p_max_res,
1298                                             OPJ_UINT32 p_dx_min,
1299                                             OPJ_UINT32 p_dy_min)
1300 {
1301         /* loop*/
1302         OPJ_UINT32 pino;
1303         /* tile coding parameter*/
1304         opj_tcp_v2_t *l_tcp = 00;
1305         /* current poc being updated*/
1306         opj_poc_t * l_current_poc = 00;
1307
1308         /* number of pocs*/
1309         OPJ_UINT32 l_poc_bound;
1310
1311         /* preconditions in debug*/
1312         assert(p_cp != 00);
1313         assert(p_tileno < p_cp->tw * p_cp->th);
1314
1315         /* initializations*/
1316         l_tcp = &p_cp->tcps [p_tileno];
1317         /* number of iterations in the loop */
1318         l_poc_bound = l_tcp->numpocs+1;
1319
1320         /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1321            store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1322         l_current_poc = l_tcp->pocs;
1323
1324         l_current_poc->compS = l_current_poc->compno0;
1325         l_current_poc->compE = l_current_poc->compno1;
1326         l_current_poc->resS = l_current_poc->resno0;
1327         l_current_poc->resE = l_current_poc->resno1;
1328         l_current_poc->layE = l_current_poc->layno1;
1329
1330         /* special treatment for the first element*/
1331         l_current_poc->layS = 0;
1332         l_current_poc->prg  = l_current_poc->prg1;
1333         l_current_poc->prcS = 0;
1334
1335         l_current_poc->prcE = p_max_prec;
1336         l_current_poc->txS = p_tx0;
1337         l_current_poc->txE = p_tx1;
1338         l_current_poc->tyS = p_ty0;
1339         l_current_poc->tyE = p_ty1;
1340         l_current_poc->dx = p_dx_min;
1341         l_current_poc->dy = p_dy_min;
1342
1343         ++ l_current_poc;
1344         for (pino = 1;pino < l_poc_bound ; ++pino) {
1345                 l_current_poc->compS = l_current_poc->compno0;
1346                 l_current_poc->compE= l_current_poc->compno1;
1347                 l_current_poc->resS = l_current_poc->resno0;
1348                 l_current_poc->resE = l_current_poc->resno1;
1349                 l_current_poc->layE = l_current_poc->layno1;
1350                 l_current_poc->prg  = l_current_poc->prg1;
1351                 l_current_poc->prcS = 0;
1352                 /* special treatment here different from the first element*/
1353                 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
1354
1355                 l_current_poc->prcE = p_max_prec;
1356                 l_current_poc->txS = p_tx0;
1357                 l_current_poc->txE = p_tx1;
1358                 l_current_poc->tyS = p_ty0;
1359                 l_current_poc->tyE = p_ty1;
1360                 l_current_poc->dx = p_dx_min;
1361                 l_current_poc->dy = p_dy_min;
1362                 ++ l_current_poc;
1363         }
1364 }
1365
1366 void opj_pi_update_encode_not_poc (     opj_cp_v2_t *p_cp,
1367                                     OPJ_UINT32 p_num_comps,
1368                                     OPJ_UINT32 p_tileno,
1369                                     OPJ_INT32 p_tx0,
1370                                     OPJ_INT32 p_tx1,
1371                                     OPJ_INT32 p_ty0,
1372                                     OPJ_INT32 p_ty1,
1373                                     OPJ_UINT32 p_max_prec,
1374                                     OPJ_UINT32 p_max_res,
1375                                     OPJ_UINT32 p_dx_min,
1376                                     OPJ_UINT32 p_dy_min)
1377 {
1378         /* loop*/
1379         OPJ_UINT32 pino;
1380         /* tile coding parameter*/
1381         opj_tcp_v2_t *l_tcp = 00;
1382         /* current poc being updated*/
1383         opj_poc_t * l_current_poc = 00;
1384         /* number of pocs*/
1385         OPJ_UINT32 l_poc_bound;
1386
1387         /* preconditions in debug*/
1388         assert(p_cp != 00);
1389         assert(p_tileno < p_cp->tw * p_cp->th);
1390
1391         /* initializations*/
1392         l_tcp = &p_cp->tcps [p_tileno];
1393
1394         /* number of iterations in the loop */
1395         l_poc_bound = l_tcp->numpocs+1;
1396
1397         /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1398            store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1399         l_current_poc = l_tcp->pocs;
1400
1401         for (pino = 0; pino < l_poc_bound ; ++pino) {
1402                 l_current_poc->compS = 0;
1403                 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1404                 l_current_poc->resS = 0;
1405                 l_current_poc->resE = p_max_res;
1406                 l_current_poc->layS = 0;
1407                 l_current_poc->layE = l_tcp->numlayers;
1408                 l_current_poc->prg  = l_tcp->prg;
1409                 l_current_poc->prcS = 0;
1410                 l_current_poc->prcE = p_max_prec;
1411                 l_current_poc->txS = p_tx0;
1412                 l_current_poc->txE = p_tx1;
1413                 l_current_poc->tyS = p_ty0;
1414                 l_current_poc->tyE = p_ty1;
1415                 l_current_poc->dx = p_dx_min;
1416                 l_current_poc->dy = p_dy_min;
1417                 ++ l_current_poc;
1418         }
1419 }
1420
1421 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1422                     OPJ_UINT32 p_nb_elements)
1423 {
1424         OPJ_UINT32 compno, pino;
1425         opj_pi_iterator_t *l_current_pi = p_pi;
1426         if
1427                 (p_pi)
1428         {
1429                 if
1430                         (p_pi->include)
1431                 {
1432                         opj_free(p_pi->include);
1433                         p_pi->include = 00;
1434                 }
1435                 /* TODO*/
1436                 for
1437                         (pino = 0; pino < p_nb_elements; ++pino)
1438                 {
1439                         if
1440                                 (l_current_pi->comps)
1441                         {
1442                                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1443                                 for
1444                                         (compno = 0; compno < l_current_pi->numcomps; compno++)
1445                                 {
1446                                         if
1447                                                 (l_current_component->resolutions)
1448                                         {
1449                                                 opj_free(l_current_component->resolutions);
1450                                                 l_current_component->resolutions = 00;
1451                                         }
1452                                         ++l_current_component;
1453                                 }
1454                                 opj_free(l_current_pi->comps);
1455                                 l_current_pi->comps = 0;
1456                         }
1457                         ++l_current_pi;
1458                 }
1459                 opj_free(p_pi);
1460         }
1461 }
1462
1463 void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
1464                                opj_tcp_v2_t * p_tcp,
1465                                OPJ_UINT32 p_max_precision,
1466                                OPJ_UINT32 p_max_res)
1467 {
1468         /* loop*/
1469         OPJ_UINT32 pino;
1470
1471         /* encoding prameters to set*/
1472         OPJ_UINT32 l_bound;
1473
1474         opj_pi_iterator_t * l_current_pi = 00;
1475         opj_poc_t* l_current_poc = 0;
1476
1477         /* preconditions in debug*/
1478         assert(p_pi != 00);
1479         assert(p_tcp != 00);
1480
1481         /* initializations*/
1482         l_bound = p_tcp->numpocs+1;
1483         l_current_pi = p_pi;
1484         l_current_poc = p_tcp->pocs;
1485
1486         for
1487                 (pino = 0;pino<l_bound;++pino)
1488         {
1489                 l_current_pi->poc.prg = l_current_poc->prg;
1490                 l_current_pi->first = 1;
1491
1492                 l_current_pi->poc.resno0 = l_current_poc->resno0;
1493                 l_current_pi->poc.compno0 = l_current_poc->compno0;
1494                 l_current_pi->poc.layno0 = 0;
1495                 l_current_pi->poc.precno0 = 0;
1496                 l_current_pi->poc.resno1 = l_current_poc->resno1;
1497                 l_current_pi->poc.compno1 = l_current_poc->compno1;
1498                 l_current_pi->poc.layno1 = l_current_poc->layno1;
1499                 l_current_pi->poc.precno1 = p_max_precision;
1500                 ++l_current_pi;
1501                 ++l_current_poc;
1502         }
1503 }
1504
1505 void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
1506                                    opj_tcp_v2_t * p_tcp,
1507                                    OPJ_UINT32 p_max_precision,
1508                                    OPJ_UINT32 p_max_res)
1509 {
1510         /* loop*/
1511         OPJ_UINT32 pino;
1512
1513         /* encoding prameters to set*/
1514         OPJ_UINT32 l_bound;
1515
1516         opj_pi_iterator_t * l_current_pi = 00;
1517         /* preconditions in debug*/
1518         assert(p_tcp != 00);
1519         assert(p_pi != 00);
1520
1521         /* initializations*/
1522         l_bound = p_tcp->numpocs+1;
1523         l_current_pi = p_pi;
1524
1525         for
1526                 (pino = 0;pino<l_bound;++pino)
1527         {
1528                 l_current_pi->poc.prg = p_tcp->prg;
1529                 l_current_pi->first = 1;
1530                 l_current_pi->poc.resno0 = 0;
1531                 l_current_pi->poc.compno0 = 0;
1532                 l_current_pi->poc.layno0 = 0;
1533                 l_current_pi->poc.precno0 = 0;
1534                 l_current_pi->poc.resno1 = p_max_res;
1535                 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1536                 l_current_pi->poc.layno1 = p_tcp->numlayers;
1537                 l_current_pi->poc.precno1 = p_max_precision;
1538                 ++l_current_pi;
1539         }
1540 }
1541
1542 void opj_pi_create_encode(      opj_pi_iterator_t *pi,
1543                                                         opj_cp_v2_t *cp,
1544                                                         OPJ_UINT32 tileno,
1545                                                         OPJ_UINT32 pino,
1546                                                         OPJ_UINT32 tpnum,
1547                                                         OPJ_INT32 tppos,
1548                                                         J2K_T2_MODE t2_mode)
1549 {
1550         const OPJ_CHAR *prog;
1551         OPJ_INT32 i;
1552         OPJ_UINT32 incr_top=1,resetX=0;
1553         opj_tcp_v2_t *tcps =&cp->tcps[tileno];
1554         opj_poc_t *tcp= &tcps->pocs[pino];
1555
1556         prog = opj_j2k_convert_progression_order(tcp->prg);
1557
1558         pi[pino].first = 1;
1559         pi[pino].poc.prg = tcp->prg;
1560
1561         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))){
1562                 pi[pino].poc.resno0 = tcp->resS;
1563                 pi[pino].poc.resno1 = tcp->resE;
1564                 pi[pino].poc.compno0 = tcp->compS;
1565                 pi[pino].poc.compno1 = tcp->compE;
1566                 pi[pino].poc.layno0 = tcp->layS;
1567                 pi[pino].poc.layno1 = tcp->layE;
1568                 pi[pino].poc.precno0 = tcp->prcS;
1569                 pi[pino].poc.precno1 = tcp->prcE;
1570                 pi[pino].poc.tx0 = tcp->txS;
1571                 pi[pino].poc.ty0 = tcp->tyS;
1572                 pi[pino].poc.tx1 = tcp->txE;
1573                 pi[pino].poc.ty1 = tcp->tyE;
1574         }else {
1575                 for(i=tppos+1;i<4;i++){
1576                         switch(prog[i]){
1577                         case 'R':
1578                                 pi[pino].poc.resno0 = tcp->resS;
1579                                 pi[pino].poc.resno1 = tcp->resE;
1580                                 break;
1581                         case 'C':
1582                                 pi[pino].poc.compno0 = tcp->compS;
1583                                 pi[pino].poc.compno1 = tcp->compE;
1584                                 break;
1585                         case 'L':
1586                                 pi[pino].poc.layno0 = tcp->layS;
1587                                 pi[pino].poc.layno1 = tcp->layE;
1588                                 break;
1589                         case 'P':
1590                                 switch(tcp->prg){
1591                                         case LRCP:
1592                                         case RLCP:
1593                                                 pi[pino].poc.precno0 = tcp->prcS;
1594                                                 pi[pino].poc.precno1 = tcp->prcE;
1595                                                 break;
1596                                         default:
1597                                                 pi[pino].poc.tx0 = tcp->txS;
1598                                                 pi[pino].poc.ty0 = tcp->tyS;
1599                                                 pi[pino].poc.tx1 = tcp->txE;
1600                                                 pi[pino].poc.ty1 = tcp->tyE;
1601                                                 break;
1602                                 }
1603                                 break;
1604                         }
1605                 }
1606
1607                 if(tpnum==0){
1608                         for(i=tppos;i>=0;i--){
1609                                 switch(prog[i]){
1610                                                 case 'C':
1611                                                         tcp->comp_t = tcp->compS;
1612                                                         pi[pino].poc.compno0 = tcp->comp_t;
1613                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1614                                                         tcp->comp_t+=1;
1615                                                         break;
1616                                                 case 'R':
1617                                                         tcp->res_t = tcp->resS;
1618                                                         pi[pino].poc.resno0 = tcp->res_t;
1619                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1620                                                         tcp->res_t+=1;
1621                                                         break;
1622                                                 case 'L':
1623                                                         tcp->lay_t = tcp->layS;
1624                                                         pi[pino].poc.layno0 = tcp->lay_t;
1625                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1626                                                         tcp->lay_t+=1;
1627                                                         break;
1628                                                 case 'P':
1629                                                         switch(tcp->prg){
1630                                                                 case LRCP:
1631                                                                 case RLCP:
1632                                                                         tcp->prc_t = tcp->prcS;
1633                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1634                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1635                                                                         tcp->prc_t+=1;
1636                                                                         break;
1637                                                                 default:
1638                                                                         tcp->tx0_t = tcp->txS;
1639                                                                         tcp->ty0_t = tcp->tyS;
1640                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1641                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1642                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1643                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1644                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1645                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1646                                                                         break;
1647                                                         }
1648                                                         break;
1649                                 }
1650                         }
1651                         incr_top=1;
1652                 }else{
1653                         for(i=tppos;i>=0;i--){
1654                                 switch(prog[i]){
1655                                                 case 'C':
1656                                                         pi[pino].poc.compno0 = tcp->comp_t-1;
1657                                                         pi[pino].poc.compno1 = tcp->comp_t;
1658                                                         break;
1659                                                 case 'R':
1660                                                         pi[pino].poc.resno0 = tcp->res_t-1;
1661                                                         pi[pino].poc.resno1 = tcp->res_t;
1662                                                         break;
1663                                                 case 'L':
1664                                                         pi[pino].poc.layno0 = tcp->lay_t-1;
1665                                                         pi[pino].poc.layno1 = tcp->lay_t;
1666                                                         break;
1667                                                 case 'P':
1668                                                         switch(tcp->prg){
1669                                                                 case LRCP:
1670                                                                 case RLCP:
1671                                                                         pi[pino].poc.precno0 = tcp->prc_t-1;
1672                                                                         pi[pino].poc.precno1 = tcp->prc_t;
1673                                                                         break;
1674                                                                 default:
1675                                                                         pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1676                                                                         pi[pino].poc.tx1 = tcp->tx0_t ;
1677                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1678                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1679                                                                         break;
1680                                                         }
1681                                                         break;
1682                                 }
1683                                 if(incr_top==1){
1684                                         switch(prog[i]){
1685                                                         case 'R':
1686                                                                 if(tcp->res_t==tcp->resE){
1687                                                                         if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1688                                                                                 tcp->res_t = tcp->resS;
1689                                                                                 pi[pino].poc.resno0 = tcp->res_t;
1690                                                                                 pi[pino].poc.resno1 = tcp->res_t+1;
1691                                                                                 tcp->res_t+=1;
1692                                                                                 incr_top=1;
1693                                                                         }else{
1694                                                                                 incr_top=0;
1695                                                                         }
1696                                                                 }else{
1697                                                                         pi[pino].poc.resno0 = tcp->res_t;
1698                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1699                                                                         tcp->res_t+=1;
1700                                                                         incr_top=0;
1701                                                                 }
1702                                                                 break;
1703                                                         case 'C':
1704                                                                 if(tcp->comp_t ==tcp->compE){
1705                                                                         if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1706                                                                                 tcp->comp_t = tcp->compS;
1707                                                                                 pi[pino].poc.compno0 = tcp->comp_t;
1708                                                                                 pi[pino].poc.compno1 = tcp->comp_t+1;
1709                                                                                 tcp->comp_t+=1;
1710                                                                                 incr_top=1;
1711                                                                         }else{
1712                                                                                 incr_top=0;
1713                                                                         }
1714                                                                 }else{
1715                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1716                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1717                                                                         tcp->comp_t+=1;
1718                                                                         incr_top=0;
1719                                                                 }
1720                                                                 break;
1721                                                         case 'L':
1722                                                                 if(tcp->lay_t == tcp->layE){
1723                                                                         if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1724                                                                                 tcp->lay_t = tcp->layS;
1725                                                                                 pi[pino].poc.layno0 = tcp->lay_t;
1726                                                                                 pi[pino].poc.layno1 = tcp->lay_t+1;
1727                                                                                 tcp->lay_t+=1;
1728                                                                                 incr_top=1;
1729                                                                         }else{
1730                                                                                 incr_top=0;
1731                                                                         }
1732                                                                 }else{
1733                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1734                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1735                                                                         tcp->lay_t+=1;
1736                                                                         incr_top=0;
1737                                                                 }
1738                                                                 break;
1739                                                         case 'P':
1740                                                                 switch(tcp->prg){
1741                                                                         case LRCP:
1742                                                                         case RLCP:
1743                                                                                 if(tcp->prc_t == tcp->prcE){
1744                                                                                         if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1745                                                                                                 tcp->prc_t = tcp->prcS;
1746                                                                                                 pi[pino].poc.precno0 = tcp->prc_t;
1747                                                                                                 pi[pino].poc.precno1 = tcp->prc_t+1;
1748                                                                                                 tcp->prc_t+=1;
1749                                                                                                 incr_top=1;
1750                                                                                         }else{
1751                                                                                                 incr_top=0;
1752                                                                                         }
1753                                                                                 }else{
1754                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1755                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1756                                                                                         tcp->prc_t+=1;
1757                                                                                         incr_top=0;
1758                                                                                 }
1759                                                                                 break;
1760                                                                         default:
1761                                                                                 if(tcp->tx0_t >= tcp->txE){
1762                                                                                         if(tcp->ty0_t >= tcp->tyE){
1763                                                                                                 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1764                                                                                                         tcp->ty0_t = tcp->tyS;
1765                                                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1766                                                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1767                                                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1768                                                                                                         incr_top=1;resetX=1;
1769                                                                                                 }else{
1770                                                                                                         incr_top=0;resetX=0;
1771                                                                                                 }
1772                                                                                         }else{
1773                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1774                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1775                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1776                                                                                                 incr_top=0;resetX=1;
1777                                                                                         }
1778                                                                                         if(resetX==1){
1779                                                                                                 tcp->tx0_t = tcp->txS;
1780                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1781                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1782                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1783                                                                                         }
1784                                                                                 }else{
1785                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1786                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1787                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1788                                                                                         incr_top=0;
1789                                                                                 }
1790                                                                                 break;
1791                                                                 }
1792                                                                 break;
1793                                         }
1794                                 }
1795                         }
1796                 }
1797         }
1798 }
1799
1800 opj_bool opj_pi_check_next_level(       OPJ_INT32 pos,
1801                                                                 opj_cp_v2_t *cp,
1802                                                                 OPJ_UINT32 tileno,
1803                                                                 OPJ_UINT32 pino,
1804                                                                 const OPJ_CHAR *prog)
1805 {
1806         OPJ_INT32 i;
1807         opj_tcp_v2_t *tcps =&cp->tcps[tileno];
1808         opj_poc_t *tcp = &tcps->pocs[pino];
1809
1810         if(pos>=0){
1811                 for(i=pos;pos>=0;i--){
1812                         switch(prog[i]){
1813                     case 'R':
1814                             if(tcp->res_t==tcp->resE){
1815                                     if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1816                                             return OPJ_TRUE;
1817                                     }else{
1818                                             return OPJ_FALSE;
1819                                     }
1820                             }else{
1821                                     return OPJ_TRUE;
1822                             }
1823                             break;
1824                     case 'C':
1825                             if(tcp->comp_t==tcp->compE){
1826                                     if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1827                                             return OPJ_TRUE;
1828                                     }else{
1829                                             return OPJ_FALSE;
1830                                     }
1831                             }else{
1832                                     return OPJ_TRUE;
1833                             }
1834                             break;
1835                     case 'L':
1836                             if(tcp->lay_t==tcp->layE){
1837                                     if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1838                                             return OPJ_TRUE;
1839                                     }else{
1840                                             return OPJ_FALSE;
1841                                     }
1842                             }else{
1843                                     return OPJ_TRUE;
1844                             }
1845                             break;
1846                     case 'P':
1847                             switch(tcp->prg){
1848                                     case LRCP||RLCP:
1849                                             if(tcp->prc_t == tcp->prcE){
1850                                                     if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1851                                                             return OPJ_TRUE;
1852                                                     }else{
1853                                                             return OPJ_FALSE;
1854                                                     }
1855                                             }else{
1856                                                     return OPJ_TRUE;
1857                                             }
1858                                             break;
1859                             default:
1860                                     if(tcp->tx0_t == tcp->txE){
1861                                             /*TY*/
1862                                             if(tcp->ty0_t == tcp->tyE){
1863                                                     if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1864                                                             return OPJ_TRUE;
1865                                                     }else{
1866                                                             return OPJ_FALSE;
1867                                                     }
1868                                             }else{
1869                                                     return OPJ_TRUE;
1870                                             }/*TY*/
1871                                     }else{
1872                                             return OPJ_TRUE;
1873                                     }
1874                                     break;
1875                             }/*end case P*/
1876                     }/*end switch*/
1877                 }/*end for*/
1878         }/*end if*/
1879         return OPJ_FALSE;
1880 }