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.
198 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
199 const opj_cp_t *p_cp,
204 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
206 OPJ_UINT32 p_max_precision,
207 OPJ_UINT32 p_max_res);
211 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
213 OPJ_UINT32 p_max_precision,
214 OPJ_UINT32 p_max_res);
219 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
223 const OPJ_CHAR *prog);
230 ==========================================================
232 ==========================================================
235 static void opj_pi_emit_error(opj_pi_iterator_t * pi, const char* msg)
241 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
243 opj_pi_comp_t *comp = NULL;
244 opj_pi_resolution_t *res = NULL;
245 OPJ_UINT32 index = 0;
248 comp = &pi->comps[pi->compno];
249 res = &comp->resolutions[pi->resno];
255 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
256 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
258 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
259 comp = &pi->comps[pi->compno];
260 if (pi->resno >= comp->numresolutions) {
263 res = &comp->resolutions[pi->resno];
265 pi->poc.precno1 = res->pw * res->ph;
267 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
268 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
269 pi->step_c + pi->precno * pi->step_p;
270 /* Avoids index out of bounds access with */
271 /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
272 /* https://github.com/uclouvain/openjpeg/issues/938 */
273 /* Not sure if this is the most clever fix. Perhaps */
274 /* include should be resized when a POC arises, or */
275 /* the POC should be rejected */
276 if (index >= pi->include_size) {
277 opj_pi_emit_error(pi, "Invalid access to pi->include");
280 if (!pi->include[index]) {
281 pi->include[index] = 1;
294 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
296 opj_pi_comp_t *comp = NULL;
297 opj_pi_resolution_t *res = NULL;
298 OPJ_UINT32 index = 0;
301 comp = &pi->comps[pi->compno];
302 res = &comp->resolutions[pi->resno];
308 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
309 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
310 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
311 comp = &pi->comps[pi->compno];
312 if (pi->resno >= comp->numresolutions) {
315 res = &comp->resolutions[pi->resno];
317 pi->poc.precno1 = res->pw * res->ph;
319 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
320 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
321 pi->step_c + pi->precno * pi->step_p;
322 if (index >= pi->include_size) {
323 opj_pi_emit_error(pi, "Invalid access to pi->include");
326 if (!pi->include[index]) {
327 pi->include[index] = 1;
340 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
342 opj_pi_comp_t *comp = NULL;
343 opj_pi_resolution_t *res = NULL;
344 OPJ_UINT32 index = 0;
349 OPJ_UINT32 compno, resno;
353 for (compno = 0; compno < pi->numcomps; compno++) {
354 comp = &pi->comps[compno];
355 for (resno = 0; resno < comp->numresolutions; resno++) {
357 res = &comp->resolutions[resno];
358 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
359 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
360 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
361 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
363 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
364 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
365 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
366 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
370 if (pi->dx == 0 || pi->dy == 0) {
375 pi->poc.ty0 = pi->ty0;
376 pi->poc.tx0 = pi->tx0;
377 pi->poc.ty1 = pi->ty1;
378 pi->poc.tx1 = pi->tx1;
380 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
381 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
382 pi->y += (pi->dy - (pi->y % pi->dy))) {
383 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
384 pi->x += (pi->dx - (pi->x % pi->dx))) {
385 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
387 OPJ_UINT32 trx0, try0;
388 OPJ_UINT32 trx1, try1;
390 OPJ_UINT32 prci, prcj;
391 comp = &pi->comps[pi->compno];
392 if (pi->resno >= comp->numresolutions) {
395 res = &comp->resolutions[pi->resno];
396 levelno = comp->numresolutions - 1 - pi->resno;
397 /* Avoids division by zero */
398 /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
399 /* of https://github.com/uclouvain/openjpeg/issues/938 */
401 ((comp->dx << levelno) >> levelno) != comp->dx ||
402 ((comp->dy << levelno) >> levelno) != comp->dy) {
405 if ((comp->dx << levelno) > INT_MAX ||
406 (comp->dy << levelno) > INT_MAX) {
409 trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
410 try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
411 trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
412 try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
413 rpx = res->pdx + levelno;
414 rpy = res->pdy + levelno;
416 /* To avoid divisions by zero / undefined behaviour on shift */
418 /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */
419 /* of https://github.com/uclouvain/openjpeg/issues/938 */
420 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
421 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
425 /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
426 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
427 ((try0 << levelno) % (1U << rpy))))) {
430 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
431 ((trx0 << levelno) % (1U << rpx))))) {
435 if ((res->pw == 0) || (res->ph == 0)) {
439 if ((trx0 == trx1) || (try0 == try1)) {
443 prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
444 (comp->dx << levelno)), res->pdx)
445 - opj_uint_floordivpow2(trx0, res->pdx);
446 prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
447 (comp->dy << levelno)), res->pdy)
448 - opj_uint_floordivpow2(try0, res->pdy);
449 pi->precno = prci + prcj * res->pw;
450 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
451 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
452 pi->step_c + pi->precno * pi->step_p;
453 if (index >= pi->include_size) {
454 opj_pi_emit_error(pi, "Invalid access to pi->include");
457 if (!pi->include[index]) {
458 pi->include[index] = 1;
472 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
474 opj_pi_comp_t *comp = NULL;
475 opj_pi_resolution_t *res = NULL;
476 OPJ_UINT32 index = 0;
479 comp = &pi->comps[pi->compno];
482 OPJ_UINT32 compno, resno;
486 for (compno = 0; compno < pi->numcomps; compno++) {
487 comp = &pi->comps[compno];
488 for (resno = 0; resno < comp->numresolutions; resno++) {
490 res = &comp->resolutions[resno];
491 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
492 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
493 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
494 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
496 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
497 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
498 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
499 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
503 if (pi->dx == 0 || pi->dy == 0) {
508 pi->poc.ty0 = pi->ty0;
509 pi->poc.tx0 = pi->tx0;
510 pi->poc.ty1 = pi->ty1;
511 pi->poc.tx1 = pi->tx1;
513 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
514 pi->y += (pi->dy - (pi->y % pi->dy))) {
515 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
516 pi->x += (pi->dx - (pi->x % pi->dx))) {
517 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
518 comp = &pi->comps[pi->compno];
519 for (pi->resno = pi->poc.resno0;
520 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
522 OPJ_UINT32 trx0, try0;
523 OPJ_UINT32 trx1, try1;
525 OPJ_UINT32 prci, prcj;
526 res = &comp->resolutions[pi->resno];
527 levelno = comp->numresolutions - 1 - pi->resno;
528 /* Avoids division by zero */
529 /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
530 /* of https://github.com/uclouvain/openjpeg/issues/938 */
532 ((comp->dx << levelno) >> levelno) != comp->dx ||
533 ((comp->dy << levelno) >> levelno) != comp->dy) {
536 if ((comp->dx << levelno) > INT_MAX ||
537 (comp->dy << levelno) > INT_MAX) {
540 trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
541 try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
542 trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
543 try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
544 rpx = res->pdx + levelno;
545 rpy = res->pdy + levelno;
547 /* To avoid divisions by zero / undefined behaviour on shift */
549 /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
550 /* of https://github.com/uclouvain/openjpeg/issues/938 */
551 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
552 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
556 /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
557 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
558 ((try0 << levelno) % (1U << rpy))))) {
561 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
562 ((trx0 << levelno) % (1U << rpx))))) {
566 if ((res->pw == 0) || (res->ph == 0)) {
570 if ((trx0 == trx1) || (try0 == try1)) {
574 prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
575 (comp->dx << levelno)), res->pdx)
576 - opj_uint_floordivpow2(trx0, res->pdx);
577 prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
578 (comp->dy << levelno)), res->pdy)
579 - opj_uint_floordivpow2(try0, res->pdy);
580 pi->precno = prci + prcj * res->pw;
581 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
582 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
583 pi->step_c + pi->precno * pi->step_p;
584 if (index >= pi->include_size) {
585 opj_pi_emit_error(pi, "Invalid access to pi->include");
588 if (!pi->include[index]) {
589 pi->include[index] = 1;
603 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
605 opj_pi_comp_t *comp = NULL;
606 opj_pi_resolution_t *res = NULL;
607 OPJ_UINT32 index = 0;
610 comp = &pi->comps[pi->compno];
616 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
618 comp = &pi->comps[pi->compno];
621 for (resno = 0; resno < comp->numresolutions; resno++) {
623 res = &comp->resolutions[resno];
624 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
625 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
626 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
627 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
629 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
630 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
631 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
632 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
635 if (pi->dx == 0 || pi->dy == 0) {
639 pi->poc.ty0 = pi->ty0;
640 pi->poc.tx0 = pi->tx0;
641 pi->poc.ty1 = pi->ty1;
642 pi->poc.tx1 = pi->tx1;
644 for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
645 pi->y += (pi->dy - (pi->y % pi->dy))) {
646 for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
647 pi->x += (pi->dx - (pi->x % pi->dx))) {
648 for (pi->resno = pi->poc.resno0;
649 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
651 OPJ_UINT32 trx0, try0;
652 OPJ_UINT32 trx1, try1;
654 OPJ_UINT32 prci, prcj;
655 res = &comp->resolutions[pi->resno];
656 levelno = comp->numresolutions - 1 - pi->resno;
657 /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
658 /* of https://github.com/uclouvain/openjpeg/issues/938 */
660 ((comp->dx << levelno) >> levelno) != comp->dx ||
661 ((comp->dy << levelno) >> levelno) != comp->dy) {
664 if ((comp->dx << levelno) > INT_MAX ||
665 (comp->dy << levelno) > INT_MAX) {
668 trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
669 try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
670 trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
671 try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
672 rpx = res->pdx + levelno;
673 rpy = res->pdy + levelno;
675 /* To avoid divisions by zero / undefined behaviour on shift */
677 /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
678 /* of https://github.com/uclouvain/openjpeg/issues/938 */
679 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
680 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
684 /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
685 if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
686 ((try0 << levelno) % (1U << rpy))))) {
689 if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
690 ((trx0 << levelno) % (1U << rpx))))) {
694 if ((res->pw == 0) || (res->ph == 0)) {
698 if ((trx0 == trx1) || (try0 == try1)) {
702 prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
703 (comp->dx << levelno)), res->pdx)
704 - opj_uint_floordivpow2(trx0, res->pdx);
705 prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
706 (comp->dy << levelno)), res->pdy)
707 - opj_uint_floordivpow2(try0, res->pdy);
708 pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
709 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
710 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
711 pi->step_c + pi->precno * pi->step_p;
712 if (index >= pi->include_size) {
713 opj_pi_emit_error(pi, "Invalid access to pi->include");
716 if (!pi->include[index]) {
717 pi->include[index] = 1;
731 static void opj_get_encoding_parameters(const opj_image_t *p_image,
732 const opj_cp_t *p_cp,
738 OPJ_UINT32 * p_dx_min,
739 OPJ_UINT32 * p_dy_min,
740 OPJ_UINT32 * p_max_prec,
741 OPJ_UINT32 * p_max_res)
744 OPJ_UINT32 compno, resno;
746 const opj_tcp_t *l_tcp = 00;
747 const opj_tccp_t * l_tccp = 00;
748 const opj_image_comp_t * l_img_comp = 00;
750 /* position in x and y of tile */
753 /* non-corrected (in regard to image offset) tile offset */
754 OPJ_UINT32 l_tx0, l_ty0;
758 assert(p_image != 00);
759 assert(p_tileno < p_cp->tw * p_cp->th);
761 /* initializations */
762 l_tcp = &p_cp->tcps [p_tileno];
763 l_img_comp = p_image->comps;
764 l_tccp = l_tcp->tccps;
766 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
767 p = p_tileno % p_cp->tw;
768 q = p_tileno / p_cp->tw;
770 /* find extent of tile */
771 l_tx0 = p_cp->tx0 + p *
772 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
773 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
774 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
775 l_ty0 = p_cp->ty0 + q *
776 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
777 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
778 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
780 /* max precision is 0 (can only grow) */
784 /* take the largest value for dx_min and dy_min */
785 *p_dx_min = 0x7fffffff;
786 *p_dy_min = 0x7fffffff;
788 for (compno = 0; compno < p_image->numcomps; ++compno) {
789 /* arithmetic variables to calculate */
790 OPJ_UINT32 l_level_no;
791 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
792 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
793 OPJ_UINT32 l_pdx, l_pdy;
794 OPJ_UINT32 l_pw, l_ph;
795 OPJ_UINT32 l_product;
796 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
798 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
799 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
800 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
801 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
803 if (l_tccp->numresolutions > *p_max_res) {
804 *p_max_res = l_tccp->numresolutions;
807 /* use custom size for precincts */
808 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
809 OPJ_UINT32 l_dx, l_dy;
811 /* precinct width and height */
812 l_pdx = l_tccp->prcw[resno];
813 l_pdy = l_tccp->prch[resno];
815 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
816 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
818 /* take the minimum size for dx for each comp and resolution */
819 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
820 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
822 /* various calculations of extents */
823 l_level_no = l_tccp->numresolutions - 1 - resno;
825 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
826 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
827 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
828 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
830 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
831 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
832 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
834 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
836 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
837 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
839 l_product = l_pw * l_ph;
841 /* update precision */
842 if (l_product > *p_max_prec) {
843 *p_max_prec = l_product;
852 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
853 const opj_cp_t *p_cp,
859 OPJ_UINT32 * p_dx_min,
860 OPJ_UINT32 * p_dy_min,
861 OPJ_UINT32 * p_max_prec,
862 OPJ_UINT32 * p_max_res,
863 OPJ_UINT32 ** p_resolutions)
866 OPJ_UINT32 compno, resno;
869 const opj_tcp_t *tcp = 00;
870 const opj_tccp_t * l_tccp = 00;
871 const opj_image_comp_t * l_img_comp = 00;
873 /* to store l_dx, l_dy, w and h for each resolution and component.*/
874 OPJ_UINT32 * lResolutionPtr;
876 /* position in x and y of tile*/
879 /* non-corrected (in regard to image offset) tile offset */
880 OPJ_UINT32 l_tx0, l_ty0;
882 /* preconditions in debug*/
884 assert(p_image != 00);
885 assert(tileno < p_cp->tw * p_cp->th);
888 tcp = &p_cp->tcps [tileno];
890 l_img_comp = p_image->comps;
892 /* position in x and y of tile*/
893 p = tileno % p_cp->tw;
894 q = tileno / p_cp->tw;
896 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
897 l_tx0 = p_cp->tx0 + p *
898 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
899 *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
900 *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
901 l_ty0 = p_cp->ty0 + q *
902 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
903 *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
904 *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
906 /* max precision and resolution is 0 (can only grow)*/
910 /* take the largest value for dx_min and dy_min*/
911 *p_dx_min = 0x7fffffff;
912 *p_dy_min = 0x7fffffff;
914 for (compno = 0; compno < p_image->numcomps; ++compno) {
915 /* aritmetic variables to calculate*/
916 OPJ_UINT32 l_level_no;
917 OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
918 OPJ_UINT32 l_px0, l_py0, l_px1, py1;
919 OPJ_UINT32 l_product;
920 OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
921 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
923 lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
925 l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
926 l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
927 l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
928 l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
930 if (l_tccp->numresolutions > *p_max_res) {
931 *p_max_res = l_tccp->numresolutions;
934 /* use custom size for precincts*/
935 l_level_no = l_tccp->numresolutions;
936 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
937 OPJ_UINT32 l_dx, l_dy;
941 /* precinct width and height*/
942 l_pdx = l_tccp->prcw[resno];
943 l_pdy = l_tccp->prch[resno];
944 if (lResolutionPtr) {
945 *lResolutionPtr++ = l_pdx;
946 *lResolutionPtr++ = l_pdy;
948 if (l_pdx + l_level_no < 32 &&
949 l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
950 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
951 /* take the minimum size for l_dx for each comp and resolution*/
952 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
954 if (l_pdy + l_level_no < 32 &&
955 l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
956 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
957 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
960 /* various calculations of extents*/
961 l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
962 l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
963 l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
964 l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
965 l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
966 l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
967 l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
968 py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
969 l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
970 l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
971 if (lResolutionPtr) {
972 *lResolutionPtr++ = l_pw;
973 *lResolutionPtr++ = l_ph;
975 l_product = l_pw * l_ph;
977 /* update precision*/
978 if (l_product > *p_max_prec) {
979 *p_max_prec = l_product;
988 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
993 OPJ_UINT32 pino, compno;
994 /* number of poc in the p_pi*/
995 OPJ_UINT32 l_poc_bound;
997 /* pointers to tile coding parameters and components.*/
998 opj_pi_iterator_t *l_pi = 00;
1000 const opj_tccp_t *tccp = 00;
1002 /* current packet iterator being allocated*/
1003 opj_pi_iterator_t *l_current_pi = 00;
1005 /* preconditions in debug*/
1007 assert(image != 00);
1008 assert(tileno < cp->tw * cp->th);
1010 /* initializations*/
1011 tcp = &cp->tcps[tileno];
1012 l_poc_bound = tcp->numpocs + 1;
1014 /* memory allocations*/
1015 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1016 sizeof(opj_pi_iterator_t));
1021 l_current_pi = l_pi;
1022 for (pino = 0; pino < l_poc_bound ; ++pino) {
1024 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1025 sizeof(opj_pi_comp_t));
1026 if (! l_current_pi->comps) {
1027 opj_pi_destroy(l_pi, l_poc_bound);
1031 l_current_pi->numcomps = image->numcomps;
1033 for (compno = 0; compno < image->numcomps; ++compno) {
1034 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1036 tccp = &tcp->tccps[compno];
1038 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1039 sizeof(opj_pi_resolution_t));
1040 if (!comp->resolutions) {
1041 opj_pi_destroy(l_pi, l_poc_bound);
1045 comp->numresolutions = tccp->numresolutions;
1052 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1053 OPJ_UINT32 p_tileno,
1058 OPJ_UINT32 p_max_prec,
1059 OPJ_UINT32 p_max_res,
1060 OPJ_UINT32 p_dx_min,
1061 OPJ_UINT32 p_dy_min)
1065 /* tile coding parameter*/
1066 opj_tcp_t *l_tcp = 00;
1067 /* current poc being updated*/
1068 opj_poc_t * l_current_poc = 00;
1071 OPJ_UINT32 l_poc_bound;
1073 OPJ_ARG_NOT_USED(p_max_res);
1075 /* preconditions in debug*/
1077 assert(p_tileno < p_cp->tw * p_cp->th);
1079 /* initializations*/
1080 l_tcp = &p_cp->tcps [p_tileno];
1081 /* number of iterations in the loop */
1082 l_poc_bound = l_tcp->numpocs + 1;
1084 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1085 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1086 l_current_poc = l_tcp->pocs;
1088 l_current_poc->compS = l_current_poc->compno0;
1089 l_current_poc->compE = l_current_poc->compno1;
1090 l_current_poc->resS = l_current_poc->resno0;
1091 l_current_poc->resE = l_current_poc->resno1;
1092 l_current_poc->layE = l_current_poc->layno1;
1094 /* special treatment for the first element*/
1095 l_current_poc->layS = 0;
1096 l_current_poc->prg = l_current_poc->prg1;
1097 l_current_poc->prcS = 0;
1099 l_current_poc->prcE = p_max_prec;
1100 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1101 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1102 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1103 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1104 l_current_poc->dx = p_dx_min;
1105 l_current_poc->dy = p_dy_min;
1108 for (pino = 1; pino < l_poc_bound ; ++pino) {
1109 l_current_poc->compS = l_current_poc->compno0;
1110 l_current_poc->compE = l_current_poc->compno1;
1111 l_current_poc->resS = l_current_poc->resno0;
1112 l_current_poc->resE = l_current_poc->resno1;
1113 l_current_poc->layE = l_current_poc->layno1;
1114 l_current_poc->prg = l_current_poc->prg1;
1115 l_current_poc->prcS = 0;
1116 /* special treatment here different from the first element*/
1117 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1118 l_current_poc->layE : 0;
1120 l_current_poc->prcE = p_max_prec;
1121 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1122 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1123 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1124 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1125 l_current_poc->dx = p_dx_min;
1126 l_current_poc->dy = p_dy_min;
1131 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1132 OPJ_UINT32 p_num_comps,
1133 OPJ_UINT32 p_tileno,
1138 OPJ_UINT32 p_max_prec,
1139 OPJ_UINT32 p_max_res,
1140 OPJ_UINT32 p_dx_min,
1141 OPJ_UINT32 p_dy_min)
1145 /* tile coding parameter*/
1146 opj_tcp_t *l_tcp = 00;
1147 /* current poc being updated*/
1148 opj_poc_t * l_current_poc = 00;
1150 OPJ_UINT32 l_poc_bound;
1152 /* preconditions in debug*/
1154 assert(p_tileno < p_cp->tw * p_cp->th);
1156 /* initializations*/
1157 l_tcp = &p_cp->tcps [p_tileno];
1159 /* number of iterations in the loop */
1160 l_poc_bound = l_tcp->numpocs + 1;
1162 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1163 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1164 l_current_poc = l_tcp->pocs;
1166 for (pino = 0; pino < l_poc_bound ; ++pino) {
1167 l_current_poc->compS = 0;
1168 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1169 l_current_poc->resS = 0;
1170 l_current_poc->resE = p_max_res;
1171 l_current_poc->layS = 0;
1172 l_current_poc->layE = l_tcp->numlayers;
1173 l_current_poc->prg = l_tcp->prg;
1174 l_current_poc->prcS = 0;
1175 l_current_poc->prcE = p_max_prec;
1176 l_current_poc->txS = p_tx0;
1177 l_current_poc->txE = p_tx1;
1178 l_current_poc->tyS = p_ty0;
1179 l_current_poc->tyE = p_ty1;
1180 l_current_poc->dx = p_dx_min;
1181 l_current_poc->dy = p_dy_min;
1186 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1188 OPJ_UINT32 p_max_precision,
1189 OPJ_UINT32 p_max_res)
1194 /* encoding prameters to set*/
1197 opj_pi_iterator_t * l_current_pi = 00;
1198 opj_poc_t* l_current_poc = 0;
1200 OPJ_ARG_NOT_USED(p_max_res);
1202 /* preconditions in debug*/
1204 assert(p_tcp != 00);
1206 /* initializations*/
1207 l_bound = p_tcp->numpocs + 1;
1208 l_current_pi = p_pi;
1209 l_current_poc = p_tcp->pocs;
1211 for (pino = 0; pino < l_bound; ++pino) {
1212 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1213 l_current_pi->first = 1;
1215 l_current_pi->poc.resno0 =
1216 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1217 l_current_pi->poc.compno0 =
1218 l_current_poc->compno0; /* Component Index #0 (Start) */
1219 l_current_pi->poc.layno0 = 0;
1220 l_current_pi->poc.precno0 = 0;
1221 l_current_pi->poc.resno1 =
1222 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1223 l_current_pi->poc.compno1 =
1224 l_current_poc->compno1; /* Component Index #0 (End) */
1225 l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1226 p_tcp->numlayers); /* Layer Index #0 (End) */
1227 l_current_pi->poc.precno1 = p_max_precision;
1233 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1235 OPJ_UINT32 p_max_precision,
1236 OPJ_UINT32 p_max_res)
1241 /* encoding prameters to set*/
1244 opj_pi_iterator_t * l_current_pi = 00;
1245 /* preconditions in debug*/
1246 assert(p_tcp != 00);
1249 /* initializations*/
1250 l_bound = p_tcp->numpocs + 1;
1251 l_current_pi = p_pi;
1253 for (pino = 0; pino < l_bound; ++pino) {
1254 l_current_pi->poc.prg = p_tcp->prg;
1255 l_current_pi->first = 1;
1256 l_current_pi->poc.resno0 = 0;
1257 l_current_pi->poc.compno0 = 0;
1258 l_current_pi->poc.layno0 = 0;
1259 l_current_pi->poc.precno0 = 0;
1260 l_current_pi->poc.resno1 = p_max_res;
1261 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1262 l_current_pi->poc.layno1 = p_tcp->numlayers;
1263 l_current_pi->poc.precno1 = p_max_precision;
1270 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1274 const OPJ_CHAR *prog)
1277 opj_tcp_t *tcps = &cp->tcps[tileno];
1278 opj_poc_t *tcp = &tcps->pocs[pino];
1281 for (i = pos; pos >= 0; i--) {
1284 if (tcp->res_t == tcp->resE) {
1285 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1295 if (tcp->comp_t == tcp->compE) {
1296 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1306 if (tcp->lay_t == tcp->layE) {
1307 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1318 case OPJ_LRCP: /* fall through */
1320 if (tcp->prc_t == tcp->prcE) {
1321 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1331 if (tcp->tx0_t == tcp->txE) {
1333 if (tcp->ty0_t == tcp->tyE) {
1334 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1355 ==========================================================
1356 Packet iterator interface
1357 ==========================================================
1359 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1361 OPJ_UINT32 p_tile_no)
1363 OPJ_UINT32 numcomps = p_image->numcomps;
1367 OPJ_UINT32 compno, resno;
1369 /* to store w, h, dx and dy fro all components and resolutions */
1370 OPJ_UINT32 * l_tmp_data;
1371 OPJ_UINT32 ** l_tmp_ptr;
1373 /* encoding prameters to set */
1374 OPJ_UINT32 l_max_res;
1375 OPJ_UINT32 l_max_prec;
1376 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1377 OPJ_UINT32 l_dx_min, l_dy_min;
1379 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1380 OPJ_UINT32 l_data_stride;
1383 opj_pi_iterator_t *l_pi = 00;
1384 opj_tcp_t *l_tcp = 00;
1385 const opj_tccp_t *l_tccp = 00;
1386 opj_pi_comp_t *l_current_comp = 00;
1387 opj_image_comp_t * l_img_comp = 00;
1388 opj_pi_iterator_t * l_current_pi = 00;
1389 OPJ_UINT32 * l_encoding_value_ptr = 00;
1391 /* preconditions in debug */
1393 assert(p_image != 00);
1394 assert(p_tile_no < p_cp->tw * p_cp->th);
1396 /* initializations */
1397 l_tcp = &p_cp->tcps[p_tile_no];
1398 l_bound = l_tcp->numpocs + 1;
1400 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1401 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1402 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1407 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1408 numcomps * sizeof(OPJ_UINT32 *));
1411 opj_free(l_tmp_data);
1415 /* memory allocation for pi */
1416 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1418 opj_free(l_tmp_data);
1419 opj_free(l_tmp_ptr);
1423 l_encoding_value_ptr = l_tmp_data;
1424 /* update pointer array */
1426 (compno = 0; compno < numcomps; ++compno) {
1427 l_tmp_ptr[compno] = l_encoding_value_ptr;
1428 l_encoding_value_ptr += l_data_stride;
1430 /* get encoding parameters */
1431 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1432 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1434 /* step calculations */
1436 l_step_c = l_max_prec * l_step_p;
1437 l_step_r = numcomps * l_step_c;
1438 l_step_l = l_max_res * l_step_r;
1440 /* set values for first packet iterator */
1441 l_current_pi = l_pi;
1443 /* memory allocation for include */
1444 /* prevent an integer overflow issue */
1445 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1446 l_current_pi->include = 00;
1447 if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1448 l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1449 l_current_pi->include = (OPJ_INT16*) opj_calloc(
1450 l_current_pi->include_size, sizeof(OPJ_INT16));
1453 if (!l_current_pi->include) {
1454 opj_free(l_tmp_data);
1455 opj_free(l_tmp_ptr);
1456 opj_pi_destroy(l_pi, l_bound);
1460 /* special treatment for the first packet iterator */
1461 l_current_comp = l_current_pi->comps;
1462 l_img_comp = p_image->comps;
1463 l_tccp = l_tcp->tccps;
1465 l_current_pi->tx0 = l_tx0;
1466 l_current_pi->ty0 = l_ty0;
1467 l_current_pi->tx1 = l_tx1;
1468 l_current_pi->ty1 = l_ty1;
1470 /*l_current_pi->dx = l_img_comp->dx;*/
1471 /*l_current_pi->dy = l_img_comp->dy;*/
1473 l_current_pi->step_p = l_step_p;
1474 l_current_pi->step_c = l_step_c;
1475 l_current_pi->step_r = l_step_r;
1476 l_current_pi->step_l = l_step_l;
1478 /* allocation for components and number of components has already been calculated by opj_pi_create */
1480 (compno = 0; compno < numcomps; ++compno) {
1481 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1482 l_encoding_value_ptr = l_tmp_ptr[compno];
1484 l_current_comp->dx = l_img_comp->dx;
1485 l_current_comp->dy = l_img_comp->dy;
1486 /* resolutions have already been initialized */
1488 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1489 l_res->pdx = *(l_encoding_value_ptr++);
1490 l_res->pdy = *(l_encoding_value_ptr++);
1491 l_res->pw = *(l_encoding_value_ptr++);
1492 l_res->ph = *(l_encoding_value_ptr++);
1501 for (pino = 1 ; pino < l_bound ; ++pino) {
1502 l_current_comp = l_current_pi->comps;
1503 l_img_comp = p_image->comps;
1504 l_tccp = l_tcp->tccps;
1506 l_current_pi->tx0 = l_tx0;
1507 l_current_pi->ty0 = l_ty0;
1508 l_current_pi->tx1 = l_tx1;
1509 l_current_pi->ty1 = l_ty1;
1510 /*l_current_pi->dx = l_dx_min;*/
1511 /*l_current_pi->dy = l_dy_min;*/
1512 l_current_pi->step_p = l_step_p;
1513 l_current_pi->step_c = l_step_c;
1514 l_current_pi->step_r = l_step_r;
1515 l_current_pi->step_l = l_step_l;
1517 /* allocation for components and number of components has already been calculated by opj_pi_create */
1519 (compno = 0; compno < numcomps; ++compno) {
1520 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1521 l_encoding_value_ptr = l_tmp_ptr[compno];
1523 l_current_comp->dx = l_img_comp->dx;
1524 l_current_comp->dy = l_img_comp->dy;
1525 /* resolutions have already been initialized */
1527 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1528 l_res->pdx = *(l_encoding_value_ptr++);
1529 l_res->pdy = *(l_encoding_value_ptr++);
1530 l_res->pw = *(l_encoding_value_ptr++);
1531 l_res->ph = *(l_encoding_value_ptr++);
1538 /* special treatment*/
1539 l_current_pi->include = (l_current_pi - 1)->include;
1540 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1543 opj_free(l_tmp_data);
1545 opj_free(l_tmp_ptr);
1549 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1551 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1557 OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
1558 const opj_cp_t *p_cp,
1559 OPJ_UINT32 p_tile_no)
1561 OPJ_UINT32 l_max_res;
1562 OPJ_UINT32 l_max_prec;
1563 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1564 OPJ_UINT32 l_dx_min, l_dy_min;
1566 /* preconditions in debug*/
1568 assert(p_image != 00);
1569 assert(p_tile_no < p_cp->tw * p_cp->th);
1571 /* get encoding parameters*/
1572 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1573 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
1575 return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
1580 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1582 OPJ_UINT32 p_tile_no,
1583 J2K_T2_MODE p_t2_mode)
1585 OPJ_UINT32 numcomps = p_image->numcomps;
1589 OPJ_UINT32 compno, resno;
1591 /* to store w, h, dx and dy fro all components and resolutions*/
1592 OPJ_UINT32 * l_tmp_data;
1593 OPJ_UINT32 ** l_tmp_ptr;
1595 /* encoding prameters to set*/
1596 OPJ_UINT32 l_max_res;
1597 OPJ_UINT32 l_max_prec;
1598 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
1599 OPJ_UINT32 l_dx_min, l_dy_min;
1601 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1602 OPJ_UINT32 l_data_stride;
1605 opj_pi_iterator_t *l_pi = 00;
1606 opj_tcp_t *l_tcp = 00;
1607 const opj_tccp_t *l_tccp = 00;
1608 opj_pi_comp_t *l_current_comp = 00;
1609 opj_image_comp_t * l_img_comp = 00;
1610 opj_pi_iterator_t * l_current_pi = 00;
1611 OPJ_UINT32 * l_encoding_value_ptr = 00;
1613 /* preconditions in debug*/
1615 assert(p_image != 00);
1616 assert(p_tile_no < p_cp->tw * p_cp->th);
1618 /* initializations*/
1619 l_tcp = &p_cp->tcps[p_tile_no];
1620 l_bound = l_tcp->numpocs + 1;
1622 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1623 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1624 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1629 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1630 numcomps * sizeof(OPJ_UINT32 *));
1632 opj_free(l_tmp_data);
1636 /* memory allocation for pi*/
1637 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1639 opj_free(l_tmp_data);
1640 opj_free(l_tmp_ptr);
1644 l_encoding_value_ptr = l_tmp_data;
1645 /* update pointer array*/
1646 for (compno = 0; compno < numcomps; ++compno) {
1647 l_tmp_ptr[compno] = l_encoding_value_ptr;
1648 l_encoding_value_ptr += l_data_stride;
1651 /* get encoding parameters*/
1652 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1653 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1655 /* step calculations*/
1657 l_step_c = l_max_prec * l_step_p;
1658 l_step_r = numcomps * l_step_c;
1659 l_step_l = l_max_res * l_step_r;
1661 /* set values for first packet iterator*/
1662 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1663 l_current_pi = l_pi;
1665 /* memory allocation for include*/
1666 l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1667 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1669 if (!l_current_pi->include) {
1670 opj_free(l_tmp_data);
1671 opj_free(l_tmp_ptr);
1672 opj_pi_destroy(l_pi, l_bound);
1676 /* special treatment for the first packet iterator*/
1677 l_current_comp = l_current_pi->comps;
1678 l_img_comp = p_image->comps;
1679 l_tccp = l_tcp->tccps;
1680 l_current_pi->tx0 = l_tx0;
1681 l_current_pi->ty0 = l_ty0;
1682 l_current_pi->tx1 = l_tx1;
1683 l_current_pi->ty1 = l_ty1;
1684 l_current_pi->dx = l_dx_min;
1685 l_current_pi->dy = l_dy_min;
1686 l_current_pi->step_p = l_step_p;
1687 l_current_pi->step_c = l_step_c;
1688 l_current_pi->step_r = l_step_r;
1689 l_current_pi->step_l = l_step_l;
1691 /* allocation for components and number of components has already been calculated by opj_pi_create */
1692 for (compno = 0; compno < numcomps; ++compno) {
1693 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1694 l_encoding_value_ptr = l_tmp_ptr[compno];
1696 l_current_comp->dx = l_img_comp->dx;
1697 l_current_comp->dy = l_img_comp->dy;
1699 /* resolutions have already been initialized */
1700 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1701 l_res->pdx = *(l_encoding_value_ptr++);
1702 l_res->pdy = *(l_encoding_value_ptr++);
1703 l_res->pw = *(l_encoding_value_ptr++);
1704 l_res->ph = *(l_encoding_value_ptr++);
1714 for (pino = 1 ; pino < l_bound ; ++pino) {
1715 l_current_comp = l_current_pi->comps;
1716 l_img_comp = p_image->comps;
1717 l_tccp = l_tcp->tccps;
1719 l_current_pi->tx0 = l_tx0;
1720 l_current_pi->ty0 = l_ty0;
1721 l_current_pi->tx1 = l_tx1;
1722 l_current_pi->ty1 = l_ty1;
1723 l_current_pi->dx = l_dx_min;
1724 l_current_pi->dy = l_dy_min;
1725 l_current_pi->step_p = l_step_p;
1726 l_current_pi->step_c = l_step_c;
1727 l_current_pi->step_r = l_step_r;
1728 l_current_pi->step_l = l_step_l;
1730 /* allocation for components and number of components has already been calculated by opj_pi_create */
1731 for (compno = 0; compno < numcomps; ++compno) {
1732 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1733 l_encoding_value_ptr = l_tmp_ptr[compno];
1735 l_current_comp->dx = l_img_comp->dx;
1736 l_current_comp->dy = l_img_comp->dy;
1737 /* resolutions have already been initialized */
1738 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1739 l_res->pdx = *(l_encoding_value_ptr++);
1740 l_res->pdy = *(l_encoding_value_ptr++);
1741 l_res->pw = *(l_encoding_value_ptr++);
1742 l_res->ph = *(l_encoding_value_ptr++);
1750 /* special treatment*/
1751 l_current_pi->include = (l_current_pi - 1)->include;
1752 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1756 opj_free(l_tmp_data);
1758 opj_free(l_tmp_ptr);
1761 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1762 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1763 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1765 opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1766 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1772 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1778 J2K_T2_MODE t2_mode)
1780 const OPJ_CHAR *prog;
1782 OPJ_UINT32 incr_top = 1, resetX = 0;
1783 opj_tcp_t *tcps = &cp->tcps[tileno];
1784 opj_poc_t *tcp = &tcps->pocs[pino];
1786 prog = opj_j2k_convert_progression_order(tcp->prg);
1789 pi[pino].poc.prg = tcp->prg;
1791 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1792 !OPJ_IS_IMF(cp->rsiz) &&
1793 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
1794 pi[pino].poc.resno0 = tcp->resS;
1795 pi[pino].poc.resno1 = tcp->resE;
1796 pi[pino].poc.compno0 = tcp->compS;
1797 pi[pino].poc.compno1 = tcp->compE;
1798 pi[pino].poc.layno0 = tcp->layS;
1799 pi[pino].poc.layno1 = tcp->layE;
1800 pi[pino].poc.precno0 = tcp->prcS;
1801 pi[pino].poc.precno1 = tcp->prcE;
1802 pi[pino].poc.tx0 = tcp->txS;
1803 pi[pino].poc.ty0 = tcp->tyS;
1804 pi[pino].poc.tx1 = tcp->txE;
1805 pi[pino].poc.ty1 = tcp->tyE;
1807 for (i = tppos + 1; i < 4; i++) {
1810 pi[pino].poc.resno0 = tcp->resS;
1811 pi[pino].poc.resno1 = tcp->resE;
1814 pi[pino].poc.compno0 = tcp->compS;
1815 pi[pino].poc.compno1 = tcp->compE;
1818 pi[pino].poc.layno0 = tcp->layS;
1819 pi[pino].poc.layno1 = tcp->layE;
1825 pi[pino].poc.precno0 = tcp->prcS;
1826 pi[pino].poc.precno1 = tcp->prcE;
1829 pi[pino].poc.tx0 = tcp->txS;
1830 pi[pino].poc.ty0 = tcp->tyS;
1831 pi[pino].poc.tx1 = tcp->txE;
1832 pi[pino].poc.ty1 = tcp->tyE;
1840 for (i = tppos; i >= 0; i--) {
1843 tcp->comp_t = tcp->compS;
1844 pi[pino].poc.compno0 = tcp->comp_t;
1845 pi[pino].poc.compno1 = tcp->comp_t + 1;
1849 tcp->res_t = tcp->resS;
1850 pi[pino].poc.resno0 = tcp->res_t;
1851 pi[pino].poc.resno1 = tcp->res_t + 1;
1855 tcp->lay_t = tcp->layS;
1856 pi[pino].poc.layno0 = tcp->lay_t;
1857 pi[pino].poc.layno1 = tcp->lay_t + 1;
1864 tcp->prc_t = tcp->prcS;
1865 pi[pino].poc.precno0 = tcp->prc_t;
1866 pi[pino].poc.precno1 = tcp->prc_t + 1;
1870 tcp->tx0_t = tcp->txS;
1871 tcp->ty0_t = tcp->tyS;
1872 pi[pino].poc.tx0 = tcp->tx0_t;
1873 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
1874 pi[pino].poc.ty0 = tcp->ty0_t;
1875 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1876 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1877 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1885 for (i = tppos; i >= 0; i--) {
1888 pi[pino].poc.compno0 = tcp->comp_t - 1;
1889 pi[pino].poc.compno1 = tcp->comp_t;
1892 pi[pino].poc.resno0 = tcp->res_t - 1;
1893 pi[pino].poc.resno1 = tcp->res_t;
1896 pi[pino].poc.layno0 = tcp->lay_t - 1;
1897 pi[pino].poc.layno1 = tcp->lay_t;
1903 pi[pino].poc.precno0 = tcp->prc_t - 1;
1904 pi[pino].poc.precno1 = tcp->prc_t;
1907 pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
1908 pi[pino].poc.tx1 = tcp->tx0_t ;
1909 pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
1910 pi[pino].poc.ty1 = tcp->ty0_t ;
1915 if (incr_top == 1) {
1918 if (tcp->res_t == tcp->resE) {
1919 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1920 tcp->res_t = tcp->resS;
1921 pi[pino].poc.resno0 = tcp->res_t;
1922 pi[pino].poc.resno1 = tcp->res_t + 1;
1929 pi[pino].poc.resno0 = tcp->res_t;
1930 pi[pino].poc.resno1 = tcp->res_t + 1;
1936 if (tcp->comp_t == tcp->compE) {
1937 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1938 tcp->comp_t = tcp->compS;
1939 pi[pino].poc.compno0 = tcp->comp_t;
1940 pi[pino].poc.compno1 = tcp->comp_t + 1;
1947 pi[pino].poc.compno0 = tcp->comp_t;
1948 pi[pino].poc.compno1 = tcp->comp_t + 1;
1954 if (tcp->lay_t == tcp->layE) {
1955 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1956 tcp->lay_t = tcp->layS;
1957 pi[pino].poc.layno0 = tcp->lay_t;
1958 pi[pino].poc.layno1 = tcp->lay_t + 1;
1965 pi[pino].poc.layno0 = tcp->lay_t;
1966 pi[pino].poc.layno1 = tcp->lay_t + 1;
1975 if (tcp->prc_t == tcp->prcE) {
1976 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1977 tcp->prc_t = tcp->prcS;
1978 pi[pino].poc.precno0 = tcp->prc_t;
1979 pi[pino].poc.precno1 = tcp->prc_t + 1;
1986 pi[pino].poc.precno0 = tcp->prc_t;
1987 pi[pino].poc.precno1 = tcp->prc_t + 1;
1993 if (tcp->tx0_t >= tcp->txE) {
1994 if (tcp->ty0_t >= tcp->tyE) {
1995 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1996 tcp->ty0_t = tcp->tyS;
1997 pi[pino].poc.ty0 = tcp->ty0_t;
1998 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
1999 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2007 pi[pino].poc.ty0 = tcp->ty0_t;
2008 pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
2009 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
2014 tcp->tx0_t = tcp->txS;
2015 pi[pino].poc.tx0 = tcp->tx0_t;
2016 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2017 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2020 pi[pino].poc.tx0 = tcp->tx0_t;
2021 pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
2022 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2035 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2036 OPJ_UINT32 p_nb_elements)
2038 OPJ_UINT32 compno, pino;
2039 opj_pi_iterator_t *l_current_pi = p_pi;
2041 if (p_pi->include) {
2042 opj_free(p_pi->include);
2045 for (pino = 0; pino < p_nb_elements; ++pino) {
2046 if (l_current_pi->comps) {
2047 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2048 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2049 if (l_current_component->resolutions) {
2050 opj_free(l_current_component->resolutions);
2051 l_current_component->resolutions = 00;
2054 ++l_current_component;
2056 opj_free(l_current_pi->comps);
2057 l_current_pi->comps = 0;
2067 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2069 OPJ_UINT32 p_tile_no)
2071 /* encoding parameters to set */
2072 OPJ_UINT32 l_max_res;
2073 OPJ_UINT32 l_max_prec;
2074 OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
2075 OPJ_UINT32 l_dx_min, l_dy_min;
2078 opj_tcp_t *l_tcp = 00;
2082 assert(p_image != 00);
2083 assert(p_tile_no < p_cp->tw * p_cp->th);
2085 l_tcp = &(p_cp->tcps[p_tile_no]);
2087 /* get encoding parameters */
2088 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2089 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2092 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2093 l_max_prec, l_max_res, l_dx_min, l_dy_min);
2095 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2096 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2100 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2102 switch (pi->poc.prg) {
2104 return opj_pi_next_lrcp(pi);
2106 return opj_pi_next_rlcp(pi);
2108 return opj_pi_next_rpcl(pi);
2110 return opj_pi_next_pcrl(pi);
2112 return opj_pi_next_cprl(pi);
2113 case OPJ_PROG_UNKNOWN: