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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include "opj_includes.h"
35 /** @defgroup PI PI - Implementation of a packet iterator */
38 /** @name Local static functions */
42 Get 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
46 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi);
48 Get 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
52 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi);
54 Get 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
58 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi);
60 Get 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
64 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi);
66 Get 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
70 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi);
75 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
76 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
77 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
78 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
79 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
81 * @param p_image the image being encoded.
82 * @param p_cp the coding parameters.
83 * @param tileno the tile index of the tile being encoded.
84 * @param p_tx0 pointer that will hold the X0 parameter for the tile
85 * @param p_tx1 pointer that will hold the X1 parameter for the tile
86 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
87 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
88 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
89 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
90 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
91 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
92 * @param p_resolutions pointer to an area corresponding to the one described above.
94 void get_all_encoding_parameters(
95 const opj_image_header_t *p_image,
96 const opj_cp_v2_t *p_cp,
102 OPJ_UINT32 * p_dx_min,
103 OPJ_UINT32 * p_dy_min,
104 OPJ_UINT32 * p_max_prec,
105 OPJ_UINT32 * p_max_res,
106 OPJ_UINT32 ** p_resolutions
111 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
112 * No other data is set. The include section of the packet iterator is not allocated.
114 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
115 * @param p_cp the coding parameters.
116 * @param p_tile_no the index of the tile from which creating the packet iterator.
118 opj_pi_iterator_t * pi_create(
119 const opj_image_header_t *image,
120 const opj_cp_v2_t *cp,
124 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
125 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res);
133 ==========================================================
135 ==========================================================
138 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
139 opj_pi_comp_t *comp = NULL;
140 opj_pi_resolution_t *res = NULL;
144 comp = &pi->comps[pi->compno];
145 res = &comp->resolutions[pi->resno];
151 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
152 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
154 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
155 comp = &pi->comps[pi->compno];
156 if (pi->resno >= comp->numresolutions) {
159 res = &comp->resolutions[pi->resno];
161 pi->poc.precno1 = res->pw * res->ph;
163 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
164 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
165 if (!pi->include[index]) {
166 pi->include[index] = 1;
178 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
179 opj_pi_comp_t *comp = NULL;
180 opj_pi_resolution_t *res = NULL;
184 comp = &pi->comps[pi->compno];
185 res = &comp->resolutions[pi->resno];
191 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
192 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
193 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
194 comp = &pi->comps[pi->compno];
195 if (pi->resno >= comp->numresolutions) {
198 res = &comp->resolutions[pi->resno];
200 pi->poc.precno1 = res->pw * res->ph;
202 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
203 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
204 if (!pi->include[index]) {
205 pi->include[index] = 1;
217 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
218 opj_pi_comp_t *comp = NULL;
219 opj_pi_resolution_t *res = NULL;
229 for (compno = 0; compno < pi->numcomps; compno++) {
230 comp = &pi->comps[compno];
231 for (resno = 0; resno < comp->numresolutions; resno++) {
233 res = &comp->resolutions[resno];
234 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
235 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
236 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
237 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
242 pi->poc.ty0 = pi->ty0;
243 pi->poc.tx0 = pi->tx0;
244 pi->poc.ty1 = pi->ty1;
245 pi->poc.tx1 = pi->tx1;
247 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
248 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
249 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
250 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
256 comp = &pi->comps[pi->compno];
257 if (pi->resno >= comp->numresolutions) {
260 res = &comp->resolutions[pi->resno];
261 levelno = comp->numresolutions - 1 - pi->resno;
262 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
263 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
264 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
265 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
266 rpx = res->pdx + levelno;
267 rpy = res->pdy + levelno;
268 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
271 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
275 if ((res->pw==0)||(res->ph==0)) continue;
277 if ((trx0==trx1)||(try0==try1)) continue;
279 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
280 - int_floordivpow2(trx0, res->pdx);
281 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
282 - int_floordivpow2(try0, res->pdy);
283 pi->precno = prci + prcj * res->pw;
284 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
285 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
286 if (!pi->include[index]) {
287 pi->include[index] = 1;
300 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
301 opj_pi_comp_t *comp = NULL;
302 opj_pi_resolution_t *res = NULL;
306 comp = &pi->comps[pi->compno];
313 for (compno = 0; compno < pi->numcomps; compno++) {
314 comp = &pi->comps[compno];
315 for (resno = 0; resno < comp->numresolutions; resno++) {
317 res = &comp->resolutions[resno];
318 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
319 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
320 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
321 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
326 pi->poc.ty0 = pi->ty0;
327 pi->poc.tx0 = pi->tx0;
328 pi->poc.ty1 = pi->ty1;
329 pi->poc.tx1 = pi->tx1;
331 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
332 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
333 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
334 comp = &pi->comps[pi->compno];
335 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
341 res = &comp->resolutions[pi->resno];
342 levelno = comp->numresolutions - 1 - pi->resno;
343 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
344 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
345 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
346 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
347 rpx = res->pdx + levelno;
348 rpy = res->pdy + levelno;
349 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
352 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
356 if ((res->pw==0)||(res->ph==0)) continue;
358 if ((trx0==trx1)||(try0==try1)) continue;
360 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
361 - int_floordivpow2(trx0, res->pdx);
362 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
363 - int_floordivpow2(try0, res->pdy);
364 pi->precno = prci + prcj * res->pw;
365 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
366 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
367 if (!pi->include[index]) {
368 pi->include[index] = 1;
381 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
382 opj_pi_comp_t *comp = NULL;
383 opj_pi_resolution_t *res = NULL;
387 comp = &pi->comps[pi->compno];
393 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
395 comp = &pi->comps[pi->compno];
398 for (resno = 0; resno < comp->numresolutions; resno++) {
400 res = &comp->resolutions[resno];
401 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
402 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
403 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
404 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
407 pi->poc.ty0 = pi->ty0;
408 pi->poc.tx0 = pi->tx0;
409 pi->poc.ty1 = pi->ty1;
410 pi->poc.tx1 = pi->tx1;
412 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
413 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
414 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
420 res = &comp->resolutions[pi->resno];
421 levelno = comp->numresolutions - 1 - pi->resno;
422 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
423 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
424 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
425 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
426 rpx = res->pdx + levelno;
427 rpy = res->pdy + levelno;
428 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
431 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
435 if ((res->pw==0)||(res->ph==0)) continue;
437 if ((trx0==trx1)||(try0==try1)) continue;
439 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
440 - int_floordivpow2(trx0, res->pdx);
441 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
442 - int_floordivpow2(try0, res->pdy);
443 pi->precno = prci + prcj * res->pw;
444 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
445 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
446 if (!pi->include[index]) {
447 pi->include[index] = 1;
461 ==========================================================
462 Packet iterator interface
463 ==========================================================
466 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
468 int compno, resno, pino;
469 opj_pi_iterator_t *pi = NULL;
470 opj_tcp_t *tcp = NULL;
471 opj_tccp_t *tccp = NULL;
473 tcp = &cp->tcps[tileno];
475 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
477 /* TODO: throw an error */
481 for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
487 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
488 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
489 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
490 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
491 pi[pino].numcomps = image->numcomps;
493 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
494 if(!pi[pino].comps) {
495 /* TODO: throw an error */
496 pi_destroy(pi, cp, tileno);
500 for (compno = 0; compno < pi->numcomps; compno++) {
501 int tcx0, tcy0, tcx1, tcy1;
502 opj_pi_comp_t *comp = &pi[pino].comps[compno];
503 tccp = &tcp->tccps[compno];
504 comp->dx = image->comps[compno].dx;
505 comp->dy = image->comps[compno].dy;
506 comp->numresolutions = tccp->numresolutions;
508 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
509 if(!comp->resolutions) {
510 /* TODO: throw an error */
511 pi_destroy(pi, cp, tileno);
515 tcx0 = int_ceildiv(pi->tx0, comp->dx);
516 tcy0 = int_ceildiv(pi->ty0, comp->dy);
517 tcx1 = int_ceildiv(pi->tx1, comp->dx);
518 tcy1 = int_ceildiv(pi->ty1, comp->dy);
519 if (comp->numresolutions > maxres) {
520 maxres = comp->numresolutions;
523 for (resno = 0; resno < comp->numresolutions; resno++) {
525 int rx0, ry0, rx1, ry1;
526 int px0, py0, px1, py1;
527 opj_pi_resolution_t *res = &comp->resolutions[resno];
528 if (tccp->csty & J2K_CCP_CSTY_PRT) {
529 res->pdx = tccp->prcw[resno];
530 res->pdy = tccp->prch[resno];
535 levelno = comp->numresolutions - 1 - resno;
536 rx0 = int_ceildivpow2(tcx0, levelno);
537 ry0 = int_ceildivpow2(tcy0, levelno);
538 rx1 = int_ceildivpow2(tcx1, levelno);
539 ry1 = int_ceildivpow2(tcy1, levelno);
540 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
541 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
542 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
543 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
544 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
545 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
547 if (res->pw*res->ph > maxprec) {
548 maxprec = res->pw*res->ph;
554 tccp = &tcp->tccps[0];
556 pi[pino].step_c = maxprec * pi[pino].step_p;
557 pi[pino].step_r = image->numcomps * pi[pino].step_c;
558 pi[pino].step_l = maxres * pi[pino].step_r;
561 pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
562 if(!pi[pino].include) {
563 /* TODO: throw an error */
564 pi_destroy(pi, cp, tileno);
569 pi[pino].include = pi[pino - 1].include;
574 pi[pino].poc.resno0 = 0;
575 pi[pino].poc.compno0 = 0;
576 pi[pino].poc.layno1 = tcp->numlayers;
577 pi[pino].poc.resno1 = maxres;
578 pi[pino].poc.compno1 = image->numcomps;
579 pi[pino].poc.prg = tcp->prg;
582 pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
583 pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
584 pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
585 pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
586 pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
587 pi[pino].poc.prg = tcp->pocs[pino].prg;
589 pi[pino].poc.layno0 = 0;
590 pi[pino].poc.precno0 = 0;
591 pi[pino].poc.precno1 = maxprec;
599 opj_pi_iterator_t *pi_create_decode_v2(
600 opj_image_header_t *p_image,
607 OPJ_UINT32 compno, resno;
609 // to store w, h, dx and dy fro all components and resolutions
610 OPJ_UINT32 * l_tmp_data;
611 OPJ_UINT32 ** l_tmp_ptr;
613 // encoding prameters to set
614 OPJ_UINT32 l_max_res;
615 OPJ_UINT32 l_max_prec;
616 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
617 OPJ_UINT32 l_dx_min,l_dy_min;
619 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
620 OPJ_UINT32 l_data_stride;
623 opj_pi_iterator_t *l_pi = 00;
624 opj_tcp_v2_t *l_tcp = 00;
625 const opj_tccp_t *l_tccp = 00;
626 opj_pi_comp_t *l_current_comp = 00;
627 opj_image_comp_header_t * l_img_comp = 00;
628 opj_pi_iterator_t * l_current_pi = 00;
629 OPJ_UINT32 * l_encoding_value_ptr = 00;
631 // preconditions in debug
633 assert(p_image != 00);
634 assert(p_tile_no < p_cp->tw * p_cp->th);
637 l_tcp = &p_cp->tcps[p_tile_no];
638 l_bound = l_tcp->numpocs+1;
640 l_data_stride = 4 * J2K_MAXRLVLS;
641 l_tmp_data = (OPJ_UINT32*)opj_malloc(
642 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
648 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
649 p_image->numcomps * sizeof(OPJ_UINT32 *));
653 opj_free(l_tmp_data);
657 // memory allocation for pi
658 l_pi = pi_create(p_image, p_cp, p_tile_no);
660 opj_free(l_tmp_data);
665 l_encoding_value_ptr = l_tmp_data;
666 // update pointer array
668 (compno = 0; compno < p_image->numcomps; ++compno)
670 l_tmp_ptr[compno] = l_encoding_value_ptr;
671 l_encoding_value_ptr += l_data_stride;
673 // get encoding parameters
674 get_all_encoding_parameters(p_image,p_cp,p_tile_no,&l_tx0,&l_tx1,&l_ty0,&l_ty1,&l_dx_min,&l_dy_min,&l_max_prec,&l_max_res,l_tmp_ptr);
678 l_step_c = l_max_prec * l_step_p;
679 l_step_r = p_image->numcomps * l_step_c;
680 l_step_l = l_max_res * l_step_r;
682 // set values for first packet iterator
685 // memory allocation for include
686 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
688 (!l_current_pi->include)
690 opj_free(l_tmp_data);
692 pi_destroy_v2(l_pi, l_bound);
695 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
697 // special treatment for the first packet iterator
698 l_current_comp = l_current_pi->comps;
699 l_img_comp = p_image->comps;
700 l_tccp = l_tcp->tccps;
702 l_current_pi->tx0 = l_tx0;
703 l_current_pi->ty0 = l_ty0;
704 l_current_pi->tx1 = l_tx1;
705 l_current_pi->ty1 = l_ty1;
707 //l_current_pi->dx = l_img_comp->dx;
708 //l_current_pi->dy = l_img_comp->dy;
710 l_current_pi->step_p = l_step_p;
711 l_current_pi->step_c = l_step_c;
712 l_current_pi->step_r = l_step_r;
713 l_current_pi->step_l = l_step_l;
715 /* allocation for components and number of components has already been calculated by pi_create */
717 (compno = 0; compno < l_current_pi->numcomps; ++compno)
719 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
720 l_encoding_value_ptr = l_tmp_ptr[compno];
722 l_current_comp->dx = l_img_comp->dx;
723 l_current_comp->dy = l_img_comp->dy;
724 /* resolutions have already been initialized */
726 (resno = 0; resno < l_current_comp->numresolutions; resno++)
728 l_res->pdx = *(l_encoding_value_ptr++);
729 l_res->pdy = *(l_encoding_value_ptr++);
730 l_res->pw = *(l_encoding_value_ptr++);
731 l_res->ph = *(l_encoding_value_ptr++);
741 (pino = 1 ; pino<l_bound ; ++pino )
743 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
744 opj_image_comp_header_t * l_img_comp = p_image->comps;
745 l_tccp = l_tcp->tccps;
747 l_current_pi->tx0 = l_tx0;
748 l_current_pi->ty0 = l_ty0;
749 l_current_pi->tx1 = l_tx1;
750 l_current_pi->ty1 = l_ty1;
751 //l_current_pi->dx = l_dx_min;
752 //l_current_pi->dy = l_dy_min;
753 l_current_pi->step_p = l_step_p;
754 l_current_pi->step_c = l_step_c;
755 l_current_pi->step_r = l_step_r;
756 l_current_pi->step_l = l_step_l;
758 /* allocation for components and number of components has already been calculated by pi_create */
760 (compno = 0; compno < l_current_pi->numcomps; ++compno)
762 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
763 l_encoding_value_ptr = l_tmp_ptr[compno];
765 l_current_comp->dx = l_img_comp->dx;
766 l_current_comp->dy = l_img_comp->dy;
767 /* resolutions have already been initialized */
769 (resno = 0; resno < l_current_comp->numresolutions; resno++)
771 l_res->pdx = *(l_encoding_value_ptr++);
772 l_res->pdy = *(l_encoding_value_ptr++);
773 l_res->pw = *(l_encoding_value_ptr++);
774 l_res->ph = *(l_encoding_value_ptr++);
782 l_current_pi->include = (l_current_pi-1)->include;
785 opj_free(l_tmp_data);
792 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
796 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
801 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){
806 opj_pi_iterator_t *pi = NULL;
807 opj_tcp_t *tcp = NULL;
808 opj_tccp_t *tccp = NULL;
810 tcp = &cp->tcps[tileno];
812 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
813 if(!pi) { return NULL;}
814 pi->tp_on = cp->tp_on;
816 for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
820 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
821 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
822 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
823 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
824 pi[pino].numcomps = image->numcomps;
826 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
827 if(!pi[pino].comps) {
828 pi_destroy(pi, cp, tileno);
832 for (compno = 0; compno < pi[pino].numcomps; compno++) {
833 int tcx0, tcy0, tcx1, tcy1;
834 opj_pi_comp_t *comp = &pi[pino].comps[compno];
835 tccp = &tcp->tccps[compno];
836 comp->dx = image->comps[compno].dx;
837 comp->dy = image->comps[compno].dy;
838 comp->numresolutions = tccp->numresolutions;
840 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
841 if(!comp->resolutions) {
842 pi_destroy(pi, cp, tileno);
846 tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
847 tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
848 tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
849 tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
850 if (comp->numresolutions > maxres) {
851 maxres = comp->numresolutions;
854 for (resno = 0; resno < comp->numresolutions; resno++) {
856 int rx0, ry0, rx1, ry1;
857 int px0, py0, px1, py1;
858 opj_pi_resolution_t *res = &comp->resolutions[resno];
859 if (tccp->csty & J2K_CCP_CSTY_PRT) {
860 res->pdx = tccp->prcw[resno];
861 res->pdy = tccp->prch[resno];
866 levelno = comp->numresolutions - 1 - resno;
867 rx0 = int_ceildivpow2(tcx0, levelno);
868 ry0 = int_ceildivpow2(tcy0, levelno);
869 rx1 = int_ceildivpow2(tcx1, levelno);
870 ry1 = int_ceildivpow2(tcy1, levelno);
871 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
872 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
873 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
874 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
875 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
876 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
878 if (res->pw*res->ph > maxprec) {
879 maxprec = res->pw * res->ph;
884 tccp = &tcp->tccps[0];
886 pi[pino].step_c = maxprec * pi[pino].step_p;
887 pi[pino].step_r = image->numcomps * pi[pino].step_c;
888 pi[pino].step_l = maxres * pi[pino].step_r;
890 for (compno = 0; compno < pi->numcomps; compno++) {
891 opj_pi_comp_t *comp = &pi->comps[compno];
892 for (resno = 0; resno < comp->numresolutions; resno++) {
894 opj_pi_resolution_t *res = &comp->resolutions[resno];
895 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
896 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
897 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
898 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
903 pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
904 if(!pi[pino].include) {
905 pi_destroy(pi, cp, tileno);
910 pi[pino].include = pi[pino - 1].include;
913 /* Generation of boundaries for each prog flag*/
914 if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
915 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
916 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
917 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
918 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
919 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
920 tcp->pocs[pino].prg = tcp->pocs[pino].prg1;
922 tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
924 tcp->pocs[pino].compS= 0;
925 tcp->pocs[pino].compE= image->numcomps;
926 tcp->pocs[pino].resS = 0;
927 tcp->pocs[pino].resE = maxres;
928 tcp->pocs[pino].layS = 0;
929 tcp->pocs[pino].layE = tcp->numlayers;
930 tcp->pocs[pino].prg = tcp->prg;
932 tcp->pocs[pino].prcS = 0;
933 tcp->pocs[pino].prcE = maxprec;;
934 tcp->pocs[pino].txS = pi[pino].tx0;
935 tcp->pocs[pino].txE = pi[pino].tx1;
936 tcp->pocs[pino].tyS = pi[pino].ty0;
937 tcp->pocs[pino].tyE = pi[pino].ty1;
938 tcp->pocs[pino].dx = pi[pino].dx;
939 tcp->pocs[pino].dy = pi[pino].dy;
946 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
948 opj_tcp_t *tcp = &cp->tcps[tileno];
950 for (pino = 0; pino < tcp->numpocs + 1; pino++) {
952 for (compno = 0; compno < pi->numcomps; compno++) {
953 opj_pi_comp_t *comp = &pi[pino].comps[compno];
954 if(comp->resolutions) {
955 opj_free(comp->resolutions);
958 opj_free(pi[pino].comps);
962 opj_free(pi->include);
968 opj_bool pi_next(opj_pi_iterator_t * pi) {
969 switch (pi->poc.prg) {
971 return pi_next_lrcp(pi);
973 return pi_next_rlcp(pi);
975 return pi_next_rpcl(pi);
977 return pi_next_pcrl(pi);
979 return pi_next_cprl(pi);
987 opj_bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp){
990 int incr_top=1,resetX=0;
991 opj_tcp_t *tcps =&cp->tcps[tileno];
992 opj_poc_t *tcp= &tcps->pocs[pino];
995 pi[pino].poc.prg = tcp->prg;
998 case CPRL: strncpy(prog, "CPRL",4);
1000 case LRCP: strncpy(prog, "LRCP",4);
1002 case PCRL: strncpy(prog, "PCRL",4);
1004 case RLCP: strncpy(prog, "RLCP",4);
1006 case RPCL: strncpy(prog, "RPCL",4);
1012 if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1013 pi[pino].poc.resno0 = tcp->resS;
1014 pi[pino].poc.resno1 = tcp->resE;
1015 pi[pino].poc.compno0 = tcp->compS;
1016 pi[pino].poc.compno1 = tcp->compE;
1017 pi[pino].poc.layno0 = tcp->layS;
1018 pi[pino].poc.layno1 = tcp->layE;
1019 pi[pino].poc.precno0 = tcp->prcS;
1020 pi[pino].poc.precno1 = tcp->prcE;
1021 pi[pino].poc.tx0 = tcp->txS;
1022 pi[pino].poc.ty0 = tcp->tyS;
1023 pi[pino].poc.tx1 = tcp->txE;
1024 pi[pino].poc.ty1 = tcp->tyE;
1026 if( tpnum < cur_totnum_tp){
1031 pi[pino].poc.compno0 = tcp->compS;
1032 pi[pino].poc.compno1 = tcp->compE;
1035 tcp->comp_t = tcp->compS;
1036 pi[pino].poc.compno0 = tcp->comp_t;
1037 pi[pino].poc.compno1 = tcp->comp_t+1;
1041 if(tcp->comp_t ==tcp->compE){
1042 tcp->comp_t = tcp->compS;
1043 pi[pino].poc.compno0 = tcp->comp_t;
1044 pi[pino].poc.compno1 = tcp->comp_t+1;
1048 pi[pino].poc.compno0 = tcp->comp_t;
1049 pi[pino].poc.compno1 = tcp->comp_t+1;
1054 pi[pino].poc.compno0 = tcp->comp_t-1;
1055 pi[pino].poc.compno1 = tcp->comp_t;
1063 pi[pino].poc.resno0 = tcp->resS;
1064 pi[pino].poc.resno1 = tcp->resE;
1067 tcp->res_t = tcp->resS;
1068 pi[pino].poc.resno0 = tcp->res_t;
1069 pi[pino].poc.resno1 = tcp->res_t+1;
1073 if(tcp->res_t==tcp->resE){
1074 tcp->res_t = tcp->resS;
1075 pi[pino].poc.resno0 = tcp->res_t;
1076 pi[pino].poc.resno1 = tcp->res_t+1;
1080 pi[pino].poc.resno0 = tcp->res_t;
1081 pi[pino].poc.resno1 = tcp->res_t+1;
1086 pi[pino].poc.resno0 = tcp->res_t - 1;
1087 pi[pino].poc.resno1 = tcp->res_t;
1095 pi[pino].poc.layno0 = tcp->layS;
1096 pi[pino].poc.layno1 = tcp->layE;
1099 tcp->lay_t = tcp->layS;
1100 pi[pino].poc.layno0 = tcp->lay_t;
1101 pi[pino].poc.layno1 = tcp->lay_t+1;
1105 if(tcp->lay_t == tcp->layE){
1106 tcp->lay_t = tcp->layS;
1107 pi[pino].poc.layno0 = tcp->lay_t;
1108 pi[pino].poc.layno1 = tcp->lay_t+1;
1112 pi[pino].poc.layno0 = tcp->lay_t;
1113 pi[pino].poc.layno1 = tcp->lay_t+1;
1118 pi[pino].poc.layno0 = tcp->lay_t - 1;
1119 pi[pino].poc.layno1 = tcp->lay_t;
1130 pi[pino].poc.precno0 = tcp->prcS;
1131 pi[pino].poc.precno1 = tcp->prcE;
1134 tcp->prc_t = tcp->prcS;
1135 pi[pino].poc.precno0 = tcp->prc_t;
1136 pi[pino].poc.precno1 = tcp->prc_t+1;
1140 if(tcp->prc_t == tcp->prcE){
1141 tcp->prc_t = tcp->prcS;
1142 pi[pino].poc.precno0 = tcp->prc_t;
1143 pi[pino].poc.precno1 = tcp->prc_t+1;
1147 pi[pino].poc.precno0 = tcp->prc_t;
1148 pi[pino].poc.precno1 = tcp->prc_t+1;
1153 pi[pino].poc.precno0 = tcp->prc_t - 1;
1154 pi[pino].poc.precno1 = tcp->prc_t;
1161 pi[pino].poc.tx0 = tcp->txS;
1162 pi[pino].poc.ty0 = tcp->tyS;
1163 pi[pino].poc.tx1 = tcp->txE;
1164 pi[pino].poc.ty1 = tcp->tyE;
1167 tcp->tx0_t = tcp->txS;
1168 tcp->ty0_t = tcp->tyS;
1169 pi[pino].poc.tx0 = tcp->tx0_t;
1170 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1171 pi[pino].poc.ty0 = tcp->ty0_t;
1172 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1173 tcp->tx0_t = pi[pino].poc.tx1;
1174 tcp->ty0_t = pi[pino].poc.ty1;
1177 if(tcp->tx0_t >= tcp->txE){
1178 if(tcp->ty0_t >= tcp->tyE){
1179 tcp->ty0_t = tcp->tyS;
1180 pi[pino].poc.ty0 = tcp->ty0_t;
1181 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1182 tcp->ty0_t = pi[pino].poc.ty1;
1183 incr_top=1;resetX=1;
1185 pi[pino].poc.ty0 = tcp->ty0_t;
1186 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1187 tcp->ty0_t = pi[pino].poc.ty1;
1188 incr_top=0;resetX=1;
1191 tcp->tx0_t = tcp->txS;
1192 pi[pino].poc.tx0 = tcp->tx0_t;
1193 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1194 tcp->tx0_t = pi[pino].poc.tx1;
1197 pi[pino].poc.tx0 = tcp->tx0_t;
1198 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1199 tcp->tx0_t = pi[pino].poc.tx1;
1200 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1201 pi[pino].poc.ty1 = tcp->ty0_t ;
1205 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1206 pi[pino].poc.tx1 = tcp->tx0_t ;
1207 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1208 pi[pino].poc.ty1 = tcp->ty0_t ;
1225 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1226 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1227 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1228 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1229 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1231 * @param p_image the image being encoded.
1232 * @param p_cp the coding parameters.
1233 * @param tileno the tile index of the tile being encoded.
1234 * @param p_tx0 pointer that will hold the X0 parameter for the tile
1235 * @param p_tx1 pointer that will hold the X1 parameter for the tile
1236 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
1237 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
1238 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
1239 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1240 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1241 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1242 * @param p_resolutions pointer to an area corresponding to the one described above.
1244 void get_all_encoding_parameters(
1245 const opj_image_header_t *p_image,
1246 const opj_cp_v2_t *p_cp,
1252 OPJ_UINT32 * p_dx_min,
1253 OPJ_UINT32 * p_dy_min,
1254 OPJ_UINT32 * p_max_prec,
1255 OPJ_UINT32 * p_max_res,
1256 OPJ_UINT32 ** p_resolutions
1260 OPJ_UINT32 compno, resno;
1263 const opj_tcp_v2_t *tcp = 00;
1264 const opj_tccp_t * l_tccp = 00;
1265 const opj_image_comp_header_t * l_img_comp = 00;
1267 // to store l_dx, l_dy, w and h for each resolution and component.
1268 OPJ_UINT32 * lResolutionPtr;
1270 // position in x and y of tile
1273 // preconditions in debug
1275 assert(p_image != 00);
1276 assert(tileno < p_cp->tw * p_cp->th);
1279 tcp = &p_cp->tcps [tileno];
1280 l_tccp = tcp->tccps;
1281 l_img_comp = p_image->comps;
1283 // position in x and y of tile
1285 p = tileno % p_cp->tw;
1286 q = tileno / p_cp->tw;
1288 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1289 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1290 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1291 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1292 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1294 // max precision and resolution is 0 (can only grow)
1298 // take the largest value for dx_min and dy_min
1299 *p_dx_min = 0x7fffffff;
1300 *p_dy_min = 0x7fffffff;
1303 (compno = 0; compno < p_image->numcomps; ++compno)
1305 // aritmetic variables to calculate
1306 OPJ_UINT32 l_level_no;
1307 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1308 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1309 OPJ_UINT32 l_product;
1310 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1311 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1313 lResolutionPtr = p_resolutions[compno];
1315 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1316 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1317 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1318 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1320 (l_tccp->numresolutions > *p_max_res)
1322 *p_max_res = l_tccp->numresolutions;
1325 // use custom size for precincts
1326 l_level_no = l_tccp->numresolutions - 1;
1328 (resno = 0; resno < l_tccp->numresolutions; ++resno)
1330 OPJ_UINT32 l_dx, l_dy;
1331 // precinct width and height
1332 l_pdx = l_tccp->prcw[resno];
1333 l_pdy = l_tccp->prch[resno];
1334 *lResolutionPtr++ = l_pdx;
1335 *lResolutionPtr++ = l_pdy;
1336 l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1337 l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1338 // take the minimum size for l_dx for each comp and resolution
1339 *p_dx_min = int_min(*p_dx_min, l_dx);
1340 *p_dy_min = int_min(*p_dy_min, l_dy);
1341 // various calculations of extents
1343 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1344 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1345 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1346 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1347 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1348 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1349 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1350 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1351 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1352 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1353 *lResolutionPtr++ = l_pw;
1354 *lResolutionPtr++ = l_ph;
1355 l_product = l_pw * l_ph;
1358 (l_product > *p_max_prec)
1360 *p_max_prec = l_product;
1370 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1371 * No other data is set. The include section of the packet iterator is not allocated.
1373 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
1374 * @param p_cp the coding parameters.
1375 * @param p_tile_no the index of the tile from which creating the packet iterator.
1377 opj_pi_iterator_t * pi_create(
1378 const opj_image_header_t *image,
1379 const opj_cp_v2_t *cp,
1384 OPJ_UINT32 pino, compno;
1385 // number of poc in the p_pi
1386 OPJ_UINT32 l_poc_bound;
1388 // pointers to tile coding parameters and components.
1389 opj_pi_iterator_t *l_pi = 00;
1390 opj_tcp_v2_t *tcp = 00;
1391 const opj_tccp_t *tccp = 00;
1393 // current packet iterator being allocated
1394 opj_pi_iterator_t *l_current_pi = 00;
1396 // preconditions in debug
1398 assert(image != 00);
1399 assert(tileno < cp->tw * cp->th);
1402 tcp = &cp->tcps[tileno];
1403 l_poc_bound = tcp->numpocs+1;
1405 // memory allocations
1406 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1410 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1412 l_current_pi = l_pi;
1413 for (pino = 0; pino < l_poc_bound ; ++pino) {
1415 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1416 if (! l_current_pi->comps) {
1417 pi_destroy_v2(l_pi, l_poc_bound);
1421 l_current_pi->numcomps = image->numcomps;
1422 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1424 for (compno = 0; compno < image->numcomps; ++compno) {
1425 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1427 tccp = &tcp->tccps[compno];
1429 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1430 if (!comp->resolutions) {
1431 pi_destroy_v2(l_pi, l_poc_bound);
1435 comp->numresolutions = tccp->numresolutions;
1436 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1446 * Destroys a packet iterator array.
1448 * @param p_pi the packet iterator array to destroy.
1449 * @param p_nb_elements the number of elements in the array.
1452 opj_pi_iterator_t *p_pi,
1453 OPJ_UINT32 p_nb_elements)
1455 OPJ_UINT32 compno, pino;
1456 opj_pi_iterator_t *l_current_pi = p_pi;
1463 opj_free(p_pi->include);
1468 (pino = 0; pino < p_nb_elements; ++pino)
1471 (l_current_pi->comps)
1473 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1475 (compno = 0; compno < l_current_pi->numcomps; compno++)
1478 (l_current_component->resolutions)
1480 opj_free(l_current_component->resolutions);
1481 l_current_component->resolutions = 00;
1483 ++l_current_component;
1485 opj_free(l_current_pi->comps);
1486 l_current_pi->comps = 0;
1496 void pi_update_decode_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
1501 // encoding prameters to set
1504 opj_pi_iterator_t * l_current_pi = 00;
1505 opj_poc_t* l_current_poc = 0;
1507 // preconditions in debug
1509 assert(p_tcp != 00);
1512 l_bound = p_tcp->numpocs+1;
1513 l_current_pi = p_pi;
1514 l_current_poc = p_tcp->pocs;
1517 (pino = 0;pino<l_bound;++pino)
1519 l_current_pi->poc.prg = l_current_poc->prg;
1520 l_current_pi->first = 1;
1522 l_current_pi->poc.resno0 = l_current_poc->resno0;
1523 l_current_pi->poc.compno0 = l_current_poc->compno0;
1524 l_current_pi->poc.layno0 = 0;
1525 l_current_pi->poc.precno0 = 0;
1526 l_current_pi->poc.resno1 = l_current_poc->resno1;
1527 l_current_pi->poc.compno1 = l_current_poc->compno1;
1528 l_current_pi->poc.layno1 = l_current_poc->layno1;
1529 l_current_pi->poc.precno1 = p_max_precision;
1536 void pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,opj_tcp_v2_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)
1541 // encoding prameters to set
1544 opj_pi_iterator_t * l_current_pi = 00;
1545 // preconditions in debug
1546 assert(p_tcp != 00);
1550 l_bound = p_tcp->numpocs+1;
1551 l_current_pi = p_pi;
1554 (pino = 0;pino<l_bound;++pino)
1556 l_current_pi->poc.prg = p_tcp->prg;
1557 l_current_pi->first = 1;
1558 l_current_pi->poc.resno0 = 0;
1559 l_current_pi->poc.compno0 = 0;
1560 l_current_pi->poc.layno0 = 0;
1561 l_current_pi->poc.precno0 = 0;
1562 l_current_pi->poc.resno1 = p_max_res;
1563 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1564 l_current_pi->poc.layno1 = p_tcp->numlayers;
1565 l_current_pi->poc.precno1 = p_max_precision;