Work In Progress: insert elements from V2 framework into the trunk
[openjpeg.git] / libopenjpeg / pi.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "opj_includes.h"
34
35 /** @defgroup PI PI - Implementation of a packet iterator */
36 /*@{*/
37
38 /** @name Local static functions */
39 /*@{*/
40
41 /**
42 Get next packet in layer-resolution-component-precinct order.
43 @param pi packet iterator to modify
44 @return returns false if pi pointed to the last packet or else returns true 
45 */
46 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi);
47 /**
48 Get next packet in resolution-layer-component-precinct order.
49 @param pi packet iterator to modify
50 @return returns false if pi pointed to the last packet or else returns true 
51 */
52 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi);
53 /**
54 Get next packet in resolution-precinct-component-layer order.
55 @param pi packet iterator to modify
56 @return returns false if pi pointed to the last packet or else returns true 
57 */
58 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi);
59 /**
60 Get next packet in precinct-component-resolution-layer order.
61 @param pi packet iterator to modify
62 @return returns false if pi pointed to the last packet or else returns true 
63 */
64 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi);
65 /**
66 Get next packet in component-precinct-resolution-layer order.
67 @param pi packet iterator to modify
68 @return returns false if pi pointed to the last packet or else returns true 
69 */
70 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi);
71
72
73
74 /**
75  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
76  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
77  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
78  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
79  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
80  *
81  * @param       p_image                 the image being encoded.
82  * @param       p_cp                    the coding parameters.
83  * @param       tileno                  the tile index of the tile being encoded.
84  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
85  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
86  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
87  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
88  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
89  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
90  * @param       dx_min                  pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
91  * @param       dy_min                  pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
92  * @param       p_resolutions   pointer to an area corresponding to the one described above.
93  */
94 void get_all_encoding_parameters(
95                                                                 const opj_image_t *p_image,
96                                                                 const opj_cp_v2_t *p_cp,
97                                                                 OPJ_UINT32 tileno,
98                                                                 OPJ_INT32 * p_tx0,
99                                                                 OPJ_INT32 * p_tx1,
100                                                                 OPJ_INT32 * p_ty0,
101                                                                 OPJ_INT32 * p_ty1,
102                                                                 OPJ_UINT32 * p_dx_min,
103                                                                 OPJ_UINT32 * p_dy_min,
104                                                                 OPJ_UINT32 * p_max_prec,
105                                                                 OPJ_UINT32 * p_max_res,
106                                                                 OPJ_UINT32 ** p_resolutions
107                                                         );
108
109
110 /**
111  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
112  * No other data is set. The include section of the packet  iterator is not allocated.
113  *
114  * @param       p_image         the image used to initialize the packet iterator (in fact only the number of components is relevant.
115  * @param       p_cp            the coding parameters.
116  * @param       p_tile_no       the index of the tile from which creating the packet iterator.
117  */
118 opj_pi_iterator_t * pi_create(
119                                                                 const opj_image_t *image,
120                                                                 const opj_cp_v2_t *cp,
121                                                                 OPJ_UINT32 tileno
122                                                         );
123
124 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
125 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
126
127
128 /*@}*/
129
130 /*@}*/
131
132 /* 
133 ==========================================================
134    local functions
135 ==========================================================
136 */
137
138 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
139         opj_pi_comp_t *comp = NULL;
140         opj_pi_resolution_t *res = NULL;
141         long index = 0;
142         
143         if (!pi->first) {
144                 comp = &pi->comps[pi->compno];
145                 res = &comp->resolutions[pi->resno];
146                 goto LABEL_SKIP;
147         } else {
148                 pi->first = 0;
149         }
150
151         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
152                 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
153                 pi->resno++) {
154                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
155                                 comp = &pi->comps[pi->compno];
156                                 if (pi->resno >= comp->numresolutions) {
157                                         continue;
158                                 }
159                                 res = &comp->resolutions[pi->resno];
160                                 if (!pi->tp_on){
161                                         pi->poc.precno1 = res->pw * res->ph;
162                                 }
163                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
164                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
165                                         if (!pi->include[index]) {
166                                                 pi->include[index] = 1;
167                                                 return OPJ_TRUE;
168                                         }
169 LABEL_SKIP:;
170                                 }
171                         }
172                 }
173         }
174         
175         return OPJ_FALSE;
176 }
177
178 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
179         opj_pi_comp_t *comp = NULL;
180         opj_pi_resolution_t *res = NULL;
181         long index = 0;
182
183         if (!pi->first) {
184                 comp = &pi->comps[pi->compno];
185                 res = &comp->resolutions[pi->resno];
186                 goto LABEL_SKIP;
187         } else {
188                 pi->first = 0;
189         }
190
191         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
192                 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
193                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
194                                 comp = &pi->comps[pi->compno];
195                                 if (pi->resno >= comp->numresolutions) {
196                                         continue;
197                                 }
198                                 res = &comp->resolutions[pi->resno];
199                                 if(!pi->tp_on){
200                                         pi->poc.precno1 = res->pw * res->ph;
201                                 }
202                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
203                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
204                                         if (!pi->include[index]) {
205                                                 pi->include[index] = 1;
206                                                 return OPJ_TRUE;
207                                         }
208 LABEL_SKIP:;
209                                 }
210                         }
211                 }
212         }
213         
214         return OPJ_FALSE;
215 }
216
217 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
218         opj_pi_comp_t *comp = NULL;
219         opj_pi_resolution_t *res = NULL;
220         long index = 0;
221
222         if (!pi->first) {
223                 goto LABEL_SKIP;
224         } else {
225                 int compno, resno;
226                 pi->first = 0;
227                 pi->dx = 0;
228                 pi->dy = 0;
229                 for (compno = 0; compno < pi->numcomps; compno++) {
230                         comp = &pi->comps[compno];
231                         for (resno = 0; resno < comp->numresolutions; resno++) {
232                                 int dx, dy;
233                                 res = &comp->resolutions[resno];
234                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
235                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
236                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
237                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
238                         }
239                 }
240         }
241 if (!pi->tp_on){
242                         pi->poc.ty0 = pi->ty0;
243                         pi->poc.tx0 = pi->tx0;
244                         pi->poc.ty1 = pi->ty1;
245                         pi->poc.tx1 = pi->tx1;
246                 }
247         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
248                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
249                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
250                                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
251                                         int levelno;
252                                         int trx0, try0;
253                                         int trx1, try1;
254                                         int rpx, rpy;
255                                         int prci, prcj;
256                                         comp = &pi->comps[pi->compno];
257                                         if (pi->resno >= comp->numresolutions) {
258                                                 continue;
259                                         }
260                                         res = &comp->resolutions[pi->resno];
261                                         levelno = comp->numresolutions - 1 - pi->resno;
262                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
263                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
264                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
265                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
266                                         rpx = res->pdx + levelno;
267                                         rpy = res->pdy + levelno;
268                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
269                                                 continue;       
270                                         }
271                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
272                                                 continue; 
273                                         }
274                                         
275                                         if ((res->pw==0)||(res->ph==0)) continue;
276                                         
277                                         if ((trx0==trx1)||(try0==try1)) continue;
278                                         
279                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
280                                                  - int_floordivpow2(trx0, res->pdx);
281                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
282                                                  - int_floordivpow2(try0, res->pdy);
283                                         pi->precno = prci + prcj * res->pw;
284                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
285                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
286                                                 if (!pi->include[index]) {
287                                                         pi->include[index] = 1;
288                                                         return OPJ_TRUE;
289                                                 }
290 LABEL_SKIP:;
291                                         }
292                                 }
293                         }
294                 }
295         }
296         
297         return OPJ_FALSE;
298 }
299
300 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
301         opj_pi_comp_t *comp = NULL;
302         opj_pi_resolution_t *res = NULL;
303         long index = 0;
304
305         if (!pi->first) {
306                 comp = &pi->comps[pi->compno];
307                 goto LABEL_SKIP;
308         } else {
309                 int compno, resno;
310                 pi->first = 0;
311                 pi->dx = 0;
312                 pi->dy = 0;
313                 for (compno = 0; compno < pi->numcomps; compno++) {
314                         comp = &pi->comps[compno];
315                         for (resno = 0; resno < comp->numresolutions; resno++) {
316                                 int dx, dy;
317                                 res = &comp->resolutions[resno];
318                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
319                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
320                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
321                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
322                         }
323                 }
324         }
325         if (!pi->tp_on){
326                         pi->poc.ty0 = pi->ty0;
327                         pi->poc.tx0 = pi->tx0;
328                         pi->poc.ty1 = pi->ty1;
329                         pi->poc.tx1 = pi->tx1;
330                 }
331         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
332                 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
333                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
334                                 comp = &pi->comps[pi->compno];
335                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
336                                         int levelno;
337                                         int trx0, try0;
338                                         int trx1, try1;
339                                         int rpx, rpy;
340                                         int prci, prcj;
341                                         res = &comp->resolutions[pi->resno];
342                                         levelno = comp->numresolutions - 1 - pi->resno;
343                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
344                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
345                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
346                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
347                                         rpx = res->pdx + levelno;
348                                         rpy = res->pdy + levelno;
349                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
350                                                 continue;       
351                                         }
352                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
353                                                 continue; 
354                                         }
355                                         
356                                         if ((res->pw==0)||(res->ph==0)) continue;
357                                         
358                                         if ((trx0==trx1)||(try0==try1)) continue;
359                                         
360                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
361                                                  - int_floordivpow2(trx0, res->pdx);
362                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
363                                                  - int_floordivpow2(try0, res->pdy);
364                                         pi->precno = prci + prcj * res->pw;
365                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
366                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
367                                                 if (!pi->include[index]) {
368                                                         pi->include[index] = 1;
369                                                         return OPJ_TRUE;
370                                                 }       
371 LABEL_SKIP:;
372                                         }
373                                 }
374                         }
375                 }
376         }
377         
378         return OPJ_FALSE;
379 }
380
381 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
382         opj_pi_comp_t *comp = NULL;
383         opj_pi_resolution_t *res = NULL;
384         long index = 0;
385
386         if (!pi->first) {
387                 comp = &pi->comps[pi->compno];
388                 goto LABEL_SKIP;
389         } else {
390                 pi->first = 0;
391         }
392
393         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
394                 int resno;
395                 comp = &pi->comps[pi->compno];
396                 pi->dx = 0;
397                 pi->dy = 0;
398                 for (resno = 0; resno < comp->numresolutions; resno++) {
399                         int dx, dy;
400                         res = &comp->resolutions[resno];
401                         dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
402                         dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
403                         pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
404                         pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
405                 }
406                 if (!pi->tp_on){
407                         pi->poc.ty0 = pi->ty0;
408                         pi->poc.tx0 = pi->tx0;
409                         pi->poc.ty1 = pi->ty1;
410                         pi->poc.tx1 = pi->tx1;
411                 }
412                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
413                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
414                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
415                                         int levelno;
416                                         int trx0, try0;
417                                         int trx1, try1;
418                                         int rpx, rpy;
419                                         int prci, prcj;
420                                         res = &comp->resolutions[pi->resno];
421                                         levelno = comp->numresolutions - 1 - pi->resno;
422                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
423                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
424                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
425                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
426                                         rpx = res->pdx + levelno;
427                                         rpy = res->pdy + levelno;
428                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
429                                                 continue;       
430                                         }
431                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
432                                                 continue; 
433                                         }
434                                         
435                                         if ((res->pw==0)||(res->ph==0)) continue;
436                                         
437                                         if ((trx0==trx1)||(try0==try1)) continue;
438                                         
439                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
440                                                  - int_floordivpow2(trx0, res->pdx);
441                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
442                                                  - int_floordivpow2(try0, res->pdy);
443                                         pi->precno = prci + prcj * res->pw;
444                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
445                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
446                                                 if (!pi->include[index]) {
447                                                         pi->include[index] = 1;
448                                                         return OPJ_TRUE;
449                                                 }
450 LABEL_SKIP:;
451                                         }
452                                 }
453                         }
454                 }
455         }
456         
457         return OPJ_FALSE;
458 }
459
460 /* 
461 ==========================================================
462    Packet iterator interface
463 ==========================================================
464 */
465
466 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
467         int p, q;
468         int compno, resno, pino;
469         opj_pi_iterator_t *pi = NULL;
470         opj_tcp_t *tcp = NULL;
471         opj_tccp_t *tccp = NULL;
472
473         tcp = &cp->tcps[tileno];
474
475         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
476         if(!pi) {
477                 /* TODO: throw an error */
478                 return NULL;
479         }
480
481         for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
482                 int maxres = 0;
483                 int maxprec = 0;
484                 p = tileno % cp->tw;
485                 q = tileno / cp->tw;
486
487                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
488                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
489                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
490                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
491                 pi[pino].numcomps = image->numcomps;
492
493                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
494                 if(!pi[pino].comps) {
495                         /* TODO: throw an error */
496                         pi_destroy(pi, cp, tileno);
497                         return NULL;
498                 }
499                 
500                 for (compno = 0; compno < pi->numcomps; compno++) {
501                         int tcx0, tcy0, tcx1, tcy1;
502                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
503                         tccp = &tcp->tccps[compno];
504                         comp->dx = image->comps[compno].dx;
505                         comp->dy = image->comps[compno].dy;
506                         comp->numresolutions = tccp->numresolutions;
507
508                         comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
509                         if(!comp->resolutions) {
510                                 /* TODO: throw an error */
511                                 pi_destroy(pi, cp, tileno);
512                                 return NULL;
513                         }
514
515                         tcx0 = int_ceildiv(pi->tx0, comp->dx);
516                         tcy0 = int_ceildiv(pi->ty0, comp->dy);
517                         tcx1 = int_ceildiv(pi->tx1, comp->dx);
518                         tcy1 = int_ceildiv(pi->ty1, comp->dy);
519                         if (comp->numresolutions > maxres) {
520                                 maxres = comp->numresolutions;
521                         }
522
523                         for (resno = 0; resno < comp->numresolutions; resno++) {
524                                 int levelno;
525                                 int rx0, ry0, rx1, ry1;
526                                 int px0, py0, px1, py1;
527                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
528                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
529                                         res->pdx = tccp->prcw[resno];
530                                         res->pdy = tccp->prch[resno];
531                                 } else {
532                                         res->pdx = 15;
533                                         res->pdy = 15;
534                                 }
535                                 levelno = comp->numresolutions - 1 - resno;
536                                 rx0 = int_ceildivpow2(tcx0, levelno);
537                                 ry0 = int_ceildivpow2(tcy0, levelno);
538                                 rx1 = int_ceildivpow2(tcx1, levelno);
539                                 ry1 = int_ceildivpow2(tcy1, levelno);
540                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
541                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
542                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
543                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
544                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
545                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
546                                 
547                                 if (res->pw*res->ph > maxprec) {
548                                         maxprec = res->pw*res->ph;
549                                 }
550                                 
551                         }
552                 }
553                 
554                 tccp = &tcp->tccps[0];
555                 pi[pino].step_p = 1;
556                 pi[pino].step_c = maxprec * pi[pino].step_p;
557                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
558                 pi[pino].step_l = maxres * pi[pino].step_r;
559                 
560                 if (pino == 0) {
561                         pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
562                         if(!pi[pino].include) {
563                                 /* TODO: throw an error */
564                                 pi_destroy(pi, cp, tileno);
565                                 return NULL;
566                         }
567                 }
568                 else {
569                         pi[pino].include = pi[pino - 1].include;
570                 }
571                 
572                 if (tcp->POC == 0) {
573                         pi[pino].first = 1;
574                         pi[pino].poc.resno0 = 0;
575                         pi[pino].poc.compno0 = 0;
576                         pi[pino].poc.layno1 = tcp->numlayers;
577                         pi[pino].poc.resno1 = maxres;
578                         pi[pino].poc.compno1 = image->numcomps;
579                         pi[pino].poc.prg = tcp->prg;
580                 } else {
581                         pi[pino].first = 1;
582                         pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
583                         pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
584                         pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
585                         pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
586                         pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
587                         pi[pino].poc.prg = tcp->pocs[pino].prg;
588                 }
589                 pi[pino].poc.layno0  = 0;
590                 pi[pino].poc.precno0 = 0; 
591                 pi[pino].poc.precno1 = maxprec;
592                         
593         }
594         
595         return pi;
596 }
597
598
599 opj_pi_iterator_t *pi_create_decode_v2(
600                                                                                 opj_image_t *p_image,
601                                                                                 opj_cp_v2_t *p_cp,
602                                                                                 OPJ_UINT32 p_tile_no
603                                                                                 )
604 {
605         // loop
606         OPJ_UINT32 pino;
607         OPJ_UINT32 compno, resno;
608
609         // to store w, h, dx and dy fro all components and resolutions
610         OPJ_UINT32 * l_tmp_data;
611         OPJ_UINT32 ** l_tmp_ptr;
612
613         // encoding prameters to set
614         OPJ_UINT32 l_max_res;
615         OPJ_UINT32 l_max_prec;
616         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
617         OPJ_UINT32 l_dx_min,l_dy_min;
618         OPJ_UINT32 l_bound;
619         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
620         OPJ_UINT32 l_data_stride;
621
622         // pointers
623         opj_pi_iterator_t *l_pi = 00;
624         opj_tcp_v2_t *l_tcp = 00;
625         const opj_tccp_t *l_tccp = 00;
626         opj_pi_comp_t *l_current_comp = 00;
627         opj_image_comp_t * l_img_comp = 00;
628         opj_pi_iterator_t * l_current_pi = 00;
629         OPJ_UINT32 * l_encoding_value_ptr = 00;
630
631         // preconditions in debug
632         assert(p_cp != 00);
633         assert(p_image != 00);
634         assert(p_tile_no < p_cp->tw * p_cp->th);
635
636         // initializations
637         l_tcp = &p_cp->tcps[p_tile_no];
638         l_bound = l_tcp->numpocs+1;
639
640         l_data_stride = 4 * J2K_MAXRLVLS;
641         l_tmp_data = (OPJ_UINT32*)opj_malloc(
642                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
643         if
644                 (! l_tmp_data)
645         {
646                 return 00;
647         }
648         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
649                 p_image->numcomps * sizeof(OPJ_UINT32 *));
650         if
651                 (! l_tmp_ptr)
652         {
653                 opj_free(l_tmp_data);
654                 return 00;
655         }
656
657         // memory allocation for pi
658         l_pi = pi_create(p_image,p_cp,p_tile_no);
659         if
660                 (!l_pi)
661         {
662                 opj_free(l_tmp_data);
663                 opj_free(l_tmp_ptr);
664                 return 00;
665         }
666
667         l_encoding_value_ptr = l_tmp_data;
668         // update pointer array
669         for
670                 (compno = 0; compno < p_image->numcomps; ++compno)
671         {
672                 l_tmp_ptr[compno] = l_encoding_value_ptr;
673                 l_encoding_value_ptr += l_data_stride;
674         }
675         // get encoding parameters
676         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);
677
678         // step calculations
679         l_step_p = 1;
680         l_step_c = l_max_prec * l_step_p;
681         l_step_r = p_image->numcomps * l_step_c;
682         l_step_l = l_max_res * l_step_r;
683
684         // set values for first packet iterator
685         l_current_pi = l_pi;
686
687         // memory allocation for include
688         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
689         if
690                 (!l_current_pi->include)
691         {
692                 opj_free(l_tmp_data);
693                 opj_free(l_tmp_ptr);
694                 pi_destroy_v2(l_pi, l_bound);
695                 return 00;
696         }
697         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
698
699         // special treatment for the first packet iterator
700         l_current_comp = l_current_pi->comps;
701         l_img_comp = p_image->comps;
702         l_tccp = l_tcp->tccps;
703
704         l_current_pi->tx0 = l_tx0;
705         l_current_pi->ty0 = l_ty0;
706         l_current_pi->tx1 = l_tx1;
707         l_current_pi->ty1 = l_ty1;
708
709         //l_current_pi->dx = l_img_comp->dx;
710         //l_current_pi->dy = l_img_comp->dy;
711
712         l_current_pi->step_p = l_step_p;
713         l_current_pi->step_c = l_step_c;
714         l_current_pi->step_r = l_step_r;
715         l_current_pi->step_l = l_step_l;
716
717         /* allocation for components and number of components has already been calculated by pi_create */
718         for
719                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
720         {
721                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
722                 l_encoding_value_ptr = l_tmp_ptr[compno];
723
724                 l_current_comp->dx = l_img_comp->dx;
725                 l_current_comp->dy = l_img_comp->dy;
726                 /* resolutions have already been initialized */
727                 for
728                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
729                 {
730                         l_res->pdx = *(l_encoding_value_ptr++);
731                         l_res->pdy = *(l_encoding_value_ptr++);
732                         l_res->pw =  *(l_encoding_value_ptr++);
733                         l_res->ph =  *(l_encoding_value_ptr++);
734                         ++l_res;
735                 }
736                 ++l_current_comp;
737                 ++l_img_comp;
738                 ++l_tccp;
739         }
740         ++l_current_pi;
741
742         for
743                 (pino = 1 ; pino<l_bound ; ++pino )
744         {
745                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
746                 opj_image_comp_t * l_img_comp = p_image->comps;
747                 l_tccp = l_tcp->tccps;
748
749                 l_current_pi->tx0 = l_tx0;
750                 l_current_pi->ty0 = l_ty0;
751                 l_current_pi->tx1 = l_tx1;
752                 l_current_pi->ty1 = l_ty1;
753                 //l_current_pi->dx = l_dx_min;
754                 //l_current_pi->dy = l_dy_min;
755                 l_current_pi->step_p = l_step_p;
756                 l_current_pi->step_c = l_step_c;
757                 l_current_pi->step_r = l_step_r;
758                 l_current_pi->step_l = l_step_l;
759
760                 /* allocation for components and number of components has already been calculated by pi_create */
761                 for
762                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
763                 {
764                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
765                         l_encoding_value_ptr = l_tmp_ptr[compno];
766
767                         l_current_comp->dx = l_img_comp->dx;
768                         l_current_comp->dy = l_img_comp->dy;
769                         /* resolutions have already been initialized */
770                         for
771                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
772                         {
773                                 l_res->pdx = *(l_encoding_value_ptr++);
774                                 l_res->pdy = *(l_encoding_value_ptr++);
775                                 l_res->pw =  *(l_encoding_value_ptr++);
776                                 l_res->ph =  *(l_encoding_value_ptr++);
777                                 ++l_res;
778                         }
779                         ++l_current_comp;
780                         ++l_img_comp;
781                         ++l_tccp;
782                 }
783                 // special treatment
784                 l_current_pi->include = (l_current_pi-1)->include;
785                 ++l_current_pi;
786         }
787         opj_free(l_tmp_data);
788         l_tmp_data = 00;
789         opj_free(l_tmp_ptr);
790         l_tmp_ptr = 00;
791         if
792                 (l_tcp->POC)
793         {
794                 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
795         }
796         else
797         {
798                 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
799         }
800         return l_pi;
801 }
802
803 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ 
804         int p, q, pino;
805         int compno, resno;
806         int maxres = 0;
807         int maxprec = 0;
808         opj_pi_iterator_t *pi = NULL;
809         opj_tcp_t *tcp = NULL;
810         opj_tccp_t *tccp = NULL;
811         
812         tcp = &cp->tcps[tileno];
813
814         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
815         if(!pi) {       return NULL;}
816         pi->tp_on = cp->tp_on;
817
818         for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
819                 p = tileno % cp->tw;
820                 q = tileno / cp->tw;
821
822                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
823                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
824                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
825                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
826                 pi[pino].numcomps = image->numcomps;
827                 
828                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
829                 if(!pi[pino].comps) {
830                         pi_destroy(pi, cp, tileno);
831                         return NULL;
832                 }
833                 
834                 for (compno = 0; compno < pi[pino].numcomps; compno++) {
835                         int tcx0, tcy0, tcx1, tcy1;
836                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
837                         tccp = &tcp->tccps[compno];
838                         comp->dx = image->comps[compno].dx;
839                         comp->dy = image->comps[compno].dy;
840                         comp->numresolutions = tccp->numresolutions;
841
842                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
843                         if(!comp->resolutions) {
844                                 pi_destroy(pi, cp, tileno);
845                                 return NULL;
846                         }
847
848                         tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
849                         tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
850                         tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
851                         tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
852                         if (comp->numresolutions > maxres) {
853                                 maxres = comp->numresolutions;
854                         }
855
856                         for (resno = 0; resno < comp->numresolutions; resno++) {
857                                 int levelno;
858                                 int rx0, ry0, rx1, ry1;
859                                 int px0, py0, px1, py1;
860                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
861                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
862                                         res->pdx = tccp->prcw[resno];
863                                         res->pdy = tccp->prch[resno];
864                                 } else {
865                                         res->pdx = 15;
866                                         res->pdy = 15;
867                                 }
868                                 levelno = comp->numresolutions - 1 - resno;
869                                 rx0 = int_ceildivpow2(tcx0, levelno);
870                                 ry0 = int_ceildivpow2(tcy0, levelno);
871                                 rx1 = int_ceildivpow2(tcx1, levelno);
872                                 ry1 = int_ceildivpow2(tcy1, levelno);
873                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
874                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
875                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
876                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
877                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
878                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
879
880                                 if (res->pw*res->ph > maxprec) {
881                                         maxprec = res->pw * res->ph;
882                                 }
883                         }
884                 }
885                 
886                 tccp = &tcp->tccps[0];
887                 pi[pino].step_p = 1;
888                 pi[pino].step_c = maxprec * pi[pino].step_p;
889                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
890                 pi[pino].step_l = maxres * pi[pino].step_r;
891                 
892                 for (compno = 0; compno < pi->numcomps; compno++) {
893                         opj_pi_comp_t *comp = &pi->comps[compno];
894                         for (resno = 0; resno < comp->numresolutions; resno++) {
895                                 int dx, dy;
896                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
897                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
898                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
899                                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
900                                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
901                         }
902                 }
903
904                 if (pino == 0) {
905                         pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
906                         if(!pi[pino].include) {
907                                 pi_destroy(pi, cp, tileno);
908                                 return NULL;
909                         }
910                 }
911                 else {
912                         pi[pino].include = pi[pino - 1].include;
913                 }
914                 
915                 /* Generation of boundaries for each prog flag*/
916                         if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
917                                 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
918                                 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
919                                 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
920                                 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
921                                 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
922                                 tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
923                                 if (pino > 0)
924                                         tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
925                         }else {
926                                 tcp->pocs[pino].compS= 0;
927                                 tcp->pocs[pino].compE= image->numcomps;
928                                 tcp->pocs[pino].resS = 0;
929                                 tcp->pocs[pino].resE = maxres;
930                                 tcp->pocs[pino].layS = 0;
931                                 tcp->pocs[pino].layE = tcp->numlayers;
932                                 tcp->pocs[pino].prg  = tcp->prg;
933                         }
934                         tcp->pocs[pino].prcS = 0;
935                         tcp->pocs[pino].prcE = maxprec;;
936                         tcp->pocs[pino].txS = pi[pino].tx0;
937                         tcp->pocs[pino].txE = pi[pino].tx1;
938                         tcp->pocs[pino].tyS = pi[pino].ty0;
939                         tcp->pocs[pino].tyE = pi[pino].ty1;
940                         tcp->pocs[pino].dx = pi[pino].dx;
941                         tcp->pocs[pino].dy = pi[pino].dy;
942                 }
943                         return pi;
944         }
945
946
947
948 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
949         int compno, pino;
950         opj_tcp_t *tcp = &cp->tcps[tileno];
951         if(pi) {
952                 for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
953                         if(pi[pino].comps) {
954                                 for (compno = 0; compno < pi->numcomps; compno++) {
955                                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
956                                         if(comp->resolutions) {
957                                                 opj_free(comp->resolutions);
958                                         }
959                                 }
960                                 opj_free(pi[pino].comps);
961                         }
962                 }
963                 if(pi->include) {
964                         opj_free(pi->include);
965                 }
966                 opj_free(pi);
967         }
968 }
969
970 opj_bool pi_next(opj_pi_iterator_t * pi) {
971         switch (pi->poc.prg) {
972                 case LRCP:
973                         return pi_next_lrcp(pi);
974                 case RLCP:
975                         return pi_next_rlcp(pi);
976                 case RPCL:
977                         return pi_next_rpcl(pi);
978                 case PCRL:
979                         return pi_next_pcrl(pi);
980                 case CPRL:
981                         return pi_next_cprl(pi);
982                 case PROG_UNKNOWN:
983                         return OPJ_FALSE;
984         }
985         
986         return OPJ_FALSE;
987 }
988
989 opj_bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp){
990         char prog[4];
991         int i;
992         int incr_top=1,resetX=0;
993         opj_tcp_t *tcps =&cp->tcps[tileno];
994         opj_poc_t *tcp= &tcps->pocs[pino];
995
996         pi[pino].first = 1;
997         pi[pino].poc.prg = tcp->prg;
998
999         switch(tcp->prg){
1000                 case CPRL: strncpy(prog, "CPRL",4);
1001                         break;
1002                 case LRCP: strncpy(prog, "LRCP",4);
1003                         break;
1004                 case PCRL: strncpy(prog, "PCRL",4);
1005                         break;
1006                 case RLCP: strncpy(prog, "RLCP",4);
1007                         break;
1008                 case RPCL: strncpy(prog, "RPCL",4);
1009                         break;
1010                 case PROG_UNKNOWN: 
1011                         return OPJ_TRUE;
1012         }
1013
1014         if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1015                 pi[pino].poc.resno0 = tcp->resS;
1016                 pi[pino].poc.resno1 = tcp->resE;
1017                 pi[pino].poc.compno0 = tcp->compS;
1018                 pi[pino].poc.compno1 = tcp->compE;
1019                 pi[pino].poc.layno0 = tcp->layS;
1020                 pi[pino].poc.layno1 = tcp->layE;
1021                 pi[pino].poc.precno0 = tcp->prcS;
1022                 pi[pino].poc.precno1 = tcp->prcE;
1023                 pi[pino].poc.tx0 = tcp->txS;
1024                 pi[pino].poc.ty0 = tcp->tyS;
1025                 pi[pino].poc.tx1 = tcp->txE;
1026                 pi[pino].poc.ty1 = tcp->tyE;
1027         }else {
1028                 if( tpnum < cur_totnum_tp){
1029                         for(i=3;i>=0;i--){
1030                                 switch(prog[i]){
1031                                 case 'C':
1032                                         if (i > tppos){
1033                                                 pi[pino].poc.compno0 = tcp->compS;
1034                                                 pi[pino].poc.compno1 = tcp->compE;
1035                                         }else{
1036                                                 if (tpnum == 0){
1037                                                         tcp->comp_t = tcp->compS;
1038                                                         pi[pino].poc.compno0 = tcp->comp_t;
1039                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1040                                                         tcp->comp_t+=1;
1041                                                 }else{
1042                                                         if (incr_top == 1){
1043                                                                 if(tcp->comp_t ==tcp->compE){
1044                                                                         tcp->comp_t = tcp->compS;
1045                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1046                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1047                                                                         tcp->comp_t+=1;
1048                                                                         incr_top=1;
1049                                                                 }else{
1050                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1051                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1052                                                                         tcp->comp_t+=1;
1053                                                                         incr_top=0;
1054                                                                 }
1055                                                         }else{
1056                                                                 pi[pino].poc.compno0 = tcp->comp_t-1;
1057                                                                 pi[pino].poc.compno1 = tcp->comp_t;
1058                                                         }
1059                                                 }
1060                                         }
1061                                         break;
1062
1063                                 case 'R':
1064                                         if (i > tppos){
1065                                                 pi[pino].poc.resno0 = tcp->resS;
1066                                                 pi[pino].poc.resno1 = tcp->resE;
1067                                         }else{
1068                                                 if (tpnum == 0){
1069                                                         tcp->res_t = tcp->resS;
1070                                                         pi[pino].poc.resno0 = tcp->res_t;
1071                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1072                                                         tcp->res_t+=1;
1073                                                 }else{
1074                                                         if (incr_top == 1){
1075                                                                 if(tcp->res_t==tcp->resE){
1076                                                                         tcp->res_t = tcp->resS;
1077                                                                         pi[pino].poc.resno0 = tcp->res_t;
1078                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1079                                                                         tcp->res_t+=1;
1080                                                                         incr_top=1;
1081                                                                 }else{
1082                                                                         pi[pino].poc.resno0 = tcp->res_t;
1083                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1084                                                                         tcp->res_t+=1;
1085                                                                         incr_top=0;
1086                                                                 }
1087                                                         }else{
1088                                                                 pi[pino].poc.resno0 = tcp->res_t - 1;
1089                                                                 pi[pino].poc.resno1 = tcp->res_t;
1090                                                         }
1091                                                 }
1092                                         }
1093                                         break;
1094
1095                                 case 'L':
1096                                         if (i > tppos){
1097                                                 pi[pino].poc.layno0 = tcp->layS;
1098                                                 pi[pino].poc.layno1 = tcp->layE;
1099                                         }else{
1100                                                 if (tpnum == 0){
1101                                                         tcp->lay_t = tcp->layS;
1102                                                         pi[pino].poc.layno0 = tcp->lay_t;
1103                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1104                                                         tcp->lay_t+=1;
1105                                                 }else{
1106                                                         if (incr_top == 1){
1107                                                                 if(tcp->lay_t == tcp->layE){
1108                                                                         tcp->lay_t = tcp->layS;
1109                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1110                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1111                                                                         tcp->lay_t+=1;
1112                                                                         incr_top=1;
1113                                                                 }else{
1114                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1115                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1116                                                                         tcp->lay_t+=1;
1117                                                                         incr_top=0;
1118                                                                 }
1119                                                         }else{
1120                                                                 pi[pino].poc.layno0 = tcp->lay_t - 1;
1121                                                                 pi[pino].poc.layno1 = tcp->lay_t;
1122                                                         }
1123                                                 }
1124                                         }
1125                                         break;
1126
1127                                 case 'P':
1128                                         switch(tcp->prg){
1129                                                 case LRCP:
1130                                                 case RLCP:
1131                                                         if (i > tppos){
1132                                                                 pi[pino].poc.precno0 = tcp->prcS;
1133                                                                 pi[pino].poc.precno1 = tcp->prcE;
1134                                                         }else{
1135                                                                 if (tpnum == 0){
1136                                                                         tcp->prc_t = tcp->prcS;
1137                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1138                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1139                                                                         tcp->prc_t+=1; 
1140                                                                 }else{
1141                                                                         if (incr_top == 1){
1142                                                                                 if(tcp->prc_t == tcp->prcE){
1143                                                                                         tcp->prc_t = tcp->prcS;
1144                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1145                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1146                                                                                         tcp->prc_t+=1;
1147                                                                                         incr_top=1;
1148                                                                                 }else{
1149                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1150                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1151                                                                                         tcp->prc_t+=1;
1152                                                                                         incr_top=0;
1153                                                                                 }
1154                                                                         }else{
1155                                                                                 pi[pino].poc.precno0 = tcp->prc_t - 1;
1156                                                                                 pi[pino].poc.precno1 = tcp->prc_t;
1157                                                                         }
1158                                                                 }
1159                                                         }
1160                                                 break;
1161                                                 default:
1162                                                         if (i > tppos){
1163                                                                 pi[pino].poc.tx0 = tcp->txS;
1164                                                                 pi[pino].poc.ty0 = tcp->tyS;
1165                                                                 pi[pino].poc.tx1 = tcp->txE;
1166                                                                 pi[pino].poc.ty1 = tcp->tyE;
1167                                                         }else{
1168                                                                 if (tpnum == 0){
1169                                                                         tcp->tx0_t = tcp->txS;
1170                                                                         tcp->ty0_t = tcp->tyS;
1171                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1172                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1173                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1174                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1175                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1176                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1177                                                                 }else{
1178                                                                         if (incr_top == 1){
1179                                                                                 if(tcp->tx0_t >= tcp->txE){
1180                                                                                         if(tcp->ty0_t >= tcp->tyE){
1181                                                                                                 tcp->ty0_t = tcp->tyS;
1182                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1183                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1184                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1185                                                                                                 incr_top=1;resetX=1;
1186                                                                                         }else{
1187                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1188                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1189                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1190                                                                                                 incr_top=0;resetX=1;
1191                                                                                         }
1192                                                                                         if(resetX==1){
1193                                                                                                 tcp->tx0_t = tcp->txS;
1194                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1195                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1196                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1197                                                                                         }
1198                                                                                 }else{
1199                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1200                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1201                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1202                                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1203                                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1204                                                                                         incr_top=0;
1205                                                                                 }
1206                                                                         }else{
1207                                                                                 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1208                                                                                 pi[pino].poc.tx1 = tcp->tx0_t ;
1209                                                                                 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1210                                                                                 pi[pino].poc.ty1 = tcp->ty0_t ;
1211                                                                         }
1212                                                                 }
1213                                                         }
1214                                                 break;
1215                                                 }
1216                                                 break;
1217                                 }               
1218                         } 
1219                 }
1220         }       
1221         return OPJ_FALSE;
1222 }
1223
1224
1225
1226 /**
1227  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1228  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1229  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1230  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1231  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1232  *
1233  * @param       p_image                 the image being encoded.
1234  * @param       p_cp                    the coding parameters.
1235  * @param       tileno                  the tile index of the tile being encoded.
1236  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
1237  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
1238  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
1239  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
1240  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
1241  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1242  * @param       dx_min                  pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1243  * @param       dy_min                  pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1244  * @param       p_resolutions   pointer to an area corresponding to the one described above.
1245  */
1246 void get_all_encoding_parameters(
1247                                                                 const opj_image_t *p_image,
1248                                                                 const opj_cp_v2_t *p_cp,
1249                                                                 OPJ_UINT32 tileno,
1250                                                                 OPJ_INT32 * p_tx0,
1251                                                                 OPJ_INT32 * p_tx1,
1252                                                                 OPJ_INT32 * p_ty0,
1253                                                                 OPJ_INT32 * p_ty1,
1254                                                                 OPJ_UINT32 * p_dx_min,
1255                                                                 OPJ_UINT32 * p_dy_min,
1256                                                                 OPJ_UINT32 * p_max_prec,
1257                                                                 OPJ_UINT32 * p_max_res,
1258                                                                 OPJ_UINT32 ** p_resolutions
1259                                                         )
1260 {
1261         // loop
1262         OPJ_UINT32 compno, resno;
1263
1264         // pointers
1265         const opj_tcp_v2_t *tcp = 00;
1266         const opj_tccp_t * l_tccp = 00;
1267         const opj_image_comp_t * l_img_comp = 00;
1268
1269         // to store l_dx, l_dy, w and h for each resolution and component.
1270         OPJ_UINT32 * lResolutionPtr;
1271
1272         // position in x and y of tile
1273         OPJ_UINT32 p, q;
1274
1275         // preconditions in debug
1276         assert(p_cp != 00);
1277         assert(p_image != 00);
1278         assert(tileno < p_cp->tw * p_cp->th);
1279
1280         // initializations
1281         tcp = &p_cp->tcps [tileno];
1282         l_tccp = tcp->tccps;
1283         l_img_comp = p_image->comps;
1284
1285         // position in x and y of tile
1286
1287         p = tileno % p_cp->tw;
1288         q = tileno / p_cp->tw;
1289
1290         /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1291         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1292         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1293         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1294         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1295
1296         // max precision and resolution is 0 (can only grow)
1297         *p_max_prec = 0;
1298         *p_max_res = 0;
1299
1300         // take the largest value for dx_min and dy_min
1301         *p_dx_min = 0x7fffffff;
1302         *p_dy_min  = 0x7fffffff;
1303
1304         for
1305                 (compno = 0; compno < p_image->numcomps; ++compno)
1306         {
1307                 // aritmetic variables to calculate
1308                 OPJ_UINT32 l_level_no;
1309                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1310                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1311                 OPJ_UINT32 l_product;
1312                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1313                 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1314
1315                 lResolutionPtr = p_resolutions[compno];
1316
1317                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1318                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1319                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1320                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1321                 if
1322                         (l_tccp->numresolutions > *p_max_res)
1323                 {
1324                         *p_max_res = l_tccp->numresolutions;
1325                 }
1326
1327                 // use custom size for precincts
1328                 l_level_no = l_tccp->numresolutions - 1;
1329                 for
1330                         (resno = 0; resno < l_tccp->numresolutions; ++resno)
1331                 {
1332                         OPJ_UINT32 l_dx, l_dy;
1333                         // precinct width and height
1334                         l_pdx = l_tccp->prcw[resno];
1335                         l_pdy = l_tccp->prch[resno];
1336                         *lResolutionPtr++ = l_pdx;
1337                         *lResolutionPtr++ = l_pdy;
1338                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1339                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1340                         // take the minimum size for l_dx for each comp and resolution
1341                         *p_dx_min = int_min(*p_dx_min, l_dx);
1342                         *p_dy_min = int_min(*p_dy_min, l_dy);
1343                         // various calculations of extents
1344
1345                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1346                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1347                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1348                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1349                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1350                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1351                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1352                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1353                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1354                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1355                         *lResolutionPtr++ = l_pw;
1356                         *lResolutionPtr++ = l_ph;
1357                         l_product = l_pw * l_ph;
1358                         // update precision
1359                         if
1360                                 (l_product > *p_max_prec)
1361                         {
1362                                 *p_max_prec = l_product;
1363                         }
1364                         --l_level_no;
1365                 }
1366                 ++l_tccp;
1367                 ++l_img_comp;
1368         }
1369 }
1370
1371 /**
1372  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1373  * No other data is set. The include section of the packet  iterator is not allocated.
1374  *
1375  * @param       p_image         the image used to initialize the packet iterator (in fact only the number of components is relevant.
1376  * @param       p_cp            the coding parameters.
1377  * @param       p_tile_no       the index of the tile from which creating the packet iterator.
1378  */
1379 opj_pi_iterator_t * pi_create(
1380                                                                 const opj_image_t *image,
1381                                                                 const opj_cp_v2_t *cp,
1382                                                                 OPJ_UINT32 tileno
1383                                                         )
1384 {
1385         // loop
1386         OPJ_UINT32 pino, compno;
1387         // number of poc in the p_pi
1388         OPJ_UINT32 l_poc_bound;
1389
1390         // pointers to tile coding parameters and components.
1391         opj_pi_iterator_t *l_pi = 00;
1392         opj_tcp_v2_t *tcp = 00;
1393         const opj_tccp_t *tccp = 00;
1394
1395         // current packet iterator being allocated
1396         opj_pi_iterator_t *l_current_pi = 00;
1397
1398         // preconditions in debug
1399         assert(cp != 00);
1400         assert(image != 00);
1401         assert(tileno < cp->tw * cp->th);
1402
1403         // initializations
1404         tcp = &cp->tcps[tileno];
1405         l_poc_bound = tcp->numpocs+1;
1406
1407
1408         // memory allocations
1409         l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1410
1411         if
1412                 (!l_pi)
1413         {
1414                 return 00;
1415         }
1416         memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1417         l_current_pi = l_pi;
1418         for
1419                 (pino = 0; pino < l_poc_bound ; ++pino)
1420         {
1421                 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1422                 if
1423                         (! l_current_pi->comps)
1424                 {
1425                         pi_destroy_v2(l_pi, l_poc_bound);
1426                         return 00;
1427                 }
1428                 l_current_pi->numcomps = image->numcomps;
1429                 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1430                 for
1431                         (compno = 0; compno < image->numcomps; ++compno)
1432                 {
1433                         opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1434                         tccp = &tcp->tccps[compno];
1435                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1436                         if
1437                                 (!comp->resolutions)
1438                         {
1439                                 pi_destroy_v2(l_pi, l_poc_bound);
1440                                 return 00;
1441                         }
1442                         comp->numresolutions = tccp->numresolutions;
1443                         memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1444                 }
1445                 ++l_current_pi;
1446         }
1447         return l_pi;
1448 }
1449
1450
1451
1452 /**
1453  * Destroys a packet iterator array.
1454  *
1455  * @param       p_pi                    the packet iterator array to destroy.
1456  * @param       p_nb_elements   the number of elements in the array.
1457  */
1458 void pi_destroy_v2(
1459                                 opj_pi_iterator_t *p_pi,
1460                                 OPJ_UINT32 p_nb_elements)
1461 {
1462         OPJ_UINT32 compno, pino;
1463         opj_pi_iterator_t *l_current_pi = p_pi;
1464         if
1465                 (p_pi)
1466         {
1467                 if
1468                         (p_pi->include)
1469                 {
1470                         opj_free(p_pi->include);
1471                         p_pi->include = 00;
1472                 }
1473                 // TODO
1474                 for
1475                         (pino = 0; pino < p_nb_elements; ++pino)
1476                 {
1477                         if
1478                                 (l_current_pi->comps)
1479                         {
1480                                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1481                                 for
1482                                         (compno = 0; compno < l_current_pi->numcomps; compno++)
1483                                 {
1484                                         if
1485                                                 (l_current_component->resolutions)
1486                                         {
1487                                                 opj_free(l_current_component->resolutions);
1488                                                 l_current_component->resolutions = 00;
1489                                         }
1490                                         ++l_current_component;
1491                                 }
1492                                 opj_free(l_current_pi->comps);
1493                                 l_current_pi->comps = 0;
1494                         }
1495                         ++l_current_pi;
1496                 }
1497                 opj_free(p_pi);
1498         }
1499 }
1500
1501
1502
1503 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
1504 {
1505         // loop
1506         OPJ_UINT32 pino;
1507
1508         // encoding prameters to set
1509         OPJ_UINT32 l_bound;
1510
1511         opj_pi_iterator_t * l_current_pi = 00;
1512         opj_poc_t* l_current_poc = 0;
1513
1514         // preconditions in debug
1515         assert(p_pi != 00);
1516         assert(p_tcp != 00);
1517
1518         // initializations
1519         l_bound = p_tcp->numpocs+1;
1520         l_current_pi = p_pi;
1521         l_current_poc = p_tcp->pocs;
1522
1523         for
1524                 (pino = 0;pino<l_bound;++pino)
1525         {
1526                 l_current_pi->poc.prg = l_current_poc->prg;
1527                 l_current_pi->first = 1;
1528
1529                 l_current_pi->poc.resno0 = l_current_poc->resno0;
1530                 l_current_pi->poc.compno0 = l_current_poc->compno0;
1531                 l_current_pi->poc.layno0 = 0;
1532                 l_current_pi->poc.precno0 = 0;
1533                 l_current_pi->poc.resno1 = l_current_poc->resno1;
1534                 l_current_pi->poc.compno1 = l_current_poc->compno1;
1535                 l_current_pi->poc.layno1 = l_current_poc->layno1;
1536                 l_current_pi->poc.precno1 = p_max_precision;
1537                 ++l_current_pi;
1538                 ++l_current_poc;
1539         }
1540 }
1541
1542
1543 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
1544 {
1545         // loop
1546         OPJ_UINT32 pino;
1547
1548         // encoding prameters to set
1549         OPJ_UINT32 l_bound;
1550
1551         opj_pi_iterator_t * l_current_pi = 00;
1552         // preconditions in debug
1553         assert(p_tcp != 00);
1554         assert(p_pi != 00);
1555
1556         // initializations
1557         l_bound = p_tcp->numpocs+1;
1558         l_current_pi = p_pi;
1559
1560         for
1561                 (pino = 0;pino<l_bound;++pino)
1562         {
1563                 l_current_pi->poc.prg = p_tcp->prg;
1564                 l_current_pi->first = 1;
1565                 l_current_pi->poc.resno0 = 0;
1566                 l_current_pi->poc.compno0 = 0;
1567                 l_current_pi->poc.layno0 = 0;
1568                 l_current_pi->poc.precno0 = 0;
1569                 l_current_pi->poc.resno1 = p_max_res;
1570                 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1571                 l_current_pi->poc.layno1 = p_tcp->numlayers;
1572                 l_current_pi->poc.precno1 = p_max_precision;
1573                 ++l_current_pi;
1574         }
1575 }