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 #define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32
41 #include "opj_includes.h"
43 /** @defgroup PI PI - Implementation of a packet iterator */
46 /** @name Local static functions */
50 Get next packet in layer-resolution-component-precinct order.
51 @param pi packet iterator to modify
52 @return returns false if pi pointed to the last packet or else returns true
54 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
56 Get next packet in resolution-layer-component-precinct order.
57 @param pi packet iterator to modify
58 @return returns false if pi pointed to the last packet or else returns true
60 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
62 Get next packet in resolution-precinct-component-layer order.
63 @param pi packet iterator to modify
64 @return returns false if pi pointed to the last packet or else returns true
66 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
68 Get next packet in precinct-component-resolution-layer order.
69 @param pi packet iterator to modify
70 @return returns false if pi pointed to the last packet or else returns true
72 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
74 Get next packet in component-precinct-resolution-layer order.
75 @param pi packet iterator to modify
76 @return returns false if pi pointed to the last packet or else returns true
78 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
81 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
83 * @param p_cp the coding parameters to modify
84 * @param p_tileno the tile index being concerned.
85 * @param p_tx0 X0 parameter for the tile
86 * @param p_tx1 X1 parameter for the tile
87 * @param p_ty0 Y0 parameter for the tile
88 * @param p_ty1 Y1 parameter for the tile
89 * @param p_max_prec the maximum precision for all the bands of the tile
90 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
91 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
92 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
94 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
100 OPJ_UINT32 p_max_prec,
101 OPJ_UINT32 p_max_res,
103 OPJ_UINT32 p_dy_min);
106 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
108 * @param p_cp the coding parameters to modify
109 * @param p_num_comps the number of components
110 * @param p_tileno the tile index being concerned.
111 * @param p_tx0 X0 parameter for the tile
112 * @param p_tx1 X1 parameter for the tile
113 * @param p_ty0 Y0 parameter for the tile
114 * @param p_ty1 Y1 parameter for the tile
115 * @param p_max_prec the maximum precision for all the bands of the tile
116 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
117 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
118 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
120 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
121 OPJ_UINT32 p_num_comps,
127 OPJ_UINT32 p_max_prec,
128 OPJ_UINT32 p_max_res,
130 OPJ_UINT32 p_dy_min);
132 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
134 * @param p_image the image being encoded.
135 * @param p_cp the coding parameters.
136 * @param tileno the tile index of the tile being encoded.
137 * @param p_tx0 pointer that will hold the X0 parameter for the tile
138 * @param p_tx1 pointer that will hold the X1 parameter for the tile
139 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
140 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
141 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
142 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
143 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
144 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
146 static void opj_get_encoding_parameters(const opj_image_t *p_image,
147 const opj_cp_t *p_cp,
153 OPJ_UINT32 * p_dx_min,
154 OPJ_UINT32 * p_dy_min,
155 OPJ_UINT32 * p_max_prec,
156 OPJ_UINT32 * p_max_res);
159 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
160 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
161 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
162 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
163 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
165 * @param p_image the image being encoded.
166 * @param p_cp the coding parameters.
167 * @param tileno the tile index of the tile being encoded.
168 * @param p_tx0 pointer that will hold the X0 parameter for the tile
169 * @param p_tx1 pointer that will hold the X1 parameter for the tile
170 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
171 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
172 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
173 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
174 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
175 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
176 * @param p_resolutions pointer to an area corresponding to the one described above.
178 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
179 const opj_cp_t *p_cp,
185 OPJ_UINT32 * p_dx_min,
186 OPJ_UINT32 * p_dy_min,
187 OPJ_UINT32 * p_max_prec,
188 OPJ_UINT32 * p_max_res,
189 OPJ_UINT32 ** p_resolutions);
191 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
192 * No other data is set. The include section of the packet iterator is not allocated.
194 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
195 * @param p_cp the coding parameters.
196 * @param tileno the index of the tile from which creating the packet iterator.
197 * @param manager Event manager
199 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
200 const opj_cp_t *p_cp,
202 opj_event_mgr_t* manager);
206 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
208 OPJ_UINT32 p_max_precision,
209 OPJ_UINT32 p_max_res);
213 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
215 OPJ_UINT32 p_max_precision,
216 OPJ_UINT32 p_max_res);
221 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
225 const OPJ_CHAR *prog);
232 ==========================================================
234 ==========================================================
237 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
239 opj_pi_comp_t *comp = NULL;
240 opj_pi_resolution_t *res = NULL;
241 OPJ_UINT32 index = 0;
243 if (pi->poc.compno0 >= pi->numcomps ||
244 pi->poc.compno1 >= pi->numcomps + 1) {
245 opj_event_msg(pi->manager, EVT_ERROR,
246 "opj_pi_next_lrcp(): invalid compno0/compno1\n");
251 comp = &pi->comps[pi->compno];
252 res = &comp->resolutions[pi->resno];
258 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
259 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
261 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
262 comp = &pi->comps[pi->compno];
263 if (pi->resno >= comp->numresolutions) {
266 res = &comp->resolutions[pi->resno];
268 pi->poc.precno1 = res->pw * res->ph;
270 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
271 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
272 pi->step_c + pi->precno * pi->step_p;
273 /* Avoids index out of bounds access with */
274 /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
275 /* https://github.com/uclouvain/openjpeg/issues/938 */
276 /* Not sure if this is the most clever fix. Perhaps */
277 /* include should be resized when a POC arises, or */
278 /* the POC should be rejected */
279 if (index >= pi->include_size) {
280 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
283 if (!pi->include[index]) {
284 pi->include[index] = 1;
297 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
299 opj_pi_comp_t *comp = NULL;
300 opj_pi_resolution_t *res = NULL;
301 OPJ_UINT32 index = 0;
303 if (pi->poc.compno0 >= pi->numcomps ||
304 pi->poc.compno1 >= pi->numcomps + 1) {
305 opj_event_msg(pi->manager, EVT_ERROR,
306 "opj_pi_next_rlcp(): invalid compno0/compno1\n");
311 comp = &pi->comps[pi->compno];
312 res = &comp->resolutions[pi->resno];
318 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
319 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
320 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
321 comp = &pi->comps[pi->compno];
322 if (pi->resno >= comp->numresolutions) {
325 res = &comp->resolutions[pi->resno];
327 pi->poc.precno1 = res->pw * res->ph;
329 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
330 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
331 pi->step_c + pi->precno * pi->step_p;
332 if (index >= pi->include_size) {
333 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
336 if (!pi->include[index]) {
337 pi->include[index] = 1;
350 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
352 opj_pi_comp_t *comp = NULL;
353 opj_pi_resolution_t *res = NULL;
354 OPJ_UINT32 index = 0;
356 if (pi->poc.compno0 >= pi->numcomps ||
357 pi->poc.compno1 >= pi->numcomps + 1) {
358 opj_event_msg(pi->manager, EVT_ERROR,
359 "opj_pi_next_rpcl(): invalid compno0/compno1\n");
366 OPJ_UINT32 compno, resno;
370 for (compno = 0; compno < pi->numcomps; compno++) {
371 comp = &pi->comps[compno];
372 for (resno = 0; resno < comp->numresolutions; resno++) {
374 res = &comp->resolutions[resno];
375 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
376 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
377 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
378 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
380 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
381 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
382 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
383 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
387 if (pi->dx == 0 || pi->dy == 0) {
392 pi->poc.ty0 = pi->ty0;
393 pi->poc.tx0 = pi->tx0;
394 pi->poc.ty1 = pi->ty1;
395 pi->poc.tx1 = pi->tx1;
397 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
398 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
399 pi->y += (pi->dy - (pi->y % pi->dy))) {
400 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
401 pi->x += (pi->dx - (pi->x % pi->dx))) {
402 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
404 OPJ_UINT32 trx0, try0;
405 OPJ_UINT32 trx1, try1;
407 OPJ_UINT32 prci, prcj;
408 comp = &pi->comps[pi->compno];
409 if (pi->resno >= comp->numresolutions) {
412 res = &comp->resolutions[pi->resno];
413 levelno = comp->numresolutions - 1 - pi->resno;
415 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
416 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
420 trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
421 ((OPJ_UINT64)comp->dx << levelno));
422 try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
423 ((OPJ_UINT64)comp->dy << levelno));
424 trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
425 ((OPJ_UINT64)comp->dx << levelno));
426 try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
427 ((OPJ_UINT64)comp->dy << levelno));
428 rpx = res->pdx + levelno;
429 rpy = res->pdy + levelno;
431 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
432 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
436 /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
437 if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
438 ((pi->y == pi->ty0) &&
439 (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
442 if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
443 ((pi->x == pi->tx0) &&
444 (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
448 if ((res->pw == 0) || (res->ph == 0)) {
452 if ((trx0 == trx1) || (try0 == try1)) {
456 prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
457 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
458 - opj_uint_floordivpow2(trx0, res->pdx);
459 prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
460 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
461 - opj_uint_floordivpow2(try0, res->pdy);
462 pi->precno = prci + prcj * res->pw;
463 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
464 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
465 pi->step_c + pi->precno * pi->step_p;
466 if (index >= pi->include_size) {
467 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
470 if (!pi->include[index]) {
471 pi->include[index] = 1;
485 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
487 opj_pi_comp_t *comp = NULL;
488 opj_pi_resolution_t *res = NULL;
489 OPJ_UINT32 index = 0;
491 if (pi->poc.compno0 >= pi->numcomps ||
492 pi->poc.compno1 >= pi->numcomps + 1) {
493 opj_event_msg(pi->manager, EVT_ERROR,
494 "opj_pi_next_pcrl(): invalid compno0/compno1\n");
499 comp = &pi->comps[pi->compno];
502 OPJ_UINT32 compno, resno;
506 for (compno = 0; compno < pi->numcomps; compno++) {
507 comp = &pi->comps[compno];
508 for (resno = 0; resno < comp->numresolutions; resno++) {
510 res = &comp->resolutions[resno];
511 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
512 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
513 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
514 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
516 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
517 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
518 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
519 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
523 if (pi->dx == 0 || pi->dy == 0) {
528 pi->poc.ty0 = pi->ty0;
529 pi->poc.tx0 = pi->tx0;
530 pi->poc.ty1 = pi->ty1;
531 pi->poc.tx1 = pi->tx1;
533 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
534 pi->y += (pi->dy - (pi->y % pi->dy))) {
535 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
536 pi->x += (pi->dx - (pi->x % pi->dx))) {
537 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
538 comp = &pi->comps[pi->compno];
539 for (pi->resno = pi->poc.resno0;
540 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
542 OPJ_UINT32 trx0, try0;
543 OPJ_UINT32 trx1, try1;
545 OPJ_UINT32 prci, prcj;
546 res = &comp->resolutions[pi->resno];
547 levelno = comp->numresolutions - 1 - pi->resno;
549 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
550 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
554 trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
555 ((OPJ_UINT64)comp->dx << levelno));
556 try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
557 ((OPJ_UINT64)comp->dy << levelno));
558 trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
559 ((OPJ_UINT64)comp->dx << levelno));
560 try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
561 ((OPJ_UINT64)comp->dy << levelno));
562 rpx = res->pdx + levelno;
563 rpy = res->pdy + levelno;
565 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
566 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
570 /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
571 if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
572 ((pi->y == pi->ty0) &&
573 (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
576 if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
577 ((pi->x == pi->tx0) &&
578 (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
582 if ((res->pw == 0) || (res->ph == 0)) {
586 if ((trx0 == trx1) || (try0 == try1)) {
590 prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
591 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
592 - opj_uint_floordivpow2(trx0, res->pdx);
593 prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
594 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
595 - opj_uint_floordivpow2(try0, res->pdy);
596 pi->precno = prci + prcj * res->pw;
597 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
598 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
599 pi->step_c + pi->precno * pi->step_p;
600 if (index >= pi->include_size) {
601 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
604 if (!pi->include[index]) {
605 pi->include[index] = 1;
619 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
621 opj_pi_comp_t *comp = NULL;
622 opj_pi_resolution_t *res = NULL;
623 OPJ_UINT32 index = 0;
625 if (pi->poc.compno0 >= pi->numcomps ||
626 pi->poc.compno1 >= pi->numcomps + 1) {
627 opj_event_msg(pi->manager, EVT_ERROR,
628 "opj_pi_next_cprl(): invalid compno0/compno1\n");
633 comp = &pi->comps[pi->compno];
639 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
641 comp = &pi->comps[pi->compno];
644 for (resno = 0; resno < comp->numresolutions; resno++) {
646 res = &comp->resolutions[resno];
647 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
648 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
649 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
650 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
652 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
653 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
654 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
655 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
658 if (pi->dx == 0 || pi->dy == 0) {
662 pi->poc.ty0 = pi->ty0;
663 pi->poc.tx0 = pi->tx0;
664 pi->poc.ty1 = pi->ty1;
665 pi->poc.tx1 = pi->tx1;
667 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
668 pi->y += (pi->dy - (pi->y % pi->dy))) {
669 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
670 pi->x += (pi->dx - (pi->x % pi->dx))) {
671 for (pi->resno = pi->poc.resno0;
672 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
674 OPJ_UINT32 trx0, try0;
675 OPJ_UINT32 trx1, try1;
677 OPJ_UINT32 prci, prcj;
678 res = &comp->resolutions[pi->resno];
679 levelno = comp->numresolutions - 1 - pi->resno;
681 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
682 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
686 trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
687 ((OPJ_UINT64)comp->dx << levelno));
688 try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
689 ((OPJ_UINT64)comp->dy << levelno));
690 trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
691 ((OPJ_UINT64)comp->dx << levelno));
692 try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
693 ((OPJ_UINT64)comp->dy << levelno));
694 rpx = res->pdx + levelno;
695 rpy = res->pdy + levelno;
697 if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
698 (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
702 /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
703 if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
704 ((pi->y == pi->ty0) &&
705 (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
708 if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
709 ((pi->x == pi->tx0) &&
710 (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
714 if ((res->pw == 0) || (res->ph == 0)) {
718 if ((trx0 == trx1) || (try0 == try1)) {
722 prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
723 ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
724 - opj_uint_floordivpow2(trx0, res->pdx);
725 prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
726 ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
727 - opj_uint_floordivpow2(try0, res->pdy);
728 pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
729 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
730 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
731 pi->step_c + pi->precno * pi->step_p;
732 if (index >= pi->include_size) {
733 opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
736 if (!pi->include[index]) {
737 pi->include[index] = 1;
751 static void opj_get_encoding_parameters(const opj_image_t *p_image,
752 const opj_cp_t *p_cp,
758 OPJ_UINT32 * p_dx_min,
759 OPJ_UINT32 * p_dy_min,
760 OPJ_UINT32 * p_max_prec,
761 OPJ_UINT32 * p_max_res)
764 OPJ_UINT32 compno, resno;
766 const opj_tcp_t *l_tcp = 00;
767 const opj_tccp_t * l_tccp = 00;
768 const opj_image_comp_t * l_img_comp = 00;
770 /* position in x and y of tile */
773 /* non-corrected (in regard to image offset) tile offset */
774 OPJ_UINT32 l_tx0, l_ty0;
778 assert(p_image != 00);
779 assert(p_tileno < p_cp->tw * p_cp->th);
781 /* initializations */
782 l_tcp = &p_cp->tcps [p_tileno];
783 l_img_comp = p_image->comps;
784 l_tccp = l_tcp->tccps;
786 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
787 p = p_tileno % p_cp->tw;
788 q = p_tileno / p_cp->tw;
790 /* find extent of tile */
791 l_tx0 = p_cp->tx0 + p *
792 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
793 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
794 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
795 l_ty0 = p_cp->ty0 + q *
796 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
797 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
798 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
800 /* max precision is 0 (can only grow) */
804 /* take the largest value for dx_min and dy_min */
805 *p_dx_min = 0x7fffffff;
806 *p_dy_min = 0x7fffffff;
808 for (compno = 0; compno < p_image->numcomps; ++compno) {
809 /* arithmetic variables to calculate */
810 OPJ_UINT32 l_level_no;
811 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
812 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
813 OPJ_UINT32 l_pdx, l_pdy;
814 OPJ_UINT32 l_pw, l_ph;
815 OPJ_UINT32 l_product;
816 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
818 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
819 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
820 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
821 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
823 if (l_tccp->numresolutions > *p_max_res) {
824 *p_max_res = l_tccp->numresolutions;
827 /* use custom size for precincts */
828 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
829 OPJ_UINT64 l_dx, l_dy;
831 /* precinct width and height */
832 l_pdx = l_tccp->prcw[resno];
833 l_pdy = l_tccp->prch[resno];
835 l_dx = l_img_comp->dx * ((OPJ_UINT64)1u << (l_pdx + l_tccp->numresolutions - 1 -
837 l_dy = l_img_comp->dy * ((OPJ_UINT64)1u << (l_pdy + l_tccp->numresolutions - 1 -
840 /* take the minimum size for dx for each comp and resolution */
841 if (l_dx <= UINT_MAX) {
842 *p_dx_min = opj_uint_min(*p_dx_min, (OPJ_UINT32)l_dx);
844 if (l_dy <= UINT_MAX) {
845 *p_dy_min = opj_uint_min(*p_dy_min, (OPJ_UINT32)l_dy);
848 /* various calculations of extents */
849 l_level_no = l_tccp->numresolutions - 1 - resno;
851 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
852 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
853 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
854 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
856 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
857 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
858 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
860 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
862 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
863 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
865 l_product = l_pw * l_ph;
867 /* update precision */
868 if (l_product > *p_max_prec) {
869 *p_max_prec = l_product;
878 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
879 const opj_cp_t *p_cp,
885 OPJ_UINT32 * p_dx_min,
886 OPJ_UINT32 * p_dy_min,
887 OPJ_UINT32 * p_max_prec,
888 OPJ_UINT32 * p_max_res,
889 OPJ_UINT32 ** p_resolutions)
892 OPJ_UINT32 compno, resno;
895 const opj_tcp_t *tcp = 00;
896 const opj_tccp_t * l_tccp = 00;
897 const opj_image_comp_t * l_img_comp = 00;
899 /* to store l_dx, l_dy, w and h for each resolution and component.*/
900 OPJ_UINT32 * lResolutionPtr;
902 /* position in x and y of tile*/
905 /* non-corrected (in regard to image offset) tile offset */
906 OPJ_UINT32 l_tx0, l_ty0;
908 /* preconditions in debug*/
910 assert(p_image != 00);
911 assert(tileno < p_cp->tw * p_cp->th);
914 tcp = &p_cp->tcps [tileno];
916 l_img_comp = p_image->comps;
918 /* position in x and y of tile*/
919 p = tileno % p_cp->tw;
920 q = tileno / p_cp->tw;
922 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
923 l_tx0 = p_cp->tx0 + p *
924 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
925 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
926 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
927 l_ty0 = p_cp->ty0 + q *
928 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
929 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
930 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
932 /* max precision and resolution is 0 (can only grow)*/
936 /* take the largest value for dx_min and dy_min*/
937 *p_dx_min = 0x7fffffff;
938 *p_dy_min = 0x7fffffff;
940 for (compno = 0; compno < p_image->numcomps; ++compno) {
941 /* arithmetic variables to calculate*/
942 OPJ_UINT32 l_level_no;
943 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
944 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
945 OPJ_UINT32 l_product;
946 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
947 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
949 lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
951 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
952 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
953 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
954 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
956 if (l_tccp->numresolutions > *p_max_res) {
957 *p_max_res = l_tccp->numresolutions;
960 /* use custom size for precincts*/
961 l_level_no = l_tccp->numresolutions;
962 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
963 OPJ_UINT32 l_dx, l_dy;
967 /* precinct width and height*/
968 l_pdx = l_tccp->prcw[resno];
969 l_pdy = l_tccp->prch[resno];
970 if (lResolutionPtr) {
971 *lResolutionPtr++ = l_pdx;
972 *lResolutionPtr++ = l_pdy;
974 if (l_pdx + l_level_no < 32 &&
975 l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
976 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
977 /* take the minimum size for l_dx for each comp and resolution*/
978 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
980 if (l_pdy + l_level_no < 32 &&
981 l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
982 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
983 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
986 /* various calculations of extents*/
987 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
988 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
989 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
990 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
991 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
992 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
993 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
994 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
995 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
996 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
997 if (lResolutionPtr) {
998 *lResolutionPtr++ = l_pw;
999 *lResolutionPtr++ = l_ph;
1001 l_product = l_pw * l_ph;
1003 /* update precision*/
1004 if (l_product > *p_max_prec) {
1005 *p_max_prec = l_product;
1014 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
1017 opj_event_mgr_t* manager)
1020 OPJ_UINT32 pino, compno;
1021 /* number of poc in the p_pi*/
1022 OPJ_UINT32 l_poc_bound;
1024 /* pointers to tile coding parameters and components.*/
1025 opj_pi_iterator_t *l_pi = 00;
1026 opj_tcp_t *tcp = 00;
1027 const opj_tccp_t *tccp = 00;
1029 /* current packet iterator being allocated*/
1030 opj_pi_iterator_t *l_current_pi = 00;
1032 /* preconditions in debug*/
1034 assert(image != 00);
1035 assert(tileno < cp->tw * cp->th);
1037 /* initializations*/
1038 tcp = &cp->tcps[tileno];
1039 l_poc_bound = tcp->numpocs + 1;
1041 /* memory allocations*/
1042 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1043 sizeof(opj_pi_iterator_t));
1048 l_current_pi = l_pi;
1049 for (pino = 0; pino < l_poc_bound ; ++pino) {
1051 l_current_pi->manager = manager;
1053 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1054 sizeof(opj_pi_comp_t));
1055 if (! l_current_pi->comps) {
1056 opj_pi_destroy(l_pi, l_poc_bound);
1060 l_current_pi->numcomps = image->numcomps;
1062 for (compno = 0; compno < image->numcomps; ++compno) {
1063 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1065 tccp = &tcp->tccps[compno];
1067 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1068 sizeof(opj_pi_resolution_t));
1069 if (!comp->resolutions) {
1070 opj_pi_destroy(l_pi, l_poc_bound);
1074 comp->numresolutions = tccp->numresolutions;
1081 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1082 OPJ_UINT32 p_tileno,
1087 OPJ_UINT32 p_max_prec,
1088 OPJ_UINT32 p_max_res,
1089 OPJ_UINT32 p_dx_min,
1090 OPJ_UINT32 p_dy_min)
1094 /* tile coding parameter*/
1095 opj_tcp_t *l_tcp = 00;
1096 /* current poc being updated*/
1097 opj_poc_t * l_current_poc = 00;
1100 OPJ_UINT32 l_poc_bound;
1102 OPJ_ARG_NOT_USED(p_max_res);
1104 /* preconditions in debug*/
1106 assert(p_tileno < p_cp->tw * p_cp->th);
1108 /* initializations*/
1109 l_tcp = &p_cp->tcps [p_tileno];
1110 /* number of iterations in the loop */
1111 l_poc_bound = l_tcp->numpocs + 1;
1113 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1114 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1115 l_current_poc = l_tcp->pocs;
1117 l_current_poc->compS = l_current_poc->compno0;
1118 l_current_poc->compE = l_current_poc->compno1;
1119 l_current_poc->resS = l_current_poc->resno0;
1120 l_current_poc->resE = l_current_poc->resno1;
1121 l_current_poc->layE = l_current_poc->layno1;
1123 /* special treatment for the first element*/
1124 l_current_poc->layS = 0;
1125 l_current_poc->prg = l_current_poc->prg1;
1126 l_current_poc->prcS = 0;
1128 l_current_poc->prcE = p_max_prec;
1129 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1130 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1131 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1132 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1133 l_current_poc->dx = p_dx_min;
1134 l_current_poc->dy = p_dy_min;
1137 for (pino = 1; pino < l_poc_bound ; ++pino) {
1138 l_current_poc->compS = l_current_poc->compno0;
1139 l_current_poc->compE = l_current_poc->compno1;
1140 l_current_poc->resS = l_current_poc->resno0;
1141 l_current_poc->resE = l_current_poc->resno1;
1142 l_current_poc->layE = l_current_poc->layno1;
1143 l_current_poc->prg = l_current_poc->prg1;
1144 l_current_poc->prcS = 0;
1145 /* special treatment here different from the first element*/
1146 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1147 l_current_poc->layE : 0;
1149 l_current_poc->prcE = p_max_prec;
1150 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1151 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1152 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1153 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1154 l_current_poc->dx = p_dx_min;
1155 l_current_poc->dy = p_dy_min;
1160 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1161 OPJ_UINT32 p_num_comps,
1162 OPJ_UINT32 p_tileno,
1167 OPJ_UINT32 p_max_prec,
1168 OPJ_UINT32 p_max_res,
1169 OPJ_UINT32 p_dx_min,
1170 OPJ_UINT32 p_dy_min)
1174 /* tile coding parameter*/
1175 opj_tcp_t *l_tcp = 00;
1176 /* current poc being updated*/
1177 opj_poc_t * l_current_poc = 00;
1179 OPJ_UINT32 l_poc_bound;
1181 /* preconditions in debug*/
1183 assert(p_tileno < p_cp->tw * p_cp->th);
1185 /* initializations*/
1186 l_tcp = &p_cp->tcps [p_tileno];
1188 /* number of iterations in the loop */
1189 l_poc_bound = l_tcp->numpocs + 1;
1191 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1192 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1193 l_current_poc = l_tcp->pocs;
1195 for (pino = 0; pino < l_poc_bound ; ++pino) {
1196 l_current_poc->compS = 0;
1197 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1198 l_current_poc->resS = 0;
1199 l_current_poc->resE = p_max_res;
1200 l_current_poc->layS = 0;
1201 l_current_poc->layE = l_tcp->numlayers;
1202 l_current_poc->prg = l_tcp->prg;
1203 l_current_poc->prcS = 0;
1204 l_current_poc->prcE = p_max_prec;
1205 l_current_poc->txS = p_tx0;
1206 l_current_poc->txE = p_tx1;
1207 l_current_poc->tyS = p_ty0;
1208 l_current_poc->tyE = p_ty1;
1209 l_current_poc->dx = p_dx_min;
1210 l_current_poc->dy = p_dy_min;
1215 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1217 OPJ_UINT32 p_max_precision,
1218 OPJ_UINT32 p_max_res)
1223 /* encoding parameters to set*/
1226 opj_pi_iterator_t * l_current_pi = 00;
1227 opj_poc_t* l_current_poc = 0;
1229 OPJ_ARG_NOT_USED(p_max_res);
1231 /* preconditions in debug*/
1233 assert(p_tcp != 00);
1235 /* initializations*/
1236 l_bound = p_tcp->numpocs + 1;
1237 l_current_pi = p_pi;
1238 l_current_poc = p_tcp->pocs;
1240 for (pino = 0; pino < l_bound; ++pino) {
1241 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1242 l_current_pi->first = 1;
1244 l_current_pi->poc.resno0 =
1245 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1246 l_current_pi->poc.compno0 =
1247 l_current_poc->compno0; /* Component Index #0 (Start) */
1248 l_current_pi->poc.layno0 = 0;
1249 l_current_pi->poc.precno0 = 0;
1250 l_current_pi->poc.resno1 =
1251 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1252 l_current_pi->poc.compno1 =
1253 l_current_poc->compno1; /* Component Index #0 (End) */
1254 l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1255 p_tcp->numlayers); /* Layer Index #0 (End) */
1256 l_current_pi->poc.precno1 = p_max_precision;
1262 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1264 OPJ_UINT32 p_max_precision,
1265 OPJ_UINT32 p_max_res)
1270 /* encoding parameters to set*/
1273 opj_pi_iterator_t * l_current_pi = 00;
1274 /* preconditions in debug*/
1275 assert(p_tcp != 00);
1278 /* initializations*/
1279 l_bound = p_tcp->numpocs + 1;
1280 l_current_pi = p_pi;
1282 for (pino = 0; pino < l_bound; ++pino) {
1283 l_current_pi->poc.prg = p_tcp->prg;
1284 l_current_pi->first = 1;
1285 l_current_pi->poc.resno0 = 0;
1286 l_current_pi->poc.compno0 = 0;
1287 l_current_pi->poc.layno0 = 0;
1288 l_current_pi->poc.precno0 = 0;
1289 l_current_pi->poc.resno1 = p_max_res;
1290 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1291 l_current_pi->poc.layno1 = p_tcp->numlayers;
1292 l_current_pi->poc.precno1 = p_max_precision;
1299 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1303 const OPJ_CHAR *prog)
1306 opj_tcp_t *tcps = &cp->tcps[tileno];
1307 opj_poc_t *tcp = &tcps->pocs[pino];
1310 for (i = pos; i >= 0; i--) {
1313 if (tcp->res_t == tcp->resE) {
1314 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1324 if (tcp->comp_t == tcp->compE) {
1325 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1335 if (tcp->lay_t == tcp->layE) {
1336 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1347 case OPJ_LRCP: /* fall through */
1349 if (tcp->prc_t == tcp->prcE) {
1350 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1360 if (tcp->tx0_t == tcp->txE) {
1362 if (tcp->ty0_t == tcp->tyE) {
1363 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1384 ==========================================================
1385 Packet iterator interface
1386 ==========================================================
1388 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1390 OPJ_UINT32 p_tile_no,
1391 opj_event_mgr_t* manager)
1393 OPJ_UINT32 numcomps = p_image->numcomps;
1397 OPJ_UINT32 compno, resno;
1399 /* to store w, h, dx and dy for all components and resolutions */
1400 OPJ_UINT32 * l_tmp_data;
1401 OPJ_UINT32 ** l_tmp_ptr;
1403 /* encoding parameters to set */
1404 OPJ_UINT32 l_max_res;
1405 OPJ_UINT32 l_max_prec;
1406 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1407 OPJ_UINT32 l_dx_min, l_dy_min;
1409 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1410 OPJ_UINT32 l_data_stride;
1413 opj_pi_iterator_t *l_pi = 00;
1414 opj_tcp_t *l_tcp = 00;
1415 const opj_tccp_t *l_tccp = 00;
1416 opj_pi_comp_t *l_current_comp = 00;
1417 opj_image_comp_t * l_img_comp = 00;
1418 opj_pi_iterator_t * l_current_pi = 00;
1419 OPJ_UINT32 * l_encoding_value_ptr = 00;
1421 /* preconditions in debug */
1423 assert(p_image != 00);
1424 assert(p_tile_no < p_cp->tw * p_cp->th);
1426 /* initializations */
1427 l_tcp = &p_cp->tcps[p_tile_no];
1428 l_bound = l_tcp->numpocs + 1;
1430 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1431 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1432 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1437 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1438 numcomps * sizeof(OPJ_UINT32 *));
1441 opj_free(l_tmp_data);
1445 /* memory allocation for pi */
1446 l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1448 opj_free(l_tmp_data);
1449 opj_free(l_tmp_ptr);
1453 l_encoding_value_ptr = l_tmp_data;
1454 /* update pointer array */
1456 (compno = 0; compno < numcomps; ++compno) {
1457 l_tmp_ptr[compno] = l_encoding_value_ptr;
1458 l_encoding_value_ptr += l_data_stride;
1460 /* get encoding parameters */
1461 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1462 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1464 /* step calculations */
1466 l_step_c = l_max_prec * l_step_p;
1467 l_step_r = numcomps * l_step_c;
1468 l_step_l = l_max_res * l_step_r;
1470 /* set values for first packet iterator */
1471 l_current_pi = l_pi;
1473 /* memory allocation for include */
1474 /* prevent an integer overflow issue */
1475 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1476 l_current_pi->include = 00;
1477 if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1478 l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1479 l_current_pi->include = (OPJ_INT16*) opj_calloc(
1480 l_current_pi->include_size, sizeof(OPJ_INT16));
1483 if (!l_current_pi->include) {
1484 opj_free(l_tmp_data);
1485 opj_free(l_tmp_ptr);
1486 opj_pi_destroy(l_pi, l_bound);
1490 /* special treatment for the first packet iterator */
1491 l_current_comp = l_current_pi->comps;
1492 l_img_comp = p_image->comps;
1493 l_tccp = l_tcp->tccps;
1495 l_current_pi->tx0 = l_tx0;
1496 l_current_pi->ty0 = l_ty0;
1497 l_current_pi->tx1 = l_tx1;
1498 l_current_pi->ty1 = l_ty1;
1500 /*l_current_pi->dx = l_img_comp->dx;*/
1501 /*l_current_pi->dy = l_img_comp->dy;*/
1503 l_current_pi->step_p = l_step_p;
1504 l_current_pi->step_c = l_step_c;
1505 l_current_pi->step_r = l_step_r;
1506 l_current_pi->step_l = l_step_l;
1508 /* allocation for components and number of components has already been calculated by opj_pi_create */
1510 (compno = 0; compno < numcomps; ++compno) {
1511 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1512 l_encoding_value_ptr = l_tmp_ptr[compno];
1514 l_current_comp->dx = l_img_comp->dx;
1515 l_current_comp->dy = l_img_comp->dy;
1516 /* resolutions have already been initialized */
1518 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1519 l_res->pdx = *(l_encoding_value_ptr++);
1520 l_res->pdy = *(l_encoding_value_ptr++);
1521 l_res->pw = *(l_encoding_value_ptr++);
1522 l_res->ph = *(l_encoding_value_ptr++);
1531 for (pino = 1 ; pino < l_bound ; ++pino) {
1532 l_current_comp = l_current_pi->comps;
1533 l_img_comp = p_image->comps;
1534 l_tccp = l_tcp->tccps;
1536 l_current_pi->tx0 = l_tx0;
1537 l_current_pi->ty0 = l_ty0;
1538 l_current_pi->tx1 = l_tx1;
1539 l_current_pi->ty1 = l_ty1;
1540 /*l_current_pi->dx = l_dx_min;*/
1541 /*l_current_pi->dy = l_dy_min;*/
1542 l_current_pi->step_p = l_step_p;
1543 l_current_pi->step_c = l_step_c;
1544 l_current_pi->step_r = l_step_r;
1545 l_current_pi->step_l = l_step_l;
1547 /* allocation for components and number of components has already been calculated by opj_pi_create */
1549 (compno = 0; compno < numcomps; ++compno) {
1550 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1551 l_encoding_value_ptr = l_tmp_ptr[compno];
1553 l_current_comp->dx = l_img_comp->dx;
1554 l_current_comp->dy = l_img_comp->dy;
1555 /* resolutions have already been initialized */
1557 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1558 l_res->pdx = *(l_encoding_value_ptr++);
1559 l_res->pdy = *(l_encoding_value_ptr++);
1560 l_res->pw = *(l_encoding_value_ptr++);
1561 l_res->ph = *(l_encoding_value_ptr++);
1568 /* special treatment*/
1569 l_current_pi->include = (l_current_pi - 1)->include;
1570 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1573 opj_free(l_tmp_data);
1575 opj_free(l_tmp_ptr);
1579 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1581 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1587 OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
1588 const opj_cp_t *p_cp,
1589 OPJ_UINT32 p_tile_no)
1591 OPJ_UINT32 l_max_res;
1592 OPJ_UINT32 l_max_prec;
1593 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1594 OPJ_UINT32 l_dx_min, l_dy_min;
1596 /* preconditions in debug*/
1598 assert(p_image != 00);
1599 assert(p_tile_no < p_cp->tw * p_cp->th);
1601 /* get encoding parameters*/
1602 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1603 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
1605 return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
1610 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1612 OPJ_UINT32 p_tile_no,
1613 J2K_T2_MODE p_t2_mode,
1614 opj_event_mgr_t* manager)
1616 OPJ_UINT32 numcomps = p_image->numcomps;
1620 OPJ_UINT32 compno, resno;
1622 /* to store w, h, dx and dy for all components and resolutions*/
1623 OPJ_UINT32 * l_tmp_data;
1624 OPJ_UINT32 ** l_tmp_ptr;
1626 /* encoding parameters to set*/
1627 OPJ_UINT32 l_max_res;
1628 OPJ_UINT32 l_max_prec;
1629 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1630 OPJ_UINT32 l_dx_min, l_dy_min;
1632 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1633 OPJ_UINT32 l_data_stride;
1636 opj_pi_iterator_t *l_pi = 00;
1637 opj_tcp_t *l_tcp = 00;
1638 const opj_tccp_t *l_tccp = 00;
1639 opj_pi_comp_t *l_current_comp = 00;
1640 opj_image_comp_t * l_img_comp = 00;
1641 opj_pi_iterator_t * l_current_pi = 00;
1642 OPJ_UINT32 * l_encoding_value_ptr = 00;
1644 /* preconditions in debug*/
1646 assert(p_image != 00);
1647 assert(p_tile_no < p_cp->tw * p_cp->th);
1649 /* initializations*/
1650 l_tcp = &p_cp->tcps[p_tile_no];
1651 l_bound = l_tcp->numpocs + 1;
1653 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1654 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1655 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1660 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1661 numcomps * sizeof(OPJ_UINT32 *));
1663 opj_free(l_tmp_data);
1667 /* memory allocation for pi*/
1668 l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
1670 opj_free(l_tmp_data);
1671 opj_free(l_tmp_ptr);
1675 l_encoding_value_ptr = l_tmp_data;
1676 /* update pointer array*/
1677 for (compno = 0; compno < numcomps; ++compno) {
1678 l_tmp_ptr[compno] = l_encoding_value_ptr;
1679 l_encoding_value_ptr += l_data_stride;
1682 /* get encoding parameters*/
1683 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1684 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1686 /* step calculations*/
1688 l_step_c = l_max_prec * l_step_p;
1689 l_step_r = numcomps * l_step_c;
1690 l_step_l = l_max_res * l_step_r;
1692 /* set values for first packet iterator*/
1693 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1694 l_current_pi = l_pi;
1696 /* memory allocation for include*/
1697 l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1698 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1700 if (!l_current_pi->include) {
1701 opj_free(l_tmp_data);
1702 opj_free(l_tmp_ptr);
1703 opj_pi_destroy(l_pi, l_bound);
1707 /* special treatment for the first packet iterator*/
1708 l_current_comp = l_current_pi->comps;
1709 l_img_comp = p_image->comps;
1710 l_tccp = l_tcp->tccps;
1711 l_current_pi->tx0 = l_tx0;
1712 l_current_pi->ty0 = l_ty0;
1713 l_current_pi->tx1 = l_tx1;
1714 l_current_pi->ty1 = l_ty1;
1715 l_current_pi->dx = l_dx_min;
1716 l_current_pi->dy = l_dy_min;
1717 l_current_pi->step_p = l_step_p;
1718 l_current_pi->step_c = l_step_c;
1719 l_current_pi->step_r = l_step_r;
1720 l_current_pi->step_l = l_step_l;
1722 /* allocation for components and number of components has already been calculated by opj_pi_create */
1723 for (compno = 0; compno < numcomps; ++compno) {
1724 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1725 l_encoding_value_ptr = l_tmp_ptr[compno];
1727 l_current_comp->dx = l_img_comp->dx;
1728 l_current_comp->dy = l_img_comp->dy;
1730 /* resolutions have already been initialized */
1731 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1732 l_res->pdx = *(l_encoding_value_ptr++);
1733 l_res->pdy = *(l_encoding_value_ptr++);
1734 l_res->pw = *(l_encoding_value_ptr++);
1735 l_res->ph = *(l_encoding_value_ptr++);
1745 for (pino = 1 ; pino < l_bound ; ++pino) {
1746 l_current_comp = l_current_pi->comps;
1747 l_img_comp = p_image->comps;
1748 l_tccp = l_tcp->tccps;
1750 l_current_pi->tx0 = l_tx0;
1751 l_current_pi->ty0 = l_ty0;
1752 l_current_pi->tx1 = l_tx1;
1753 l_current_pi->ty1 = l_ty1;
1754 l_current_pi->dx = l_dx_min;
1755 l_current_pi->dy = l_dy_min;
1756 l_current_pi->step_p = l_step_p;
1757 l_current_pi->step_c = l_step_c;
1758 l_current_pi->step_r = l_step_r;
1759 l_current_pi->step_l = l_step_l;
1761 /* allocation for components and number of components has already been calculated by opj_pi_create */
1762 for (compno = 0; compno < numcomps; ++compno) {
1763 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1764 l_encoding_value_ptr = l_tmp_ptr[compno];
1766 l_current_comp->dx = l_img_comp->dx;
1767 l_current_comp->dy = l_img_comp->dy;
1768 /* resolutions have already been initialized */
1769 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1770 l_res->pdx = *(l_encoding_value_ptr++);
1771 l_res->pdy = *(l_encoding_value_ptr++);
1772 l_res->pw = *(l_encoding_value_ptr++);
1773 l_res->ph = *(l_encoding_value_ptr++);
1781 /* special treatment*/
1782 l_current_pi->include = (l_current_pi - 1)->include;
1783 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1787 opj_free(l_tmp_data);
1789 opj_free(l_tmp_ptr);
1792 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1793 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1794 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1796 opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1797 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1803 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1809 J2K_T2_MODE t2_mode)
1811 const OPJ_CHAR *prog;
1813 OPJ_UINT32 incr_top = 1, resetX = 0;
1814 opj_tcp_t *tcps = &cp->tcps[tileno];
1815 opj_poc_t *tcp = &tcps->pocs[pino];
1817 prog = opj_j2k_convert_progression_order(tcp->prg);
1820 pi[pino].poc.prg = tcp->prg;
1822 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1823 !OPJ_IS_IMF(cp->rsiz) &&
1824 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
1825 pi[pino].poc.resno0 = tcp->resS;
1826 pi[pino].poc.resno1 = tcp->resE;
1827 pi[pino].poc.compno0 = tcp->compS;
1828 pi[pino].poc.compno1 = tcp->compE;
1829 pi[pino].poc.layno0 = tcp->layS;
1830 pi[pino].poc.layno1 = tcp->layE;
1831 pi[pino].poc.precno0 = tcp->prcS;
1832 pi[pino].poc.precno1 = tcp->prcE;
1833 pi[pino].poc.tx0 = tcp->txS;
1834 pi[pino].poc.ty0 = tcp->tyS;
1835 pi[pino].poc.tx1 = tcp->txE;
1836 pi[pino].poc.ty1 = tcp->tyE;
1838 for (i = tppos + 1; i < 4; i++) {
1841 pi[pino].poc.resno0 = tcp->resS;
1842 pi[pino].poc.resno1 = tcp->resE;
1845 pi[pino].poc.compno0 = tcp->compS;
1846 pi[pino].poc.compno1 = tcp->compE;
1849 pi[pino].poc.layno0 = tcp->layS;
1850 pi[pino].poc.layno1 = tcp->layE;
1856 pi[pino].poc.precno0 = tcp->prcS;
1857 pi[pino].poc.precno1 = tcp->prcE;
1860 pi[pino].poc.tx0 = tcp->txS;
1861 pi[pino].poc.ty0 = tcp->tyS;
1862 pi[pino].poc.tx1 = tcp->txE;
1863 pi[pino].poc.ty1 = tcp->tyE;
1871 for (i = tppos; i >= 0; i--) {
1874 tcp->comp_t = tcp->compS;
1875 pi[pino].poc.compno0 = tcp->comp_t;
1876 pi[pino].poc.compno1 = tcp->comp_t + 1;
1880 tcp->res_t = tcp->resS;
1881 pi[pino].poc.resno0 = tcp->res_t;
1882 pi[pino].poc.resno1 = tcp->res_t + 1;
1886 tcp->lay_t = tcp->layS;
1887 pi[pino].poc.layno0 = tcp->lay_t;
1888 pi[pino].poc.layno1 = tcp->lay_t + 1;
1895 tcp->prc_t = tcp->prcS;
1896 pi[pino].poc.precno0 = tcp->prc_t;
1897 pi[pino].poc.precno1 = tcp->prc_t + 1;
1901 tcp->tx0_t = tcp->txS;
1902 tcp->ty0_t = tcp->tyS;
1903 pi[pino].poc.tx0 = tcp->tx0_t;
1904 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1905 pi[pino].poc.ty0 = tcp->ty0_t;
1906 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1907 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1908 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1916 for (i = tppos; i >= 0; i--) {
1919 pi[pino].poc.compno0 = tcp->comp_t - 1;
1920 pi[pino].poc.compno1 = tcp->comp_t;
1923 pi[pino].poc.resno0 = tcp->res_t - 1;
1924 pi[pino].poc.resno1 = tcp->res_t;
1927 pi[pino].poc.layno0 = tcp->lay_t - 1;
1928 pi[pino].poc.layno1 = tcp->lay_t;
1934 pi[pino].poc.precno0 = tcp->prc_t - 1;
1935 pi[pino].poc.precno1 = tcp->prc_t;
1938 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1939 pi[pino].poc.tx1 = tcp->tx0_t ;
1940 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1941 pi[pino].poc.ty1 = tcp->ty0_t ;
1946 if (incr_top == 1) {
1949 if (tcp->res_t == tcp->resE) {
1950 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1951 tcp->res_t = tcp->resS;
1952 pi[pino].poc.resno0 = tcp->res_t;
1953 pi[pino].poc.resno1 = tcp->res_t + 1;
1960 pi[pino].poc.resno0 = tcp->res_t;
1961 pi[pino].poc.resno1 = tcp->res_t + 1;
1967 if (tcp->comp_t == tcp->compE) {
1968 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1969 tcp->comp_t = tcp->compS;
1970 pi[pino].poc.compno0 = tcp->comp_t;
1971 pi[pino].poc.compno1 = tcp->comp_t + 1;
1978 pi[pino].poc.compno0 = tcp->comp_t;
1979 pi[pino].poc.compno1 = tcp->comp_t + 1;
1985 if (tcp->lay_t == tcp->layE) {
1986 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1987 tcp->lay_t = tcp->layS;
1988 pi[pino].poc.layno0 = tcp->lay_t;
1989 pi[pino].poc.layno1 = tcp->lay_t + 1;
1996 pi[pino].poc.layno0 = tcp->lay_t;
1997 pi[pino].poc.layno1 = tcp->lay_t + 1;
2006 if (tcp->prc_t == tcp->prcE) {
2007 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2008 tcp->prc_t = tcp->prcS;
2009 pi[pino].poc.precno0 = tcp->prc_t;
2010 pi[pino].poc.precno1 = tcp->prc_t + 1;
2017 pi[pino].poc.precno0 = tcp->prc_t;
2018 pi[pino].poc.precno1 = tcp->prc_t + 1;
2024 if (tcp->tx0_t >= tcp->txE) {
2025 if (tcp->ty0_t >= tcp->tyE) {
2026 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
2027 tcp->ty0_t = tcp->tyS;
2028 pi[pino].poc.ty0 = tcp->ty0_t;
2029 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2030 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2038 pi[pino].poc.ty0 = tcp->ty0_t;
2039 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2040 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2045 tcp->tx0_t = tcp->txS;
2046 pi[pino].poc.tx0 = tcp->tx0_t;
2047 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2048 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2051 pi[pino].poc.tx0 = tcp->tx0_t;
2052 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2053 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2066 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2067 OPJ_UINT32 p_nb_elements)
2069 OPJ_UINT32 compno, pino;
2070 opj_pi_iterator_t *l_current_pi = p_pi;
2072 if (p_pi->include) {
2073 opj_free(p_pi->include);
2076 for (pino = 0; pino < p_nb_elements; ++pino) {
2077 if (l_current_pi->comps) {
2078 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2079 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2080 if (l_current_component->resolutions) {
2081 opj_free(l_current_component->resolutions);
2082 l_current_component->resolutions = 00;
2085 ++l_current_component;
2087 opj_free(l_current_pi->comps);
2088 l_current_pi->comps = 0;
2098 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2100 OPJ_UINT32 p_tile_no)
2102 /* encoding parameters to set */
2103 OPJ_UINT32 l_max_res;
2104 OPJ_UINT32 l_max_prec;
2105 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
2106 OPJ_UINT32 l_dx_min, l_dy_min;
2109 opj_tcp_t *l_tcp = 00;
2113 assert(p_image != 00);
2114 assert(p_tile_no < p_cp->tw * p_cp->th);
2116 l_tcp = &(p_cp->tcps[p_tile_no]);
2118 /* get encoding parameters */
2119 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2120 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2123 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2124 l_max_prec, l_max_res, l_dx_min, l_dy_min);
2126 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2127 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2131 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2133 switch (pi->poc.prg) {
2135 return opj_pi_next_lrcp(pi);
2137 return opj_pi_next_rlcp(pi);
2139 return opj_pi_next_rpcl(pi);
2141 return opj_pi_next_pcrl(pi);
2143 return opj_pi_next_cprl(pi);
2144 case OPJ_PROG_UNKNOWN: