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