2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include "opj_includes.h"
41 /** @defgroup PI PI - Implementation of a packet iterator */
44 /** @name Local static functions */
48 Get next packet in layer-resolution-component-precinct order.
49 @param pi packet iterator to modify
50 @return returns false if pi pointed to the last packet or else returns true
52 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
54 Get next packet in resolution-layer-component-precinct order.
55 @param pi packet iterator to modify
56 @return returns false if pi pointed to the last packet or else returns true
58 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
60 Get next packet in resolution-precinct-component-layer order.
61 @param pi packet iterator to modify
62 @return returns false if pi pointed to the last packet or else returns true
64 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
66 Get next packet in precinct-component-resolution-layer order.
67 @param pi packet iterator to modify
68 @return returns false if pi pointed to the last packet or else returns true
70 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
72 Get next packet in component-precinct-resolution-layer order.
73 @param pi packet iterator to modify
74 @return returns false if pi pointed to the last packet or else returns true
76 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
79 * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
81 * @param p_cp the coding parameters to modify
82 * @param p_tileno the tile index being concerned.
83 * @param p_tx0 X0 parameter for the tile
84 * @param p_tx1 X1 parameter for the tile
85 * @param p_ty0 Y0 parameter for the tile
86 * @param p_ty1 Y1 parameter for the tile
87 * @param p_max_prec the maximum precision for all the bands of the tile
88 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
89 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
90 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
92 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
98 OPJ_UINT32 p_max_prec,
101 OPJ_UINT32 p_dy_min);
104 * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
106 * @param p_cp the coding parameters to modify
107 * @param p_num_comps the number of components
108 * @param p_tileno the tile index being concerned.
109 * @param p_tx0 X0 parameter for the tile
110 * @param p_tx1 X1 parameter for the tile
111 * @param p_ty0 Y0 parameter for the tile
112 * @param p_ty1 Y1 parameter for the tile
113 * @param p_max_prec the maximum precision for all the bands of the tile
114 * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
115 * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
116 * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
118 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
119 OPJ_UINT32 p_num_comps,
125 OPJ_UINT32 p_max_prec,
126 OPJ_UINT32 p_max_res,
128 OPJ_UINT32 p_dy_min);
130 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
132 * @param p_image the image being encoded.
133 * @param p_cp the coding parameters.
134 * @param tileno the tile index of the tile being encoded.
135 * @param p_tx0 pointer that will hold the X0 parameter for the tile
136 * @param p_tx1 pointer that will hold the X1 parameter for the tile
137 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
138 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
139 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
140 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
141 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
142 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
144 static void opj_get_encoding_parameters(const opj_image_t *p_image,
145 const opj_cp_t *p_cp,
151 OPJ_UINT32 * p_dx_min,
152 OPJ_UINT32 * p_dy_min,
153 OPJ_UINT32 * p_max_prec,
154 OPJ_UINT32 * p_max_res);
157 * Gets the encoding parameters needed to update the coding parameters and all the pocs.
158 * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
159 * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
160 * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
161 * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
163 * @param p_image the image being encoded.
164 * @param p_cp the coding parameters.
165 * @param tileno the tile index of the tile being encoded.
166 * @param p_tx0 pointer that will hold the X0 parameter for the tile
167 * @param p_tx1 pointer that will hold the X1 parameter for the tile
168 * @param p_ty0 pointer that will hold the Y0 parameter for the tile
169 * @param p_ty1 pointer that will hold the Y1 parameter for the tile
170 * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
171 * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
172 * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
173 * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
174 * @param p_resolutions pointer to an area corresponding to the one described above.
176 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
177 const opj_cp_t *p_cp,
183 OPJ_UINT32 * p_dx_min,
184 OPJ_UINT32 * p_dy_min,
185 OPJ_UINT32 * p_max_prec,
186 OPJ_UINT32 * p_max_res,
187 OPJ_UINT32 ** p_resolutions);
189 * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
190 * No other data is set. The include section of the packet iterator is not allocated.
192 * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
193 * @param p_cp the coding parameters.
194 * @param tileno the index of the tile from which creating the packet iterator.
196 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
197 const opj_cp_t *p_cp,
202 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
204 OPJ_UINT32 p_max_precision,
205 OPJ_UINT32 p_max_res);
209 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
211 OPJ_UINT32 p_max_precision,
212 OPJ_UINT32 p_max_res);
217 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
221 const OPJ_CHAR *prog);
228 ==========================================================
230 ==========================================================
233 static void opj_pi_emit_error(opj_pi_iterator_t * pi, const char* msg)
239 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
241 opj_pi_comp_t *comp = NULL;
242 opj_pi_resolution_t *res = NULL;
243 OPJ_UINT32 index = 0;
246 comp = &pi->comps[pi->compno];
247 res = &comp->resolutions[pi->resno];
253 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
254 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
256 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
257 comp = &pi->comps[pi->compno];
258 if (pi->resno >= comp->numresolutions) {
261 res = &comp->resolutions[pi->resno];
263 pi->poc.precno1 = res->pw * res->ph;
265 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
266 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
267 pi->step_c + pi->precno * pi->step_p;
268 /* Avoids index out of bounds access with */
269 /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
270 /* https://github.com/uclouvain/openjpeg/issues/938 */
271 /* Not sure if this is the most clever fix. Perhaps */
272 /* include should be resized when a POC arises, or */
273 /* the POC should be rejected */
274 if (index >= pi->include_size) {
275 opj_pi_emit_error(pi, "Invalid access to pi->include");
278 if (!pi->include[index]) {
279 pi->include[index] = 1;
292 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
294 opj_pi_comp_t *comp = NULL;
295 opj_pi_resolution_t *res = NULL;
296 OPJ_UINT32 index = 0;
299 comp = &pi->comps[pi->compno];
300 res = &comp->resolutions[pi->resno];
306 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
307 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
308 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
309 comp = &pi->comps[pi->compno];
310 if (pi->resno >= comp->numresolutions) {
313 res = &comp->resolutions[pi->resno];
315 pi->poc.precno1 = res->pw * res->ph;
317 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
318 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
319 pi->step_c + pi->precno * pi->step_p;
320 if (index >= pi->include_size) {
321 opj_pi_emit_error(pi, "Invalid access to pi->include");
324 if (!pi->include[index]) {
325 pi->include[index] = 1;
338 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
340 opj_pi_comp_t *comp = NULL;
341 opj_pi_resolution_t *res = NULL;
342 OPJ_UINT32 index = 0;
347 OPJ_UINT32 compno, resno;
351 for (compno = 0; compno < pi->numcomps; compno++) {
352 comp = &pi->comps[compno];
353 for (resno = 0; resno < comp->numresolutions; resno++) {
355 res = &comp->resolutions[resno];
356 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
357 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
358 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
359 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
361 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
362 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
363 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
364 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
368 if (pi->dx == 0 || pi->dy == 0) {
373 pi->poc.ty0 = pi->ty0;
374 pi->poc.tx0 = pi->tx0;
375 pi->poc.ty1 = pi->ty1;
376 pi->poc.tx1 = pi->tx1;
378 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
379 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
380 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
381 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
382 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
383 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
385 OPJ_INT32 trx0, try0;
386 OPJ_INT32 trx1, try1;
388 OPJ_INT32 prci, prcj;
389 comp = &pi->comps[pi->compno];
390 if (pi->resno >= comp->numresolutions) {
393 res = &comp->resolutions[pi->resno];
394 levelno = comp->numresolutions - 1 - pi->resno;
395 /* Avoids division by zero */
396 /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
397 /* of https://github.com/uclouvain/openjpeg/issues/938 */
399 ((comp->dx << levelno) >> levelno) != comp->dx ||
400 ((comp->dy << levelno) >> levelno) != comp->dy) {
403 if ((comp->dx << levelno) > INT_MAX ||
404 (comp->dy << levelno) > INT_MAX) {
407 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
408 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
409 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
410 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
411 rpx = res->pdx + levelno;
412 rpy = res->pdy + levelno;
414 /* To avoid divisions by zero / undefined behaviour on shift */
416 /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */
417 /* of https://github.com/uclouvain/openjpeg/issues/938 */
418 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
419 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
423 /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
424 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
425 ((try0 << levelno) % (1 << rpy))))) {
428 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
429 ((trx0 << levelno) % (1 << rpx))))) {
433 if ((res->pw == 0) || (res->ph == 0)) {
437 if ((trx0 == trx1) || (try0 == try1)) {
441 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
442 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
443 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
444 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
445 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
446 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
447 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
448 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
449 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
450 pi->step_c + pi->precno * pi->step_p;
451 if (index >= pi->include_size) {
452 opj_pi_emit_error(pi, "Invalid access to pi->include");
455 if (!pi->include[index]) {
456 pi->include[index] = 1;
470 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
472 opj_pi_comp_t *comp = NULL;
473 opj_pi_resolution_t *res = NULL;
474 OPJ_UINT32 index = 0;
477 comp = &pi->comps[pi->compno];
480 OPJ_UINT32 compno, resno;
484 for (compno = 0; compno < pi->numcomps; compno++) {
485 comp = &pi->comps[compno];
486 for (resno = 0; resno < comp->numresolutions; resno++) {
488 res = &comp->resolutions[resno];
489 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
490 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
491 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
492 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
494 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
495 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
496 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
497 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
501 if (pi->dx == 0 || pi->dy == 0) {
506 pi->poc.ty0 = pi->ty0;
507 pi->poc.tx0 = pi->tx0;
508 pi->poc.ty1 = pi->ty1;
509 pi->poc.tx1 = pi->tx1;
511 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
512 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
513 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
514 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
515 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
516 comp = &pi->comps[pi->compno];
517 for (pi->resno = pi->poc.resno0;
518 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
520 OPJ_INT32 trx0, try0;
521 OPJ_INT32 trx1, try1;
523 OPJ_INT32 prci, prcj;
524 res = &comp->resolutions[pi->resno];
525 levelno = comp->numresolutions - 1 - pi->resno;
526 /* Avoids division by zero */
527 /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
528 /* of https://github.com/uclouvain/openjpeg/issues/938 */
530 ((comp->dx << levelno) >> levelno) != comp->dx ||
531 ((comp->dy << levelno) >> levelno) != comp->dy) {
534 if ((comp->dx << levelno) > INT_MAX ||
535 (comp->dy << levelno) > INT_MAX) {
538 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
539 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
540 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
541 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
542 rpx = res->pdx + levelno;
543 rpy = res->pdy + levelno;
545 /* To avoid divisions by zero / undefined behaviour on shift */
547 /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
548 /* of https://github.com/uclouvain/openjpeg/issues/938 */
549 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
550 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
554 /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
555 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
556 ((try0 << levelno) % (1 << rpy))))) {
559 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
560 ((trx0 << levelno) % (1 << rpx))))) {
564 if ((res->pw == 0) || (res->ph == 0)) {
568 if ((trx0 == trx1) || (try0 == try1)) {
572 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
573 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
574 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
575 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
576 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
577 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
578 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
579 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
580 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
581 pi->step_c + pi->precno * pi->step_p;
582 if (index >= pi->include_size) {
583 opj_pi_emit_error(pi, "Invalid access to pi->include");
586 if (!pi->include[index]) {
587 pi->include[index] = 1;
601 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
603 opj_pi_comp_t *comp = NULL;
604 opj_pi_resolution_t *res = NULL;
605 OPJ_UINT32 index = 0;
608 comp = &pi->comps[pi->compno];
614 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
616 comp = &pi->comps[pi->compno];
619 for (resno = 0; resno < comp->numresolutions; resno++) {
621 res = &comp->resolutions[resno];
622 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
623 comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
624 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
625 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
627 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
628 comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
629 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
630 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
633 if (pi->dx == 0 || pi->dy == 0) {
637 pi->poc.ty0 = pi->ty0;
638 pi->poc.tx0 = pi->tx0;
639 pi->poc.ty1 = pi->ty1;
640 pi->poc.tx1 = pi->tx1;
642 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
643 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
644 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
645 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
646 for (pi->resno = pi->poc.resno0;
647 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
649 OPJ_INT32 trx0, try0;
650 OPJ_INT32 trx1, try1;
652 OPJ_INT32 prci, prcj;
653 res = &comp->resolutions[pi->resno];
654 levelno = comp->numresolutions - 1 - pi->resno;
655 /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
656 /* of https://github.com/uclouvain/openjpeg/issues/938 */
658 ((comp->dx << levelno) >> levelno) != comp->dx ||
659 ((comp->dy << levelno) >> levelno) != comp->dy) {
662 if ((comp->dx << levelno) > INT_MAX ||
663 (comp->dy << levelno) > INT_MAX) {
666 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
667 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
668 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
669 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
670 rpx = res->pdx + levelno;
671 rpy = res->pdy + levelno;
673 /* To avoid divisions by zero / undefined behaviour on shift */
675 /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
676 /* of https://github.com/uclouvain/openjpeg/issues/938 */
677 if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
678 rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
682 /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
683 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
684 ((try0 << levelno) % (1 << rpy))))) {
687 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
688 ((trx0 << levelno) % (1 << rpx))))) {
692 if ((res->pw == 0) || (res->ph == 0)) {
696 if ((trx0 == trx1) || (try0 == try1)) {
700 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
701 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
702 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
703 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
704 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
705 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
706 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
707 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
708 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
709 pi->step_c + pi->precno * pi->step_p;
710 if (index >= pi->include_size) {
711 opj_pi_emit_error(pi, "Invalid access to pi->include");
714 if (!pi->include[index]) {
715 pi->include[index] = 1;
729 static void opj_get_encoding_parameters(const opj_image_t *p_image,
730 const opj_cp_t *p_cp,
736 OPJ_UINT32 * p_dx_min,
737 OPJ_UINT32 * p_dy_min,
738 OPJ_UINT32 * p_max_prec,
739 OPJ_UINT32 * p_max_res)
742 OPJ_UINT32 compno, resno;
744 const opj_tcp_t *l_tcp = 00;
745 const opj_tccp_t * l_tccp = 00;
746 const opj_image_comp_t * l_img_comp = 00;
748 /* position in x and y of tile */
751 /* non-corrected (in regard to image offset) tile offset */
752 OPJ_UINT32 l_tx0, l_ty0;
756 assert(p_image != 00);
757 assert(p_tileno < p_cp->tw * p_cp->th);
759 /* initializations */
760 l_tcp = &p_cp->tcps [p_tileno];
761 l_img_comp = p_image->comps;
762 l_tccp = l_tcp->tccps;
764 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
765 p = p_tileno % p_cp->tw;
766 q = p_tileno / p_cp->tw;
768 /* find extent of tile */
769 l_tx0 = p_cp->tx0 + p *
770 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
771 *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
772 *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
773 l_ty0 = p_cp->ty0 + q *
774 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
775 *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
776 *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
778 /* max precision is 0 (can only grow) */
782 /* take the largest value for dx_min and dy_min */
783 *p_dx_min = 0x7fffffff;
784 *p_dy_min = 0x7fffffff;
786 for (compno = 0; compno < p_image->numcomps; ++compno) {
787 /* arithmetic variables to calculate */
788 OPJ_UINT32 l_level_no;
789 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
790 OPJ_INT32 l_px0, l_py0, l_px1, py1;
791 OPJ_UINT32 l_pdx, l_pdy;
792 OPJ_UINT32 l_pw, l_ph;
793 OPJ_UINT32 l_product;
794 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
796 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
797 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
798 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
799 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
801 if (l_tccp->numresolutions > *p_max_res) {
802 *p_max_res = l_tccp->numresolutions;
805 /* use custom size for precincts */
806 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
807 OPJ_UINT32 l_dx, l_dy;
809 /* precinct width and height */
810 l_pdx = l_tccp->prcw[resno];
811 l_pdy = l_tccp->prch[resno];
813 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
814 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
816 /* take the minimum size for dx for each comp and resolution */
817 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
818 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
820 /* various calculations of extents */
821 l_level_no = l_tccp->numresolutions - 1 - resno;
823 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
824 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
825 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
826 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
828 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
829 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
830 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
832 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
834 l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
835 l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
837 l_product = l_pw * l_ph;
839 /* update precision */
840 if (l_product > *p_max_prec) {
841 *p_max_prec = l_product;
850 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
851 const opj_cp_t *p_cp,
857 OPJ_UINT32 * p_dx_min,
858 OPJ_UINT32 * p_dy_min,
859 OPJ_UINT32 * p_max_prec,
860 OPJ_UINT32 * p_max_res,
861 OPJ_UINT32 ** p_resolutions)
864 OPJ_UINT32 compno, resno;
867 const opj_tcp_t *tcp = 00;
868 const opj_tccp_t * l_tccp = 00;
869 const opj_image_comp_t * l_img_comp = 00;
871 /* to store l_dx, l_dy, w and h for each resolution and component.*/
872 OPJ_UINT32 * lResolutionPtr;
874 /* position in x and y of tile*/
877 /* non-corrected (in regard to image offset) tile offset */
878 OPJ_UINT32 l_tx0, l_ty0;
880 /* preconditions in debug*/
882 assert(p_image != 00);
883 assert(tileno < p_cp->tw * p_cp->th);
886 tcp = &p_cp->tcps [tileno];
888 l_img_comp = p_image->comps;
890 /* position in x and y of tile*/
891 p = tileno % p_cp->tw;
892 q = tileno / p_cp->tw;
894 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
895 l_tx0 = p_cp->tx0 + p *
896 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
897 *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
898 *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
899 l_ty0 = p_cp->ty0 + q *
900 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
901 *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
902 *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
904 /* max precision and resolution is 0 (can only grow)*/
908 /* take the largest value for dx_min and dy_min*/
909 *p_dx_min = 0x7fffffff;
910 *p_dy_min = 0x7fffffff;
912 for (compno = 0; compno < p_image->numcomps; ++compno) {
913 /* aritmetic variables to calculate*/
914 OPJ_UINT32 l_level_no;
915 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
916 OPJ_INT32 l_px0, l_py0, l_px1, py1;
917 OPJ_UINT32 l_product;
918 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
919 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
921 lResolutionPtr = p_resolutions[compno];
923 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
924 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
925 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
926 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
928 if (l_tccp->numresolutions > *p_max_res) {
929 *p_max_res = l_tccp->numresolutions;
932 /* use custom size for precincts*/
933 l_level_no = l_tccp->numresolutions;
934 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
935 OPJ_UINT32 l_dx, l_dy;
939 /* precinct width and height*/
940 l_pdx = l_tccp->prcw[resno];
941 l_pdy = l_tccp->prch[resno];
942 *lResolutionPtr++ = l_pdx;
943 *lResolutionPtr++ = l_pdy;
944 if (l_pdx + l_level_no < 32 &&
945 l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
946 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
947 /* take the minimum size for l_dx for each comp and resolution*/
948 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dx_min, (OPJ_INT32)l_dx);
950 if (l_pdy + l_level_no < 32 &&
951 l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
952 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
953 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dy_min, (OPJ_INT32)l_dy);
956 /* various calculations of extents*/
957 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
958 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
959 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
960 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
961 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
962 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
963 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
964 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
965 l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
966 l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
967 *lResolutionPtr++ = l_pw;
968 *lResolutionPtr++ = l_ph;
969 l_product = l_pw * l_ph;
971 /* update precision*/
972 if (l_product > *p_max_prec) {
973 *p_max_prec = l_product;
982 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
987 OPJ_UINT32 pino, compno;
988 /* number of poc in the p_pi*/
989 OPJ_UINT32 l_poc_bound;
991 /* pointers to tile coding parameters and components.*/
992 opj_pi_iterator_t *l_pi = 00;
994 const opj_tccp_t *tccp = 00;
996 /* current packet iterator being allocated*/
997 opj_pi_iterator_t *l_current_pi = 00;
999 /* preconditions in debug*/
1001 assert(image != 00);
1002 assert(tileno < cp->tw * cp->th);
1004 /* initializations*/
1005 tcp = &cp->tcps[tileno];
1006 l_poc_bound = tcp->numpocs + 1;
1008 /* memory allocations*/
1009 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1010 sizeof(opj_pi_iterator_t));
1015 l_current_pi = l_pi;
1016 for (pino = 0; pino < l_poc_bound ; ++pino) {
1018 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1019 sizeof(opj_pi_comp_t));
1020 if (! l_current_pi->comps) {
1021 opj_pi_destroy(l_pi, l_poc_bound);
1025 l_current_pi->numcomps = image->numcomps;
1027 for (compno = 0; compno < image->numcomps; ++compno) {
1028 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1030 tccp = &tcp->tccps[compno];
1032 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1033 sizeof(opj_pi_resolution_t));
1034 if (!comp->resolutions) {
1035 opj_pi_destroy(l_pi, l_poc_bound);
1039 comp->numresolutions = tccp->numresolutions;
1046 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1047 OPJ_UINT32 p_tileno,
1052 OPJ_UINT32 p_max_prec,
1053 OPJ_UINT32 p_max_res,
1054 OPJ_UINT32 p_dx_min,
1055 OPJ_UINT32 p_dy_min)
1059 /* tile coding parameter*/
1060 opj_tcp_t *l_tcp = 00;
1061 /* current poc being updated*/
1062 opj_poc_t * l_current_poc = 00;
1065 OPJ_UINT32 l_poc_bound;
1067 OPJ_ARG_NOT_USED(p_max_res);
1069 /* preconditions in debug*/
1071 assert(p_tileno < p_cp->tw * p_cp->th);
1073 /* initializations*/
1074 l_tcp = &p_cp->tcps [p_tileno];
1075 /* number of iterations in the loop */
1076 l_poc_bound = l_tcp->numpocs + 1;
1078 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1079 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1080 l_current_poc = l_tcp->pocs;
1082 l_current_poc->compS = l_current_poc->compno0;
1083 l_current_poc->compE = l_current_poc->compno1;
1084 l_current_poc->resS = l_current_poc->resno0;
1085 l_current_poc->resE = l_current_poc->resno1;
1086 l_current_poc->layE = l_current_poc->layno1;
1088 /* special treatment for the first element*/
1089 l_current_poc->layS = 0;
1090 l_current_poc->prg = l_current_poc->prg1;
1091 l_current_poc->prcS = 0;
1093 l_current_poc->prcE = p_max_prec;
1094 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1095 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1096 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1097 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1098 l_current_poc->dx = p_dx_min;
1099 l_current_poc->dy = p_dy_min;
1102 for (pino = 1; pino < l_poc_bound ; ++pino) {
1103 l_current_poc->compS = l_current_poc->compno0;
1104 l_current_poc->compE = l_current_poc->compno1;
1105 l_current_poc->resS = l_current_poc->resno0;
1106 l_current_poc->resE = l_current_poc->resno1;
1107 l_current_poc->layE = l_current_poc->layno1;
1108 l_current_poc->prg = l_current_poc->prg1;
1109 l_current_poc->prcS = 0;
1110 /* special treatment here different from the first element*/
1111 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1112 l_current_poc->layE : 0;
1114 l_current_poc->prcE = p_max_prec;
1115 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1116 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1117 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1118 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1119 l_current_poc->dx = p_dx_min;
1120 l_current_poc->dy = p_dy_min;
1125 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1126 OPJ_UINT32 p_num_comps,
1127 OPJ_UINT32 p_tileno,
1132 OPJ_UINT32 p_max_prec,
1133 OPJ_UINT32 p_max_res,
1134 OPJ_UINT32 p_dx_min,
1135 OPJ_UINT32 p_dy_min)
1139 /* tile coding parameter*/
1140 opj_tcp_t *l_tcp = 00;
1141 /* current poc being updated*/
1142 opj_poc_t * l_current_poc = 00;
1144 OPJ_UINT32 l_poc_bound;
1146 /* preconditions in debug*/
1148 assert(p_tileno < p_cp->tw * p_cp->th);
1150 /* initializations*/
1151 l_tcp = &p_cp->tcps [p_tileno];
1153 /* number of iterations in the loop */
1154 l_poc_bound = l_tcp->numpocs + 1;
1156 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1157 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1158 l_current_poc = l_tcp->pocs;
1160 for (pino = 0; pino < l_poc_bound ; ++pino) {
1161 l_current_poc->compS = 0;
1162 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1163 l_current_poc->resS = 0;
1164 l_current_poc->resE = p_max_res;
1165 l_current_poc->layS = 0;
1166 l_current_poc->layE = l_tcp->numlayers;
1167 l_current_poc->prg = l_tcp->prg;
1168 l_current_poc->prcS = 0;
1169 l_current_poc->prcE = p_max_prec;
1170 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1171 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1172 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1173 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1174 l_current_poc->dx = p_dx_min;
1175 l_current_poc->dy = p_dy_min;
1180 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1182 OPJ_UINT32 p_max_precision,
1183 OPJ_UINT32 p_max_res)
1188 /* encoding prameters to set*/
1191 opj_pi_iterator_t * l_current_pi = 00;
1192 opj_poc_t* l_current_poc = 0;
1194 OPJ_ARG_NOT_USED(p_max_res);
1196 /* preconditions in debug*/
1198 assert(p_tcp != 00);
1200 /* initializations*/
1201 l_bound = p_tcp->numpocs + 1;
1202 l_current_pi = p_pi;
1203 l_current_poc = p_tcp->pocs;
1205 for (pino = 0; pino < l_bound; ++pino) {
1206 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1207 l_current_pi->first = 1;
1209 l_current_pi->poc.resno0 =
1210 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1211 l_current_pi->poc.compno0 =
1212 l_current_poc->compno0; /* Component Index #0 (Start) */
1213 l_current_pi->poc.layno0 = 0;
1214 l_current_pi->poc.precno0 = 0;
1215 l_current_pi->poc.resno1 =
1216 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1217 l_current_pi->poc.compno1 =
1218 l_current_poc->compno1; /* Component Index #0 (End) */
1219 l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1220 p_tcp->numlayers); /* Layer Index #0 (End) */
1221 l_current_pi->poc.precno1 = p_max_precision;
1227 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1229 OPJ_UINT32 p_max_precision,
1230 OPJ_UINT32 p_max_res)
1235 /* encoding prameters to set*/
1238 opj_pi_iterator_t * l_current_pi = 00;
1239 /* preconditions in debug*/
1240 assert(p_tcp != 00);
1243 /* initializations*/
1244 l_bound = p_tcp->numpocs + 1;
1245 l_current_pi = p_pi;
1247 for (pino = 0; pino < l_bound; ++pino) {
1248 l_current_pi->poc.prg = p_tcp->prg;
1249 l_current_pi->first = 1;
1250 l_current_pi->poc.resno0 = 0;
1251 l_current_pi->poc.compno0 = 0;
1252 l_current_pi->poc.layno0 = 0;
1253 l_current_pi->poc.precno0 = 0;
1254 l_current_pi->poc.resno1 = p_max_res;
1255 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1256 l_current_pi->poc.layno1 = p_tcp->numlayers;
1257 l_current_pi->poc.precno1 = p_max_precision;
1264 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1268 const OPJ_CHAR *prog)
1271 opj_tcp_t *tcps = &cp->tcps[tileno];
1272 opj_poc_t *tcp = &tcps->pocs[pino];
1275 for (i = pos; pos >= 0; i--) {
1278 if (tcp->res_t == tcp->resE) {
1279 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1289 if (tcp->comp_t == tcp->compE) {
1290 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1300 if (tcp->lay_t == tcp->layE) {
1301 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1312 case OPJ_LRCP: /* fall through */
1314 if (tcp->prc_t == tcp->prcE) {
1315 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1325 if (tcp->tx0_t == tcp->txE) {
1327 if (tcp->ty0_t == tcp->tyE) {
1328 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1349 ==========================================================
1350 Packet iterator interface
1351 ==========================================================
1353 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1355 OPJ_UINT32 p_tile_no)
1357 OPJ_UINT32 numcomps = p_image->numcomps;
1361 OPJ_UINT32 compno, resno;
1363 /* to store w, h, dx and dy fro all components and resolutions */
1364 OPJ_UINT32 * l_tmp_data;
1365 OPJ_UINT32 ** l_tmp_ptr;
1367 /* encoding prameters to set */
1368 OPJ_UINT32 l_max_res;
1369 OPJ_UINT32 l_max_prec;
1370 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1371 OPJ_UINT32 l_dx_min, l_dy_min;
1373 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1374 OPJ_UINT32 l_data_stride;
1377 opj_pi_iterator_t *l_pi = 00;
1378 opj_tcp_t *l_tcp = 00;
1379 const opj_tccp_t *l_tccp = 00;
1380 opj_pi_comp_t *l_current_comp = 00;
1381 opj_image_comp_t * l_img_comp = 00;
1382 opj_pi_iterator_t * l_current_pi = 00;
1383 OPJ_UINT32 * l_encoding_value_ptr = 00;
1385 /* preconditions in debug */
1387 assert(p_image != 00);
1388 assert(p_tile_no < p_cp->tw * p_cp->th);
1390 /* initializations */
1391 l_tcp = &p_cp->tcps[p_tile_no];
1392 l_bound = l_tcp->numpocs + 1;
1394 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1395 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1396 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1401 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1402 numcomps * sizeof(OPJ_UINT32 *));
1405 opj_free(l_tmp_data);
1409 /* memory allocation for pi */
1410 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1412 opj_free(l_tmp_data);
1413 opj_free(l_tmp_ptr);
1417 l_encoding_value_ptr = l_tmp_data;
1418 /* update pointer array */
1420 (compno = 0; compno < numcomps; ++compno) {
1421 l_tmp_ptr[compno] = l_encoding_value_ptr;
1422 l_encoding_value_ptr += l_data_stride;
1424 /* get encoding parameters */
1425 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1426 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1428 /* step calculations */
1430 l_step_c = l_max_prec * l_step_p;
1431 l_step_r = numcomps * l_step_c;
1432 l_step_l = l_max_res * l_step_r;
1434 /* set values for first packet iterator */
1435 l_current_pi = l_pi;
1437 /* memory allocation for include */
1438 /* prevent an integer overflow issue */
1439 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1440 l_current_pi->include = 00;
1441 if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1442 l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1443 l_current_pi->include = (OPJ_INT16*) opj_calloc(
1444 l_current_pi->include_size, sizeof(OPJ_INT16));
1447 if (!l_current_pi->include) {
1448 opj_free(l_tmp_data);
1449 opj_free(l_tmp_ptr);
1450 opj_pi_destroy(l_pi, l_bound);
1454 /* special treatment for the first packet iterator */
1455 l_current_comp = l_current_pi->comps;
1456 l_img_comp = p_image->comps;
1457 l_tccp = l_tcp->tccps;
1459 l_current_pi->tx0 = l_tx0;
1460 l_current_pi->ty0 = l_ty0;
1461 l_current_pi->tx1 = l_tx1;
1462 l_current_pi->ty1 = l_ty1;
1464 /*l_current_pi->dx = l_img_comp->dx;*/
1465 /*l_current_pi->dy = l_img_comp->dy;*/
1467 l_current_pi->step_p = l_step_p;
1468 l_current_pi->step_c = l_step_c;
1469 l_current_pi->step_r = l_step_r;
1470 l_current_pi->step_l = l_step_l;
1472 /* allocation for components and number of components has already been calculated by opj_pi_create */
1474 (compno = 0; compno < numcomps; ++compno) {
1475 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1476 l_encoding_value_ptr = l_tmp_ptr[compno];
1478 l_current_comp->dx = l_img_comp->dx;
1479 l_current_comp->dy = l_img_comp->dy;
1480 /* resolutions have already been initialized */
1482 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1483 l_res->pdx = *(l_encoding_value_ptr++);
1484 l_res->pdy = *(l_encoding_value_ptr++);
1485 l_res->pw = *(l_encoding_value_ptr++);
1486 l_res->ph = *(l_encoding_value_ptr++);
1495 for (pino = 1 ; pino < l_bound ; ++pino) {
1496 l_current_comp = l_current_pi->comps;
1497 l_img_comp = p_image->comps;
1498 l_tccp = l_tcp->tccps;
1500 l_current_pi->tx0 = l_tx0;
1501 l_current_pi->ty0 = l_ty0;
1502 l_current_pi->tx1 = l_tx1;
1503 l_current_pi->ty1 = l_ty1;
1504 /*l_current_pi->dx = l_dx_min;*/
1505 /*l_current_pi->dy = l_dy_min;*/
1506 l_current_pi->step_p = l_step_p;
1507 l_current_pi->step_c = l_step_c;
1508 l_current_pi->step_r = l_step_r;
1509 l_current_pi->step_l = l_step_l;
1511 /* allocation for components and number of components has already been calculated by opj_pi_create */
1513 (compno = 0; compno < numcomps; ++compno) {
1514 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1515 l_encoding_value_ptr = l_tmp_ptr[compno];
1517 l_current_comp->dx = l_img_comp->dx;
1518 l_current_comp->dy = l_img_comp->dy;
1519 /* resolutions have already been initialized */
1521 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1522 l_res->pdx = *(l_encoding_value_ptr++);
1523 l_res->pdy = *(l_encoding_value_ptr++);
1524 l_res->pw = *(l_encoding_value_ptr++);
1525 l_res->ph = *(l_encoding_value_ptr++);
1532 /* special treatment*/
1533 l_current_pi->include = (l_current_pi - 1)->include;
1534 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1537 opj_free(l_tmp_data);
1539 opj_free(l_tmp_ptr);
1543 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1545 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1552 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1554 OPJ_UINT32 p_tile_no,
1555 J2K_T2_MODE p_t2_mode)
1557 OPJ_UINT32 numcomps = p_image->numcomps;
1561 OPJ_UINT32 compno, resno;
1563 /* to store w, h, dx and dy fro all components and resolutions*/
1564 OPJ_UINT32 * l_tmp_data;
1565 OPJ_UINT32 ** l_tmp_ptr;
1567 /* encoding prameters to set*/
1568 OPJ_UINT32 l_max_res;
1569 OPJ_UINT32 l_max_prec;
1570 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1571 OPJ_UINT32 l_dx_min, l_dy_min;
1573 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1574 OPJ_UINT32 l_data_stride;
1577 opj_pi_iterator_t *l_pi = 00;
1578 opj_tcp_t *l_tcp = 00;
1579 const opj_tccp_t *l_tccp = 00;
1580 opj_pi_comp_t *l_current_comp = 00;
1581 opj_image_comp_t * l_img_comp = 00;
1582 opj_pi_iterator_t * l_current_pi = 00;
1583 OPJ_UINT32 * l_encoding_value_ptr = 00;
1585 /* preconditions in debug*/
1587 assert(p_image != 00);
1588 assert(p_tile_no < p_cp->tw * p_cp->th);
1590 /* initializations*/
1591 l_tcp = &p_cp->tcps[p_tile_no];
1592 l_bound = l_tcp->numpocs + 1;
1594 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1595 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1596 l_data_stride * numcomps * sizeof(OPJ_UINT32));
1601 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1602 numcomps * sizeof(OPJ_UINT32 *));
1604 opj_free(l_tmp_data);
1608 /* memory allocation for pi*/
1609 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1611 opj_free(l_tmp_data);
1612 opj_free(l_tmp_ptr);
1616 l_encoding_value_ptr = l_tmp_data;
1617 /* update pointer array*/
1618 for (compno = 0; compno < numcomps; ++compno) {
1619 l_tmp_ptr[compno] = l_encoding_value_ptr;
1620 l_encoding_value_ptr += l_data_stride;
1623 /* get encoding parameters*/
1624 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1625 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1627 /* step calculations*/
1629 l_step_c = l_max_prec * l_step_p;
1630 l_step_r = numcomps * l_step_c;
1631 l_step_l = l_max_res * l_step_r;
1633 /* set values for first packet iterator*/
1634 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1635 l_current_pi = l_pi;
1637 /* memory allocation for include*/
1638 l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1639 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1641 if (!l_current_pi->include) {
1642 opj_free(l_tmp_data);
1643 opj_free(l_tmp_ptr);
1644 opj_pi_destroy(l_pi, l_bound);
1648 /* special treatment for the first packet iterator*/
1649 l_current_comp = l_current_pi->comps;
1650 l_img_comp = p_image->comps;
1651 l_tccp = l_tcp->tccps;
1652 l_current_pi->tx0 = l_tx0;
1653 l_current_pi->ty0 = l_ty0;
1654 l_current_pi->tx1 = l_tx1;
1655 l_current_pi->ty1 = l_ty1;
1656 l_current_pi->dx = l_dx_min;
1657 l_current_pi->dy = l_dy_min;
1658 l_current_pi->step_p = l_step_p;
1659 l_current_pi->step_c = l_step_c;
1660 l_current_pi->step_r = l_step_r;
1661 l_current_pi->step_l = l_step_l;
1663 /* allocation for components and number of components has already been calculated by opj_pi_create */
1664 for (compno = 0; compno < numcomps; ++compno) {
1665 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1666 l_encoding_value_ptr = l_tmp_ptr[compno];
1668 l_current_comp->dx = l_img_comp->dx;
1669 l_current_comp->dy = l_img_comp->dy;
1671 /* resolutions have already been initialized */
1672 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1673 l_res->pdx = *(l_encoding_value_ptr++);
1674 l_res->pdy = *(l_encoding_value_ptr++);
1675 l_res->pw = *(l_encoding_value_ptr++);
1676 l_res->ph = *(l_encoding_value_ptr++);
1686 for (pino = 1 ; pino < l_bound ; ++pino) {
1687 l_current_comp = l_current_pi->comps;
1688 l_img_comp = p_image->comps;
1689 l_tccp = l_tcp->tccps;
1691 l_current_pi->tx0 = l_tx0;
1692 l_current_pi->ty0 = l_ty0;
1693 l_current_pi->tx1 = l_tx1;
1694 l_current_pi->ty1 = l_ty1;
1695 l_current_pi->dx = l_dx_min;
1696 l_current_pi->dy = l_dy_min;
1697 l_current_pi->step_p = l_step_p;
1698 l_current_pi->step_c = l_step_c;
1699 l_current_pi->step_r = l_step_r;
1700 l_current_pi->step_l = l_step_l;
1702 /* allocation for components and number of components has already been calculated by opj_pi_create */
1703 for (compno = 0; compno < numcomps; ++compno) {
1704 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1705 l_encoding_value_ptr = l_tmp_ptr[compno];
1707 l_current_comp->dx = l_img_comp->dx;
1708 l_current_comp->dy = l_img_comp->dy;
1709 /* resolutions have already been initialized */
1710 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1711 l_res->pdx = *(l_encoding_value_ptr++);
1712 l_res->pdy = *(l_encoding_value_ptr++);
1713 l_res->pw = *(l_encoding_value_ptr++);
1714 l_res->ph = *(l_encoding_value_ptr++);
1722 /* special treatment*/
1723 l_current_pi->include = (l_current_pi - 1)->include;
1724 l_current_pi->include_size = (l_current_pi - 1)->include_size;
1728 opj_free(l_tmp_data);
1730 opj_free(l_tmp_ptr);
1733 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1734 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1735 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1737 opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1738 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1744 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1750 J2K_T2_MODE t2_mode)
1752 const OPJ_CHAR *prog;
1754 OPJ_UINT32 incr_top = 1, resetX = 0;
1755 opj_tcp_t *tcps = &cp->tcps[tileno];
1756 opj_poc_t *tcp = &tcps->pocs[pino];
1758 prog = opj_j2k_convert_progression_order(tcp->prg);
1761 pi[pino].poc.prg = tcp->prg;
1763 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1764 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))) {
1765 pi[pino].poc.resno0 = tcp->resS;
1766 pi[pino].poc.resno1 = tcp->resE;
1767 pi[pino].poc.compno0 = tcp->compS;
1768 pi[pino].poc.compno1 = tcp->compE;
1769 pi[pino].poc.layno0 = tcp->layS;
1770 pi[pino].poc.layno1 = tcp->layE;
1771 pi[pino].poc.precno0 = tcp->prcS;
1772 pi[pino].poc.precno1 = tcp->prcE;
1773 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1774 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1775 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1776 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1778 for (i = tppos + 1; i < 4; i++) {
1781 pi[pino].poc.resno0 = tcp->resS;
1782 pi[pino].poc.resno1 = tcp->resE;
1785 pi[pino].poc.compno0 = tcp->compS;
1786 pi[pino].poc.compno1 = tcp->compE;
1789 pi[pino].poc.layno0 = tcp->layS;
1790 pi[pino].poc.layno1 = tcp->layE;
1796 pi[pino].poc.precno0 = tcp->prcS;
1797 pi[pino].poc.precno1 = tcp->prcE;
1800 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1801 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1802 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1803 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1811 for (i = tppos; i >= 0; i--) {
1814 tcp->comp_t = tcp->compS;
1815 pi[pino].poc.compno0 = tcp->comp_t;
1816 pi[pino].poc.compno1 = tcp->comp_t + 1;
1820 tcp->res_t = tcp->resS;
1821 pi[pino].poc.resno0 = tcp->res_t;
1822 pi[pino].poc.resno1 = tcp->res_t + 1;
1826 tcp->lay_t = tcp->layS;
1827 pi[pino].poc.layno0 = tcp->lay_t;
1828 pi[pino].poc.layno1 = tcp->lay_t + 1;
1835 tcp->prc_t = tcp->prcS;
1836 pi[pino].poc.precno0 = tcp->prc_t;
1837 pi[pino].poc.precno1 = tcp->prc_t + 1;
1841 tcp->tx0_t = tcp->txS;
1842 tcp->ty0_t = tcp->tyS;
1843 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1844 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1845 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1846 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1847 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1848 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1856 for (i = tppos; i >= 0; i--) {
1859 pi[pino].poc.compno0 = tcp->comp_t - 1;
1860 pi[pino].poc.compno1 = tcp->comp_t;
1863 pi[pino].poc.resno0 = tcp->res_t - 1;
1864 pi[pino].poc.resno1 = tcp->res_t;
1867 pi[pino].poc.layno0 = tcp->lay_t - 1;
1868 pi[pino].poc.layno1 = tcp->lay_t;
1874 pi[pino].poc.precno0 = tcp->prc_t - 1;
1875 pi[pino].poc.precno1 = tcp->prc_t;
1878 pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1879 pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1880 pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1881 pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1886 if (incr_top == 1) {
1889 if (tcp->res_t == tcp->resE) {
1890 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1891 tcp->res_t = tcp->resS;
1892 pi[pino].poc.resno0 = tcp->res_t;
1893 pi[pino].poc.resno1 = tcp->res_t + 1;
1900 pi[pino].poc.resno0 = tcp->res_t;
1901 pi[pino].poc.resno1 = tcp->res_t + 1;
1907 if (tcp->comp_t == tcp->compE) {
1908 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1909 tcp->comp_t = tcp->compS;
1910 pi[pino].poc.compno0 = tcp->comp_t;
1911 pi[pino].poc.compno1 = tcp->comp_t + 1;
1918 pi[pino].poc.compno0 = tcp->comp_t;
1919 pi[pino].poc.compno1 = tcp->comp_t + 1;
1925 if (tcp->lay_t == tcp->layE) {
1926 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1927 tcp->lay_t = tcp->layS;
1928 pi[pino].poc.layno0 = tcp->lay_t;
1929 pi[pino].poc.layno1 = tcp->lay_t + 1;
1936 pi[pino].poc.layno0 = tcp->lay_t;
1937 pi[pino].poc.layno1 = tcp->lay_t + 1;
1946 if (tcp->prc_t == tcp->prcE) {
1947 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1948 tcp->prc_t = tcp->prcS;
1949 pi[pino].poc.precno0 = tcp->prc_t;
1950 pi[pino].poc.precno1 = tcp->prc_t + 1;
1957 pi[pino].poc.precno0 = tcp->prc_t;
1958 pi[pino].poc.precno1 = tcp->prc_t + 1;
1964 if (tcp->tx0_t >= tcp->txE) {
1965 if (tcp->ty0_t >= tcp->tyE) {
1966 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1967 tcp->ty0_t = tcp->tyS;
1968 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1969 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1970 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1978 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1979 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1980 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1985 tcp->tx0_t = tcp->txS;
1986 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1987 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1988 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1991 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1992 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1993 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2006 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2007 OPJ_UINT32 p_nb_elements)
2009 OPJ_UINT32 compno, pino;
2010 opj_pi_iterator_t *l_current_pi = p_pi;
2012 if (p_pi->include) {
2013 opj_free(p_pi->include);
2016 for (pino = 0; pino < p_nb_elements; ++pino) {
2017 if (l_current_pi->comps) {
2018 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2019 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2020 if (l_current_component->resolutions) {
2021 opj_free(l_current_component->resolutions);
2022 l_current_component->resolutions = 00;
2025 ++l_current_component;
2027 opj_free(l_current_pi->comps);
2028 l_current_pi->comps = 0;
2038 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2040 OPJ_UINT32 p_tile_no)
2042 /* encoding parameters to set */
2043 OPJ_UINT32 l_max_res;
2044 OPJ_UINT32 l_max_prec;
2045 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
2046 OPJ_UINT32 l_dx_min, l_dy_min;
2049 opj_tcp_t *l_tcp = 00;
2053 assert(p_image != 00);
2054 assert(p_tile_no < p_cp->tw * p_cp->th);
2056 l_tcp = &(p_cp->tcps[p_tile_no]);
2058 /* get encoding parameters */
2059 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2060 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2063 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2064 l_max_prec, l_max_res, l_dx_min, l_dy_min);
2066 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2067 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2071 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2073 switch (pi->poc.prg) {
2075 return opj_pi_next_lrcp(pi);
2077 return opj_pi_next_rlcp(pi);
2079 return opj_pi_next_rpcl(pi);
2081 return opj_pi_next_pcrl(pi);
2083 return opj_pi_next_cprl(pi);
2084 case OPJ_PROG_UNKNOWN: