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 opj_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 opj_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 opj_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 opj_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 opj_pi_next_cprl(opj_pi_iterator_t * pi);
73 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
75 * @param p_cp the coding parameters to modify
76 * @param p_tileno the tile index being concerned.
77 * @param p_tx0 X0 parameter for the tile
78 * @param p_tx1 X1 parameter for the tile
79 * @param p_ty0 Y0 parameter for the tile
80 * @param p_ty1 Y1 parameter for the tile
81 * @param p_max_prec the maximum precision for all the bands of the tile
82 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
83 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
84 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
86 static void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
92 OPJ_UINT32 p_max_prec,
98 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
100 * @param p_cp the coding parameters to modify
101 * @param p_tileno the tile index being concerned.
102 * @param p_tx0 X0 parameter for the tile
103 * @param p_tx1 X1 parameter for the tile
104 * @param p_ty0 Y0 parameter for the tile
105 * @param p_ty1 Y1 parameter for the tile
106 * @param p_max_prec the maximum precision for all the bands of the tile
107 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
108 * @param dx_min the minimum dx of all the components of all the resolutions for the tile.
109 * @param dy_min the minimum dy of all the components of all the resolutions for the tile.
111 static void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
112 OPJ_UINT32 p_num_comps,
118 OPJ_UINT32 p_max_prec,
119 OPJ_UINT32 p_max_res,
121 OPJ_UINT32 p_dy_min);
123 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
125 * @param p_image the image being encoded.
126 * @param p_cp the coding parameters.
127 * @param p_tileno the tile index of the tile being encoded.
128 * @param p_tx0 pointer that will hold the X0 parameter for the tile
129 * @param p_tx1 pointer that will hold the X1 parameter for the tile
130 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
131 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
132 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
133 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
134 * @param dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
135 * @param dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
137 static void opj_get_encoding_parameters(const opj_image_t *p_image,
138 const opj_cp_t *p_cp,
144 OPJ_UINT32 * p_dx_min,
145 OPJ_UINT32 * p_dy_min,
146 OPJ_UINT32 * p_max_prec,
147 OPJ_UINT32 * p_max_res );
150 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
151 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
152 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
153 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
154 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
156 * @param p_image the image being encoded.
157 * @param p_cp the coding parameters.
158 * @param tileno the tile index of the tile being encoded.
159 * @param p_tx0 pointer that will hold the X0 parameter for the tile
160 * @param p_tx1 pointer that will hold the X1 parameter for the tile
161 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
162 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
163 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
164 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
165 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
166 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
167 * @param p_resolutions pointer to an area corresponding to the one described above.
169 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
170 const opj_cp_t *p_cp,
176 OPJ_UINT32 * p_dx_min,
177 OPJ_UINT32 * p_dy_min,
178 OPJ_UINT32 * p_max_prec,
179 OPJ_UINT32 * p_max_res,
180 OPJ_UINT32 ** p_resolutions );
182 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
183 * No other data is set. The include section of the packet iterator is not allocated.
185 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
186 * @param p_cp the coding parameters.
187 * @param p_tile_no the index of the tile from which creating the packet iterator.
189 static opj_pi_iterator_t * opj_pi_create( const opj_image_t *image,
195 static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
196 opj_tcp_v2_t * p_tcp,
197 OPJ_UINT32 p_max_precision,
198 OPJ_UINT32 p_max_res);
202 static void opj_pi_update_decode_poc ( opj_pi_iterator_t * p_pi,
203 opj_tcp_v2_t * p_tcp,
204 OPJ_UINT32 p_max_precision,
205 OPJ_UINT32 p_max_res);
210 opj_bool opj_pi_check_next_level( OPJ_INT32 pos,
214 const OPJ_CHAR *prog);
221 ==========================================================
223 ==========================================================
226 opj_bool opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
227 opj_pi_comp_t *comp = NULL;
228 opj_pi_resolution_t *res = NULL;
229 OPJ_UINT32 index = 0;
232 comp = &pi->comps[pi->compno];
233 res = &comp->resolutions[pi->resno];
239 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
240 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
242 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
243 comp = &pi->comps[pi->compno];
244 if (pi->resno >= comp->numresolutions) {
247 res = &comp->resolutions[pi->resno];
249 pi->poc.precno1 = res->pw * res->ph;
251 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
252 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
253 if (!pi->include[index]) {
254 pi->include[index] = 1;
266 opj_bool opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
267 opj_pi_comp_t *comp = NULL;
268 opj_pi_resolution_t *res = NULL;
269 OPJ_UINT32 index = 0;
272 comp = &pi->comps[pi->compno];
273 res = &comp->resolutions[pi->resno];
279 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
280 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
281 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
282 comp = &pi->comps[pi->compno];
283 if (pi->resno >= comp->numresolutions) {
286 res = &comp->resolutions[pi->resno];
288 pi->poc.precno1 = res->pw * res->ph;
290 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
291 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
292 if (!pi->include[index]) {
293 pi->include[index] = 1;
305 opj_bool opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
306 opj_pi_comp_t *comp = NULL;
307 opj_pi_resolution_t *res = NULL;
308 OPJ_UINT32 index = 0;
313 OPJ_UINT32 compno, resno;
317 for (compno = 0; compno < pi->numcomps; compno++) {
318 comp = &pi->comps[compno];
319 for (resno = 0; resno < comp->numresolutions; resno++) {
321 res = &comp->resolutions[resno];
322 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
323 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
324 pi->dx = !pi->dx ? dx : opj_int_min(pi->dx, dx);
325 pi->dy = !pi->dy ? dy : opj_int_min(pi->dy, dy);
330 pi->poc.ty0 = pi->ty0;
331 pi->poc.tx0 = pi->tx0;
332 pi->poc.ty1 = pi->ty1;
333 pi->poc.tx1 = pi->tx1;
335 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
336 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
337 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
338 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
340 OPJ_INT32 trx0, try0;
341 OPJ_INT32 trx1, try1;
343 OPJ_INT32 prci, prcj;
344 comp = &pi->comps[pi->compno];
345 if (pi->resno >= comp->numresolutions) {
348 res = &comp->resolutions[pi->resno];
349 levelno = comp->numresolutions - 1 - pi->resno;
350 trx0 = opj_int_ceildiv(pi->tx0, comp->dx << levelno);
351 try0 = opj_int_ceildiv(pi->ty0, comp->dy << levelno);
352 trx1 = opj_int_ceildiv(pi->tx1, comp->dx << levelno);
353 try1 = opj_int_ceildiv(pi->ty1, comp->dy << levelno);
354 rpx = res->pdx + levelno;
355 rpy = res->pdy + levelno;
356 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
359 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
363 if ((res->pw==0)||(res->ph==0)) continue;
365 if ((trx0==trx1)||(try0==try1)) continue;
367 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
368 - opj_int_floordivpow2(trx0, res->pdx);
369 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
370 - opj_int_floordivpow2(try0, res->pdy);
371 pi->precno = prci + prcj * res->pw;
372 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
373 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
374 if (!pi->include[index]) {
375 pi->include[index] = 1;
388 opj_bool opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
389 opj_pi_comp_t *comp = NULL;
390 opj_pi_resolution_t *res = NULL;
391 OPJ_UINT32 index = 0;
394 comp = &pi->comps[pi->compno];
397 OPJ_UINT32 compno, resno;
401 for (compno = 0; compno < pi->numcomps; compno++) {
402 comp = &pi->comps[compno];
403 for (resno = 0; resno < comp->numresolutions; resno++) {
405 res = &comp->resolutions[resno];
406 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
407 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
408 pi->dx = !pi->dx ? dx : opj_int_min(pi->dx, dx);
409 pi->dy = !pi->dy ? dy : opj_int_min(pi->dy, dy);
414 pi->poc.ty0 = pi->ty0;
415 pi->poc.tx0 = pi->tx0;
416 pi->poc.ty1 = pi->ty1;
417 pi->poc.tx1 = pi->tx1;
419 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
420 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
421 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
422 comp = &pi->comps[pi->compno];
423 for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
425 OPJ_INT32 trx0, try0;
426 OPJ_INT32 trx1, try1;
428 OPJ_INT32 prci, prcj;
429 res = &comp->resolutions[pi->resno];
430 levelno = comp->numresolutions - 1 - pi->resno;
431 trx0 = opj_int_ceildiv(pi->tx0, comp->dx << levelno);
432 try0 = opj_int_ceildiv(pi->ty0, comp->dy << levelno);
433 trx1 = opj_int_ceildiv(pi->tx1, comp->dx << levelno);
434 try1 = opj_int_ceildiv(pi->ty1, comp->dy << levelno);
435 rpx = res->pdx + levelno;
436 rpy = res->pdy + levelno;
437 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
440 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
444 if ((res->pw==0)||(res->ph==0)) continue;
446 if ((trx0==trx1)||(try0==try1)) continue;
448 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
449 - opj_int_floordivpow2(trx0, res->pdx);
450 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
451 - opj_int_floordivpow2(try0, res->pdy);
452 pi->precno = prci + prcj * res->pw;
453 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
454 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
455 if (!pi->include[index]) {
456 pi->include[index] = 1;
469 opj_bool opj_pi_next_cprl(opj_pi_iterator_t * pi) {
470 opj_pi_comp_t *comp = NULL;
471 opj_pi_resolution_t *res = NULL;
472 OPJ_UINT32 index = 0;
475 comp = &pi->comps[pi->compno];
481 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
483 comp = &pi->comps[pi->compno];
486 for (resno = 0; resno < comp->numresolutions; resno++) {
488 res = &comp->resolutions[resno];
489 dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
490 dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
491 pi->dx = !pi->dx ? dx : opj_int_min(pi->dx, dx);
492 pi->dy = !pi->dy ? dy : opj_int_min(pi->dy, dy);
495 pi->poc.ty0 = pi->ty0;
496 pi->poc.tx0 = pi->tx0;
497 pi->poc.ty1 = pi->ty1;
498 pi->poc.tx1 = pi->tx1;
500 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
501 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
502 for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
504 OPJ_INT32 trx0, try0;
505 OPJ_INT32 trx1, try1;
507 OPJ_INT32 prci, prcj;
508 res = &comp->resolutions[pi->resno];
509 levelno = comp->numresolutions - 1 - pi->resno;
510 trx0 = opj_int_ceildiv(pi->tx0, comp->dx << levelno);
511 try0 = opj_int_ceildiv(pi->ty0, comp->dy << levelno);
512 trx1 = opj_int_ceildiv(pi->tx1, comp->dx << levelno);
513 try1 = opj_int_ceildiv(pi->ty1, comp->dy << levelno);
514 rpx = res->pdx + levelno;
515 rpy = res->pdy + levelno;
516 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
519 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
523 if ((res->pw==0)||(res->ph==0)) continue;
525 if ((trx0==trx1)||(try0==try1)) continue;
527 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
528 - opj_int_floordivpow2(trx0, res->pdx);
529 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
530 - opj_int_floordivpow2(try0, res->pdy);
531 pi->precno = prci + prcj * res->pw;
532 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
533 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
534 if (!pi->include[index]) {
535 pi->include[index] = 1;
548 void opj_get_encoding_parameters( const opj_image_t *p_image,
549 const opj_cp_t *p_cp,
555 OPJ_UINT32 * p_dx_min,
556 OPJ_UINT32 * p_dy_min,
557 OPJ_UINT32 * p_max_prec,
558 OPJ_UINT32 * p_max_res )
561 OPJ_UINT32 compno, resno;
563 const opj_tcp_v2_t *l_tcp = 00;
564 const opj_tccp_t * l_tccp = 00;
565 const opj_image_comp_t * l_img_comp = 00;
567 /* position in x and y of tile */
572 assert(p_image != 00);
573 assert(p_tileno < p_cp->tw * p_cp->th);
575 /* initializations */
576 l_tcp = &p_cp->tcps [p_tileno];
577 l_img_comp = p_image->comps;
578 l_tccp = l_tcp->tccps;
580 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
581 p = p_tileno % p_cp->tw;
582 q = p_tileno / p_cp->tw;
584 /* find extent of tile */
585 *p_tx0 = opj_int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
586 *p_tx1 = opj_int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
587 *p_ty0 = opj_int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
588 *p_ty1 = opj_int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
590 /* max precision is 0 (can only grow) */
594 /* take the largest value for dx_min and dy_min */
595 *p_dx_min = 0x7fffffff;
596 *p_dy_min = 0x7fffffff;
598 for (compno = 0; compno < p_image->numcomps; ++compno) {
599 /* arithmetic variables to calculate */
600 OPJ_UINT32 l_level_no;
601 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
602 OPJ_INT32 l_px0, l_py0, l_px1, py1;
603 OPJ_UINT32 l_pdx, l_pdy;
604 OPJ_UINT32 l_pw, l_ph;
605 OPJ_UINT32 l_product;
606 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
608 l_tcx0 = opj_int_ceildiv(*p_tx0, l_img_comp->dx);
609 l_tcy0 = opj_int_ceildiv(*p_ty0, l_img_comp->dy);
610 l_tcx1 = opj_int_ceildiv(*p_tx1, l_img_comp->dx);
611 l_tcy1 = opj_int_ceildiv(*p_ty1, l_img_comp->dy);
613 if (l_tccp->numresolutions > *p_max_res) {
614 *p_max_res = l_tccp->numresolutions;
617 /* use custom size for precincts */
618 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
619 OPJ_UINT32 l_dx, l_dy;
621 /* precinct width and height */
622 l_pdx = l_tccp->prcw[resno];
623 l_pdy = l_tccp->prch[resno];
625 l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
626 l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
628 /* take the minimum size for dx for each comp and resolution */
629 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
630 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
632 /* various calculations of extents */
633 l_level_no = l_tccp->numresolutions - 1 - resno;
635 l_rx0 = opj_int_ceildivpow2(l_tcx0, l_level_no);
636 l_ry0 = opj_int_ceildivpow2(l_tcy0, l_level_no);
637 l_rx1 = opj_int_ceildivpow2(l_tcx1, l_level_no);
638 l_ry1 = opj_int_ceildivpow2(l_tcy1, l_level_no);
640 l_px0 = opj_int_floordivpow2(l_rx0, l_pdx) << l_pdx;
641 l_py0 = opj_int_floordivpow2(l_ry0, l_pdy) << l_pdy;
642 l_px1 = opj_int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
644 py1 = opj_int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
646 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
647 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
649 l_product = l_pw * l_ph;
651 /* update precision */
652 if (l_product > *p_max_prec) {
653 *p_max_prec = l_product;
662 void opj_get_all_encoding_parameters( const opj_image_t *p_image,
663 const opj_cp_t *p_cp,
669 OPJ_UINT32 * p_dx_min,
670 OPJ_UINT32 * p_dy_min,
671 OPJ_UINT32 * p_max_prec,
672 OPJ_UINT32 * p_max_res,
673 OPJ_UINT32 ** p_resolutions )
676 OPJ_UINT32 compno, resno;
679 const opj_tcp_v2_t *tcp = 00;
680 const opj_tccp_t * l_tccp = 00;
681 const opj_image_comp_t * l_img_comp = 00;
683 /* to store l_dx, l_dy, w and h for each resolution and component.*/
684 OPJ_UINT32 * lResolutionPtr;
686 /* position in x and y of tile*/
689 /* preconditions in debug*/
691 assert(p_image != 00);
692 assert(tileno < p_cp->tw * p_cp->th);
695 tcp = &p_cp->tcps [tileno];
697 l_img_comp = p_image->comps;
699 /* position in x and y of tile*/
700 p = tileno % p_cp->tw;
701 q = tileno / p_cp->tw;
703 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
704 *p_tx0 = opj_int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
705 *p_tx1 = opj_int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
706 *p_ty0 = opj_int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
707 *p_ty1 = opj_int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
709 /* max precision and resolution is 0 (can only grow)*/
713 /* take the largest value for dx_min and dy_min*/
714 *p_dx_min = 0x7fffffff;
715 *p_dy_min = 0x7fffffff;
717 for (compno = 0; compno < p_image->numcomps; ++compno) {
718 /* aritmetic variables to calculate*/
719 OPJ_UINT32 l_level_no;
720 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
721 OPJ_INT32 l_px0, l_py0, l_px1, py1;
722 OPJ_UINT32 l_product;
723 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
724 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
726 lResolutionPtr = p_resolutions[compno];
728 l_tcx0 = opj_int_ceildiv(*p_tx0, l_img_comp->dx);
729 l_tcy0 = opj_int_ceildiv(*p_ty0, l_img_comp->dy);
730 l_tcx1 = opj_int_ceildiv(*p_tx1, l_img_comp->dx);
731 l_tcy1 = opj_int_ceildiv(*p_ty1, l_img_comp->dy);
733 if (l_tccp->numresolutions > *p_max_res) {
734 *p_max_res = l_tccp->numresolutions;
737 /* use custom size for precincts*/
738 l_level_no = l_tccp->numresolutions - 1;
739 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
740 OPJ_UINT32 l_dx, l_dy;
742 /* precinct width and height*/
743 l_pdx = l_tccp->prcw[resno];
744 l_pdy = l_tccp->prch[resno];
745 *lResolutionPtr++ = l_pdx;
746 *lResolutionPtr++ = l_pdy;
747 l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
748 l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
749 /* take the minimum size for l_dx for each comp and resolution*/
750 *p_dx_min = opj_int_min(*p_dx_min, l_dx);
751 *p_dy_min = opj_int_min(*p_dy_min, l_dy);
753 /* various calculations of extents*/
754 l_rx0 = opj_int_ceildivpow2(l_tcx0, l_level_no);
755 l_ry0 = opj_int_ceildivpow2(l_tcy0, l_level_no);
756 l_rx1 = opj_int_ceildivpow2(l_tcx1, l_level_no);
757 l_ry1 = opj_int_ceildivpow2(l_tcy1, l_level_no);
758 l_px0 = opj_int_floordivpow2(l_rx0, l_pdx) << l_pdx;
759 l_py0 = opj_int_floordivpow2(l_ry0, l_pdy) << l_pdy;
760 l_px1 = opj_int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
761 py1 = opj_int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
762 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
763 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
764 *lResolutionPtr++ = l_pw;
765 *lResolutionPtr++ = l_ph;
766 l_product = l_pw * l_ph;
768 /* update precision*/
769 if (l_product > *p_max_prec) {
770 *p_max_prec = l_product;
780 opj_pi_iterator_t * opj_pi_create( const opj_image_t *image,
785 OPJ_UINT32 pino, compno;
786 /* number of poc in the p_pi*/
787 OPJ_UINT32 l_poc_bound;
789 /* pointers to tile coding parameters and components.*/
790 opj_pi_iterator_t *l_pi = 00;
791 opj_tcp_v2_t *tcp = 00;
792 const opj_tccp_t *tccp = 00;
794 /* current packet iterator being allocated*/
795 opj_pi_iterator_t *l_current_pi = 00;
797 /* preconditions in debug*/
800 assert(tileno < cp->tw * cp->th);
803 tcp = &cp->tcps[tileno];
804 l_poc_bound = tcp->numpocs+1;
806 /* memory allocations*/
807 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
811 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
814 for (pino = 0; pino < l_poc_bound ; ++pino) {
816 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
817 if (! l_current_pi->comps) {
818 opj_pi_destroy(l_pi, l_poc_bound);
822 l_current_pi->numcomps = image->numcomps;
823 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
825 for (compno = 0; compno < image->numcomps; ++compno) {
826 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
828 tccp = &tcp->tccps[compno];
830 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
831 if (!comp->resolutions) {
832 opj_pi_destroy(l_pi, l_poc_bound);
836 comp->numresolutions = tccp->numresolutions;
837 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
844 void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
850 OPJ_UINT32 p_max_prec,
851 OPJ_UINT32 p_max_res,
857 /* tile coding parameter*/
858 opj_tcp_v2_t *l_tcp = 00;
859 /* current poc being updated*/
860 opj_poc_t * l_current_poc = 00;
863 OPJ_UINT32 l_poc_bound;
865 /* preconditions in debug*/
867 assert(p_tileno < p_cp->tw * p_cp->th);
870 l_tcp = &p_cp->tcps [p_tileno];
871 /* number of iterations in the loop */
872 l_poc_bound = l_tcp->numpocs+1;
874 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
875 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
876 l_current_poc = l_tcp->pocs;
878 l_current_poc->compS = l_current_poc->compno0;
879 l_current_poc->compE = l_current_poc->compno1;
880 l_current_poc->resS = l_current_poc->resno0;
881 l_current_poc->resE = l_current_poc->resno1;
882 l_current_poc->layE = l_current_poc->layno1;
884 /* special treatment for the first element*/
885 l_current_poc->layS = 0;
886 l_current_poc->prg = l_current_poc->prg1;
887 l_current_poc->prcS = 0;
889 l_current_poc->prcE = p_max_prec;
890 l_current_poc->txS = p_tx0;
891 l_current_poc->txE = p_tx1;
892 l_current_poc->tyS = p_ty0;
893 l_current_poc->tyE = p_ty1;
894 l_current_poc->dx = p_dx_min;
895 l_current_poc->dy = p_dy_min;
898 for (pino = 1;pino < l_poc_bound ; ++pino) {
899 l_current_poc->compS = l_current_poc->compno0;
900 l_current_poc->compE= l_current_poc->compno1;
901 l_current_poc->resS = l_current_poc->resno0;
902 l_current_poc->resE = l_current_poc->resno1;
903 l_current_poc->layE = l_current_poc->layno1;
904 l_current_poc->prg = l_current_poc->prg1;
905 l_current_poc->prcS = 0;
906 /* special treatment here different from the first element*/
907 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
909 l_current_poc->prcE = p_max_prec;
910 l_current_poc->txS = p_tx0;
911 l_current_poc->txE = p_tx1;
912 l_current_poc->tyS = p_ty0;
913 l_current_poc->tyE = p_ty1;
914 l_current_poc->dx = p_dx_min;
915 l_current_poc->dy = p_dy_min;
920 void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
921 OPJ_UINT32 p_num_comps,
927 OPJ_UINT32 p_max_prec,
928 OPJ_UINT32 p_max_res,
934 /* tile coding parameter*/
935 opj_tcp_v2_t *l_tcp = 00;
936 /* current poc being updated*/
937 opj_poc_t * l_current_poc = 00;
939 OPJ_UINT32 l_poc_bound;
941 /* preconditions in debug*/
943 assert(p_tileno < p_cp->tw * p_cp->th);
946 l_tcp = &p_cp->tcps [p_tileno];
948 /* number of iterations in the loop */
949 l_poc_bound = l_tcp->numpocs+1;
951 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
952 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
953 l_current_poc = l_tcp->pocs;
955 for (pino = 0; pino < l_poc_bound ; ++pino) {
956 l_current_poc->compS = 0;
957 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
958 l_current_poc->resS = 0;
959 l_current_poc->resE = p_max_res;
960 l_current_poc->layS = 0;
961 l_current_poc->layE = l_tcp->numlayers;
962 l_current_poc->prg = l_tcp->prg;
963 l_current_poc->prcS = 0;
964 l_current_poc->prcE = p_max_prec;
965 l_current_poc->txS = p_tx0;
966 l_current_poc->txE = p_tx1;
967 l_current_poc->tyS = p_ty0;
968 l_current_poc->tyE = p_ty1;
969 l_current_poc->dx = p_dx_min;
970 l_current_poc->dy = p_dy_min;
975 void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
976 opj_tcp_v2_t * p_tcp,
977 OPJ_UINT32 p_max_precision,
978 OPJ_UINT32 p_max_res)
983 /* encoding prameters to set*/
986 opj_pi_iterator_t * l_current_pi = 00;
987 opj_poc_t* l_current_poc = 0;
989 /* preconditions in debug*/
994 l_bound = p_tcp->numpocs+1;
996 l_current_poc = p_tcp->pocs;
998 for (pino = 0;pino<l_bound;++pino) {
999 l_current_pi->poc.prg = l_current_poc->prg;
1000 l_current_pi->first = 1;
1002 l_current_pi->poc.resno0 = l_current_poc->resno0;
1003 l_current_pi->poc.compno0 = l_current_poc->compno0;
1004 l_current_pi->poc.layno0 = 0;
1005 l_current_pi->poc.precno0 = 0;
1006 l_current_pi->poc.resno1 = l_current_poc->resno1;
1007 l_current_pi->poc.compno1 = l_current_poc->compno1;
1008 l_current_pi->poc.layno1 = l_current_poc->layno1;
1009 l_current_pi->poc.precno1 = p_max_precision;
1015 void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
1016 opj_tcp_v2_t * p_tcp,
1017 OPJ_UINT32 p_max_precision,
1018 OPJ_UINT32 p_max_res)
1023 /* encoding prameters to set*/
1026 opj_pi_iterator_t * l_current_pi = 00;
1027 /* preconditions in debug*/
1028 assert(p_tcp != 00);
1031 /* initializations*/
1032 l_bound = p_tcp->numpocs+1;
1033 l_current_pi = p_pi;
1035 for (pino = 0;pino<l_bound;++pino) {
1036 l_current_pi->poc.prg = p_tcp->prg;
1037 l_current_pi->first = 1;
1038 l_current_pi->poc.resno0 = 0;
1039 l_current_pi->poc.compno0 = 0;
1040 l_current_pi->poc.layno0 = 0;
1041 l_current_pi->poc.precno0 = 0;
1042 l_current_pi->poc.resno1 = p_max_res;
1043 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1044 l_current_pi->poc.layno1 = p_tcp->numlayers;
1045 l_current_pi->poc.precno1 = p_max_precision;
1052 opj_bool opj_pi_check_next_level( OPJ_INT32 pos,
1056 const OPJ_CHAR *prog)
1059 opj_tcp_v2_t *tcps =&cp->tcps[tileno];
1060 opj_poc_t *tcp = &tcps->pocs[pino];
1063 for(i=pos;pos>=0;i--){
1066 if(tcp->res_t==tcp->resE){
1067 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1077 if(tcp->comp_t==tcp->compE){
1078 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1088 if(tcp->lay_t==tcp->layE){
1089 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1101 if(tcp->prc_t == tcp->prcE){
1102 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1112 if(tcp->tx0_t == tcp->txE){
1114 if(tcp->ty0_t == tcp->tyE){
1115 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1136 ==========================================================
1137 Packet iterator interface
1138 ==========================================================
1140 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1142 OPJ_UINT32 p_tile_no)
1146 OPJ_UINT32 compno, resno;
1148 /* to store w, h, dx and dy fro all components and resolutions */
1149 OPJ_UINT32 * l_tmp_data;
1150 OPJ_UINT32 ** l_tmp_ptr;
1152 /* encoding prameters to set */
1153 OPJ_UINT32 l_max_res;
1154 OPJ_UINT32 l_max_prec;
1155 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1156 OPJ_UINT32 l_dx_min,l_dy_min;
1158 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1159 OPJ_UINT32 l_data_stride;
1162 opj_pi_iterator_t *l_pi = 00;
1163 opj_tcp_v2_t *l_tcp = 00;
1164 const opj_tccp_t *l_tccp = 00;
1165 opj_pi_comp_t *l_current_comp = 00;
1166 opj_image_comp_t * l_img_comp = 00;
1167 opj_pi_iterator_t * l_current_pi = 00;
1168 OPJ_UINT32 * l_encoding_value_ptr = 00;
1170 /* preconditions in debug */
1172 assert(p_image != 00);
1173 assert(p_tile_no < p_cp->tw * p_cp->th);
1175 /* initializations */
1176 l_tcp = &p_cp->tcps[p_tile_no];
1177 l_bound = l_tcp->numpocs+1;
1179 l_data_stride = 4 * J2K_MAXRLVLS;
1180 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1181 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1187 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1188 p_image->numcomps * sizeof(OPJ_UINT32 *));
1192 opj_free(l_tmp_data);
1196 /* memory allocation for pi */
1197 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1199 opj_free(l_tmp_data);
1200 opj_free(l_tmp_ptr);
1204 l_encoding_value_ptr = l_tmp_data;
1205 /* update pointer array */
1207 (compno = 0; compno < p_image->numcomps; ++compno)
1209 l_tmp_ptr[compno] = l_encoding_value_ptr;
1210 l_encoding_value_ptr += l_data_stride;
1212 /* get encoding parameters */
1213 opj_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);
1215 /* step calculations */
1217 l_step_c = l_max_prec * l_step_p;
1218 l_step_r = p_image->numcomps * l_step_c;
1219 l_step_l = l_max_res * l_step_r;
1221 /* set values for first packet iterator */
1222 l_current_pi = l_pi;
1224 /* memory allocation for include */
1225 l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
1227 (!l_current_pi->include)
1229 opj_free(l_tmp_data);
1230 opj_free(l_tmp_ptr);
1231 opj_pi_destroy(l_pi, l_bound);
1234 memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
1236 /* special treatment for the first packet iterator */
1237 l_current_comp = l_current_pi->comps;
1238 l_img_comp = p_image->comps;
1239 l_tccp = l_tcp->tccps;
1241 l_current_pi->tx0 = l_tx0;
1242 l_current_pi->ty0 = l_ty0;
1243 l_current_pi->tx1 = l_tx1;
1244 l_current_pi->ty1 = l_ty1;
1246 /*l_current_pi->dx = l_img_comp->dx;*/
1247 /*l_current_pi->dy = l_img_comp->dy;*/
1249 l_current_pi->step_p = l_step_p;
1250 l_current_pi->step_c = l_step_c;
1251 l_current_pi->step_r = l_step_r;
1252 l_current_pi->step_l = l_step_l;
1254 /* allocation for components and number of components has already been calculated by opj_pi_create */
1256 (compno = 0; compno < l_current_pi->numcomps; ++compno)
1258 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1259 l_encoding_value_ptr = l_tmp_ptr[compno];
1261 l_current_comp->dx = l_img_comp->dx;
1262 l_current_comp->dy = l_img_comp->dy;
1263 /* resolutions have already been initialized */
1265 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1267 l_res->pdx = *(l_encoding_value_ptr++);
1268 l_res->pdy = *(l_encoding_value_ptr++);
1269 l_res->pw = *(l_encoding_value_ptr++);
1270 l_res->ph = *(l_encoding_value_ptr++);
1280 (pino = 1 ; pino<l_bound ; ++pino )
1282 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
1283 opj_image_comp_t * l_img_comp = p_image->comps;
1284 l_tccp = l_tcp->tccps;
1286 l_current_pi->tx0 = l_tx0;
1287 l_current_pi->ty0 = l_ty0;
1288 l_current_pi->tx1 = l_tx1;
1289 l_current_pi->ty1 = l_ty1;
1290 /*l_current_pi->dx = l_dx_min;*/
1291 /*l_current_pi->dy = l_dy_min;*/
1292 l_current_pi->step_p = l_step_p;
1293 l_current_pi->step_c = l_step_c;
1294 l_current_pi->step_r = l_step_r;
1295 l_current_pi->step_l = l_step_l;
1297 /* allocation for components and number of components has already been calculated by opj_pi_create */
1299 (compno = 0; compno < l_current_pi->numcomps; ++compno)
1301 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1302 l_encoding_value_ptr = l_tmp_ptr[compno];
1304 l_current_comp->dx = l_img_comp->dx;
1305 l_current_comp->dy = l_img_comp->dy;
1306 /* resolutions have already been initialized */
1308 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1310 l_res->pdx = *(l_encoding_value_ptr++);
1311 l_res->pdy = *(l_encoding_value_ptr++);
1312 l_res->pw = *(l_encoding_value_ptr++);
1313 l_res->ph = *(l_encoding_value_ptr++);
1320 /* special treatment*/
1321 l_current_pi->include = (l_current_pi-1)->include;
1324 opj_free(l_tmp_data);
1326 opj_free(l_tmp_ptr);
1331 opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
1335 opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
1342 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1344 OPJ_UINT32 p_tile_no,
1345 J2K_T2_MODE p_t2_mode )
1349 OPJ_UINT32 compno, resno;
1351 /* to store w, h, dx and dy fro all components and resolutions*/
1352 OPJ_UINT32 * l_tmp_data;
1353 OPJ_UINT32 ** l_tmp_ptr;
1355 /* encoding prameters to set*/
1356 OPJ_UINT32 l_max_res;
1357 OPJ_UINT32 l_max_prec;
1358 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1359 OPJ_UINT32 l_dx_min,l_dy_min;
1361 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1362 OPJ_UINT32 l_data_stride;
1365 opj_pi_iterator_t *l_pi = 00;
1366 opj_tcp_v2_t *l_tcp = 00;
1367 const opj_tccp_t *l_tccp = 00;
1368 opj_pi_comp_t *l_current_comp = 00;
1369 opj_image_comp_t * l_img_comp = 00;
1370 opj_pi_iterator_t * l_current_pi = 00;
1371 OPJ_UINT32 * l_encoding_value_ptr = 00;
1373 /* preconditions in debug*/
1375 assert(p_image != 00);
1376 assert(p_tile_no < p_cp->tw * p_cp->th);
1378 /* initializations*/
1379 l_tcp = &p_cp->tcps[p_tile_no];
1380 l_bound = l_tcp->numpocs+1;
1382 l_data_stride = 4 * J2K_MAXRLVLS;
1383 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1384 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1389 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1390 p_image->numcomps * sizeof(OPJ_UINT32 *));
1392 opj_free(l_tmp_data);
1396 /* memory allocation for pi*/
1397 l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
1399 opj_free(l_tmp_data);
1400 opj_free(l_tmp_ptr);
1404 l_encoding_value_ptr = l_tmp_data;
1405 /* update pointer array*/
1406 for (compno = 0; compno < p_image->numcomps; ++compno) {
1407 l_tmp_ptr[compno] = l_encoding_value_ptr;
1408 l_encoding_value_ptr += l_data_stride;
1411 /* get encoding parameters*/
1412 opj_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);
1414 /* step calculations*/
1416 l_step_c = l_max_prec * l_step_p;
1417 l_step_r = p_image->numcomps * l_step_c;
1418 l_step_l = l_max_res * l_step_r;
1420 /* set values for first packet iterator*/
1421 l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1422 l_current_pi = l_pi;
1424 /* memory allocation for include*/
1425 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1426 if (!l_current_pi->include) {
1427 opj_free(l_tmp_data);
1428 opj_free(l_tmp_ptr);
1429 opj_pi_destroy(l_pi, l_bound);
1432 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
1434 /* special treatment for the first packet iterator*/
1435 l_current_comp = l_current_pi->comps;
1436 l_img_comp = p_image->comps;
1437 l_tccp = l_tcp->tccps;
1438 l_current_pi->tx0 = l_tx0;
1439 l_current_pi->ty0 = l_ty0;
1440 l_current_pi->tx1 = l_tx1;
1441 l_current_pi->ty1 = l_ty1;
1442 l_current_pi->dx = l_dx_min;
1443 l_current_pi->dy = l_dy_min;
1444 l_current_pi->step_p = l_step_p;
1445 l_current_pi->step_c = l_step_c;
1446 l_current_pi->step_r = l_step_r;
1447 l_current_pi->step_l = l_step_l;
1449 /* allocation for components and number of components has already been calculated by opj_pi_create */
1450 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1451 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1452 l_encoding_value_ptr = l_tmp_ptr[compno];
1454 l_current_comp->dx = l_img_comp->dx;
1455 l_current_comp->dy = l_img_comp->dy;
1457 /* resolutions have already been initialized */
1458 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1459 l_res->pdx = *(l_encoding_value_ptr++);
1460 l_res->pdy = *(l_encoding_value_ptr++);
1461 l_res->pw = *(l_encoding_value_ptr++);
1462 l_res->ph = *(l_encoding_value_ptr++);
1472 for (pino = 1 ; pino<l_bound ; ++pino ) {
1473 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
1474 opj_image_comp_t * l_img_comp = p_image->comps;
1475 l_tccp = l_tcp->tccps;
1477 l_current_pi->tx0 = l_tx0;
1478 l_current_pi->ty0 = l_ty0;
1479 l_current_pi->tx1 = l_tx1;
1480 l_current_pi->ty1 = l_ty1;
1481 l_current_pi->dx = l_dx_min;
1482 l_current_pi->dy = l_dy_min;
1483 l_current_pi->step_p = l_step_p;
1484 l_current_pi->step_c = l_step_c;
1485 l_current_pi->step_r = l_step_r;
1486 l_current_pi->step_l = l_step_l;
1488 /* allocation for components and number of components has already been calculated by opj_pi_create */
1489 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1490 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1491 l_encoding_value_ptr = l_tmp_ptr[compno];
1493 l_current_comp->dx = l_img_comp->dx;
1494 l_current_comp->dy = l_img_comp->dy;
1495 /* resolutions have already been initialized */
1496 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1497 l_res->pdx = *(l_encoding_value_ptr++);
1498 l_res->pdy = *(l_encoding_value_ptr++);
1499 l_res->pw = *(l_encoding_value_ptr++);
1500 l_res->ph = *(l_encoding_value_ptr++);
1508 /* special treatment*/
1509 l_current_pi->include = (l_current_pi-1)->include;
1513 opj_free(l_tmp_data);
1515 opj_free(l_tmp_ptr);
1518 if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
1519 opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1522 opj_pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1528 void opj_pi_create_encode( opj_pi_iterator_t *pi,
1534 J2K_T2_MODE t2_mode)
1536 const OPJ_CHAR *prog;
1538 OPJ_UINT32 incr_top=1,resetX=0;
1539 opj_tcp_v2_t *tcps =&cp->tcps[tileno];
1540 opj_poc_t *tcp= &tcps->pocs[pino];
1542 prog = opj_j2k_convert_progression_order(tcp->prg);
1545 pi[pino].poc.prg = tcp->prg;
1547 if(!(cp->m_specific_param.m_enc.m_tp_on && ((!cp->m_specific_param.m_enc.m_cinema && (t2_mode == FINAL_PASS)) || cp->m_specific_param.m_enc.m_cinema))){
1548 pi[pino].poc.resno0 = tcp->resS;
1549 pi[pino].poc.resno1 = tcp->resE;
1550 pi[pino].poc.compno0 = tcp->compS;
1551 pi[pino].poc.compno1 = tcp->compE;
1552 pi[pino].poc.layno0 = tcp->layS;
1553 pi[pino].poc.layno1 = tcp->layE;
1554 pi[pino].poc.precno0 = tcp->prcS;
1555 pi[pino].poc.precno1 = tcp->prcE;
1556 pi[pino].poc.tx0 = tcp->txS;
1557 pi[pino].poc.ty0 = tcp->tyS;
1558 pi[pino].poc.tx1 = tcp->txE;
1559 pi[pino].poc.ty1 = tcp->tyE;
1561 for(i=tppos+1;i<4;i++){
1564 pi[pino].poc.resno0 = tcp->resS;
1565 pi[pino].poc.resno1 = tcp->resE;
1568 pi[pino].poc.compno0 = tcp->compS;
1569 pi[pino].poc.compno1 = tcp->compE;
1572 pi[pino].poc.layno0 = tcp->layS;
1573 pi[pino].poc.layno1 = tcp->layE;
1579 pi[pino].poc.precno0 = tcp->prcS;
1580 pi[pino].poc.precno1 = tcp->prcE;
1583 pi[pino].poc.tx0 = tcp->txS;
1584 pi[pino].poc.ty0 = tcp->tyS;
1585 pi[pino].poc.tx1 = tcp->txE;
1586 pi[pino].poc.ty1 = tcp->tyE;
1594 for(i=tppos;i>=0;i--){
1597 tcp->comp_t = tcp->compS;
1598 pi[pino].poc.compno0 = tcp->comp_t;
1599 pi[pino].poc.compno1 = tcp->comp_t+1;
1603 tcp->res_t = tcp->resS;
1604 pi[pino].poc.resno0 = tcp->res_t;
1605 pi[pino].poc.resno1 = tcp->res_t+1;
1609 tcp->lay_t = tcp->layS;
1610 pi[pino].poc.layno0 = tcp->lay_t;
1611 pi[pino].poc.layno1 = tcp->lay_t+1;
1618 tcp->prc_t = tcp->prcS;
1619 pi[pino].poc.precno0 = tcp->prc_t;
1620 pi[pino].poc.precno1 = tcp->prc_t+1;
1624 tcp->tx0_t = tcp->txS;
1625 tcp->ty0_t = tcp->tyS;
1626 pi[pino].poc.tx0 = tcp->tx0_t;
1627 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1628 pi[pino].poc.ty0 = tcp->ty0_t;
1629 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1630 tcp->tx0_t = pi[pino].poc.tx1;
1631 tcp->ty0_t = pi[pino].poc.ty1;
1639 for(i=tppos;i>=0;i--){
1642 pi[pino].poc.compno0 = tcp->comp_t-1;
1643 pi[pino].poc.compno1 = tcp->comp_t;
1646 pi[pino].poc.resno0 = tcp->res_t-1;
1647 pi[pino].poc.resno1 = tcp->res_t;
1650 pi[pino].poc.layno0 = tcp->lay_t-1;
1651 pi[pino].poc.layno1 = tcp->lay_t;
1657 pi[pino].poc.precno0 = tcp->prc_t-1;
1658 pi[pino].poc.precno1 = tcp->prc_t;
1661 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1662 pi[pino].poc.tx1 = tcp->tx0_t ;
1663 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1664 pi[pino].poc.ty1 = tcp->ty0_t ;
1672 if(tcp->res_t==tcp->resE){
1673 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1674 tcp->res_t = tcp->resS;
1675 pi[pino].poc.resno0 = tcp->res_t;
1676 pi[pino].poc.resno1 = tcp->res_t+1;
1683 pi[pino].poc.resno0 = tcp->res_t;
1684 pi[pino].poc.resno1 = tcp->res_t+1;
1690 if(tcp->comp_t ==tcp->compE){
1691 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1692 tcp->comp_t = tcp->compS;
1693 pi[pino].poc.compno0 = tcp->comp_t;
1694 pi[pino].poc.compno1 = tcp->comp_t+1;
1701 pi[pino].poc.compno0 = tcp->comp_t;
1702 pi[pino].poc.compno1 = tcp->comp_t+1;
1708 if(tcp->lay_t == tcp->layE){
1709 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1710 tcp->lay_t = tcp->layS;
1711 pi[pino].poc.layno0 = tcp->lay_t;
1712 pi[pino].poc.layno1 = tcp->lay_t+1;
1719 pi[pino].poc.layno0 = tcp->lay_t;
1720 pi[pino].poc.layno1 = tcp->lay_t+1;
1729 if(tcp->prc_t == tcp->prcE){
1730 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1731 tcp->prc_t = tcp->prcS;
1732 pi[pino].poc.precno0 = tcp->prc_t;
1733 pi[pino].poc.precno1 = tcp->prc_t+1;
1740 pi[pino].poc.precno0 = tcp->prc_t;
1741 pi[pino].poc.precno1 = tcp->prc_t+1;
1747 if(tcp->tx0_t >= tcp->txE){
1748 if(tcp->ty0_t >= tcp->tyE){
1749 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1750 tcp->ty0_t = tcp->tyS;
1751 pi[pino].poc.ty0 = tcp->ty0_t;
1752 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1753 tcp->ty0_t = pi[pino].poc.ty1;
1754 incr_top=1;resetX=1;
1756 incr_top=0;resetX=0;
1759 pi[pino].poc.ty0 = tcp->ty0_t;
1760 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1761 tcp->ty0_t = pi[pino].poc.ty1;
1762 incr_top=0;resetX=1;
1765 tcp->tx0_t = tcp->txS;
1766 pi[pino].poc.tx0 = tcp->tx0_t;
1767 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1768 tcp->tx0_t = pi[pino].poc.tx1;
1771 pi[pino].poc.tx0 = tcp->tx0_t;
1772 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1773 tcp->tx0_t = pi[pino].poc.tx1;
1786 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1787 OPJ_UINT32 p_nb_elements)
1789 OPJ_UINT32 compno, pino;
1790 opj_pi_iterator_t *l_current_pi = p_pi;
1792 if (p_pi->include) {
1793 opj_free(p_pi->include);
1796 for (pino = 0; pino < p_nb_elements; ++pino){
1797 if(l_current_pi->comps) {
1798 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1799 for (compno = 0; compno < l_current_pi->numcomps; compno++){
1800 if(l_current_component->resolutions) {
1801 opj_free(l_current_component->resolutions);
1802 l_current_component->resolutions = 00;
1805 ++l_current_component;
1807 opj_free(l_current_pi->comps);
1808 l_current_pi->comps = 0;
1818 void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
1820 OPJ_UINT32 p_tile_no )
1822 /* encoding parameters to set */
1823 OPJ_UINT32 l_max_res;
1824 OPJ_UINT32 l_max_prec;
1825 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1826 OPJ_UINT32 l_dx_min,l_dy_min;
1829 opj_tcp_v2_t *l_tcp = 00;
1833 assert(p_image != 00);
1834 assert(p_tile_no < p_cp->tw * p_cp->th);
1836 l_tcp = &(p_cp->tcps[p_tile_no]);
1838 /* get encoding parameters */
1839 opj_get_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);
1842 opj_pi_update_encode_poc_and_final(p_cp,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1845 opj_pi_update_encode_not_poc(p_cp,p_image->numcomps,p_tile_no,l_tx0,l_tx1,l_ty0,l_ty1,l_max_prec,l_max_res,l_dx_min,l_dy_min);
1849 opj_bool opj_pi_next(opj_pi_iterator_t * pi) {
1850 switch (pi->poc.prg) {
1852 return opj_pi_next_lrcp(pi);
1854 return opj_pi_next_rlcp(pi);
1856 return opj_pi_next_rpcl(pi);
1858 return opj_pi_next_pcrl(pi);
1860 return opj_pi_next_cprl(pi);