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