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