2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
41 /** @defgroup PI PI - Implementation of a packet iterator */
44 /** @name Local static functions */
48 Get next packet in layer-resolution-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_lrcp(opj_pi_iterator_t * pi);
54 Get next packet in resolution-layer-component-precinct 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_rlcp(opj_pi_iterator_t * pi);
60 Get next packet in resolution-precinct-component-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_rpcl(opj_pi_iterator_t * pi);
66 Get next packet in precinct-component-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_pcrl(opj_pi_iterator_t * pi);
72 Get next packet in component-precinct-resolution-layer order.
73 @param pi packet iterator to modify
74 @return returns false if pi pointed to the last packet or else returns true
76 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
79 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
81 * @param p_cp the coding parameters to modify
82 * @param p_tileno the tile index being concerned.
83 * @param p_tx0 X0 parameter for the tile
84 * @param p_tx1 X1 parameter for the tile
85 * @param p_ty0 Y0 parameter for the tile
86 * @param p_ty1 Y1 parameter for the tile
87 * @param p_max_prec the maximum precision for all the bands of the tile
88 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
89 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
90 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
92 static void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
98 OPJ_UINT32 p_max_prec,
101 OPJ_UINT32 p_dy_min);
104 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
106 * @param p_cp the coding parameters to modify
107 * @param p_num_comps the number of components
108 * @param p_tileno the tile index being concerned.
109 * @param p_tx0 X0 parameter for the tile
110 * @param p_tx1 X1 parameter for the tile
111 * @param p_ty0 Y0 parameter for the tile
112 * @param p_ty1 Y1 parameter for the tile
113 * @param p_max_prec the maximum precision for all the bands of the tile
114 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
115 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
116 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
118 static void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
119 OPJ_UINT32 p_num_comps,
125 OPJ_UINT32 p_max_prec,
126 OPJ_UINT32 p_max_res,
128 OPJ_UINT32 p_dy_min);
130 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
132 * @param p_image the image being encoded.
133 * @param p_cp the coding parameters.
134 * @param tileno the tile index of the tile being encoded.
135 * @param p_tx0 pointer that will hold the X0 parameter for the tile
136 * @param p_tx1 pointer that will hold the X1 parameter for the tile
137 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
138 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
139 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
140 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
141 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
142 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
144 static void opj_get_encoding_parameters(const opj_image_t *p_image,
145 const opj_cp_t *p_cp,
151 OPJ_UINT32 * p_dx_min,
152 OPJ_UINT32 * p_dy_min,
153 OPJ_UINT32 * p_max_prec,
154 OPJ_UINT32 * p_max_res );
157 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
158 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
159 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
160 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
161 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
163 * @param p_image the image being encoded.
164 * @param p_cp the coding parameters.
165 * @param tileno the tile index of the tile being encoded.
166 * @param p_tx0 pointer that will hold the X0 parameter for the tile
167 * @param p_tx1 pointer that will hold the X1 parameter for the tile
168 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
169 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
170 * @param p_max_prec pointer that will hold the the maximum precision for all the bands of the tile
171 * @param p_max_res pointer that will hold the the maximum number of resolutions for all the poc inside the tile.
172 * @param p_dx_min pointer that will hold the the minimum dx of all the components of all the resolutions for the tile.
173 * @param p_dy_min pointer that will hold the the minimum dy of all the components of all the resolutions for the tile.
174 * @param p_resolutions pointer to an area corresponding to the one described above.
176 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
177 const opj_cp_t *p_cp,
183 OPJ_UINT32 * p_dx_min,
184 OPJ_UINT32 * p_dy_min,
185 OPJ_UINT32 * p_max_prec,
186 OPJ_UINT32 * p_max_res,
187 OPJ_UINT32 ** p_resolutions );
189 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
190 * No other data is set. The include section of the packet iterator is not allocated.
192 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
193 * @param p_cp the coding parameters.
194 * @param tileno the index of the tile from which creating the packet iterator.
196 static opj_pi_iterator_t * opj_pi_create( const opj_image_t *p_image,
197 const opj_cp_t *p_cp,
202 static void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
204 OPJ_UINT32 p_max_precision,
205 OPJ_UINT32 p_max_res);
209 static void opj_pi_update_decode_poc ( opj_pi_iterator_t * p_pi,
211 OPJ_UINT32 p_max_precision,
212 OPJ_UINT32 p_max_res);
217 OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
221 const OPJ_CHAR *prog);
228 ==========================================================
230 ==========================================================
233 OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi) {
234 opj_pi_comp_t *comp = NULL;
235 opj_pi_resolution_t *res = NULL;
236 OPJ_UINT32 index = 0;
239 comp = &pi->comps[pi->compno];
240 res = &comp->resolutions[pi->resno];
246 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
247 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
249 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
250 comp = &pi->comps[pi->compno];
251 if (pi->resno >= comp->numresolutions) {
254 res = &comp->resolutions[pi->resno];
256 pi->poc.precno1 = res->pw * res->ph;
258 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
259 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
260 if (!pi->include[index]) {
261 pi->include[index] = 1;
273 OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi) {
274 opj_pi_comp_t *comp = NULL;
275 opj_pi_resolution_t *res = NULL;
276 OPJ_UINT32 index = 0;
279 comp = &pi->comps[pi->compno];
280 res = &comp->resolutions[pi->resno];
286 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
287 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
288 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
289 comp = &pi->comps[pi->compno];
290 if (pi->resno >= comp->numresolutions) {
293 res = &comp->resolutions[pi->resno];
295 pi->poc.precno1 = res->pw * res->ph;
297 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
298 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
299 if (!pi->include[index]) {
300 pi->include[index] = 1;
312 OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) {
313 opj_pi_comp_t *comp = NULL;
314 opj_pi_resolution_t *res = NULL;
315 OPJ_UINT32 index = 0;
320 OPJ_UINT32 compno, resno;
324 for (compno = 0; compno < pi->numcomps; compno++) {
325 comp = &pi->comps[compno];
326 for (resno = 0; resno < comp->numresolutions; resno++) {
328 res = &comp->resolutions[resno];
329 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
330 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
331 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
332 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
337 pi->poc.ty0 = pi->ty0;
338 pi->poc.tx0 = pi->tx0;
339 pi->poc.ty1 = pi->ty1;
340 pi->poc.tx1 = pi->tx1;
342 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
343 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))) {
344 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))) {
345 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
347 OPJ_INT32 trx0, try0;
348 OPJ_INT32 trx1, try1;
350 OPJ_INT32 prci, prcj;
351 comp = &pi->comps[pi->compno];
352 if (pi->resno >= comp->numresolutions) {
355 res = &comp->resolutions[pi->resno];
356 levelno = comp->numresolutions - 1 - pi->resno;
357 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
358 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
359 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
360 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
361 rpx = res->pdx + levelno;
362 rpy = res->pdy + levelno;
363 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
366 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
370 if ((res->pw==0)||(res->ph==0)) continue;
372 if ((trx0==trx1)||(try0==try1)) continue;
374 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
375 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
376 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
377 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
378 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
379 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
380 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
381 if (!pi->include[index]) {
382 pi->include[index] = 1;
395 OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
396 opj_pi_comp_t *comp = NULL;
397 opj_pi_resolution_t *res = NULL;
398 OPJ_UINT32 index = 0;
401 comp = &pi->comps[pi->compno];
404 OPJ_UINT32 compno, resno;
408 for (compno = 0; compno < pi->numcomps; compno++) {
409 comp = &pi->comps[compno];
410 for (resno = 0; resno < comp->numresolutions; resno++) {
412 res = &comp->resolutions[resno];
413 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
414 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
415 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
416 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
421 pi->poc.ty0 = pi->ty0;
422 pi->poc.tx0 = pi->tx0;
423 pi->poc.ty1 = pi->ty1;
424 pi->poc.tx1 = pi->tx1;
426 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))) {
427 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))) {
428 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
429 comp = &pi->comps[pi->compno];
430 for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
432 OPJ_INT32 trx0, try0;
433 OPJ_INT32 trx1, try1;
435 OPJ_INT32 prci, prcj;
436 res = &comp->resolutions[pi->resno];
437 levelno = comp->numresolutions - 1 - pi->resno;
438 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
439 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
440 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
441 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
442 rpx = res->pdx + levelno;
443 rpy = res->pdy + levelno;
444 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
447 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
451 if ((res->pw==0)||(res->ph==0)) continue;
453 if ((trx0==trx1)||(try0==try1)) continue;
455 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
456 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
457 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
458 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
459 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
460 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
461 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
462 if (!pi->include[index]) {
463 pi->include[index] = 1;
476 OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
477 opj_pi_comp_t *comp = NULL;
478 opj_pi_resolution_t *res = NULL;
479 OPJ_UINT32 index = 0;
482 comp = &pi->comps[pi->compno];
488 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
490 comp = &pi->comps[pi->compno];
493 for (resno = 0; resno < comp->numresolutions; resno++) {
495 res = &comp->resolutions[resno];
496 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
497 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
498 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
499 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
502 pi->poc.ty0 = pi->ty0;
503 pi->poc.tx0 = pi->tx0;
504 pi->poc.ty1 = pi->ty1;
505 pi->poc.tx1 = pi->tx1;
507 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))) {
508 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))) {
509 for (pi->resno = pi->poc.resno0; pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
511 OPJ_INT32 trx0, try0;
512 OPJ_INT32 trx1, try1;
514 OPJ_INT32 prci, prcj;
515 res = &comp->resolutions[pi->resno];
516 levelno = comp->numresolutions - 1 - pi->resno;
517 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
518 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
519 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
520 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
521 rpx = res->pdx + levelno;
522 rpy = res->pdy + levelno;
523 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
526 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
530 if ((res->pw==0)||(res->ph==0)) continue;
532 if ((trx0==trx1)||(try0==try1)) continue;
534 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
535 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
536 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
537 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
538 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
539 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
540 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
541 if (!pi->include[index]) {
542 pi->include[index] = 1;
555 void opj_get_encoding_parameters( const opj_image_t *p_image,
556 const opj_cp_t *p_cp,
562 OPJ_UINT32 * p_dx_min,
563 OPJ_UINT32 * p_dy_min,
564 OPJ_UINT32 * p_max_prec,
565 OPJ_UINT32 * p_max_res )
568 OPJ_UINT32 compno, resno;
570 const opj_tcp_t *l_tcp = 00;
571 const opj_tccp_t * l_tccp = 00;
572 const opj_image_comp_t * l_img_comp = 00;
574 /* position in x and y of tile */
579 assert(p_image != 00);
580 assert(p_tileno < p_cp->tw * p_cp->th);
582 /* initializations */
583 l_tcp = &p_cp->tcps [p_tileno];
584 l_img_comp = p_image->comps;
585 l_tccp = l_tcp->tccps;
587 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
588 p = p_tileno % p_cp->tw;
589 q = p_tileno / p_cp->tw;
591 /* find extent of tile */
592 *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
593 *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
594 *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
595 *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
597 /* max precision is 0 (can only grow) */
601 /* take the largest value for dx_min and dy_min */
602 *p_dx_min = 0x7fffffff;
603 *p_dy_min = 0x7fffffff;
605 for (compno = 0; compno < p_image->numcomps; ++compno) {
606 /* arithmetic variables to calculate */
607 OPJ_UINT32 l_level_no;
608 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
609 OPJ_INT32 l_px0, l_py0, l_px1, py1;
610 OPJ_UINT32 l_pdx, l_pdy;
611 OPJ_UINT32 l_pw, l_ph;
612 OPJ_UINT32 l_product;
613 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
615 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
616 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
617 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
618 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
620 if (l_tccp->numresolutions > *p_max_res) {
621 *p_max_res = l_tccp->numresolutions;
624 /* use custom size for precincts */
625 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
626 OPJ_UINT32 l_dx, l_dy;
628 /* precinct width and height */
629 l_pdx = l_tccp->prcw[resno];
630 l_pdy = l_tccp->prch[resno];
632 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
633 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
635 /* take the minimum size for dx for each comp and resolution */
636 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
637 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
639 /* various calculations of extents */
640 l_level_no = l_tccp->numresolutions - 1 - resno;
642 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
643 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
644 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
645 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
647 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
648 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
649 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
651 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
653 l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
654 l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
656 l_product = l_pw * l_ph;
658 /* update precision */
659 if (l_product > *p_max_prec) {
660 *p_max_prec = l_product;
669 void opj_get_all_encoding_parameters( const opj_image_t *p_image,
670 const opj_cp_t *p_cp,
676 OPJ_UINT32 * p_dx_min,
677 OPJ_UINT32 * p_dy_min,
678 OPJ_UINT32 * p_max_prec,
679 OPJ_UINT32 * p_max_res,
680 OPJ_UINT32 ** p_resolutions )
683 OPJ_UINT32 compno, resno;
686 const opj_tcp_t *tcp = 00;
687 const opj_tccp_t * l_tccp = 00;
688 const opj_image_comp_t * l_img_comp = 00;
690 /* to store l_dx, l_dy, w and h for each resolution and component.*/
691 OPJ_UINT32 * lResolutionPtr;
693 /* position in x and y of tile*/
696 /* preconditions in debug*/
698 assert(p_image != 00);
699 assert(tileno < p_cp->tw * p_cp->th);
702 tcp = &p_cp->tcps [tileno];
704 l_img_comp = p_image->comps;
706 /* position in x and y of tile*/
707 p = tileno % p_cp->tw;
708 q = tileno / p_cp->tw;
710 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
711 *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx), (OPJ_INT32)p_image->x0);
712 *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx), (OPJ_INT32)p_image->x1);
713 *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy), (OPJ_INT32)p_image->y0);
714 *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy), (OPJ_INT32)p_image->y1);
716 /* max precision and resolution is 0 (can only grow)*/
720 /* take the largest value for dx_min and dy_min*/
721 *p_dx_min = 0x7fffffff;
722 *p_dy_min = 0x7fffffff;
724 for (compno = 0; compno < p_image->numcomps; ++compno) {
725 /* aritmetic variables to calculate*/
726 OPJ_UINT32 l_level_no;
727 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
728 OPJ_INT32 l_px0, l_py0, l_px1, py1;
729 OPJ_UINT32 l_product;
730 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
731 OPJ_UINT32 l_pdx, l_pdy , l_pw , l_ph;
733 lResolutionPtr = p_resolutions[compno];
735 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
736 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
737 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
738 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
740 if (l_tccp->numresolutions > *p_max_res) {
741 *p_max_res = l_tccp->numresolutions;
744 /* use custom size for precincts*/
745 l_level_no = l_tccp->numresolutions - 1;
746 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
747 OPJ_UINT32 l_dx, l_dy;
749 /* precinct width and height*/
750 l_pdx = l_tccp->prcw[resno];
751 l_pdy = l_tccp->prch[resno];
752 *lResolutionPtr++ = l_pdx;
753 *lResolutionPtr++ = l_pdy;
754 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
755 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
756 /* take the minimum size for l_dx for each comp and resolution*/
757 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dx_min, (OPJ_INT32)l_dx);
758 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32)*p_dy_min, (OPJ_INT32)l_dy);
760 /* various calculations of extents*/
761 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
762 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
763 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
764 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
765 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
766 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
767 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
768 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
769 l_pw = (l_rx0==l_rx1)?0:(OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
770 l_ph = (l_ry0==l_ry1)?0:(OPJ_UINT32)((py1 - l_py0) >> l_pdy);
771 *lResolutionPtr++ = l_pw;
772 *lResolutionPtr++ = l_ph;
773 l_product = l_pw * l_ph;
775 /* update precision*/
776 if (l_product > *p_max_prec) {
777 *p_max_prec = l_product;
787 opj_pi_iterator_t * opj_pi_create( const opj_image_t *image,
792 OPJ_UINT32 pino, compno;
793 /* number of poc in the p_pi*/
794 OPJ_UINT32 l_poc_bound;
796 /* pointers to tile coding parameters and components.*/
797 opj_pi_iterator_t *l_pi = 00;
799 const opj_tccp_t *tccp = 00;
801 /* current packet iterator being allocated*/
802 opj_pi_iterator_t *l_current_pi = 00;
804 /* preconditions in debug*/
807 assert(tileno < cp->tw * cp->th);
810 tcp = &cp->tcps[tileno];
811 l_poc_bound = tcp->numpocs+1;
813 /* memory allocations*/
814 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound), sizeof(opj_pi_iterator_t));
820 for (pino = 0; pino < l_poc_bound ; ++pino) {
822 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
823 if (! l_current_pi->comps) {
824 opj_pi_destroy(l_pi, l_poc_bound);
828 l_current_pi->numcomps = image->numcomps;
830 for (compno = 0; compno < image->numcomps; ++compno) {
831 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
833 tccp = &tcp->tccps[compno];
835 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions, sizeof(opj_pi_resolution_t));
836 if (!comp->resolutions) {
837 opj_pi_destroy(l_pi, l_poc_bound);
841 comp->numresolutions = tccp->numresolutions;
848 void opj_pi_update_encode_poc_and_final ( opj_cp_t *p_cp,
854 OPJ_UINT32 p_max_prec,
855 OPJ_UINT32 p_max_res,
861 /* tile coding parameter*/
862 opj_tcp_t *l_tcp = 00;
863 /* current poc being updated*/
864 opj_poc_t * l_current_poc = 00;
867 OPJ_UINT32 l_poc_bound;
869 OPJ_ARG_NOT_USED(p_max_res);
871 /* preconditions in debug*/
873 assert(p_tileno < p_cp->tw * p_cp->th);
876 l_tcp = &p_cp->tcps [p_tileno];
877 /* number of iterations in the loop */
878 l_poc_bound = l_tcp->numpocs+1;
880 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
881 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
882 l_current_poc = l_tcp->pocs;
884 l_current_poc->compS = l_current_poc->compno0;
885 l_current_poc->compE = l_current_poc->compno1;
886 l_current_poc->resS = l_current_poc->resno0;
887 l_current_poc->resE = l_current_poc->resno1;
888 l_current_poc->layE = l_current_poc->layno1;
890 /* special treatment for the first element*/
891 l_current_poc->layS = 0;
892 l_current_poc->prg = l_current_poc->prg1;
893 l_current_poc->prcS = 0;
895 l_current_poc->prcE = p_max_prec;
896 l_current_poc->txS = (OPJ_UINT32)p_tx0;
897 l_current_poc->txE = (OPJ_UINT32)p_tx1;
898 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
899 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
900 l_current_poc->dx = p_dx_min;
901 l_current_poc->dy = p_dy_min;
904 for (pino = 1;pino < l_poc_bound ; ++pino) {
905 l_current_poc->compS = l_current_poc->compno0;
906 l_current_poc->compE= l_current_poc->compno1;
907 l_current_poc->resS = l_current_poc->resno0;
908 l_current_poc->resE = l_current_poc->resno1;
909 l_current_poc->layE = l_current_poc->layno1;
910 l_current_poc->prg = l_current_poc->prg1;
911 l_current_poc->prcS = 0;
912 /* special treatment here different from the first element*/
913 l_current_poc->layS = (l_current_poc->layE > (l_current_poc-1)->layE) ? l_current_poc->layE : 0;
915 l_current_poc->prcE = p_max_prec;
916 l_current_poc->txS = (OPJ_UINT32)p_tx0;
917 l_current_poc->txE = (OPJ_UINT32)p_tx1;
918 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
919 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
920 l_current_poc->dx = p_dx_min;
921 l_current_poc->dy = p_dy_min;
926 void opj_pi_update_encode_not_poc ( opj_cp_t *p_cp,
927 OPJ_UINT32 p_num_comps,
933 OPJ_UINT32 p_max_prec,
934 OPJ_UINT32 p_max_res,
940 /* tile coding parameter*/
941 opj_tcp_t *l_tcp = 00;
942 /* current poc being updated*/
943 opj_poc_t * l_current_poc = 00;
945 OPJ_UINT32 l_poc_bound;
947 /* preconditions in debug*/
949 assert(p_tileno < p_cp->tw * p_cp->th);
952 l_tcp = &p_cp->tcps [p_tileno];
954 /* number of iterations in the loop */
955 l_poc_bound = l_tcp->numpocs+1;
957 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
958 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
959 l_current_poc = l_tcp->pocs;
961 for (pino = 0; pino < l_poc_bound ; ++pino) {
962 l_current_poc->compS = 0;
963 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
964 l_current_poc->resS = 0;
965 l_current_poc->resE = p_max_res;
966 l_current_poc->layS = 0;
967 l_current_poc->layE = l_tcp->numlayers;
968 l_current_poc->prg = l_tcp->prg;
969 l_current_poc->prcS = 0;
970 l_current_poc->prcE = p_max_prec;
971 l_current_poc->txS = (OPJ_UINT32)p_tx0;
972 l_current_poc->txE = (OPJ_UINT32)p_tx1;
973 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
974 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
975 l_current_poc->dx = p_dx_min;
976 l_current_poc->dy = p_dy_min;
981 void opj_pi_update_decode_poc (opj_pi_iterator_t * p_pi,
983 OPJ_UINT32 p_max_precision,
984 OPJ_UINT32 p_max_res)
989 /* encoding prameters to set*/
992 opj_pi_iterator_t * l_current_pi = 00;
993 opj_poc_t* l_current_poc = 0;
995 OPJ_ARG_NOT_USED(p_max_res);
997 /* preconditions in debug*/
1001 /* initializations*/
1002 l_bound = p_tcp->numpocs+1;
1003 l_current_pi = p_pi;
1004 l_current_poc = p_tcp->pocs;
1006 for (pino = 0;pino<l_bound;++pino) {
1007 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1008 l_current_pi->first = 1;
1010 l_current_pi->poc.resno0 = l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1011 l_current_pi->poc.compno0 = l_current_poc->compno0; /* Component Index #0 (Start) */
1012 l_current_pi->poc.layno0 = 0;
1013 l_current_pi->poc.precno0 = 0;
1014 l_current_pi->poc.resno1 = l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1015 l_current_pi->poc.compno1 = l_current_poc->compno1; /* Component Index #0 (End) */
1016 l_current_pi->poc.layno1 = l_current_poc->layno1; /* Layer Index #0 (End) */
1017 l_current_pi->poc.precno1 = p_max_precision;
1023 void opj_pi_update_decode_not_poc (opj_pi_iterator_t * p_pi,
1025 OPJ_UINT32 p_max_precision,
1026 OPJ_UINT32 p_max_res)
1031 /* encoding prameters to set*/
1034 opj_pi_iterator_t * l_current_pi = 00;
1035 /* preconditions in debug*/
1036 assert(p_tcp != 00);
1039 /* initializations*/
1040 l_bound = p_tcp->numpocs+1;
1041 l_current_pi = p_pi;
1043 for (pino = 0;pino<l_bound;++pino) {
1044 l_current_pi->poc.prg = p_tcp->prg;
1045 l_current_pi->first = 1;
1046 l_current_pi->poc.resno0 = 0;
1047 l_current_pi->poc.compno0 = 0;
1048 l_current_pi->poc.layno0 = 0;
1049 l_current_pi->poc.precno0 = 0;
1050 l_current_pi->poc.resno1 = p_max_res;
1051 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1052 l_current_pi->poc.layno1 = p_tcp->numlayers;
1053 l_current_pi->poc.precno1 = p_max_precision;
1060 OPJ_BOOL opj_pi_check_next_level( OPJ_INT32 pos,
1064 const OPJ_CHAR *prog)
1067 opj_tcp_t *tcps =&cp->tcps[tileno];
1068 opj_poc_t *tcp = &tcps->pocs[pino];
1071 for(i=pos;pos>=0;i--){
1074 if(tcp->res_t==tcp->resE){
1075 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1085 if(tcp->comp_t==tcp->compE){
1086 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1096 if(tcp->lay_t==tcp->layE){
1097 if(opj_pi_check_next_level(pos-1,cp,tileno,pino,prog)){
1108 case OPJ_LRCP: /* fall through */
1110 if(tcp->prc_t == tcp->prcE){
1111 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1121 if(tcp->tx0_t == tcp->txE){
1123 if(tcp->ty0_t == tcp->tyE){
1124 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1145 ==========================================================
1146 Packet iterator interface
1147 ==========================================================
1149 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1151 OPJ_UINT32 p_tile_no)
1155 OPJ_UINT32 compno, resno;
1157 /* to store w, h, dx and dy fro all components and resolutions */
1158 OPJ_UINT32 * l_tmp_data;
1159 OPJ_UINT32 ** l_tmp_ptr;
1161 /* encoding prameters to set */
1162 OPJ_UINT32 l_max_res;
1163 OPJ_UINT32 l_max_prec;
1164 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1165 OPJ_UINT32 l_dx_min,l_dy_min;
1167 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1168 OPJ_UINT32 l_data_stride;
1171 opj_pi_iterator_t *l_pi = 00;
1172 opj_tcp_t *l_tcp = 00;
1173 const opj_tccp_t *l_tccp = 00;
1174 opj_pi_comp_t *l_current_comp = 00;
1175 opj_image_comp_t * l_img_comp = 00;
1176 opj_pi_iterator_t * l_current_pi = 00;
1177 OPJ_UINT32 * l_encoding_value_ptr = 00;
1179 /* preconditions in debug */
1181 assert(p_image != 00);
1182 assert(p_tile_no < p_cp->tw * p_cp->th);
1184 /* initializations */
1185 l_tcp = &p_cp->tcps[p_tile_no];
1186 l_bound = l_tcp->numpocs+1;
1188 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1189 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1190 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1196 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1197 p_image->numcomps * sizeof(OPJ_UINT32 *));
1201 opj_free(l_tmp_data);
1205 /* memory allocation for pi */
1206 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1208 opj_free(l_tmp_data);
1209 opj_free(l_tmp_ptr);
1213 l_encoding_value_ptr = l_tmp_data;
1214 /* update pointer array */
1216 (compno = 0; compno < p_image->numcomps; ++compno)
1218 l_tmp_ptr[compno] = l_encoding_value_ptr;
1219 l_encoding_value_ptr += l_data_stride;
1221 /* get encoding parameters */
1222 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);
1224 /* step calculations */
1226 l_step_c = l_max_prec * l_step_p;
1227 l_step_r = p_image->numcomps * l_step_c;
1228 l_step_l = l_max_res * l_step_r;
1230 /* set values for first packet iterator */
1231 l_current_pi = l_pi;
1233 /* memory allocation for include */
1234 l_current_pi->include = (OPJ_INT16*) opj_calloc((l_tcp->numlayers +1) * l_step_l, sizeof(OPJ_INT16));
1236 (!l_current_pi->include)
1238 opj_free(l_tmp_data);
1239 opj_free(l_tmp_ptr);
1240 opj_pi_destroy(l_pi, l_bound);
1244 /* special treatment for the first packet iterator */
1245 l_current_comp = l_current_pi->comps;
1246 l_img_comp = p_image->comps;
1247 l_tccp = l_tcp->tccps;
1249 l_current_pi->tx0 = l_tx0;
1250 l_current_pi->ty0 = l_ty0;
1251 l_current_pi->tx1 = l_tx1;
1252 l_current_pi->ty1 = l_ty1;
1254 /*l_current_pi->dx = l_img_comp->dx;*/
1255 /*l_current_pi->dy = l_img_comp->dy;*/
1257 l_current_pi->step_p = l_step_p;
1258 l_current_pi->step_c = l_step_c;
1259 l_current_pi->step_r = l_step_r;
1260 l_current_pi->step_l = l_step_l;
1262 /* allocation for components and number of components has already been calculated by opj_pi_create */
1264 (compno = 0; compno < l_current_pi->numcomps; ++compno)
1266 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1267 l_encoding_value_ptr = l_tmp_ptr[compno];
1269 l_current_comp->dx = l_img_comp->dx;
1270 l_current_comp->dy = l_img_comp->dy;
1271 /* resolutions have already been initialized */
1273 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1275 l_res->pdx = *(l_encoding_value_ptr++);
1276 l_res->pdy = *(l_encoding_value_ptr++);
1277 l_res->pw = *(l_encoding_value_ptr++);
1278 l_res->ph = *(l_encoding_value_ptr++);
1287 for (pino = 1 ; pino<l_bound ; ++pino )
1289 l_current_comp = l_current_pi->comps;
1290 l_img_comp = p_image->comps;
1291 l_tccp = l_tcp->tccps;
1293 l_current_pi->tx0 = l_tx0;
1294 l_current_pi->ty0 = l_ty0;
1295 l_current_pi->tx1 = l_tx1;
1296 l_current_pi->ty1 = l_ty1;
1297 /*l_current_pi->dx = l_dx_min;*/
1298 /*l_current_pi->dy = l_dy_min;*/
1299 l_current_pi->step_p = l_step_p;
1300 l_current_pi->step_c = l_step_c;
1301 l_current_pi->step_r = l_step_r;
1302 l_current_pi->step_l = l_step_l;
1304 /* allocation for components and number of components has already been calculated by opj_pi_create */
1306 (compno = 0; compno < l_current_pi->numcomps; ++compno)
1308 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1309 l_encoding_value_ptr = l_tmp_ptr[compno];
1311 l_current_comp->dx = l_img_comp->dx;
1312 l_current_comp->dy = l_img_comp->dy;
1313 /* resolutions have already been initialized */
1315 (resno = 0; resno < l_current_comp->numresolutions; resno++)
1317 l_res->pdx = *(l_encoding_value_ptr++);
1318 l_res->pdy = *(l_encoding_value_ptr++);
1319 l_res->pw = *(l_encoding_value_ptr++);
1320 l_res->ph = *(l_encoding_value_ptr++);
1327 /* special treatment*/
1328 l_current_pi->include = (l_current_pi-1)->include;
1331 opj_free(l_tmp_data);
1333 opj_free(l_tmp_ptr);
1338 opj_pi_update_decode_poc (l_pi,l_tcp,l_max_prec,l_max_res);
1342 opj_pi_update_decode_not_poc(l_pi,l_tcp,l_max_prec,l_max_res);
1349 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1351 OPJ_UINT32 p_tile_no,
1352 J2K_T2_MODE p_t2_mode )
1356 OPJ_UINT32 compno, resno;
1358 /* to store w, h, dx and dy fro all components and resolutions*/
1359 OPJ_UINT32 * l_tmp_data;
1360 OPJ_UINT32 ** l_tmp_ptr;
1362 /* encoding prameters to set*/
1363 OPJ_UINT32 l_max_res;
1364 OPJ_UINT32 l_max_prec;
1365 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1366 OPJ_UINT32 l_dx_min,l_dy_min;
1368 OPJ_UINT32 l_step_p , l_step_c , l_step_r , l_step_l ;
1369 OPJ_UINT32 l_data_stride;
1372 opj_pi_iterator_t *l_pi = 00;
1373 opj_tcp_t *l_tcp = 00;
1374 const opj_tccp_t *l_tccp = 00;
1375 opj_pi_comp_t *l_current_comp = 00;
1376 opj_image_comp_t * l_img_comp = 00;
1377 opj_pi_iterator_t * l_current_pi = 00;
1378 OPJ_UINT32 * l_encoding_value_ptr = 00;
1380 /* preconditions in debug*/
1382 assert(p_image != 00);
1383 assert(p_tile_no < p_cp->tw * p_cp->th);
1385 /* initializations*/
1386 l_tcp = &p_cp->tcps[p_tile_no];
1387 l_bound = l_tcp->numpocs+1;
1389 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1390 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1391 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1396 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1397 p_image->numcomps * sizeof(OPJ_UINT32 *));
1399 opj_free(l_tmp_data);
1403 /* memory allocation for pi*/
1404 l_pi = opj_pi_create(p_image,p_cp,p_tile_no);
1406 opj_free(l_tmp_data);
1407 opj_free(l_tmp_ptr);
1411 l_encoding_value_ptr = l_tmp_data;
1412 /* update pointer array*/
1413 for (compno = 0; compno < p_image->numcomps; ++compno) {
1414 l_tmp_ptr[compno] = l_encoding_value_ptr;
1415 l_encoding_value_ptr += l_data_stride;
1418 /* get encoding parameters*/
1419 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);
1421 /* step calculations*/
1423 l_step_c = l_max_prec * l_step_p;
1424 l_step_r = p_image->numcomps * l_step_c;
1425 l_step_l = l_max_res * l_step_r;
1427 /* set values for first packet iterator*/
1428 l_pi->tp_on = p_cp->m_specific_param.m_enc.m_tp_on;
1429 l_current_pi = l_pi;
1431 /* memory allocation for include*/
1432 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l, sizeof(OPJ_INT16));
1433 if (!l_current_pi->include) {
1434 opj_free(l_tmp_data);
1435 opj_free(l_tmp_ptr);
1436 opj_pi_destroy(l_pi, l_bound);
1440 /* special treatment for the first packet iterator*/
1441 l_current_comp = l_current_pi->comps;
1442 l_img_comp = p_image->comps;
1443 l_tccp = l_tcp->tccps;
1444 l_current_pi->tx0 = l_tx0;
1445 l_current_pi->ty0 = l_ty0;
1446 l_current_pi->tx1 = l_tx1;
1447 l_current_pi->ty1 = l_ty1;
1448 l_current_pi->dx = l_dx_min;
1449 l_current_pi->dy = l_dy_min;
1450 l_current_pi->step_p = l_step_p;
1451 l_current_pi->step_c = l_step_c;
1452 l_current_pi->step_r = l_step_r;
1453 l_current_pi->step_l = l_step_l;
1455 /* allocation for components and number of components has already been calculated by opj_pi_create */
1456 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1457 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1458 l_encoding_value_ptr = l_tmp_ptr[compno];
1460 l_current_comp->dx = l_img_comp->dx;
1461 l_current_comp->dy = l_img_comp->dy;
1463 /* resolutions have already been initialized */
1464 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1465 l_res->pdx = *(l_encoding_value_ptr++);
1466 l_res->pdy = *(l_encoding_value_ptr++);
1467 l_res->pw = *(l_encoding_value_ptr++);
1468 l_res->ph = *(l_encoding_value_ptr++);
1478 for (pino = 1 ; pino<l_bound ; ++pino ) {
1479 l_current_comp = l_current_pi->comps;
1480 l_img_comp = p_image->comps;
1481 l_tccp = l_tcp->tccps;
1483 l_current_pi->tx0 = l_tx0;
1484 l_current_pi->ty0 = l_ty0;
1485 l_current_pi->tx1 = l_tx1;
1486 l_current_pi->ty1 = l_ty1;
1487 l_current_pi->dx = l_dx_min;
1488 l_current_pi->dy = l_dy_min;
1489 l_current_pi->step_p = l_step_p;
1490 l_current_pi->step_c = l_step_c;
1491 l_current_pi->step_r = l_step_r;
1492 l_current_pi->step_l = l_step_l;
1494 /* allocation for components and number of components has already been calculated by opj_pi_create */
1495 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1496 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1497 l_encoding_value_ptr = l_tmp_ptr[compno];
1499 l_current_comp->dx = l_img_comp->dx;
1500 l_current_comp->dy = l_img_comp->dy;
1501 /* resolutions have already been initialized */
1502 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1503 l_res->pdx = *(l_encoding_value_ptr++);
1504 l_res->pdy = *(l_encoding_value_ptr++);
1505 l_res->pw = *(l_encoding_value_ptr++);
1506 l_res->ph = *(l_encoding_value_ptr++);
1514 /* special treatment*/
1515 l_current_pi->include = (l_current_pi-1)->include;
1519 opj_free(l_tmp_data);
1521 opj_free(l_tmp_ptr);
1524 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1525 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);
1528 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);
1534 void opj_pi_create_encode( opj_pi_iterator_t *pi,
1540 J2K_T2_MODE t2_mode)
1542 const OPJ_CHAR *prog;
1544 OPJ_UINT32 incr_top=1,resetX=0;
1545 opj_tcp_t *tcps =&cp->tcps[tileno];
1546 opj_poc_t *tcp= &tcps->pocs[pino];
1548 prog = opj_j2k_convert_progression_order(tcp->prg);
1551 pi[pino].poc.prg = tcp->prg;
1553 if(!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) && (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))){
1554 pi[pino].poc.resno0 = tcp->resS;
1555 pi[pino].poc.resno1 = tcp->resE;
1556 pi[pino].poc.compno0 = tcp->compS;
1557 pi[pino].poc.compno1 = tcp->compE;
1558 pi[pino].poc.layno0 = tcp->layS;
1559 pi[pino].poc.layno1 = tcp->layE;
1560 pi[pino].poc.precno0 = tcp->prcS;
1561 pi[pino].poc.precno1 = tcp->prcE;
1562 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1563 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1564 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1565 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1567 for(i=tppos+1;i<4;i++){
1570 pi[pino].poc.resno0 = tcp->resS;
1571 pi[pino].poc.resno1 = tcp->resE;
1574 pi[pino].poc.compno0 = tcp->compS;
1575 pi[pino].poc.compno1 = tcp->compE;
1578 pi[pino].poc.layno0 = tcp->layS;
1579 pi[pino].poc.layno1 = tcp->layE;
1585 pi[pino].poc.precno0 = tcp->prcS;
1586 pi[pino].poc.precno1 = tcp->prcE;
1589 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1590 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1591 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1592 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1600 for(i=tppos;i>=0;i--){
1603 tcp->comp_t = tcp->compS;
1604 pi[pino].poc.compno0 = tcp->comp_t;
1605 pi[pino].poc.compno1 = tcp->comp_t+1;
1609 tcp->res_t = tcp->resS;
1610 pi[pino].poc.resno0 = tcp->res_t;
1611 pi[pino].poc.resno1 = tcp->res_t+1;
1615 tcp->lay_t = tcp->layS;
1616 pi[pino].poc.layno0 = tcp->lay_t;
1617 pi[pino].poc.layno1 = tcp->lay_t+1;
1624 tcp->prc_t = tcp->prcS;
1625 pi[pino].poc.precno0 = tcp->prc_t;
1626 pi[pino].poc.precno1 = tcp->prc_t+1;
1630 tcp->tx0_t = tcp->txS;
1631 tcp->ty0_t = tcp->tyS;
1632 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1633 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1634 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1635 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1636 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1637 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1645 for(i=tppos;i>=0;i--){
1648 pi[pino].poc.compno0 = tcp->comp_t-1;
1649 pi[pino].poc.compno1 = tcp->comp_t;
1652 pi[pino].poc.resno0 = tcp->res_t-1;
1653 pi[pino].poc.resno1 = tcp->res_t;
1656 pi[pino].poc.layno0 = tcp->lay_t-1;
1657 pi[pino].poc.layno1 = tcp->lay_t;
1663 pi[pino].poc.precno0 = tcp->prc_t-1;
1664 pi[pino].poc.precno1 = tcp->prc_t;
1667 pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1668 pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1669 pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1670 pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1678 if(tcp->res_t==tcp->resE){
1679 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1680 tcp->res_t = tcp->resS;
1681 pi[pino].poc.resno0 = tcp->res_t;
1682 pi[pino].poc.resno1 = tcp->res_t+1;
1689 pi[pino].poc.resno0 = tcp->res_t;
1690 pi[pino].poc.resno1 = tcp->res_t+1;
1696 if(tcp->comp_t ==tcp->compE){
1697 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1698 tcp->comp_t = tcp->compS;
1699 pi[pino].poc.compno0 = tcp->comp_t;
1700 pi[pino].poc.compno1 = tcp->comp_t+1;
1707 pi[pino].poc.compno0 = tcp->comp_t;
1708 pi[pino].poc.compno1 = tcp->comp_t+1;
1714 if(tcp->lay_t == tcp->layE){
1715 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1716 tcp->lay_t = tcp->layS;
1717 pi[pino].poc.layno0 = tcp->lay_t;
1718 pi[pino].poc.layno1 = tcp->lay_t+1;
1725 pi[pino].poc.layno0 = tcp->lay_t;
1726 pi[pino].poc.layno1 = tcp->lay_t+1;
1735 if(tcp->prc_t == tcp->prcE){
1736 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1737 tcp->prc_t = tcp->prcS;
1738 pi[pino].poc.precno0 = tcp->prc_t;
1739 pi[pino].poc.precno1 = tcp->prc_t+1;
1746 pi[pino].poc.precno0 = tcp->prc_t;
1747 pi[pino].poc.precno1 = tcp->prc_t+1;
1753 if(tcp->tx0_t >= tcp->txE){
1754 if(tcp->ty0_t >= tcp->tyE){
1755 if(opj_pi_check_next_level(i-1,cp,tileno,pino,prog)){
1756 tcp->ty0_t = tcp->tyS;
1757 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1758 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1759 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1760 incr_top=1;resetX=1;
1762 incr_top=0;resetX=0;
1765 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1766 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1767 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1768 incr_top=0;resetX=1;
1771 tcp->tx0_t = tcp->txS;
1772 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1773 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
1774 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1777 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1778 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx));
1779 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1792 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1793 OPJ_UINT32 p_nb_elements)
1795 OPJ_UINT32 compno, pino;
1796 opj_pi_iterator_t *l_current_pi = p_pi;
1798 if (p_pi->include) {
1799 opj_free(p_pi->include);
1802 for (pino = 0; pino < p_nb_elements; ++pino){
1803 if(l_current_pi->comps) {
1804 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1805 for (compno = 0; compno < l_current_pi->numcomps; compno++){
1806 if(l_current_component->resolutions) {
1807 opj_free(l_current_component->resolutions);
1808 l_current_component->resolutions = 00;
1811 ++l_current_component;
1813 opj_free(l_current_pi->comps);
1814 l_current_pi->comps = 0;
1824 void opj_pi_update_encoding_parameters( const opj_image_t *p_image,
1826 OPJ_UINT32 p_tile_no )
1828 /* encoding parameters to set */
1829 OPJ_UINT32 l_max_res;
1830 OPJ_UINT32 l_max_prec;
1831 OPJ_INT32 l_tx0,l_tx1,l_ty0,l_ty1;
1832 OPJ_UINT32 l_dx_min,l_dy_min;
1835 opj_tcp_t *l_tcp = 00;
1839 assert(p_image != 00);
1840 assert(p_tile_no < p_cp->tw * p_cp->th);
1842 l_tcp = &(p_cp->tcps[p_tile_no]);
1844 /* get encoding parameters */
1845 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);
1848 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);
1851 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);
1855 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi) {
1856 switch (pi->poc.prg) {
1858 return opj_pi_next_lrcp(pi);
1860 return opj_pi_next_rlcp(pi);
1862 return opj_pi_next_rpcl(pi);
1864 return opj_pi_next_pcrl(pi);
1866 return opj_pi_next_cprl(pi);
1867 case OPJ_PROG_UNKNOWN: