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