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_v2_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_v2_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_v2_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_v2_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,
190 const opj_cp_v2_t *cp,
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 : int_min(pi->dx, dx);
325 pi->dy = !pi->dy ? dy : 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 = int_ceildiv(pi->tx0, comp->dx << levelno);
351 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
352 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
353 try1 = 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 = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
368 - int_floordivpow2(trx0, res->pdx);
369 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
370 - 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 : int_min(pi->dx, dx);
409 pi->dy = !pi->dy ? dy : 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 < 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 = int_ceildiv(pi->tx0, comp->dx << levelno);
432 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
433 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
434 try1 = 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 = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
449 - int_floordivpow2(trx0, res->pdx);
450 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
451 - 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 : int_min(pi->dx, dx);
492 pi->dy = !pi->dy ? dy : 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 < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
508 res = &comp->resolutions[pi->resno];
509 levelno = comp->numresolutions - 1 - pi->resno;
510 trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
511 try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
512 trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
513 try1 = 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 = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
528 - int_floordivpow2(trx0, res->pdx);
529 prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
530 - 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;
549 ==========================================================
550 Packet iterator interface
551 ==========================================================
553 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
555 OPJ_UINT32 p_tile_no)
559 OPJ_UINT32 compno, resno;
561 /* to store w, h, dx and dy fro all components and resolutions */
562 OPJ_UINT32 * l_tmp_data;
563 OPJ_UINT32 ** l_tmp_ptr;
565 /* encoding prameters to set */
566 OPJ_UINT32 l_max_res;
567 OPJ_UINT32 l_max_prec;
568 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
569 OPJ_UINT32 l_dx_min,l_dy_min;
571 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
572 OPJ_UINT32 l_data_stride;
575 opj_pi_iterator_t *l_pi = 00;
576 opj_tcp_v2_t *l_tcp = 00;
577 const opj_tccp_t *l_tccp = 00;
578 opj_pi_comp_t *l_current_comp = 00;
579 opj_image_comp_t * l_img_comp = 00;
580 opj_pi_iterator_t * l_current_pi = 00;
581 OPJ_UINT32 * l_encoding_value_ptr = 00;
583 /* preconditions in debug */
585 assert(p_image != 00);
586 assert(p_tile_no < p_cp->tw * p_cp->th);
588 /* initializations */
589 l_tcp = &p_cp->tcps[p_tile_no];
590 l_bound = l_tcp->numpocs+1;
592 l_data_stride = 4 * J2K_MAXRLVLS;
593 l_tmp_data = (OPJ_UINT32*)opj_malloc(
594 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
600 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
601 p_image->numcomps * sizeof(OPJ_UINT32 *));
605 opj_free(l_tmp_data);
609 /* memory allocation for pi */
610 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
612 opj_free(l_tmp_data);
617 l_encoding_value_ptr = l_tmp_data;
618 /* update pointer array */
620 (compno = 0; compno < p_image->numcomps; ++compno)
622 l_tmp_ptr[compno] = l_encoding_value_ptr;
623 l_encoding_value_ptr += l_data_stride;
625 /* get encoding parameters */
626 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);
628 /* step calculations */
630 l_step_c = l_max_prec * l_step_p;
631 l_step_r = p_image->numcomps * l_step_c;
632 l_step_l = l_max_res * l_step_r;
634 /* set values for first packet iterator */
637 /* memory allocation for include */
638 l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
640 (!l_current_pi->include)
642 opj_free(l_tmp_data);
644 opj_pi_destroy(l_pi, l_bound);
647 memset(l_current_pi->include,0, (l_tcp->numlayers + 1) * l_step_l* sizeof(OPJ_INT16));
649 /* special treatment for the first packet iterator */
650 l_current_comp = l_current_pi->comps;
651 l_img_comp = p_image->comps;
652 l_tccp = l_tcp->tccps;
654 l_current_pi->tx0 = l_tx0;
655 l_current_pi->ty0 = l_ty0;
656 l_current_pi->tx1 = l_tx1;
657 l_current_pi->ty1 = l_ty1;
659 /*l_current_pi->dx = l_img_comp->dx;*/
660 /*l_current_pi->dy = l_img_comp->dy;*/
662 l_current_pi->step_p = l_step_p;
663 l_current_pi->step_c = l_step_c;
664 l_current_pi->step_r = l_step_r;
665 l_current_pi->step_l = l_step_l;
667 /* allocation for components and number of components has already been calculated by opj_pi_create */
669 (compno = 0; compno < l_current_pi->numcomps; ++compno)
671 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
672 l_encoding_value_ptr = l_tmp_ptr[compno];
674 l_current_comp->dx = l_img_comp->dx;
675 l_current_comp->dy = l_img_comp->dy;
676 /* resolutions have already been initialized */
678 (resno = 0; resno < l_current_comp->numresolutions; resno++)
680 l_res->pdx = *(l_encoding_value_ptr++);
681 l_res->pdy = *(l_encoding_value_ptr++);
682 l_res->pw = *(l_encoding_value_ptr++);
683 l_res->ph = *(l_encoding_value_ptr++);
693 (pino = 1 ; pino<l_bound ; ++pino )
695 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
696 opj_image_comp_t * l_img_comp = p_image->comps;
697 l_tccp = l_tcp->tccps;
699 l_current_pi->tx0 = l_tx0;
700 l_current_pi->ty0 = l_ty0;
701 l_current_pi->tx1 = l_tx1;
702 l_current_pi->ty1 = l_ty1;
703 /*l_current_pi->dx = l_dx_min;*/
704 /*l_current_pi->dy = l_dy_min;*/
705 l_current_pi->step_p = l_step_p;
706 l_current_pi->step_c = l_step_c;
707 l_current_pi->step_r = l_step_r;
708 l_current_pi->step_l = l_step_l;
710 /* allocation for components and number of components has already been calculated by opj_pi_create */
712 (compno = 0; compno < l_current_pi->numcomps; ++compno)
714 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
715 l_encoding_value_ptr = l_tmp_ptr[compno];
717 l_current_comp->dx = l_img_comp->dx;
718 l_current_comp->dy = l_img_comp->dy;
719 /* resolutions have already been initialized */
721 (resno = 0; resno < l_current_comp->numresolutions; resno++)
723 l_res->pdx = *(l_encoding_value_ptr++);
724 l_res->pdy = *(l_encoding_value_ptr++);
725 l_res->pw = *(l_encoding_value_ptr++);
726 l_res->ph = *(l_encoding_value_ptr++);
733 /* special treatment*/
734 l_current_pi->include = (l_current_pi-1)->include;
737 opj_free(l_tmp_data);
744 opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
748 opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
755 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
757 OPJ_UINT32 p_tile_no,
758 J2K_T2_MODE p_t2_mode )
762 OPJ_UINT32 compno, resno;
764 /* to store w, h, dx and dy fro all components and resolutions*/
765 OPJ_UINT32 * l_tmp_data;
766 OPJ_UINT32 ** l_tmp_ptr;
768 /* encoding prameters to set*/
769 OPJ_UINT32 l_max_res;
770 OPJ_UINT32 l_max_prec;
771 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
772 OPJ_UINT32 l_dx_min,l_dy_min;
774 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
775 OPJ_UINT32 l_data_stride;
778 opj_pi_iterator_t *l_pi = 00;
779 opj_tcp_v2_t *l_tcp = 00;
780 const opj_tccp_t *l_tccp = 00;
781 opj_pi_comp_t *l_current_comp = 00;
782 opj_image_comp_t * l_img_comp = 00;
783 opj_pi_iterator_t * l_current_pi = 00;
784 OPJ_UINT32 * l_encoding_value_ptr = 00;
786 /* preconditions in debug*/
788 assert(p_image != 00);
789 assert(p_tile_no < p_cp->tw * p_cp->th);
792 l_tcp = &p_cp->tcps[p_tile_no];
793 l_bound = l_tcp->numpocs+1;
795 l_data_stride = 4 * J2K_MAXRLVLS;
796 l_tmp_data = (OPJ_UINT32*)opj_malloc(
797 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
802 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
803 p_image->numcomps * sizeof(OPJ_UINT32 *));
805 opj_free(l_tmp_data);
809 /* memory allocation for pi*/
810 l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
812 opj_free(l_tmp_data);
817 l_encoding_value_ptr = l_tmp_data;
818 /* update pointer array*/
819 for (compno = 0; compno < p_image->numcomps; ++compno) {
820 l_tmp_ptr[compno] = l_encoding_value_ptr;
821 l_encoding_value_ptr += l_data_stride;
824 /* get encoding parameters*/
825 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);
827 /* step calculations*/
829 l_step_c = l_max_prec * l_step_p;
830 l_step_r = p_image->numcomps * l_step_c;
831 l_step_l = l_max_res * l_step_r;
833 /* set values for first packet iterator*/
834 l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
837 /* memory allocation for include*/
838 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
839 if (!l_current_pi->include) {
840 opj_free(l_tmp_data);
842 opj_pi_destroy(l_pi, l_bound);
845 memset(l_current_pi->include,0,l_tcp->numlayers * l_step_l* sizeof(OPJ_INT16));
847 /* special treatment for the first packet iterator*/
848 l_current_comp = l_current_pi->comps;
849 l_img_comp = p_image->comps;
850 l_tccp = l_tcp->tccps;
851 l_current_pi->tx0 = l_tx0;
852 l_current_pi->ty0 = l_ty0;
853 l_current_pi->tx1 = l_tx1;
854 l_current_pi->ty1 = l_ty1;
855 l_current_pi->dx = l_dx_min;
856 l_current_pi->dy = l_dy_min;
857 l_current_pi->step_p = l_step_p;
858 l_current_pi->step_c = l_step_c;
859 l_current_pi->step_r = l_step_r;
860 l_current_pi->step_l = l_step_l;
862 /* allocation for components and number of components has already been calculated by opj_pi_create */
863 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
864 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
865 l_encoding_value_ptr = l_tmp_ptr[compno];
867 l_current_comp->dx = l_img_comp->dx;
868 l_current_comp->dy = l_img_comp->dy;
870 /* resolutions have already been initialized */
871 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
872 l_res->pdx = *(l_encoding_value_ptr++);
873 l_res->pdy = *(l_encoding_value_ptr++);
874 l_res->pw = *(l_encoding_value_ptr++);
875 l_res->ph = *(l_encoding_value_ptr++);
885 for (pino = 1 ; pino<l_bound ; ++pino ) {
886 opj_pi_comp_t *l_current_comp = l_current_pi->comps;
887 opj_image_comp_t * l_img_comp = p_image->comps;
888 l_tccp = l_tcp->tccps;
890 l_current_pi->tx0 = l_tx0;
891 l_current_pi->ty0 = l_ty0;
892 l_current_pi->tx1 = l_tx1;
893 l_current_pi->ty1 = l_ty1;
894 l_current_pi->dx = l_dx_min;
895 l_current_pi->dy = l_dy_min;
896 l_current_pi->step_p = l_step_p;
897 l_current_pi->step_c = l_step_c;
898 l_current_pi->step_r = l_step_r;
899 l_current_pi->step_l = l_step_l;
901 /* allocation for components and number of components has already been calculated by opj_pi_create */
902 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
903 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
904 l_encoding_value_ptr = l_tmp_ptr[compno];
906 l_current_comp->dx = l_img_comp->dx;
907 l_current_comp->dy = l_img_comp->dy;
908 /* resolutions have already been initialized */
909 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
910 l_res->pdx = *(l_encoding_value_ptr++);
911 l_res->pdy = *(l_encoding_value_ptr++);
912 l_res->pw = *(l_encoding_value_ptr++);
913 l_res->ph = *(l_encoding_value_ptr++);
921 /* special treatment*/
922 l_current_pi->include = (l_current_pi-1)->include;
926 opj_free(l_tmp_data);
931 if (l_tcp->POC && ( p_cp->m_specific_param.m_enc.m_cinema || p_t2_mode == FINAL_PASS)) {
932 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);
935 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);
941 opj_bool opj_pi_next(opj_pi_iterator_t * pi) {
942 switch (pi->poc.prg) {
944 return opj_pi_next_lrcp(pi);
946 return opj_pi_next_rlcp(pi);
948 return opj_pi_next_rpcl(pi);
950 return opj_pi_next_pcrl(pi);
952 return opj_pi_next_cprl(pi);
962 void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
964 OPJ_UINT32 p_tile_no )
966 /* encoding parameters to set */
967 OPJ_UINT32 l_max_res;
968 OPJ_UINT32 l_max_prec;
969 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
970 OPJ_UINT32 l_dx_min,l_dy_min;
973 opj_tcp_v2_t *l_tcp = 00;
977 assert(p_image != 00);
978 assert(p_tile_no < p_cp->tw * p_cp->th);
980 l_tcp = &(p_cp->tcps[p_tile_no]);
982 /* get encoding parameters */
983 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);
986 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);
989 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);
994 void opj_get_encoding_parameters( const opj_image_t *p_image,
995 const opj_cp_v2_t *p_cp,
1001 OPJ_UINT32 * p_dx_min,
1002 OPJ_UINT32 * p_dy_min,
1003 OPJ_UINT32 * p_max_prec,
1004 OPJ_UINT32 * p_max_res )
1007 OPJ_UINT32 compno, resno;
1009 const opj_tcp_v2_t *l_tcp = 00;
1010 const opj_tccp_t * l_tccp = 00;
1011 const opj_image_comp_t * l_img_comp = 00;
1013 /* position in x and y of tile */
1018 assert(p_image != 00);
1019 assert(p_tileno < p_cp->tw * p_cp->th);
1021 /* initializations */
1022 l_tcp = &p_cp->tcps [p_tileno];
1023 l_img_comp = p_image->comps;
1024 l_tccp = l_tcp->tccps;
1026 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
1027 p = p_tileno % p_cp->tw;
1028 q = p_tileno / p_cp->tw;
1030 /* find extent of tile */
1031 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1032 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1033 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1034 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1036 /* max precision is 0 (can only grow) */
1040 /* take the largest value for dx_min and dy_min */
1041 *p_dx_min = 0x7fffffff;
1042 *p_dy_min = 0x7fffffff;
1044 for (compno = 0; compno < p_image->numcomps; ++compno) {
1045 /* arithmetic variables to calculate */
1046 OPJ_UINT32 l_level_no;
1047 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1048 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1049 OPJ_UINT32 l_pdx, l_pdy;
1050 OPJ_UINT32 l_pw, l_ph;
1051 OPJ_UINT32 l_product;
1052 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1054 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1055 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1056 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1057 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1059 if (l_tccp->numresolutions > *p_max_res) {
1060 *p_max_res = l_tccp->numresolutions;
1063 /* use custom size for precincts */
1064 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1065 OPJ_UINT32 l_dx, l_dy;
1067 /* precinct width and height */
1068 l_pdx = l_tccp->prcw[resno];
1069 l_pdy = l_tccp->prch[resno];
1071 l_dx = l_img_comp->dx * (1 << (l_pdx + l_tccp->numresolutions - 1 - resno));
1072 l_dy = l_img_comp->dy * (1 << (l_pdy + l_tccp->numresolutions - 1 - resno));
1074 /* take the minimum size for dx for each comp and resolution */
1075 *p_dx_min = uint_min(*p_dx_min, l_dx);
1076 *p_dy_min = uint_min(*p_dy_min, l_dy);
1078 /* various calculations of extents */
1079 l_level_no = l_tccp->numresolutions - 1 - resno;
1081 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1082 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1083 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1084 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1086 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1087 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1088 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1090 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1092 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1093 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1095 l_product = l_pw * l_ph;
1097 /* update precision */
1098 if (l_product > *p_max_prec) {
1099 *p_max_prec = l_product;
1108 void opj_get_all_encoding_parameters( const opj_image_t *p_image,
1109 const opj_cp_v2_t *p_cp,
1115 OPJ_UINT32 * p_dx_min,
1116 OPJ_UINT32 * p_dy_min,
1117 OPJ_UINT32 * p_max_prec,
1118 OPJ_UINT32 * p_max_res,
1119 OPJ_UINT32 ** p_resolutions )
1122 OPJ_UINT32 compno, resno;
1125 const opj_tcp_v2_t *tcp = 00;
1126 const opj_tccp_t * l_tccp = 00;
1127 const opj_image_comp_t * l_img_comp = 00;
1129 /* to store l_dx, l_dy, w and h for each resolution and component.*/
1130 OPJ_UINT32 * lResolutionPtr;
1132 /* position in x and y of tile*/
1135 /* preconditions in debug*/
1137 assert(p_image != 00);
1138 assert(tileno < p_cp->tw * p_cp->th);
1140 /* initializations*/
1141 tcp = &p_cp->tcps [tileno];
1142 l_tccp = tcp->tccps;
1143 l_img_comp = p_image->comps;
1145 /* position in x and y of tile*/
1146 p = tileno % p_cp->tw;
1147 q = tileno / p_cp->tw;
1149 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
1150 *p_tx0 = int_max(p_cp->tx0 + p * p_cp->tdx, p_image->x0);
1151 *p_tx1 = int_min(p_cp->tx0 + (p + 1) * p_cp->tdx, p_image->x1);
1152 *p_ty0 = int_max(p_cp->ty0 + q * p_cp->tdy, p_image->y0);
1153 *p_ty1 = int_min(p_cp->ty0 + (q + 1) * p_cp->tdy, p_image->y1);
1155 /* max precision and resolution is 0 (can only grow)*/
1159 /* take the largest value for dx_min and dy_min*/
1160 *p_dx_min = 0x7fffffff;
1161 *p_dy_min = 0x7fffffff;
1163 for (compno = 0; compno < p_image->numcomps; ++compno) {
1164 /* aritmetic variables to calculate*/
1165 OPJ_UINT32 l_level_no;
1166 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
1167 OPJ_INT32 l_px0, l_py0, l_px1, py1;
1168 OPJ_UINT32 l_product;
1169 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
1170 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
1172 lResolutionPtr = p_resolutions[compno];
1174 l_tcx0 = int_ceildiv(*p_tx0, l_img_comp->dx);
1175 l_tcy0 = int_ceildiv(*p_ty0, l_img_comp->dy);
1176 l_tcx1 = int_ceildiv(*p_tx1, l_img_comp->dx);
1177 l_tcy1 = int_ceildiv(*p_ty1, l_img_comp->dy);
1179 if (l_tccp->numresolutions > *p_max_res) {
1180 *p_max_res = l_tccp->numresolutions;
1183 /* use custom size for precincts*/
1184 l_level_no = l_tccp->numresolutions - 1;
1185 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
1186 OPJ_UINT32 l_dx, l_dy;
1188 /* precinct width and height*/
1189 l_pdx = l_tccp->prcw[resno];
1190 l_pdy = l_tccp->prch[resno];
1191 *lResolutionPtr++ = l_pdx;
1192 *lResolutionPtr++ = l_pdy;
1193 l_dx = l_img_comp->dx * (1 << (l_pdx + l_level_no));
1194 l_dy = l_img_comp->dy * (1 << (l_pdy + l_level_no));
1195 /* take the minimum size for l_dx for each comp and resolution*/
1196 *p_dx_min = int_min(*p_dx_min, l_dx);
1197 *p_dy_min = int_min(*p_dy_min, l_dy);
1199 /* various calculations of extents*/
1200 l_rx0 = int_ceildivpow2(l_tcx0, l_level_no);
1201 l_ry0 = int_ceildivpow2(l_tcy0, l_level_no);
1202 l_rx1 = int_ceildivpow2(l_tcx1, l_level_no);
1203 l_ry1 = int_ceildivpow2(l_tcy1, l_level_no);
1204 l_px0 = int_floordivpow2(l_rx0, l_pdx) << l_pdx;
1205 l_py0 = int_floordivpow2(l_ry0, l_pdy) << l_pdy;
1206 l_px1 = int_ceildivpow2(l_rx1, l_pdx) << l_pdx;
1207 py1 = int_ceildivpow2(l_ry1, l_pdy) << l_pdy;
1208 l_pw = (l_rx0==l_rx1)?0:((l_px1 - l_px0) >> l_pdx);
1209 l_ph = (l_ry0==l_ry1)?0:((py1 - l_py0) >> l_pdy);
1210 *lResolutionPtr++ = l_pw;
1211 *lResolutionPtr++ = l_ph;
1212 l_product = l_pw * l_ph;
1214 /* update precision*/
1215 if (l_product > *p_max_prec) {
1216 *p_max_prec = l_product;
1226 opj_pi_iterator_t * opj_pi_create( const opj_image_t *image,
1227 const opj_cp_v2_t *cp,
1231 OPJ_UINT32 pino, compno;
1232 /* number of poc in the p_pi*/
1233 OPJ_UINT32 l_poc_bound;
1235 /* pointers to tile coding parameters and components.*/
1236 opj_pi_iterator_t *l_pi = 00;
1237 opj_tcp_v2_t *tcp = 00;
1238 const opj_tccp_t *tccp = 00;
1240 /* current packet iterator being allocated*/
1241 opj_pi_iterator_t *l_current_pi = 00;
1243 /* preconditions in debug*/
1245 assert(image != 00);
1246 assert(tileno < cp->tw * cp->th);
1248 /* initializations*/
1249 tcp = &cp->tcps[tileno];
1250 l_poc_bound = tcp->numpocs+1;
1252 /* memory allocations*/
1253 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
1257 memset(l_pi,0,l_poc_bound * sizeof(opj_pi_iterator_t));
1259 l_current_pi = l_pi;
1260 for (pino = 0; pino < l_poc_bound ; ++pino) {
1262 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
1263 if (! l_current_pi->comps) {
1264 opj_pi_destroy(l_pi, l_poc_bound);
1268 l_current_pi->numcomps = image->numcomps;
1269 memset(l_current_pi->comps,0,image->numcomps * sizeof(opj_pi_comp_t));
1271 for (compno = 0; compno < image->numcomps; ++compno) {
1272 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1274 tccp = &tcp->tccps[compno];
1276 comp->resolutions = (opj_pi_resolution_t*) opj_malloc(tccp->numresolutions * sizeof(opj_pi_resolution_t));
1277 if (!comp->resolutions) {
1278 opj_pi_destroy(l_pi, l_poc_bound);
1282 comp->numresolutions = tccp->numresolutions;
1283 memset(comp->resolutions,0,tccp->numresolutions * sizeof(opj_pi_resolution_t));
1290 void opj_pi_update_encode_poc_and_final ( opj_cp_v2_t *p_cp,
1291 OPJ_UINT32 p_tileno,
1296 OPJ_UINT32 p_max_prec,
1297 OPJ_UINT32 p_max_res,
1298 OPJ_UINT32 p_dx_min,
1299 OPJ_UINT32 p_dy_min)
1303 /* tile coding parameter*/
1304 opj_tcp_v2_t *l_tcp = 00;
1305 /* current poc being updated*/
1306 opj_poc_t * l_current_poc = 00;
1309 OPJ_UINT32 l_poc_bound;
1311 /* preconditions in debug*/
1313 assert(p_tileno < p_cp->tw * p_cp->th);
1315 /* initializations*/
1316 l_tcp = &p_cp->tcps [p_tileno];
1317 /* number of iterations in the loop */
1318 l_poc_bound = l_tcp->numpocs+1;
1320 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1321 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1322 l_current_poc = l_tcp->pocs;
1324 l_current_poc->compS = l_current_poc->compno0;
1325 l_current_poc->compE = l_current_poc->compno1;
1326 l_current_poc->resS = l_current_poc->resno0;
1327 l_current_poc->resE = l_current_poc->resno1;
1328 l_current_poc->layE = l_current_poc->layno1;
1330 /* special treatment for the first element*/
1331 l_current_poc->layS = 0;
1332 l_current_poc->prg = l_current_poc->prg1;
1333 l_current_poc->prcS = 0;
1335 l_current_poc->prcE = p_max_prec;
1336 l_current_poc->txS = p_tx0;
1337 l_current_poc->txE = p_tx1;
1338 l_current_poc->tyS = p_ty0;
1339 l_current_poc->tyE = p_ty1;
1340 l_current_poc->dx = p_dx_min;
1341 l_current_poc->dy = p_dy_min;
1344 for (pino = 1;pino < l_poc_bound ; ++pino) {
1345 l_current_poc->compS = l_current_poc->compno0;
1346 l_current_poc->compE= l_current_poc->compno1;
1347 l_current_poc->resS = l_current_poc->resno0;
1348 l_current_poc->resE = l_current_poc->resno1;
1349 l_current_poc->layE = l_current_poc->layno1;
1350 l_current_poc->prg = l_current_poc->prg1;
1351 l_current_poc->prcS = 0;
1352 /* special treatment here different from the first element*/
1353 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
1355 l_current_poc->prcE = p_max_prec;
1356 l_current_poc->txS = p_tx0;
1357 l_current_poc->txE = p_tx1;
1358 l_current_poc->tyS = p_ty0;
1359 l_current_poc->tyE = p_ty1;
1360 l_current_poc->dx = p_dx_min;
1361 l_current_poc->dy = p_dy_min;
1366 void opj_pi_update_encode_not_poc ( opj_cp_v2_t *p_cp,
1367 OPJ_UINT32 p_num_comps,
1368 OPJ_UINT32 p_tileno,
1373 OPJ_UINT32 p_max_prec,
1374 OPJ_UINT32 p_max_res,
1375 OPJ_UINT32 p_dx_min,
1376 OPJ_UINT32 p_dy_min)
1380 /* tile coding parameter*/
1381 opj_tcp_v2_t *l_tcp = 00;
1382 /* current poc being updated*/
1383 opj_poc_t * l_current_poc = 00;
1385 OPJ_UINT32 l_poc_bound;
1387 /* preconditions in debug*/
1389 assert(p_tileno < p_cp->tw * p_cp->th);
1391 /* initializations*/
1392 l_tcp = &p_cp->tcps [p_tileno];
1394 /* number of iterations in the loop */
1395 l_poc_bound = l_tcp->numpocs+1;
1397 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1398 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1399 l_current_poc = l_tcp->pocs;
1401 for (pino = 0; pino < l_poc_bound ; ++pino) {
1402 l_current_poc->compS = 0;
1403 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1404 l_current_poc->resS = 0;
1405 l_current_poc->resE = p_max_res;
1406 l_current_poc->layS = 0;
1407 l_current_poc->layE = l_tcp->numlayers;
1408 l_current_poc->prg = l_tcp->prg;
1409 l_current_poc->prcS = 0;
1410 l_current_poc->prcE = p_max_prec;
1411 l_current_poc->txS = p_tx0;
1412 l_current_poc->txE = p_tx1;
1413 l_current_poc->tyS = p_ty0;
1414 l_current_poc->tyE = p_ty1;
1415 l_current_poc->dx = p_dx_min;
1416 l_current_poc->dy = p_dy_min;
1421 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1422 OPJ_UINT32 p_nb_elements)
1424 OPJ_UINT32 compno, pino;
1425 opj_pi_iterator_t *l_current_pi = p_pi;
1432 opj_free(p_pi->include);
1437 (pino = 0; pino < p_nb_elements; ++pino)
1440 (l_current_pi->comps)
1442 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1444 (compno = 0; compno < l_current_pi->numcomps; compno++)
1447 (l_current_component->resolutions)
1449 opj_free(l_current_component->resolutions);
1450 l_current_component->resolutions = 00;
1452 ++l_current_component;
1454 opj_free(l_current_pi->comps);
1455 l_current_pi->comps = 0;
1463 void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
1464 opj_tcp_v2_t * p_tcp,
1465 OPJ_UINT32 p_max_precision,
1466 OPJ_UINT32 p_max_res)
1471 /* encoding prameters to set*/
1474 opj_pi_iterator_t * l_current_pi = 00;
1475 opj_poc_t* l_current_poc = 0;
1477 /* preconditions in debug*/
1479 assert(p_tcp != 00);
1481 /* initializations*/
1482 l_bound = p_tcp->numpocs+1;
1483 l_current_pi = p_pi;
1484 l_current_poc = p_tcp->pocs;
1487 (pino = 0;pino<l_bound;++pino)
1489 l_current_pi->poc.prg = l_current_poc->prg;
1490 l_current_pi->first = 1;
1492 l_current_pi->poc.resno0 = l_current_poc->resno0;
1493 l_current_pi->poc.compno0 = l_current_poc->compno0;
1494 l_current_pi->poc.layno0 = 0;
1495 l_current_pi->poc.precno0 = 0;
1496 l_current_pi->poc.resno1 = l_current_poc->resno1;
1497 l_current_pi->poc.compno1 = l_current_poc->compno1;
1498 l_current_pi->poc.layno1 = l_current_poc->layno1;
1499 l_current_pi->poc.precno1 = p_max_precision;
1505 void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
1506 opj_tcp_v2_t * p_tcp,
1507 OPJ_UINT32 p_max_precision,
1508 OPJ_UINT32 p_max_res)
1513 /* encoding prameters to set*/
1516 opj_pi_iterator_t * l_current_pi = 00;
1517 /* preconditions in debug*/
1518 assert(p_tcp != 00);
1521 /* initializations*/
1522 l_bound = p_tcp->numpocs+1;
1523 l_current_pi = p_pi;
1526 (pino = 0;pino<l_bound;++pino)
1528 l_current_pi->poc.prg = p_tcp->prg;
1529 l_current_pi->first = 1;
1530 l_current_pi->poc.resno0 = 0;
1531 l_current_pi->poc.compno0 = 0;
1532 l_current_pi->poc.layno0 = 0;
1533 l_current_pi->poc.precno0 = 0;
1534 l_current_pi->poc.resno1 = p_max_res;
1535 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1536 l_current_pi->poc.layno1 = p_tcp->numlayers;
1537 l_current_pi->poc.precno1 = p_max_precision;
1542 void opj_pi_create_encode( opj_pi_iterator_t *pi,
1548 J2K_T2_MODE t2_mode)
1550 const OPJ_CHAR *prog;
1552 OPJ_UINT32 incr_top=1,resetX=0;
1553 opj_tcp_v2_t *tcps =&cp->tcps[tileno];
1554 opj_poc_t *tcp= &tcps->pocs[pino];
1556 prog = opj_j2k_convert_progression_order(tcp->prg);
1559 pi[pino].poc.prg = tcp->prg;
1561 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))){
1562 pi[pino].poc.resno0 = tcp->resS;
1563 pi[pino].poc.resno1 = tcp->resE;
1564 pi[pino].poc.compno0 = tcp->compS;
1565 pi[pino].poc.compno1 = tcp->compE;
1566 pi[pino].poc.layno0 = tcp->layS;
1567 pi[pino].poc.layno1 = tcp->layE;
1568 pi[pino].poc.precno0 = tcp->prcS;
1569 pi[pino].poc.precno1 = tcp->prcE;
1570 pi[pino].poc.tx0 = tcp->txS;
1571 pi[pino].poc.ty0 = tcp->tyS;
1572 pi[pino].poc.tx1 = tcp->txE;
1573 pi[pino].poc.ty1 = tcp->tyE;
1575 for(i=tppos+1;i<4;i++){
1578 pi[pino].poc.resno0 = tcp->resS;
1579 pi[pino].poc.resno1 = tcp->resE;
1582 pi[pino].poc.compno0 = tcp->compS;
1583 pi[pino].poc.compno1 = tcp->compE;
1586 pi[pino].poc.layno0 = tcp->layS;
1587 pi[pino].poc.layno1 = tcp->layE;
1593 pi[pino].poc.precno0 = tcp->prcS;
1594 pi[pino].poc.precno1 = tcp->prcE;
1597 pi[pino].poc.tx0 = tcp->txS;
1598 pi[pino].poc.ty0 = tcp->tyS;
1599 pi[pino].poc.tx1 = tcp->txE;
1600 pi[pino].poc.ty1 = tcp->tyE;
1608 for(i=tppos;i>=0;i--){
1611 tcp->comp_t = tcp->compS;
1612 pi[pino].poc.compno0 = tcp->comp_t;
1613 pi[pino].poc.compno1 = tcp->comp_t+1;
1617 tcp->res_t = tcp->resS;
1618 pi[pino].poc.resno0 = tcp->res_t;
1619 pi[pino].poc.resno1 = tcp->res_t+1;
1623 tcp->lay_t = tcp->layS;
1624 pi[pino].poc.layno0 = tcp->lay_t;
1625 pi[pino].poc.layno1 = tcp->lay_t+1;
1632 tcp->prc_t = tcp->prcS;
1633 pi[pino].poc.precno0 = tcp->prc_t;
1634 pi[pino].poc.precno1 = tcp->prc_t+1;
1638 tcp->tx0_t = tcp->txS;
1639 tcp->ty0_t = tcp->tyS;
1640 pi[pino].poc.tx0 = tcp->tx0_t;
1641 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1642 pi[pino].poc.ty0 = tcp->ty0_t;
1643 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1644 tcp->tx0_t = pi[pino].poc.tx1;
1645 tcp->ty0_t = pi[pino].poc.ty1;
1653 for(i=tppos;i>=0;i--){
1656 pi[pino].poc.compno0 = tcp->comp_t-1;
1657 pi[pino].poc.compno1 = tcp->comp_t;
1660 pi[pino].poc.resno0 = tcp->res_t-1;
1661 pi[pino].poc.resno1 = tcp->res_t;
1664 pi[pino].poc.layno0 = tcp->lay_t-1;
1665 pi[pino].poc.layno1 = tcp->lay_t;
1671 pi[pino].poc.precno0 = tcp->prc_t-1;
1672 pi[pino].poc.precno1 = tcp->prc_t;
1675 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1676 pi[pino].poc.tx1 = tcp->tx0_t ;
1677 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1678 pi[pino].poc.ty1 = tcp->ty0_t ;
1686 if(tcp->res_t==tcp->resE){
1687 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1688 tcp->res_t = tcp->resS;
1689 pi[pino].poc.resno0 = tcp->res_t;
1690 pi[pino].poc.resno1 = tcp->res_t+1;
1697 pi[pino].poc.resno0 = tcp->res_t;
1698 pi[pino].poc.resno1 = tcp->res_t+1;
1704 if(tcp->comp_t ==tcp->compE){
1705 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1706 tcp->comp_t = tcp->compS;
1707 pi[pino].poc.compno0 = tcp->comp_t;
1708 pi[pino].poc.compno1 = tcp->comp_t+1;
1715 pi[pino].poc.compno0 = tcp->comp_t;
1716 pi[pino].poc.compno1 = tcp->comp_t+1;
1722 if(tcp->lay_t == tcp->layE){
1723 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1724 tcp->lay_t = tcp->layS;
1725 pi[pino].poc.layno0 = tcp->lay_t;
1726 pi[pino].poc.layno1 = tcp->lay_t+1;
1733 pi[pino].poc.layno0 = tcp->lay_t;
1734 pi[pino].poc.layno1 = tcp->lay_t+1;
1743 if(tcp->prc_t == tcp->prcE){
1744 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1745 tcp->prc_t = tcp->prcS;
1746 pi[pino].poc.precno0 = tcp->prc_t;
1747 pi[pino].poc.precno1 = tcp->prc_t+1;
1754 pi[pino].poc.precno0 = tcp->prc_t;
1755 pi[pino].poc.precno1 = tcp->prc_t+1;
1761 if(tcp->tx0_t >= tcp->txE){
1762 if(tcp->ty0_t >= tcp->tyE){
1763 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1764 tcp->ty0_t = tcp->tyS;
1765 pi[pino].poc.ty0 = tcp->ty0_t;
1766 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1767 tcp->ty0_t = pi[pino].poc.ty1;
1768 incr_top=1;resetX=1;
1770 incr_top=0;resetX=0;
1773 pi[pino].poc.ty0 = tcp->ty0_t;
1774 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1775 tcp->ty0_t = pi[pino].poc.ty1;
1776 incr_top=0;resetX=1;
1779 tcp->tx0_t = tcp->txS;
1780 pi[pino].poc.tx0 = tcp->tx0_t;
1781 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1782 tcp->tx0_t = pi[pino].poc.tx1;
1785 pi[pino].poc.tx0 = tcp->tx0_t;
1786 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
1787 tcp->tx0_t = pi[pino].poc.tx1;
1800 opj_bool opj_pi_check_next_level( OPJ_INT32 pos,
1804 const OPJ_CHAR *prog)
1807 opj_tcp_v2_t *tcps =&cp->tcps[tileno];
1808 opj_poc_t *tcp = &tcps->pocs[pino];
1811 for(i=pos;pos>=0;i--){
1814 if(tcp->res_t==tcp->resE){
1815 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1825 if(tcp->comp_t==tcp->compE){
1826 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1836 if(tcp->lay_t==tcp->layE){
1837 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1849 if(tcp->prc_t == tcp->prcE){
1850 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1860 if(tcp->tx0_t == tcp->txE){
1862 if(tcp->ty0_t == tcp->tyE){
1863 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){