update j2k_lib with new opj type
[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  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
74  *
75  * @param       p_cp            the coding parameters to modify
76  * @param       p_tileno        the tile index being concerned.
77  * @param       p_tx0           X0 parameter for the tile
78  * @param       p_tx1           X1 parameter for the tile
79  * @param       p_ty0           Y0 parameter for the tile
80  * @param       p_ty1           Y1 parameter for the tile
81  * @param       p_max_prec      the maximum precision for all the bands of the tile
82  * @param       p_max_res       the maximum number of resolutions for all the poc inside the tile.
83  * @param       p_dx_min                the minimum dx of all the components of all the resolutions for the tile.
84  * @param       p_dy_min                the minimum dy of all the components of all the resolutions for the tile.
85  */
86 static void pi_update_encode_poc_and_final (opj_cp_v2_t *p_cp,
87                                                                          OPJ_UINT32 p_tileno,
88                                                                          OPJ_INT32 p_tx0,
89                                                                          OPJ_INT32 p_tx1,
90                                                                          OPJ_INT32 p_ty0,
91                                                                          OPJ_INT32 p_ty1,
92                                                                          OPJ_UINT32 p_max_prec,
93                                                                          OPJ_UINT32 p_max_res,
94                                      OPJ_UINT32 p_dx_min,
95                                                                          OPJ_UINT32 p_dy_min);
96
97 void pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
98                                                                 OPJ_UINT32 p_num_comps,
99                                                                 OPJ_UINT32 p_tileno,
100                                                                 OPJ_INT32 p_tx0,
101                                                                 OPJ_INT32 p_tx1,
102                                                                 OPJ_INT32 p_ty0,
103                                                                 OPJ_INT32 p_ty1,
104                                                                 OPJ_UINT32 p_max_prec,
105                                                                 OPJ_UINT32 p_max_res,
106                                 OPJ_UINT32 p_dx_min,
107                                                                 OPJ_UINT32 p_dy_min);
108
109 void get_encoding_parameters(   const opj_image_t *p_image,
110                                                                 const opj_cp_v2_t *p_cp,
111                                                                 OPJ_UINT32  tileno,
112                                                                 OPJ_INT32  * p_tx0,
113                                                                 OPJ_INT32 * p_tx1,
114                                                                 OPJ_INT32 * p_ty0,
115                                                                 OPJ_INT32 * p_ty1,
116                                                                 OPJ_UINT32 * p_dx_min,
117                                                                 OPJ_UINT32 * p_dy_min,
118                                                                 OPJ_UINT32 * p_max_prec,
119                                                                 OPJ_UINT32 * p_max_res );
120
121 void get_all_encoding_parameters(
122                                                                 const opj_image_t *p_image,
123                                                                 const opj_cp_v2_t *p_cp,
124                                                                 OPJ_UINT32 tileno,
125                                                                 OPJ_INT32 * p_tx0,
126                                                                 OPJ_INT32 * p_tx1,
127                                                                 OPJ_INT32 * p_ty0,
128                                                                 OPJ_INT32 * p_ty1,
129                                                                 OPJ_UINT32 * p_dx_min,
130                                                                 OPJ_UINT32 * p_dy_min,
131                                                                 OPJ_UINT32 * p_max_prec,
132                                                                 OPJ_UINT32 * p_max_res,
133                                                                 OPJ_UINT32 ** p_resolutions
134                                                         );
135
136 opj_pi_iterator_t * pi_create(  const opj_image_t *image,
137                                                                 const opj_cp_v2_t *cp,
138                                                                 OPJ_UINT32 tileno );
139
140 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);
141 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);
142
143 OPJ_INT32 pi_check_next_level(  OPJ_INT32 pos,
144                                                                 opj_cp_v2_t *cp,
145                                                                 OPJ_UINT32 tileno,
146                                                                 OPJ_UINT32 pino,
147                                                                 const OPJ_CHAR *prog);
148
149 /*@}*/
150
151 /*@}*/
152
153 /*
154 ==========================================================
155    local functions
156 ==========================================================
157 */
158
159 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
160         opj_pi_comp_t *comp = NULL;
161         opj_pi_resolution_t *res = NULL;
162         long index = 0;
163         
164         if (!pi->first) {
165                 comp = &pi->comps[pi->compno];
166                 res = &comp->resolutions[pi->resno];
167                 goto LABEL_SKIP;
168         } else {
169                 pi->first = 0;
170         }
171
172         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
173                 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
174                 pi->resno++) {
175                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
176                                 comp = &pi->comps[pi->compno];
177                                 if (pi->resno >= comp->numresolutions) {
178                                         continue;
179                                 }
180                                 res = &comp->resolutions[pi->resno];
181                                 if (!pi->tp_on){
182                                         pi->poc.precno1 = res->pw * res->ph;
183                                 }
184                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
185                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
186                                         if (!pi->include[index]) {
187                                                 pi->include[index] = 1;
188                                                 return OPJ_TRUE;
189                                         }
190 LABEL_SKIP:;
191                                 }
192                         }
193                 }
194         }
195         
196         return OPJ_FALSE;
197 }
198
199 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
200         opj_pi_comp_t *comp = NULL;
201         opj_pi_resolution_t *res = NULL;
202         long index = 0;
203
204         if (!pi->first) {
205                 comp = &pi->comps[pi->compno];
206                 res = &comp->resolutions[pi->resno];
207                 goto LABEL_SKIP;
208         } else {
209                 pi->first = 0;
210         }
211
212         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
213                 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
214                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
215                                 comp = &pi->comps[pi->compno];
216                                 if (pi->resno >= comp->numresolutions) {
217                                         continue;
218                                 }
219                                 res = &comp->resolutions[pi->resno];
220                                 if(!pi->tp_on){
221                                         pi->poc.precno1 = res->pw * res->ph;
222                                 }
223                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
224                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
225                                         if (!pi->include[index]) {
226                                                 pi->include[index] = 1;
227                                                 return OPJ_TRUE;
228                                         }
229 LABEL_SKIP:;
230                                 }
231                         }
232                 }
233         }
234         
235         return OPJ_FALSE;
236 }
237
238 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
239         opj_pi_comp_t *comp = NULL;
240         opj_pi_resolution_t *res = NULL;
241         long index = 0;
242
243         if (!pi->first) {
244                 goto LABEL_SKIP;
245         } else {
246                 int compno, resno;
247                 pi->first = 0;
248                 pi->dx = 0;
249                 pi->dy = 0;
250                 for (compno = 0; compno < pi->numcomps; compno++) {
251                         comp = &pi->comps[compno];
252                         for (resno = 0; resno < comp->numresolutions; resno++) {
253                                 int dx, dy;
254                                 res = &comp->resolutions[resno];
255                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
256                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
257                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
258                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
259                         }
260                 }
261         }
262 if (!pi->tp_on){
263                         pi->poc.ty0 = pi->ty0;
264                         pi->poc.tx0 = pi->tx0;
265                         pi->poc.ty1 = pi->ty1;
266                         pi->poc.tx1 = pi->tx1;
267                 }
268         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
269                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
270                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
271                                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
272                                         int levelno;
273                                         int trx0, try0;
274                                         int trx1, try1;
275                                         int rpx, rpy;
276                                         int prci, prcj;
277                                         comp = &pi->comps[pi->compno];
278                                         if (pi->resno >= comp->numresolutions) {
279                                                 continue;
280                                         }
281                                         res = &comp->resolutions[pi->resno];
282                                         levelno = comp->numresolutions - 1 - pi->resno;
283                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
284                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
285                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
286                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
287                                         rpx = res->pdx + levelno;
288                                         rpy = res->pdy + levelno;
289                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
290                                                 continue;       
291                                         }
292                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
293                                                 continue;
294                                         }
295                                         
296                                         if ((res->pw==0)||(res->ph==0)) continue;
297                                         
298                                         if ((trx0==trx1)||(try0==try1)) continue;
299                                         
300                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
301                                                  - int_floordivpow2(trx0, res->pdx);
302                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
303                                                  - int_floordivpow2(try0, res->pdy);
304                                         pi->precno = prci + prcj * res->pw;
305                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
306                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
307                                                 if (!pi->include[index]) {
308                                                         pi->include[index] = 1;
309                                                         return OPJ_TRUE;
310                                                 }
311 LABEL_SKIP:;
312                                         }
313                                 }
314                         }
315                 }
316         }
317         
318         return OPJ_FALSE;
319 }
320
321 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
322         opj_pi_comp_t *comp = NULL;
323         opj_pi_resolution_t *res = NULL;
324         long index = 0;
325
326         if (!pi->first) {
327                 comp = &pi->comps[pi->compno];
328                 goto LABEL_SKIP;
329         } else {
330                 int compno, resno;
331                 pi->first = 0;
332                 pi->dx = 0;
333                 pi->dy = 0;
334                 for (compno = 0; compno < pi->numcomps; compno++) {
335                         comp = &pi->comps[compno];
336                         for (resno = 0; resno < comp->numresolutions; resno++) {
337                                 int dx, dy;
338                                 res = &comp->resolutions[resno];
339                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
340                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
341                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
342                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
343                         }
344                 }
345         }
346         if (!pi->tp_on){
347                         pi->poc.ty0 = pi->ty0;
348                         pi->poc.tx0 = pi->tx0;
349                         pi->poc.ty1 = pi->ty1;
350                         pi->poc.tx1 = pi->tx1;
351                 }
352         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
353                 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
354                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
355                                 comp = &pi->comps[pi->compno];
356                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
357                                         int levelno;
358                                         int trx0, try0;
359                                         int trx1, try1;
360                                         int rpx, rpy;
361                                         int prci, prcj;
362                                         res = &comp->resolutions[pi->resno];
363                                         levelno = comp->numresolutions - 1 - pi->resno;
364                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
365                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
366                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
367                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
368                                         rpx = res->pdx + levelno;
369                                         rpy = res->pdy + levelno;
370                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
371                                                 continue;       
372                                         }
373                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
374                                                 continue;
375                                         }
376                                         
377                                         if ((res->pw==0)||(res->ph==0)) continue;
378                                         
379                                         if ((trx0==trx1)||(try0==try1)) continue;
380                                         
381                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
382                                                  - int_floordivpow2(trx0, res->pdx);
383                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
384                                                  - int_floordivpow2(try0, res->pdy);
385                                         pi->precno = prci + prcj * res->pw;
386                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
387                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
388                                                 if (!pi->include[index]) {
389                                                         pi->include[index] = 1;
390                                                         return OPJ_TRUE;
391                                                 }       
392 LABEL_SKIP:;
393                                         }
394                                 }
395                         }
396                 }
397         }
398         
399         return OPJ_FALSE;
400 }
401
402 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
403         opj_pi_comp_t *comp = NULL;
404         opj_pi_resolution_t *res = NULL;
405         long index = 0;
406
407         if (!pi->first) {
408                 comp = &pi->comps[pi->compno];
409                 goto LABEL_SKIP;
410         } else {
411                 pi->first = 0;
412         }
413
414         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
415                 int resno;
416                 comp = &pi->comps[pi->compno];
417                 pi->dx = 0;
418                 pi->dy = 0;
419                 for (resno = 0; resno < comp->numresolutions; resno++) {
420                         int dx, dy;
421                         res = &comp->resolutions[resno];
422                         dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
423                         dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
424                         pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
425                         pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
426                 }
427                 if (!pi->tp_on){
428                         pi->poc.ty0 = pi->ty0;
429                         pi->poc.tx0 = pi->tx0;
430                         pi->poc.ty1 = pi->ty1;
431                         pi->poc.tx1 = pi->tx1;
432                 }
433                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
434                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
435                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
436                                         int levelno;
437                                         int trx0, try0;
438                                         int trx1, try1;
439                                         int rpx, rpy;
440                                         int prci, prcj;
441                                         res = &comp->resolutions[pi->resno];
442                                         levelno = comp->numresolutions - 1 - pi->resno;
443                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
444                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
445                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
446                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
447                                         rpx = res->pdx + levelno;
448                                         rpy = res->pdy + levelno;
449                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
450                                                 continue;       
451                                         }
452                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
453                                                 continue;
454                                         }
455                                         
456                                         if ((res->pw==0)||(res->ph==0)) continue;
457                                         
458                                         if ((trx0==trx1)||(try0==try1)) continue;
459                                         
460                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
461                                                  - int_floordivpow2(trx0, res->pdx);
462                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
463                                                  - int_floordivpow2(try0, res->pdy);
464                                         pi->precno = prci + prcj * res->pw;
465                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
466                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
467                                                 if (!pi->include[index]) {
468                                                         pi->include[index] = 1;
469                                                         return OPJ_TRUE;
470                                                 }
471 LABEL_SKIP:;
472                                         }
473                                 }
474                         }
475                 }
476         }
477         
478         return OPJ_FALSE;
479 }
480
481 /*
482 ==========================================================
483    Packet iterator interface
484 ==========================================================
485 */
486
487 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
488         int p, q;
489         int compno, resno, pino;
490         opj_pi_iterator_t *pi = NULL;
491         opj_tcp_t *tcp = NULL;
492         opj_tccp_t *tccp = NULL;
493
494         tcp = &cp->tcps[tileno];
495
496         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
497         if(!pi) {
498                 /* TODO: throw an error */
499                 return NULL;
500         }
501
502         for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
503                 int maxres = 0;
504                 int maxprec = 0;
505                 p = tileno % cp->tw;
506                 q = tileno / cp->tw;
507
508                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
509                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
510                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
511                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
512                 pi[pino].numcomps = image->numcomps;
513
514                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
515                 if(!pi[pino].comps) {
516                         /* TODO: throw an error */
517                         pi_destroy(pi, cp, tileno);
518                         return NULL;
519                 }
520                 
521                 for (compno = 0; compno < pi->numcomps; compno++) {
522                         int tcx0, tcy0, tcx1, tcy1;
523                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
524                         tccp = &tcp->tccps[compno];
525                         comp->dx = image->comps[compno].dx;
526                         comp->dy = image->comps[compno].dy;
527                         comp->numresolutions = tccp->numresolutions;
528
529                         comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
530                         if(!comp->resolutions) {
531                                 /* TODO: throw an error */
532                                 pi_destroy(pi, cp, tileno);
533                                 return NULL;
534                         }
535
536                         tcx0 = int_ceildiv(pi->tx0, comp->dx);
537                         tcy0 = int_ceildiv(pi->ty0, comp->dy);
538                         tcx1 = int_ceildiv(pi->tx1, comp->dx);
539                         tcy1 = int_ceildiv(pi->ty1, comp->dy);
540                         if (comp->numresolutions > maxres) {
541                                 maxres = comp->numresolutions;
542                         }
543
544                         for (resno = 0; resno < comp->numresolutions; resno++) {
545                                 int levelno;
546                                 int rx0, ry0, rx1, ry1;
547                                 int px0, py0, px1, py1;
548                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
549                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
550                                         res->pdx = tccp->prcw[resno];
551                                         res->pdy = tccp->prch[resno];
552                                 } else {
553                                         res->pdx = 15;
554                                         res->pdy = 15;
555                                 }
556                                 levelno = comp->numresolutions - 1 - resno;
557                                 rx0 = int_ceildivpow2(tcx0, levelno);
558                                 ry0 = int_ceildivpow2(tcy0, levelno);
559                                 rx1 = int_ceildivpow2(tcx1, levelno);
560                                 ry1 = int_ceildivpow2(tcy1, levelno);
561                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
562                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
563                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
564                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
565                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
566                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
567                                 
568                                 if (res->pw*res->ph > maxprec) {
569                                         maxprec = res->pw*res->ph;
570                                 }
571                                 
572                         }
573                 }
574                 
575                 tccp = &tcp->tccps[0];
576                 pi[pino].step_p = 1;
577                 pi[pino].step_c = maxprec * pi[pino].step_p;
578                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
579                 pi[pino].step_l = maxres * pi[pino].step_r;
580                 
581                 if (pino == 0) {
582                         pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
583                         if(!pi[pino].include) {
584                                 /* TODO: throw an error */
585                                 pi_destroy(pi, cp, tileno);
586                                 return NULL;
587                         }
588                 }
589                 else {
590                         pi[pino].include = pi[pino - 1].include;
591                 }
592                 
593                 if (tcp->POC == 0) {
594                         pi[pino].first = 1;
595                         pi[pino].poc.resno0 = 0;
596                         pi[pino].poc.compno0 = 0;
597                         pi[pino].poc.layno1 = tcp->numlayers;
598                         pi[pino].poc.resno1 = maxres;
599                         pi[pino].poc.compno1 = image->numcomps;
600                         pi[pino].poc.prg = tcp->prg;
601                 } else {
602                         pi[pino].first = 1;
603                         pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
604                         pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
605                         pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
606                         pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
607                         pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
608                         pi[pino].poc.prg = tcp->pocs[pino].prg;
609                 }
610                 pi[pino].poc.layno0  = 0;
611                 pi[pino].poc.precno0 = 0;
612                 pi[pino].poc.precno1 = maxprec;
613                         
614         }
615         
616         return pi;
617 }
618
619 opj_pi_iterator_t *pi_create_decode_v2( opj_image_t *p_image,
620                                                                                 opj_cp_v2_t *p_cp,
621                                                                                 OPJ_UINT32 p_tile_no
622                                                                                 )
623 {
624         /* loop */
625         OPJ_UINT32 pino;
626         OPJ_UINT32 compno, resno;
627
628         /* to store w, h, dx and dy fro all components and resolutions */
629         OPJ_UINT32 * l_tmp_data;
630         OPJ_UINT32 ** l_tmp_ptr;
631
632         /* encoding prameters to set */
633         OPJ_UINT32 l_max_res;
634         OPJ_UINT32 l_max_prec;
635         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
636         OPJ_UINT32 l_dx_min,l_dy_min;
637         OPJ_UINT32 l_bound;
638         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
639         OPJ_UINT32 l_data_stride;
640
641         /* pointers */
642         opj_pi_iterator_t *l_pi = 00;
643         opj_tcp_v2_t *l_tcp = 00;
644         const opj_tccp_t *l_tccp = 00;
645         opj_pi_comp_t *l_current_comp = 00;
646         opj_image_comp_t * l_img_comp = 00;
647         opj_pi_iterator_t * l_current_pi = 00;
648         OPJ_UINT32 * l_encoding_value_ptr = 00;
649
650         /* preconditions in debug */
651         assert(p_cp != 00);
652         assert(p_image != 00);
653         assert(p_tile_no < p_cp->tw * p_cp->th);
654
655         /* initializations */
656         l_tcp = &p_cp->tcps[p_tile_no];
657         l_bound = l_tcp->numpocs+1;
658
659         l_data_stride = 4 * J2K_MAXRLVLS;
660         l_tmp_data = (OPJ_UINT32*)opj_malloc(
661                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
662         if
663                 (! l_tmp_data)
664         {
665                 return 00;
666         }
667         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
668                 p_image->numcomps * sizeof(OPJ_UINT32 *));
669         if
670                 (! l_tmp_ptr)
671         {
672                 opj_free(l_tmp_data);
673                 return 00;
674         }
675
676         /* memory allocation for pi */
677         l_pi = pi_create(p_image, p_cp, p_tile_no);
678         if (!l_pi) {
679                 opj_free(l_tmp_data);
680                 opj_free(l_tmp_ptr);
681                 return 00;
682         }
683
684         l_encoding_value_ptr = l_tmp_data;
685         /* update pointer array */
686         for
687                 (compno = 0; compno < p_image->numcomps; ++compno)
688         {
689                 l_tmp_ptr[compno] = l_encoding_value_ptr;
690                 l_encoding_value_ptr += l_data_stride;
691         }
692         /* get encoding parameters */
693         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);
694
695         /* step calculations */
696         l_step_p = 1;
697         l_step_c = l_max_prec * l_step_p;
698         l_step_r = p_image->numcomps * l_step_c;
699         l_step_l = l_max_res * l_step_r;
700
701         /* set values for first packet iterator */
702         l_current_pi = l_pi;
703
704         /* memory allocation for include */
705         l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
706         if
707                 (!l_current_pi->include)
708         {
709                 opj_free(l_tmp_data);
710                 opj_free(l_tmp_ptr);
711                 pi_destroy_v2(l_pi, l_bound);
712                 return 00;
713         }
714         memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
715
716         /* special treatment for the first packet iterator */
717         l_current_comp = l_current_pi->comps;
718         l_img_comp = p_image->comps;
719         l_tccp = l_tcp->tccps;
720
721         l_current_pi->tx0 = l_tx0;
722         l_current_pi->ty0 = l_ty0;
723         l_current_pi->tx1 = l_tx1;
724         l_current_pi->ty1 = l_ty1;
725
726         /*l_current_pi->dx = l_img_comp->dx;*/
727         /*l_current_pi->dy = l_img_comp->dy;*/
728
729         l_current_pi->step_p = l_step_p;
730         l_current_pi->step_c = l_step_c;
731         l_current_pi->step_r = l_step_r;
732         l_current_pi->step_l = l_step_l;
733
734         /* allocation for components and number of components has already been calculated by pi_create */
735         for
736                 (compno = 0; compno < l_current_pi->numcomps; ++compno)
737         {
738                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
739                 l_encoding_value_ptr = l_tmp_ptr[compno];
740
741                 l_current_comp->dx = l_img_comp->dx;
742                 l_current_comp->dy = l_img_comp->dy;
743                 /* resolutions have already been initialized */
744                 for
745                         (resno = 0; resno < l_current_comp->numresolutions; resno++)
746                 {
747                         l_res->pdx = *(l_encoding_value_ptr++);
748                         l_res->pdy = *(l_encoding_value_ptr++);
749                         l_res->pw =  *(l_encoding_value_ptr++);
750                         l_res->ph =  *(l_encoding_value_ptr++);
751                         ++l_res;
752                 }
753                 ++l_current_comp;
754                 ++l_img_comp;
755                 ++l_tccp;
756         }
757         ++l_current_pi;
758
759         for
760                 (pino = 1 ; pino<l_bound ; ++pino )
761         {
762                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
763                 opj_image_comp_t * l_img_comp = p_image->comps;
764                 l_tccp = l_tcp->tccps;
765
766                 l_current_pi->tx0 = l_tx0;
767                 l_current_pi->ty0 = l_ty0;
768                 l_current_pi->tx1 = l_tx1;
769                 l_current_pi->ty1 = l_ty1;
770                 /*l_current_pi->dx = l_dx_min;*/
771                 /*l_current_pi->dy = l_dy_min;*/
772                 l_current_pi->step_p = l_step_p;
773                 l_current_pi->step_c = l_step_c;
774                 l_current_pi->step_r = l_step_r;
775                 l_current_pi->step_l = l_step_l;
776
777                 /* allocation for components and number of components has already been calculated by pi_create */
778                 for
779                         (compno = 0; compno < l_current_pi->numcomps; ++compno)
780                 {
781                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
782                         l_encoding_value_ptr = l_tmp_ptr[compno];
783
784                         l_current_comp->dx = l_img_comp->dx;
785                         l_current_comp->dy = l_img_comp->dy;
786                         /* resolutions have already been initialized */
787                         for
788                                 (resno = 0; resno < l_current_comp->numresolutions; resno++)
789                         {
790                                 l_res->pdx = *(l_encoding_value_ptr++);
791                                 l_res->pdy = *(l_encoding_value_ptr++);
792                                 l_res->pw =  *(l_encoding_value_ptr++);
793                                 l_res->ph =  *(l_encoding_value_ptr++);
794                                 ++l_res;
795                         }
796                         ++l_current_comp;
797                         ++l_img_comp;
798                         ++l_tccp;
799                 }
800                 /* special treatment*/
801                 l_current_pi->include = (l_current_pi-1)->include;
802                 ++l_current_pi;
803         }
804         opj_free(l_tmp_data);
805         l_tmp_data = 00;
806         opj_free(l_tmp_ptr);
807         l_tmp_ptr = 00;
808         if
809                 (l_tcp->POC)
810         {
811                 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
812         }
813         else
814         {
815                 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
816         }
817         return l_pi;
818 }
819
820 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){
821         int p, q, pino;
822         int compno, resno;
823         int maxres = 0;
824         int maxprec = 0;
825         opj_pi_iterator_t *pi = NULL;
826         opj_tcp_t *tcp = NULL;
827         opj_tccp_t *tccp = NULL;
828         
829         tcp = &cp->tcps[tileno];
830
831         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
832         if(!pi) {       return NULL;}
833         pi->tp_on = cp->tp_on;
834
835         for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
836                 p = tileno % cp->tw;
837                 q = tileno / cp->tw;
838
839                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
840                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
841                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
842                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
843                 pi[pino].numcomps = image->numcomps;
844                 
845                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
846                 if(!pi[pino].comps) {
847                         pi_destroy(pi, cp, tileno);
848                         return NULL;
849                 }
850                 
851                 for (compno = 0; compno < pi[pino].numcomps; compno++) {
852                         int tcx0, tcy0, tcx1, tcy1;
853                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
854                         tccp = &tcp->tccps[compno];
855                         comp->dx = image->comps[compno].dx;
856                         comp->dy = image->comps[compno].dy;
857                         comp->numresolutions = tccp->numresolutions;
858
859                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
860                         if(!comp->resolutions) {
861                                 pi_destroy(pi, cp, tileno);
862                                 return NULL;
863                         }
864
865                         tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
866                         tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
867                         tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
868                         tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
869                         if (comp->numresolutions > maxres) {
870                                 maxres = comp->numresolutions;
871                         }
872
873                         for (resno = 0; resno < comp->numresolutions; resno++) {
874                                 int levelno;
875                                 int rx0, ry0, rx1, ry1;
876                                 int px0, py0, px1, py1;
877                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
878                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
879                                         res->pdx = tccp->prcw[resno];
880                                         res->pdy = tccp->prch[resno];
881                                 } else {
882                                         res->pdx = 15;
883                                         res->pdy = 15;
884                                 }
885                                 levelno = comp->numresolutions - 1 - resno;
886                                 rx0 = int_ceildivpow2(tcx0, levelno);
887                                 ry0 = int_ceildivpow2(tcy0, levelno);
888                                 rx1 = int_ceildivpow2(tcx1, levelno);
889                                 ry1 = int_ceildivpow2(tcy1, levelno);
890                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
891                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
892                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
893                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
894                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
895                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
896
897                                 if (res->pw*res->ph > maxprec) {
898                                         maxprec = res->pw * res->ph;
899                                 }
900                         }
901                 }
902                 
903                 tccp = &tcp->tccps[0];
904                 pi[pino].step_p = 1;
905                 pi[pino].step_c = maxprec * pi[pino].step_p;
906                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
907                 pi[pino].step_l = maxres * pi[pino].step_r;
908                 
909                 for (compno = 0; compno < pi->numcomps; compno++) {
910                         opj_pi_comp_t *comp = &pi->comps[compno];
911                         for (resno = 0; resno < comp->numresolutions; resno++) {
912                                 int dx, dy;
913                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
914                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
915                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
916                                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
917                                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
918                         }
919                 }
920
921                 if (pino == 0) {
922                         pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
923                         if(!pi[pino].include) {
924                                 pi_destroy(pi, cp, tileno);
925                                 return NULL;
926                         }
927                 }
928                 else {
929                         pi[pino].include = pi[pino - 1].include;
930                 }
931                 
932                 /* Generation of boundaries for each prog flag*/
933                         if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
934                                 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
935                                 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
936                                 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
937                                 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
938                                 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
939                                 tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
940                                 if (pino > 0)
941                                         tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
942                         }else {
943                                 tcp->pocs[pino].compS= 0;
944                                 tcp->pocs[pino].compE= image->numcomps;
945                                 tcp->pocs[pino].resS = 0;
946                                 tcp->pocs[pino].resE = maxres;
947                                 tcp->pocs[pino].layS = 0;
948                                 tcp->pocs[pino].layE = tcp->numlayers;
949                                 tcp->pocs[pino].prg  = tcp->prg;
950                         }
951                         tcp->pocs[pino].prcS = 0;
952                         tcp->pocs[pino].prcE = maxprec;;
953                         tcp->pocs[pino].txS = pi[pino].tx0;
954                         tcp->pocs[pino].txE = pi[pino].tx1;
955                         tcp->pocs[pino].tyS = pi[pino].ty0;
956                         tcp->pocs[pino].tyE = pi[pino].ty1;
957                         tcp->pocs[pino].dx = pi[pino].dx;
958                         tcp->pocs[pino].dy = pi[pino].dy;
959                 }
960                         return pi;
961         }
962
963 opj_pi_iterator_t *pi_initialise_encode_v2(
964                                                                                 const opj_image_t *p_image,
965                                                                                 opj_cp_v2_t *p_cp,
966                                                                                 OPJ_UINT32 p_tile_no,
967                                                                                 J2K_T2_MODE p_t2_mode
968                                                                                 )
969 {
970         /* loop*/
971         OPJ_UINT32 pino;
972         OPJ_UINT32 compno, resno;
973
974         /* to store w, h, dx and dy fro all components and resolutions*/
975         OPJ_UINT32 * l_tmp_data;
976         OPJ_UINT32 ** l_tmp_ptr;
977
978         /* encoding prameters to set*/
979         OPJ_UINT32 l_max_res;
980         OPJ_UINT32 l_max_prec;
981         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
982         OPJ_UINT32 l_dx_min,l_dy_min;
983         OPJ_UINT32 l_bound;
984         OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
985         OPJ_UINT32 l_data_stride;
986
987         /* pointers*/
988         opj_pi_iterator_t *l_pi = 00;
989         opj_tcp_v2_t *l_tcp = 00;
990         const opj_tccp_t *l_tccp = 00;
991         opj_pi_comp_t *l_current_comp = 00;
992         opj_image_comp_t * l_img_comp = 00;
993         opj_pi_iterator_t * l_current_pi = 00;
994         OPJ_UINT32 * l_encoding_value_ptr = 00;
995
996         /* preconditions in debug*/
997         assert(p_cp != 00);
998         assert(p_image != 00);
999         assert(p_tile_no < p_cp->tw * p_cp->th);
1000
1001         /* initializations*/
1002         l_tcp = &p_cp->tcps[p_tile_no];
1003         l_bound = l_tcp->numpocs+1;
1004
1005         l_data_stride = 4 * J2K_MAXRLVLS;
1006         l_tmp_data = (OPJ_UINT32*)opj_malloc(
1007                 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1008         if (! l_tmp_data) {
1009                 return 00;
1010         }
1011
1012         l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1013                 p_image->numcomps * sizeof(OPJ_UINT32 *));
1014         if (! l_tmp_ptr) {
1015                 opj_free(l_tmp_data);
1016                 return 00;
1017         }
1018
1019         /* memory allocation for pi*/
1020         l_pi = pi_create(p_image,p_cp,p_tile_no);
1021         if (!l_pi) {
1022                 opj_free(l_tmp_data);
1023                 opj_free(l_tmp_ptr);
1024                 return 00;
1025         }
1026
1027         l_encoding_value_ptr = l_tmp_data;
1028         /* update pointer array*/
1029         for (compno = 0; compno < p_image->numcomps; ++compno) {
1030                 l_tmp_ptr[compno] = l_encoding_value_ptr;
1031                 l_encoding_value_ptr += l_data_stride;
1032         }
1033
1034         /* get encoding parameters*/
1035         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);
1036
1037         /* step calculations*/
1038         l_step_p = 1;
1039         l_step_c = l_max_prec * l_step_p;
1040         l_step_r = p_image->numcomps * l_step_c;
1041         l_step_l = l_max_res * l_step_r;
1042
1043         /* set values for first packet iterator*/
1044         l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1045         l_current_pi = l_pi;
1046
1047         /* memory allocation for include*/
1048         l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1049         if (!l_current_pi->include) {
1050                 opj_free(l_tmp_data);
1051                 opj_free(l_tmp_ptr);
1052                 pi_destroy_v2(l_pi, l_bound);
1053                 return 00;
1054         }
1055         memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
1056
1057         /* special treatment for the first packet iterator*/
1058         l_current_comp = l_current_pi->comps;
1059         l_img_comp = p_image->comps;
1060         l_tccp = l_tcp->tccps;
1061         l_current_pi->tx0 = l_tx0;
1062         l_current_pi->ty0 = l_ty0;
1063         l_current_pi->tx1 = l_tx1;
1064         l_current_pi->ty1 = l_ty1;
1065         l_current_pi->dx = l_dx_min;
1066         l_current_pi->dy = l_dy_min;
1067         l_current_pi->step_p = l_step_p;
1068         l_current_pi->step_c = l_step_c;
1069         l_current_pi->step_r = l_step_r;
1070         l_current_pi->step_l = l_step_l;
1071
1072         /* allocation for components and number of components has already been calculated by pi_create */
1073         for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1074                 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1075                 l_encoding_value_ptr = l_tmp_ptr[compno];
1076
1077                 l_current_comp->dx = l_img_comp->dx;
1078                 l_current_comp->dy = l_img_comp->dy;
1079
1080                 /* resolutions have already been initialized */
1081                 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1082                         l_res->pdx = *(l_encoding_value_ptr++);
1083                         l_res->pdy = *(l_encoding_value_ptr++);
1084                         l_res->pw =  *(l_encoding_value_ptr++);
1085                         l_res->ph =  *(l_encoding_value_ptr++);
1086                         ++l_res;
1087                 }
1088
1089                 ++l_current_comp;
1090                 ++l_img_comp;
1091                 ++l_tccp;
1092         }
1093         ++l_current_pi;
1094
1095         for (pino = 1 ; pino<l_bound ; ++pino ) {
1096                 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
1097                 opj_image_comp_t * l_img_comp = p_image->comps;
1098                 l_tccp = l_tcp->tccps;
1099
1100                 l_current_pi->tx0 = l_tx0;
1101                 l_current_pi->ty0 = l_ty0;
1102                 l_current_pi->tx1 = l_tx1;
1103                 l_current_pi->ty1 = l_ty1;
1104                 l_current_pi->dx = l_dx_min;
1105                 l_current_pi->dy = l_dy_min;
1106                 l_current_pi->step_p = l_step_p;
1107                 l_current_pi->step_c = l_step_c;
1108                 l_current_pi->step_r = l_step_r;
1109                 l_current_pi->step_l = l_step_l;
1110
1111                 /* allocation for components and number of components has already been calculated by pi_create */
1112                 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1113                         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1114                         l_encoding_value_ptr = l_tmp_ptr[compno];
1115
1116                         l_current_comp->dx = l_img_comp->dx;
1117                         l_current_comp->dy = l_img_comp->dy;
1118                         /* resolutions have already been initialized */
1119                         for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1120                                 l_res->pdx = *(l_encoding_value_ptr++);
1121                                 l_res->pdy = *(l_encoding_value_ptr++);
1122                                 l_res->pw =  *(l_encoding_value_ptr++);
1123                                 l_res->ph =  *(l_encoding_value_ptr++);
1124                                 ++l_res;
1125                         }
1126                         ++l_current_comp;
1127                         ++l_img_comp;
1128                         ++l_tccp;
1129                 }
1130
1131                 /* special treatment*/
1132                 l_current_pi->include = (l_current_pi-1)->include;
1133                 ++l_current_pi;
1134         }
1135
1136         opj_free(l_tmp_data);
1137         l_tmp_data = 00;
1138         opj_free(l_tmp_ptr);
1139         l_tmp_ptr = 00;
1140
1141         if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
1142                 pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1143         }
1144         else {
1145                 pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1146         }
1147
1148         return l_pi;
1149 }
1150
1151 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
1152         int compno, pino;
1153         opj_tcp_t *tcp = &cp->tcps[tileno];
1154         if(pi) {
1155                 for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
1156                         if(pi[pino].comps) {
1157                                 for (compno = 0; compno < pi->numcomps; compno++) {
1158                                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
1159                                         if(comp->resolutions) {
1160                                                 opj_free(comp->resolutions);
1161                                         }
1162                                 }
1163                                 opj_free(pi[pino].comps);
1164                         }
1165                 }
1166                 if(pi->include) {
1167                         opj_free(pi->include);
1168                 }
1169                 opj_free(pi);
1170         }
1171 }
1172
1173 opj_bool pi_next(opj_pi_iterator_t * pi) {
1174         switch (pi->poc.prg) {
1175                 case LRCP:
1176                         return pi_next_lrcp(pi);
1177                 case RLCP:
1178                         return pi_next_rlcp(pi);
1179                 case RPCL:
1180                         return pi_next_rpcl(pi);
1181                 case PCRL:
1182                         return pi_next_pcrl(pi);
1183                 case CPRL:
1184                         return pi_next_cprl(pi);
1185                 case PROG_UNKNOWN:
1186                         return OPJ_FALSE;
1187         }
1188         
1189         return OPJ_FALSE;
1190 }
1191
1192 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){
1193         char prog[4];
1194         int i;
1195         int incr_top=1,resetX=0;
1196         opj_tcp_t *tcps =&cp->tcps[tileno];
1197         opj_poc_t *tcp= &tcps->pocs[pino];
1198
1199         pi[pino].first = 1;
1200         pi[pino].poc.prg = tcp->prg;
1201
1202         switch(tcp->prg){
1203                 case CPRL: strncpy(prog, "CPRL",4);
1204                         break;
1205                 case LRCP: strncpy(prog, "LRCP",4);
1206                         break;
1207                 case PCRL: strncpy(prog, "PCRL",4);
1208                         break;
1209                 case RLCP: strncpy(prog, "RLCP",4);
1210                         break;
1211                 case RPCL: strncpy(prog, "RPCL",4);
1212                         break;
1213                 case PROG_UNKNOWN:
1214                         return OPJ_TRUE;
1215         }
1216
1217         if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1218                 pi[pino].poc.resno0 = tcp->resS;
1219                 pi[pino].poc.resno1 = tcp->resE;
1220                 pi[pino].poc.compno0 = tcp->compS;
1221                 pi[pino].poc.compno1 = tcp->compE;
1222                 pi[pino].poc.layno0 = tcp->layS;
1223                 pi[pino].poc.layno1 = tcp->layE;
1224                 pi[pino].poc.precno0 = tcp->prcS;
1225                 pi[pino].poc.precno1 = tcp->prcE;
1226                 pi[pino].poc.tx0 = tcp->txS;
1227                 pi[pino].poc.ty0 = tcp->tyS;
1228                 pi[pino].poc.tx1 = tcp->txE;
1229                 pi[pino].poc.ty1 = tcp->tyE;
1230         }else {
1231                 if( tpnum < cur_totnum_tp){
1232                         for(i=3;i>=0;i--){
1233                                 switch(prog[i]){
1234                                 case 'C':
1235                                         if (i > tppos){
1236                                                 pi[pino].poc.compno0 = tcp->compS;
1237                                                 pi[pino].poc.compno1 = tcp->compE;
1238                                         }else{
1239                                                 if (tpnum == 0){
1240                                                         tcp->comp_t = tcp->compS;
1241                                                         pi[pino].poc.compno0 = tcp->comp_t;
1242                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1243                                                         tcp->comp_t+=1;
1244                                                 }else{
1245                                                         if (incr_top == 1){
1246                                                                 if(tcp->comp_t ==tcp->compE){
1247                                                                         tcp->comp_t = tcp->compS;
1248                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1249                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1250                                                                         tcp->comp_t+=1;
1251                                                                         incr_top=1;
1252                                                                 }else{
1253                                                                         pi[pino].poc.compno0 = tcp->comp_t;
1254                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
1255                                                                         tcp->comp_t+=1;
1256                                                                         incr_top=0;
1257                                                                 }
1258                                                         }else{
1259                                                                 pi[pino].poc.compno0 = tcp->comp_t-1;
1260                                                                 pi[pino].poc.compno1 = tcp->comp_t;
1261                                                         }
1262                                                 }
1263                                         }
1264                                         break;
1265
1266                                 case 'R':
1267                                         if (i > tppos){
1268                                                 pi[pino].poc.resno0 = tcp->resS;
1269                                                 pi[pino].poc.resno1 = tcp->resE;
1270                                         }else{
1271                                                 if (tpnum == 0){
1272                                                         tcp->res_t = tcp->resS;
1273                                                         pi[pino].poc.resno0 = tcp->res_t;
1274                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1275                                                         tcp->res_t+=1;
1276                                                 }else{
1277                                                         if (incr_top == 1){
1278                                                                 if(tcp->res_t==tcp->resE){
1279                                                                         tcp->res_t = tcp->resS;
1280                                                                         pi[pino].poc.resno0 = tcp->res_t;
1281                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1282                                                                         tcp->res_t+=1;
1283                                                                         incr_top=1;
1284                                                                 }else{
1285                                                                         pi[pino].poc.resno0 = tcp->res_t;
1286                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
1287                                                                         tcp->res_t+=1;
1288                                                                         incr_top=0;
1289                                                                 }
1290                                                         }else{
1291                                                                 pi[pino].poc.resno0 = tcp->res_t - 1;
1292                                                                 pi[pino].poc.resno1 = tcp->res_t;
1293                                                         }
1294                                                 }
1295                                         }
1296                                         break;
1297
1298                                 case 'L':
1299                                         if (i > tppos){
1300                                                 pi[pino].poc.layno0 = tcp->layS;
1301                                                 pi[pino].poc.layno1 = tcp->layE;
1302                                         }else{
1303                                                 if (tpnum == 0){
1304                                                         tcp->lay_t = tcp->layS;
1305                                                         pi[pino].poc.layno0 = tcp->lay_t;
1306                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1307                                                         tcp->lay_t+=1;
1308                                                 }else{
1309                                                         if (incr_top == 1){
1310                                                                 if(tcp->lay_t == tcp->layE){
1311                                                                         tcp->lay_t = tcp->layS;
1312                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1313                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1314                                                                         tcp->lay_t+=1;
1315                                                                         incr_top=1;
1316                                                                 }else{
1317                                                                         pi[pino].poc.layno0 = tcp->lay_t;
1318                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
1319                                                                         tcp->lay_t+=1;
1320                                                                         incr_top=0;
1321                                                                 }
1322                                                         }else{
1323                                                                 pi[pino].poc.layno0 = tcp->lay_t - 1;
1324                                                                 pi[pino].poc.layno1 = tcp->lay_t;
1325                                                         }
1326                                                 }
1327                                         }
1328                                         break;
1329
1330                                 case 'P':
1331                                         switch(tcp->prg){
1332                                                 case LRCP:
1333                                                 case RLCP:
1334                                                         if (i > tppos){
1335                                                                 pi[pino].poc.precno0 = tcp->prcS;
1336                                                                 pi[pino].poc.precno1 = tcp->prcE;
1337                                                         }else{
1338                                                                 if (tpnum == 0){
1339                                                                         tcp->prc_t = tcp->prcS;
1340                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1341                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1342                                                                         tcp->prc_t+=1;
1343                                                                 }else{
1344                                                                         if (incr_top == 1){
1345                                                                                 if(tcp->prc_t == tcp->prcE){
1346                                                                                         tcp->prc_t = tcp->prcS;
1347                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1348                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1349                                                                                         tcp->prc_t+=1;
1350                                                                                         incr_top=1;
1351                                                                                 }else{
1352                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
1353                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
1354                                                                                         tcp->prc_t+=1;
1355                                                                                         incr_top=0;
1356                                                                                 }
1357                                                                         }else{
1358                                                                                 pi[pino].poc.precno0 = tcp->prc_t - 1;
1359                                                                                 pi[pino].poc.precno1 = tcp->prc_t;
1360                                                                         }
1361                                                                 }
1362                                                         }
1363                                                 break;
1364                                                 default:
1365                                                         if (i > tppos){
1366                                                                 pi[pino].poc.tx0 = tcp->txS;
1367                                                                 pi[pino].poc.ty0 = tcp->tyS;
1368                                                                 pi[pino].poc.tx1 = tcp->txE;
1369                                                                 pi[pino].poc.ty1 = tcp->tyE;
1370                                                         }else{
1371                                                                 if (tpnum == 0){
1372                                                                         tcp->tx0_t = tcp->txS;
1373                                                                         tcp->ty0_t = tcp->tyS;
1374                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1375                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1376                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
1377                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1378                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1379                                                                         tcp->ty0_t = pi[pino].poc.ty1;
1380                                                                 }else{
1381                                                                         if (incr_top == 1){
1382                                                                                 if(tcp->tx0_t >= tcp->txE){
1383                                                                                         if(tcp->ty0_t >= tcp->tyE){
1384                                                                                                 tcp->ty0_t = tcp->tyS;
1385                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1386                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1387                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1388                                                                                                 incr_top=1;resetX=1;
1389                                                                                         }else{
1390                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
1391                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1392                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
1393                                                                                                 incr_top=0;resetX=1;
1394                                                                                         }
1395                                                                                         if(resetX==1){
1396                                                                                                 tcp->tx0_t = tcp->txS;
1397                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
1398                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1399                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
1400                                                                                         }
1401                                                                                 }else{
1402                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
1403                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1404                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
1405                                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1406                                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1407                                                                                         incr_top=0;
1408                                                                                 }
1409                                                                         }else{
1410                                                                                 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1411                                                                                 pi[pino].poc.tx1 = tcp->tx0_t ;
1412                                                                                 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1413                                                                                 pi[pino].poc.ty1 = tcp->ty0_t ;
1414                                                                         }
1415                                                                 }
1416                                                         }
1417                                                 break;
1418                                                 }
1419                                                 break;
1420                                 }               
1421                         }
1422                 }
1423         }       
1424         return OPJ_FALSE;
1425 }
1426
1427 void pi_update_encoding_parameters(     const opj_image_t *p_image,
1428                                                                         opj_cp_v2_t *p_cp,
1429                                                                         OPJ_UINT32 p_tile_no )
1430 {
1431         /* encoding parameters to set */
1432         OPJ_UINT32 l_max_res;
1433         OPJ_UINT32 l_max_prec;
1434         OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1435         OPJ_UINT32 l_dx_min,l_dy_min;
1436
1437         /* pointers */
1438         opj_tcp_v2_t *l_tcp = 00;
1439
1440         /* preconditions */
1441         assert(p_cp != 00);
1442         assert(p_image != 00);
1443         assert(p_tile_no < p_cp->tw * p_cp->th);
1444
1445         l_tcp = &(p_cp->tcps[p_tile_no]);
1446
1447         /* get encoding parameters */
1448         get_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res);
1449
1450         if (l_tcp->POC) {
1451                 pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1452         }
1453         else {
1454                 pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1455         }
1456
1457 }
1458
1459 void get_encoding_parameters(   const opj_image_t *p_image,
1460                                                                 const opj_cp_v2_t *p_cp,
1461                                                                 OPJ_UINT32 p_tileno,
1462                                                                 OPJ_INT32 * p_tx0,
1463                                                                 OPJ_INT32  * p_tx1,
1464                                                                 OPJ_INT32  * p_ty0,
1465                                                                 OPJ_INT32  * p_ty1,
1466                                                                 OPJ_UINT32 * p_dx_min,
1467                                                                 OPJ_UINT32 * p_dy_min,
1468                                                                 OPJ_UINT32 * p_max_prec,
1469                                                                 OPJ_UINT32 * p_max_res )
1470 {
1471         /* loop */
1472         OPJ_UINT32  compno, resno;
1473         /* pointers */
1474         const opj_tcp_v2_t *l_tcp = 00;
1475         const opj_tccp_t * l_tccp = 00;
1476         const opj_image_comp_t * l_img_comp = 00;
1477
1478         /* position in x and y of tile */
1479         OPJ_UINT32 p, q;
1480
1481         /* preconditions */
1482         assert(p_cp != 00);
1483         assert(p_image != 00);
1484         assert(p_tileno < p_cp->tw * p_cp->th);
1485
1486         /* initializations */
1487         l_tcp = &p_cp->tcps [p_tileno];
1488         l_img_comp = p_image->comps;
1489         l_tccp = l_tcp->tccps;
1490
1491         /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
1492         p = p_tileno % p_cp->tw;
1493         q = p_tileno / p_cp->tw;
1494
1495         /* find extent of tile */
1496         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1497         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1498         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1499         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1500
1501         /* max precision is 0 (can only grow) */
1502         *p_max_prec = 0;
1503         *p_max_res = 0;
1504
1505         /* take the largest value for dx_min and dy_min */
1506         *p_dx_min = 0x7fffffff;
1507         *p_dy_min  = 0x7fffffff;
1508
1509         for (compno = 0; compno < p_image->numcomps; ++compno) {
1510                 /* arithmetic variables to calculate */
1511                 OPJ_UINT32 l_level_no;
1512                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1513                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1514                 OPJ_UINT32 l_pdx, l_pdy;
1515                 OPJ_UINT32 l_pw, l_ph;
1516                 OPJ_UINT32 l_product;
1517                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1518
1519                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1520                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1521                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1522                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1523
1524                 if (l_tccp->numresolutions > *p_max_res) {
1525                         *p_max_res = l_tccp->numresolutions;
1526                 }
1527
1528                 /* use custom size for precincts */
1529                 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1530                         OPJ_UINT32 l_dx, l_dy;
1531
1532                         /* precinct width and height */
1533                         l_pdx = l_tccp->prcw[resno];
1534                         l_pdy = l_tccp->prch[resno];
1535
1536                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
1537                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
1538
1539                         /* take the minimum size for dx for each comp and resolution */
1540                         *p_dx_min = uint_min(*p_dx_min, l_dx);
1541                         *p_dy_min = uint_min(*p_dy_min, l_dy);
1542
1543                         /* various calculations of extents */
1544                         l_level_no = l_tccp->numresolutions - 1 - resno;
1545
1546                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1547                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1548                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1549                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1550
1551                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1552                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1553                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1554
1555                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1556
1557                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1558                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1559
1560                         l_product = l_pw * l_ph;
1561
1562                         /* update precision */
1563                         if (l_product > *p_max_prec) {
1564                                 *p_max_prec = l_product;
1565                         }
1566                 }
1567                 ++l_img_comp;
1568                 ++l_tccp;
1569         }
1570 }
1571
1572 /**
1573  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1574  * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1575  * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1576  * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1577  * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1578  *
1579  * @param       p_image                 the image being encoded.
1580  * @param       p_cp                    the coding parameters.
1581  * @param       tileno                  the tile index of the tile being encoded.
1582  * @param       p_tx0                   pointer that will hold the X0 parameter for the tile
1583  * @param       p_tx1                   pointer that will hold the X1 parameter for the tile
1584  * @param       p_ty0                   pointer that will hold the Y0 parameter for the tile
1585  * @param       p_ty1                   pointer that will hold the Y1 parameter for the tile
1586  * @param       p_max_prec              pointer that will hold the the maximum precision for all the bands of the tile
1587  * @param       p_max_res               pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1588  * @param       p_dx_min                pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1589  * @param       p_dy_min                pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1590  * @param       p_resolutions   pointer to an area corresponding to the one described above.
1591  */
1592 void get_all_encoding_parameters(
1593                                                                 const opj_image_t *p_image,
1594                                                                 const opj_cp_v2_t *p_cp,
1595                                                                 OPJ_UINT32 tileno,
1596                                                                 OPJ_INT32 * p_tx0,
1597                                                                 OPJ_INT32 * p_tx1,
1598                                                                 OPJ_INT32 * p_ty0,
1599                                                                 OPJ_INT32 * p_ty1,
1600                                                                 OPJ_UINT32 * p_dx_min,
1601                                                                 OPJ_UINT32 * p_dy_min,
1602                                                                 OPJ_UINT32 * p_max_prec,
1603                                                                 OPJ_UINT32 * p_max_res,
1604                                                                 OPJ_UINT32 ** p_resolutions
1605                                                         )
1606 {
1607         /* loop*/
1608         OPJ_UINT32 compno, resno;
1609
1610         /* pointers*/
1611         const opj_tcp_v2_t *tcp = 00;
1612         const opj_tccp_t * l_tccp = 00;
1613         const opj_image_comp_t * l_img_comp = 00;
1614
1615         /* to store l_dx, l_dy, w and h for each resolution and component.*/
1616         OPJ_UINT32 * lResolutionPtr;
1617
1618         /* position in x and y of tile*/
1619         OPJ_UINT32 p, q;
1620
1621         /* preconditions in debug*/
1622         assert(p_cp != 00);
1623         assert(p_image != 00);
1624         assert(tileno < p_cp->tw * p_cp->th);
1625
1626         /* initializations*/
1627         tcp = &p_cp->tcps [tileno];
1628         l_tccp = tcp->tccps;
1629         l_img_comp = p_image->comps;
1630
1631         /* position in x and y of tile*/
1632
1633         p = tileno % p_cp->tw;
1634         q = tileno / p_cp->tw;
1635
1636         /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1637         *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1638         *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1639         *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1640         *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1641
1642         /* max precision and resolution is 0 (can only grow)*/
1643         *p_max_prec = 0;
1644         *p_max_res = 0;
1645
1646         /* take the largest value for dx_min and dy_min*/
1647         *p_dx_min = 0x7fffffff;
1648         *p_dy_min  = 0x7fffffff;
1649
1650         for
1651                 (compno = 0; compno < p_image->numcomps; ++compno)
1652         {
1653                 /* aritmetic variables to calculate*/
1654                 OPJ_UINT32 l_level_no;
1655                 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1656                 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1657                 OPJ_UINT32 l_product;
1658                 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1659                 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1660
1661                 lResolutionPtr = p_resolutions[compno];
1662
1663                 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1664                 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1665                 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1666                 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1667                 if
1668                         (l_tccp->numresolutions > *p_max_res)
1669                 {
1670                         *p_max_res = l_tccp->numresolutions;
1671                 }
1672
1673                 /* use custom size for precincts*/
1674                 l_level_no = l_tccp->numresolutions - 1;
1675                 for
1676                         (resno = 0; resno < l_tccp->numresolutions; ++resno)
1677                 {
1678                         OPJ_UINT32 l_dx, l_dy;
1679                         /* precinct width and height*/
1680                         l_pdx = l_tccp->prcw[resno];
1681                         l_pdy = l_tccp->prch[resno];
1682                         *lResolutionPtr++ = l_pdx;
1683                         *lResolutionPtr++ = l_pdy;
1684                         l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1685                         l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1686                         /* take the minimum size for l_dx for each comp and resolution*/
1687                         *p_dx_min = int_min(*p_dx_min, l_dx);
1688                         *p_dy_min = int_min(*p_dy_min, l_dy);
1689                         /* various calculations of extents*/
1690
1691                         l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1692                         l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1693                         l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1694                         l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1695                         l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1696                         l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1697                         l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1698                         py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1699                         l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1700                         l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1701                         *lResolutionPtr++ = l_pw;
1702                         *lResolutionPtr++ = l_ph;
1703                         l_product = l_pw * l_ph;
1704                         /* update precision*/
1705                         if
1706                                 (l_product > *p_max_prec)
1707                         {
1708                                 *p_max_prec = l_product;
1709                         }
1710                         --l_level_no;
1711                 }
1712                 ++l_tccp;
1713                 ++l_img_comp;
1714         }
1715 }
1716
1717 opj_pi_iterator_t * pi_create(  const opj_image_t *image,
1718                                                                 const opj_cp_v2_t *cp,
1719                                                                 OPJ_UINT32 tileno )
1720 {
1721         /* loop*/
1722         OPJ_UINT32 pino, compno;
1723         /* number of poc in the p_pi*/
1724         OPJ_UINT32 l_poc_bound;
1725
1726         /* pointers to tile coding parameters and components.*/
1727         opj_pi_iterator_t *l_pi = 00;
1728         opj_tcp_v2_t *tcp = 00;
1729         const opj_tccp_t *tccp = 00;
1730
1731         /* current packet iterator being allocated*/
1732         opj_pi_iterator_t *l_current_pi = 00;
1733
1734         /* preconditions in debug*/
1735         assert(cp != 00);
1736         assert(image != 00);
1737         assert(tileno < cp->tw * cp->th);
1738
1739         /* initializations*/
1740         tcp = &cp->tcps[tileno];
1741         l_poc_bound = tcp->numpocs+1;
1742
1743         /* memory allocations*/
1744         l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1745         if (!l_pi) {
1746                 return NULL;
1747         }
1748         memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1749
1750         l_current_pi = l_pi;
1751         for (pino = 0; pino < l_poc_bound ; ++pino) {
1752
1753                 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1754                 if (! l_current_pi->comps) {
1755                         pi_destroy_v2(l_pi, l_poc_bound);
1756                         return NULL;
1757                 }
1758
1759                 l_current_pi->numcomps = image->numcomps;
1760                 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1761
1762                 for (compno = 0; compno < image->numcomps; ++compno) {
1763                         opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1764
1765                         tccp = &tcp->tccps[compno];
1766
1767                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1768                         if (!comp->resolutions) {
1769                                 pi_destroy_v2(l_pi, l_poc_bound);
1770                                 return 00;
1771                         }
1772
1773                         comp->numresolutions = tccp->numresolutions;
1774                         memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1775                 }
1776                 ++l_current_pi;
1777         }
1778         return l_pi;
1779 }
1780
1781 void pi_update_encode_poc_and_final (opj_cp_v2_t *p_cp,
1782                                                                          OPJ_UINT32 p_tileno,
1783                                                                          OPJ_INT32 p_tx0,
1784                                                                          OPJ_INT32 p_tx1,
1785                                                                          OPJ_INT32 p_ty0,
1786                                                                          OPJ_INT32 p_ty1,
1787                                                                          OPJ_UINT32 p_max_prec,
1788                                                                          OPJ_UINT32 p_max_res,
1789                                      OPJ_UINT32 p_dx_min,
1790                                                                          OPJ_UINT32 p_dy_min)
1791 {
1792         /* loop*/
1793         OPJ_UINT32 pino;
1794         /* tile coding parameter*/
1795         opj_tcp_v2_t *l_tcp = 00;
1796         /* current poc being updated*/
1797         opj_poc_t * l_current_poc = 00;
1798
1799         /* number of pocs*/
1800         OPJ_UINT32 l_poc_bound;
1801
1802         /* preconditions in debug*/
1803         assert(p_cp != 00);
1804         assert(p_tileno < p_cp->tw * p_cp->th);
1805
1806         /* initializations*/
1807         l_tcp = &p_cp->tcps [p_tileno];
1808         /* number of iterations in the loop */
1809         l_poc_bound = l_tcp->numpocs+1;
1810
1811         /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1812            store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1813         l_current_poc = l_tcp->pocs;
1814
1815         l_current_poc->compS = l_current_poc->compno0;
1816         l_current_poc->compE = l_current_poc->compno1;
1817         l_current_poc->resS = l_current_poc->resno0;
1818         l_current_poc->resE = l_current_poc->resno1;
1819         l_current_poc->layE = l_current_poc->layno1;
1820
1821         /* special treatment for the first element*/
1822         l_current_poc->layS = 0;
1823         l_current_poc->prg  = l_current_poc->prg1;
1824         l_current_poc->prcS = 0;
1825
1826         l_current_poc->prcE = p_max_prec;
1827         l_current_poc->txS = p_tx0;
1828         l_current_poc->txE = p_tx1;
1829         l_current_poc->tyS = p_ty0;
1830         l_current_poc->tyE = p_ty1;
1831         l_current_poc->dx = p_dx_min;
1832         l_current_poc->dy = p_dy_min;
1833
1834         ++ l_current_poc;
1835         for (pino = 1;pino < l_poc_bound ; ++pino) {
1836                 l_current_poc->compS = l_current_poc->compno0;
1837                 l_current_poc->compE= l_current_poc->compno1;
1838                 l_current_poc->resS = l_current_poc->resno0;
1839                 l_current_poc->resE = l_current_poc->resno1;
1840                 l_current_poc->layE = l_current_poc->layno1;
1841                 l_current_poc->prg  = l_current_poc->prg1;
1842                 l_current_poc->prcS = 0;
1843                 /* special treatment here different from the first element*/
1844                 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
1845
1846                 l_current_poc->prcE = p_max_prec;
1847                 l_current_poc->txS = p_tx0;
1848                 l_current_poc->txE = p_tx1;
1849                 l_current_poc->tyS = p_ty0;
1850                 l_current_poc->tyE = p_ty1;
1851                 l_current_poc->dx = p_dx_min;
1852                 l_current_poc->dy = p_dy_min;
1853                 ++ l_current_poc;
1854         }
1855 }
1856
1857 void pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
1858                                                                 OPJ_UINT32 p_num_comps,
1859                                                                 OPJ_UINT32 p_tileno,
1860                                                                 OPJ_INT32 p_tx0,
1861                                                                 OPJ_INT32 p_tx1,
1862                                                                 OPJ_INT32 p_ty0,
1863                                                                 OPJ_INT32 p_ty1,
1864                                                                 OPJ_UINT32 p_max_prec,
1865                                                                 OPJ_UINT32 p_max_res,
1866                                 OPJ_UINT32 p_dx_min,
1867                                                                 OPJ_UINT32 p_dy_min)
1868 {
1869         /* loop*/
1870         OPJ_UINT32 pino;
1871         /* tile coding parameter*/
1872         opj_tcp_v2_t *l_tcp = 00;
1873         /* current poc being updated*/
1874         opj_poc_t * l_current_poc = 00;
1875         /* number of pocs*/
1876         OPJ_UINT32 l_poc_bound;
1877
1878         /* preconditions in debug*/
1879         assert(p_cp != 00);
1880         assert(p_tileno < p_cp->tw * p_cp->th);
1881
1882         /* initializations*/
1883         l_tcp = &p_cp->tcps [p_tileno];
1884
1885         /* number of iterations in the loop */
1886         l_poc_bound = l_tcp->numpocs+1;
1887
1888         /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1889            store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1890         l_current_poc = l_tcp->pocs;
1891
1892         for (pino = 0; pino < l_poc_bound ; ++pino) {
1893                 l_current_poc->compS = 0;
1894                 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1895                 l_current_poc->resS = 0;
1896                 l_current_poc->resE = p_max_res;
1897                 l_current_poc->layS = 0;
1898                 l_current_poc->layE = l_tcp->numlayers;
1899                 l_current_poc->prg  = l_tcp->prg;
1900                 l_current_poc->prcS = 0;
1901                 l_current_poc->prcE = p_max_prec;
1902                 l_current_poc->txS = p_tx0;
1903                 l_current_poc->txE = p_tx1;
1904                 l_current_poc->tyS = p_ty0;
1905                 l_current_poc->tyE = p_ty1;
1906                 l_current_poc->dx = p_dx_min;
1907                 l_current_poc->dy = p_dy_min;
1908                 ++ l_current_poc;
1909         }
1910 }
1911
1912 void pi_destroy_v2(
1913                                 opj_pi_iterator_t *p_pi,
1914                                 OPJ_UINT32 p_nb_elements)
1915 {
1916         OPJ_UINT32 compno, pino;
1917         opj_pi_iterator_t *l_current_pi = p_pi;
1918         if
1919                 (p_pi)
1920         {
1921                 if
1922                         (p_pi->include)
1923                 {
1924                         opj_free(p_pi->include);
1925                         p_pi->include = 00;
1926                 }
1927                 /* TODO*/
1928                 for
1929                         (pino = 0; pino < p_nb_elements; ++pino)
1930                 {
1931                         if
1932                                 (l_current_pi->comps)
1933                         {
1934                                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1935                                 for
1936                                         (compno = 0; compno < l_current_pi->numcomps; compno++)
1937                                 {
1938                                         if
1939                                                 (l_current_component->resolutions)
1940                                         {
1941                                                 opj_free(l_current_component->resolutions);
1942                                                 l_current_component->resolutions = 00;
1943                                         }
1944                                         ++l_current_component;
1945                                 }
1946                                 opj_free(l_current_pi->comps);
1947                                 l_current_pi->comps = 0;
1948                         }
1949                         ++l_current_pi;
1950                 }
1951                 opj_free(p_pi);
1952         }
1953 }
1954
1955 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)
1956 {
1957         /* loop*/
1958         OPJ_UINT32 pino;
1959
1960         /* encoding prameters to set*/
1961         OPJ_UINT32 l_bound;
1962
1963         opj_pi_iterator_t * l_current_pi = 00;
1964         opj_poc_t* l_current_poc = 0;
1965
1966         /* preconditions in debug*/
1967         assert(p_pi != 00);
1968         assert(p_tcp != 00);
1969
1970         /* initializations*/
1971         l_bound = p_tcp->numpocs+1;
1972         l_current_pi = p_pi;
1973         l_current_poc = p_tcp->pocs;
1974
1975         for
1976                 (pino = 0;pino<l_bound;++pino)
1977         {
1978                 l_current_pi->poc.prg = l_current_poc->prg;
1979                 l_current_pi->first = 1;
1980
1981                 l_current_pi->poc.resno0 = l_current_poc->resno0;
1982                 l_current_pi->poc.compno0 = l_current_poc->compno0;
1983                 l_current_pi->poc.layno0 = 0;
1984                 l_current_pi->poc.precno0 = 0;
1985                 l_current_pi->poc.resno1 = l_current_poc->resno1;
1986                 l_current_pi->poc.compno1 = l_current_poc->compno1;
1987                 l_current_pi->poc.layno1 = l_current_poc->layno1;
1988                 l_current_pi->poc.precno1 = p_max_precision;
1989                 ++l_current_pi;
1990                 ++l_current_poc;
1991         }
1992 }
1993
1994 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)
1995 {
1996         /* loop*/
1997         OPJ_UINT32 pino;
1998
1999         /* encoding prameters to set*/
2000         OPJ_UINT32 l_bound;
2001
2002         opj_pi_iterator_t * l_current_pi = 00;
2003         /* preconditions in debug*/
2004         assert(p_tcp != 00);
2005         assert(p_pi != 00);
2006
2007         /* initializations*/
2008         l_bound = p_tcp->numpocs+1;
2009         l_current_pi = p_pi;
2010
2011         for
2012                 (pino = 0;pino<l_bound;++pino)
2013         {
2014                 l_current_pi->poc.prg = p_tcp->prg;
2015                 l_current_pi->first = 1;
2016                 l_current_pi->poc.resno0 = 0;
2017                 l_current_pi->poc.compno0 = 0;
2018                 l_current_pi->poc.layno0 = 0;
2019                 l_current_pi->poc.precno0 = 0;
2020                 l_current_pi->poc.resno1 = p_max_res;
2021                 l_current_pi->poc.compno1 = l_current_pi->numcomps;
2022                 l_current_pi->poc.layno1 = p_tcp->numlayers;
2023                 l_current_pi->poc.precno1 = p_max_precision;
2024                 ++l_current_pi;
2025         }
2026 }
2027
2028 void pi_create_encode_v2(       opj_pi_iterator_t *pi,
2029                                                         opj_cp_v2_t *cp,
2030                                                         OPJ_UINT32 tileno,
2031                                                         OPJ_UINT32 pino,
2032                                                         OPJ_UINT32 tpnum,
2033                                                         OPJ_INT32 tppos,
2034                                                         J2K_T2_MODE t2_mode)
2035 {
2036         const OPJ_CHAR *prog;
2037         OPJ_INT32 i,l;
2038         OPJ_UINT32 incr_top=1,resetX=0;
2039         opj_tcp_v2_t *tcps =&cp->tcps[tileno];
2040         opj_poc_t *tcp= &tcps->pocs[pino];
2041
2042         prog = opj_j2k_convert_progression_order(tcp->prg);
2043
2044         pi[pino].first = 1;
2045         pi[pino].poc.prg = tcp->prg;
2046
2047         if(!(cp->m_specific_param.m_enc.m_tp_on&& ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
2048                 pi[pino].poc.resno0 = tcp->resS;
2049                 pi[pino].poc.resno1 = tcp->resE;
2050                 pi[pino].poc.compno0 = tcp->compS;
2051                 pi[pino].poc.compno1 = tcp->compE;
2052                 pi[pino].poc.layno0 = tcp->layS;
2053                 pi[pino].poc.layno1 = tcp->layE;
2054                 pi[pino].poc.precno0 = tcp->prcS;
2055                 pi[pino].poc.precno1 = tcp->prcE;
2056                 pi[pino].poc.tx0 = tcp->txS;
2057                 pi[pino].poc.ty0 = tcp->tyS;
2058                 pi[pino].poc.tx1 = tcp->txE;
2059                 pi[pino].poc.ty1 = tcp->tyE;
2060         }else {
2061                 for(i=tppos+1;i<4;i++){
2062                         switch(prog[i]){
2063                         case 'R':
2064                                 pi[pino].poc.resno0 = tcp->resS;
2065                                 pi[pino].poc.resno1 = tcp->resE;
2066                                 break;
2067                         case 'C':
2068                                 pi[pino].poc.compno0 = tcp->compS;
2069                                 pi[pino].poc.compno1 = tcp->compE;
2070                                 break;
2071                         case 'L':
2072                                 pi[pino].poc.layno0 = tcp->layS;
2073                                 pi[pino].poc.layno1 = tcp->layE;
2074                                 break;
2075                         case 'P':
2076                                 switch(tcp->prg){
2077                                         case LRCP:
2078                                         case RLCP:
2079                                                 pi[pino].poc.precno0 = tcp->prcS;
2080                                                 pi[pino].poc.precno1 = tcp->prcE;
2081                                                 break;
2082                                         default:
2083                                                 pi[pino].poc.tx0 = tcp->txS;
2084                                                 pi[pino].poc.ty0 = tcp->tyS;
2085                                                 pi[pino].poc.tx1 = tcp->txE;
2086                                                 pi[pino].poc.ty1 = tcp->tyE;
2087                                                 break;
2088                                 }
2089                                 break;
2090                         }
2091                 }
2092
2093                 if(tpnum==0){
2094                         for(i=tppos;i>=0;i--){
2095                                 switch(prog[i]){
2096                                                 case 'C':
2097                                                         tcp->comp_t = tcp->compS;
2098                                                         pi[pino].poc.compno0 = tcp->comp_t;
2099                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
2100                                                         tcp->comp_t+=1;
2101                                                         break;
2102                                                 case 'R':
2103                                                         tcp->res_t = tcp->resS;
2104                                                         pi[pino].poc.resno0 = tcp->res_t;
2105                                                         pi[pino].poc.resno1 = tcp->res_t+1;
2106                                                         tcp->res_t+=1;
2107                                                         break;
2108                                                 case 'L':
2109                                                         tcp->lay_t = tcp->layS;
2110                                                         pi[pino].poc.layno0 = tcp->lay_t;
2111                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
2112                                                         tcp->lay_t+=1;
2113                                                         break;
2114                                                 case 'P':
2115                                                         switch(tcp->prg){
2116                                                                 case LRCP:
2117                                                                 case RLCP:
2118                                                                         tcp->prc_t = tcp->prcS;
2119                                                                         pi[pino].poc.precno0 = tcp->prc_t;
2120                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
2121                                                                         tcp->prc_t+=1;
2122                                                                         break;
2123                                                                 default:
2124                                                                         tcp->tx0_t = tcp->txS;
2125                                                                         tcp->ty0_t = tcp->tyS;
2126                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
2127                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2128                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
2129                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2130                                                                         tcp->tx0_t = pi[pino].poc.tx1;
2131                                                                         tcp->ty0_t = pi[pino].poc.ty1;
2132                                                                         break;
2133                                                         }
2134                                                         break;
2135                                 }
2136                         }
2137                         incr_top=1;
2138                 }else{
2139                         for(i=tppos;i>=0;i--){
2140                                 switch(prog[i]){
2141                                                 case 'C':
2142                                                         pi[pino].poc.compno0 = tcp->comp_t-1;
2143                                                         pi[pino].poc.compno1 = tcp->comp_t;
2144                                                         break;
2145                                                 case 'R':
2146                                                         pi[pino].poc.resno0 = tcp->res_t-1;
2147                                                         pi[pino].poc.resno1 = tcp->res_t;
2148                                                         break;
2149                                                 case 'L':
2150                                                         pi[pino].poc.layno0 = tcp->lay_t-1;
2151                                                         pi[pino].poc.layno1 = tcp->lay_t;
2152                                                         break;
2153                                                 case 'P':
2154                                                         switch(tcp->prg){
2155                                                                 case LRCP:
2156                                                                 case RLCP:
2157                                                                         pi[pino].poc.precno0 = tcp->prc_t-1;
2158                                                                         pi[pino].poc.precno1 = tcp->prc_t;
2159                                                                         break;
2160                                                                 default:
2161                                                                         pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
2162                                                                         pi[pino].poc.tx1 = tcp->tx0_t ;
2163                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
2164                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
2165                                                                         break;
2166                                                         }
2167                                                         break;
2168                                 }
2169                                 if(incr_top==1){
2170                                         switch(prog[i]){
2171                                                         case 'R':
2172                                                                 if(tcp->res_t==tcp->resE){
2173                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2174                                                                         if(l==1){
2175                                                                                 tcp->res_t = tcp->resS;
2176                                                                                 pi[pino].poc.resno0 = tcp->res_t;
2177                                                                                 pi[pino].poc.resno1 = tcp->res_t+1;
2178                                                                                 tcp->res_t+=1;
2179                                                                                 incr_top=1;
2180                                                                         }else{
2181                                                                                 incr_top=0;
2182                                                                         }
2183                                                                 }else{
2184                                                                         pi[pino].poc.resno0 = tcp->res_t;
2185                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
2186                                                                         tcp->res_t+=1;
2187                                                                         incr_top=0;
2188                                                                 }
2189                                                                 break;
2190                                                         case 'C':
2191                                                                 if(tcp->comp_t ==tcp->compE){
2192                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2193                                                                         if(l==1){
2194                                                                                 tcp->comp_t = tcp->compS;
2195                                                                                 pi[pino].poc.compno0 = tcp->comp_t;
2196                                                                                 pi[pino].poc.compno1 = tcp->comp_t+1;
2197                                                                                 tcp->comp_t+=1;
2198                                                                                 incr_top=1;
2199                                                                         }else{
2200                                                                                 incr_top=0;
2201                                                                         }
2202                                                                 }else{
2203                                                                         pi[pino].poc.compno0 = tcp->comp_t;
2204                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
2205                                                                         tcp->comp_t+=1;
2206                                                                         incr_top=0;
2207                                                                 }
2208                                                                 break;
2209                                                         case 'L':
2210                                                                 if(tcp->lay_t == tcp->layE){
2211                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2212                                                                         if(l==1){
2213                                                                                 tcp->lay_t = tcp->layS;
2214                                                                                 pi[pino].poc.layno0 = tcp->lay_t;
2215                                                                                 pi[pino].poc.layno1 = tcp->lay_t+1;
2216                                                                                 tcp->lay_t+=1;
2217                                                                                 incr_top=1;
2218                                                                         }else{
2219                                                                                 incr_top=0;
2220                                                                         }
2221                                                                 }else{
2222                                                                         pi[pino].poc.layno0 = tcp->lay_t;
2223                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
2224                                                                         tcp->lay_t+=1;
2225                                                                         incr_top=0;
2226                                                                 }
2227                                                                 break;
2228                                                         case 'P':
2229                                                                 switch(tcp->prg){
2230                                                                         case LRCP:
2231                                                                         case RLCP:
2232                                                                                 if(tcp->prc_t == tcp->prcE){
2233                                                                                         l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2234                                                                                         if(l==1){
2235                                                                                                 tcp->prc_t = tcp->prcS;
2236                                                                                                 pi[pino].poc.precno0 = tcp->prc_t;
2237                                                                                                 pi[pino].poc.precno1 = tcp->prc_t+1;
2238                                                                                                 tcp->prc_t+=1;
2239                                                                                                 incr_top=1;
2240                                                                                         }else{
2241                                                                                                 incr_top=0;
2242                                                                                         }
2243                                                                                 }else{
2244                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
2245                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
2246                                                                                         tcp->prc_t+=1;
2247                                                                                         incr_top=0;
2248                                                                                 }
2249                                                                                 break;
2250                                                                         default:
2251                                                                                 if(tcp->tx0_t >= tcp->txE){
2252                                                                                         if(tcp->ty0_t >= tcp->tyE){
2253                                                                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2254                                                                                                 if(l==1){
2255                                                                                                         tcp->ty0_t = tcp->tyS;
2256                                                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
2257                                                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2258                                                                                                         tcp->ty0_t = pi[pino].poc.ty1;
2259                                                                                                         incr_top=1;resetX=1;
2260                                                                                                 }else{
2261                                                                                                         incr_top=0;resetX=0;
2262                                                                                                 }
2263                                                                                         }else{
2264                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
2265                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2266                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
2267                                                                                                 incr_top=0;resetX=1;
2268                                                                                         }
2269                                                                                         if(resetX==1){
2270                                                                                                 tcp->tx0_t = tcp->txS;
2271                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
2272                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
2273                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
2274                                                                                         }
2275                                                                                 }else{
2276                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
2277                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
2278                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
2279                                                                                         incr_top=0;
2280                                                                                 }
2281                                                                                 break;
2282                                                                 }
2283                                                                 break;
2284                                         }
2285                                 }
2286                         }
2287                 }
2288         }
2289 }
2290
2291 OPJ_INT32 pi_check_next_level(  OPJ_INT32 pos,
2292                                                                 opj_cp_v2_t *cp,
2293                                                                 OPJ_UINT32 tileno,
2294                                                                 OPJ_UINT32 pino,
2295                                                                 const OPJ_CHAR *prog)
2296 {
2297         OPJ_INT32 i,l;
2298         opj_tcp_v2_t *tcps =&cp->tcps[tileno];
2299         opj_poc_t *tcp = &tcps->pocs[pino];
2300
2301         if(pos>=0){
2302                 for(i=pos;pos>=0;i--){
2303                         switch(prog[i]){
2304                 case 'R':
2305                         if(tcp->res_t==tcp->resE){
2306                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2307                                 if(l==1){
2308                                         return 1;
2309                                 }else{
2310                                         return 0;
2311                                 }
2312                         }else{
2313                                 return 1;
2314                         }
2315                         break;
2316                 case 'C':
2317                         if(tcp->comp_t==tcp->compE){
2318                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2319                                 if(l==1){
2320                                         return 1;
2321                                 }else{
2322                                         return 0;
2323                                 }
2324                         }else{
2325                                 return 1;
2326                         }
2327                         break;
2328                 case 'L':
2329                         if(tcp->lay_t==tcp->layE){
2330                                 l=pi_check_next_level(pos-1,cp,tileno,pino,prog);
2331                                 if(l==1){
2332                                         return 1;
2333                                 }else{
2334                                         return 0;
2335                                 }
2336                         }else{
2337                                 return 1;
2338                         }
2339                         break;
2340                 case 'P':
2341                         switch(tcp->prg){
2342                                 case LRCP||RLCP:
2343                                         if(tcp->prc_t == tcp->prcE){
2344                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2345                                                 if(l==1){
2346                                                         return 1;
2347                                                 }else{
2348                                                         return 0;
2349                                                 }
2350                                         }else{
2351                                                 return 1;
2352                                         }
2353                                         break;
2354                         default:
2355                                 if(tcp->tx0_t == tcp->txE){
2356                                         /*TY*/
2357                                         if(tcp->ty0_t == tcp->tyE){
2358                                                 l=pi_check_next_level(i-1,cp,tileno,pino,prog);
2359                                                 if(l==1){
2360                                                         return 1;
2361                                                 }else{
2362                                                         return 0;
2363                                                 }
2364                                         }else{
2365                                                 return 1;
2366                                         }/*TY*/
2367                                 }else{
2368                                         return 1;
2369                                 }
2370                                 break;
2371                         }/*end case P*/
2372                 }/*end switch*/
2373                 }/*end for*/
2374         }/*end if*/
2375         return 0;
2376 }