Mango request: added an input node to use track's position in compositor
[blender.git] / extern / libopenjpeg / pi.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "opj_includes.h"
34
35 /** @defgroup PI PI - Implementation of a packet iterator */
36 /*@{*/
37
38 /** @name Local static functions */
39 /*@{*/
40
41 /**
42 Get next packet in layer-resolution-component-precinct order.
43 @param pi packet iterator to modify
44 @return returns false if pi pointed to the last packet or else returns true 
45 */
46 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi);
47 /**
48 Get next packet in resolution-layer-component-precinct order.
49 @param pi packet iterator to modify
50 @return returns false if pi pointed to the last packet or else returns true 
51 */
52 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi);
53 /**
54 Get next packet in resolution-precinct-component-layer order.
55 @param pi packet iterator to modify
56 @return returns false if pi pointed to the last packet or else returns true 
57 */
58 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi);
59 /**
60 Get next packet in precinct-component-resolution-layer order.
61 @param pi packet iterator to modify
62 @return returns false if pi pointed to the last packet or else returns true 
63 */
64 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi);
65 /**
66 Get next packet in component-precinct-resolution-layer order.
67 @param pi packet iterator to modify
68 @return returns false if pi pointed to the last packet or else returns true 
69 */
70 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi);
71
72 /*@}*/
73
74 /*@}*/
75
76 /* 
77 ==========================================================
78    local functions
79 ==========================================================
80 */
81
82 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
83         opj_pi_comp_t *comp = NULL;
84         opj_pi_resolution_t *res = NULL;
85         long index = 0;
86         
87         if (!pi->first) {
88                 comp = &pi->comps[pi->compno];
89                 res = &comp->resolutions[pi->resno];
90                 goto LABEL_SKIP;
91         } else {
92                 pi->first = 0;
93         }
94
95         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
96                 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
97                 pi->resno++) {
98                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
99                                 comp = &pi->comps[pi->compno];
100                                 if (pi->resno >= comp->numresolutions) {
101                                         continue;
102                                 }
103                                 res = &comp->resolutions[pi->resno];
104                                 if (!pi->tp_on){
105                                         pi->poc.precno1 = res->pw * res->ph;
106                                 }
107                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
108                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
109                                         if (!pi->include[index]) {
110                                                 pi->include[index] = 1;
111                                                 return OPJ_TRUE;
112                                         }
113 LABEL_SKIP:;
114                                 }
115                         }
116                 }
117         }
118         
119         return OPJ_FALSE;
120 }
121
122 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
123         opj_pi_comp_t *comp = NULL;
124         opj_pi_resolution_t *res = NULL;
125         long index = 0;
126
127         if (!pi->first) {
128                 comp = &pi->comps[pi->compno];
129                 res = &comp->resolutions[pi->resno];
130                 goto LABEL_SKIP;
131         } else {
132                 pi->first = 0;
133         }
134
135         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
136                 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
137                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
138                                 comp = &pi->comps[pi->compno];
139                                 if (pi->resno >= comp->numresolutions) {
140                                         continue;
141                                 }
142                                 res = &comp->resolutions[pi->resno];
143                                 if(!pi->tp_on){
144                                         pi->poc.precno1 = res->pw * res->ph;
145                                 }
146                                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
147                                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
148                                         if (!pi->include[index]) {
149                                                 pi->include[index] = 1;
150                                                 return OPJ_TRUE;
151                                         }
152 LABEL_SKIP:;
153                                 }
154                         }
155                 }
156         }
157         
158         return OPJ_FALSE;
159 }
160
161 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
162         opj_pi_comp_t *comp = NULL;
163         opj_pi_resolution_t *res = NULL;
164         long index = 0;
165
166         if (!pi->first) {
167                 goto LABEL_SKIP;
168         } else {
169                 int compno, resno;
170                 pi->first = 0;
171                 pi->dx = 0;
172                 pi->dy = 0;
173                 for (compno = 0; compno < pi->numcomps; compno++) {
174                         comp = &pi->comps[compno];
175                         for (resno = 0; resno < comp->numresolutions; resno++) {
176                                 int dx, dy;
177                                 res = &comp->resolutions[resno];
178                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
179                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
180                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
181                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
182                         }
183                 }
184         }
185 if (!pi->tp_on){
186                         pi->poc.ty0 = pi->ty0;
187                         pi->poc.tx0 = pi->tx0;
188                         pi->poc.ty1 = pi->ty1;
189                         pi->poc.tx1 = pi->tx1;
190                 }
191         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
192                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
193                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
194                                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
195                                         int levelno;
196                                         int trx0, try0;
197                                         int trx1, try1;
198                                         int rpx, rpy;
199                                         int prci, prcj;
200                                         comp = &pi->comps[pi->compno];
201                                         if (pi->resno >= comp->numresolutions) {
202                                                 continue;
203                                         }
204                                         res = &comp->resolutions[pi->resno];
205                                         levelno = comp->numresolutions - 1 - pi->resno;
206                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
207                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
208                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
209                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
210                                         rpx = res->pdx + levelno;
211                                         rpy = res->pdy + levelno;
212                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
213                                                 continue;       
214                                         }
215                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
216                                                 continue; 
217                                         }
218                                         
219                                         if ((res->pw==0)||(res->ph==0)) continue;
220                                         
221                                         if ((trx0==trx1)||(try0==try1)) continue;
222                                         
223                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
224                                                  - int_floordivpow2(trx0, res->pdx);
225                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
226                                                  - int_floordivpow2(try0, res->pdy);
227                                         pi->precno = prci + prcj * res->pw;
228                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
229                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
230                                                 if (!pi->include[index]) {
231                                                         pi->include[index] = 1;
232                                                         return OPJ_TRUE;
233                                                 }
234 LABEL_SKIP:;
235                                         }
236                                 }
237                         }
238                 }
239         }
240         
241         return OPJ_FALSE;
242 }
243
244 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
245         opj_pi_comp_t *comp = NULL;
246         opj_pi_resolution_t *res = NULL;
247         long index = 0;
248
249         if (!pi->first) {
250                 comp = &pi->comps[pi->compno];
251                 goto LABEL_SKIP;
252         } else {
253                 int compno, resno;
254                 pi->first = 0;
255                 pi->dx = 0;
256                 pi->dy = 0;
257                 for (compno = 0; compno < pi->numcomps; compno++) {
258                         comp = &pi->comps[compno];
259                         for (resno = 0; resno < comp->numresolutions; resno++) {
260                                 int dx, dy;
261                                 res = &comp->resolutions[resno];
262                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
263                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
264                                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
265                                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
266                         }
267                 }
268         }
269         if (!pi->tp_on){
270                         pi->poc.ty0 = pi->ty0;
271                         pi->poc.tx0 = pi->tx0;
272                         pi->poc.ty1 = pi->ty1;
273                         pi->poc.tx1 = pi->tx1;
274                 }
275         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
276                 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
277                         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
278                                 comp = &pi->comps[pi->compno];
279                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
280                                         int levelno;
281                                         int trx0, try0;
282                                         int trx1, try1;
283                                         int rpx, rpy;
284                                         int prci, prcj;
285                                         res = &comp->resolutions[pi->resno];
286                                         levelno = comp->numresolutions - 1 - pi->resno;
287                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
288                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
289                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
290                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
291                                         rpx = res->pdx + levelno;
292                                         rpy = res->pdy + levelno;
293                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
294                                                 continue;       
295                                         }
296                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
297                                                 continue; 
298                                         }
299                                         
300                                         if ((res->pw==0)||(res->ph==0)) continue;
301                                         
302                                         if ((trx0==trx1)||(try0==try1)) continue;
303                                         
304                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
305                                                  - int_floordivpow2(trx0, res->pdx);
306                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
307                                                  - int_floordivpow2(try0, res->pdy);
308                                         pi->precno = prci + prcj * res->pw;
309                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
310                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
311                                                 if (!pi->include[index]) {
312                                                         pi->include[index] = 1;
313                                                         return OPJ_TRUE;
314                                                 }       
315 LABEL_SKIP:;
316                                         }
317                                 }
318                         }
319                 }
320         }
321         
322         return OPJ_FALSE;
323 }
324
325 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
326         opj_pi_comp_t *comp = NULL;
327         opj_pi_resolution_t *res = NULL;
328         long index = 0;
329
330         if (!pi->first) {
331                 comp = &pi->comps[pi->compno];
332                 goto LABEL_SKIP;
333         } else {
334                 pi->first = 0;
335         }
336
337         for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
338                 int resno;
339                 comp = &pi->comps[pi->compno];
340                 pi->dx = 0;
341                 pi->dy = 0;
342                 for (resno = 0; resno < comp->numresolutions; resno++) {
343                         int dx, dy;
344                         res = &comp->resolutions[resno];
345                         dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
346                         dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
347                         pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
348                         pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
349                 }
350                 if (!pi->tp_on){
351                         pi->poc.ty0 = pi->ty0;
352                         pi->poc.tx0 = pi->tx0;
353                         pi->poc.ty1 = pi->ty1;
354                         pi->poc.tx1 = pi->tx1;
355                 }
356                 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
357                         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
358                                 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
359                                         int levelno;
360                                         int trx0, try0;
361                                         int trx1, try1;
362                                         int rpx, rpy;
363                                         int prci, prcj;
364                                         res = &comp->resolutions[pi->resno];
365                                         levelno = comp->numresolutions - 1 - pi->resno;
366                                         trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
367                                         try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
368                                         trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
369                                         try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
370                                         rpx = res->pdx + levelno;
371                                         rpy = res->pdy + levelno;
372                                         if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
373                                                 continue;       
374                                         }
375                                         if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
376                                                 continue; 
377                                         }
378                                         
379                                         if ((res->pw==0)||(res->ph==0)) continue;
380                                         
381                                         if ((trx0==trx1)||(try0==try1)) continue;
382                                         
383                                         prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx) 
384                                                  - int_floordivpow2(trx0, res->pdx);
385                                         prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy) 
386                                                  - int_floordivpow2(try0, res->pdy);
387                                         pi->precno = prci + prcj * res->pw;
388                                         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
389                                                 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
390                                                 if (!pi->include[index]) {
391                                                         pi->include[index] = 1;
392                                                         return OPJ_TRUE;
393                                                 }
394 LABEL_SKIP:;
395                                         }
396                                 }
397                         }
398                 }
399         }
400         
401         return OPJ_FALSE;
402 }
403
404 /* 
405 ==========================================================
406    Packet iterator interface
407 ==========================================================
408 */
409
410 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
411         int p, q;
412         int compno, resno, pino;
413         opj_pi_iterator_t *pi = NULL;
414         opj_tcp_t *tcp = NULL;
415         opj_tccp_t *tccp = NULL;
416
417         tcp = &cp->tcps[tileno];
418
419         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
420         if(!pi) {
421                 /* TODO: throw an error */
422                 return NULL;
423         }
424
425         for (pino = 0; pino < tcp->numpocs + 1; pino++) {       /* change */
426                 int maxres = 0;
427                 int maxprec = 0;
428                 p = tileno % cp->tw;
429                 q = tileno / cp->tw;
430
431                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
432                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
433                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
434                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
435                 pi[pino].numcomps = image->numcomps;
436
437                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
438                 if(!pi[pino].comps) {
439                         /* TODO: throw an error */
440                         pi_destroy(pi, cp, tileno);
441                         return NULL;
442                 }
443                 
444                 for (compno = 0; compno < pi->numcomps; compno++) {
445                         int tcx0, tcy0, tcx1, tcy1;
446                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
447                         tccp = &tcp->tccps[compno];
448                         comp->dx = image->comps[compno].dx;
449                         comp->dy = image->comps[compno].dy;
450                         comp->numresolutions = tccp->numresolutions;
451
452                         comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
453                         if(!comp->resolutions) {
454                                 /* TODO: throw an error */
455                                 pi_destroy(pi, cp, tileno);
456                                 return NULL;
457                         }
458
459                         tcx0 = int_ceildiv(pi->tx0, comp->dx);
460                         tcy0 = int_ceildiv(pi->ty0, comp->dy);
461                         tcx1 = int_ceildiv(pi->tx1, comp->dx);
462                         tcy1 = int_ceildiv(pi->ty1, comp->dy);
463                         if (comp->numresolutions > maxres) {
464                                 maxres = comp->numresolutions;
465                         }
466
467                         for (resno = 0; resno < comp->numresolutions; resno++) {
468                                 int levelno;
469                                 int rx0, ry0, rx1, ry1;
470                                 int px0, py0, px1, py1;
471                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
472                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
473                                         res->pdx = tccp->prcw[resno];
474                                         res->pdy = tccp->prch[resno];
475                                 } else {
476                                         res->pdx = 15;
477                                         res->pdy = 15;
478                                 }
479                                 levelno = comp->numresolutions - 1 - resno;
480                                 rx0 = int_ceildivpow2(tcx0, levelno);
481                                 ry0 = int_ceildivpow2(tcy0, levelno);
482                                 rx1 = int_ceildivpow2(tcx1, levelno);
483                                 ry1 = int_ceildivpow2(tcy1, levelno);
484                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
485                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
486                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
487                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
488                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
489                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
490                                 
491                                 if (res->pw*res->ph > maxprec) {
492                                         maxprec = res->pw*res->ph;
493                                 }
494                                 
495                         }
496                 }
497                 
498                 tccp = &tcp->tccps[0];
499                 pi[pino].step_p = 1;
500                 pi[pino].step_c = maxprec * pi[pino].step_p;
501                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
502                 pi[pino].step_l = maxres * pi[pino].step_r;
503                 
504                 if (pino == 0) {
505                         pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
506                         if(!pi[pino].include) {
507                                 /* TODO: throw an error */
508                                 pi_destroy(pi, cp, tileno);
509                                 return NULL;
510                         }
511                 }
512                 else {
513                         pi[pino].include = pi[pino - 1].include;
514                 }
515                 
516                 if (tcp->POC == 0) {
517                         pi[pino].first = 1;
518                         pi[pino].poc.resno0 = 0;
519                         pi[pino].poc.compno0 = 0;
520                         pi[pino].poc.layno1 = tcp->numlayers;
521                         pi[pino].poc.resno1 = maxres;
522                         pi[pino].poc.compno1 = image->numcomps;
523                         pi[pino].poc.prg = tcp->prg;
524                 } else {
525                         pi[pino].first = 1;
526                         pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
527                         pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
528                         pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
529                         pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
530                         pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
531                         pi[pino].poc.prg = tcp->pocs[pino].prg;
532                 }
533                 pi[pino].poc.layno0  = 0;
534                 pi[pino].poc.precno0 = 0; 
535                 pi[pino].poc.precno1 = maxprec;
536                         
537         }
538         
539         return pi;
540 }
541
542
543 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ 
544         int p, q, pino;
545         int compno, resno;
546         int maxres = 0;
547         int maxprec = 0;
548         opj_pi_iterator_t *pi = NULL;
549         opj_tcp_t *tcp = NULL;
550         opj_tccp_t *tccp = NULL;
551         
552         tcp = &cp->tcps[tileno];
553
554         pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
555         if(!pi) {       return NULL;}
556         pi->tp_on = cp->tp_on;
557
558         for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
559                 p = tileno % cp->tw;
560                 q = tileno / cp->tw;
561
562                 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
563                 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
564                 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
565                 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
566                 pi[pino].numcomps = image->numcomps;
567                 
568                 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
569                 if(!pi[pino].comps) {
570                         pi_destroy(pi, cp, tileno);
571                         return NULL;
572                 }
573                 
574                 for (compno = 0; compno < pi[pino].numcomps; compno++) {
575                         int tcx0, tcy0, tcx1, tcy1;
576                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
577                         tccp = &tcp->tccps[compno];
578                         comp->dx = image->comps[compno].dx;
579                         comp->dy = image->comps[compno].dy;
580                         comp->numresolutions = tccp->numresolutions;
581
582                         comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
583                         if(!comp->resolutions) {
584                                 pi_destroy(pi, cp, tileno);
585                                 return NULL;
586                         }
587
588                         tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
589                         tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
590                         tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
591                         tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
592                         if (comp->numresolutions > maxres) {
593                                 maxres = comp->numresolutions;
594                         }
595
596                         for (resno = 0; resno < comp->numresolutions; resno++) {
597                                 int levelno;
598                                 int rx0, ry0, rx1, ry1;
599                                 int px0, py0, px1, py1;
600                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
601                                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
602                                         res->pdx = tccp->prcw[resno];
603                                         res->pdy = tccp->prch[resno];
604                                 } else {
605                                         res->pdx = 15;
606                                         res->pdy = 15;
607                                 }
608                                 levelno = comp->numresolutions - 1 - resno;
609                                 rx0 = int_ceildivpow2(tcx0, levelno);
610                                 ry0 = int_ceildivpow2(tcy0, levelno);
611                                 rx1 = int_ceildivpow2(tcx1, levelno);
612                                 ry1 = int_ceildivpow2(tcy1, levelno);
613                                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
614                                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
615                                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
616                                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
617                                 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
618                                 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
619
620                                 if (res->pw*res->ph > maxprec) {
621                                         maxprec = res->pw * res->ph;
622                                 }
623                         }
624                 }
625                 
626                 tccp = &tcp->tccps[0];
627                 pi[pino].step_p = 1;
628                 pi[pino].step_c = maxprec * pi[pino].step_p;
629                 pi[pino].step_r = image->numcomps * pi[pino].step_c;
630                 pi[pino].step_l = maxres * pi[pino].step_r;
631                 
632                 for (compno = 0; compno < pi->numcomps; compno++) {
633                         opj_pi_comp_t *comp = &pi->comps[compno];
634                         for (resno = 0; resno < comp->numresolutions; resno++) {
635                                 int dx, dy;
636                                 opj_pi_resolution_t *res = &comp->resolutions[resno];
637                                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
638                                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
639                                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
640                                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
641                         }
642                 }
643
644                 if (pino == 0) {
645                         pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
646                         if(!pi[pino].include) {
647                                 pi_destroy(pi, cp, tileno);
648                                 return NULL;
649                         }
650                 }
651                 else {
652                         pi[pino].include = pi[pino - 1].include;
653                 }
654                 
655                 /* Generation of boundaries for each prog flag*/
656                         if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
657                                 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
658                                 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
659                                 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
660                                 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
661                                 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
662                                 tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
663                                 if (pino > 0)
664                                         tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
665                         }else {
666                                 tcp->pocs[pino].compS= 0;
667                                 tcp->pocs[pino].compE= image->numcomps;
668                                 tcp->pocs[pino].resS = 0;
669                                 tcp->pocs[pino].resE = maxres;
670                                 tcp->pocs[pino].layS = 0;
671                                 tcp->pocs[pino].layE = tcp->numlayers;
672                                 tcp->pocs[pino].prg  = tcp->prg;
673                         }
674                         tcp->pocs[pino].prcS = 0;
675                         tcp->pocs[pino].prcE = maxprec;;
676                         tcp->pocs[pino].txS = pi[pino].tx0;
677                         tcp->pocs[pino].txE = pi[pino].tx1;
678                         tcp->pocs[pino].tyS = pi[pino].ty0;
679                         tcp->pocs[pino].tyE = pi[pino].ty1;
680                         tcp->pocs[pino].dx = pi[pino].dx;
681                         tcp->pocs[pino].dy = pi[pino].dy;
682                 }
683                         return pi;
684         }
685
686
687
688 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
689         int compno, pino;
690         opj_tcp_t *tcp = &cp->tcps[tileno];
691         if(pi) {
692                 for (pino = 0; pino < tcp->numpocs + 1; pino++) {       
693                         if(pi[pino].comps) {
694                                 for (compno = 0; compno < pi->numcomps; compno++) {
695                                         opj_pi_comp_t *comp = &pi[pino].comps[compno];
696                                         if(comp->resolutions) {
697                                                 opj_free(comp->resolutions);
698                                         }
699                                 }
700                                 opj_free(pi[pino].comps);
701                         }
702                 }
703                 if(pi->include) {
704                         opj_free(pi->include);
705                 }
706                 opj_free(pi);
707         }
708 }
709
710 opj_bool pi_next(opj_pi_iterator_t * pi) {
711         switch (pi->poc.prg) {
712                 case LRCP:
713                         return pi_next_lrcp(pi);
714                 case RLCP:
715                         return pi_next_rlcp(pi);
716                 case RPCL:
717                         return pi_next_rpcl(pi);
718                 case PCRL:
719                         return pi_next_pcrl(pi);
720                 case CPRL:
721                         return pi_next_cprl(pi);
722                 case PROG_UNKNOWN:
723                         return OPJ_FALSE;
724         }
725         
726         return OPJ_FALSE;
727 }
728
729 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){
730         char prog[4];
731         int i;
732         int incr_top=1,resetX=0;
733         opj_tcp_t *tcps =&cp->tcps[tileno];
734         opj_poc_t *tcp= &tcps->pocs[pino];
735
736         pi[pino].first = 1;
737         pi[pino].poc.prg = tcp->prg;
738
739         switch(tcp->prg){
740                 case CPRL: strncpy(prog, "CPRL",4);
741                         break;
742                 case LRCP: strncpy(prog, "LRCP",4);
743                         break;
744                 case PCRL: strncpy(prog, "PCRL",4);
745                         break;
746                 case RLCP: strncpy(prog, "RLCP",4);
747                         break;
748                 case RPCL: strncpy(prog, "RPCL",4);
749                         break;
750                 case PROG_UNKNOWN: 
751                         return OPJ_TRUE;
752         }
753
754         if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
755                 pi[pino].poc.resno0 = tcp->resS;
756                 pi[pino].poc.resno1 = tcp->resE;
757                 pi[pino].poc.compno0 = tcp->compS;
758                 pi[pino].poc.compno1 = tcp->compE;
759                 pi[pino].poc.layno0 = tcp->layS;
760                 pi[pino].poc.layno1 = tcp->layE;
761                 pi[pino].poc.precno0 = tcp->prcS;
762                 pi[pino].poc.precno1 = tcp->prcE;
763                 pi[pino].poc.tx0 = tcp->txS;
764                 pi[pino].poc.ty0 = tcp->tyS;
765                 pi[pino].poc.tx1 = tcp->txE;
766                 pi[pino].poc.ty1 = tcp->tyE;
767         }else {
768                 if( tpnum < cur_totnum_tp){
769                         for(i=3;i>=0;i--){
770                                 switch(prog[i]){
771                                 case 'C':
772                                         if (i > tppos){
773                                                 pi[pino].poc.compno0 = tcp->compS;
774                                                 pi[pino].poc.compno1 = tcp->compE;
775                                         }else{
776                                                 if (tpnum == 0){
777                                                         tcp->comp_t = tcp->compS;
778                                                         pi[pino].poc.compno0 = tcp->comp_t;
779                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
780                                                         tcp->comp_t+=1;
781                                                 }else{
782                                                         if (incr_top == 1){
783                                                                 if(tcp->comp_t ==tcp->compE){
784                                                                         tcp->comp_t = tcp->compS;
785                                                                         pi[pino].poc.compno0 = tcp->comp_t;
786                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
787                                                                         tcp->comp_t+=1;
788                                                                         incr_top=1;
789                                                                 }else{
790                                                                         pi[pino].poc.compno0 = tcp->comp_t;
791                                                                         pi[pino].poc.compno1 = tcp->comp_t+1;
792                                                                         tcp->comp_t+=1;
793                                                                         incr_top=0;
794                                                                 }
795                                                         }else{
796                                                                 pi[pino].poc.compno0 = tcp->comp_t-1;
797                                                                 pi[pino].poc.compno1 = tcp->comp_t;
798                                                         }
799                                                 }
800                                         }
801                                         break;
802
803                                 case 'R':
804                                         if (i > tppos){
805                                                 pi[pino].poc.resno0 = tcp->resS;
806                                                 pi[pino].poc.resno1 = tcp->resE;
807                                         }else{
808                                                 if (tpnum == 0){
809                                                         tcp->res_t = tcp->resS;
810                                                         pi[pino].poc.resno0 = tcp->res_t;
811                                                         pi[pino].poc.resno1 = tcp->res_t+1;
812                                                         tcp->res_t+=1;
813                                                 }else{
814                                                         if (incr_top == 1){
815                                                                 if(tcp->res_t==tcp->resE){
816                                                                         tcp->res_t = tcp->resS;
817                                                                         pi[pino].poc.resno0 = tcp->res_t;
818                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
819                                                                         tcp->res_t+=1;
820                                                                         incr_top=1;
821                                                                 }else{
822                                                                         pi[pino].poc.resno0 = tcp->res_t;
823                                                                         pi[pino].poc.resno1 = tcp->res_t+1;
824                                                                         tcp->res_t+=1;
825                                                                         incr_top=0;
826                                                                 }
827                                                         }else{
828                                                                 pi[pino].poc.resno0 = tcp->res_t - 1;
829                                                                 pi[pino].poc.resno1 = tcp->res_t;
830                                                         }
831                                                 }
832                                         }
833                                         break;
834
835                                 case 'L':
836                                         if (i > tppos){
837                                                 pi[pino].poc.layno0 = tcp->layS;
838                                                 pi[pino].poc.layno1 = tcp->layE;
839                                         }else{
840                                                 if (tpnum == 0){
841                                                         tcp->lay_t = tcp->layS;
842                                                         pi[pino].poc.layno0 = tcp->lay_t;
843                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
844                                                         tcp->lay_t+=1;
845                                                 }else{
846                                                         if (incr_top == 1){
847                                                                 if(tcp->lay_t == tcp->layE){
848                                                                         tcp->lay_t = tcp->layS;
849                                                                         pi[pino].poc.layno0 = tcp->lay_t;
850                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
851                                                                         tcp->lay_t+=1;
852                                                                         incr_top=1;
853                                                                 }else{
854                                                                         pi[pino].poc.layno0 = tcp->lay_t;
855                                                                         pi[pino].poc.layno1 = tcp->lay_t+1;
856                                                                         tcp->lay_t+=1;
857                                                                         incr_top=0;
858                                                                 }
859                                                         }else{
860                                                                 pi[pino].poc.layno0 = tcp->lay_t - 1;
861                                                                 pi[pino].poc.layno1 = tcp->lay_t;
862                                                         }
863                                                 }
864                                         }
865                                         break;
866
867                                 case 'P':
868                                         switch(tcp->prg){
869                                                 case LRCP:
870                                                 case RLCP:
871                                                         if (i > tppos){
872                                                                 pi[pino].poc.precno0 = tcp->prcS;
873                                                                 pi[pino].poc.precno1 = tcp->prcE;
874                                                         }else{
875                                                                 if (tpnum == 0){
876                                                                         tcp->prc_t = tcp->prcS;
877                                                                         pi[pino].poc.precno0 = tcp->prc_t;
878                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
879                                                                         tcp->prc_t+=1; 
880                                                                 }else{
881                                                                         if (incr_top == 1){
882                                                                                 if(tcp->prc_t == tcp->prcE){
883                                                                                         tcp->prc_t = tcp->prcS;
884                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
885                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
886                                                                                         tcp->prc_t+=1;
887                                                                                         incr_top=1;
888                                                                                 }else{
889                                                                                         pi[pino].poc.precno0 = tcp->prc_t;
890                                                                                         pi[pino].poc.precno1 = tcp->prc_t+1;
891                                                                                         tcp->prc_t+=1;
892                                                                                         incr_top=0;
893                                                                                 }
894                                                                         }else{
895                                                                                 pi[pino].poc.precno0 = tcp->prc_t - 1;
896                                                                                 pi[pino].poc.precno1 = tcp->prc_t;
897                                                                         }
898                                                                 }
899                                                         }
900                                                 break;
901                                                 default:
902                                                         if (i > tppos){
903                                                                 pi[pino].poc.tx0 = tcp->txS;
904                                                                 pi[pino].poc.ty0 = tcp->tyS;
905                                                                 pi[pino].poc.tx1 = tcp->txE;
906                                                                 pi[pino].poc.ty1 = tcp->tyE;
907                                                         }else{
908                                                                 if (tpnum == 0){
909                                                                         tcp->tx0_t = tcp->txS;
910                                                                         tcp->ty0_t = tcp->tyS;
911                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
912                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
913                                                                         pi[pino].poc.ty0 = tcp->ty0_t;
914                                                                         pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
915                                                                         tcp->tx0_t = pi[pino].poc.tx1;
916                                                                         tcp->ty0_t = pi[pino].poc.ty1;
917                                                                 }else{
918                                                                         if (incr_top == 1){
919                                                                                 if(tcp->tx0_t >= tcp->txE){
920                                                                                         if(tcp->ty0_t >= tcp->tyE){
921                                                                                                 tcp->ty0_t = tcp->tyS;
922                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
923                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
924                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
925                                                                                                 incr_top=1;resetX=1;
926                                                                                         }else{
927                                                                                                 pi[pino].poc.ty0 = tcp->ty0_t;
928                                                                                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
929                                                                                                 tcp->ty0_t = pi[pino].poc.ty1;
930                                                                                                 incr_top=0;resetX=1;
931                                                                                         }
932                                                                                         if(resetX==1){
933                                                                                                 tcp->tx0_t = tcp->txS;
934                                                                                                 pi[pino].poc.tx0 = tcp->tx0_t;
935                                                                                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
936                                                                                                 tcp->tx0_t = pi[pino].poc.tx1;
937                                                                                         }
938                                                                                 }else{
939                                                                                         pi[pino].poc.tx0 = tcp->tx0_t;
940                                                                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
941                                                                                         tcp->tx0_t = pi[pino].poc.tx1;
942                                                                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
943                                                                                         pi[pino].poc.ty1 = tcp->ty0_t ;
944                                                                                         incr_top=0;
945                                                                                 }
946                                                                         }else{
947                                                                                 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
948                                                                                 pi[pino].poc.tx1 = tcp->tx0_t ;
949                                                                                 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
950                                                                                 pi[pino].poc.ty1 = tcp->ty0_t ;
951                                                                         }
952                                                                 }
953                                                         }
954                                                 break;
955                                                 }
956                                                 break;
957                                 }               
958                         } 
959                 }
960         }       
961         return OPJ_FALSE;
962 }
963