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