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_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( const opj_image_t *image,
119 const opj_cp_v2_t *cp,
122 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);
123 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);
131 ==========================================================
133 ==========================================================
136 static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
137 opj_pi_comp_t *comp = NULL;
138 opj_pi_resolution_t *res = NULL;
142 comp = &pi->comps[pi->compno];
143 res = &comp->resolutions[pi->resno];
149 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
150 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
152 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
153 comp = &pi->comps[pi->compno];
154 if (pi->resno >= comp->numresolutions) {
157 res = &comp->resolutions[pi->resno];
159 pi->poc.precno1 = res->pw * res->ph;
161 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
162 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
163 if (!pi->include[index]) {
164 pi->include[index] = 1;
176 static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
177 opj_pi_comp_t *comp = NULL;
178 opj_pi_resolution_t *res = NULL;
182 comp = &pi->comps[pi->compno];
183 res = &comp->resolutions[pi->resno];
189 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
190 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
191 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
192 comp = &pi->comps[pi->compno];
193 if (pi->resno >= comp->numresolutions) {
196 res = &comp->resolutions[pi->resno];
198 pi->poc.precno1 = res->pw * res->ph;
200 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
201 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
202 if (!pi->include[index]) {
203 pi->include[index] = 1;
215 static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
216 opj_pi_comp_t *comp = NULL;
217 opj_pi_resolution_t *res = NULL;
227 for (compno = 0; compno < pi->numcomps; compno++) {
228 comp = &pi->comps[compno];
229 for (resno = 0; resno < comp->numresolutions; resno++) {
231 res = &comp->resolutions[resno];
232 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
233 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
234 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
235 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
240 pi->poc.ty0 = pi->ty0;
241 pi->poc.tx0 = pi->tx0;
242 pi->poc.ty1 = pi->ty1;
243 pi->poc.tx1 = pi->tx1;
245 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
246 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
247 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
248 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
254 comp = &pi->comps[pi->compno];
255 if (pi->resno >= comp->numresolutions) {
258 res = &comp->resolutions[pi->resno];
259 levelno = comp->numresolutions - 1 - pi->resno;
260 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
261 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
262 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
263 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
264 rpx = res->pdx + levelno;
265 rpy = res->pdy + levelno;
266 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
269 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
273 if ((res->pw==0)||(res->ph==0)) continue;
275 if ((trx0==trx1)||(try0==try1)) continue;
277 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
278 - int_floordivpow2(trx0, res->pdx);
279 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
280 - int_floordivpow2(try0, res->pdy);
281 pi->precno = prci + prcj * res->pw;
282 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
283 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
284 if (!pi->include[index]) {
285 pi->include[index] = 1;
298 static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
299 opj_pi_comp_t *comp = NULL;
300 opj_pi_resolution_t *res = NULL;
304 comp = &pi->comps[pi->compno];
311 for (compno = 0; compno < pi->numcomps; compno++) {
312 comp = &pi->comps[compno];
313 for (resno = 0; resno < comp->numresolutions; resno++) {
315 res = &comp->resolutions[resno];
316 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
317 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
318 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
319 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
324 pi->poc.ty0 = pi->ty0;
325 pi->poc.tx0 = pi->tx0;
326 pi->poc.ty1 = pi->ty1;
327 pi->poc.tx1 = pi->tx1;
329 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
330 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
331 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
332 comp = &pi->comps[pi->compno];
333 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
339 res = &comp->resolutions[pi->resno];
340 levelno = comp->numresolutions - 1 - pi->resno;
341 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
342 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
343 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
344 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
345 rpx = res->pdx + levelno;
346 rpy = res->pdy + levelno;
347 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
350 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
354 if ((res->pw==0)||(res->ph==0)) continue;
356 if ((trx0==trx1)||(try0==try1)) continue;
358 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
359 - int_floordivpow2(trx0, res->pdx);
360 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
361 - int_floordivpow2(try0, res->pdy);
362 pi->precno = prci + prcj * res->pw;
363 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
364 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
365 if (!pi->include[index]) {
366 pi->include[index] = 1;
379 static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
380 opj_pi_comp_t *comp = NULL;
381 opj_pi_resolution_t *res = NULL;
385 comp = &pi->comps[pi->compno];
391 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
393 comp = &pi->comps[pi->compno];
396 for (resno = 0; resno < comp->numresolutions; resno++) {
398 res = &comp->resolutions[resno];
399 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
400 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
401 pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
402 pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
405 pi->poc.ty0 = pi->ty0;
406 pi->poc.tx0 = pi->tx0;
407 pi->poc.ty1 = pi->ty1;
408 pi->poc.tx1 = pi->tx1;
410 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
411 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
412 for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
418 res = &comp->resolutions[pi->resno];
419 levelno = comp->numresolutions - 1 - pi->resno;
420 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
421 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
422 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
423 try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
424 rpx = res->pdx + levelno;
425 rpy = res->pdy + levelno;
426 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
429 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
433 if ((res->pw==0)||(res->ph==0)) continue;
435 if ((trx0==trx1)||(try0==try1)) continue;
437 prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
438 - int_floordivpow2(trx0, res->pdx);
439 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
440 - int_floordivpow2(try0, res->pdy);
441 pi->precno = prci + prcj * res->pw;
442 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
443 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
444 if (!pi->include[index]) {
445 pi->include[index] = 1;
459 ==========================================================
460 Packet iterator interface
461 ==========================================================
464 opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
466 int compno, resno, pino;
467 opj_pi_iterator_t *pi = NULL;
468 opj_tcp_t *tcp = NULL;
469 opj_tccp_t *tccp = NULL;
471 tcp = &cp->tcps[tileno];
473 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
475 /* TODO: throw an error */
479 for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
485 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
486 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
487 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
488 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
489 pi[pino].numcomps = image->numcomps;
491 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
492 if(!pi[pino].comps) {
493 /* TODO: throw an error */
494 pi_destroy(pi, cp, tileno);
498 for (compno = 0; compno < pi->numcomps; compno++) {
499 int tcx0, tcy0, tcx1, tcy1;
500 opj_pi_comp_t *comp = &pi[pino].comps[compno];
501 tccp = &tcp->tccps[compno];
502 comp->dx = image->comps[compno].dx;
503 comp->dy = image->comps[compno].dy;
504 comp->numresolutions = tccp->numresolutions;
506 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
507 if(!comp->resolutions) {
508 /* TODO: throw an error */
509 pi_destroy(pi, cp, tileno);
513 tcx0 = int_ceildiv(pi->tx0, comp->dx);
514 tcy0 = int_ceildiv(pi->ty0, comp->dy);
515 tcx1 = int_ceildiv(pi->tx1, comp->dx);
516 tcy1 = int_ceildiv(pi->ty1, comp->dy);
517 if (comp->numresolutions > maxres) {
518 maxres = comp->numresolutions;
521 for (resno = 0; resno < comp->numresolutions; resno++) {
523 int rx0, ry0, rx1, ry1;
524 int px0, py0, px1, py1;
525 opj_pi_resolution_t *res = &comp->resolutions[resno];
526 if (tccp->csty & J2K_CCP_CSTY_PRT) {
527 res->pdx = tccp->prcw[resno];
528 res->pdy = tccp->prch[resno];
533 levelno = comp->numresolutions - 1 - resno;
534 rx0 = int_ceildivpow2(tcx0, levelno);
535 ry0 = int_ceildivpow2(tcy0, levelno);
536 rx1 = int_ceildivpow2(tcx1, levelno);
537 ry1 = int_ceildivpow2(tcy1, levelno);
538 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
539 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
540 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
541 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
542 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
543 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
545 if (res->pw*res->ph > maxprec) {
546 maxprec = res->pw*res->ph;
552 tccp = &tcp->tccps[0];
554 pi[pino].step_c = maxprec * pi[pino].step_p;
555 pi[pino].step_r = image->numcomps * pi[pino].step_c;
556 pi[pino].step_l = maxres * pi[pino].step_r;
559 pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
560 if(!pi[pino].include) {
561 /* TODO: throw an error */
562 pi_destroy(pi, cp, tileno);
567 pi[pino].include = pi[pino - 1].include;
572 pi[pino].poc.resno0 = 0;
573 pi[pino].poc.compno0 = 0;
574 pi[pino].poc.layno1 = tcp->numlayers;
575 pi[pino].poc.resno1 = maxres;
576 pi[pino].poc.compno1 = image->numcomps;
577 pi[pino].poc.prg = tcp->prg;
580 pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
581 pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
582 pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
583 pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
584 pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
585 pi[pino].poc.prg = tcp->pocs[pino].prg;
587 pi[pino].poc.layno0 = 0;
588 pi[pino].poc.precno0 = 0;
589 pi[pino].poc.precno1 = maxprec;
597 opj_pi_iterator_t *pi_create_decode_v2( opj_image_t *p_image,
604 OPJ_UINT32 compno, resno;
606 // to store w, h, dx and dy fro all components and resolutions
607 OPJ_UINT32 * l_tmp_data;
608 OPJ_UINT32 ** l_tmp_ptr;
610 // encoding prameters to set
611 OPJ_UINT32 l_max_res;
612 OPJ_UINT32 l_max_prec;
613 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
614 OPJ_UINT32 l_dx_min,l_dy_min;
616 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
617 OPJ_UINT32 l_data_stride;
620 opj_pi_iterator_t *l_pi = 00;
621 opj_tcp_v2_t *l_tcp = 00;
622 const opj_tccp_t *l_tccp = 00;
623 opj_pi_comp_t *l_current_comp = 00;
624 opj_image_comp_t * l_img_comp = 00;
625 opj_pi_iterator_t * l_current_pi = 00;
626 OPJ_UINT32 * l_encoding_value_ptr = 00;
628 // preconditions in debug
630 assert(p_image != 00);
631 assert(p_tile_no < p_cp->tw * p_cp->th);
634 l_tcp = &p_cp->tcps[p_tile_no];
635 l_bound = l_tcp->numpocs+1;
637 l_data_stride = 4 * J2K_MAXRLVLS;
638 l_tmp_data = (OPJ_UINT32*)opj_malloc(
639 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
645 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
646 p_image->numcomps * sizeof(OPJ_UINT32 *));
650 opj_free(l_tmp_data);
654 // memory allocation for pi
655 l_pi = pi_create(p_image, p_cp, p_tile_no);
657 opj_free(l_tmp_data);
662 l_encoding_value_ptr = l_tmp_data;
663 // update pointer array
665 (compno = 0; compno < p_image->numcomps; ++compno)
667 l_tmp_ptr[compno] = l_encoding_value_ptr;
668 l_encoding_value_ptr += l_data_stride;
670 // get encoding parameters
671 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);
675 l_step_c = l_max_prec * l_step_p;
676 l_step_r = p_image->numcomps * l_step_c;
677 l_step_l = l_max_res * l_step_r;
679 // set values for first packet iterator
682 // memory allocation for include
683 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
685 (!l_current_pi->include)
687 opj_free(l_tmp_data);
689 pi_destroy_v2(l_pi, l_bound);
692 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
694 // special treatment for the first packet iterator
695 l_current_comp = l_current_pi->comps;
696 l_img_comp = p_image->comps;
697 l_tccp = l_tcp->tccps;
699 l_current_pi->tx0 = l_tx0;
700 l_current_pi->ty0 = l_ty0;
701 l_current_pi->tx1 = l_tx1;
702 l_current_pi->ty1 = l_ty1;
704 //l_current_pi->dx = l_img_comp->dx;
705 //l_current_pi->dy = l_img_comp->dy;
707 l_current_pi->step_p = l_step_p;
708 l_current_pi->step_c = l_step_c;
709 l_current_pi->step_r = l_step_r;
710 l_current_pi->step_l = l_step_l;
712 /* allocation for components and number of components has already been calculated by pi_create */
714 (compno = 0; compno < l_current_pi->numcomps; ++compno)
716 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
717 l_encoding_value_ptr = l_tmp_ptr[compno];
719 l_current_comp->dx = l_img_comp->dx;
720 l_current_comp->dy = l_img_comp->dy;
721 /* resolutions have already been initialized */
723 (resno = 0; resno < l_current_comp->numresolutions; resno++)
725 l_res->pdx = *(l_encoding_value_ptr++);
726 l_res->pdy = *(l_encoding_value_ptr++);
727 l_res->pw = *(l_encoding_value_ptr++);
728 l_res->ph = *(l_encoding_value_ptr++);
738 (pino = 1 ; pino<l_bound ; ++pino )
740 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
741 opj_image_comp_t * l_img_comp = p_image->comps;
742 l_tccp = l_tcp->tccps;
744 l_current_pi->tx0 = l_tx0;
745 l_current_pi->ty0 = l_ty0;
746 l_current_pi->tx1 = l_tx1;
747 l_current_pi->ty1 = l_ty1;
748 //l_current_pi->dx = l_dx_min;
749 //l_current_pi->dy = l_dy_min;
750 l_current_pi->step_p = l_step_p;
751 l_current_pi->step_c = l_step_c;
752 l_current_pi->step_r = l_step_r;
753 l_current_pi->step_l = l_step_l;
755 /* allocation for components and number of components has already been calculated by pi_create */
757 (compno = 0; compno < l_current_pi->numcomps; ++compno)
759 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
760 l_encoding_value_ptr = l_tmp_ptr[compno];
762 l_current_comp->dx = l_img_comp->dx;
763 l_current_comp->dy = l_img_comp->dy;
764 /* resolutions have already been initialized */
766 (resno = 0; resno < l_current_comp->numresolutions; resno++)
768 l_res->pdx = *(l_encoding_value_ptr++);
769 l_res->pdy = *(l_encoding_value_ptr++);
770 l_res->pw = *(l_encoding_value_ptr++);
771 l_res->ph = *(l_encoding_value_ptr++);
779 l_current_pi->include = (l_current_pi-1)->include;
782 opj_free(l_tmp_data);
789 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
793 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
798 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){
803 opj_pi_iterator_t *pi = NULL;
804 opj_tcp_t *tcp = NULL;
805 opj_tccp_t *tccp = NULL;
807 tcp = &cp->tcps[tileno];
809 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
810 if(!pi) { return NULL;}
811 pi->tp_on = cp->tp_on;
813 for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
817 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
818 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
819 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
820 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
821 pi[pino].numcomps = image->numcomps;
823 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
824 if(!pi[pino].comps) {
825 pi_destroy(pi, cp, tileno);
829 for (compno = 0; compno < pi[pino].numcomps; compno++) {
830 int tcx0, tcy0, tcx1, tcy1;
831 opj_pi_comp_t *comp = &pi[pino].comps[compno];
832 tccp = &tcp->tccps[compno];
833 comp->dx = image->comps[compno].dx;
834 comp->dy = image->comps[compno].dy;
835 comp->numresolutions = tccp->numresolutions;
837 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
838 if(!comp->resolutions) {
839 pi_destroy(pi, cp, tileno);
843 tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
844 tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
845 tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
846 tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
847 if (comp->numresolutions > maxres) {
848 maxres = comp->numresolutions;
851 for (resno = 0; resno < comp->numresolutions; resno++) {
853 int rx0, ry0, rx1, ry1;
854 int px0, py0, px1, py1;
855 opj_pi_resolution_t *res = &comp->resolutions[resno];
856 if (tccp->csty & J2K_CCP_CSTY_PRT) {
857 res->pdx = tccp->prcw[resno];
858 res->pdy = tccp->prch[resno];
863 levelno = comp->numresolutions - 1 - resno;
864 rx0 = int_ceildivpow2(tcx0, levelno);
865 ry0 = int_ceildivpow2(tcy0, levelno);
866 rx1 = int_ceildivpow2(tcx1, levelno);
867 ry1 = int_ceildivpow2(tcy1, levelno);
868 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
869 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
870 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
871 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
872 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
873 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
875 if (res->pw*res->ph > maxprec) {
876 maxprec = res->pw * res->ph;
881 tccp = &tcp->tccps[0];
883 pi[pino].step_c = maxprec * pi[pino].step_p;
884 pi[pino].step_r = image->numcomps * pi[pino].step_c;
885 pi[pino].step_l = maxres * pi[pino].step_r;
887 for (compno = 0; compno < pi->numcomps; compno++) {
888 opj_pi_comp_t *comp = &pi->comps[compno];
889 for (resno = 0; resno < comp->numresolutions; resno++) {
891 opj_pi_resolution_t *res = &comp->resolutions[resno];
892 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
893 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
894 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
895 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
900 pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
901 if(!pi[pino].include) {
902 pi_destroy(pi, cp, tileno);
907 pi[pino].include = pi[pino - 1].include;
910 /* Generation of boundaries for each prog flag*/
911 if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
912 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
913 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
914 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
915 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
916 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
917 tcp->pocs[pino].prg = tcp->pocs[pino].prg1;
919 tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
921 tcp->pocs[pino].compS= 0;
922 tcp->pocs[pino].compE= image->numcomps;
923 tcp->pocs[pino].resS = 0;
924 tcp->pocs[pino].resE = maxres;
925 tcp->pocs[pino].layS = 0;
926 tcp->pocs[pino].layE = tcp->numlayers;
927 tcp->pocs[pino].prg = tcp->prg;
929 tcp->pocs[pino].prcS = 0;
930 tcp->pocs[pino].prcE = maxprec;;
931 tcp->pocs[pino].txS = pi[pino].tx0;
932 tcp->pocs[pino].txE = pi[pino].tx1;
933 tcp->pocs[pino].tyS = pi[pino].ty0;
934 tcp->pocs[pino].tyE = pi[pino].ty1;
935 tcp->pocs[pino].dx = pi[pino].dx;
936 tcp->pocs[pino].dy = pi[pino].dy;
943 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
945 opj_tcp_t *tcp = &cp->tcps[tileno];
947 for (pino = 0; pino < tcp->numpocs + 1; pino++) {
949 for (compno = 0; compno < pi->numcomps; compno++) {
950 opj_pi_comp_t *comp = &pi[pino].comps[compno];
951 if(comp->resolutions) {
952 opj_free(comp->resolutions);
955 opj_free(pi[pino].comps);
959 opj_free(pi->include);
965 opj_bool pi_next(opj_pi_iterator_t * pi) {
966 switch (pi->poc.prg) {
968 return pi_next_lrcp(pi);
970 return pi_next_rlcp(pi);
972 return pi_next_rpcl(pi);
974 return pi_next_pcrl(pi);
976 return pi_next_cprl(pi);
984 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){
987 int incr_top=1,resetX=0;
988 opj_tcp_t *tcps =&cp->tcps[tileno];
989 opj_poc_t *tcp= &tcps->pocs[pino];
992 pi[pino].poc.prg = tcp->prg;
995 case CPRL: strncpy(prog, "CPRL",4);
997 case LRCP: strncpy(prog, "LRCP",4);
999 case PCRL: strncpy(prog, "PCRL",4);
1001 case RLCP: strncpy(prog, "RLCP",4);
1003 case RPCL: strncpy(prog, "RPCL",4);
1009 if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1010 pi[pino].poc.resno0 = tcp->resS;
1011 pi[pino].poc.resno1 = tcp->resE;
1012 pi[pino].poc.compno0 = tcp->compS;
1013 pi[pino].poc.compno1 = tcp->compE;
1014 pi[pino].poc.layno0 = tcp->layS;
1015 pi[pino].poc.layno1 = tcp->layE;
1016 pi[pino].poc.precno0 = tcp->prcS;
1017 pi[pino].poc.precno1 = tcp->prcE;
1018 pi[pino].poc.tx0 = tcp->txS;
1019 pi[pino].poc.ty0 = tcp->tyS;
1020 pi[pino].poc.tx1 = tcp->txE;
1021 pi[pino].poc.ty1 = tcp->tyE;
1023 if( tpnum < cur_totnum_tp){
1028 pi[pino].poc.compno0 = tcp->compS;
1029 pi[pino].poc.compno1 = tcp->compE;
1032 tcp->comp_t = tcp->compS;
1033 pi[pino].poc.compno0 = tcp->comp_t;
1034 pi[pino].poc.compno1 = tcp->comp_t+1;
1038 if(tcp->comp_t ==tcp->compE){
1039 tcp->comp_t = tcp->compS;
1040 pi[pino].poc.compno0 = tcp->comp_t;
1041 pi[pino].poc.compno1 = tcp->comp_t+1;
1045 pi[pino].poc.compno0 = tcp->comp_t;
1046 pi[pino].poc.compno1 = tcp->comp_t+1;
1051 pi[pino].poc.compno0 = tcp->comp_t-1;
1052 pi[pino].poc.compno1 = tcp->comp_t;
1060 pi[pino].poc.resno0 = tcp->resS;
1061 pi[pino].poc.resno1 = tcp->resE;
1064 tcp->res_t = tcp->resS;
1065 pi[pino].poc.resno0 = tcp->res_t;
1066 pi[pino].poc.resno1 = tcp->res_t+1;
1070 if(tcp->res_t==tcp->resE){
1071 tcp->res_t = tcp->resS;
1072 pi[pino].poc.resno0 = tcp->res_t;
1073 pi[pino].poc.resno1 = tcp->res_t+1;
1077 pi[pino].poc.resno0 = tcp->res_t;
1078 pi[pino].poc.resno1 = tcp->res_t+1;
1083 pi[pino].poc.resno0 = tcp->res_t - 1;
1084 pi[pino].poc.resno1 = tcp->res_t;
1092 pi[pino].poc.layno0 = tcp->layS;
1093 pi[pino].poc.layno1 = tcp->layE;
1096 tcp->lay_t = tcp->layS;
1097 pi[pino].poc.layno0 = tcp->lay_t;
1098 pi[pino].poc.layno1 = tcp->lay_t+1;
1102 if(tcp->lay_t == tcp->layE){
1103 tcp->lay_t = tcp->layS;
1104 pi[pino].poc.layno0 = tcp->lay_t;
1105 pi[pino].poc.layno1 = tcp->lay_t+1;
1109 pi[pino].poc.layno0 = tcp->lay_t;
1110 pi[pino].poc.layno1 = tcp->lay_t+1;
1115 pi[pino].poc.layno0 = tcp->lay_t - 1;
1116 pi[pino].poc.layno1 = tcp->lay_t;
1127 pi[pino].poc.precno0 = tcp->prcS;
1128 pi[pino].poc.precno1 = tcp->prcE;
1131 tcp->prc_t = tcp->prcS;
1132 pi[pino].poc.precno0 = tcp->prc_t;
1133 pi[pino].poc.precno1 = tcp->prc_t+1;
1137 if(tcp->prc_t == tcp->prcE){
1138 tcp->prc_t = tcp->prcS;
1139 pi[pino].poc.precno0 = tcp->prc_t;
1140 pi[pino].poc.precno1 = tcp->prc_t+1;
1144 pi[pino].poc.precno0 = tcp->prc_t;
1145 pi[pino].poc.precno1 = tcp->prc_t+1;
1150 pi[pino].poc.precno0 = tcp->prc_t - 1;
1151 pi[pino].poc.precno1 = tcp->prc_t;
1158 pi[pino].poc.tx0 = tcp->txS;
1159 pi[pino].poc.ty0 = tcp->tyS;
1160 pi[pino].poc.tx1 = tcp->txE;
1161 pi[pino].poc.ty1 = tcp->tyE;
1164 tcp->tx0_t = tcp->txS;
1165 tcp->ty0_t = tcp->tyS;
1166 pi[pino].poc.tx0 = tcp->tx0_t;
1167 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1168 pi[pino].poc.ty0 = tcp->ty0_t;
1169 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1170 tcp->tx0_t = pi[pino].poc.tx1;
1171 tcp->ty0_t = pi[pino].poc.ty1;
1174 if(tcp->tx0_t >= tcp->txE){
1175 if(tcp->ty0_t >= tcp->tyE){
1176 tcp->ty0_t = tcp->tyS;
1177 pi[pino].poc.ty0 = tcp->ty0_t;
1178 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1179 tcp->ty0_t = pi[pino].poc.ty1;
1180 incr_top=1;resetX=1;
1182 pi[pino].poc.ty0 = tcp->ty0_t;
1183 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1184 tcp->ty0_t = pi[pino].poc.ty1;
1185 incr_top=0;resetX=1;
1188 tcp->tx0_t = tcp->txS;
1189 pi[pino].poc.tx0 = tcp->tx0_t;
1190 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1191 tcp->tx0_t = pi[pino].poc.tx1;
1194 pi[pino].poc.tx0 = tcp->tx0_t;
1195 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1196 tcp->tx0_t = pi[pino].poc.tx1;
1197 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1198 pi[pino].poc.ty1 = tcp->ty0_t ;
1202 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1203 pi[pino].poc.tx1 = tcp->tx0_t ;
1204 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1205 pi[pino].poc.ty1 = tcp->ty0_t ;
1222 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1223 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1224 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1225 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1226 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1228 * @param p_image the image being encoded.
1229 * @param p_cp the coding parameters.
1230 * @param tileno the tile index of the tile being encoded.
1231 * @param p_tx0 pointer that will hold the X0 parameter for the tile
1232 * @param p_tx1 pointer that will hold the X1 parameter for the tile
1233 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
1234 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
1235 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
1236 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1237 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1238 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1239 * @param p_resolutions pointer to an area corresponding to the one described above.
1241 void get_all_encoding_parameters(
1242 const opj_image_t *p_image,
1243 const opj_cp_v2_t *p_cp,
1249 OPJ_UINT32 * p_dx_min,
1250 OPJ_UINT32 * p_dy_min,
1251 OPJ_UINT32 * p_max_prec,
1252 OPJ_UINT32 * p_max_res,
1253 OPJ_UINT32 ** p_resolutions
1257 OPJ_UINT32 compno, resno;
1260 const opj_tcp_v2_t *tcp = 00;
1261 const opj_tccp_t * l_tccp = 00;
1262 const opj_image_comp_t * l_img_comp = 00;
1264 // to store l_dx, l_dy, w and h for each resolution and component.
1265 OPJ_UINT32 * lResolutionPtr;
1267 // position in x and y of tile
1270 // preconditions in debug
1272 assert(p_image != 00);
1273 assert(tileno < p_cp->tw * p_cp->th);
1276 tcp = &p_cp->tcps [tileno];
1277 l_tccp = tcp->tccps;
1278 l_img_comp = p_image->comps;
1280 // position in x and y of tile
1282 p = tileno % p_cp->tw;
1283 q = tileno / p_cp->tw;
1285 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1286 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1287 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1288 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1289 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1291 // max precision and resolution is 0 (can only grow)
1295 // take the largest value for dx_min and dy_min
1296 *p_dx_min = 0x7fffffff;
1297 *p_dy_min = 0x7fffffff;
1300 (compno = 0; compno < p_image->numcomps; ++compno)
1302 // aritmetic variables to calculate
1303 OPJ_UINT32 l_level_no;
1304 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1305 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1306 OPJ_UINT32 l_product;
1307 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1308 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1310 lResolutionPtr = p_resolutions[compno];
1312 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1313 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1314 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1315 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1317 (l_tccp->numresolutions > *p_max_res)
1319 *p_max_res = l_tccp->numresolutions;
1322 // use custom size for precincts
1323 l_level_no = l_tccp->numresolutions - 1;
1325 (resno = 0; resno < l_tccp->numresolutions; ++resno)
1327 OPJ_UINT32 l_dx, l_dy;
1328 // precinct width and height
1329 l_pdx = l_tccp->prcw[resno];
1330 l_pdy = l_tccp->prch[resno];
1331 *lResolutionPtr++ = l_pdx;
1332 *lResolutionPtr++ = l_pdy;
1333 l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1334 l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1335 // take the minimum size for l_dx for each comp and resolution
1336 *p_dx_min = int_min(*p_dx_min, l_dx);
1337 *p_dy_min = int_min(*p_dy_min, l_dy);
1338 // various calculations of extents
1340 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1341 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1342 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1343 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1344 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1345 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1346 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1347 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1348 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1349 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1350 *lResolutionPtr++ = l_pw;
1351 *lResolutionPtr++ = l_ph;
1352 l_product = l_pw * l_ph;
1355 (l_product > *p_max_prec)
1357 *p_max_prec = l_product;
1367 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1368 * No other data is set. The include section of the packet iterator is not allocated.
1370 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
1371 * @param p_cp the coding parameters.
1372 * @param p_tile_no the index of the tile from which creating the packet iterator.
1374 opj_pi_iterator_t * pi_create( const opj_image_t *image,
1375 const opj_cp_v2_t *cp,
1379 OPJ_UINT32 pino, compno;
1380 // number of poc in the p_pi
1381 OPJ_UINT32 l_poc_bound;
1383 // pointers to tile coding parameters and components.
1384 opj_pi_iterator_t *l_pi = 00;
1385 opj_tcp_v2_t *tcp = 00;
1386 const opj_tccp_t *tccp = 00;
1388 // current packet iterator being allocated
1389 opj_pi_iterator_t *l_current_pi = 00;
1391 // preconditions in debug
1393 assert(image != 00);
1394 assert(tileno < cp->tw * cp->th);
1397 tcp = &cp->tcps[tileno];
1398 l_poc_bound = tcp->numpocs+1;
1400 // memory allocations
1401 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1405 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1407 l_current_pi = l_pi;
1408 for (pino = 0; pino < l_poc_bound ; ++pino) {
1410 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1411 if (! l_current_pi->comps) {
1412 pi_destroy_v2(l_pi, l_poc_bound);
1416 l_current_pi->numcomps = image->numcomps;
1417 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1419 for (compno = 0; compno < image->numcomps; ++compno) {
1420 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1422 tccp = &tcp->tccps[compno];
1424 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1425 if (!comp->resolutions) {
1426 pi_destroy_v2(l_pi, l_poc_bound);
1430 comp->numresolutions = tccp->numresolutions;
1431 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1441 * Destroys a packet iterator array.
1443 * @param p_pi the packet iterator array to destroy.
1444 * @param p_nb_elements the number of elements in the array.
1447 opj_pi_iterator_t *p_pi,
1448 OPJ_UINT32 p_nb_elements)
1450 OPJ_UINT32 compno, pino;
1451 opj_pi_iterator_t *l_current_pi = p_pi;
1458 opj_free(p_pi->include);
1463 (pino = 0; pino < p_nb_elements; ++pino)
1466 (l_current_pi->comps)
1468 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1470 (compno = 0; compno < l_current_pi->numcomps; compno++)
1473 (l_current_component->resolutions)
1475 opj_free(l_current_component->resolutions);
1476 l_current_component->resolutions = 00;
1478 ++l_current_component;
1480 opj_free(l_current_pi->comps);
1481 l_current_pi->comps = 0;
1491 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)
1496 // encoding prameters to set
1499 opj_pi_iterator_t * l_current_pi = 00;
1500 opj_poc_t* l_current_poc = 0;
1502 // preconditions in debug
1504 assert(p_tcp != 00);
1507 l_bound = p_tcp->numpocs+1;
1508 l_current_pi = p_pi;
1509 l_current_poc = p_tcp->pocs;
1512 (pino = 0;pino<l_bound;++pino)
1514 l_current_pi->poc.prg = l_current_poc->prg;
1515 l_current_pi->first = 1;
1517 l_current_pi->poc.resno0 = l_current_poc->resno0;
1518 l_current_pi->poc.compno0 = l_current_poc->compno0;
1519 l_current_pi->poc.layno0 = 0;
1520 l_current_pi->poc.precno0 = 0;
1521 l_current_pi->poc.resno1 = l_current_poc->resno1;
1522 l_current_pi->poc.compno1 = l_current_poc->compno1;
1523 l_current_pi->poc.layno1 = l_current_poc->layno1;
1524 l_current_pi->poc.precno1 = p_max_precision;
1531 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)
1536 // encoding prameters to set
1539 opj_pi_iterator_t * l_current_pi = 00;
1540 // preconditions in debug
1541 assert(p_tcp != 00);
1545 l_bound = p_tcp->numpocs+1;
1546 l_current_pi = p_pi;
1549 (pino = 0;pino<l_bound;++pino)
1551 l_current_pi->poc.prg = p_tcp->prg;
1552 l_current_pi->first = 1;
1553 l_current_pi->poc.resno0 = 0;
1554 l_current_pi->poc.compno0 = 0;
1555 l_current_pi->poc.layno0 = 0;
1556 l_current_pi->poc.precno0 = 0;
1557 l_current_pi->poc.resno1 = p_max_res;
1558 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1559 l_current_pi->poc.layno1 = p_tcp->numlayers;
1560 l_current_pi->poc.precno1 = p_max_precision;