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