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(
119 const opj_image_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_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_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);
662 opj_free(l_tmp_data);
667 l_encoding_value_ptr = l_tmp_data;
668 // update pointer array
670 (compno = 0; compno < p_image->numcomps; ++compno)
672 l_tmp_ptr[compno] = l_encoding_value_ptr;
673 l_encoding_value_ptr += l_data_stride;
675 // get encoding parameters
676 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);
680 l_step_c = l_max_prec * l_step_p;
681 l_step_r = p_image->numcomps * l_step_c;
682 l_step_l = l_max_res * l_step_r;
684 // set values for first packet iterator
687 // memory allocation for include
688 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
690 (!l_current_pi->include)
692 opj_free(l_tmp_data);
694 pi_destroy_v2(l_pi, l_bound);
697 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
699 // special treatment for the first packet iterator
700 l_current_comp = l_current_pi->comps;
701 l_img_comp = p_image->comps;
702 l_tccp = l_tcp->tccps;
704 l_current_pi->tx0 = l_tx0;
705 l_current_pi->ty0 = l_ty0;
706 l_current_pi->tx1 = l_tx1;
707 l_current_pi->ty1 = l_ty1;
709 //l_current_pi->dx = l_img_comp->dx;
710 //l_current_pi->dy = l_img_comp->dy;
712 l_current_pi->step_p = l_step_p;
713 l_current_pi->step_c = l_step_c;
714 l_current_pi->step_r = l_step_r;
715 l_current_pi->step_l = l_step_l;
717 /* allocation for components and number of components has already been calculated by pi_create */
719 (compno = 0; compno < l_current_pi->numcomps; ++compno)
721 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
722 l_encoding_value_ptr = l_tmp_ptr[compno];
724 l_current_comp->dx = l_img_comp->dx;
725 l_current_comp->dy = l_img_comp->dy;
726 /* resolutions have already been initialized */
728 (resno = 0; resno < l_current_comp->numresolutions; resno++)
730 l_res->pdx = *(l_encoding_value_ptr++);
731 l_res->pdy = *(l_encoding_value_ptr++);
732 l_res->pw = *(l_encoding_value_ptr++);
733 l_res->ph = *(l_encoding_value_ptr++);
743 (pino = 1 ; pino<l_bound ; ++pino )
745 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
746 opj_image_comp_t * l_img_comp = p_image->comps;
747 l_tccp = l_tcp->tccps;
749 l_current_pi->tx0 = l_tx0;
750 l_current_pi->ty0 = l_ty0;
751 l_current_pi->tx1 = l_tx1;
752 l_current_pi->ty1 = l_ty1;
753 //l_current_pi->dx = l_dx_min;
754 //l_current_pi->dy = l_dy_min;
755 l_current_pi->step_p = l_step_p;
756 l_current_pi->step_c = l_step_c;
757 l_current_pi->step_r = l_step_r;
758 l_current_pi->step_l = l_step_l;
760 /* allocation for components and number of components has already been calculated by pi_create */
762 (compno = 0; compno < l_current_pi->numcomps; ++compno)
764 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
765 l_encoding_value_ptr = l_tmp_ptr[compno];
767 l_current_comp->dx = l_img_comp->dx;
768 l_current_comp->dy = l_img_comp->dy;
769 /* resolutions have already been initialized */
771 (resno = 0; resno < l_current_comp->numresolutions; resno++)
773 l_res->pdx = *(l_encoding_value_ptr++);
774 l_res->pdy = *(l_encoding_value_ptr++);
775 l_res->pw = *(l_encoding_value_ptr++);
776 l_res->ph = *(l_encoding_value_ptr++);
784 l_current_pi->include = (l_current_pi-1)->include;
787 opj_free(l_tmp_data);
794 pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
798 pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
803 opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){
808 opj_pi_iterator_t *pi = NULL;
809 opj_tcp_t *tcp = NULL;
810 opj_tccp_t *tccp = NULL;
812 tcp = &cp->tcps[tileno];
814 pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
815 if(!pi) { return NULL;}
816 pi->tp_on = cp->tp_on;
818 for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
822 pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
823 pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
824 pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
825 pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
826 pi[pino].numcomps = image->numcomps;
828 pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
829 if(!pi[pino].comps) {
830 pi_destroy(pi, cp, tileno);
834 for (compno = 0; compno < pi[pino].numcomps; compno++) {
835 int tcx0, tcy0, tcx1, tcy1;
836 opj_pi_comp_t *comp = &pi[pino].comps[compno];
837 tccp = &tcp->tccps[compno];
838 comp->dx = image->comps[compno].dx;
839 comp->dy = image->comps[compno].dy;
840 comp->numresolutions = tccp->numresolutions;
842 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
843 if(!comp->resolutions) {
844 pi_destroy(pi, cp, tileno);
848 tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
849 tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
850 tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
851 tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
852 if (comp->numresolutions > maxres) {
853 maxres = comp->numresolutions;
856 for (resno = 0; resno < comp->numresolutions; resno++) {
858 int rx0, ry0, rx1, ry1;
859 int px0, py0, px1, py1;
860 opj_pi_resolution_t *res = &comp->resolutions[resno];
861 if (tccp->csty & J2K_CCP_CSTY_PRT) {
862 res->pdx = tccp->prcw[resno];
863 res->pdy = tccp->prch[resno];
868 levelno = comp->numresolutions - 1 - resno;
869 rx0 = int_ceildivpow2(tcx0, levelno);
870 ry0 = int_ceildivpow2(tcy0, levelno);
871 rx1 = int_ceildivpow2(tcx1, levelno);
872 ry1 = int_ceildivpow2(tcy1, levelno);
873 px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
874 py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
875 px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
876 py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
877 res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
878 res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
880 if (res->pw*res->ph > maxprec) {
881 maxprec = res->pw * res->ph;
886 tccp = &tcp->tccps[0];
888 pi[pino].step_c = maxprec * pi[pino].step_p;
889 pi[pino].step_r = image->numcomps * pi[pino].step_c;
890 pi[pino].step_l = maxres * pi[pino].step_r;
892 for (compno = 0; compno < pi->numcomps; compno++) {
893 opj_pi_comp_t *comp = &pi->comps[compno];
894 for (resno = 0; resno < comp->numresolutions; resno++) {
896 opj_pi_resolution_t *res = &comp->resolutions[resno];
897 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
898 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
899 pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
900 pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
905 pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
906 if(!pi[pino].include) {
907 pi_destroy(pi, cp, tileno);
912 pi[pino].include = pi[pino - 1].include;
915 /* Generation of boundaries for each prog flag*/
916 if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
917 tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
918 tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
919 tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
920 tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
921 tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
922 tcp->pocs[pino].prg = tcp->pocs[pino].prg1;
924 tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
926 tcp->pocs[pino].compS= 0;
927 tcp->pocs[pino].compE= image->numcomps;
928 tcp->pocs[pino].resS = 0;
929 tcp->pocs[pino].resE = maxres;
930 tcp->pocs[pino].layS = 0;
931 tcp->pocs[pino].layE = tcp->numlayers;
932 tcp->pocs[pino].prg = tcp->prg;
934 tcp->pocs[pino].prcS = 0;
935 tcp->pocs[pino].prcE = maxprec;;
936 tcp->pocs[pino].txS = pi[pino].tx0;
937 tcp->pocs[pino].txE = pi[pino].tx1;
938 tcp->pocs[pino].tyS = pi[pino].ty0;
939 tcp->pocs[pino].tyE = pi[pino].ty1;
940 tcp->pocs[pino].dx = pi[pino].dx;
941 tcp->pocs[pino].dy = pi[pino].dy;
948 void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
950 opj_tcp_t *tcp = &cp->tcps[tileno];
952 for (pino = 0; pino < tcp->numpocs + 1; pino++) {
954 for (compno = 0; compno < pi->numcomps; compno++) {
955 opj_pi_comp_t *comp = &pi[pino].comps[compno];
956 if(comp->resolutions) {
957 opj_free(comp->resolutions);
960 opj_free(pi[pino].comps);
964 opj_free(pi->include);
970 opj_bool pi_next(opj_pi_iterator_t * pi) {
971 switch (pi->poc.prg) {
973 return pi_next_lrcp(pi);
975 return pi_next_rlcp(pi);
977 return pi_next_rpcl(pi);
979 return pi_next_pcrl(pi);
981 return pi_next_cprl(pi);
989 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){
992 int incr_top=1,resetX=0;
993 opj_tcp_t *tcps =&cp->tcps[tileno];
994 opj_poc_t *tcp= &tcps->pocs[pino];
997 pi[pino].poc.prg = tcp->prg;
1000 case CPRL: strncpy(prog, "CPRL",4);
1002 case LRCP: strncpy(prog, "LRCP",4);
1004 case PCRL: strncpy(prog, "PCRL",4);
1006 case RLCP: strncpy(prog, "RLCP",4);
1008 case RPCL: strncpy(prog, "RPCL",4);
1014 if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
1015 pi[pino].poc.resno0 = tcp->resS;
1016 pi[pino].poc.resno1 = tcp->resE;
1017 pi[pino].poc.compno0 = tcp->compS;
1018 pi[pino].poc.compno1 = tcp->compE;
1019 pi[pino].poc.layno0 = tcp->layS;
1020 pi[pino].poc.layno1 = tcp->layE;
1021 pi[pino].poc.precno0 = tcp->prcS;
1022 pi[pino].poc.precno1 = tcp->prcE;
1023 pi[pino].poc.tx0 = tcp->txS;
1024 pi[pino].poc.ty0 = tcp->tyS;
1025 pi[pino].poc.tx1 = tcp->txE;
1026 pi[pino].poc.ty1 = tcp->tyE;
1028 if( tpnum < cur_totnum_tp){
1033 pi[pino].poc.compno0 = tcp->compS;
1034 pi[pino].poc.compno1 = tcp->compE;
1037 tcp->comp_t = tcp->compS;
1038 pi[pino].poc.compno0 = tcp->comp_t;
1039 pi[pino].poc.compno1 = tcp->comp_t+1;
1043 if(tcp->comp_t ==tcp->compE){
1044 tcp->comp_t = tcp->compS;
1045 pi[pino].poc.compno0 = tcp->comp_t;
1046 pi[pino].poc.compno1 = tcp->comp_t+1;
1050 pi[pino].poc.compno0 = tcp->comp_t;
1051 pi[pino].poc.compno1 = tcp->comp_t+1;
1056 pi[pino].poc.compno0 = tcp->comp_t-1;
1057 pi[pino].poc.compno1 = tcp->comp_t;
1065 pi[pino].poc.resno0 = tcp->resS;
1066 pi[pino].poc.resno1 = tcp->resE;
1069 tcp->res_t = tcp->resS;
1070 pi[pino].poc.resno0 = tcp->res_t;
1071 pi[pino].poc.resno1 = tcp->res_t+1;
1075 if(tcp->res_t==tcp->resE){
1076 tcp->res_t = tcp->resS;
1077 pi[pino].poc.resno0 = tcp->res_t;
1078 pi[pino].poc.resno1 = tcp->res_t+1;
1082 pi[pino].poc.resno0 = tcp->res_t;
1083 pi[pino].poc.resno1 = tcp->res_t+1;
1088 pi[pino].poc.resno0 = tcp->res_t - 1;
1089 pi[pino].poc.resno1 = tcp->res_t;
1097 pi[pino].poc.layno0 = tcp->layS;
1098 pi[pino].poc.layno1 = tcp->layE;
1101 tcp->lay_t = tcp->layS;
1102 pi[pino].poc.layno0 = tcp->lay_t;
1103 pi[pino].poc.layno1 = tcp->lay_t+1;
1107 if(tcp->lay_t == tcp->layE){
1108 tcp->lay_t = tcp->layS;
1109 pi[pino].poc.layno0 = tcp->lay_t;
1110 pi[pino].poc.layno1 = tcp->lay_t+1;
1114 pi[pino].poc.layno0 = tcp->lay_t;
1115 pi[pino].poc.layno1 = tcp->lay_t+1;
1120 pi[pino].poc.layno0 = tcp->lay_t - 1;
1121 pi[pino].poc.layno1 = tcp->lay_t;
1132 pi[pino].poc.precno0 = tcp->prcS;
1133 pi[pino].poc.precno1 = tcp->prcE;
1136 tcp->prc_t = tcp->prcS;
1137 pi[pino].poc.precno0 = tcp->prc_t;
1138 pi[pino].poc.precno1 = tcp->prc_t+1;
1142 if(tcp->prc_t == tcp->prcE){
1143 tcp->prc_t = tcp->prcS;
1144 pi[pino].poc.precno0 = tcp->prc_t;
1145 pi[pino].poc.precno1 = tcp->prc_t+1;
1149 pi[pino].poc.precno0 = tcp->prc_t;
1150 pi[pino].poc.precno1 = tcp->prc_t+1;
1155 pi[pino].poc.precno0 = tcp->prc_t - 1;
1156 pi[pino].poc.precno1 = tcp->prc_t;
1163 pi[pino].poc.tx0 = tcp->txS;
1164 pi[pino].poc.ty0 = tcp->tyS;
1165 pi[pino].poc.tx1 = tcp->txE;
1166 pi[pino].poc.ty1 = tcp->tyE;
1169 tcp->tx0_t = tcp->txS;
1170 tcp->ty0_t = tcp->tyS;
1171 pi[pino].poc.tx0 = tcp->tx0_t;
1172 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1173 pi[pino].poc.ty0 = tcp->ty0_t;
1174 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1175 tcp->tx0_t = pi[pino].poc.tx1;
1176 tcp->ty0_t = pi[pino].poc.ty1;
1179 if(tcp->tx0_t >= tcp->txE){
1180 if(tcp->ty0_t >= tcp->tyE){
1181 tcp->ty0_t = tcp->tyS;
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=1;resetX=1;
1187 pi[pino].poc.ty0 = tcp->ty0_t;
1188 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1189 tcp->ty0_t = pi[pino].poc.ty1;
1190 incr_top=0;resetX=1;
1193 tcp->tx0_t = tcp->txS;
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;
1199 pi[pino].poc.tx0 = tcp->tx0_t;
1200 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1201 tcp->tx0_t = pi[pino].poc.tx1;
1202 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1203 pi[pino].poc.ty1 = tcp->ty0_t ;
1207 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1208 pi[pino].poc.tx1 = tcp->tx0_t ;
1209 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1210 pi[pino].poc.ty1 = tcp->ty0_t ;
1227 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
1228 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
1229 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
1230 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
1231 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
1233 * @param p_image the image being encoded.
1234 * @param p_cp the coding parameters.
1235 * @param tileno the tile index of the tile being encoded.
1236 * @param p_tx0 pointer that will hold the X0 parameter for the tile
1237 * @param p_tx1 pointer that will hold the X1 parameter for the tile
1238 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
1239 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
1240 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
1241 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
1242 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
1243 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
1244 * @param p_resolutions pointer to an area corresponding to the one described above.
1246 void get_all_encoding_parameters(
1247 const opj_image_t *p_image,
1248 const opj_cp_v2_t *p_cp,
1254 OPJ_UINT32 * p_dx_min,
1255 OPJ_UINT32 * p_dy_min,
1256 OPJ_UINT32 * p_max_prec,
1257 OPJ_UINT32 * p_max_res,
1258 OPJ_UINT32 ** p_resolutions
1262 OPJ_UINT32 compno, resno;
1265 const opj_tcp_v2_t *tcp = 00;
1266 const opj_tccp_t * l_tccp = 00;
1267 const opj_image_comp_t * l_img_comp = 00;
1269 // to store l_dx, l_dy, w and h for each resolution and component.
1270 OPJ_UINT32 * lResolutionPtr;
1272 // position in x and y of tile
1275 // preconditions in debug
1277 assert(p_image != 00);
1278 assert(tileno < p_cp->tw * p_cp->th);
1281 tcp = &p_cp->tcps [tileno];
1282 l_tccp = tcp->tccps;
1283 l_img_comp = p_image->comps;
1285 // position in x and y of tile
1287 p = tileno % p_cp->tw;
1288 q = tileno / p_cp->tw;
1290 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1291 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1292 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1293 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1294 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1296 // max precision and resolution is 0 (can only grow)
1300 // take the largest value for dx_min and dy_min
1301 *p_dx_min = 0x7fffffff;
1302 *p_dy_min = 0x7fffffff;
1305 (compno = 0; compno < p_image->numcomps; ++compno)
1307 // aritmetic variables to calculate
1308 OPJ_UINT32 l_level_no;
1309 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1310 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1311 OPJ_UINT32 l_product;
1312 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1313 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1315 lResolutionPtr = p_resolutions[compno];
1317 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1318 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1319 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1320 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1322 (l_tccp->numresolutions > *p_max_res)
1324 *p_max_res = l_tccp->numresolutions;
1327 // use custom size for precincts
1328 l_level_no = l_tccp->numresolutions - 1;
1330 (resno = 0; resno < l_tccp->numresolutions; ++resno)
1332 OPJ_UINT32 l_dx, l_dy;
1333 // precinct width and height
1334 l_pdx = l_tccp->prcw[resno];
1335 l_pdy = l_tccp->prch[resno];
1336 *lResolutionPtr++ = l_pdx;
1337 *lResolutionPtr++ = l_pdy;
1338 l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1339 l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1340 // take the minimum size for l_dx for each comp and resolution
1341 *p_dx_min = int_min(*p_dx_min, l_dx);
1342 *p_dy_min = int_min(*p_dy_min, l_dy);
1343 // various calculations of extents
1345 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1346 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1347 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1348 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1349 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1350 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1351 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1352 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1353 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1354 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1355 *lResolutionPtr++ = l_pw;
1356 *lResolutionPtr++ = l_ph;
1357 l_product = l_pw * l_ph;
1360 (l_product > *p_max_prec)
1362 *p_max_prec = l_product;
1372 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
1373 * No other data is set. The include section of the packet iterator is not allocated.
1375 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
1376 * @param p_cp the coding parameters.
1377 * @param p_tile_no the index of the tile from which creating the packet iterator.
1379 opj_pi_iterator_t * pi_create(
1380 const opj_image_t *image,
1381 const opj_cp_v2_t *cp,
1386 OPJ_UINT32 pino, compno;
1387 // number of poc in the p_pi
1388 OPJ_UINT32 l_poc_bound;
1390 // pointers to tile coding parameters and components.
1391 opj_pi_iterator_t *l_pi = 00;
1392 opj_tcp_v2_t *tcp = 00;
1393 const opj_tccp_t *tccp = 00;
1395 // current packet iterator being allocated
1396 opj_pi_iterator_t *l_current_pi = 00;
1398 // preconditions in debug
1400 assert(image != 00);
1401 assert(tileno < cp->tw * cp->th);
1404 tcp = &cp->tcps[tileno];
1405 l_poc_bound = tcp->numpocs+1;
1408 // memory allocations
1409 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1416 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1417 l_current_pi = l_pi;
1419 (pino = 0; pino < l_poc_bound ; ++pino)
1421 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1423 (! l_current_pi->comps)
1425 pi_destroy_v2(l_pi, l_poc_bound);
1428 l_current_pi->numcomps = image->numcomps;
1429 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1431 (compno = 0; compno < image->numcomps; ++compno)
1433 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1434 tccp = &tcp->tccps[compno];
1435 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1437 (!comp->resolutions)
1439 pi_destroy_v2(l_pi, l_poc_bound);
1442 comp->numresolutions = tccp->numresolutions;
1443 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1453 * Destroys a packet iterator array.
1455 * @param p_pi the packet iterator array to destroy.
1456 * @param p_nb_elements the number of elements in the array.
1459 opj_pi_iterator_t *p_pi,
1460 OPJ_UINT32 p_nb_elements)
1462 OPJ_UINT32 compno, pino;
1463 opj_pi_iterator_t *l_current_pi = p_pi;
1470 opj_free(p_pi->include);
1475 (pino = 0; pino < p_nb_elements; ++pino)
1478 (l_current_pi->comps)
1480 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1482 (compno = 0; compno < l_current_pi->numcomps; compno++)
1485 (l_current_component->resolutions)
1487 opj_free(l_current_component->resolutions);
1488 l_current_component->resolutions = 00;
1490 ++l_current_component;
1492 opj_free(l_current_pi->comps);
1493 l_current_pi->comps = 0;
1503 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)
1508 // encoding prameters to set
1511 opj_pi_iterator_t * l_current_pi = 00;
1512 opj_poc_t* l_current_poc = 0;
1514 // preconditions in debug
1516 assert(p_tcp != 00);
1519 l_bound = p_tcp->numpocs+1;
1520 l_current_pi = p_pi;
1521 l_current_poc = p_tcp->pocs;
1524 (pino = 0;pino<l_bound;++pino)
1526 l_current_pi->poc.prg = l_current_poc->prg;
1527 l_current_pi->first = 1;
1529 l_current_pi->poc.resno0 = l_current_poc->resno0;
1530 l_current_pi->poc.compno0 = l_current_poc->compno0;
1531 l_current_pi->poc.layno0 = 0;
1532 l_current_pi->poc.precno0 = 0;
1533 l_current_pi->poc.resno1 = l_current_poc->resno1;
1534 l_current_pi->poc.compno1 = l_current_poc->compno1;
1535 l_current_pi->poc.layno1 = l_current_poc->layno1;
1536 l_current_pi->poc.precno1 = p_max_precision;
1543 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)
1548 // encoding prameters to set
1551 opj_pi_iterator_t * l_current_pi = 00;
1552 // preconditions in debug
1553 assert(p_tcp != 00);
1557 l_bound = p_tcp->numpocs+1;
1558 l_current_pi = p_pi;
1561 (pino = 0;pino<l_bound;++pino)
1563 l_current_pi->poc.prg = p_tcp->prg;
1564 l_current_pi->first = 1;
1565 l_current_pi->poc.resno0 = 0;
1566 l_current_pi->poc.compno0 = 0;
1567 l_current_pi->poc.layno0 = 0;
1568 l_current_pi->poc.precno0 = 0;
1569 l_current_pi->poc.resno1 = p_max_res;
1570 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1571 l_current_pi->poc.layno1 = p_tcp->numlayers;
1572 l_current_pi->poc.precno1 = p_max_precision;