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