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 /* To avoid divisions by zero / undefined behaviour on shift */
427 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
428 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
429 continue;
430 }
431
432 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
433 ((try0 << levelno) % (1 << rpy))))) {
434 continue;
435 }
436 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
437 ((trx0 << levelno) % (1 << rpx))))) {
438 continue;
439 }
440
441 if ((res->pw == 0) || (res->ph == 0)) {
442 continue;
443 }
444
445 if ((trx0 == trx1) || (try0 == try1)) {
446 continue;
447 }
448
449 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
450 - int_floordivpow2(trx0, res->pdx);
451 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
452 - int_floordivpow2(try0, res->pdy);
453 pi->precno = prci + prcj * res->pw;
454 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
455 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
456 pi->step_c + pi->precno * pi->step_p;
457 if (!pi->include[index]) {
458 pi->include[index] = 1;
459 return OPJ_TRUE;
460 }
461 LABEL_SKIP:
462 ;
463 }
464 }
465 }
466 }
467 }
468
469 return OPJ_FALSE;
470 }
471
472 /*
473 ==========================================================
474 Packet iterator interface
475 ==========================================================
476 */
477
pi_create_decode(opj_image_t * image,opj_cp_t * cp,int tileno)478 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp,
479 int tileno)
480 {
481 int p, q;
482 int compno, resno, pino;
483 opj_pi_iterator_t *pi = NULL;
484 opj_tcp_t *tcp = NULL;
485 opj_tccp_t *tccp = NULL;
486
487 tcp = &cp->tcps[tileno];
488
489 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1),
490 sizeof(opj_pi_iterator_t));
491 if (!pi) {
492 /* TODO: throw an error */
493 return NULL;
494 }
495
496 for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
497 int maxres = 0;
498 int maxprec = 0;
499 p = tileno % cp->tw;
500 q = tileno / cp->tw;
501
502 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
503 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
504 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
505 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
506 pi[pino].numcomps = image->numcomps;
507
508 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
509 sizeof(opj_pi_comp_t));
510 if (!pi[pino].comps) {
511 /* TODO: throw an error */
512 pi_destroy(pi, cp, tileno);
513 return NULL;
514 }
515
516 for (compno = 0; compno < pi->numcomps; compno++) {
517 int tcx0, tcy0, tcx1, tcy1;
518 opj_pi_comp_t *comp = &pi[pino].comps[compno];
519 tccp = &tcp->tccps[compno];
520 comp->dx = image->comps[compno].dx;
521 comp->dy = image->comps[compno].dy;
522 comp->numresolutions = tccp->numresolutions;
523
524 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions,
525 sizeof(opj_pi_resolution_t));
526 if (!comp->resolutions) {
527 /* TODO: throw an error */
528 pi_destroy(pi, cp, tileno);
529 return NULL;
530 }
531
532 tcx0 = int_ceildiv(pi->tx0, comp->dx);
533 tcy0 = int_ceildiv(pi->ty0, comp->dy);
534 tcx1 = int_ceildiv(pi->tx1, comp->dx);
535 tcy1 = int_ceildiv(pi->ty1, comp->dy);
536 if (comp->numresolutions > maxres) {
537 maxres = comp->numresolutions;
538 }
539
540 for (resno = 0; resno < comp->numresolutions; resno++) {
541 int levelno;
542 int rx0, ry0, rx1, ry1;
543 int px0, py0, px1, py1;
544 opj_pi_resolution_t *res = &comp->resolutions[resno];
545 if (tccp->csty & J2K_CCP_CSTY_PRT) {
546 res->pdx = tccp->prcw[resno];
547 res->pdy = tccp->prch[resno];
548 } else {
549 res->pdx = 15;
550 res->pdy = 15;
551 }
552 levelno = comp->numresolutions - 1 - resno;
553 rx0 = int_ceildivpow2(tcx0, levelno);
554 ry0 = int_ceildivpow2(tcy0, levelno);
555 rx1 = int_ceildivpow2(tcx1, levelno);
556 ry1 = int_ceildivpow2(tcy1, levelno);
557 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
558 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
559 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
560 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
561 res->pw = (rx0 == rx1) ? 0 : ((px1 - px0) >> res->pdx);
562 res->ph = (ry0 == ry1) ? 0 : ((py1 - py0) >> res->pdy);
563
564 if (res->pw * res->ph > maxprec) {
565 maxprec = res->pw * res->ph;
566 }
567
568 }
569 }
570
571 tccp = &tcp->tccps[0];
572 pi[pino].step_p = 1;
573 pi[pino].step_c = maxprec * pi[pino].step_p;
574 pi[pino].step_r = image->numcomps * pi[pino].step_c;
575 pi[pino].step_l = maxres * pi[pino].step_r;
576
577 if (pino == 0) {
578 pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres *
579 tcp->numlayers * maxprec, sizeof(short int));
580 if (!pi[pino].include) {
581 /* TODO: throw an error */
582 pi_destroy(pi, cp, tileno);
583 return NULL;
584 }
585 } else {
586 pi[pino].include = pi[pino - 1].include;
587 }
588
589 if (tcp->POC == 0) {
590 pi[pino].first = 1;
591 pi[pino].poc.resno0 = 0;
592 pi[pino].poc.compno0 = 0;
593 pi[pino].poc.layno1 = tcp->numlayers;
594 pi[pino].poc.resno1 = maxres;
595 pi[pino].poc.compno1 = image->numcomps;
596 pi[pino].poc.prg = tcp->prg;
597 } else {
598 pi[pino].first = 1;
599 pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
600 pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
601 pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
602 pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
603 pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
604 pi[pino].poc.prg = tcp->pocs[pino].prg;
605 }
606 pi[pino].poc.layno0 = 0;
607 pi[pino].poc.precno0 = 0;
608 pi[pino].poc.precno1 = maxprec;
609
610 }
611
612 return pi;
613 }
614
615
pi_initialise_encode(opj_image_t * image,opj_cp_t * cp,int tileno,J2K_T2_MODE t2_mode)616 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp,
617 int tileno, J2K_T2_MODE t2_mode)
618 {
619 int p, q, pino;
620 int compno, resno;
621 int maxres = 0;
622 int maxprec = 0;
623 opj_pi_iterator_t *pi = NULL;
624 opj_tcp_t *tcp = NULL;
625 opj_tccp_t *tccp = NULL;
626
627 tcp = &cp->tcps[tileno];
628
629 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1),
630 sizeof(opj_pi_iterator_t));
631 if (!pi) {
632 return NULL;
633 }
634 pi->tp_on = cp->tp_on;
635
636 for (pino = 0; pino < tcp->numpocs + 1 ; pino ++) {
637 p = tileno % cp->tw;
638 q = tileno / cp->tw;
639
640 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
641 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
642 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
643 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
644 pi[pino].numcomps = image->numcomps;
645
646 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
647 sizeof(opj_pi_comp_t));
648 if (!pi[pino].comps) {
649 pi_destroy(pi, cp, tileno);
650 return NULL;
651 }
652
653 for (compno = 0; compno < pi[pino].numcomps; compno++) {
654 int tcx0, tcy0, tcx1, tcy1;
655 opj_pi_comp_t *comp = &pi[pino].comps[compno];
656 tccp = &tcp->tccps[compno];
657 comp->dx = image->comps[compno].dx;
658 comp->dy = image->comps[compno].dy;
659 comp->numresolutions = tccp->numresolutions;
660
661 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions *
662 sizeof(opj_pi_resolution_t));
663 if (!comp->resolutions) {
664 pi_destroy(pi, cp, tileno);
665 return NULL;
666 }
667
668 tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
669 tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
670 tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
671 tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
672 if (comp->numresolutions > maxres) {
673 maxres = comp->numresolutions;
674 }
675
676 for (resno = 0; resno < comp->numresolutions; resno++) {
677 int levelno;
678 int rx0, ry0, rx1, ry1;
679 int px0, py0, px1, py1;
680 opj_pi_resolution_t *res = &comp->resolutions[resno];
681 if (tccp->csty & J2K_CCP_CSTY_PRT) {
682 res->pdx = tccp->prcw[resno];
683 res->pdy = tccp->prch[resno];
684 } else {
685 res->pdx = 15;
686 res->pdy = 15;
687 }
688 levelno = comp->numresolutions - 1 - resno;
689 rx0 = int_ceildivpow2(tcx0, levelno);
690 ry0 = int_ceildivpow2(tcy0, levelno);
691 rx1 = int_ceildivpow2(tcx1, levelno);
692 ry1 = int_ceildivpow2(tcy1, levelno);
693 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
694 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
695 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
696 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
697 res->pw = (rx0 == rx1) ? 0 : ((px1 - px0) >> res->pdx);
698 res->ph = (ry0 == ry1) ? 0 : ((py1 - py0) >> res->pdy);
699
700 if (res->pw * res->ph > maxprec) {
701 maxprec = res->pw * res->ph;
702 }
703 }
704 }
705
706 tccp = &tcp->tccps[0];
707 pi[pino].step_p = 1;
708 pi[pino].step_c = maxprec * pi[pino].step_p;
709 pi[pino].step_r = image->numcomps * pi[pino].step_c;
710 pi[pino].step_l = maxres * pi[pino].step_r;
711
712 for (compno = 0; compno < pi->numcomps; compno++) {
713 opj_pi_comp_t *comp = &pi->comps[compno];
714 for (resno = 0; resno < comp->numresolutions; resno++) {
715 int dx, dy;
716 opj_pi_resolution_t *res = &comp->resolutions[resno];
717 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
718 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
719 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
720 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
721 }
722 }
723
724 if (pino == 0) {
725 pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l,
726 sizeof(short int));
727 if (!pi[pino].include) {
728 pi_destroy(pi, cp, tileno);
729 return NULL;
730 }
731 } else {
732 pi[pino].include = pi[pino - 1].include;
733 }
734
735 /* Generation of boundaries for each prog flag*/
736 if (tcp->POC && (cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))) {
737 tcp->pocs[pino].compS = tcp->pocs[pino].compno0;
738 tcp->pocs[pino].compE = tcp->pocs[pino].compno1;
739 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
740 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
741 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
742 tcp->pocs[pino].prg = tcp->pocs[pino].prg1;
743 if (pino > 0) {
744 tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ?
745 tcp->pocs[pino - 1].layE : 0;
746 }
747 } else {
748 tcp->pocs[pino].compS = 0;
749 tcp->pocs[pino].compE = image->numcomps;
750 tcp->pocs[pino].resS = 0;
751 tcp->pocs[pino].resE = maxres;
752 tcp->pocs[pino].layS = 0;
753 tcp->pocs[pino].layE = tcp->numlayers;
754 tcp->pocs[pino].prg = tcp->prg;
755 }
756 tcp->pocs[pino].prcS = 0;
757 tcp->pocs[pino].prcE = maxprec;;
758 tcp->pocs[pino].txS = pi[pino].tx0;
759 tcp->pocs[pino].txE = pi[pino].tx1;
760 tcp->pocs[pino].tyS = pi[pino].ty0;
761 tcp->pocs[pino].tyE = pi[pino].ty1;
762 tcp->pocs[pino].dx = pi[pino].dx;
763 tcp->pocs[pino].dy = pi[pino].dy;
764 }
765 return pi;
766 }
767
768
769
pi_destroy(opj_pi_iterator_t * pi,opj_cp_t * cp,int tileno)770 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno)
771 {
772 int compno, pino;
773 opj_tcp_t *tcp = &cp->tcps[tileno];
774 if (pi) {
775 for (pino = 0; pino < tcp->numpocs + 1; pino++) {
776 if (pi[pino].comps) {
777 for (compno = 0; compno < pi->numcomps; compno++) {
778 opj_pi_comp_t *comp = &pi[pino].comps[compno];
779 if (comp->resolutions) {
780 opj_free(comp->resolutions);
781 }
782 }
783 opj_free(pi[pino].comps);
784 }
785 }
786 if (pi->include) {
787 opj_free(pi->include);
788 }
789 opj_free(pi);
790 }
791 }
792
pi_next(opj_pi_iterator_t * pi)793 opj_bool pi_next(opj_pi_iterator_t * pi)
794 {
795 switch (pi->poc.prg) {
796 case LRCP:
797 return pi_next_lrcp(pi);
798 case RLCP:
799 return pi_next_rlcp(pi);
800 case RPCL:
801 return pi_next_rpcl(pi);
802 case PCRL:
803 return pi_next_pcrl(pi);
804 case CPRL:
805 return pi_next_cprl(pi);
806 case PROG_UNKNOWN:
807 return OPJ_FALSE;
808 }
809
810 return OPJ_FALSE;
811 }
812
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)813 opj_bool pi_create_encode(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno,
814 int pino, int tpnum, int tppos, J2K_T2_MODE t2_mode, int cur_totnum_tp)
815 {
816 char prog[4];
817 int i;
818 int incr_top = 1, resetX = 0;
819 opj_tcp_t *tcps = &cp->tcps[tileno];
820 opj_poc_t *tcp = &tcps->pocs[pino];
821
822 pi[pino].first = 1;
823 pi[pino].poc.prg = tcp->prg;
824
825 switch (tcp->prg) {
826 case CPRL:
827 strncpy(prog, "CPRL", 4);
828 break;
829 case LRCP:
830 strncpy(prog, "LRCP", 4);
831 break;
832 case PCRL:
833 strncpy(prog, "PCRL", 4);
834 break;
835 case RLCP:
836 strncpy(prog, "RLCP", 4);
837 break;
838 case RPCL:
839 strncpy(prog, "RPCL", 4);
840 break;
841 case PROG_UNKNOWN:
842 return OPJ_TRUE;
843 }
844
845 if (!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))) {
846 pi[pino].poc.resno0 = tcp->resS;
847 pi[pino].poc.resno1 = tcp->resE;
848 pi[pino].poc.compno0 = tcp->compS;
849 pi[pino].poc.compno1 = tcp->compE;
850 pi[pino].poc.layno0 = tcp->layS;
851 pi[pino].poc.layno1 = tcp->layE;
852 pi[pino].poc.precno0 = tcp->prcS;
853 pi[pino].poc.precno1 = tcp->prcE;
854 pi[pino].poc.tx0 = tcp->txS;
855 pi[pino].poc.ty0 = tcp->tyS;
856 pi[pino].poc.tx1 = tcp->txE;
857 pi[pino].poc.ty1 = tcp->tyE;
858 } else {
859 if (tpnum < cur_totnum_tp) {
860 for (i = 3; i >= 0; i--) {
861 switch (prog[i]) {
862 case 'C':
863 if (i > tppos) {
864 pi[pino].poc.compno0 = tcp->compS;
865 pi[pino].poc.compno1 = tcp->compE;
866 } else {
867 if (tpnum == 0) {
868 tcp->comp_t = tcp->compS;
869 pi[pino].poc.compno0 = tcp->comp_t;
870 pi[pino].poc.compno1 = tcp->comp_t + 1;
871 tcp->comp_t += 1;
872 } else {
873 if (incr_top == 1) {
874 if (tcp->comp_t == tcp->compE) {
875 tcp->comp_t = tcp->compS;
876 pi[pino].poc.compno0 = tcp->comp_t;
877 pi[pino].poc.compno1 = tcp->comp_t + 1;
878 tcp->comp_t += 1;
879 incr_top = 1;
880 } else {
881 pi[pino].poc.compno0 = tcp->comp_t;
882 pi[pino].poc.compno1 = tcp->comp_t + 1;
883 tcp->comp_t += 1;
884 incr_top = 0;
885 }
886 } else {
887 pi[pino].poc.compno0 = tcp->comp_t - 1;
888 pi[pino].poc.compno1 = tcp->comp_t;
889 }
890 }
891 }
892 break;
893
894 case 'R':
895 if (i > tppos) {
896 pi[pino].poc.resno0 = tcp->resS;
897 pi[pino].poc.resno1 = tcp->resE;
898 } else {
899 if (tpnum == 0) {
900 tcp->res_t = tcp->resS;
901 pi[pino].poc.resno0 = tcp->res_t;
902 pi[pino].poc.resno1 = tcp->res_t + 1;
903 tcp->res_t += 1;
904 } else {
905 if (incr_top == 1) {
906 if (tcp->res_t == tcp->resE) {
907 tcp->res_t = tcp->resS;
908 pi[pino].poc.resno0 = tcp->res_t;
909 pi[pino].poc.resno1 = tcp->res_t + 1;
910 tcp->res_t += 1;
911 incr_top = 1;
912 } else {
913 pi[pino].poc.resno0 = tcp->res_t;
914 pi[pino].poc.resno1 = tcp->res_t + 1;
915 tcp->res_t += 1;
916 incr_top = 0;
917 }
918 } else {
919 pi[pino].poc.resno0 = tcp->res_t - 1;
920 pi[pino].poc.resno1 = tcp->res_t;
921 }
922 }
923 }
924 break;
925
926 case 'L':
927 if (i > tppos) {
928 pi[pino].poc.layno0 = tcp->layS;
929 pi[pino].poc.layno1 = tcp->layE;
930 } else {
931 if (tpnum == 0) {
932 tcp->lay_t = tcp->layS;
933 pi[pino].poc.layno0 = tcp->lay_t;
934 pi[pino].poc.layno1 = tcp->lay_t + 1;
935 tcp->lay_t += 1;
936 } else {
937 if (incr_top == 1) {
938 if (tcp->lay_t == tcp->layE) {
939 tcp->lay_t = tcp->layS;
940 pi[pino].poc.layno0 = tcp->lay_t;
941 pi[pino].poc.layno1 = tcp->lay_t + 1;
942 tcp->lay_t += 1;
943 incr_top = 1;
944 } else {
945 pi[pino].poc.layno0 = tcp->lay_t;
946 pi[pino].poc.layno1 = tcp->lay_t + 1;
947 tcp->lay_t += 1;
948 incr_top = 0;
949 }
950 } else {
951 pi[pino].poc.layno0 = tcp->lay_t - 1;
952 pi[pino].poc.layno1 = tcp->lay_t;
953 }
954 }
955 }
956 break;
957
958 case 'P':
959 switch (tcp->prg) {
960 case LRCP:
961 case RLCP:
962 if (i > tppos) {
963 pi[pino].poc.precno0 = tcp->prcS;
964 pi[pino].poc.precno1 = tcp->prcE;
965 } else {
966 if (tpnum == 0) {
967 tcp->prc_t = tcp->prcS;
968 pi[pino].poc.precno0 = tcp->prc_t;
969 pi[pino].poc.precno1 = tcp->prc_t + 1;
970 tcp->prc_t += 1;
971 } else {
972 if (incr_top == 1) {
973 if (tcp->prc_t == tcp->prcE) {
974 tcp->prc_t = tcp->prcS;
975 pi[pino].poc.precno0 = tcp->prc_t;
976 pi[pino].poc.precno1 = tcp->prc_t + 1;
977 tcp->prc_t += 1;
978 incr_top = 1;
979 } else {
980 pi[pino].poc.precno0 = tcp->prc_t;
981 pi[pino].poc.precno1 = tcp->prc_t + 1;
982 tcp->prc_t += 1;
983 incr_top = 0;
984 }
985 } else {
986 pi[pino].poc.precno0 = tcp->prc_t - 1;
987 pi[pino].poc.precno1 = tcp->prc_t;
988 }
989 }
990 }
991 break;
992 default:
993 if (i > tppos) {
994 pi[pino].poc.tx0 = tcp->txS;
995 pi[pino].poc.ty0 = tcp->tyS;
996 pi[pino].poc.tx1 = tcp->txE;
997 pi[pino].poc.ty1 = tcp->tyE;
998 } else {
999 if (tpnum == 0) {
1000 tcp->tx0_t = tcp->txS;
1001 tcp->ty0_t = tcp->tyS;
1002 pi[pino].poc.tx0 = tcp->tx0_t;
1003 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1004 pi[pino].poc.ty0 = tcp->ty0_t;
1005 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1006 tcp->tx0_t = pi[pino].poc.tx1;
1007 tcp->ty0_t = pi[pino].poc.ty1;
1008 } else {
1009 if (incr_top == 1) {
1010 if (tcp->tx0_t >= tcp->txE) {
1011 if (tcp->ty0_t >= tcp->tyE) {
1012 tcp->ty0_t = tcp->tyS;
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 = 1;
1017 resetX = 1;
1018 } else {
1019 pi[pino].poc.ty0 = tcp->ty0_t;
1020 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1021 tcp->ty0_t = pi[pino].poc.ty1;
1022 incr_top = 0;
1023 resetX = 1;
1024 }
1025 if (resetX == 1) {
1026 tcp->tx0_t = tcp->txS;
1027 pi[pino].poc.tx0 = tcp->tx0_t;
1028 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1029 tcp->tx0_t = pi[pino].poc.tx1;
1030 }
1031 } else {
1032 pi[pino].poc.tx0 = tcp->tx0_t;
1033 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1034 tcp->tx0_t = pi[pino].poc.tx1;
1035 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1036 pi[pino].poc.ty1 = tcp->ty0_t ;
1037 incr_top = 0;
1038 }
1039 } else {
1040 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1041 pi[pino].poc.tx1 = tcp->tx0_t ;
1042 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1043 pi[pino].poc.ty1 = tcp->ty0_t ;
1044 }
1045 }
1046 }
1047 break;
1048 }
1049 break;
1050 }
1051 }
1052 }
1053 }
1054 return OPJ_FALSE;
1055 }
1056
1057