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