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_num_comps the number of components
102 * @param p_tileno the tile index being concerned.
103 * @param p_tx0 X0 parameter for the tile
104 * @param p_tx1 X1 parameter for the tile
105 * @param p_ty0 Y0 parameter for the tile
106 * @param p_ty1 Y1 parameter for the tile
107 * @param p_max_prec the maximum precision for all the bands of the tile
108 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
109 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
110 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
112 static void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
113 OPJ_UINT32 p_num_comps,
119 OPJ_UINT32 p_max_prec,
120 OPJ_UINT32 p_max_res,
122 OPJ_UINT32 p_dy_min);
124 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
126 * @param p_image the image being encoded.
127 * @param p_cp the coding parameters.
128 * @param tileno the tile index of the tile being encoded.
129 * @param p_tx0 pointer that will hold the X0 parameter for the tile
130 * @param p_tx1 pointer that will hold the X1 parameter for the tile
131 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
132 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
133 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
134 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
135 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
136 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
138 static void opj_get_encoding_parameters(const opj_image_t *p_image,
139 const opj_cp_t *p_cp,
145 OPJ_UINT32 * p_dx_min,
146 OPJ_UINT32 * p_dy_min,
147 OPJ_UINT32 * p_max_prec,
148 OPJ_UINT32 * p_max_res );
151 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
152 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
153 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
154 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
155 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
157 * @param p_image the image being encoded.
158 * @param p_cp the coding parameters.
159 * @param tileno the tile index of the tile being encoded.
160 * @param p_tx0 pointer that will hold the X0 parameter for the tile
161 * @param p_tx1 pointer that will hold the X1 parameter for the tile
162 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
163 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
164 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
165 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
166 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
167 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
168 * @param p_resolutions pointer to an area corresponding to the one described above.
170 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
171 const opj_cp_t *p_cp,
177 OPJ_UINT32 * p_dx_min,
178 OPJ_UINT32 * p_dy_min,
179 OPJ_UINT32 * p_max_prec,
180 OPJ_UINT32 * p_max_res,
181 OPJ_UINT32 ** p_resolutions );
183 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
184 * No other data is set. The include section of the packet iterator is not allocated.
186 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
187 * @param p_cp the coding parameters.
188 * @param tileno the index of the tile from which creating the packet iterator.
190 static opj_pi_iterator_t * opj_pi_create( const opj_image_t *p_image,
191 const opj_cp_t *p_cp,
196 static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
198 OPJ_UINT32 p_max_precision,
199 OPJ_UINT32 p_max_res);
203 static void opj_pi_update_decode_poc ( opj_pi_iterator_t * p_pi,
205 OPJ_UINT32 p_max_precision,
206 OPJ_UINT32 p_max_res);
211 OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
215 const OPJ_CHAR *prog);
222 ==========================================================
224 ==========================================================
227 OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
228 opj_pi_comp_t *comp = NULL;
229 opj_pi_resolution_t *res = NULL;
230 OPJ_UINT32 index = 0;
233 comp = &pi->comps[pi->compno];
234 res = &comp->resolutions[pi->resno];
240 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
241 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
243 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
244 comp = &pi->comps[pi->compno];
245 if (pi->resno >= comp->numresolutions) {
248 res = &comp->resolutions[pi->resno];
250 pi->poc.precno1 = res->pw * res->ph;
252 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
253 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
254 if (!pi->include[index]) {
255 pi->include[index] = 1;
267 OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
268 opj_pi_comp_t *comp = NULL;
269 opj_pi_resolution_t *res = NULL;
270 OPJ_UINT32 index = 0;
273 comp = &pi->comps[pi->compno];
274 res = &comp->resolutions[pi->resno];
280 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
281 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
282 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
283 comp = &pi->comps[pi->compno];
284 if (pi->resno >= comp->numresolutions) {
287 res = &comp->resolutions[pi->resno];
289 pi->poc.precno1 = res->pw * res->ph;
291 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
292 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
293 if (!pi->include[index]) {
294 pi->include[index] = 1;
306 OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
307 opj_pi_comp_t *comp = NULL;
308 opj_pi_resolution_t *res = NULL;
309 OPJ_UINT32 index = 0;
314 OPJ_UINT32 compno, resno;
318 for (compno = 0; compno < pi->numcomps; compno++) {
319 comp = &pi->comps[compno];
320 for (resno = 0; resno < comp->numresolutions; resno++) {
322 res = &comp->resolutions[resno];
323 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
324 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
325 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
326 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
331 pi->poc.ty0 = pi->ty0;
332 pi->poc.tx0 = pi->tx0;
333 pi->poc.ty1 = pi->ty1;
334 pi->poc.tx1 = pi->tx1;
336 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
337 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
338 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
339 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
341 OPJ_INT32 trx0, try0;
342 OPJ_INT32 trx1, try1;
344 OPJ_INT32 prci, prcj;
345 comp = &pi->comps[pi->compno];
346 if (pi->resno >= comp->numresolutions) {
349 res = &comp->resolutions[pi->resno];
350 levelno = comp->numresolutions - 1 - pi->resno;
351 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
352 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
353 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
354 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
355 rpx = res->pdx + levelno;
356 rpy = res->pdy + levelno;
357 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
360 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
364 if ((res->pw==0)||(res->ph==0)) continue;
366 if ((trx0==trx1)||(try0==try1)) continue;
368 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
369 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
370 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
371 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
372 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
373 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
374 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
375 if (!pi->include[index]) {
376 pi->include[index] = 1;
389 OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
390 opj_pi_comp_t *comp = NULL;
391 opj_pi_resolution_t *res = NULL;
392 OPJ_UINT32 index = 0;
395 comp = &pi->comps[pi->compno];
398 OPJ_UINT32 compno, resno;
402 for (compno = 0; compno < pi->numcomps; compno++) {
403 comp = &pi->comps[compno];
404 for (resno = 0; resno < comp->numresolutions; resno++) {
406 res = &comp->resolutions[resno];
407 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
408 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
409 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
410 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
415 pi->poc.ty0 = pi->ty0;
416 pi->poc.tx0 = pi->tx0;
417 pi->poc.ty1 = pi->ty1;
418 pi->poc.tx1 = pi->tx1;
420 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
421 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
422 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
423 comp = &pi->comps[pi->compno];
424 for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
426 OPJ_INT32 trx0, try0;
427 OPJ_INT32 trx1, try1;
429 OPJ_INT32 prci, prcj;
430 res = &comp->resolutions[pi->resno];
431 levelno = comp->numresolutions - 1 - pi->resno;
432 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
433 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
434 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
435 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
436 rpx = res->pdx + levelno;
437 rpy = res->pdy + levelno;
438 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
441 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
445 if ((res->pw==0)||(res->ph==0)) continue;
447 if ((trx0==trx1)||(try0==try1)) continue;
449 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
450 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
451 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
452 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
453 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
454 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
455 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
456 if (!pi->include[index]) {
457 pi->include[index] = 1;
470 OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
471 opj_pi_comp_t *comp = NULL;
472 opj_pi_resolution_t *res = NULL;
473 OPJ_UINT32 index = 0;
476 comp = &pi->comps[pi->compno];
482 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
484 comp = &pi->comps[pi->compno];
487 for (resno = 0; resno < comp->numresolutions; resno++) {
489 res = &comp->resolutions[resno];
490 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
491 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
492 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
493 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
496 pi->poc.ty0 = pi->ty0;
497 pi->poc.tx0 = pi->tx0;
498 pi->poc.ty1 = pi->ty1;
499 pi->poc.tx1 = pi->tx1;
501 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
502 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
503 for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
505 OPJ_INT32 trx0, try0;
506 OPJ_INT32 trx1, try1;
508 OPJ_INT32 prci, prcj;
509 res = &comp->resolutions[pi->resno];
510 levelno = comp->numresolutions - 1 - pi->resno;
511 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
512 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
513 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
514 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
515 rpx = res->pdx + levelno;
516 rpy = res->pdy + levelno;
517 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
520 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
524 if ((res->pw==0)||(res->ph==0)) continue;
526 if ((trx0==trx1)||(try0==try1)) continue;
528 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
529 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
530 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
531 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
532 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
533 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
534 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
535 if (!pi->include[index]) {
536 pi->include[index] = 1;
549 void opj_get_encoding_parameters( const opj_image_t *p_image,
550 const opj_cp_t *p_cp,
556 OPJ_UINT32 * p_dx_min,
557 OPJ_UINT32 * p_dy_min,
558 OPJ_UINT32 * p_max_prec,
559 OPJ_UINT32 * p_max_res )
562 OPJ_UINT32 compno, resno;
564 const opj_tcp_t *l_tcp = 00;
565 const opj_tccp_t * l_tccp = 00;
566 const opj_image_comp_t * l_img_comp = 00;
568 /* position in x and y of tile */
573 assert(p_image != 00);
574 assert(p_tileno < p_cp->tw * p_cp->th);
576 /* initializations */
577 l_tcp = &p_cp->tcps [p_tileno];
578 l_img_comp = p_image->comps;
579 l_tccp = l_tcp->tccps;
581 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
582 p = p_tileno % p_cp->tw;
583 q = p_tileno / p_cp->tw;
585 /* find extent of tile */
586 *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
587 *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
588 *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
589 *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
591 /* max precision is 0 (can only grow) */
595 /* take the largest value for dx_min and dy_min */
596 *p_dx_min = 0x7fffffff;
597 *p_dy_min = 0x7fffffff;
599 for (compno = 0; compno < p_image->numcomps; ++compno) {
600 /* arithmetic variables to calculate */
601 OPJ_UINT32 l_level_no;
602 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
603 OPJ_INT32 l_px0, l_py0, l_px1, py1;
604 OPJ_UINT32 l_pdx, l_pdy;
605 OPJ_UINT32 l_pw, l_ph;
606 OPJ_UINT32 l_product;
607 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
609 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
610 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
611 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
612 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
614 if (l_tccp->numresolutions > *p_max_res) {
615 *p_max_res = l_tccp->numresolutions;
618 /* use custom size for precincts */
619 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
620 OPJ_UINT32 l_dx, l_dy;
622 /* precinct width and height */
623 l_pdx = l_tccp->prcw[resno];
624 l_pdy = l_tccp->prch[resno];
626 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
627 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
629 /* take the minimum size for dx for each comp and resolution */
630 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
631 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
633 /* various calculations of extents */
634 l_level_no = l_tccp->numresolutions - 1 - resno;
636 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
637 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
638 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
639 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
641 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
642 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
643 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
645 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
647 l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
648 l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
650 l_product = l_pw * l_ph;
652 /* update precision */
653 if (l_product > *p_max_prec) {
654 *p_max_prec = l_product;
663 void opj_get_all_encoding_parameters( const opj_image_t *p_image,
664 const opj_cp_t *p_cp,
670 OPJ_UINT32 * p_dx_min,
671 OPJ_UINT32 * p_dy_min,
672 OPJ_UINT32 * p_max_prec,
673 OPJ_UINT32 * p_max_res,
674 OPJ_UINT32 ** p_resolutions )
677 OPJ_UINT32 compno, resno;
680 const opj_tcp_t *tcp = 00;
681 const opj_tccp_t * l_tccp = 00;
682 const opj_image_comp_t * l_img_comp = 00;
684 /* to store l_dx, l_dy, w and h for each resolution and component.*/
685 OPJ_UINT32 * lResolutionPtr;
687 /* position in x and y of tile*/
690 /* preconditions in debug*/
692 assert(p_image != 00);
693 assert(tileno < p_cp->tw * p_cp->th);
696 tcp = &p_cp->tcps [tileno];
698 l_img_comp = p_image->comps;
700 /* position in x and y of tile*/
701 p = tileno % p_cp->tw;
702 q = tileno / p_cp->tw;
704 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
705 *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
706 *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
707 *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
708 *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
710 /* max precision and resolution is 0 (can only grow)*/
714 /* take the largest value for dx_min and dy_min*/
715 *p_dx_min = 0x7fffffff;
716 *p_dy_min = 0x7fffffff;
718 for (compno = 0; compno < p_image->numcomps; ++compno) {
719 /* aritmetic variables to calculate*/
720 OPJ_UINT32 l_level_no;
721 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
722 OPJ_INT32 l_px0, l_py0, l_px1, py1;
723 OPJ_UINT32 l_product;
724 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
725 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
727 lResolutionPtr = p_resolutions[compno];
729 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
730 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
731 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
732 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
734 if (l_tccp->numresolutions > *p_max_res) {
735 *p_max_res = l_tccp->numresolutions;
738 /* use custom size for precincts*/
739 l_level_no = l_tccp->numresolutions - 1;
740 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
741 OPJ_UINT32 l_dx, l_dy;
743 /* precinct width and height*/
744 l_pdx = l_tccp->prcw[resno];
745 l_pdy = l_tccp->prch[resno];
746 *lResolutionPtr++ = l_pdx;
747 *lResolutionPtr++ = l_pdy;
748 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
749 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
750 /* take the minimum size for l_dx for each comp and resolution*/
751 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dx_min, (OPJ_INT32)l_dx);
752 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dy_min, (OPJ_INT32)l_dy);
754 /* various calculations of extents*/
755 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
756 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
757 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
758 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
759 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
760 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
761 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
762 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
763 l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
764 l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
765 *lResolutionPtr++ = l_pw;
766 *lResolutionPtr++ = l_ph;
767 l_product = l_pw * l_ph;
769 /* update precision*/
770 if (l_product > *p_max_prec) {
771 *p_max_prec = l_product;
781 opj_pi_iterator_t * opj_pi_create( const opj_image_t *image,
786 OPJ_UINT32 pino, compno;
787 /* number of poc in the p_pi*/
788 OPJ_UINT32 l_poc_bound;
790 /* pointers to tile coding parameters and components.*/
791 opj_pi_iterator_t *l_pi = 00;
793 const opj_tccp_t *tccp = 00;
795 /* current packet iterator being allocated*/
796 opj_pi_iterator_t *l_current_pi = 00;
798 /* preconditions in debug*/
801 assert(tileno < cp->tw * cp->th);
804 tcp = &cp->tcps[tileno];
805 l_poc_bound = tcp->numpocs+1;
807 /* memory allocations*/
808 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
812 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
815 for (pino = 0; pino < l_poc_bound ; ++pino) {
817 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
818 if (! l_current_pi->comps) {
819 opj_pi_destroy(l_pi, l_poc_bound);
823 l_current_pi->numcomps = image->numcomps;
824 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
826 for (compno = 0; compno < image->numcomps; ++compno) {
827 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
829 tccp = &tcp->tccps[compno];
831 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
832 if (!comp->resolutions) {
833 opj_pi_destroy(l_pi, l_poc_bound);
837 comp->numresolutions = tccp->numresolutions;
838 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
845 void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
851 OPJ_UINT32 p_max_prec,
852 OPJ_UINT32 p_max_res,
858 /* tile coding parameter*/
859 opj_tcp_t *l_tcp = 00;
860 /* current poc being updated*/
861 opj_poc_t * l_current_poc = 00;
864 OPJ_UINT32 l_poc_bound;
866 OPJ_ARG_NOT_USED(p_max_res);
868 /* preconditions in debug*/
870 assert(p_tileno < p_cp->tw * p_cp->th);
873 l_tcp = &p_cp->tcps [p_tileno];
874 /* number of iterations in the loop */
875 l_poc_bound = l_tcp->numpocs+1;
877 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
878 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
879 l_current_poc = l_tcp->pocs;
881 l_current_poc->compS = l_current_poc->compno0;
882 l_current_poc->compE = l_current_poc->compno1;
883 l_current_poc->resS = l_current_poc->resno0;
884 l_current_poc->resE = l_current_poc->resno1;
885 l_current_poc->layE = l_current_poc->layno1;
887 /* special treatment for the first element*/
888 l_current_poc->layS = 0;
889 l_current_poc->prg = l_current_poc->prg1;
890 l_current_poc->prcS = 0;
892 l_current_poc->prcE = p_max_prec;
893 l_current_poc->txS = (OPJ_UINT32)p_tx0;
894 l_current_poc->txE = (OPJ_UINT32)p_tx1;
895 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
896 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
897 l_current_poc->dx = p_dx_min;
898 l_current_poc->dy = p_dy_min;
901 for (pino = 1;pino < l_poc_bound ; ++pino) {
902 l_current_poc->compS = l_current_poc->compno0;
903 l_current_poc->compE= l_current_poc->compno1;
904 l_current_poc->resS = l_current_poc->resno0;
905 l_current_poc->resE = l_current_poc->resno1;
906 l_current_poc->layE = l_current_poc->layno1;
907 l_current_poc->prg = l_current_poc->prg1;
908 l_current_poc->prcS = 0;
909 /* special treatment here different from the first element*/
910 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
912 l_current_poc->prcE = p_max_prec;
913 l_current_poc->txS = (OPJ_UINT32)p_tx0;
914 l_current_poc->txE = (OPJ_UINT32)p_tx1;
915 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
916 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
917 l_current_poc->dx = p_dx_min;
918 l_current_poc->dy = p_dy_min;
923 void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
924 OPJ_UINT32 p_num_comps,
930 OPJ_UINT32 p_max_prec,
931 OPJ_UINT32 p_max_res,
937 /* tile coding parameter*/
938 opj_tcp_t *l_tcp = 00;
939 /* current poc being updated*/
940 opj_poc_t * l_current_poc = 00;
942 OPJ_UINT32 l_poc_bound;
944 /* preconditions in debug*/
946 assert(p_tileno < p_cp->tw * p_cp->th);
949 l_tcp = &p_cp->tcps [p_tileno];
951 /* number of iterations in the loop */
952 l_poc_bound = l_tcp->numpocs+1;
954 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
955 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
956 l_current_poc = l_tcp->pocs;
958 for (pino = 0; pino < l_poc_bound ; ++pino) {
959 l_current_poc->compS = 0;
960 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
961 l_current_poc->resS = 0;
962 l_current_poc->resE = p_max_res;
963 l_current_poc->layS = 0;
964 l_current_poc->layE = l_tcp->numlayers;
965 l_current_poc->prg = l_tcp->prg;
966 l_current_poc->prcS = 0;
967 l_current_poc->prcE = p_max_prec;
968 l_current_poc->txS = (OPJ_UINT32)p_tx0;
969 l_current_poc->txE = (OPJ_UINT32)p_tx1;
970 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
971 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
972 l_current_poc->dx = p_dx_min;
973 l_current_poc->dy = p_dy_min;
978 void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
980 OPJ_UINT32 p_max_precision,
981 OPJ_UINT32 p_max_res)
986 /* encoding prameters to set*/
989 opj_pi_iterator_t * l_current_pi = 00;
990 opj_poc_t* l_current_poc = 0;
992 OPJ_ARG_NOT_USED(p_max_res);
994 /* preconditions in debug*/
999 l_bound = p_tcp->numpocs+1;
1000 l_current_pi = p_pi;
1001 l_current_poc = p_tcp->pocs;
1003 for (pino = 0;pino<l_bound;++pino) {
1004 l_current_pi->poc.prg = l_current_poc->prg;
1005 l_current_pi->first = 1;
1007 l_current_pi->poc.resno0 = l_current_poc->resno0;
1008 l_current_pi->poc.compno0 = l_current_poc->compno0;
1009 l_current_pi->poc.layno0 = 0;
1010 l_current_pi->poc.precno0 = 0;
1011 l_current_pi->poc.resno1 = l_current_poc->resno1;
1012 l_current_pi->poc.compno1 = l_current_poc->compno1;
1013 l_current_pi->poc.layno1 = l_current_poc->layno1;
1014 l_current_pi->poc.precno1 = p_max_precision;
1020 void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
1022 OPJ_UINT32 p_max_precision,
1023 OPJ_UINT32 p_max_res)
1028 /* encoding prameters to set*/
1031 opj_pi_iterator_t * l_current_pi = 00;
1032 /* preconditions in debug*/
1033 assert(p_tcp != 00);
1036 /* initializations*/
1037 l_bound = p_tcp->numpocs+1;
1038 l_current_pi = p_pi;
1040 for (pino = 0;pino<l_bound;++pino) {
1041 l_current_pi->poc.prg = p_tcp->prg;
1042 l_current_pi->first = 1;
1043 l_current_pi->poc.resno0 = 0;
1044 l_current_pi->poc.compno0 = 0;
1045 l_current_pi->poc.layno0 = 0;
1046 l_current_pi->poc.precno0 = 0;
1047 l_current_pi->poc.resno1 = p_max_res;
1048 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1049 l_current_pi->poc.layno1 = p_tcp->numlayers;
1050 l_current_pi->poc.precno1 = p_max_precision;
1057 OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
1061 const OPJ_CHAR *prog)
1064 opj_tcp_t *tcps =&cp->tcps[tileno];
1065 opj_poc_t *tcp = &tcps->pocs[pino];
1068 for(i=pos;pos>=0;i--){
1071 if(tcp->res_t==tcp->resE){
1072 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1082 if(tcp->comp_t==tcp->compE){
1083 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1093 if(tcp->lay_t==tcp->layE){
1094 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1105 case OPJ_LRCP||OPJ_RLCP:
1106 if(tcp->prc_t == tcp->prcE){
1107 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1117 if(tcp->tx0_t == tcp->txE){
1119 if(tcp->ty0_t == tcp->tyE){
1120 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1141 ==========================================================
1142 Packet iterator interface
1143 ==========================================================
1145 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1147 OPJ_UINT32 p_tile_no)
1151 OPJ_UINT32 compno, resno;
1153 /* to store w, h, dx and dy fro all components and resolutions */
1154 OPJ_UINT32 * l_tmp_data;
1155 OPJ_UINT32 ** l_tmp_ptr;
1157 /* encoding prameters to set */
1158 OPJ_UINT32 l_max_res;
1159 OPJ_UINT32 l_max_prec;
1160 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1161 OPJ_UINT32 l_dx_min,l_dy_min;
1163 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1164 OPJ_UINT32 l_data_stride;
1167 opj_pi_iterator_t *l_pi = 00;
1168 opj_tcp_t *l_tcp = 00;
1169 const opj_tccp_t *l_tccp = 00;
1170 opj_pi_comp_t *l_current_comp = 00;
1171 opj_image_comp_t * l_img_comp = 00;
1172 opj_pi_iterator_t * l_current_pi = 00;
1173 OPJ_UINT32 * l_encoding_value_ptr = 00;
1175 /* preconditions in debug */
1177 assert(p_image != 00);
1178 assert(p_tile_no < p_cp->tw * p_cp->th);
1180 /* initializations */
1181 l_tcp = &p_cp->tcps[p_tile_no];
1182 l_bound = l_tcp->numpocs+1;
1184 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1185 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1186 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1192 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1193 p_image->numcomps * sizeof(OPJ_UINT32 *));
1197 opj_free(l_tmp_data);
1201 /* memory allocation for pi */
1202 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1204 opj_free(l_tmp_data);
1205 opj_free(l_tmp_ptr);
1209 l_encoding_value_ptr = l_tmp_data;
1210 /* update pointer array */
1212 (compno = 0; compno < p_image->numcomps; ++compno)
1214 l_tmp_ptr[compno] = l_encoding_value_ptr;
1215 l_encoding_value_ptr += l_data_stride;
1217 /* get encoding parameters */
1218 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);
1220 /* step calculations */
1222 l_step_c = l_max_prec * l_step_p;
1223 l_step_r = p_image->numcomps * l_step_c;
1224 l_step_l = l_max_res * l_step_r;
1226 /* set values for first packet iterator */
1227 l_current_pi = l_pi;
1229 /* memory allocation for include */
1230 l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
1232 (!l_current_pi->include)
1234 opj_free(l_tmp_data);
1235 opj_free(l_tmp_ptr);
1236 opj_pi_destroy(l_pi, l_bound);
1239 memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
1241 /* special treatment for the first packet iterator */
1242 l_current_comp = l_current_pi->comps;
1243 l_img_comp = p_image->comps;
1244 l_tccp = l_tcp->tccps;
1246 l_current_pi->tx0 = l_tx0;
1247 l_current_pi->ty0 = l_ty0;
1248 l_current_pi->tx1 = l_tx1;
1249 l_current_pi->ty1 = l_ty1;
1251 /*l_current_pi->dx = l_img_comp->dx;*/
1252 /*l_current_pi->dy = l_img_comp->dy;*/
1254 l_current_pi->step_p = l_step_p;
1255 l_current_pi->step_c = l_step_c;
1256 l_current_pi->step_r = l_step_r;
1257 l_current_pi->step_l = l_step_l;
1259 /* allocation for components and number of components has already been calculated by opj_pi_create */
1261 (compno = 0; compno < l_current_pi->numcomps; ++compno)
1263 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1264 l_encoding_value_ptr = l_tmp_ptr[compno];
1266 l_current_comp->dx = l_img_comp->dx;
1267 l_current_comp->dy = l_img_comp->dy;
1268 /* resolutions have already been initialized */
1270 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1272 l_res->pdx = *(l_encoding_value_ptr++);
1273 l_res->pdy = *(l_encoding_value_ptr++);
1274 l_res->pw = *(l_encoding_value_ptr++);
1275 l_res->ph = *(l_encoding_value_ptr++);
1284 for (pino = 1 ; pino<l_bound ; ++pino )
1286 l_current_comp = l_current_pi->comps;
1287 l_img_comp = p_image->comps;
1288 l_tccp = l_tcp->tccps;
1290 l_current_pi->tx0 = l_tx0;
1291 l_current_pi->ty0 = l_ty0;
1292 l_current_pi->tx1 = l_tx1;
1293 l_current_pi->ty1 = l_ty1;
1294 /*l_current_pi->dx = l_dx_min;*/
1295 /*l_current_pi->dy = l_dy_min;*/
1296 l_current_pi->step_p = l_step_p;
1297 l_current_pi->step_c = l_step_c;
1298 l_current_pi->step_r = l_step_r;
1299 l_current_pi->step_l = l_step_l;
1301 /* allocation for components and number of components has already been calculated by opj_pi_create */
1303 (compno = 0; compno < l_current_pi->numcomps; ++compno)
1305 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1306 l_encoding_value_ptr = l_tmp_ptr[compno];
1308 l_current_comp->dx = l_img_comp->dx;
1309 l_current_comp->dy = l_img_comp->dy;
1310 /* resolutions have already been initialized */
1312 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1314 l_res->pdx = *(l_encoding_value_ptr++);
1315 l_res->pdy = *(l_encoding_value_ptr++);
1316 l_res->pw = *(l_encoding_value_ptr++);
1317 l_res->ph = *(l_encoding_value_ptr++);
1324 /* special treatment*/
1325 l_current_pi->include = (l_current_pi-1)->include;
1328 opj_free(l_tmp_data);
1330 opj_free(l_tmp_ptr);
1335 opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
1339 opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
1346 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1348 OPJ_UINT32 p_tile_no,
1349 J2K_T2_MODE p_t2_mode )
1353 OPJ_UINT32 compno, resno;
1355 /* to store w, h, dx and dy fro all components and resolutions*/
1356 OPJ_UINT32 * l_tmp_data;
1357 OPJ_UINT32 ** l_tmp_ptr;
1359 /* encoding prameters to set*/
1360 OPJ_UINT32 l_max_res;
1361 OPJ_UINT32 l_max_prec;
1362 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1363 OPJ_UINT32 l_dx_min,l_dy_min;
1365 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1366 OPJ_UINT32 l_data_stride;
1369 opj_pi_iterator_t *l_pi = 00;
1370 opj_tcp_t *l_tcp = 00;
1371 const opj_tccp_t *l_tccp = 00;
1372 opj_pi_comp_t *l_current_comp = 00;
1373 opj_image_comp_t * l_img_comp = 00;
1374 opj_pi_iterator_t * l_current_pi = 00;
1375 OPJ_UINT32 * l_encoding_value_ptr = 00;
1377 /* preconditions in debug*/
1379 assert(p_image != 00);
1380 assert(p_tile_no < p_cp->tw * p_cp->th);
1382 /* initializations*/
1383 l_tcp = &p_cp->tcps[p_tile_no];
1384 l_bound = l_tcp->numpocs+1;
1386 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1387 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1388 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1393 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1394 p_image->numcomps * sizeof(OPJ_UINT32 *));
1396 opj_free(l_tmp_data);
1400 /* memory allocation for pi*/
1401 l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
1403 opj_free(l_tmp_data);
1404 opj_free(l_tmp_ptr);
1408 l_encoding_value_ptr = l_tmp_data;
1409 /* update pointer array*/
1410 for (compno = 0; compno < p_image->numcomps; ++compno) {
1411 l_tmp_ptr[compno] = l_encoding_value_ptr;
1412 l_encoding_value_ptr += l_data_stride;
1415 /* get encoding parameters*/
1416 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);
1418 /* step calculations*/
1420 l_step_c = l_max_prec * l_step_p;
1421 l_step_r = p_image->numcomps * l_step_c;
1422 l_step_l = l_max_res * l_step_r;
1424 /* set values for first packet iterator*/
1425 l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1426 l_current_pi = l_pi;
1428 /* memory allocation for include*/
1429 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1430 if (!l_current_pi->include) {
1431 opj_free(l_tmp_data);
1432 opj_free(l_tmp_ptr);
1433 opj_pi_destroy(l_pi, l_bound);
1436 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
1438 /* special treatment for the first packet iterator*/
1439 l_current_comp = l_current_pi->comps;
1440 l_img_comp = p_image->comps;
1441 l_tccp = l_tcp->tccps;
1442 l_current_pi->tx0 = l_tx0;
1443 l_current_pi->ty0 = l_ty0;
1444 l_current_pi->tx1 = l_tx1;
1445 l_current_pi->ty1 = l_ty1;
1446 l_current_pi->dx = l_dx_min;
1447 l_current_pi->dy = l_dy_min;
1448 l_current_pi->step_p = l_step_p;
1449 l_current_pi->step_c = l_step_c;
1450 l_current_pi->step_r = l_step_r;
1451 l_current_pi->step_l = l_step_l;
1453 /* allocation for components and number of components has already been calculated by opj_pi_create */
1454 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1455 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1456 l_encoding_value_ptr = l_tmp_ptr[compno];
1458 l_current_comp->dx = l_img_comp->dx;
1459 l_current_comp->dy = l_img_comp->dy;
1461 /* resolutions have already been initialized */
1462 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1463 l_res->pdx = *(l_encoding_value_ptr++);
1464 l_res->pdy = *(l_encoding_value_ptr++);
1465 l_res->pw = *(l_encoding_value_ptr++);
1466 l_res->ph = *(l_encoding_value_ptr++);
1476 for (pino = 1 ; pino<l_bound ; ++pino ) {
1477 l_current_comp = l_current_pi->comps;
1478 l_img_comp = p_image->comps;
1479 l_tccp = l_tcp->tccps;
1481 l_current_pi->tx0 = l_tx0;
1482 l_current_pi->ty0 = l_ty0;
1483 l_current_pi->tx1 = l_tx1;
1484 l_current_pi->ty1 = l_ty1;
1485 l_current_pi->dx = l_dx_min;
1486 l_current_pi->dy = l_dy_min;
1487 l_current_pi->step_p = l_step_p;
1488 l_current_pi->step_c = l_step_c;
1489 l_current_pi->step_r = l_step_r;
1490 l_current_pi->step_l = l_step_l;
1492 /* allocation for components and number of components has already been calculated by opj_pi_create */
1493 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1494 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1495 l_encoding_value_ptr = l_tmp_ptr[compno];
1497 l_current_comp->dx = l_img_comp->dx;
1498 l_current_comp->dy = l_img_comp->dy;
1499 /* resolutions have already been initialized */
1500 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1501 l_res->pdx = *(l_encoding_value_ptr++);
1502 l_res->pdy = *(l_encoding_value_ptr++);
1503 l_res->pw = *(l_encoding_value_ptr++);
1504 l_res->ph = *(l_encoding_value_ptr++);
1512 /* special treatment*/
1513 l_current_pi->include = (l_current_pi-1)->include;
1517 opj_free(l_tmp_data);
1519 opj_free(l_tmp_ptr);
1522 if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
1523 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);
1526 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);
1532 void opj_pi_create_encode( opj_pi_iterator_t *pi,
1538 J2K_T2_MODE t2_mode)
1540 const OPJ_CHAR *prog;
1542 OPJ_UINT32 incr_top=1,resetX=0;
1543 opj_tcp_t *tcps =&cp->tcps[tileno];
1544 opj_poc_t *tcp= &tcps->pocs[pino];
1546 prog = opj_j2k_convert_progression_order(tcp->prg);
1549 pi[pino].poc.prg = tcp->prg;
1551 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))){
1552 pi[pino].poc.resno0 = tcp->resS;
1553 pi[pino].poc.resno1 = tcp->resE;
1554 pi[pino].poc.compno0 = tcp->compS;
1555 pi[pino].poc.compno1 = tcp->compE;
1556 pi[pino].poc.layno0 = tcp->layS;
1557 pi[pino].poc.layno1 = tcp->layE;
1558 pi[pino].poc.precno0 = tcp->prcS;
1559 pi[pino].poc.precno1 = tcp->prcE;
1560 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1561 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1562 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1563 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1565 for(i=tppos+1;i<4;i++){
1568 pi[pino].poc.resno0 = tcp->resS;
1569 pi[pino].poc.resno1 = tcp->resE;
1572 pi[pino].poc.compno0 = tcp->compS;
1573 pi[pino].poc.compno1 = tcp->compE;
1576 pi[pino].poc.layno0 = tcp->layS;
1577 pi[pino].poc.layno1 = tcp->layE;
1583 pi[pino].poc.precno0 = tcp->prcS;
1584 pi[pino].poc.precno1 = tcp->prcE;
1587 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1588 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1589 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1590 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1598 for(i=tppos;i>=0;i--){
1601 tcp->comp_t = tcp->compS;
1602 pi[pino].poc.compno0 = tcp->comp_t;
1603 pi[pino].poc.compno1 = tcp->comp_t+1;
1607 tcp->res_t = tcp->resS;
1608 pi[pino].poc.resno0 = tcp->res_t;
1609 pi[pino].poc.resno1 = tcp->res_t+1;
1613 tcp->lay_t = tcp->layS;
1614 pi[pino].poc.layno0 = tcp->lay_t;
1615 pi[pino].poc.layno1 = tcp->lay_t+1;
1622 tcp->prc_t = tcp->prcS;
1623 pi[pino].poc.precno0 = tcp->prc_t;
1624 pi[pino].poc.precno1 = tcp->prc_t+1;
1628 tcp->tx0_t = tcp->txS;
1629 tcp->ty0_t = tcp->tyS;
1630 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1631 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1632 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1633 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1634 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1635 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1643 for(i=tppos;i>=0;i--){
1646 pi[pino].poc.compno0 = tcp->comp_t-1;
1647 pi[pino].poc.compno1 = tcp->comp_t;
1650 pi[pino].poc.resno0 = tcp->res_t-1;
1651 pi[pino].poc.resno1 = tcp->res_t;
1654 pi[pino].poc.layno0 = tcp->lay_t-1;
1655 pi[pino].poc.layno1 = tcp->lay_t;
1661 pi[pino].poc.precno0 = tcp->prc_t-1;
1662 pi[pino].poc.precno1 = tcp->prc_t;
1665 pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1666 pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1667 pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1668 pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1676 if(tcp->res_t==tcp->resE){
1677 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1678 tcp->res_t = tcp->resS;
1679 pi[pino].poc.resno0 = tcp->res_t;
1680 pi[pino].poc.resno1 = tcp->res_t+1;
1687 pi[pino].poc.resno0 = tcp->res_t;
1688 pi[pino].poc.resno1 = tcp->res_t+1;
1694 if(tcp->comp_t ==tcp->compE){
1695 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1696 tcp->comp_t = tcp->compS;
1697 pi[pino].poc.compno0 = tcp->comp_t;
1698 pi[pino].poc.compno1 = tcp->comp_t+1;
1705 pi[pino].poc.compno0 = tcp->comp_t;
1706 pi[pino].poc.compno1 = tcp->comp_t+1;
1712 if(tcp->lay_t == tcp->layE){
1713 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1714 tcp->lay_t = tcp->layS;
1715 pi[pino].poc.layno0 = tcp->lay_t;
1716 pi[pino].poc.layno1 = tcp->lay_t+1;
1723 pi[pino].poc.layno0 = tcp->lay_t;
1724 pi[pino].poc.layno1 = tcp->lay_t+1;
1733 if(tcp->prc_t == tcp->prcE){
1734 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1735 tcp->prc_t = tcp->prcS;
1736 pi[pino].poc.precno0 = tcp->prc_t;
1737 pi[pino].poc.precno1 = tcp->prc_t+1;
1744 pi[pino].poc.precno0 = tcp->prc_t;
1745 pi[pino].poc.precno1 = tcp->prc_t+1;
1751 if(tcp->tx0_t >= tcp->txE){
1752 if(tcp->ty0_t >= tcp->tyE){
1753 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1754 tcp->ty0_t = tcp->tyS;
1755 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1756 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1757 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1758 incr_top=1;resetX=1;
1760 incr_top=0;resetX=0;
1763 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1764 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1765 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1766 incr_top=0;resetX=1;
1769 tcp->tx0_t = tcp->txS;
1770 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1771 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
1772 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1775 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1776 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
1777 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1790 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1791 OPJ_UINT32 p_nb_elements)
1793 OPJ_UINT32 compno, pino;
1794 opj_pi_iterator_t *l_current_pi = p_pi;
1796 if (p_pi->include) {
1797 opj_free(p_pi->include);
1800 for (pino = 0; pino < p_nb_elements; ++pino){
1801 if(l_current_pi->comps) {
1802 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1803 for (compno = 0; compno < l_current_pi->numcomps; compno++){
1804 if(l_current_component->resolutions) {
1805 opj_free(l_current_component->resolutions);
1806 l_current_component->resolutions = 00;
1809 ++l_current_component;
1811 opj_free(l_current_pi->comps);
1812 l_current_pi->comps = 0;
1822 void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
1824 OPJ_UINT32 p_tile_no )
1826 /* encoding parameters to set */
1827 OPJ_UINT32 l_max_res;
1828 OPJ_UINT32 l_max_prec;
1829 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1830 OPJ_UINT32 l_dx_min,l_dy_min;
1833 opj_tcp_t *l_tcp = 00;
1837 assert(p_image != 00);
1838 assert(p_tile_no < p_cp->tw * p_cp->th);
1840 l_tcp = &(p_cp->tcps[p_tile_no]);
1842 /* get encoding parameters */
1843 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);
1846 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);
1849 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);
1853 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi) {
1854 switch (pi->poc.prg) {
1856 return opj_pi_next_lrcp(pi);
1858 return opj_pi_next_rlcp(pi);
1860 return opj_pi_next_rpcl(pi);
1862 return opj_pi_next_pcrl(pi);
1864 return opj_pi_next_cprl(pi);
1865 case OPJ_PROG_UNKNOWN: