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