f7942afcfb38e81b3e38264492247cbefd1454c3
[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_header_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_header_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_header_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_header_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 (!l_pi) {
660                 opj_free(l_tmp_data);
661                 opj_free(l_tmp_ptr);
662                 return 00;
663         }
664
665         l_encoding_value_ptr = l_tmp_data;
666         // update pointer array
667         for
668                 (compno = 0; compno < p_image->numcomps; ++compno)
669         {
670                 l_tmp_ptr[compno] = l_encoding_value_ptr;
671                 l_encoding_value_ptr += l_data_stride;
672         }
673         // get encoding parameters
674         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);
675
676         // step calculations
677         l_step_p = 1;
678         l_step_c = l_max_prec * l_step_p;
679         l_step_r = p_image->numcomps * l_step_c;
680         l_step_l = l_max_res * l_step_r;
681
682         // set values for first packet iterator
683         l_current_pi = l_pi;
684
685         // memory allocation for include
686         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
687         if
688                 (!l_current_pi->include)
689         {
690                 opj_free(l_tmp_data);
691                 opj_free(l_tmp_ptr);
692                 pi_destroy_v2(l_pi, l_bound);
693                 return 00;
694         }
695         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
696
697         // special treatment for the first packet iterator
698         l_current_comp = l_current_pi->comps;
699         l_img_comp = p_image->comps;
700         l_tccp = l_tcp->tccps;
701
702         l_current_pi->tx0 = l_tx0;
703         l_current_pi->ty0 = l_ty0;
704         l_current_pi->tx1 = l_tx1;
705         l_current_pi->ty1 = l_ty1;
706
707         //l_current_pi->dx = l_img_comp->dx;
708         //l_current_pi->dy = l_img_comp->dy;
709
710         l_current_pi->step_p = l_step_p;
711         l_current_pi->step_c = l_step_c;
712         l_current_pi->step_r = l_step_r;
713         l_current_pi->step_l = l_step_l;
714
715         /* allocation for components and number of components has already been calculated by pi_create */
716         for
717                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
718         {
719                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
720                 l_encoding_value_ptr = l_tmp_ptr[compno];
721
722                 l_current_comp->dx = l_img_comp->dx;
723                 l_current_comp->dy = l_img_comp->dy;
724                 /* resolutions have already been initialized */
725                 for
726                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
727                 {
728                         l_res->pdx = *(l_encoding_value_ptr++);
729                         l_res->pdy = *(l_encoding_value_ptr++);
730                         l_res->pw =  *(l_encoding_value_ptr++);
731                         l_res->ph =  *(l_encoding_value_ptr++);
732                         ++l_res;
733                 }
734                 ++l_current_comp;
735                 ++l_img_comp;
736                 ++l_tccp;
737         }
738         ++l_current_pi;
739
740         for
741                 (pino = 1 ; pino<l_bound ; ++pino )
742         {
743                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
744                 opj_image_comp_header_t * l_img_comp = p_image->comps;
745                 l_tccp = l_tcp->tccps;
746
747                 l_current_pi->tx0 = l_tx0;
748                 l_current_pi->ty0 = l_ty0;
749                 l_current_pi->tx1 = l_tx1;
750                 l_current_pi->ty1 = l_ty1;
751                 //l_current_pi->dx = l_dx_min;
752                 //l_current_pi->dy = l_dy_min;
753                 l_current_pi->step_p = l_step_p;
754                 l_current_pi->step_c = l_step_c;
755                 l_current_pi->step_r = l_step_r;
756                 l_current_pi->step_l = l_step_l;
757
758                 /* allocation for components and number of components has already been calculated by pi_create */
759                 for
760                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
761                 {
762                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
763                         l_encoding_value_ptr = l_tmp_ptr[compno];
764
765                         l_current_comp->dx = l_img_comp->dx;
766                         l_current_comp->dy = l_img_comp->dy;
767                         /* resolutions have already been initialized */
768                         for
769                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
770                         {
771                                 l_res->pdx = *(l_encoding_value_ptr++);
772                                 l_res->pdy = *(l_encoding_value_ptr++);
773                                 l_res->pw =  *(l_encoding_value_ptr++);
774                                 l_res->ph =  *(l_encoding_value_ptr++);
775                                 ++l_res;
776                         }
777                         ++l_current_comp;
778                         ++l_img_comp;
779                         ++l_tccp;
780                 }
781                 // special treatment
782                 l_current_pi->include = (l_current_pi-1)->include;
783                 ++l_current_pi;
784         }
785         opj_free(l_tmp_data);
786         l_tmp_data = 00;
787         opj_free(l_tmp_ptr);
788         l_tmp_ptr = 00;
789         if
790                 (l_tcp->POC)
791         {
792                 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
793         }
794         else
795         {
796                 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
797         }
798         return l_pi;
799 }
800
801 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ 
802         int p, q, pino;
803         int compno, resno;
804         int maxres = 0;
805         int maxprec = 0;
806         opj_pi_iterator_t *pi = NULL;
807         opj_tcp_t *tcp = NULL;
808         opj_tccp_t *tccp = NULL;
809         
810         tcp = &cp->tcps[tileno];
811
812         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
813         if(!pi) {       return NULL;}
814         pi->tp_on = cp->tp_on;
815
816         for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
817                 p = tileno % cp->tw;
818                 q = tileno / cp->tw;
819
820                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
821                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
822                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
823                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
824                 pi[pino].numcomps = image->numcomps;
825                 
826                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
827                 if(!pi[pino].comps) {
828                         pi_destroy(pi, cp, tileno);
829                         return NULL;
830                 }
831                 
832                 for (compno = 0; compno < pi[pino].numcomps; compno++) {
833                         int tcx0, tcy0, tcx1, tcy1;
834                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
835                         tccp = &tcp->tccps[compno];
836                         comp->dx = image->comps[compno].dx;
837                         comp->dy = image->comps[compno].dy;
838                         comp->numresolutions = tccp->numresolutions;
839
840                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
841                         if(!comp->resolutions) {
842                                 pi_destroy(pi, cp, tileno);
843                                 return NULL;
844                         }
845
846                         tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
847                         tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
848                         tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
849                         tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
850                         if (comp->numresolutions > maxres) {
851                                 maxres = comp->numresolutions;
852                         }
853
854                         for (resno = 0; resno < comp->numresolutions; resno++) {
855                                 int levelno;
856                                 int rx0, ry0, rx1, ry1;
857                                 int px0, py0, px1, py1;
858                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
859                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
860                                         res->pdx = tccp->prcw[resno];
861                                         res->pdy = tccp->prch[resno];
862                                 } else {
863                                         res->pdx = 15;
864                                         res->pdy = 15;
865                                 }
866                                 levelno = comp->numresolutions - 1 - resno;
867                                 rx0 = int_ceildivpow2(tcx0, levelno);
868                                 ry0 = int_ceildivpow2(tcy0, levelno);
869                                 rx1 = int_ceildivpow2(tcx1, levelno);
870                                 ry1 = int_ceildivpow2(tcy1, levelno);
871                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
872                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
873                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
874                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
875                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
876                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
877
878                                 if (res->pw*res->ph > maxprec) {
879                                         maxprec = res->pw * res->ph;
880                                 }
881                         }
882                 }
883                 
884                 tccp = &tcp->tccps[0];
885                 pi[pino].step_p = 1;
886                 pi[pino].step_c = maxprec * pi[pino].step_p;
887                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
888                 pi[pino].step_l = maxres * pi[pino].step_r;
889                 
890                 for (compno = 0; compno < pi->numcomps; compno++) {
891                         opj_pi_comp_t *comp = &pi->comps[compno];
892                         for (resno = 0; resno < comp->numresolutions; resno++) {
893                                 int dx, dy;
894                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
895                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
896                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
897                                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
898                                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
899                         }
900                 }
901
902                 if (pino == 0) {
903                         pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
904                         if(!pi[pino].include) {
905                                 pi_destroy(pi, cp, tileno);
906                                 return NULL;
907                         }
908                 }
909                 else {
910                         pi[pino].include = pi[pino - 1].include;
911                 }
912                 
913                 /* Generation of boundaries for each prog flag*/
914                         if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
915                                 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
916                                 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
917                                 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
918                                 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
919                                 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
920                                 tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
921                                 if (pino > 0)
922                                         tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
923                         }else {
924                                 tcp->pocs[pino].compS= 0;
925                                 tcp->pocs[pino].compE= image->numcomps;
926                                 tcp->pocs[pino].resS = 0;
927                                 tcp->pocs[pino].resE = maxres;
928                                 tcp->pocs[pino].layS = 0;
929                                 tcp->pocs[pino].layE = tcp->numlayers;
930                                 tcp->pocs[pino].prg  = tcp->prg;
931                         }
932                         tcp->pocs[pino].prcS = 0;
933                         tcp->pocs[pino].prcE = maxprec;;
934                         tcp->pocs[pino].txS = pi[pino].tx0;
935                         tcp->pocs[pino].txE = pi[pino].tx1;
936                         tcp->pocs[pino].tyS = pi[pino].ty0;
937                         tcp->pocs[pino].tyE = pi[pino].ty1;
938                         tcp->pocs[pino].dx = pi[pino].dx;
939                         tcp->pocs[pino].dy = pi[pino].dy;
940                 }
941                         return pi;
942         }
943
944
945
946 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
947         int compno, pino;
948         opj_tcp_t *tcp = &cp->tcps[tileno];
949         if(pi) {
950                 for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
951                         if(pi[pino].comps) {
952                                 for (compno = 0; compno < pi->numcomps; compno++) {
953                                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
954                                         if(comp->resolutions) {
955                                                 opj_free(comp->resolutions);
956                                         }
957                                 }
958                                 opj_free(pi[pino].comps);
959                         }
960                 }
961                 if(pi->include) {
962                         opj_free(pi->include);
963                 }
964                 opj_free(pi);
965         }
966 }
967
968 opj_bool pi_next(opj_pi_iterator_t * pi) {
969         switch (pi->poc.prg) {
970                 case LRCP:
971                         return pi_next_lrcp(pi);
972                 case RLCP:
973                         return pi_next_rlcp(pi);
974                 case RPCL:
975                         return pi_next_rpcl(pi);
976                 case PCRL:
977                         return pi_next_pcrl(pi);
978                 case CPRL:
979                         return pi_next_cprl(pi);
980                 case PROG_UNKNOWN:
981                         return OPJ_FALSE;
982         }
983         
984         return OPJ_FALSE;
985 }
986
987 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){
988         char prog[4];
989         int i;
990         int incr_top=1,resetX=0;
991         opj_tcp_t *tcps =&cp->tcps[tileno];
992         opj_poc_t *tcp= &tcps->pocs[pino];
993
994         pi[pino].first = 1;
995         pi[pino].poc.prg = tcp->prg;
996
997         switch(tcp->prg){
998                 case CPRL: strncpy(prog, "CPRL",4);
999                         break;
1000                 case LRCP: strncpy(prog, "LRCP",4);
1001                         break;
1002                 case PCRL: strncpy(prog, "PCRL",4);
1003                         break;
1004                 case RLCP: strncpy(prog, "RLCP",4);
1005                         break;
1006                 case RPCL: strncpy(prog, "RPCL",4);
1007                         break;
1008                 case PROG_UNKNOWN: 
1009                         return OPJ_TRUE;
1010         }
1011
1012         if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1013                 pi[pino].poc.resno0 = tcp->resS;
1014                 pi[pino].poc.resno1 = tcp->resE;
1015                 pi[pino].poc.compno0 = tcp->compS;
1016                 pi[pino].poc.compno1 = tcp->compE;
1017                 pi[pino].poc.layno0 = tcp->layS;
1018                 pi[pino].poc.layno1 = tcp->layE;
1019                 pi[pino].poc.precno0 = tcp->prcS;
1020                 pi[pino].poc.precno1 = tcp->prcE;
1021                 pi[pino].poc.tx0 = tcp->txS;
1022                 pi[pino].poc.ty0 = tcp->tyS;
1023                 pi[pino].poc.tx1 = tcp->txE;
1024                 pi[pino].poc.ty1 = tcp->tyE;
1025         }else {
1026                 if( tpnum < cur_totnum_tp){
1027                         for(i=3;i>=0;i--){
1028                                 switch(prog[i]){
1029                                 case 'C':
1030                                         if (i > tppos){
1031                                                 pi[pino].poc.compno0 = tcp->compS;
1032                                                 pi[pino].poc.compno1 = tcp->compE;
1033                                         }else{
1034                                                 if (tpnum == 0){
1035                                                         tcp->comp_t = tcp->compS;
1036                                                         pi[pino].poc.compno0 = tcp->comp_t;
1037                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1038                                                         tcp->comp_t+=1;
1039                                                 }else{
1040                                                         if (incr_top == 1){
1041                                                                 if(tcp->comp_t ==tcp->compE){
1042                                                                         tcp->comp_t = tcp->compS;
1043                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1044                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1045                                                                         tcp->comp_t+=1;
1046                                                                         incr_top=1;
1047                                                                 }else{
1048                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1049                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1050                                                                         tcp->comp_t+=1;
1051                                                                         incr_top=0;
1052                                                                 }
1053                                                         }else{
1054                                                                 pi[pino].poc.compno0 = tcp->comp_t-1;
1055                                                                 pi[pino].poc.compno1 = tcp->comp_t;
1056                                                         }
1057                                                 }
1058                                         }
1059                                         break;
1060
1061                                 case 'R':
1062                                         if (i > tppos){
1063                                                 pi[pino].poc.resno0 = tcp->resS;
1064                                                 pi[pino].poc.resno1 = tcp->resE;
1065                                         }else{
1066                                                 if (tpnum == 0){
1067                                                         tcp->res_t = tcp->resS;
1068                                                         pi[pino].poc.resno0 = tcp->res_t;
1069                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1070                                                         tcp->res_t+=1;
1071                                                 }else{
1072                                                         if (incr_top == 1){
1073                                                                 if(tcp->res_t==tcp->resE){
1074                                                                         tcp->res_t = tcp->resS;
1075                                                                         pi[pino].poc.resno0 = tcp->res_t;
1076                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1077                                                                         tcp->res_t+=1;
1078                                                                         incr_top=1;
1079                                                                 }else{
1080                                                                         pi[pino].poc.resno0 = tcp->res_t;
1081                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1082                                                                         tcp->res_t+=1;
1083                                                                         incr_top=0;
1084                                                                 }
1085                                                         }else{
1086                                                                 pi[pino].poc.resno0 = tcp->res_t - 1;
1087                                                                 pi[pino].poc.resno1 = tcp->res_t;
1088                                                         }
1089                                                 }
1090                                         }
1091                                         break;
1092
1093                                 case 'L':
1094                                         if (i > tppos){
1095                                                 pi[pino].poc.layno0 = tcp->layS;
1096                                                 pi[pino].poc.layno1 = tcp->layE;
1097                                         }else{
1098                                                 if (tpnum == 0){
1099                                                         tcp->lay_t = tcp->layS;
1100                                                         pi[pino].poc.layno0 = tcp->lay_t;
1101                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1102                                                         tcp->lay_t+=1;
1103                                                 }else{
1104                                                         if (incr_top == 1){
1105                                                                 if(tcp->lay_t == tcp->layE){
1106                                                                         tcp->lay_t = tcp->layS;
1107                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1108                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1109                                                                         tcp->lay_t+=1;
1110                                                                         incr_top=1;
1111                                                                 }else{
1112                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1113                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1114                                                                         tcp->lay_t+=1;
1115                                                                         incr_top=0;
1116                                                                 }
1117                                                         }else{
1118                                                                 pi[pino].poc.layno0 = tcp->lay_t - 1;
1119                                                                 pi[pino].poc.layno1 = tcp->lay_t;
1120                                                         }
1121                                                 }
1122                                         }
1123                                         break;
1124
1125                                 case 'P':
1126                                         switch(tcp->prg){
1127                                                 case LRCP:
1128                                                 case RLCP:
1129                                                         if (i > tppos){
1130                                                                 pi[pino].poc.precno0 = tcp->prcS;
1131                                                                 pi[pino].poc.precno1 = tcp->prcE;
1132                                                         }else{
1133                                                                 if (tpnum == 0){
1134                                                                         tcp->prc_t = tcp->prcS;
1135                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1136                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1137                                                                         tcp->prc_t+=1; 
1138                                                                 }else{
1139                                                                         if (incr_top == 1){
1140                                                                                 if(tcp->prc_t == tcp->prcE){
1141                                                                                         tcp->prc_t = tcp->prcS;
1142                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1143                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1144                                                                                         tcp->prc_t+=1;
1145                                                                                         incr_top=1;
1146                                                                                 }else{
1147                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1148                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1149                                                                                         tcp->prc_t+=1;
1150                                                                                         incr_top=0;
1151                                                                                 }
1152                                                                         }else{
1153                                                                                 pi[pino].poc.precno0 = tcp->prc_t - 1;
1154                                                                                 pi[pino].poc.precno1 = tcp->prc_t;
1155                                                                         }
1156                                                                 }
1157                                                         }
1158                                                 break;
1159                                                 default:
1160                                                         if (i > tppos){
1161                                                                 pi[pino].poc.tx0 = tcp->txS;
1162                                                                 pi[pino].poc.ty0 = tcp->tyS;
1163                                                                 pi[pino].poc.tx1 = tcp->txE;
1164                                                                 pi[pino].poc.ty1 = tcp->tyE;
1165                                                         }else{
1166                                                                 if (tpnum == 0){
1167                                                                         tcp->tx0_t = tcp->txS;
1168                                                                         tcp->ty0_t = tcp->tyS;
1169                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1170                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1171                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1172                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1173                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1174                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1175                                                                 }else{
1176                                                                         if (incr_top == 1){
1177                                                                                 if(tcp->tx0_t >= tcp->txE){
1178                                                                                         if(tcp->ty0_t >= tcp->tyE){
1179                                                                                                 tcp->ty0_t = tcp->tyS;
1180                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1181                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1182                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1183                                                                                                 incr_top=1;resetX=1;
1184                                                                                         }else{
1185                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1186                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1187                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1188                                                                                                 incr_top=0;resetX=1;
1189                                                                                         }
1190                                                                                         if(resetX==1){
1191                                                                                                 tcp->tx0_t = tcp->txS;
1192                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1193                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1194                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1195                                                                                         }
1196                                                                                 }else{
1197                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1198                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1199                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1200                                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1201                                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1202                                                                                         incr_top=0;
1203                                                                                 }
1204                                                                         }else{
1205                                                                                 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1206                                                                                 pi[pino].poc.tx1 = tcp->tx0_t ;
1207                                                                                 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1208                                                                                 pi[pino].poc.ty1 = tcp->ty0_t ;
1209                                                                         }
1210                                                                 }
1211                                                         }
1212                                                 break;
1213                                                 }
1214                                                 break;
1215                                 }               
1216                         } 
1217                 }
1218         }       
1219         return OPJ_FALSE;
1220 }
1221
1222
1223
1224 /**
1225  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1226  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1227  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1228  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1229  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1230  *
1231  * @param       p_image                 the image being encoded.
1232  * @param       p_cp                    the coding parameters.
1233  * @param       tileno                  the tile index of the tile being encoded.
1234  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
1235  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
1236  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
1237  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
1238  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
1239  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1240  * @param       dx_min                  pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1241  * @param       dy_min                  pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1242  * @param       p_resolutions   pointer to an area corresponding to the one described above.
1243  */
1244 void get_all_encoding_parameters(
1245                                                                 const opj_image_header_t *p_image,
1246                                                                 const opj_cp_v2_t *p_cp,
1247                                                                 OPJ_UINT32 tileno,
1248                                                                 OPJ_INT32 * p_tx0,
1249                                                                 OPJ_INT32 * p_tx1,
1250                                                                 OPJ_INT32 * p_ty0,
1251                                                                 OPJ_INT32 * p_ty1,
1252                                                                 OPJ_UINT32 * p_dx_min,
1253                                                                 OPJ_UINT32 * p_dy_min,
1254                                                                 OPJ_UINT32 * p_max_prec,
1255                                                                 OPJ_UINT32 * p_max_res,
1256                                                                 OPJ_UINT32 ** p_resolutions
1257                                                         )
1258 {
1259         // loop
1260         OPJ_UINT32 compno, resno;
1261
1262         // pointers
1263         const opj_tcp_v2_t *tcp = 00;
1264         const opj_tccp_t * l_tccp = 00;
1265         const opj_image_comp_header_t * l_img_comp = 00;
1266
1267         // to store l_dx, l_dy, w and h for each resolution and component.
1268         OPJ_UINT32 * lResolutionPtr;
1269
1270         // position in x and y of tile
1271         OPJ_UINT32 p, q;
1272
1273         // preconditions in debug
1274         assert(p_cp != 00);
1275         assert(p_image != 00);
1276         assert(tileno < p_cp->tw * p_cp->th);
1277
1278         // initializations
1279         tcp = &p_cp->tcps [tileno];
1280         l_tccp = tcp->tccps;
1281         l_img_comp = p_image->comps;
1282
1283         // position in x and y of tile
1284
1285         p = tileno % p_cp->tw;
1286         q = tileno / p_cp->tw;
1287
1288         /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1289         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1290         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1291         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1292         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1293
1294         // max precision and resolution is 0 (can only grow)
1295         *p_max_prec = 0;
1296         *p_max_res = 0;
1297
1298         // take the largest value for dx_min and dy_min
1299         *p_dx_min = 0x7fffffff;
1300         *p_dy_min  = 0x7fffffff;
1301
1302         for
1303                 (compno = 0; compno < p_image->numcomps; ++compno)
1304         {
1305                 // aritmetic variables to calculate
1306                 OPJ_UINT32 l_level_no;
1307                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1308                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1309                 OPJ_UINT32 l_product;
1310                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1311                 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1312
1313                 lResolutionPtr = p_resolutions[compno];
1314
1315                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1316                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1317                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1318                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1319                 if
1320                         (l_tccp->numresolutions > *p_max_res)
1321                 {
1322                         *p_max_res = l_tccp->numresolutions;
1323                 }
1324
1325                 // use custom size for precincts
1326                 l_level_no = l_tccp->numresolutions - 1;
1327                 for
1328                         (resno = 0; resno < l_tccp->numresolutions; ++resno)
1329                 {
1330                         OPJ_UINT32 l_dx, l_dy;
1331                         // precinct width and height
1332                         l_pdx = l_tccp->prcw[resno];
1333                         l_pdy = l_tccp->prch[resno];
1334                         *lResolutionPtr++ = l_pdx;
1335                         *lResolutionPtr++ = l_pdy;
1336                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1337                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1338                         // take the minimum size for l_dx for each comp and resolution
1339                         *p_dx_min = int_min(*p_dx_min, l_dx);
1340                         *p_dy_min = int_min(*p_dy_min, l_dy);
1341                         // various calculations of extents
1342
1343                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1344                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1345                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1346                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1347                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1348                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1349                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1350                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1351                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1352                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1353                         *lResolutionPtr++ = l_pw;
1354                         *lResolutionPtr++ = l_ph;
1355                         l_product = l_pw * l_ph;
1356                         // update precision
1357                         if
1358                                 (l_product > *p_max_prec)
1359                         {
1360                                 *p_max_prec = l_product;
1361                         }
1362                         --l_level_no;
1363                 }
1364                 ++l_tccp;
1365                 ++l_img_comp;
1366         }
1367 }
1368
1369 /**
1370  * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1371  * No other data is set. The include section of the packet  iterator is not allocated.
1372  *
1373  * @param       p_image         the image used to initialize the packet iterator (in fact only the number of components is relevant.
1374  * @param       p_cp            the coding parameters.
1375  * @param       p_tile_no       the index of the tile from which creating the packet iterator.
1376  */
1377 opj_pi_iterator_t * pi_create(
1378                                                                 const opj_image_header_t *image,
1379                                                                 const opj_cp_v2_t *cp,
1380                                                                 OPJ_UINT32 tileno
1381                                                         )
1382 {
1383         // loop
1384         OPJ_UINT32 pino, compno;
1385         // number of poc in the p_pi
1386         OPJ_UINT32 l_poc_bound;
1387
1388         // pointers to tile coding parameters and components.
1389         opj_pi_iterator_t *l_pi = 00;
1390         opj_tcp_v2_t *tcp = 00;
1391         const opj_tccp_t *tccp = 00;
1392
1393         // current packet iterator being allocated
1394         opj_pi_iterator_t *l_current_pi = 00;
1395
1396         // preconditions in debug
1397         assert(cp != 00);
1398         assert(image != 00);
1399         assert(tileno < cp->tw * cp->th);
1400
1401         // initializations
1402         tcp = &cp->tcps[tileno];
1403         l_poc_bound = tcp->numpocs+1;
1404
1405         // memory allocations
1406         l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1407         if (!l_pi) {
1408                 return NULL;
1409         }
1410         memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1411
1412         l_current_pi = l_pi;
1413         for (pino = 0; pino < l_poc_bound ; ++pino) {
1414
1415                 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1416                 if (! l_current_pi->comps) {
1417                         pi_destroy_v2(l_pi, l_poc_bound);
1418                         return NULL;
1419                 }
1420
1421                 l_current_pi->numcomps = image->numcomps;
1422                 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1423
1424                 for (compno = 0; compno < image->numcomps; ++compno) {
1425                         opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1426
1427                         tccp = &tcp->tccps[compno];
1428
1429                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1430                         if (!comp->resolutions) {
1431                                 pi_destroy_v2(l_pi, l_poc_bound);
1432                                 return 00;
1433                         }
1434
1435                         comp->numresolutions = tccp->numresolutions;
1436                         memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1437                 }
1438                 ++l_current_pi;
1439         }
1440         return l_pi;
1441 }
1442
1443
1444
1445 /**
1446  * Destroys a packet iterator array.
1447  *
1448  * @param       p_pi                    the packet iterator array to destroy.
1449  * @param       p_nb_elements   the number of elements in the array.
1450  */
1451 void pi_destroy_v2(
1452                                 opj_pi_iterator_t *p_pi,
1453                                 OPJ_UINT32 p_nb_elements)
1454 {
1455         OPJ_UINT32 compno, pino;
1456         opj_pi_iterator_t *l_current_pi = p_pi;
1457         if
1458                 (p_pi)
1459         {
1460                 if
1461                         (p_pi->include)
1462                 {
1463                         opj_free(p_pi->include);
1464                         p_pi->include = 00;
1465                 }
1466                 // TODO
1467                 for
1468                         (pino = 0; pino < p_nb_elements; ++pino)
1469                 {
1470                         if
1471                                 (l_current_pi->comps)
1472                         {
1473                                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1474                                 for
1475                                         (compno = 0; compno < l_current_pi->numcomps; compno++)
1476                                 {
1477                                         if
1478                                                 (l_current_component->resolutions)
1479                                         {
1480                                                 opj_free(l_current_component->resolutions);
1481                                                 l_current_component->resolutions = 00;
1482                                         }
1483                                         ++l_current_component;
1484                                 }
1485                                 opj_free(l_current_pi->comps);
1486                                 l_current_pi->comps = 0;
1487                         }
1488                         ++l_current_pi;
1489                 }
1490                 opj_free(p_pi);
1491         }
1492 }
1493
1494
1495
1496 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)
1497 {
1498         // loop
1499         OPJ_UINT32 pino;
1500
1501         // encoding prameters to set
1502         OPJ_UINT32 l_bound;
1503
1504         opj_pi_iterator_t * l_current_pi = 00;
1505         opj_poc_t* l_current_poc = 0;
1506
1507         // preconditions in debug
1508         assert(p_pi != 00);
1509         assert(p_tcp != 00);
1510
1511         // initializations
1512         l_bound = p_tcp->numpocs+1;
1513         l_current_pi = p_pi;
1514         l_current_poc = p_tcp->pocs;
1515
1516         for
1517                 (pino = 0;pino<l_bound;++pino)
1518         {
1519                 l_current_pi->poc.prg = l_current_poc->prg;
1520                 l_current_pi->first = 1;
1521
1522                 l_current_pi->poc.resno0 = l_current_poc->resno0;
1523                 l_current_pi->poc.compno0 = l_current_poc->compno0;
1524                 l_current_pi->poc.layno0 = 0;
1525                 l_current_pi->poc.precno0 = 0;
1526                 l_current_pi->poc.resno1 = l_current_poc->resno1;
1527                 l_current_pi->poc.compno1 = l_current_poc->compno1;
1528                 l_current_pi->poc.layno1 = l_current_poc->layno1;
1529                 l_current_pi->poc.precno1 = p_max_precision;
1530                 ++l_current_pi;
1531                 ++l_current_poc;
1532         }
1533 }
1534
1535
1536 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)
1537 {
1538         // loop
1539         OPJ_UINT32 pino;
1540
1541         // encoding prameters to set
1542         OPJ_UINT32 l_bound;
1543
1544         opj_pi_iterator_t * l_current_pi = 00;
1545         // preconditions in debug
1546         assert(p_tcp != 00);
1547         assert(p_pi != 00);
1548
1549         // initializations
1550         l_bound = p_tcp->numpocs+1;
1551         l_current_pi = p_pi;
1552
1553         for
1554                 (pino = 0;pino<l_bound;++pino)
1555         {
1556                 l_current_pi->poc.prg = p_tcp->prg;
1557                 l_current_pi->first = 1;
1558                 l_current_pi->poc.resno0 = 0;
1559                 l_current_pi->poc.compno0 = 0;
1560                 l_current_pi->poc.layno0 = 0;
1561                 l_current_pi->poc.precno0 = 0;
1562                 l_current_pi->poc.resno1 = p_max_res;
1563                 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1564                 l_current_pi->poc.layno1 = p_tcp->numlayers;
1565                 l_current_pi->poc.precno1 = p_max_precision;
1566                 ++l_current_pi;
1567         }
1568 }