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