1 /*
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.
6  *
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.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
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.
25  *
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.
37  */
38 
39 #include "opj_includes.h"
40 
41 /** @defgroup PI PI - Implementation of a packet iterator */
42 /*@{*/
43 
44 /** @name Local static functions */
45 /*@{*/
46 
47 /**
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
51 */
52 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
53 /**
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
57 */
58 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
59 /**
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
63 */
64 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
65 /**
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
69 */
70 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
71 /**
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
75 */
76 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
77 
78 /**
79  * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
80  *
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.
91  */
92 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
93         OPJ_UINT32 p_tileno,
94         OPJ_INT32 p_tx0,
95         OPJ_INT32 p_tx1,
96         OPJ_INT32 p_ty0,
97         OPJ_INT32 p_ty1,
98         OPJ_UINT32 p_max_prec,
99         OPJ_UINT32 p_max_res,
100         OPJ_UINT32 p_dx_min,
101         OPJ_UINT32 p_dy_min);
102 
103 /**
104  * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
105  *
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.
117  */
118 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
119         OPJ_UINT32 p_num_comps,
120         OPJ_UINT32 p_tileno,
121         OPJ_INT32 p_tx0,
122         OPJ_INT32 p_tx1,
123         OPJ_INT32 p_ty0,
124         OPJ_INT32 p_ty1,
125         OPJ_UINT32 p_max_prec,
126         OPJ_UINT32 p_max_res,
127         OPJ_UINT32 p_dx_min,
128         OPJ_UINT32 p_dy_min);
129 /**
130  * Gets the encoding parameters needed to update the coding parameters and all the pocs.
131  *
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.
143  */
144 static void opj_get_encoding_parameters(const opj_image_t *p_image,
145                                         const opj_cp_t *p_cp,
146                                         OPJ_UINT32  tileno,
147                                         OPJ_INT32  * p_tx0,
148                                         OPJ_INT32 * p_tx1,
149                                         OPJ_INT32 * p_ty0,
150                                         OPJ_INT32 * p_ty1,
151                                         OPJ_UINT32 * p_dx_min,
152                                         OPJ_UINT32 * p_dy_min,
153                                         OPJ_UINT32 * p_max_prec,
154                                         OPJ_UINT32 * p_max_res);
155 
156 /**
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 , ...
162  *
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.
175  */
176 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
177         const opj_cp_t *p_cp,
178         OPJ_UINT32 tileno,
179         OPJ_INT32 * p_tx0,
180         OPJ_INT32 * p_tx1,
181         OPJ_INT32 * p_ty0,
182         OPJ_INT32 * p_ty1,
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);
188 /**
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.
191  *
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.
195  */
196 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
197         const opj_cp_t *p_cp,
198         OPJ_UINT32 tileno);
199 /**
200  * FIXME DOC
201  */
202 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
203         opj_tcp_t * p_tcp,
204         OPJ_UINT32 p_max_precision,
205         OPJ_UINT32 p_max_res);
206 /**
207  * FIXME DOC
208  */
209 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
210                                      opj_tcp_t * p_tcp,
211                                      OPJ_UINT32 p_max_precision,
212                                      OPJ_UINT32 p_max_res);
213 
214 /**
215  * FIXME DOC
216  */
217 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
218                                         opj_cp_t *cp,
219                                         OPJ_UINT32 tileno,
220                                         OPJ_UINT32 pino,
221                                         const OPJ_CHAR *prog);
222 
223 /*@}*/
224 
225 /*@}*/
226 
227 /*
228 ==========================================================
229    local functions
230 ==========================================================
231 */
232 
opj_pi_emit_error(opj_pi_iterator_t * pi,const char * msg)233 static void opj_pi_emit_error(opj_pi_iterator_t * pi, const char* msg)
234 {
235     (void)pi;
236     (void)msg;
237 }
238 
opj_pi_next_lrcp(opj_pi_iterator_t * pi)239 static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
240 {
241     opj_pi_comp_t *comp = NULL;
242     opj_pi_resolution_t *res = NULL;
243     OPJ_UINT32 index = 0;
244 
245     if (!pi->first) {
246         comp = &pi->comps[pi->compno];
247         res = &comp->resolutions[pi->resno];
248         goto LABEL_SKIP;
249     } else {
250         pi->first = 0;
251     }
252 
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;
255                 pi->resno++) {
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) {
259                     continue;
260                 }
261                 res = &comp->resolutions[pi->resno];
262                 if (!pi->tp_on) {
263                     pi->poc.precno1 = res->pw * res->ph;
264                 }
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");
276                         return OPJ_FALSE;
277                     }
278                     if (!pi->include[index]) {
279                         pi->include[index] = 1;
280                         return OPJ_TRUE;
281                     }
282 LABEL_SKIP:
283                     ;
284                 }
285             }
286         }
287     }
288 
289     return OPJ_FALSE;
290 }
291 
opj_pi_next_rlcp(opj_pi_iterator_t * pi)292 static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
293 {
294     opj_pi_comp_t *comp = NULL;
295     opj_pi_resolution_t *res = NULL;
296     OPJ_UINT32 index = 0;
297 
298     if (!pi->first) {
299         comp = &pi->comps[pi->compno];
300         res = &comp->resolutions[pi->resno];
301         goto LABEL_SKIP;
302     } else {
303         pi->first = 0;
304     }
305 
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) {
311                     continue;
312                 }
313                 res = &comp->resolutions[pi->resno];
314                 if (!pi->tp_on) {
315                     pi->poc.precno1 = res->pw * res->ph;
316                 }
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");
322                         return OPJ_FALSE;
323                     }
324                     if (!pi->include[index]) {
325                         pi->include[index] = 1;
326                         return OPJ_TRUE;
327                     }
328 LABEL_SKIP:
329                     ;
330                 }
331             }
332         }
333     }
334 
335     return OPJ_FALSE;
336 }
337 
opj_pi_next_rpcl(opj_pi_iterator_t * pi)338 static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
339 {
340     opj_pi_comp_t *comp = NULL;
341     opj_pi_resolution_t *res = NULL;
342     OPJ_UINT32 index = 0;
343 
344     if (!pi->first) {
345         goto LABEL_SKIP;
346     } else {
347         OPJ_UINT32 compno, resno;
348         pi->first = 0;
349         pi->dx = 0;
350         pi->dy = 0;
351         for (compno = 0; compno < pi->numcomps; compno++) {
352             comp = &pi->comps[compno];
353             for (resno = 0; resno < comp->numresolutions; resno++) {
354                 OPJ_UINT32 dx, dy;
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);
360                 }
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);
365                 }
366             }
367         }
368         if (pi->dx == 0 || pi->dy == 0) {
369             return OPJ_FALSE;
370         }
371     }
372     if (!pi->tp_on) {
373         pi->poc.ty0 = pi->ty0;
374         pi->poc.tx0 = pi->tx0;
375         pi->poc.ty1 = pi->ty1;
376         pi->poc.tx1 = pi->tx1;
377     }
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++) {
384                     OPJ_UINT32 levelno;
385                     OPJ_INT32 trx0, try0;
386                     OPJ_INT32  trx1, try1;
387                     OPJ_UINT32  rpx, rpy;
388                     OPJ_INT32  prci, prcj;
389                     comp = &pi->comps[pi->compno];
390                     if (pi->resno >= comp->numresolutions) {
391                         continue;
392                     }
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 */
398                     if (levelno >= 32 ||
399                             ((comp->dx << levelno) >> levelno) != comp->dx ||
400                             ((comp->dy << levelno) >> levelno) != comp->dy) {
401                         continue;
402                     }
403                     if ((comp->dx << levelno) > INT_MAX ||
404                             (comp->dy << levelno) > INT_MAX) {
405                         continue;
406                     }
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;
413 
414                     /* To avoid divisions by zero / undefined behaviour on shift */
415                     /* in below tests */
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) {
420                         continue;
421                     }
422 
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))))) {
426                         continue;
427                     }
428                     if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
429                             ((trx0 << levelno) % (1 << rpx))))) {
430                         continue;
431                     }
432 
433                     if ((res->pw == 0) || (res->ph == 0)) {
434                         continue;
435                     }
436 
437                     if ((trx0 == trx1) || (try0 == try1)) {
438                         continue;
439                     }
440 
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                     if (pi->precno >= res->pw * res->ph) {
449                       return OPJ_FALSE;
450                     }
451                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
452                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
453                                 pi->step_c + pi->precno * pi->step_p;
454                         if (index >= pi->include_size) {
455                             opj_pi_emit_error(pi, "Invalid access to pi->include");
456                             return OPJ_FALSE;
457                         }
458                         if (!pi->include[index]) {
459                             pi->include[index] = 1;
460                             return OPJ_TRUE;
461                         }
462 LABEL_SKIP:
463                         ;
464                     }
465                 }
466             }
467         }
468     }
469 
470     return OPJ_FALSE;
471 }
472 
opj_pi_next_pcrl(opj_pi_iterator_t * pi)473 static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
474 {
475     opj_pi_comp_t *comp = NULL;
476     opj_pi_resolution_t *res = NULL;
477     OPJ_UINT32 index = 0;
478 
479     if (!pi->first) {
480         comp = &pi->comps[pi->compno];
481         goto LABEL_SKIP;
482     } else {
483         OPJ_UINT32 compno, resno;
484         pi->first = 0;
485         pi->dx = 0;
486         pi->dy = 0;
487         for (compno = 0; compno < pi->numcomps; compno++) {
488             comp = &pi->comps[compno];
489             for (resno = 0; resno < comp->numresolutions; resno++) {
490                 OPJ_UINT32 dx, dy;
491                 res = &comp->resolutions[resno];
492                 if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
493                         comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
494                     dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
495                     pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
496                 }
497                 if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
498                         comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
499                     dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
500                     pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
501                 }
502             }
503         }
504         if (pi->dx == 0 || pi->dy == 0) {
505             return OPJ_FALSE;
506         }
507     }
508     if (!pi->tp_on) {
509         pi->poc.ty0 = pi->ty0;
510         pi->poc.tx0 = pi->tx0;
511         pi->poc.ty1 = pi->ty1;
512         pi->poc.tx1 = pi->tx1;
513     }
514     for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
515             pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
516         for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
517                 pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
518             for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
519                 comp = &pi->comps[pi->compno];
520                 for (pi->resno = pi->poc.resno0;
521                         pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
522                     OPJ_UINT32 levelno;
523                     OPJ_INT32 trx0, try0;
524                     OPJ_INT32 trx1, try1;
525                     OPJ_UINT32 rpx, rpy;
526                     OPJ_INT32 prci, prcj;
527                     res = &comp->resolutions[pi->resno];
528                     levelno = comp->numresolutions - 1 - pi->resno;
529                     /* Avoids division by zero */
530                     /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
531                     /* of  https://github.com/uclouvain/openjpeg/issues/938 */
532                     if (levelno >= 32 ||
533                             ((comp->dx << levelno) >> levelno) != comp->dx ||
534                             ((comp->dy << levelno) >> levelno) != comp->dy) {
535                         continue;
536                     }
537                     if ((comp->dx << levelno) > INT_MAX ||
538                             (comp->dy << levelno) > INT_MAX) {
539                         continue;
540                     }
541                     trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
542                     try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
543                     trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
544                     try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
545                     rpx = res->pdx + levelno;
546                     rpy = res->pdy + levelno;
547 
548                     /* To avoid divisions by zero / undefined behaviour on shift */
549                     /* in below tests */
550                     /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
551                     /* of https://github.com/uclouvain/openjpeg/issues/938 */
552                     if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
553                             rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
554                         continue;
555                     }
556 
557                     /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
558                     if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
559                             ((try0 << levelno) % (1 << rpy))))) {
560                         continue;
561                     }
562                     if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
563                             ((trx0 << levelno) % (1 << rpx))))) {
564                         continue;
565                     }
566 
567                     if ((res->pw == 0) || (res->ph == 0)) {
568                         continue;
569                     }
570 
571                     if ((trx0 == trx1) || (try0 == try1)) {
572                         continue;
573                     }
574 
575                     prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
576                                                 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
577                            - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
578                     prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
579                                                 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
580                            - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
581                     pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
582                     if (pi->precno >= res->pw * res->ph) {
583                       return OPJ_FALSE;
584                     }
585                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
586                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
587                                 pi->step_c + pi->precno * pi->step_p;
588                         if (index >= pi->include_size) {
589                             opj_pi_emit_error(pi, "Invalid access to pi->include");
590                             return OPJ_FALSE;
591                         }
592                         if (!pi->include[index]) {
593                             pi->include[index] = 1;
594                             return OPJ_TRUE;
595                         }
596 LABEL_SKIP:
597                         ;
598                     }
599                 }
600             }
601         }
602     }
603 
604     return OPJ_FALSE;
605 }
606 
opj_pi_next_cprl(opj_pi_iterator_t * pi)607 static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
608 {
609     opj_pi_comp_t *comp = NULL;
610     opj_pi_resolution_t *res = NULL;
611     OPJ_UINT32 index = 0;
612 
613     if (!pi->first) {
614         comp = &pi->comps[pi->compno];
615         goto LABEL_SKIP;
616     } else {
617         pi->first = 0;
618     }
619 
620     for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
621         OPJ_UINT32 resno;
622         comp = &pi->comps[pi->compno];
623         pi->dx = 0;
624         pi->dy = 0;
625         for (resno = 0; resno < comp->numresolutions; resno++) {
626             OPJ_UINT32 dx, dy;
627             res = &comp->resolutions[resno];
628             if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
629                     comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
630                 dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
631                 pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
632             }
633             if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
634                     comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
635                 dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
636                 pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
637             }
638         }
639         if (pi->dx == 0 || pi->dy == 0) {
640             return OPJ_FALSE;
641         }
642         if (!pi->tp_on) {
643             pi->poc.ty0 = pi->ty0;
644             pi->poc.tx0 = pi->tx0;
645             pi->poc.ty1 = pi->ty1;
646             pi->poc.tx1 = pi->tx1;
647         }
648         for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1;
649                 pi->y += (OPJ_INT32)(pi->dy - (OPJ_UINT32)(pi->y % (OPJ_INT32)pi->dy))) {
650             for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1;
651                     pi->x += (OPJ_INT32)(pi->dx - (OPJ_UINT32)(pi->x % (OPJ_INT32)pi->dx))) {
652                 for (pi->resno = pi->poc.resno0;
653                         pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
654                     OPJ_UINT32 levelno;
655                     OPJ_INT32 trx0, try0;
656                     OPJ_INT32 trx1, try1;
657                     OPJ_UINT32 rpx, rpy;
658                     OPJ_INT32 prci, prcj;
659                     res = &comp->resolutions[pi->resno];
660                     levelno = comp->numresolutions - 1 - pi->resno;
661                     /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
662                     /* of  https://github.com/uclouvain/openjpeg/issues/938 */
663                     if (levelno >= 32 ||
664                             ((comp->dx << levelno) >> levelno) != comp->dx ||
665                             ((comp->dy << levelno) >> levelno) != comp->dy) {
666                         continue;
667                     }
668                     if ((comp->dx << levelno) > INT_MAX ||
669                             (comp->dy << levelno) > INT_MAX) {
670                         continue;
671                     }
672                     trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
673                     try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
674                     trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
675                     try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
676                     rpx = res->pdx + levelno;
677                     rpy = res->pdy + levelno;
678 
679                     /* To avoid divisions by zero / undefined behaviour on shift */
680                     /* in below tests */
681                     /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
682                     /* of https://github.com/uclouvain/openjpeg/issues/938 */
683                     if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
684                             rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
685                         continue;
686                     }
687 
688                     /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
689                     if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
690                             ((try0 << levelno) % (1 << rpy))))) {
691                         continue;
692                     }
693                     if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
694                             ((trx0 << levelno) % (1 << rpx))))) {
695                         continue;
696                     }
697 
698                     if ((res->pw == 0) || (res->ph == 0)) {
699                         continue;
700                     }
701 
702                     if ((trx0 == trx1) || (try0 == try1)) {
703                         continue;
704                     }
705 
706                     prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x,
707                                                 (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
708                            - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
709                     prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y,
710                                                 (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
711                            - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
712                     pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
713                     if (pi->precno >= res->pw * res->ph) {
714                       return OPJ_FALSE;
715                     }
716                     for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
717                         index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
718                                 pi->step_c + pi->precno * pi->step_p;
719                         if (index >= pi->include_size) {
720                             opj_pi_emit_error(pi, "Invalid access to pi->include");
721                             return OPJ_FALSE;
722                         }
723                         if (!pi->include[index]) {
724                             pi->include[index] = 1;
725                             return OPJ_TRUE;
726                         }
727 LABEL_SKIP:
728                         ;
729                     }
730                 }
731             }
732         }
733     }
734 
735     return OPJ_FALSE;
736 }
737 
opj_get_encoding_parameters(const opj_image_t * p_image,const opj_cp_t * p_cp,OPJ_UINT32 p_tileno,OPJ_INT32 * p_tx0,OPJ_INT32 * p_tx1,OPJ_INT32 * p_ty0,OPJ_INT32 * p_ty1,OPJ_UINT32 * p_dx_min,OPJ_UINT32 * p_dy_min,OPJ_UINT32 * p_max_prec,OPJ_UINT32 * p_max_res)738 static void opj_get_encoding_parameters(const opj_image_t *p_image,
739                                         const opj_cp_t *p_cp,
740                                         OPJ_UINT32 p_tileno,
741                                         OPJ_INT32 * p_tx0,
742                                         OPJ_INT32  * p_tx1,
743                                         OPJ_INT32  * p_ty0,
744                                         OPJ_INT32  * p_ty1,
745                                         OPJ_UINT32 * p_dx_min,
746                                         OPJ_UINT32 * p_dy_min,
747                                         OPJ_UINT32 * p_max_prec,
748                                         OPJ_UINT32 * p_max_res)
749 {
750     /* loop */
751     OPJ_UINT32  compno, resno;
752     /* pointers */
753     const opj_tcp_t *l_tcp = 00;
754     const opj_tccp_t * l_tccp = 00;
755     const opj_image_comp_t * l_img_comp = 00;
756 
757     /* position in x and y of tile */
758     OPJ_UINT32 p, q;
759 
760     /* non-corrected (in regard to image offset) tile offset */
761     OPJ_UINT32 l_tx0, l_ty0;
762 
763     /* preconditions */
764     assert(p_cp != 00);
765     assert(p_image != 00);
766     assert(p_tileno < p_cp->tw * p_cp->th);
767 
768     /* initializations */
769     l_tcp = &p_cp->tcps [p_tileno];
770     l_img_comp = p_image->comps;
771     l_tccp = l_tcp->tccps;
772 
773     /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
774     p = p_tileno % p_cp->tw;
775     q = p_tileno / p_cp->tw;
776 
777     /* find extent of tile */
778     l_tx0 = p_cp->tx0 + p *
779             p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
780     *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
781     *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
782     l_ty0 = p_cp->ty0 + q *
783             p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
784     *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
785     *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
786 
787     /* max precision is 0 (can only grow) */
788     *p_max_prec = 0;
789     *p_max_res = 0;
790 
791     /* take the largest value for dx_min and dy_min */
792     *p_dx_min = 0x7fffffff;
793     *p_dy_min  = 0x7fffffff;
794 
795     for (compno = 0; compno < p_image->numcomps; ++compno) {
796         /* arithmetic variables to calculate */
797         OPJ_UINT32 l_level_no;
798         OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
799         OPJ_INT32 l_px0, l_py0, l_px1, py1;
800         OPJ_UINT32 l_pdx, l_pdy;
801         OPJ_UINT32 l_pw, l_ph;
802         OPJ_UINT32 l_product;
803         OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
804 
805         l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
806         l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
807         l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
808         l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
809 
810         if (l_tccp->numresolutions > *p_max_res) {
811             *p_max_res = l_tccp->numresolutions;
812         }
813 
814         /* use custom size for precincts */
815         for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
816             OPJ_UINT32 l_dx, l_dy;
817 
818             /* precinct width and height */
819             l_pdx = l_tccp->prcw[resno];
820             l_pdy = l_tccp->prch[resno];
821 
822             l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
823             l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
824 
825             /* take the minimum size for dx for each comp and resolution */
826             *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
827             *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
828 
829             /* various calculations of extents */
830             l_level_no = l_tccp->numresolutions - 1 - resno;
831 
832             l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
833             l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
834             l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
835             l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
836 
837             l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
838             l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
839             l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
840 
841             py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
842 
843             l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
844             l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
845 
846             l_product = l_pw * l_ph;
847 
848             /* update precision */
849             if (l_product > *p_max_prec) {
850                 *p_max_prec = l_product;
851             }
852         }
853         ++l_img_comp;
854         ++l_tccp;
855     }
856 }
857 
858 
opj_get_all_encoding_parameters(const opj_image_t * p_image,const opj_cp_t * p_cp,OPJ_UINT32 tileno,OPJ_INT32 * p_tx0,OPJ_INT32 * p_tx1,OPJ_INT32 * p_ty0,OPJ_INT32 * p_ty1,OPJ_UINT32 * p_dx_min,OPJ_UINT32 * p_dy_min,OPJ_UINT32 * p_max_prec,OPJ_UINT32 * p_max_res,OPJ_UINT32 ** p_resolutions)859 static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
860         const opj_cp_t *p_cp,
861         OPJ_UINT32 tileno,
862         OPJ_INT32 * p_tx0,
863         OPJ_INT32 * p_tx1,
864         OPJ_INT32 * p_ty0,
865         OPJ_INT32 * p_ty1,
866         OPJ_UINT32 * p_dx_min,
867         OPJ_UINT32 * p_dy_min,
868         OPJ_UINT32 * p_max_prec,
869         OPJ_UINT32 * p_max_res,
870         OPJ_UINT32 ** p_resolutions)
871 {
872     /* loop*/
873     OPJ_UINT32 compno, resno;
874 
875     /* pointers*/
876     const opj_tcp_t *tcp = 00;
877     const opj_tccp_t * l_tccp = 00;
878     const opj_image_comp_t * l_img_comp = 00;
879 
880     /* to store l_dx, l_dy, w and h for each resolution and component.*/
881     OPJ_UINT32 * lResolutionPtr;
882 
883     /* position in x and y of tile*/
884     OPJ_UINT32 p, q;
885 
886     /* non-corrected (in regard to image offset) tile offset */
887     OPJ_UINT32 l_tx0, l_ty0;
888 
889     /* preconditions in debug*/
890     assert(p_cp != 00);
891     assert(p_image != 00);
892     assert(tileno < p_cp->tw * p_cp->th);
893 
894     /* initializations*/
895     tcp = &p_cp->tcps [tileno];
896     l_tccp = tcp->tccps;
897     l_img_comp = p_image->comps;
898 
899     /* position in x and y of tile*/
900     p = tileno % p_cp->tw;
901     q = tileno / p_cp->tw;
902 
903     /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
904     l_tx0 = p_cp->tx0 + p *
905             p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
906     *p_tx0 = (OPJ_INT32)opj_uint_max(l_tx0, p_image->x0);
907     *p_tx1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
908     l_ty0 = p_cp->ty0 + q *
909             p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
910     *p_ty0 = (OPJ_INT32)opj_uint_max(l_ty0, p_image->y0);
911     *p_ty1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
912 
913     /* max precision and resolution is 0 (can only grow)*/
914     *p_max_prec = 0;
915     *p_max_res = 0;
916 
917     /* take the largest value for dx_min and dy_min*/
918     *p_dx_min = 0x7fffffff;
919     *p_dy_min = 0x7fffffff;
920 
921     for (compno = 0; compno < p_image->numcomps; ++compno) {
922         /* aritmetic variables to calculate*/
923         OPJ_UINT32 l_level_no;
924         OPJ_INT32 l_rx0, l_ry0, l_rx1, l_ry1;
925         OPJ_INT32 l_px0, l_py0, l_px1, py1;
926         OPJ_UINT32 l_product;
927         OPJ_INT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
928         OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
929 
930         lResolutionPtr = p_resolutions[compno];
931 
932         l_tcx0 = opj_int_ceildiv(*p_tx0, (OPJ_INT32)l_img_comp->dx);
933         l_tcy0 = opj_int_ceildiv(*p_ty0, (OPJ_INT32)l_img_comp->dy);
934         l_tcx1 = opj_int_ceildiv(*p_tx1, (OPJ_INT32)l_img_comp->dx);
935         l_tcy1 = opj_int_ceildiv(*p_ty1, (OPJ_INT32)l_img_comp->dy);
936 
937         if (l_tccp->numresolutions > *p_max_res) {
938             *p_max_res = l_tccp->numresolutions;
939         }
940 
941         /* use custom size for precincts*/
942         l_level_no = l_tccp->numresolutions;
943         for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
944             OPJ_UINT32 l_dx, l_dy;
945 
946             --l_level_no;
947 
948             /* precinct width and height*/
949             l_pdx = l_tccp->prcw[resno];
950             l_pdy = l_tccp->prch[resno];
951             *lResolutionPtr++ = l_pdx;
952             *lResolutionPtr++ = l_pdy;
953             if (l_pdx + l_level_no < 32 &&
954                     l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
955                 l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
956                 /* take the minimum size for l_dx for each comp and resolution*/
957                 *p_dx_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dx_min, (OPJ_INT32)l_dx);
958             }
959             if (l_pdy + l_level_no < 32 &&
960                     l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
961                 l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
962                 *p_dy_min = (OPJ_UINT32)opj_int_min((OPJ_INT32) * p_dy_min, (OPJ_INT32)l_dy);
963             }
964 
965             /* various calculations of extents*/
966             l_rx0 = opj_int_ceildivpow2(l_tcx0, (OPJ_INT32)l_level_no);
967             l_ry0 = opj_int_ceildivpow2(l_tcy0, (OPJ_INT32)l_level_no);
968             l_rx1 = opj_int_ceildivpow2(l_tcx1, (OPJ_INT32)l_level_no);
969             l_ry1 = opj_int_ceildivpow2(l_tcy1, (OPJ_INT32)l_level_no);
970             l_px0 = opj_int_floordivpow2(l_rx0, (OPJ_INT32)l_pdx) << l_pdx;
971             l_py0 = opj_int_floordivpow2(l_ry0, (OPJ_INT32)l_pdy) << l_pdy;
972             l_px1 = opj_int_ceildivpow2(l_rx1, (OPJ_INT32)l_pdx) << l_pdx;
973             py1 = opj_int_ceildivpow2(l_ry1, (OPJ_INT32)l_pdy) << l_pdy;
974             l_pw = (l_rx0 == l_rx1) ? 0 : (OPJ_UINT32)((l_px1 - l_px0) >> l_pdx);
975             l_ph = (l_ry0 == l_ry1) ? 0 : (OPJ_UINT32)((py1 - l_py0) >> l_pdy);
976             *lResolutionPtr++ = l_pw;
977             *lResolutionPtr++ = l_ph;
978             l_product = l_pw * l_ph;
979 
980             /* update precision*/
981             if (l_product > *p_max_prec) {
982                 *p_max_prec = l_product;
983             }
984 
985         }
986         ++l_tccp;
987         ++l_img_comp;
988     }
989 }
990 
opj_pi_create(const opj_image_t * image,const opj_cp_t * cp,OPJ_UINT32 tileno)991 static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
992         const opj_cp_t *cp,
993         OPJ_UINT32 tileno)
994 {
995     /* loop*/
996     OPJ_UINT32 pino, compno;
997     /* number of poc in the p_pi*/
998     OPJ_UINT32 l_poc_bound;
999 
1000     /* pointers to tile coding parameters and components.*/
1001     opj_pi_iterator_t *l_pi = 00;
1002     opj_tcp_t *tcp = 00;
1003     const opj_tccp_t *tccp = 00;
1004 
1005     /* current packet iterator being allocated*/
1006     opj_pi_iterator_t *l_current_pi = 00;
1007 
1008     /* preconditions in debug*/
1009     assert(cp != 00);
1010     assert(image != 00);
1011     assert(tileno < cp->tw * cp->th);
1012 
1013     /* initializations*/
1014     tcp = &cp->tcps[tileno];
1015     l_poc_bound = tcp->numpocs + 1;
1016 
1017     /* memory allocations*/
1018     l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
1019                                            sizeof(opj_pi_iterator_t));
1020     if (!l_pi) {
1021         return NULL;
1022     }
1023 
1024     l_current_pi = l_pi;
1025     for (pino = 0; pino < l_poc_bound ; ++pino) {
1026 
1027         l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
1028                               sizeof(opj_pi_comp_t));
1029         if (! l_current_pi->comps) {
1030             opj_pi_destroy(l_pi, l_poc_bound);
1031             return NULL;
1032         }
1033 
1034         l_current_pi->numcomps = image->numcomps;
1035 
1036         for (compno = 0; compno < image->numcomps; ++compno) {
1037             opj_pi_comp_t *comp = &l_current_pi->comps[compno];
1038 
1039             tccp = &tcp->tccps[compno];
1040 
1041             comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
1042                                 sizeof(opj_pi_resolution_t));
1043             if (!comp->resolutions) {
1044                 opj_pi_destroy(l_pi, l_poc_bound);
1045                 return 00;
1046             }
1047 
1048             comp->numresolutions = tccp->numresolutions;
1049         }
1050         ++l_current_pi;
1051     }
1052     return l_pi;
1053 }
1054 
opj_pi_update_encode_poc_and_final(opj_cp_t * p_cp,OPJ_UINT32 p_tileno,OPJ_INT32 p_tx0,OPJ_INT32 p_tx1,OPJ_INT32 p_ty0,OPJ_INT32 p_ty1,OPJ_UINT32 p_max_prec,OPJ_UINT32 p_max_res,OPJ_UINT32 p_dx_min,OPJ_UINT32 p_dy_min)1055 static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
1056         OPJ_UINT32 p_tileno,
1057         OPJ_INT32 p_tx0,
1058         OPJ_INT32 p_tx1,
1059         OPJ_INT32 p_ty0,
1060         OPJ_INT32 p_ty1,
1061         OPJ_UINT32 p_max_prec,
1062         OPJ_UINT32 p_max_res,
1063         OPJ_UINT32 p_dx_min,
1064         OPJ_UINT32 p_dy_min)
1065 {
1066     /* loop*/
1067     OPJ_UINT32 pino;
1068     /* tile coding parameter*/
1069     opj_tcp_t *l_tcp = 00;
1070     /* current poc being updated*/
1071     opj_poc_t * l_current_poc = 00;
1072 
1073     /* number of pocs*/
1074     OPJ_UINT32 l_poc_bound;
1075 
1076     OPJ_ARG_NOT_USED(p_max_res);
1077 
1078     /* preconditions in debug*/
1079     assert(p_cp != 00);
1080     assert(p_tileno < p_cp->tw * p_cp->th);
1081 
1082     /* initializations*/
1083     l_tcp = &p_cp->tcps [p_tileno];
1084     /* number of iterations in the loop */
1085     l_poc_bound = l_tcp->numpocs + 1;
1086 
1087     /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1088        store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1089     l_current_poc = l_tcp->pocs;
1090 
1091     l_current_poc->compS = l_current_poc->compno0;
1092     l_current_poc->compE = l_current_poc->compno1;
1093     l_current_poc->resS = l_current_poc->resno0;
1094     l_current_poc->resE = l_current_poc->resno1;
1095     l_current_poc->layE = l_current_poc->layno1;
1096 
1097     /* special treatment for the first element*/
1098     l_current_poc->layS = 0;
1099     l_current_poc->prg  = l_current_poc->prg1;
1100     l_current_poc->prcS = 0;
1101 
1102     l_current_poc->prcE = p_max_prec;
1103     l_current_poc->txS = (OPJ_UINT32)p_tx0;
1104     l_current_poc->txE = (OPJ_UINT32)p_tx1;
1105     l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1106     l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1107     l_current_poc->dx = p_dx_min;
1108     l_current_poc->dy = p_dy_min;
1109 
1110     ++ l_current_poc;
1111     for (pino = 1; pino < l_poc_bound ; ++pino) {
1112         l_current_poc->compS = l_current_poc->compno0;
1113         l_current_poc->compE = l_current_poc->compno1;
1114         l_current_poc->resS = l_current_poc->resno0;
1115         l_current_poc->resE = l_current_poc->resno1;
1116         l_current_poc->layE = l_current_poc->layno1;
1117         l_current_poc->prg  = l_current_poc->prg1;
1118         l_current_poc->prcS = 0;
1119         /* special treatment here different from the first element*/
1120         l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
1121                               l_current_poc->layE : 0;
1122 
1123         l_current_poc->prcE = p_max_prec;
1124         l_current_poc->txS = (OPJ_UINT32)p_tx0;
1125         l_current_poc->txE = (OPJ_UINT32)p_tx1;
1126         l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1127         l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1128         l_current_poc->dx = p_dx_min;
1129         l_current_poc->dy = p_dy_min;
1130         ++ l_current_poc;
1131     }
1132 }
1133 
opj_pi_update_encode_not_poc(opj_cp_t * p_cp,OPJ_UINT32 p_num_comps,OPJ_UINT32 p_tileno,OPJ_INT32 p_tx0,OPJ_INT32 p_tx1,OPJ_INT32 p_ty0,OPJ_INT32 p_ty1,OPJ_UINT32 p_max_prec,OPJ_UINT32 p_max_res,OPJ_UINT32 p_dx_min,OPJ_UINT32 p_dy_min)1134 static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
1135         OPJ_UINT32 p_num_comps,
1136         OPJ_UINT32 p_tileno,
1137         OPJ_INT32 p_tx0,
1138         OPJ_INT32 p_tx1,
1139         OPJ_INT32 p_ty0,
1140         OPJ_INT32 p_ty1,
1141         OPJ_UINT32 p_max_prec,
1142         OPJ_UINT32 p_max_res,
1143         OPJ_UINT32 p_dx_min,
1144         OPJ_UINT32 p_dy_min)
1145 {
1146     /* loop*/
1147     OPJ_UINT32 pino;
1148     /* tile coding parameter*/
1149     opj_tcp_t *l_tcp = 00;
1150     /* current poc being updated*/
1151     opj_poc_t * l_current_poc = 00;
1152     /* number of pocs*/
1153     OPJ_UINT32 l_poc_bound;
1154 
1155     /* preconditions in debug*/
1156     assert(p_cp != 00);
1157     assert(p_tileno < p_cp->tw * p_cp->th);
1158 
1159     /* initializations*/
1160     l_tcp = &p_cp->tcps [p_tileno];
1161 
1162     /* number of iterations in the loop */
1163     l_poc_bound = l_tcp->numpocs + 1;
1164 
1165     /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
1166        store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
1167     l_current_poc = l_tcp->pocs;
1168 
1169     for (pino = 0; pino < l_poc_bound ; ++pino) {
1170         l_current_poc->compS = 0;
1171         l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
1172         l_current_poc->resS = 0;
1173         l_current_poc->resE = p_max_res;
1174         l_current_poc->layS = 0;
1175         l_current_poc->layE = l_tcp->numlayers;
1176         l_current_poc->prg  = l_tcp->prg;
1177         l_current_poc->prcS = 0;
1178         l_current_poc->prcE = p_max_prec;
1179         l_current_poc->txS = (OPJ_UINT32)p_tx0;
1180         l_current_poc->txE = (OPJ_UINT32)p_tx1;
1181         l_current_poc->tyS = (OPJ_UINT32)p_ty0;
1182         l_current_poc->tyE = (OPJ_UINT32)p_ty1;
1183         l_current_poc->dx = p_dx_min;
1184         l_current_poc->dy = p_dy_min;
1185         ++ l_current_poc;
1186     }
1187 }
1188 
opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)1189 static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
1190                                      opj_tcp_t * p_tcp,
1191                                      OPJ_UINT32 p_max_precision,
1192                                      OPJ_UINT32 p_max_res)
1193 {
1194     /* loop*/
1195     OPJ_UINT32 pino;
1196 
1197     /* encoding prameters to set*/
1198     OPJ_UINT32 l_bound;
1199 
1200     opj_pi_iterator_t * l_current_pi = 00;
1201     opj_poc_t* l_current_poc = 0;
1202 
1203     OPJ_ARG_NOT_USED(p_max_res);
1204 
1205     /* preconditions in debug*/
1206     assert(p_pi != 00);
1207     assert(p_tcp != 00);
1208 
1209     /* initializations*/
1210     l_bound = p_tcp->numpocs + 1;
1211     l_current_pi = p_pi;
1212     l_current_poc = p_tcp->pocs;
1213 
1214     for (pino = 0; pino < l_bound; ++pino) {
1215         l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
1216         l_current_pi->first = 1;
1217 
1218         l_current_pi->poc.resno0 =
1219             l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
1220         l_current_pi->poc.compno0 =
1221             l_current_poc->compno0; /* Component Index #0 (Start) */
1222         l_current_pi->poc.layno0 = 0;
1223         l_current_pi->poc.precno0 = 0;
1224         l_current_pi->poc.resno1 =
1225             l_current_poc->resno1; /* Resolution Level Index #0 (End) */
1226         l_current_pi->poc.compno1 =
1227             l_current_poc->compno1; /* Component Index #0 (End) */
1228         l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
1229                                                 p_tcp->numlayers); /* Layer Index #0 (End) */
1230         l_current_pi->poc.precno1 = p_max_precision;
1231         ++l_current_pi;
1232         ++l_current_poc;
1233     }
1234 }
1235 
opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,opj_tcp_t * p_tcp,OPJ_UINT32 p_max_precision,OPJ_UINT32 p_max_res)1236 static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
1237         opj_tcp_t * p_tcp,
1238         OPJ_UINT32 p_max_precision,
1239         OPJ_UINT32 p_max_res)
1240 {
1241     /* loop*/
1242     OPJ_UINT32 pino;
1243 
1244     /* encoding prameters to set*/
1245     OPJ_UINT32 l_bound;
1246 
1247     opj_pi_iterator_t * l_current_pi = 00;
1248     /* preconditions in debug*/
1249     assert(p_tcp != 00);
1250     assert(p_pi != 00);
1251 
1252     /* initializations*/
1253     l_bound = p_tcp->numpocs + 1;
1254     l_current_pi = p_pi;
1255 
1256     for (pino = 0; pino < l_bound; ++pino) {
1257         l_current_pi->poc.prg = p_tcp->prg;
1258         l_current_pi->first = 1;
1259         l_current_pi->poc.resno0 = 0;
1260         l_current_pi->poc.compno0 = 0;
1261         l_current_pi->poc.layno0 = 0;
1262         l_current_pi->poc.precno0 = 0;
1263         l_current_pi->poc.resno1 = p_max_res;
1264         l_current_pi->poc.compno1 = l_current_pi->numcomps;
1265         l_current_pi->poc.layno1 = p_tcp->numlayers;
1266         l_current_pi->poc.precno1 = p_max_precision;
1267         ++l_current_pi;
1268     }
1269 }
1270 
1271 
1272 
opj_pi_check_next_level(OPJ_INT32 pos,opj_cp_t * cp,OPJ_UINT32 tileno,OPJ_UINT32 pino,const OPJ_CHAR * prog)1273 static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
1274                                         opj_cp_t *cp,
1275                                         OPJ_UINT32 tileno,
1276                                         OPJ_UINT32 pino,
1277                                         const OPJ_CHAR *prog)
1278 {
1279     OPJ_INT32 i;
1280     opj_tcp_t *tcps = &cp->tcps[tileno];
1281     opj_poc_t *tcp = &tcps->pocs[pino];
1282 
1283     if (pos >= 0) {
1284         for (i = pos; pos >= 0; i--) {
1285             switch (prog[i]) {
1286             case 'R':
1287                 if (tcp->res_t == tcp->resE) {
1288                     if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1289                         return OPJ_TRUE;
1290                     } else {
1291                         return OPJ_FALSE;
1292                     }
1293                 } else {
1294                     return OPJ_TRUE;
1295                 }
1296                 break;
1297             case 'C':
1298                 if (tcp->comp_t == tcp->compE) {
1299                     if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1300                         return OPJ_TRUE;
1301                     } else {
1302                         return OPJ_FALSE;
1303                     }
1304                 } else {
1305                     return OPJ_TRUE;
1306                 }
1307                 break;
1308             case 'L':
1309                 if (tcp->lay_t == tcp->layE) {
1310                     if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
1311                         return OPJ_TRUE;
1312                     } else {
1313                         return OPJ_FALSE;
1314                     }
1315                 } else {
1316                     return OPJ_TRUE;
1317                 }
1318                 break;
1319             case 'P':
1320                 switch (tcp->prg) {
1321                 case OPJ_LRCP: /* fall through */
1322                 case OPJ_RLCP:
1323                     if (tcp->prc_t == tcp->prcE) {
1324                         if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1325                             return OPJ_TRUE;
1326                         } else {
1327                             return OPJ_FALSE;
1328                         }
1329                     } else {
1330                         return OPJ_TRUE;
1331                     }
1332                     break;
1333                 default:
1334                     if (tcp->tx0_t == tcp->txE) {
1335                         /*TY*/
1336                         if (tcp->ty0_t == tcp->tyE) {
1337                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1338                                 return OPJ_TRUE;
1339                             } else {
1340                                 return OPJ_FALSE;
1341                             }
1342                         } else {
1343                             return OPJ_TRUE;
1344                         }/*TY*/
1345                     } else {
1346                         return OPJ_TRUE;
1347                     }
1348                     break;
1349                 }/*end case P*/
1350             }/*end switch*/
1351         }/*end for*/
1352     }/*end if*/
1353     return OPJ_FALSE;
1354 }
1355 
1356 
1357 /*
1358 ==========================================================
1359    Packet iterator interface
1360 ==========================================================
1361 */
opj_pi_create_decode(opj_image_t * p_image,opj_cp_t * p_cp,OPJ_UINT32 p_tile_no)1362 opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
1363                                         opj_cp_t *p_cp,
1364                                         OPJ_UINT32 p_tile_no)
1365 {
1366     OPJ_UINT32 numcomps = p_image->numcomps;
1367 
1368     /* loop */
1369     OPJ_UINT32 pino;
1370     OPJ_UINT32 compno, resno;
1371 
1372     /* to store w, h, dx and dy fro all components and resolutions */
1373     OPJ_UINT32 * l_tmp_data;
1374     OPJ_UINT32 ** l_tmp_ptr;
1375 
1376     /* encoding prameters to set */
1377     OPJ_UINT32 l_max_res;
1378     OPJ_UINT32 l_max_prec;
1379     OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1380     OPJ_UINT32 l_dx_min, l_dy_min;
1381     OPJ_UINT32 l_bound;
1382     OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1383     OPJ_UINT32 l_data_stride;
1384 
1385     /* pointers */
1386     opj_pi_iterator_t *l_pi = 00;
1387     opj_tcp_t *l_tcp = 00;
1388     const opj_tccp_t *l_tccp = 00;
1389     opj_pi_comp_t *l_current_comp = 00;
1390     opj_image_comp_t * l_img_comp = 00;
1391     opj_pi_iterator_t * l_current_pi = 00;
1392     OPJ_UINT32 * l_encoding_value_ptr = 00;
1393 
1394     /* preconditions in debug */
1395     assert(p_cp != 00);
1396     assert(p_image != 00);
1397     assert(p_tile_no < p_cp->tw * p_cp->th);
1398 
1399     /* initializations */
1400     l_tcp = &p_cp->tcps[p_tile_no];
1401     l_bound = l_tcp->numpocs + 1;
1402 
1403     l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1404     l_tmp_data = (OPJ_UINT32*)opj_malloc(
1405                      l_data_stride * numcomps * sizeof(OPJ_UINT32));
1406     if
1407     (! l_tmp_data) {
1408         return 00;
1409     }
1410     l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1411                     numcomps * sizeof(OPJ_UINT32 *));
1412     if
1413     (! l_tmp_ptr) {
1414         opj_free(l_tmp_data);
1415         return 00;
1416     }
1417 
1418     /* memory allocation for pi */
1419     l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1420     if (!l_pi) {
1421         opj_free(l_tmp_data);
1422         opj_free(l_tmp_ptr);
1423         return 00;
1424     }
1425 
1426     l_encoding_value_ptr = l_tmp_data;
1427     /* update pointer array */
1428     for
1429     (compno = 0; compno < numcomps; ++compno) {
1430         l_tmp_ptr[compno] = l_encoding_value_ptr;
1431         l_encoding_value_ptr += l_data_stride;
1432     }
1433     /* get encoding parameters */
1434     opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1435                                     &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1436 
1437     /* step calculations */
1438     l_step_p = 1;
1439     l_step_c = l_max_prec * l_step_p;
1440     l_step_r = numcomps * l_step_c;
1441     l_step_l = l_max_res * l_step_r;
1442 
1443     /* set values for first packet iterator */
1444     l_current_pi = l_pi;
1445 
1446     /* memory allocation for include */
1447     /* prevent an integer overflow issue */
1448     /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
1449     l_current_pi->include = 00;
1450     if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
1451         l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
1452         l_current_pi->include = (OPJ_INT16*) opj_calloc(
1453                                     l_current_pi->include_size, sizeof(OPJ_INT16));
1454     }
1455 
1456     if (!l_current_pi->include) {
1457         opj_free(l_tmp_data);
1458         opj_free(l_tmp_ptr);
1459         opj_pi_destroy(l_pi, l_bound);
1460         return 00;
1461     }
1462 
1463     /* special treatment for the first packet iterator */
1464     l_current_comp = l_current_pi->comps;
1465     l_img_comp = p_image->comps;
1466     l_tccp = l_tcp->tccps;
1467 
1468     l_current_pi->tx0 = l_tx0;
1469     l_current_pi->ty0 = l_ty0;
1470     l_current_pi->tx1 = l_tx1;
1471     l_current_pi->ty1 = l_ty1;
1472 
1473     /*l_current_pi->dx = l_img_comp->dx;*/
1474     /*l_current_pi->dy = l_img_comp->dy;*/
1475 
1476     l_current_pi->step_p = l_step_p;
1477     l_current_pi->step_c = l_step_c;
1478     l_current_pi->step_r = l_step_r;
1479     l_current_pi->step_l = l_step_l;
1480 
1481     /* allocation for components and number of components has already been calculated by opj_pi_create */
1482     for
1483     (compno = 0; compno < numcomps; ++compno) {
1484         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1485         l_encoding_value_ptr = l_tmp_ptr[compno];
1486 
1487         l_current_comp->dx = l_img_comp->dx;
1488         l_current_comp->dy = l_img_comp->dy;
1489         /* resolutions have already been initialized */
1490         for
1491         (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1492             l_res->pdx = *(l_encoding_value_ptr++);
1493             l_res->pdy = *(l_encoding_value_ptr++);
1494             l_res->pw =  *(l_encoding_value_ptr++);
1495             l_res->ph =  *(l_encoding_value_ptr++);
1496             ++l_res;
1497         }
1498         ++l_current_comp;
1499         ++l_img_comp;
1500         ++l_tccp;
1501     }
1502     ++l_current_pi;
1503 
1504     for (pino = 1 ; pino < l_bound ; ++pino) {
1505         l_current_comp = l_current_pi->comps;
1506         l_img_comp = p_image->comps;
1507         l_tccp = l_tcp->tccps;
1508 
1509         l_current_pi->tx0 = l_tx0;
1510         l_current_pi->ty0 = l_ty0;
1511         l_current_pi->tx1 = l_tx1;
1512         l_current_pi->ty1 = l_ty1;
1513         /*l_current_pi->dx = l_dx_min;*/
1514         /*l_current_pi->dy = l_dy_min;*/
1515         l_current_pi->step_p = l_step_p;
1516         l_current_pi->step_c = l_step_c;
1517         l_current_pi->step_r = l_step_r;
1518         l_current_pi->step_l = l_step_l;
1519 
1520         /* allocation for components and number of components has already been calculated by opj_pi_create */
1521         for
1522         (compno = 0; compno < numcomps; ++compno) {
1523             opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1524             l_encoding_value_ptr = l_tmp_ptr[compno];
1525 
1526             l_current_comp->dx = l_img_comp->dx;
1527             l_current_comp->dy = l_img_comp->dy;
1528             /* resolutions have already been initialized */
1529             for
1530             (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1531                 l_res->pdx = *(l_encoding_value_ptr++);
1532                 l_res->pdy = *(l_encoding_value_ptr++);
1533                 l_res->pw =  *(l_encoding_value_ptr++);
1534                 l_res->ph =  *(l_encoding_value_ptr++);
1535                 ++l_res;
1536             }
1537             ++l_current_comp;
1538             ++l_img_comp;
1539             ++l_tccp;
1540         }
1541         /* special treatment*/
1542         l_current_pi->include = (l_current_pi - 1)->include;
1543         l_current_pi->include_size = (l_current_pi - 1)->include_size;
1544         ++l_current_pi;
1545     }
1546     opj_free(l_tmp_data);
1547     l_tmp_data = 00;
1548     opj_free(l_tmp_ptr);
1549     l_tmp_ptr = 00;
1550     if
1551     (l_tcp->POC) {
1552         opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1553     } else {
1554         opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
1555     }
1556     return l_pi;
1557 }
1558 
1559 
1560 
opj_pi_initialise_encode(const opj_image_t * p_image,opj_cp_t * p_cp,OPJ_UINT32 p_tile_no,J2K_T2_MODE p_t2_mode)1561 opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
1562         opj_cp_t *p_cp,
1563         OPJ_UINT32 p_tile_no,
1564         J2K_T2_MODE p_t2_mode)
1565 {
1566     OPJ_UINT32 numcomps = p_image->numcomps;
1567 
1568     /* loop*/
1569     OPJ_UINT32 pino;
1570     OPJ_UINT32 compno, resno;
1571 
1572     /* to store w, h, dx and dy fro all components and resolutions*/
1573     OPJ_UINT32 * l_tmp_data;
1574     OPJ_UINT32 ** l_tmp_ptr;
1575 
1576     /* encoding prameters to set*/
1577     OPJ_UINT32 l_max_res;
1578     OPJ_UINT32 l_max_prec;
1579     OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
1580     OPJ_UINT32 l_dx_min, l_dy_min;
1581     OPJ_UINT32 l_bound;
1582     OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
1583     OPJ_UINT32 l_data_stride;
1584 
1585     /* pointers*/
1586     opj_pi_iterator_t *l_pi = 00;
1587     opj_tcp_t *l_tcp = 00;
1588     const opj_tccp_t *l_tccp = 00;
1589     opj_pi_comp_t *l_current_comp = 00;
1590     opj_image_comp_t * l_img_comp = 00;
1591     opj_pi_iterator_t * l_current_pi = 00;
1592     OPJ_UINT32 * l_encoding_value_ptr = 00;
1593 
1594     /* preconditions in debug*/
1595     assert(p_cp != 00);
1596     assert(p_image != 00);
1597     assert(p_tile_no < p_cp->tw * p_cp->th);
1598 
1599     /* initializations*/
1600     l_tcp = &p_cp->tcps[p_tile_no];
1601     l_bound = l_tcp->numpocs + 1;
1602 
1603     l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
1604     l_tmp_data = (OPJ_UINT32*)opj_malloc(
1605                      l_data_stride * numcomps * sizeof(OPJ_UINT32));
1606     if (! l_tmp_data) {
1607         return 00;
1608     }
1609 
1610     l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
1611                     numcomps * sizeof(OPJ_UINT32 *));
1612     if (! l_tmp_ptr) {
1613         opj_free(l_tmp_data);
1614         return 00;
1615     }
1616 
1617     /* memory allocation for pi*/
1618     l_pi = opj_pi_create(p_image, p_cp, p_tile_no);
1619     if (!l_pi) {
1620         opj_free(l_tmp_data);
1621         opj_free(l_tmp_ptr);
1622         return 00;
1623     }
1624 
1625     l_encoding_value_ptr = l_tmp_data;
1626     /* update pointer array*/
1627     for (compno = 0; compno < numcomps; ++compno) {
1628         l_tmp_ptr[compno] = l_encoding_value_ptr;
1629         l_encoding_value_ptr += l_data_stride;
1630     }
1631 
1632     /* get encoding parameters*/
1633     opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
1634                                     &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
1635 
1636     /* step calculations*/
1637     l_step_p = 1;
1638     l_step_c = l_max_prec * l_step_p;
1639     l_step_r = numcomps * l_step_c;
1640     l_step_l = l_max_res * l_step_r;
1641 
1642     /* set values for first packet iterator*/
1643     l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
1644     l_current_pi = l_pi;
1645 
1646     /* memory allocation for include*/
1647     l_current_pi->include_size = l_tcp->numlayers * l_step_l;
1648     l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
1649                             sizeof(OPJ_INT16));
1650     if (!l_current_pi->include) {
1651         opj_free(l_tmp_data);
1652         opj_free(l_tmp_ptr);
1653         opj_pi_destroy(l_pi, l_bound);
1654         return 00;
1655     }
1656 
1657     /* special treatment for the first packet iterator*/
1658     l_current_comp = l_current_pi->comps;
1659     l_img_comp = p_image->comps;
1660     l_tccp = l_tcp->tccps;
1661     l_current_pi->tx0 = l_tx0;
1662     l_current_pi->ty0 = l_ty0;
1663     l_current_pi->tx1 = l_tx1;
1664     l_current_pi->ty1 = l_ty1;
1665     l_current_pi->dx = l_dx_min;
1666     l_current_pi->dy = l_dy_min;
1667     l_current_pi->step_p = l_step_p;
1668     l_current_pi->step_c = l_step_c;
1669     l_current_pi->step_r = l_step_r;
1670     l_current_pi->step_l = l_step_l;
1671 
1672     /* allocation for components and number of components has already been calculated by opj_pi_create */
1673     for (compno = 0; compno < numcomps; ++compno) {
1674         opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1675         l_encoding_value_ptr = l_tmp_ptr[compno];
1676 
1677         l_current_comp->dx = l_img_comp->dx;
1678         l_current_comp->dy = l_img_comp->dy;
1679 
1680         /* resolutions have already been initialized */
1681         for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1682             l_res->pdx = *(l_encoding_value_ptr++);
1683             l_res->pdy = *(l_encoding_value_ptr++);
1684             l_res->pw =  *(l_encoding_value_ptr++);
1685             l_res->ph =  *(l_encoding_value_ptr++);
1686             ++l_res;
1687         }
1688 
1689         ++l_current_comp;
1690         ++l_img_comp;
1691         ++l_tccp;
1692     }
1693     ++l_current_pi;
1694 
1695     for (pino = 1 ; pino < l_bound ; ++pino) {
1696         l_current_comp = l_current_pi->comps;
1697         l_img_comp = p_image->comps;
1698         l_tccp = l_tcp->tccps;
1699 
1700         l_current_pi->tx0 = l_tx0;
1701         l_current_pi->ty0 = l_ty0;
1702         l_current_pi->tx1 = l_tx1;
1703         l_current_pi->ty1 = l_ty1;
1704         l_current_pi->dx = l_dx_min;
1705         l_current_pi->dy = l_dy_min;
1706         l_current_pi->step_p = l_step_p;
1707         l_current_pi->step_c = l_step_c;
1708         l_current_pi->step_r = l_step_r;
1709         l_current_pi->step_l = l_step_l;
1710 
1711         /* allocation for components and number of components has already been calculated by opj_pi_create */
1712         for (compno = 0; compno < numcomps; ++compno) {
1713             opj_pi_resolution_t *l_res = l_current_comp->resolutions;
1714             l_encoding_value_ptr = l_tmp_ptr[compno];
1715 
1716             l_current_comp->dx = l_img_comp->dx;
1717             l_current_comp->dy = l_img_comp->dy;
1718             /* resolutions have already been initialized */
1719             for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
1720                 l_res->pdx = *(l_encoding_value_ptr++);
1721                 l_res->pdy = *(l_encoding_value_ptr++);
1722                 l_res->pw =  *(l_encoding_value_ptr++);
1723                 l_res->ph =  *(l_encoding_value_ptr++);
1724                 ++l_res;
1725             }
1726             ++l_current_comp;
1727             ++l_img_comp;
1728             ++l_tccp;
1729         }
1730 
1731         /* special treatment*/
1732         l_current_pi->include = (l_current_pi - 1)->include;
1733         l_current_pi->include_size = (l_current_pi - 1)->include_size;
1734         ++l_current_pi;
1735     }
1736 
1737     opj_free(l_tmp_data);
1738     l_tmp_data = 00;
1739     opj_free(l_tmp_ptr);
1740     l_tmp_ptr = 00;
1741 
1742     if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
1743         opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
1744                                            l_max_prec, l_max_res, l_dx_min, l_dy_min);
1745     } else {
1746         opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
1747                                      l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
1748     }
1749 
1750     return l_pi;
1751 }
1752 
opj_pi_create_encode(opj_pi_iterator_t * pi,opj_cp_t * cp,OPJ_UINT32 tileno,OPJ_UINT32 pino,OPJ_UINT32 tpnum,OPJ_INT32 tppos,J2K_T2_MODE t2_mode)1753 void opj_pi_create_encode(opj_pi_iterator_t *pi,
1754                           opj_cp_t *cp,
1755                           OPJ_UINT32 tileno,
1756                           OPJ_UINT32 pino,
1757                           OPJ_UINT32 tpnum,
1758                           OPJ_INT32 tppos,
1759                           J2K_T2_MODE t2_mode)
1760 {
1761     const OPJ_CHAR *prog;
1762     OPJ_INT32 i;
1763     OPJ_UINT32 incr_top = 1, resetX = 0;
1764     opj_tcp_t *tcps = &cp->tcps[tileno];
1765     opj_poc_t *tcp = &tcps->pocs[pino];
1766 
1767     prog = opj_j2k_convert_progression_order(tcp->prg);
1768 
1769     pi[pino].first = 1;
1770     pi[pino].poc.prg = tcp->prg;
1771 
1772     if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
1773             (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz)))) {
1774         pi[pino].poc.resno0 = tcp->resS;
1775         pi[pino].poc.resno1 = tcp->resE;
1776         pi[pino].poc.compno0 = tcp->compS;
1777         pi[pino].poc.compno1 = tcp->compE;
1778         pi[pino].poc.layno0 = tcp->layS;
1779         pi[pino].poc.layno1 = tcp->layE;
1780         pi[pino].poc.precno0 = tcp->prcS;
1781         pi[pino].poc.precno1 = tcp->prcE;
1782         pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1783         pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1784         pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1785         pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1786     } else {
1787         for (i = tppos + 1; i < 4; i++) {
1788             switch (prog[i]) {
1789             case 'R':
1790                 pi[pino].poc.resno0 = tcp->resS;
1791                 pi[pino].poc.resno1 = tcp->resE;
1792                 break;
1793             case 'C':
1794                 pi[pino].poc.compno0 = tcp->compS;
1795                 pi[pino].poc.compno1 = tcp->compE;
1796                 break;
1797             case 'L':
1798                 pi[pino].poc.layno0 = tcp->layS;
1799                 pi[pino].poc.layno1 = tcp->layE;
1800                 break;
1801             case 'P':
1802                 switch (tcp->prg) {
1803                 case OPJ_LRCP:
1804                 case OPJ_RLCP:
1805                     pi[pino].poc.precno0 = tcp->prcS;
1806                     pi[pino].poc.precno1 = tcp->prcE;
1807                     break;
1808                 default:
1809                     pi[pino].poc.tx0 = (OPJ_INT32)tcp->txS;
1810                     pi[pino].poc.ty0 = (OPJ_INT32)tcp->tyS;
1811                     pi[pino].poc.tx1 = (OPJ_INT32)tcp->txE;
1812                     pi[pino].poc.ty1 = (OPJ_INT32)tcp->tyE;
1813                     break;
1814                 }
1815                 break;
1816             }
1817         }
1818 
1819         if (tpnum == 0) {
1820             for (i = tppos; i >= 0; i--) {
1821                 switch (prog[i]) {
1822                 case 'C':
1823                     tcp->comp_t = tcp->compS;
1824                     pi[pino].poc.compno0 = tcp->comp_t;
1825                     pi[pino].poc.compno1 = tcp->comp_t + 1;
1826                     tcp->comp_t += 1;
1827                     break;
1828                 case 'R':
1829                     tcp->res_t = tcp->resS;
1830                     pi[pino].poc.resno0 = tcp->res_t;
1831                     pi[pino].poc.resno1 = tcp->res_t + 1;
1832                     tcp->res_t += 1;
1833                     break;
1834                 case 'L':
1835                     tcp->lay_t = tcp->layS;
1836                     pi[pino].poc.layno0 = tcp->lay_t;
1837                     pi[pino].poc.layno1 = tcp->lay_t + 1;
1838                     tcp->lay_t += 1;
1839                     break;
1840                 case 'P':
1841                     switch (tcp->prg) {
1842                     case OPJ_LRCP:
1843                     case OPJ_RLCP:
1844                         tcp->prc_t = tcp->prcS;
1845                         pi[pino].poc.precno0 = tcp->prc_t;
1846                         pi[pino].poc.precno1 = tcp->prc_t + 1;
1847                         tcp->prc_t += 1;
1848                         break;
1849                     default:
1850                         tcp->tx0_t = tcp->txS;
1851                         tcp->ty0_t = tcp->tyS;
1852                         pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1853                         pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1854                         pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1855                         pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1856                         tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1857                         tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1858                         break;
1859                     }
1860                     break;
1861                 }
1862             }
1863             incr_top = 1;
1864         } else {
1865             for (i = tppos; i >= 0; i--) {
1866                 switch (prog[i]) {
1867                 case 'C':
1868                     pi[pino].poc.compno0 = tcp->comp_t - 1;
1869                     pi[pino].poc.compno1 = tcp->comp_t;
1870                     break;
1871                 case 'R':
1872                     pi[pino].poc.resno0 = tcp->res_t - 1;
1873                     pi[pino].poc.resno1 = tcp->res_t;
1874                     break;
1875                 case 'L':
1876                     pi[pino].poc.layno0 = tcp->lay_t - 1;
1877                     pi[pino].poc.layno1 = tcp->lay_t;
1878                     break;
1879                 case 'P':
1880                     switch (tcp->prg) {
1881                     case OPJ_LRCP:
1882                     case OPJ_RLCP:
1883                         pi[pino].poc.precno0 = tcp->prc_t - 1;
1884                         pi[pino].poc.precno1 = tcp->prc_t;
1885                         break;
1886                     default:
1887                         pi[pino].poc.tx0 = (OPJ_INT32)(tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx));
1888                         pi[pino].poc.tx1 = (OPJ_INT32)tcp->tx0_t ;
1889                         pi[pino].poc.ty0 = (OPJ_INT32)(tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy));
1890                         pi[pino].poc.ty1 = (OPJ_INT32)tcp->ty0_t ;
1891                         break;
1892                     }
1893                     break;
1894                 }
1895                 if (incr_top == 1) {
1896                     switch (prog[i]) {
1897                     case 'R':
1898                         if (tcp->res_t == tcp->resE) {
1899                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1900                                 tcp->res_t = tcp->resS;
1901                                 pi[pino].poc.resno0 = tcp->res_t;
1902                                 pi[pino].poc.resno1 = tcp->res_t + 1;
1903                                 tcp->res_t += 1;
1904                                 incr_top = 1;
1905                             } else {
1906                                 incr_top = 0;
1907                             }
1908                         } else {
1909                             pi[pino].poc.resno0 = tcp->res_t;
1910                             pi[pino].poc.resno1 = tcp->res_t + 1;
1911                             tcp->res_t += 1;
1912                             incr_top = 0;
1913                         }
1914                         break;
1915                     case 'C':
1916                         if (tcp->comp_t == tcp->compE) {
1917                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1918                                 tcp->comp_t = tcp->compS;
1919                                 pi[pino].poc.compno0 = tcp->comp_t;
1920                                 pi[pino].poc.compno1 = tcp->comp_t + 1;
1921                                 tcp->comp_t += 1;
1922                                 incr_top = 1;
1923                             } else {
1924                                 incr_top = 0;
1925                             }
1926                         } else {
1927                             pi[pino].poc.compno0 = tcp->comp_t;
1928                             pi[pino].poc.compno1 = tcp->comp_t + 1;
1929                             tcp->comp_t += 1;
1930                             incr_top = 0;
1931                         }
1932                         break;
1933                     case 'L':
1934                         if (tcp->lay_t == tcp->layE) {
1935                             if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1936                                 tcp->lay_t = tcp->layS;
1937                                 pi[pino].poc.layno0 = tcp->lay_t;
1938                                 pi[pino].poc.layno1 = tcp->lay_t + 1;
1939                                 tcp->lay_t += 1;
1940                                 incr_top = 1;
1941                             } else {
1942                                 incr_top = 0;
1943                             }
1944                         } else {
1945                             pi[pino].poc.layno0 = tcp->lay_t;
1946                             pi[pino].poc.layno1 = tcp->lay_t + 1;
1947                             tcp->lay_t += 1;
1948                             incr_top = 0;
1949                         }
1950                         break;
1951                     case 'P':
1952                         switch (tcp->prg) {
1953                         case OPJ_LRCP:
1954                         case OPJ_RLCP:
1955                             if (tcp->prc_t == tcp->prcE) {
1956                                 if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1957                                     tcp->prc_t = tcp->prcS;
1958                                     pi[pino].poc.precno0 = tcp->prc_t;
1959                                     pi[pino].poc.precno1 = tcp->prc_t + 1;
1960                                     tcp->prc_t += 1;
1961                                     incr_top = 1;
1962                                 } else {
1963                                     incr_top = 0;
1964                                 }
1965                             } else {
1966                                 pi[pino].poc.precno0 = tcp->prc_t;
1967                                 pi[pino].poc.precno1 = tcp->prc_t + 1;
1968                                 tcp->prc_t += 1;
1969                                 incr_top = 0;
1970                             }
1971                             break;
1972                         default:
1973                             if (tcp->tx0_t >= tcp->txE) {
1974                                 if (tcp->ty0_t >= tcp->tyE) {
1975                                     if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
1976                                         tcp->ty0_t = tcp->tyS;
1977                                         pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1978                                         pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1979                                         tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1980                                         incr_top = 1;
1981                                         resetX = 1;
1982                                     } else {
1983                                         incr_top = 0;
1984                                         resetX = 0;
1985                                     }
1986                                 } else {
1987                                     pi[pino].poc.ty0 = (OPJ_INT32)tcp->ty0_t;
1988                                     pi[pino].poc.ty1 = (OPJ_INT32)(tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy));
1989                                     tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
1990                                     incr_top = 0;
1991                                     resetX = 1;
1992                                 }
1993                                 if (resetX == 1) {
1994                                     tcp->tx0_t = tcp->txS;
1995                                     pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
1996                                     pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
1997                                     tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
1998                                 }
1999                             } else {
2000                                 pi[pino].poc.tx0 = (OPJ_INT32)tcp->tx0_t;
2001                                 pi[pino].poc.tx1 = (OPJ_INT32)(tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx));
2002                                 tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
2003                                 incr_top = 0;
2004                             }
2005                             break;
2006                         }
2007                         break;
2008                     }
2009                 }
2010             }
2011         }
2012     }
2013 }
2014 
opj_pi_destroy(opj_pi_iterator_t * p_pi,OPJ_UINT32 p_nb_elements)2015 void opj_pi_destroy(opj_pi_iterator_t *p_pi,
2016                     OPJ_UINT32 p_nb_elements)
2017 {
2018     OPJ_UINT32 compno, pino;
2019     opj_pi_iterator_t *l_current_pi = p_pi;
2020     if (p_pi) {
2021         if (p_pi->include) {
2022             opj_free(p_pi->include);
2023             p_pi->include = 00;
2024         }
2025         for (pino = 0; pino < p_nb_elements; ++pino) {
2026             if (l_current_pi->comps) {
2027                 opj_pi_comp_t *l_current_component = l_current_pi->comps;
2028                 for (compno = 0; compno < l_current_pi->numcomps; compno++) {
2029                     if (l_current_component->resolutions) {
2030                         opj_free(l_current_component->resolutions);
2031                         l_current_component->resolutions = 00;
2032                     }
2033 
2034                     ++l_current_component;
2035                 }
2036                 opj_free(l_current_pi->comps);
2037                 l_current_pi->comps = 0;
2038             }
2039             ++l_current_pi;
2040         }
2041         opj_free(p_pi);
2042     }
2043 }
2044 
2045 
2046 
opj_pi_update_encoding_parameters(const opj_image_t * p_image,opj_cp_t * p_cp,OPJ_UINT32 p_tile_no)2047 void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
2048                                        opj_cp_t *p_cp,
2049                                        OPJ_UINT32 p_tile_no)
2050 {
2051     /* encoding parameters to set */
2052     OPJ_UINT32 l_max_res;
2053     OPJ_UINT32 l_max_prec;
2054     OPJ_INT32 l_tx0, l_tx1, l_ty0, l_ty1;
2055     OPJ_UINT32 l_dx_min, l_dy_min;
2056 
2057     /* pointers */
2058     opj_tcp_t *l_tcp = 00;
2059 
2060     /* preconditions */
2061     assert(p_cp != 00);
2062     assert(p_image != 00);
2063     assert(p_tile_no < p_cp->tw * p_cp->th);
2064 
2065     l_tcp = &(p_cp->tcps[p_tile_no]);
2066 
2067     /* get encoding parameters */
2068     opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
2069                                 &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
2070 
2071     if (l_tcp->POC) {
2072         opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
2073                                            l_max_prec, l_max_res, l_dx_min, l_dy_min);
2074     } else {
2075         opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
2076                                      l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
2077     }
2078 }
2079 
opj_pi_next(opj_pi_iterator_t * pi)2080 OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
2081 {
2082     switch (pi->poc.prg) {
2083     case OPJ_LRCP:
2084         return opj_pi_next_lrcp(pi);
2085     case OPJ_RLCP:
2086         return opj_pi_next_rlcp(pi);
2087     case OPJ_RPCL:
2088         return opj_pi_next_rpcl(pi);
2089     case OPJ_PCRL:
2090         return opj_pi_next_pcrl(pi);
2091     case OPJ_CPRL:
2092         return opj_pi_next_cprl(pi);
2093     case OPJ_PROG_UNKNOWN:
2094         return OPJ_FALSE;
2095     }
2096 
2097     return OPJ_FALSE;
2098 }
2099