Changed cmake version test to allow for cmake 2.8.11.x
[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 {
90     opj_pi_comp_t *comp = NULL;
91     opj_pi_resolution_t *res = NULL;
92     long index = 0;
93
94     if (!pi->first) {
95         comp = &pi->comps[pi->compno];
96         res = &comp->resolutions[pi->resno];
97         goto LABEL_SKIP;
98     } else {
99         pi->first = 0;
100     }
101
102     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
103         for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
104                 pi->resno++) {
105             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
106                 comp = &pi->comps[pi->compno];
107                 if (pi->resno >= comp->numresolutions) {
108                     continue;
109                 }
110                 res = &comp->resolutions[pi->resno];
111                 if (!pi->tp_on) {
112                     pi->poc.precno1 = res->pw * res->ph;
113                 }
114                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
115                     index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
116                             pi->step_c + pi->precno * pi->step_p;
117                     if (!pi->include[index]) {
118                         pi->include[index] = 1;
119                         return OPJ_TRUE;
120                     }
121 LABEL_SKIP:
122                     ;
123                 }
124             }
125         }
126     }
127
128     return OPJ_FALSE;
129 }
130
131 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi)
132 {
133     opj_pi_comp_t *comp = NULL;
134     opj_pi_resolution_t *res = NULL;
135     long index = 0;
136
137     if (!pi->first) {
138         comp = &pi->comps[pi->compno];
139         res = &comp->resolutions[pi->resno];
140         goto LABEL_SKIP;
141     } else {
142         pi->first = 0;
143     }
144
145     for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
146         for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
147             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
148                 comp = &pi->comps[pi->compno];
149                 if (pi->resno >= comp->numresolutions) {
150                     continue;
151                 }
152                 res = &comp->resolutions[pi->resno];
153                 if (!pi->tp_on) {
154                     pi->poc.precno1 = res->pw * res->ph;
155                 }
156                 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
157                     index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
158                             pi->step_c + pi->precno * pi->step_p;
159                     if (!pi->include[index]) {
160                         pi->include[index] = 1;
161                         return OPJ_TRUE;
162                     }
163 LABEL_SKIP:
164                     ;
165                 }
166             }
167         }
168     }
169
170     return OPJ_FALSE;
171 }
172
173 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi)
174 {
175     opj_pi_comp_t *comp = NULL;
176     opj_pi_resolution_t *res = NULL;
177     long index = 0;
178
179     if (!pi->first) {
180         goto LABEL_SKIP;
181     } else {
182         int compno, resno;
183         pi->first = 0;
184         pi->dx = 0;
185         pi->dy = 0;
186         for (compno = 0; compno < pi->numcomps; compno++) {
187             comp = &pi->comps[compno];
188             for (resno = 0; resno < comp->numresolutions; resno++) {
189                 int dx, dy;
190                 res = &comp->resolutions[resno];
191                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
192                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
193                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
194                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
195             }
196         }
197     }
198     if (!pi->tp_on) {
199         pi->poc.ty0 = pi->ty0;
200         pi->poc.tx0 = pi->tx0;
201         pi->poc.ty1 = pi->ty1;
202         pi->poc.tx1 = pi->tx1;
203     }
204     for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
205         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
206                 pi->y += pi->dy - (pi->y % pi->dy)) {
207             for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
208                     pi->x += pi->dx - (pi->x % pi->dx)) {
209                 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
210                     int levelno;
211                     int trx0, try0;
212                     int trx1, try1;
213                     int rpx, rpy;
214                     int prci, prcj;
215                     comp = &pi->comps[pi->compno];
216                     if (pi->resno >= comp->numresolutions) {
217                         continue;
218                     }
219                     res = &comp->resolutions[pi->resno];
220                     levelno = comp->numresolutions - 1 - pi->resno;
221                     trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
222                     try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
223                     trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
224                     try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
225                     rpx = res->pdx + levelno;
226                     rpy = res->pdy + levelno;
227                     if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
228                             ((try0 << levelno) % (1 << rpy))))) {
229                         continue;
230                     }
231                     if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
232                             ((trx0 << levelno) % (1 << rpx))))) {
233                         continue;
234                     }
235
236                     if ((res->pw == 0) || (res->ph == 0)) {
237                         continue;
238                     }
239
240                     if ((trx0 == trx1) || (try0 == try1)) {
241                         continue;
242                     }
243
244                     prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
245                            - int_floordivpow2(trx0, res->pdx);
246                     prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
247                            - int_floordivpow2(try0, res->pdy);
248                     pi->precno = prci + prcj * res->pw;
249                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
250                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
251                                 pi->step_c + pi->precno * pi->step_p;
252                         if (!pi->include[index]) {
253                             pi->include[index] = 1;
254                             return OPJ_TRUE;
255                         }
256 LABEL_SKIP:
257                         ;
258                     }
259                 }
260             }
261         }
262     }
263
264     return OPJ_FALSE;
265 }
266
267 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi)
268 {
269     opj_pi_comp_t *comp = NULL;
270     opj_pi_resolution_t *res = NULL;
271     long index = 0;
272
273     if (!pi->first) {
274         comp = &pi->comps[pi->compno];
275         goto LABEL_SKIP;
276     } else {
277         int compno, resno;
278         pi->first = 0;
279         pi->dx = 0;
280         pi->dy = 0;
281         for (compno = 0; compno < pi->numcomps; compno++) {
282             comp = &pi->comps[compno];
283             for (resno = 0; resno < comp->numresolutions; resno++) {
284                 int dx, dy;
285                 res = &comp->resolutions[resno];
286                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
287                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
288                 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
289                 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
290             }
291         }
292     }
293     if (!pi->tp_on) {
294         pi->poc.ty0 = pi->ty0;
295         pi->poc.tx0 = pi->tx0;
296         pi->poc.ty1 = pi->ty1;
297         pi->poc.tx1 = pi->tx1;
298     }
299     for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
300             pi->y += pi->dy - (pi->y % pi->dy)) {
301         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
302                 pi->x += pi->dx - (pi->x % pi->dx)) {
303             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
304                 comp = &pi->comps[pi->compno];
305                 for (pi->resno = pi->poc.resno0;
306                         pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
307                     int levelno;
308                     int trx0, try0;
309                     int trx1, try1;
310                     int rpx, rpy;
311                     int prci, prcj;
312                     res = &comp->resolutions[pi->resno];
313                     levelno = comp->numresolutions - 1 - pi->resno;
314                     trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
315                     try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
316                     trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
317                     try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
318                     rpx = res->pdx + levelno;
319                     rpy = res->pdy + levelno;
320                     if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
321                             ((try0 << levelno) % (1 << rpy))))) {
322                         continue;
323                     }
324                     if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
325                             ((trx0 << levelno) % (1 << rpx))))) {
326                         continue;
327                     }
328
329                     if ((res->pw == 0) || (res->ph == 0)) {
330                         continue;
331                     }
332
333                     if ((trx0 == trx1) || (try0 == try1)) {
334                         continue;
335                     }
336
337                     prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
338                            - int_floordivpow2(trx0, res->pdx);
339                     prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
340                            - int_floordivpow2(try0, res->pdy);
341                     pi->precno = prci + prcj * res->pw;
342                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
343                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
344                                 pi->step_c + pi->precno * pi->step_p;
345                         if (!pi->include[index]) {
346                             pi->include[index] = 1;
347                             return OPJ_TRUE;
348                         }
349 LABEL_SKIP:
350                         ;
351                     }
352                 }
353             }
354         }
355     }
356
357     return OPJ_FALSE;
358 }
359
360 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi)
361 {
362     opj_pi_comp_t *comp = NULL;
363     opj_pi_resolution_t *res = NULL;
364     long index = 0;
365
366     if (!pi->first) {
367         comp = &pi->comps[pi->compno];
368         goto LABEL_SKIP;
369     } else {
370         pi->first = 0;
371     }
372
373     for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
374         int resno;
375         comp = &pi->comps[pi->compno];
376         pi->dx = 0;
377         pi->dy = 0;
378         for (resno = 0; resno < comp->numresolutions; resno++) {
379             int dx, dy;
380             res = &comp->resolutions[resno];
381             dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
382             dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
383             pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
384             pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
385         }
386         if (!pi->tp_on) {
387             pi->poc.ty0 = pi->ty0;
388             pi->poc.tx0 = pi->tx0;
389             pi->poc.ty1 = pi->ty1;
390             pi->poc.tx1 = pi->tx1;
391         }
392         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
393                 pi->y += pi->dy - (pi->y % pi->dy)) {
394             for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
395                     pi->x += pi->dx - (pi->x % pi->dx)) {
396                 for (pi->resno = pi->poc.resno0;
397                         pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
398                     int levelno;
399                     int trx0, try0;
400                     int trx1, try1;
401                     int rpx, rpy;
402                     int prci, prcj;
403                     res = &comp->resolutions[pi->resno];
404                     levelno = comp->numresolutions - 1 - pi->resno;
405                     trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
406                     try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
407                     trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
408                     try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
409                     rpx = res->pdx + levelno;
410                     rpy = res->pdy + levelno;
411                     if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
412                             ((try0 << levelno) % (1 << rpy))))) {
413                         continue;
414                     }
415                     if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
416                             ((trx0 << levelno) % (1 << rpx))))) {
417                         continue;
418                     }
419
420                     if ((res->pw == 0) || (res->ph == 0)) {
421                         continue;
422                     }
423
424                     if ((trx0 == trx1) || (try0 == try1)) {
425                         continue;
426                     }
427
428                     prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
429                            - int_floordivpow2(trx0, res->pdx);
430                     prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
431                            - int_floordivpow2(try0, res->pdy);
432                     pi->precno = prci + prcj * res->pw;
433                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
434                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
435                                 pi->step_c + pi->precno * pi->step_p;
436                         if (!pi->include[index]) {
437                             pi->include[index] = 1;
438                             return OPJ_TRUE;
439                         }
440 LABEL_SKIP:
441                         ;
442                     }
443                 }
444             }
445         }
446     }
447
448     return OPJ_FALSE;
449 }
450
451 /*
452 ==========================================================
453    Packet iterator interface
454 ==========================================================
455 */
456
457 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp,
458                                     int tileno)
459 {
460     int p, q;
461     int compno, resno, pino;
462     opj_pi_iterator_t *pi = NULL;
463     opj_tcp_t *tcp = NULL;
464     opj_tccp_t *tccp = NULL;
465
466     tcp = &cp->tcps[tileno];
467
468     pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1),
469                                          sizeof(opj_pi_iterator_t));
470     if (!pi) {
471         /* TODO: throw an error */
472         return NULL;
473     }
474
475     for (pino = 0; pino < tcp->numpocs + 1; pino++) {   /* change */
476         int maxres = 0;
477         int maxprec = 0;
478         p = tileno % cp->tw;
479         q = tileno / cp->tw;
480
481         pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
482         pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
483         pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
484         pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
485         pi[pino].numcomps = image->numcomps;
486
487         pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
488                          sizeof(opj_pi_comp_t));
489         if (!pi[pino].comps) {
490             /* TODO: throw an error */
491             pi_destroy(pi, cp, tileno);
492             return NULL;
493         }
494
495         for (compno = 0; compno < pi->numcomps; compno++) {
496             int tcx0, tcy0, tcx1, tcy1;
497             opj_pi_comp_t *comp = &pi[pino].comps[compno];
498             tccp = &tcp->tccps[compno];
499             comp->dx = image->comps[compno].dx;
500             comp->dy = image->comps[compno].dy;
501             comp->numresolutions = tccp->numresolutions;
502
503             comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions,
504                                 sizeof(opj_pi_resolution_t));
505             if (!comp->resolutions) {
506                 /* TODO: throw an error */
507                 pi_destroy(pi, cp, tileno);
508                 return NULL;
509             }
510
511             tcx0 = int_ceildiv(pi->tx0, comp->dx);
512             tcy0 = int_ceildiv(pi->ty0, comp->dy);
513             tcx1 = int_ceildiv(pi->tx1, comp->dx);
514             tcy1 = int_ceildiv(pi->ty1, comp->dy);
515             if (comp->numresolutions > maxres) {
516                 maxres = comp->numresolutions;
517             }
518
519             for (resno = 0; resno < comp->numresolutions; resno++) {
520                 int levelno;
521                 int rx0, ry0, rx1, ry1;
522                 int px0, py0, px1, py1;
523                 opj_pi_resolution_t *res = &comp->resolutions[resno];
524                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
525                     res->pdx = tccp->prcw[resno];
526                     res->pdy = tccp->prch[resno];
527                 } else {
528                     res->pdx = 15;
529                     res->pdy = 15;
530                 }
531                 levelno = comp->numresolutions - 1 - resno;
532                 rx0 = int_ceildivpow2(tcx0, levelno);
533                 ry0 = int_ceildivpow2(tcy0, levelno);
534                 rx1 = int_ceildivpow2(tcx1, levelno);
535                 ry1 = int_ceildivpow2(tcy1, levelno);
536                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
537                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
538                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
539                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
540                 res->pw = (rx0 == rx1) ? 0 : ((px1 - px0) >> res->pdx);
541                 res->ph = (ry0 == ry1) ? 0 : ((py1 - py0) >> res->pdy);
542
543                 if (res->pw * res->ph > maxprec) {
544                     maxprec = res->pw * res->ph;
545                 }
546
547             }
548         }
549
550         tccp = &tcp->tccps[0];
551         pi[pino].step_p = 1;
552         pi[pino].step_c = maxprec * pi[pino].step_p;
553         pi[pino].step_r = image->numcomps * pi[pino].step_c;
554         pi[pino].step_l = maxres * pi[pino].step_r;
555
556         if (pino == 0) {
557             pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres *
558                                tcp->numlayers * maxprec, sizeof(short int));
559             if (!pi[pino].include) {
560                 /* TODO: throw an error */
561                 pi_destroy(pi, cp, tileno);
562                 return NULL;
563             }
564         } else {
565             pi[pino].include = pi[pino - 1].include;
566         }
567
568         if (tcp->POC == 0) {
569             pi[pino].first = 1;
570             pi[pino].poc.resno0 = 0;
571             pi[pino].poc.compno0 = 0;
572             pi[pino].poc.layno1 = tcp->numlayers;
573             pi[pino].poc.resno1 = maxres;
574             pi[pino].poc.compno1 = image->numcomps;
575             pi[pino].poc.prg = tcp->prg;
576         } else {
577             pi[pino].first = 1;
578             pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
579             pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
580             pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
581             pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
582             pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
583             pi[pino].poc.prg = tcp->pocs[pino].prg;
584         }
585         pi[pino].poc.layno0  = 0;
586         pi[pino].poc.precno0 = 0;
587         pi[pino].poc.precno1 = maxprec;
588
589     }
590
591     return pi;
592 }
593
594
595 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp,
596                                         int tileno, J2K_T2_MODE t2_mode)
597 {
598     int p, q, pino;
599     int compno, resno;
600     int maxres = 0;
601     int maxprec = 0;
602     opj_pi_iterator_t *pi = NULL;
603     opj_tcp_t *tcp = NULL;
604     opj_tccp_t *tccp = NULL;
605
606     tcp = &cp->tcps[tileno];
607
608     pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1),
609                                          sizeof(opj_pi_iterator_t));
610     if (!pi) {
611         return NULL;
612     }
613     pi->tp_on = cp->tp_on;
614
615     for (pino = 0; pino < tcp->numpocs + 1 ; pino ++) {
616         p = tileno % cp->tw;
617         q = tileno / cp->tw;
618
619         pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
620         pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
621         pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
622         pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
623         pi[pino].numcomps = image->numcomps;
624
625         pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
626                          sizeof(opj_pi_comp_t));
627         if (!pi[pino].comps) {
628             pi_destroy(pi, cp, tileno);
629             return NULL;
630         }
631
632         for (compno = 0; compno < pi[pino].numcomps; compno++) {
633             int tcx0, tcy0, tcx1, tcy1;
634             opj_pi_comp_t *comp = &pi[pino].comps[compno];
635             tccp = &tcp->tccps[compno];
636             comp->dx = image->comps[compno].dx;
637             comp->dy = image->comps[compno].dy;
638             comp->numresolutions = tccp->numresolutions;
639
640             comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions *
641                                 sizeof(opj_pi_resolution_t));
642             if (!comp->resolutions) {
643                 pi_destroy(pi, cp, tileno);
644                 return NULL;
645             }
646
647             tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
648             tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
649             tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
650             tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
651             if (comp->numresolutions > maxres) {
652                 maxres = comp->numresolutions;
653             }
654
655             for (resno = 0; resno < comp->numresolutions; resno++) {
656                 int levelno;
657                 int rx0, ry0, rx1, ry1;
658                 int px0, py0, px1, py1;
659                 opj_pi_resolution_t *res = &comp->resolutions[resno];
660                 if (tccp->csty & J2K_CCP_CSTY_PRT) {
661                     res->pdx = tccp->prcw[resno];
662                     res->pdy = tccp->prch[resno];
663                 } else {
664                     res->pdx = 15;
665                     res->pdy = 15;
666                 }
667                 levelno = comp->numresolutions - 1 - resno;
668                 rx0 = int_ceildivpow2(tcx0, levelno);
669                 ry0 = int_ceildivpow2(tcy0, levelno);
670                 rx1 = int_ceildivpow2(tcx1, levelno);
671                 ry1 = int_ceildivpow2(tcy1, levelno);
672                 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
673                 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
674                 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
675                 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
676                 res->pw = (rx0 == rx1) ? 0 : ((px1 - px0) >> res->pdx);
677                 res->ph = (ry0 == ry1) ? 0 : ((py1 - py0) >> res->pdy);
678
679                 if (res->pw * res->ph > maxprec) {
680                     maxprec = res->pw * res->ph;
681                 }
682             }
683         }
684
685         tccp = &tcp->tccps[0];
686         pi[pino].step_p = 1;
687         pi[pino].step_c = maxprec * pi[pino].step_p;
688         pi[pino].step_r = image->numcomps * pi[pino].step_c;
689         pi[pino].step_l = maxres * pi[pino].step_r;
690
691         for (compno = 0; compno < pi->numcomps; compno++) {
692             opj_pi_comp_t *comp = &pi->comps[compno];
693             for (resno = 0; resno < comp->numresolutions; resno++) {
694                 int dx, dy;
695                 opj_pi_resolution_t *res = &comp->resolutions[resno];
696                 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
697                 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
698                 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
699                 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
700             }
701         }
702
703         if (pino == 0) {
704             pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l,
705                                sizeof(short int));
706             if (!pi[pino].include) {
707                 pi_destroy(pi, cp, tileno);
708                 return NULL;
709             }
710         } else {
711             pi[pino].include = pi[pino - 1].include;
712         }
713
714         /* Generation of boundaries for each prog flag*/
715         if (tcp->POC && (cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))) {
716             tcp->pocs[pino].compS = tcp->pocs[pino].compno0;
717             tcp->pocs[pino].compE = tcp->pocs[pino].compno1;
718             tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
719             tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
720             tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
721             tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
722             if (pino > 0) {
723                 tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ?
724                                        tcp->pocs[pino - 1].layE : 0;
725             }
726         } else {
727             tcp->pocs[pino].compS = 0;
728             tcp->pocs[pino].compE = image->numcomps;
729             tcp->pocs[pino].resS = 0;
730             tcp->pocs[pino].resE = maxres;
731             tcp->pocs[pino].layS = 0;
732             tcp->pocs[pino].layE = tcp->numlayers;
733             tcp->pocs[pino].prg  = tcp->prg;
734         }
735         tcp->pocs[pino].prcS = 0;
736         tcp->pocs[pino].prcE = maxprec;;
737         tcp->pocs[pino].txS = pi[pino].tx0;
738         tcp->pocs[pino].txE = pi[pino].tx1;
739         tcp->pocs[pino].tyS = pi[pino].ty0;
740         tcp->pocs[pino].tyE = pi[pino].ty1;
741         tcp->pocs[pino].dx = pi[pino].dx;
742         tcp->pocs[pino].dy = pi[pino].dy;
743     }
744     return pi;
745 }
746
747
748
749 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno)
750 {
751     int compno, pino;
752     opj_tcp_t *tcp = &cp->tcps[tileno];
753     if (pi) {
754         for (pino = 0; pino < tcp->numpocs + 1; pino++) {
755             if (pi[pino].comps) {
756                 for (compno = 0; compno < pi->numcomps; compno++) {
757                     opj_pi_comp_t *comp = &pi[pino].comps[compno];
758                     if (comp->resolutions) {
759                         opj_free(comp->resolutions);
760                     }
761                 }
762                 opj_free(pi[pino].comps);
763             }
764         }
765         if (pi->include) {
766             opj_free(pi->include);
767         }
768         opj_free(pi);
769     }
770 }
771
772 opj_bool pi_next(opj_pi_iterator_t * pi)
773 {
774     switch (pi->poc.prg) {
775     case LRCP:
776         return pi_next_lrcp(pi);
777     case RLCP:
778         return pi_next_rlcp(pi);
779     case RPCL:
780         return pi_next_rpcl(pi);
781     case PCRL:
782         return pi_next_pcrl(pi);
783     case CPRL:
784         return pi_next_cprl(pi);
785     case PROG_UNKNOWN:
786         return OPJ_FALSE;
787     }
788
789     return OPJ_FALSE;
790 }
791
792 opj_bool pi_create_encode(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno,
793                           int pino, int tpnum, int tppos, J2K_T2_MODE t2_mode, int cur_totnum_tp)
794 {
795     char prog[4];
796     int i;
797     int incr_top = 1, resetX = 0;
798     opj_tcp_t *tcps = &cp->tcps[tileno];
799     opj_poc_t *tcp = &tcps->pocs[pino];
800
801     pi[pino].first = 1;
802     pi[pino].poc.prg = tcp->prg;
803
804     switch (tcp->prg) {
805     case CPRL:
806         strncpy(prog, "CPRL", 4);
807         break;
808     case LRCP:
809         strncpy(prog, "LRCP", 4);
810         break;
811     case PCRL:
812         strncpy(prog, "PCRL", 4);
813         break;
814     case RLCP:
815         strncpy(prog, "RLCP", 4);
816         break;
817     case RPCL:
818         strncpy(prog, "RPCL", 4);
819         break;
820     case PROG_UNKNOWN:
821         return OPJ_TRUE;
822     }
823
824     if (!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))) {
825         pi[pino].poc.resno0 = tcp->resS;
826         pi[pino].poc.resno1 = tcp->resE;
827         pi[pino].poc.compno0 = tcp->compS;
828         pi[pino].poc.compno1 = tcp->compE;
829         pi[pino].poc.layno0 = tcp->layS;
830         pi[pino].poc.layno1 = tcp->layE;
831         pi[pino].poc.precno0 = tcp->prcS;
832         pi[pino].poc.precno1 = tcp->prcE;
833         pi[pino].poc.tx0 = tcp->txS;
834         pi[pino].poc.ty0 = tcp->tyS;
835         pi[pino].poc.tx1 = tcp->txE;
836         pi[pino].poc.ty1 = tcp->tyE;
837     } else {
838         if (tpnum < cur_totnum_tp) {
839             for (i = 3; i >= 0; i--) {
840                 switch (prog[i]) {
841                 case 'C':
842                     if (i > tppos) {
843                         pi[pino].poc.compno0 = tcp->compS;
844                         pi[pino].poc.compno1 = tcp->compE;
845                     } else {
846                         if (tpnum == 0) {
847                             tcp->comp_t = tcp->compS;
848                             pi[pino].poc.compno0 = tcp->comp_t;
849                             pi[pino].poc.compno1 = tcp->comp_t + 1;
850                             tcp->comp_t += 1;
851                         } else {
852                             if (incr_top == 1) {
853                                 if (tcp->comp_t == tcp->compE) {
854                                     tcp->comp_t = tcp->compS;
855                                     pi[pino].poc.compno0 = tcp->comp_t;
856                                     pi[pino].poc.compno1 = tcp->comp_t + 1;
857                                     tcp->comp_t += 1;
858                                     incr_top = 1;
859                                 } else {
860                                     pi[pino].poc.compno0 = tcp->comp_t;
861                                     pi[pino].poc.compno1 = tcp->comp_t + 1;
862                                     tcp->comp_t += 1;
863                                     incr_top = 0;
864                                 }
865                             } else {
866                                 pi[pino].poc.compno0 = tcp->comp_t - 1;
867                                 pi[pino].poc.compno1 = tcp->comp_t;
868                             }
869                         }
870                     }
871                     break;
872
873                 case 'R':
874                     if (i > tppos) {
875                         pi[pino].poc.resno0 = tcp->resS;
876                         pi[pino].poc.resno1 = tcp->resE;
877                     } else {
878                         if (tpnum == 0) {
879                             tcp->res_t = tcp->resS;
880                             pi[pino].poc.resno0 = tcp->res_t;
881                             pi[pino].poc.resno1 = tcp->res_t + 1;
882                             tcp->res_t += 1;
883                         } else {
884                             if (incr_top == 1) {
885                                 if (tcp->res_t == tcp->resE) {
886                                     tcp->res_t = tcp->resS;
887                                     pi[pino].poc.resno0 = tcp->res_t;
888                                     pi[pino].poc.resno1 = tcp->res_t + 1;
889                                     tcp->res_t += 1;
890                                     incr_top = 1;
891                                 } else {
892                                     pi[pino].poc.resno0 = tcp->res_t;
893                                     pi[pino].poc.resno1 = tcp->res_t + 1;
894                                     tcp->res_t += 1;
895                                     incr_top = 0;
896                                 }
897                             } else {
898                                 pi[pino].poc.resno0 = tcp->res_t - 1;
899                                 pi[pino].poc.resno1 = tcp->res_t;
900                             }
901                         }
902                     }
903                     break;
904
905                 case 'L':
906                     if (i > tppos) {
907                         pi[pino].poc.layno0 = tcp->layS;
908                         pi[pino].poc.layno1 = tcp->layE;
909                     } else {
910                         if (tpnum == 0) {
911                             tcp->lay_t = tcp->layS;
912                             pi[pino].poc.layno0 = tcp->lay_t;
913                             pi[pino].poc.layno1 = tcp->lay_t + 1;
914                             tcp->lay_t += 1;
915                         } else {
916                             if (incr_top == 1) {
917                                 if (tcp->lay_t == tcp->layE) {
918                                     tcp->lay_t = tcp->layS;
919                                     pi[pino].poc.layno0 = tcp->lay_t;
920                                     pi[pino].poc.layno1 = tcp->lay_t + 1;
921                                     tcp->lay_t += 1;
922                                     incr_top = 1;
923                                 } else {
924                                     pi[pino].poc.layno0 = tcp->lay_t;
925                                     pi[pino].poc.layno1 = tcp->lay_t + 1;
926                                     tcp->lay_t += 1;
927                                     incr_top = 0;
928                                 }
929                             } else {
930                                 pi[pino].poc.layno0 = tcp->lay_t - 1;
931                                 pi[pino].poc.layno1 = tcp->lay_t;
932                             }
933                         }
934                     }
935                     break;
936
937                 case 'P':
938                     switch (tcp->prg) {
939                     case LRCP:
940                     case RLCP:
941                         if (i > tppos) {
942                             pi[pino].poc.precno0 = tcp->prcS;
943                             pi[pino].poc.precno1 = tcp->prcE;
944                         } else {
945                             if (tpnum == 0) {
946                                 tcp->prc_t = tcp->prcS;
947                                 pi[pino].poc.precno0 = tcp->prc_t;
948                                 pi[pino].poc.precno1 = tcp->prc_t + 1;
949                                 tcp->prc_t += 1;
950                             } else {
951                                 if (incr_top == 1) {
952                                     if (tcp->prc_t == tcp->prcE) {
953                                         tcp->prc_t = tcp->prcS;
954                                         pi[pino].poc.precno0 = tcp->prc_t;
955                                         pi[pino].poc.precno1 = tcp->prc_t + 1;
956                                         tcp->prc_t += 1;
957                                         incr_top = 1;
958                                     } else {
959                                         pi[pino].poc.precno0 = tcp->prc_t;
960                                         pi[pino].poc.precno1 = tcp->prc_t + 1;
961                                         tcp->prc_t += 1;
962                                         incr_top = 0;
963                                     }
964                                 } else {
965                                     pi[pino].poc.precno0 = tcp->prc_t - 1;
966                                     pi[pino].poc.precno1 = tcp->prc_t;
967                                 }
968                             }
969                         }
970                         break;
971                     default:
972                         if (i > tppos) {
973                             pi[pino].poc.tx0 = tcp->txS;
974                             pi[pino].poc.ty0 = tcp->tyS;
975                             pi[pino].poc.tx1 = tcp->txE;
976                             pi[pino].poc.ty1 = tcp->tyE;
977                         } else {
978                             if (tpnum == 0) {
979                                 tcp->tx0_t = tcp->txS;
980                                 tcp->ty0_t = tcp->tyS;
981                                 pi[pino].poc.tx0 = tcp->tx0_t;
982                                 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
983                                 pi[pino].poc.ty0 = tcp->ty0_t;
984                                 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
985                                 tcp->tx0_t = pi[pino].poc.tx1;
986                                 tcp->ty0_t = pi[pino].poc.ty1;
987                             } else {
988                                 if (incr_top == 1) {
989                                     if (tcp->tx0_t >= tcp->txE) {
990                                         if (tcp->ty0_t >= tcp->tyE) {
991                                             tcp->ty0_t = tcp->tyS;
992                                             pi[pino].poc.ty0 = tcp->ty0_t;
993                                             pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
994                                             tcp->ty0_t = pi[pino].poc.ty1;
995                                             incr_top = 1;
996                                             resetX = 1;
997                                         } else {
998                                             pi[pino].poc.ty0 = tcp->ty0_t;
999                                             pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1000                                             tcp->ty0_t = pi[pino].poc.ty1;
1001                                             incr_top = 0;
1002                                             resetX = 1;
1003                                         }
1004                                         if (resetX == 1) {
1005                                             tcp->tx0_t = tcp->txS;
1006                                             pi[pino].poc.tx0 = tcp->tx0_t;
1007                                             pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1008                                             tcp->tx0_t = pi[pino].poc.tx1;
1009                                         }
1010                                     } else {
1011                                         pi[pino].poc.tx0 = tcp->tx0_t;
1012                                         pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1013                                         tcp->tx0_t = pi[pino].poc.tx1;
1014                                         pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1015                                         pi[pino].poc.ty1 = tcp->ty0_t ;
1016                                         incr_top = 0;
1017                                     }
1018                                 } else {
1019                                     pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1020                                     pi[pino].poc.tx1 = tcp->tx0_t ;
1021                                     pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1022                                     pi[pino].poc.ty1 = tcp->ty0_t ;
1023                                 }
1024                             }
1025                         }
1026                         break;
1027                     }
1028                     break;
1029                 }
1030             }
1031         }
1032     }
1033     return OPJ_FALSE;
1034 }
1035