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