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 OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
235 opj_pi_comp_t *comp = NULL;
236 opj_pi_resolution_t *res = NULL;
237 OPJ_UINT32 index = 0;
240 comp = &pi->comps[pi->compno];
241 res = &comp->resolutions[pi->resno];
247 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
248 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
250 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
251 comp = &pi->comps[pi->compno];
252 if (pi->resno >= comp->numresolutions) {
255 res = &comp->resolutions[pi->resno];
257 pi->poc.precno1 = res->pw * res->ph;
259 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
260 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
261 pi->step_c + pi->precno * pi->step_p;
262 if (!pi->include[index]) {
263 pi->include[index] = 1;
276 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
278 opj_pi_comp_t *comp = NULL;
279 opj_pi_resolution_t *res = NULL;
280 OPJ_UINT32 index = 0;
283 comp = &pi->comps[pi->compno];
284 res = &comp->resolutions[pi->resno];
290 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
291 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
292 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
293 comp = &pi->comps[pi->compno];
294 if (pi->resno >= comp->numresolutions) {
297 res = &comp->resolutions[pi->resno];
299 pi->poc.precno1 = res->pw * res->ph;
301 for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
302 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
303 pi->step_c + pi->precno * pi->step_p;
304 if (!pi->include[index]) {
305 pi->include[index] = 1;
318 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
320 opj_pi_comp_t *comp = NULL;
321 opj_pi_resolution_t *res = NULL;
322 OPJ_UINT32 index = 0;
327 OPJ_UINT32 compno, resno;
331 for (compno = 0; compno < pi->numcomps; compno++) {
332 comp = &pi->comps[compno];
333 for (resno = 0; resno < comp->numresolutions; resno++) {
335 res = &comp->resolutions[resno];
336 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
337 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
338 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
339 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
344 pi->poc.ty0 = pi->ty0;
345 pi->poc.tx0 = pi->tx0;
346 pi->poc.ty1 = pi->ty1;
347 pi->poc.tx1 = pi->tx1;
349 for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
350 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
351 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
352 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
353 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
354 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
356 OPJ_INT32 trx0, try0;
357 OPJ_INT32 trx1, try1;
359 OPJ_INT32 prci, prcj;
360 comp = &pi->comps[pi->compno];
361 if (pi->resno >= comp->numresolutions) {
364 res = &comp->resolutions[pi->resno];
365 levelno = comp->numresolutions - 1 - pi->resno;
366 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
367 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
368 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
369 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
370 rpx = res->pdx + levelno;
371 rpy = res->pdy + levelno;
372 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
373 ((try0 << levelno) % (1 << rpy))))) {
376 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
377 ((trx0 << levelno) % (1 << rpx))))) {
381 if ((res->pw == 0) || (res->ph == 0)) {
385 if ((trx0 == trx1) || (try0 == try1)) {
389 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
390 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
391 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
392 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
393 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
394 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
395 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
396 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
397 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
398 pi->step_c + pi->precno * pi->step_p;
399 if (!pi->include[index]) {
400 pi->include[index] = 1;
414 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
416 opj_pi_comp_t *comp = NULL;
417 opj_pi_resolution_t *res = NULL;
418 OPJ_UINT32 index = 0;
421 comp = &pi->comps[pi->compno];
424 OPJ_UINT32 compno, resno;
428 for (compno = 0; compno < pi->numcomps; compno++) {
429 comp = &pi->comps[compno];
430 for (resno = 0; resno < comp->numresolutions; resno++) {
432 res = &comp->resolutions[resno];
433 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
434 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
435 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
436 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
441 pi->poc.ty0 = pi->ty0;
442 pi->poc.tx0 = pi->tx0;
443 pi->poc.ty1 = pi->ty1;
444 pi->poc.tx1 = pi->tx1;
446 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
447 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
448 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
449 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
450 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
451 comp = &pi->comps[pi->compno];
452 for (pi->resno = pi->poc.resno0;
453 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
455 OPJ_INT32 trx0, try0;
456 OPJ_INT32 trx1, try1;
458 OPJ_INT32 prci, prcj;
459 res = &comp->resolutions[pi->resno];
460 levelno = comp->numresolutions - 1 - pi->resno;
461 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
462 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
463 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
464 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
465 rpx = res->pdx + levelno;
466 rpy = res->pdy + levelno;
467 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
468 ((try0 << levelno) % (1 << rpy))))) {
471 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
472 ((trx0 << levelno) % (1 << rpx))))) {
476 if ((res->pw == 0) || (res->ph == 0)) {
480 if ((trx0 == trx1) || (try0 == try1)) {
484 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
485 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
486 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
487 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
488 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
489 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
490 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
491 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
492 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
493 pi->step_c + pi->precno * pi->step_p;
494 if (!pi->include[index]) {
495 pi->include[index] = 1;
509 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
511 opj_pi_comp_t *comp = NULL;
512 opj_pi_resolution_t *res = NULL;
513 OPJ_UINT32 index = 0;
516 comp = &pi->comps[pi->compno];
522 for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
524 comp = &pi->comps[pi->compno];
527 for (resno = 0; resno < comp->numresolutions; resno++) {
529 res = &comp->resolutions[resno];
530 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
531 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
532 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
533 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
536 pi->poc.ty0 = pi->ty0;
537 pi->poc.tx0 = pi->tx0;
538 pi->poc.ty1 = pi->ty1;
539 pi->poc.tx1 = pi->tx1;
541 for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
542 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
543 for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
544 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
545 for (pi->resno = pi->poc.resno0;
546 pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
548 OPJ_INT32 trx0, try0;
549 OPJ_INT32 trx1, try1;
551 OPJ_INT32 prci, prcj;
552 res = &comp->resolutions[pi->resno];
553 levelno = comp->numresolutions - 1 - pi->resno;
554 trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
555 try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
556 trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
557 try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
558 rpx = res->pdx + levelno;
559 rpy = res->pdy + levelno;
560 if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
561 ((try0 << levelno) % (1 << rpy))))) {
564 if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
565 ((trx0 << levelno) % (1 << rpx))))) {
569 if ((res->pw == 0) || (res->ph == 0)) {
573 if ((trx0 == trx1) || (try0 == try1)) {
577 prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
578 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
579 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
580 prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
581 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
582 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
583 pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
584 for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
585 index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
586 pi->step_c + pi->precno * pi->step_p;
587 if (!pi->include[index]) {
588 pi->include[index] = 1;
602 static void opj_get_encoding_parameters(const opj_image_t *p_image,
603 const opj_cp_t *p_cp,
609 OPJ_UINT32 * p_dx_min,
610 OPJ_UINT32 * p_dy_min,
611 OPJ_UINT32 * p_max_prec,
612 OPJ_UINT32 * p_max_res)
615 OPJ_UINT32 compno, resno;
617 const opj_tcp_t *l_tcp = 00;
618 const opj_tccp_t * l_tccp = 00;
619 const opj_image_comp_t * l_img_comp = 00;
621 /* position in x and y of tile */
626 assert(p_image != 00);
627 assert(p_tileno < p_cp->tw * p_cp->th);
629 /* initializations */
630 l_tcp = &p_cp->tcps [p_tileno];
631 l_img_comp = p_image->comps;
632 l_tccp = l_tcp->tccps;
634 /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
635 p = p_tileno % p_cp->tw;
636 q = p_tileno / p_cp->tw;
638 /* find extent of tile */
639 *p_tx0 = opj_int_max((OPJ_INT32)(p_cp->tx0 + p * p_cp->tdx),
640 (OPJ_INT32)p_image->x0);
641 *p_tx1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + (p + 1) * p_cp->tdx),
642 (OPJ_INT32)p_image->x1);
643 *p_ty0 = opj_int_max((OPJ_INT32)(p_cp->ty0 + q * p_cp->tdy),
644 (OPJ_INT32)p_image->y0);
645 *p_ty1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + (q + 1) * p_cp->tdy),
646 (OPJ_INT32)p_image->y1);
648 /* max precision is 0 (can only grow) */
652 /* take the largest value for dx_min and dy_min */
653 *p_dx_min = 0x7fffffff;
654 *p_dy_min = 0x7fffffff;
656 for (compno = 0; compno < p_image->numcomps; ++compno) {
657 /* arithmetic variables to calculate */
658 OPJ_UINT32 l_level_no;
659 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
660 OPJ_INT32 l_px0, l_py0, l_px1, py1;
661 OPJ_UINT32 l_pdx, l_pdy;
662 OPJ_UINT32 l_pw, l_ph;
663 OPJ_UINT32 l_product;
664 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
666 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
667 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
668 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
669 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
671 if (l_tccp->numresolutions > *p_max_res) {
672 *p_max_res = l_tccp->numresolutions;
675 /* use custom size for precincts */
676 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
677 OPJ_UINT32 l_dx, l_dy;
679 /* precinct width and height */
680 l_pdx = l_tccp->prcw[resno];
681 l_pdy = l_tccp->prch[resno];
683 l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
684 l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
686 /* take the minimum size for dx for each comp and resolution */
687 *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
688 *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
690 /* various calculations of extents */
691 l_level_no = l_tccp->numresolutions - 1 - resno;
693 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
694 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
695 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
696 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
698 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
699 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
700 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
702 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
704 l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
705 l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
707 l_product = l_pw * l_ph;
709 /* update precision */
710 if (l_product > *p_max_prec) {
711 *p_max_prec = l_product;
720 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
721 const opj_cp_t *p_cp,
727 OPJ_UINT32 * p_dx_min,
728 OPJ_UINT32 * p_dy_min,
729 OPJ_UINT32 * p_max_prec,
730 OPJ_UINT32 * p_max_res,
731 OPJ_UINT32 ** p_resolutions)
734 OPJ_UINT32 compno, resno;
737 const opj_tcp_t *tcp = 00;
738 const opj_tccp_t * l_tccp = 00;
739 const opj_image_comp_t * l_img_comp = 00;
741 /* to store l_dx, l_dy, w and h for each resolution and component.*/
742 OPJ_UINT32 * lResolutionPtr;
744 /* position in x and y of tile*/
747 /* non-corrected (in regard to image offset) tile offset */
748 OPJ_UINT32 l_tx0, l_ty0;
750 /* preconditions in debug*/
752 assert(p_image != 00);
753 assert(tileno < p_cp->tw * p_cp->th);
756 tcp = &p_cp->tcps [tileno];
758 l_img_comp = p_image->comps;
760 /* position in x and y of tile*/
761 p = tileno % p_cp->tw;
762 q = tileno / p_cp->tw;
764 /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
765 l_tx0 = p_cp->tx0 + p *
766 p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
767 *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
768 *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
769 l_ty0 = p_cp->ty0 + q *
770 p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
771 *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
772 *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
774 /* max precision and resolution is 0 (can only grow)*/
778 /* take the largest value for dx_min and dy_min*/
779 *p_dx_min = 0x7fffffff;
780 *p_dy_min = 0x7fffffff;
782 for (compno = 0; compno < p_image->numcomps; ++compno) {
783 /* aritmetic variables to calculate*/
784 OPJ_UINT32 l_level_no;
785 OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
786 OPJ_INT32 l_px0, l_py0, l_px1, py1;
787 OPJ_UINT32 l_product;
788 OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
789 OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
791 lResolutionPtr = p_resolutions[compno];
793 l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
794 l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
795 l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
796 l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
798 if (l_tccp->numresolutions > *p_max_res) {
799 *p_max_res = l_tccp->numresolutions;
802 /* use custom size for precincts*/
803 l_level_no = l_tccp->numresolutions;
804 for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
805 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];
812 *lResolutionPtr++ = l_pdx;
813 *lResolutionPtr++ = l_pdy;
814 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
815 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
816 /* take the minimum size for l_dx for each comp and resolution*/
817 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dx_min, (OPJ_INT32)l_dx);
818 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dy_min, (OPJ_INT32)l_dy);
820 /* various calculations of extents*/
821 l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
822 l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
823 l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
824 l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
825 l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
826 l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
827 l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
828 py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
829 l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
830 l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
831 *lResolutionPtr++ = l_pw;
832 *lResolutionPtr++ = l_ph;
833 l_product = l_pw * l_ph;
835 /* update precision*/
836 if (l_product > *p_max_prec) {
837 *p_max_prec = l_product;
846 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
851 OPJ_UINT32 pino, compno;
852 /* number of poc in the p_pi*/
853 OPJ_UINT32 l_poc_bound;
855 /* pointers to tile coding parameters and components.*/
856 opj_pi_iterator_t *l_pi = 00;
858 const opj_tccp_t *tccp = 00;
860 /* current packet iterator being allocated*/
861 opj_pi_iterator_t *l_current_pi = 00;
863 /* preconditions in debug*/
866 assert(tileno < cp->tw * cp->th);
869 tcp = &cp->tcps[tileno];
870 l_poc_bound = tcp->numpocs + 1;
872 /* memory allocations*/
873 l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
874 sizeof(opj_pi_iterator_t));
880 for (pino = 0; pino < l_poc_bound ; ++pino) {
882 l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
883 sizeof(opj_pi_comp_t));
884 if (! l_current_pi->comps) {
885 opj_pi_destroy(l_pi, l_poc_bound);
889 l_current_pi->numcomps = image->numcomps;
891 for (compno = 0; compno < image->numcomps; ++compno) {
892 opj_pi_comp_t *comp = &l_current_pi->comps[compno];
894 tccp = &tcp->tccps[compno];
896 comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
897 sizeof(opj_pi_resolution_t));
898 if (!comp->resolutions) {
899 opj_pi_destroy(l_pi, l_poc_bound);
903 comp->numresolutions = tccp->numresolutions;
910 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
916 OPJ_UINT32 p_max_prec,
917 OPJ_UINT32 p_max_res,
923 /* tile coding parameter*/
924 opj_tcp_t *l_tcp = 00;
925 /* current poc being updated*/
926 opj_poc_t * l_current_poc = 00;
929 OPJ_UINT32 l_poc_bound;
931 OPJ_ARG_NOT_USED(p_max_res);
933 /* preconditions in debug*/
935 assert(p_tileno < p_cp->tw * p_cp->th);
938 l_tcp = &p_cp->tcps [p_tileno];
939 /* number of iterations in the loop */
940 l_poc_bound = l_tcp->numpocs + 1;
942 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
943 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
944 l_current_poc = l_tcp->pocs;
946 l_current_poc->compS = l_current_poc->compno0;
947 l_current_poc->compE = l_current_poc->compno1;
948 l_current_poc->resS = l_current_poc->resno0;
949 l_current_poc->resE = l_current_poc->resno1;
950 l_current_poc->layE = l_current_poc->layno1;
952 /* special treatment for the first element*/
953 l_current_poc->layS = 0;
954 l_current_poc->prg = l_current_poc->prg1;
955 l_current_poc->prcS = 0;
957 l_current_poc->prcE = p_max_prec;
958 l_current_poc->txS = (OPJ_UINT32)p_tx0;
959 l_current_poc->txE = (OPJ_UINT32)p_tx1;
960 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
961 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
962 l_current_poc->dx = p_dx_min;
963 l_current_poc->dy = p_dy_min;
966 for (pino = 1; pino < l_poc_bound ; ++pino) {
967 l_current_poc->compS = l_current_poc->compno0;
968 l_current_poc->compE = l_current_poc->compno1;
969 l_current_poc->resS = l_current_poc->resno0;
970 l_current_poc->resE = l_current_poc->resno1;
971 l_current_poc->layE = l_current_poc->layno1;
972 l_current_poc->prg = l_current_poc->prg1;
973 l_current_poc->prcS = 0;
974 /* special treatment here different from the first element*/
975 l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
976 l_current_poc->layE : 0;
978 l_current_poc->prcE = p_max_prec;
979 l_current_poc->txS = (OPJ_UINT32)p_tx0;
980 l_current_poc->txE = (OPJ_UINT32)p_tx1;
981 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
982 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
983 l_current_poc->dx = p_dx_min;
984 l_current_poc->dy = p_dy_min;
989 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
990 OPJ_UINT32 p_num_comps,
996 OPJ_UINT32 p_max_prec,
997 OPJ_UINT32 p_max_res,
1003 /* tile coding parameter*/
1004 opj_tcp_t *l_tcp = 00;
1005 /* current poc being updated*/
1006 opj_poc_t * l_current_poc = 00;
1008 OPJ_UINT32 l_poc_bound;
1010 /* preconditions in debug*/
1012 assert(p_tileno < p_cp->tw * p_cp->th);
1014 /* initializations*/
1015 l_tcp = &p_cp->tcps [p_tileno];
1017 /* number of iterations in the loop */
1018 l_poc_bound = l_tcp->numpocs + 1;
1020 /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1021 store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1022 l_current_poc = l_tcp->pocs;
1024 for (pino = 0; pino < l_poc_bound ; ++pino) {
1025 l_current_poc->compS = 0;
1026 l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1027 l_current_poc->resS = 0;
1028 l_current_poc->resE = p_max_res;
1029 l_current_poc->layS = 0;
1030 l_current_poc->layE = l_tcp->numlayers;
1031 l_current_poc->prg = l_tcp->prg;
1032 l_current_poc->prcS = 0;
1033 l_current_poc->prcE = p_max_prec;
1034 l_current_poc->txS = (OPJ_UINT32)p_tx0;
1035 l_current_poc->txE = (OPJ_UINT32)p_tx1;
1036 l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1037 l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1038 l_current_poc->dx = p_dx_min;
1039 l_current_poc->dy = p_dy_min;
1044 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1046 OPJ_UINT32 p_max_precision,
1047 OPJ_UINT32 p_max_res)
1052 /* encoding prameters to set*/
1055 opj_pi_iterator_t * l_current_pi = 00;
1056 opj_poc_t* l_current_poc = 0;
1058 OPJ_ARG_NOT_USED(p_max_res);
1060 /* preconditions in debug*/
1062 assert(p_tcp != 00);
1064 /* initializations*/
1065 l_bound = p_tcp->numpocs + 1;
1066 l_current_pi = p_pi;
1067 l_current_poc = p_tcp->pocs;
1069 for (pino = 0; pino < l_bound; ++pino) {
1070 l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1071 l_current_pi->first = 1;
1073 l_current_pi->poc.resno0 =
1074 l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1075 l_current_pi->poc.compno0 =
1076 l_current_poc->compno0; /* Component Index #0 (Start) */
1077 l_current_pi->poc.layno0 = 0;
1078 l_current_pi->poc.precno0 = 0;
1079 l_current_pi->poc.resno1 =
1080 l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1081 l_current_pi->poc.compno1 =
1082 l_current_poc->compno1; /* Component Index #0 (End) */
1083 l_current_pi->poc.layno1 = l_current_poc->layno1; /* Layer Index #0 (End) */
1084 l_current_pi->poc.precno1 = p_max_precision;
1090 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1092 OPJ_UINT32 p_max_precision,
1093 OPJ_UINT32 p_max_res)
1098 /* encoding prameters to set*/
1101 opj_pi_iterator_t * l_current_pi = 00;
1102 /* preconditions in debug*/
1103 assert(p_tcp != 00);
1106 /* initializations*/
1107 l_bound = p_tcp->numpocs + 1;
1108 l_current_pi = p_pi;
1110 for (pino = 0; pino < l_bound; ++pino) {
1111 l_current_pi->poc.prg = p_tcp->prg;
1112 l_current_pi->first = 1;
1113 l_current_pi->poc.resno0 = 0;
1114 l_current_pi->poc.compno0 = 0;
1115 l_current_pi->poc.layno0 = 0;
1116 l_current_pi->poc.precno0 = 0;
1117 l_current_pi->poc.resno1 = p_max_res;
1118 l_current_pi->poc.compno1 = l_current_pi->numcomps;
1119 l_current_pi->poc.layno1 = p_tcp->numlayers;
1120 l_current_pi->poc.precno1 = p_max_precision;
1127 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1131 const OPJ_CHAR *prog)
1134 opj_tcp_t *tcps = &cp->tcps[tileno];
1135 opj_poc_t *tcp = &tcps->pocs[pino];
1138 for (i = pos; pos >= 0; i--) {
1141 if (tcp->res_t == tcp->resE) {
1142 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1152 if (tcp->comp_t == tcp->compE) {
1153 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1163 if (tcp->lay_t == tcp->layE) {
1164 if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1175 case OPJ_LRCP: /* fall through */
1177 if (tcp->prc_t == tcp->prcE) {
1178 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1188 if (tcp->tx0_t == tcp->txE) {
1190 if (tcp->ty0_t == tcp->tyE) {
1191 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1212 ==========================================================
1213 Packet iterator interface
1214 ==========================================================
1216 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1218 OPJ_UINT32 p_tile_no)
1222 OPJ_UINT32 compno, resno;
1224 /* to store w, h, dx and dy fro all components and resolutions */
1225 OPJ_UINT32 * l_tmp_data;
1226 OPJ_UINT32 ** l_tmp_ptr;
1228 /* encoding prameters to set */
1229 OPJ_UINT32 l_max_res;
1230 OPJ_UINT32 l_max_prec;
1231 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1232 OPJ_UINT32 l_dx_min, l_dy_min;
1234 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1235 OPJ_UINT32 l_data_stride;
1238 opj_pi_iterator_t *l_pi = 00;
1239 opj_tcp_t *l_tcp = 00;
1240 const opj_tccp_t *l_tccp = 00;
1241 opj_pi_comp_t *l_current_comp = 00;
1242 opj_image_comp_t * l_img_comp = 00;
1243 opj_pi_iterator_t * l_current_pi = 00;
1244 OPJ_UINT32 * l_encoding_value_ptr = 00;
1246 /* preconditions in debug */
1248 assert(p_image != 00);
1249 assert(p_tile_no < p_cp->tw * p_cp->th);
1251 /* initializations */
1252 l_tcp = &p_cp->tcps[p_tile_no];
1253 l_bound = l_tcp->numpocs + 1;
1255 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1256 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1257 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1262 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1263 p_image->numcomps * sizeof(OPJ_UINT32 *));
1266 opj_free(l_tmp_data);
1270 /* memory allocation for pi */
1271 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1273 opj_free(l_tmp_data);
1274 opj_free(l_tmp_ptr);
1278 l_encoding_value_ptr = l_tmp_data;
1279 /* update pointer array */
1281 (compno = 0; compno < p_image->numcomps; ++compno) {
1282 l_tmp_ptr[compno] = l_encoding_value_ptr;
1283 l_encoding_value_ptr += l_data_stride;
1285 /* get encoding parameters */
1286 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1287 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1289 /* step calculations */
1291 l_step_c = l_max_prec * l_step_p;
1292 l_step_r = p_image->numcomps * l_step_c;
1293 l_step_l = l_max_res * l_step_r;
1295 /* set values for first packet iterator */
1296 l_current_pi = l_pi;
1298 /* memory allocation for include */
1299 /* prevent an integer overflow issue */
1300 /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1301 l_current_pi->include = 00;
1302 if (l_step_l <= (SIZE_MAX / (l_tcp->numlayers + 1U))) {
1303 l_current_pi->include = (OPJ_INT16*) opj_calloc((size_t)(
1304 l_tcp->numlayers + 1U) * l_step_l, sizeof(OPJ_INT16));
1307 if (!l_current_pi->include) {
1308 opj_free(l_tmp_data);
1309 opj_free(l_tmp_ptr);
1310 opj_pi_destroy(l_pi, l_bound);
1314 /* special treatment for the first packet iterator */
1315 l_current_comp = l_current_pi->comps;
1316 l_img_comp = p_image->comps;
1317 l_tccp = l_tcp->tccps;
1319 l_current_pi->tx0 = l_tx0;
1320 l_current_pi->ty0 = l_ty0;
1321 l_current_pi->tx1 = l_tx1;
1322 l_current_pi->ty1 = l_ty1;
1324 /*l_current_pi->dx = l_img_comp->dx;*/
1325 /*l_current_pi->dy = l_img_comp->dy;*/
1327 l_current_pi->step_p = l_step_p;
1328 l_current_pi->step_c = l_step_c;
1329 l_current_pi->step_r = l_step_r;
1330 l_current_pi->step_l = l_step_l;
1332 /* allocation for components and number of components has already been calculated by opj_pi_create */
1334 (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1335 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1336 l_encoding_value_ptr = l_tmp_ptr[compno];
1338 l_current_comp->dx = l_img_comp->dx;
1339 l_current_comp->dy = l_img_comp->dy;
1340 /* resolutions have already been initialized */
1342 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1343 l_res->pdx = *(l_encoding_value_ptr++);
1344 l_res->pdy = *(l_encoding_value_ptr++);
1345 l_res->pw = *(l_encoding_value_ptr++);
1346 l_res->ph = *(l_encoding_value_ptr++);
1355 for (pino = 1 ; pino < l_bound ; ++pino) {
1356 l_current_comp = l_current_pi->comps;
1357 l_img_comp = p_image->comps;
1358 l_tccp = l_tcp->tccps;
1360 l_current_pi->tx0 = l_tx0;
1361 l_current_pi->ty0 = l_ty0;
1362 l_current_pi->tx1 = l_tx1;
1363 l_current_pi->ty1 = l_ty1;
1364 /*l_current_pi->dx = l_dx_min;*/
1365 /*l_current_pi->dy = l_dy_min;*/
1366 l_current_pi->step_p = l_step_p;
1367 l_current_pi->step_c = l_step_c;
1368 l_current_pi->step_r = l_step_r;
1369 l_current_pi->step_l = l_step_l;
1371 /* allocation for components and number of components has already been calculated by opj_pi_create */
1373 (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1374 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1375 l_encoding_value_ptr = l_tmp_ptr[compno];
1377 l_current_comp->dx = l_img_comp->dx;
1378 l_current_comp->dy = l_img_comp->dy;
1379 /* resolutions have already been initialized */
1381 (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1382 l_res->pdx = *(l_encoding_value_ptr++);
1383 l_res->pdy = *(l_encoding_value_ptr++);
1384 l_res->pw = *(l_encoding_value_ptr++);
1385 l_res->ph = *(l_encoding_value_ptr++);
1392 /* special treatment*/
1393 l_current_pi->include = (l_current_pi - 1)->include;
1396 opj_free(l_tmp_data);
1398 opj_free(l_tmp_ptr);
1402 opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1404 opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1411 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1413 OPJ_UINT32 p_tile_no,
1414 J2K_T2_MODE p_t2_mode)
1418 OPJ_UINT32 compno, resno;
1420 /* to store w, h, dx and dy fro all components and resolutions*/
1421 OPJ_UINT32 * l_tmp_data;
1422 OPJ_UINT32 ** l_tmp_ptr;
1424 /* encoding prameters to set*/
1425 OPJ_UINT32 l_max_res;
1426 OPJ_UINT32 l_max_prec;
1427 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1428 OPJ_UINT32 l_dx_min, l_dy_min;
1430 OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1431 OPJ_UINT32 l_data_stride;
1434 opj_pi_iterator_t *l_pi = 00;
1435 opj_tcp_t *l_tcp = 00;
1436 const opj_tccp_t *l_tccp = 00;
1437 opj_pi_comp_t *l_current_comp = 00;
1438 opj_image_comp_t * l_img_comp = 00;
1439 opj_pi_iterator_t * l_current_pi = 00;
1440 OPJ_UINT32 * l_encoding_value_ptr = 00;
1442 /* preconditions in debug*/
1444 assert(p_image != 00);
1445 assert(p_tile_no < p_cp->tw * p_cp->th);
1447 /* initializations*/
1448 l_tcp = &p_cp->tcps[p_tile_no];
1449 l_bound = l_tcp->numpocs + 1;
1451 l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1452 l_tmp_data = (OPJ_UINT32*)opj_malloc(
1453 l_data_stride * p_image->numcomps * sizeof(OPJ_UINT32));
1458 l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1459 p_image->numcomps * sizeof(OPJ_UINT32 *));
1461 opj_free(l_tmp_data);
1465 /* memory allocation for pi*/
1466 l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1468 opj_free(l_tmp_data);
1469 opj_free(l_tmp_ptr);
1473 l_encoding_value_ptr = l_tmp_data;
1474 /* update pointer array*/
1475 for (compno = 0; compno < p_image->numcomps; ++compno) {
1476 l_tmp_ptr[compno] = l_encoding_value_ptr;
1477 l_encoding_value_ptr += l_data_stride;
1480 /* get encoding parameters*/
1481 opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1482 &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1484 /* step calculations*/
1486 l_step_c = l_max_prec * l_step_p;
1487 l_step_r = p_image->numcomps * l_step_c;
1488 l_step_l = l_max_res * l_step_r;
1490 /* set values for first packet iterator*/
1491 l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1492 l_current_pi = l_pi;
1494 /* memory allocation for include*/
1495 l_current_pi->include = (OPJ_INT16*) opj_calloc(l_tcp->numlayers * l_step_l,
1497 if (!l_current_pi->include) {
1498 opj_free(l_tmp_data);
1499 opj_free(l_tmp_ptr);
1500 opj_pi_destroy(l_pi, l_bound);
1504 /* special treatment for the first packet iterator*/
1505 l_current_comp = l_current_pi->comps;
1506 l_img_comp = p_image->comps;
1507 l_tccp = l_tcp->tccps;
1508 l_current_pi->tx0 = l_tx0;
1509 l_current_pi->ty0 = l_ty0;
1510 l_current_pi->tx1 = l_tx1;
1511 l_current_pi->ty1 = l_ty1;
1512 l_current_pi->dx = l_dx_min;
1513 l_current_pi->dy = l_dy_min;
1514 l_current_pi->step_p = l_step_p;
1515 l_current_pi->step_c = l_step_c;
1516 l_current_pi->step_r = l_step_r;
1517 l_current_pi->step_l = l_step_l;
1519 /* allocation for components and number of components has already been calculated by opj_pi_create */
1520 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1521 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1522 l_encoding_value_ptr = l_tmp_ptr[compno];
1524 l_current_comp->dx = l_img_comp->dx;
1525 l_current_comp->dy = l_img_comp->dy;
1527 /* resolutions have already been initialized */
1528 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1529 l_res->pdx = *(l_encoding_value_ptr++);
1530 l_res->pdy = *(l_encoding_value_ptr++);
1531 l_res->pw = *(l_encoding_value_ptr++);
1532 l_res->ph = *(l_encoding_value_ptr++);
1542 for (pino = 1 ; pino < l_bound ; ++pino) {
1543 l_current_comp = l_current_pi->comps;
1544 l_img_comp = p_image->comps;
1545 l_tccp = l_tcp->tccps;
1547 l_current_pi->tx0 = l_tx0;
1548 l_current_pi->ty0 = l_ty0;
1549 l_current_pi->tx1 = l_tx1;
1550 l_current_pi->ty1 = l_ty1;
1551 l_current_pi->dx = l_dx_min;
1552 l_current_pi->dy = l_dy_min;
1553 l_current_pi->step_p = l_step_p;
1554 l_current_pi->step_c = l_step_c;
1555 l_current_pi->step_r = l_step_r;
1556 l_current_pi->step_l = l_step_l;
1558 /* allocation for components and number of components has already been calculated by opj_pi_create */
1559 for (compno = 0; compno < l_current_pi->numcomps; ++compno) {
1560 opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1561 l_encoding_value_ptr = l_tmp_ptr[compno];
1563 l_current_comp->dx = l_img_comp->dx;
1564 l_current_comp->dy = l_img_comp->dy;
1565 /* resolutions have already been initialized */
1566 for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1567 l_res->pdx = *(l_encoding_value_ptr++);
1568 l_res->pdy = *(l_encoding_value_ptr++);
1569 l_res->pw = *(l_encoding_value_ptr++);
1570 l_res->ph = *(l_encoding_value_ptr++);
1578 /* special treatment*/
1579 l_current_pi->include = (l_current_pi - 1)->include;
1583 opj_free(l_tmp_data);
1585 opj_free(l_tmp_ptr);
1588 if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1589 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1590 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1592 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
1593 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1599 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1605 J2K_T2_MODE t2_mode)
1607 const OPJ_CHAR *prog;
1609 OPJ_UINT32 incr_top = 1, resetX = 0;
1610 opj_tcp_t *tcps = &cp->tcps[tileno];
1611 opj_poc_t *tcp = &tcps->pocs[pino];
1613 prog = opj_j2k_convert_progression_order(tcp->prg);
1616 pi[pino].poc.prg = tcp->prg;
1618 if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1619 (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))) {
1620 pi[pino].poc.resno0 = tcp->resS;
1621 pi[pino].poc.resno1 = tcp->resE;
1622 pi[pino].poc.compno0 = tcp->compS;
1623 pi[pino].poc.compno1 = tcp->compE;
1624 pi[pino].poc.layno0 = tcp->layS;
1625 pi[pino].poc.layno1 = tcp->layE;
1626 pi[pino].poc.precno0 = tcp->prcS;
1627 pi[pino].poc.precno1 = tcp->prcE;
1628 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1629 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1630 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1631 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1633 for (i = tppos + 1; i < 4; i++) {
1636 pi[pino].poc.resno0 = tcp->resS;
1637 pi[pino].poc.resno1 = tcp->resE;
1640 pi[pino].poc.compno0 = tcp->compS;
1641 pi[pino].poc.compno1 = tcp->compE;
1644 pi[pino].poc.layno0 = tcp->layS;
1645 pi[pino].poc.layno1 = tcp->layE;
1651 pi[pino].poc.precno0 = tcp->prcS;
1652 pi[pino].poc.precno1 = tcp->prcE;
1655 pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1656 pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1657 pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1658 pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1666 for (i = tppos; i >= 0; i--) {
1669 tcp->comp_t = tcp->compS;
1670 pi[pino].poc.compno0 = tcp->comp_t;
1671 pi[pino].poc.compno1 = tcp->comp_t + 1;
1675 tcp->res_t = tcp->resS;
1676 pi[pino].poc.resno0 = tcp->res_t;
1677 pi[pino].poc.resno1 = tcp->res_t + 1;
1681 tcp->lay_t = tcp->layS;
1682 pi[pino].poc.layno0 = tcp->lay_t;
1683 pi[pino].poc.layno1 = tcp->lay_t + 1;
1690 tcp->prc_t = tcp->prcS;
1691 pi[pino].poc.precno0 = tcp->prc_t;
1692 pi[pino].poc.precno1 = tcp->prc_t + 1;
1696 tcp->tx0_t = tcp->txS;
1697 tcp->ty0_t = tcp->tyS;
1698 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1699 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1700 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1701 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1702 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1703 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1711 for (i = tppos; i >= 0; i--) {
1714 pi[pino].poc.compno0 = tcp->comp_t - 1;
1715 pi[pino].poc.compno1 = tcp->comp_t;
1718 pi[pino].poc.resno0 = tcp->res_t - 1;
1719 pi[pino].poc.resno1 = tcp->res_t;
1722 pi[pino].poc.layno0 = tcp->lay_t - 1;
1723 pi[pino].poc.layno1 = tcp->lay_t;
1729 pi[pino].poc.precno0 = tcp->prc_t - 1;
1730 pi[pino].poc.precno1 = tcp->prc_t;
1733 pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1734 pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1735 pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1736 pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1741 if (incr_top == 1) {
1744 if (tcp->res_t == tcp->resE) {
1745 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1746 tcp->res_t = tcp->resS;
1747 pi[pino].poc.resno0 = tcp->res_t;
1748 pi[pino].poc.resno1 = tcp->res_t + 1;
1755 pi[pino].poc.resno0 = tcp->res_t;
1756 pi[pino].poc.resno1 = tcp->res_t + 1;
1762 if (tcp->comp_t == tcp->compE) {
1763 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1764 tcp->comp_t = tcp->compS;
1765 pi[pino].poc.compno0 = tcp->comp_t;
1766 pi[pino].poc.compno1 = tcp->comp_t + 1;
1773 pi[pino].poc.compno0 = tcp->comp_t;
1774 pi[pino].poc.compno1 = tcp->comp_t + 1;
1780 if (tcp->lay_t == tcp->layE) {
1781 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1782 tcp->lay_t = tcp->layS;
1783 pi[pino].poc.layno0 = tcp->lay_t;
1784 pi[pino].poc.layno1 = tcp->lay_t + 1;
1791 pi[pino].poc.layno0 = tcp->lay_t;
1792 pi[pino].poc.layno1 = tcp->lay_t + 1;
1801 if (tcp->prc_t == tcp->prcE) {
1802 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1803 tcp->prc_t = tcp->prcS;
1804 pi[pino].poc.precno0 = tcp->prc_t;
1805 pi[pino].poc.precno1 = tcp->prc_t + 1;
1812 pi[pino].poc.precno0 = tcp->prc_t;
1813 pi[pino].poc.precno1 = tcp->prc_t + 1;
1819 if (tcp->tx0_t >= tcp->txE) {
1820 if (tcp->ty0_t >= tcp->tyE) {
1821 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1822 tcp->ty0_t = tcp->tyS;
1823 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1824 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1825 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1833 pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1834 pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1835 tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1840 tcp->tx0_t = tcp->txS;
1841 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1842 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1843 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1846 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1847 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1848 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1861 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
1862 OPJ_UINT32 p_nb_elements)
1864 OPJ_UINT32 compno, pino;
1865 opj_pi_iterator_t *l_current_pi = p_pi;
1867 if (p_pi->include) {
1868 opj_free(p_pi->include);
1871 for (pino = 0; pino < p_nb_elements; ++pino) {
1872 if (l_current_pi->comps) {
1873 opj_pi_comp_t *l_current_component = l_current_pi->comps;
1874 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
1875 if (l_current_component->resolutions) {
1876 opj_free(l_current_component->resolutions);
1877 l_current_component->resolutions = 00;
1880 ++l_current_component;
1882 opj_free(l_current_pi->comps);
1883 l_current_pi->comps = 0;
1893 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
1895 OPJ_UINT32 p_tile_no)
1897 /* encoding parameters to set */
1898 OPJ_UINT32 l_max_res;
1899 OPJ_UINT32 l_max_prec;
1900 OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1901 OPJ_UINT32 l_dx_min, l_dy_min;
1904 opj_tcp_t *l_tcp = 00;
1908 assert(p_image != 00);
1909 assert(p_tile_no < p_cp->tw * p_cp->th);
1911 l_tcp = &(p_cp->tcps[p_tile_no]);
1913 /* get encoding parameters */
1914 opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
1915 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
1918 opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1919 l_max_prec, l_max_res, l_dx_min, l_dy_min);
1921 opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
1922 l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1926 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
1928 switch (pi->poc.prg) {
1930 return opj_pi_next_lrcp(pi);
1932 return opj_pi_next_rlcp(pi);
1934 return opj_pi_next_rpcl(pi);
1936 return opj_pi_next_pcrl(pi);
1938 return opj_pi_next_cprl(pi);
1939 case OPJ_PROG_UNKNOWN: