1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2006-2007, Parvatha Elangovan
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "opj_includes.h"
34 
35 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
36 /*@{*/
37 
38 /** @name Local static functions */
39 /*@{*/
40 
41 /**
42 Write the SOC marker (Start Of Codestream)
43 @param j2k J2K handle
44 */
45 static void j2k_write_soc(opj_j2k_t *j2k);
46 /**
47 Read the SOC marker (Start of Codestream)
48 @param j2k J2K handle
49 */
50 static void j2k_read_soc(opj_j2k_t *j2k);
51 /**
52 Write the SIZ marker (image and tile size)
53 @param j2k J2K handle
54 */
55 static void j2k_write_siz(opj_j2k_t *j2k);
56 /**
57 Read the SIZ marker (image and tile size)
58 @param j2k J2K handle
59 */
60 static void j2k_read_siz(opj_j2k_t *j2k);
61 /**
62 Write the COM marker (comment)
63 @param j2k J2K handle
64 */
65 static void j2k_write_com(opj_j2k_t *j2k);
66 /**
67 Read the COM marker (comment)
68 @param j2k J2K handle
69 */
70 static void j2k_read_com(opj_j2k_t *j2k);
71 /**
72 Write the value concerning the specified component in the marker COD and COC
73 @param j2k J2K handle
74 @param compno Number of the component concerned by the information written
75 */
76 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
77 /**
78 Read the value concerning the specified component in the marker COD and COC
79 @param j2k J2K handle
80 @param compno Number of the component concerned by the information read
81 */
82 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
83 /**
84 Write the COD marker (coding style default)
85 @param j2k J2K handle
86 */
87 static void j2k_write_cod(opj_j2k_t *j2k);
88 /**
89 Read the COD marker (coding style default)
90 @param j2k J2K handle
91 */
92 static void j2k_read_cod(opj_j2k_t *j2k);
93 /**
94 Write the COC marker (coding style component)
95 @param j2k J2K handle
96 @param compno Number of the component concerned by the information written
97 */
98 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
99 /**
100 Read the COC marker (coding style component)
101 @param j2k J2K handle
102 */
103 static void j2k_read_coc(opj_j2k_t *j2k);
104 /**
105 Write the value concerning the specified component in the marker QCD and QCC
106 @param j2k J2K handle
107 @param compno Number of the component concerned by the information written
108 */
109 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
110 /**
111 Read the value concerning the specified component in the marker QCD and QCC
112 @param j2k J2K handle
113 @param compno Number of the component concern by the information read
114 @param len Length of the information in the QCX part of the marker QCD/QCC
115 */
116 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
117 /**
118 Write the QCD marker (quantization default)
119 @param j2k J2K handle
120 */
121 static void j2k_write_qcd(opj_j2k_t *j2k);
122 /**
123 Read the QCD marker (quantization default)
124 @param j2k J2K handle
125 */
126 static void j2k_read_qcd(opj_j2k_t *j2k);
127 /**
128 Write the QCC marker (quantization component)
129 @param j2k J2K handle
130 @param compno Number of the component concerned by the information written
131 */
132 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
133 /**
134 Read the QCC marker (quantization component)
135 @param j2k J2K handle
136 */
137 static void j2k_read_qcc(opj_j2k_t *j2k);
138 /**
139 Write the POC marker (progression order change)
140 @param j2k J2K handle
141 */
142 static void j2k_write_poc(opj_j2k_t *j2k);
143 /**
144 Read the POC marker (progression order change)
145 @param j2k J2K handle
146 */
147 static void j2k_read_poc(opj_j2k_t *j2k);
148 /**
149 Read the CRG marker (component registration)
150 @param j2k J2K handle
151 */
152 static void j2k_read_crg(opj_j2k_t *j2k);
153 /**
154 Read the TLM marker (tile-part lengths)
155 @param j2k J2K handle
156 */
157 static void j2k_read_tlm(opj_j2k_t *j2k);
158 /**
159 Read the PLM marker (packet length, main header)
160 @param j2k J2K handle
161 */
162 static void j2k_read_plm(opj_j2k_t *j2k);
163 /**
164 Read the PLT marker (packet length, tile-part header)
165 @param j2k J2K handle
166 */
167 static void j2k_read_plt(opj_j2k_t *j2k);
168 /**
169 Read the PPM marker (packet packet headers, main header)
170 @param j2k J2K handle
171 */
172 static void j2k_read_ppm(opj_j2k_t *j2k);
173 /**
174 Read the PPT marker (packet packet headers, tile-part header)
175 @param j2k J2K handle
176 */
177 static void j2k_read_ppt(opj_j2k_t *j2k);
178 /**
179 Write the TLM marker (Mainheader)
180 @param j2k J2K handle
181 */
182 static void j2k_write_tlm(opj_j2k_t *j2k);
183 /**
184 Write the SOT marker (start of tile-part)
185 @param j2k J2K handle
186 */
187 static void j2k_write_sot(opj_j2k_t *j2k);
188 /**
189 Read the SOT marker (start of tile-part)
190 @param j2k J2K handle
191 */
192 static void j2k_read_sot(opj_j2k_t *j2k);
193 /**
194 Write the SOD marker (start of data)
195 @param j2k J2K handle
196 @param tile_coder Pointer to a TCD handle
197 */
198 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
199 /**
200 Read the SOD marker (start of data)
201 @param j2k J2K handle
202 */
203 static void j2k_read_sod(opj_j2k_t *j2k);
204 /**
205 Write the RGN marker (region-of-interest)
206 @param j2k J2K handle
207 @param compno Number of the component concerned by the information written
208 @param tileno Number of the tile concerned by the information written
209 */
210 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
211 /**
212 Read the RGN marker (region-of-interest)
213 @param j2k J2K handle
214 */
215 static void j2k_read_rgn(opj_j2k_t *j2k);
216 /**
217 Write the EOC marker (end of codestream)
218 @param j2k J2K handle
219 */
220 static void j2k_write_eoc(opj_j2k_t *j2k);
221 /**
222 Read the EOC marker (end of codestream)
223 @param j2k J2K handle
224 */
225 static void j2k_read_eoc(opj_j2k_t *j2k);
226 /**
227 Read an unknown marker
228 @param j2k J2K handle
229 */
230 static void j2k_read_unk(opj_j2k_t *j2k);
231 
232 /*@}*/
233 
234 /*@}*/
235 
236 /* ----------------------------------------------------------------------- */
237 typedef struct j2k_prog_order{
238 	OPJ_PROG_ORDER enum_prog;
239 	char str_prog[4];
240 }j2k_prog_order_t;
241 
242 j2k_prog_order_t j2k_prog_order_list[] = {
243 	{CPRL, "CPRL"},
244 	{LRCP, "LRCP"},
245 	{PCRL, "PCRL"},
246 	{RLCP, "RLCP"},
247 	{RPCL, "RPCL"},
248 	{(OPJ_PROG_ORDER)-1, ""}
249 };
250 
j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)251 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
252 	j2k_prog_order_t *po;
253 	for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
254 		if(po->enum_prog == prg_order){
255 			break;
256 		}
257 	}
258 	return po->str_prog;
259 }
260 
261 /* ----------------------------------------------------------------------- */
j2k_get_num_tp(opj_cp_t * cp,int pino,int tileno)262 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
263 	char *prog;
264 	int i;
265 	int tpnum=1,tpend=0;
266 	opj_tcp_t *tcp = &cp->tcps[tileno];
267 	prog = j2k_convert_progression_order(tcp->prg);
268 
269 	if(cp->tp_on == 1){
270 		for(i=0;i<4;i++){
271 			if(tpend!=1){
272 				if( cp->tp_flag == prog[i] ){
273 					tpend=1;cp->tp_pos=i;
274 				}
275 				switch(prog[i]){
276 				case 'C':
277 					tpnum= tpnum * tcp->pocs[pino].compE;
278 					break;
279 				case 'R':
280 					tpnum= tpnum * tcp->pocs[pino].resE;
281 					break;
282 				case 'P':
283 					tpnum= tpnum * tcp->pocs[pino].prcE;
284 					break;
285 				case 'L':
286 					tpnum= tpnum * tcp->pocs[pino].layE;
287 					break;
288 				}
289 			}
290 		}
291 	}else{
292 		tpnum=1;
293 	}
294 	return tpnum;
295 }
296 
297 /**	mem allocation for TLM marker*/
j2k_calculate_tp(opj_cp_t * cp,int img_numcomp,opj_image_t * image,opj_j2k_t * j2k)298 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
299 	int pino,tileno,totnum_tp=0;
300 	j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
301 	for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
302 		int cur_totnum_tp = 0;
303 		opj_tcp_t *tcp = &cp->tcps[tileno];
304 		for(pino = 0; pino <= tcp->numpocs; pino++) {
305 			int tp_num=0;
306 			opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
307 			if(!pi) { return -1;}
308 			tp_num = j2k_get_num_tp(cp,pino,tileno);
309 			totnum_tp = totnum_tp + tp_num;
310 			cur_totnum_tp = cur_totnum_tp + tp_num;
311 			pi_destroy(pi, cp, tileno);
312 		}
313 		j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
314 		/* INDEX >> */
315 		if (j2k->cstr_info) {
316 			j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
317 			j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
318 		}
319 		/* << INDEX */
320 	}
321 	return totnum_tp;
322 }
323 
j2k_write_soc(opj_j2k_t * j2k)324 static void j2k_write_soc(opj_j2k_t *j2k) {
325 	opj_cio_t *cio = j2k->cio;
326 	cio_write(cio, J2K_MS_SOC, 2);
327 
328 /* UniPG>> */
329 #ifdef USE_JPWL
330 
331 	/* update markers struct */
332 	j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
333 
334 #endif /* USE_JPWL */
335 /* <<UniPG */
336 }
337 
j2k_read_soc(opj_j2k_t * j2k)338 static void j2k_read_soc(opj_j2k_t *j2k) {
339 	j2k->state = J2K_STATE_MHSIZ;
340 	/* Index */
341 	if (j2k->cstr_info) {
342 		j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
343 		j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
344 	}
345 }
346 
j2k_write_siz(opj_j2k_t * j2k)347 static void j2k_write_siz(opj_j2k_t *j2k) {
348 	int i;
349 	int lenp, len;
350 
351 	opj_cio_t *cio = j2k->cio;
352 	opj_image_t *image = j2k->image;
353 	opj_cp_t *cp = j2k->cp;
354 
355 	cio_write(cio, J2K_MS_SIZ, 2);	/* SIZ */
356 	lenp = cio_tell(cio);
357 	cio_skip(cio, 2);
358 	cio_write(cio, cp->rsiz, 2);			/* Rsiz (capabilities) */
359 	cio_write(cio, image->x1, 4);	/* Xsiz */
360 	cio_write(cio, image->y1, 4);	/* Ysiz */
361 	cio_write(cio, image->x0, 4);	/* X0siz */
362 	cio_write(cio, image->y0, 4);	/* Y0siz */
363 	cio_write(cio, cp->tdx, 4);		/* XTsiz */
364 	cio_write(cio, cp->tdy, 4);		/* YTsiz */
365 	cio_write(cio, cp->tx0, 4);		/* XT0siz */
366 	cio_write(cio, cp->ty0, 4);		/* YT0siz */
367 	cio_write(cio, image->numcomps, 2);	/* Csiz */
368 	for (i = 0; i < image->numcomps; i++) {
369 		cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);	/* Ssiz_i */
370 		cio_write(cio, image->comps[i].dx, 1);	/* XRsiz_i */
371 		cio_write(cio, image->comps[i].dy, 1);	/* YRsiz_i */
372 	}
373 	len = cio_tell(cio) - lenp;
374 	cio_seek(cio, lenp);
375 	cio_write(cio, len, 2);		/* Lsiz */
376 	cio_seek(cio, lenp + len);
377 }
378 
j2k_read_siz(opj_j2k_t * j2k)379 static void j2k_read_siz(opj_j2k_t *j2k) {
380 	int len, i;
381 
382 	opj_cio_t *cio = j2k->cio;
383 	opj_image_t *image = j2k->image;
384 	opj_cp_t *cp = j2k->cp;
385 
386 	len = cio_read(cio, 2);			/* Lsiz */
387 	cio_read(cio, 2);				/* Rsiz (capabilities) */
388 	image->x1 = cio_read(cio, 4);	/* Xsiz */
389 	image->y1 = cio_read(cio, 4);	/* Ysiz */
390 	image->x0 = cio_read(cio, 4);	/* X0siz */
391 	image->y0 = cio_read(cio, 4);	/* Y0siz */
392 	cp->tdx = cio_read(cio, 4);		/* XTsiz */
393 	cp->tdy = cio_read(cio, 4);		/* YTsiz */
394 	cp->tx0 = cio_read(cio, 4);		/* XT0siz */
395 	cp->ty0 = cio_read(cio, 4);		/* YT0siz */
396 
397 	if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
398 		opj_event_msg(j2k->cinfo, EVT_ERROR,
399 									"%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
400 									image->x0,image->x1,image->y0,image->y1);
401 		return;
402 	}
403 
404 	image->numcomps = cio_read(cio, 2);	/* Csiz */
405 
406 #ifdef USE_JPWL
407 	if (j2k->cp->correct) {
408 		/* if JPWL is on, we check whether TX errors have damaged
409 		  too much the SIZ parameters */
410 		if (!(image->x1 * image->y1)) {
411 			opj_event_msg(j2k->cinfo, EVT_ERROR,
412 				"JPWL: bad image size (%d x %d)\n",
413 				image->x1, image->y1);
414 			if (!JPWL_ASSUME || JPWL_ASSUME) {
415 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
416 				return;
417 			}
418 		}
419 		if (image->numcomps != ((len - 38) / 3)) {
420 			opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
421 				"JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
422 				image->numcomps, ((len - 38) / 3));
423 			if (!JPWL_ASSUME) {
424 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
425 				return;
426 			}
427 			/* we try to correct */
428 			opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
429 			if (image->numcomps < ((len - 38) / 3)) {
430 				len = 38 + 3 * image->numcomps;
431 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
432 					len);
433 			} else {
434 				image->numcomps = ((len - 38) / 3);
435 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
436 					image->numcomps);
437 			}
438 		}
439 
440 		/* update components number in the jpwl_exp_comps filed */
441 		cp->exp_comps = image->numcomps;
442 	}
443 #endif /* USE_JPWL */
444 
445 	image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
446 	for (i = 0; i < image->numcomps; i++) {
447 		int tmp, w, h;
448 		tmp = cio_read(cio, 1);		/* Ssiz_i */
449 		image->comps[i].prec = (tmp & 0x7f) + 1;
450 		image->comps[i].sgnd = tmp >> 7;
451 		image->comps[i].dx = cio_read(cio, 1);	/* XRsiz_i */
452 		image->comps[i].dy = cio_read(cio, 1);	/* YRsiz_i */
453 
454 #ifdef USE_JPWL
455 		if (j2k->cp->correct) {
456 		/* if JPWL is on, we check whether TX errors have damaged
457 			too much the SIZ parameters, again */
458 			if (!(image->comps[i].dx * image->comps[i].dy)) {
459 				opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
460 					"JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
461 					i, i, image->comps[i].dx, image->comps[i].dy);
462 				if (!JPWL_ASSUME) {
463 					opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
464 					return;
465 				}
466 				/* we try to correct */
467 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
468 				if (!image->comps[i].dx) {
469 					image->comps[i].dx = 1;
470 					opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
471 						i, image->comps[i].dx);
472 				}
473 				if (!image->comps[i].dy) {
474 					image->comps[i].dy = 1;
475 					opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
476 						i, image->comps[i].dy);
477 				}
478 			}
479 
480 		}
481 #endif /* USE_JPWL */
482 
483 		/* TODO: unused ? */
484 		w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
485 		h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
486 
487 		image->comps[i].resno_decoded = 0;	/* number of resolution decoded */
488 		image->comps[i].factor = cp->reduce; /* reducing factor per component */
489 	}
490 
491 	cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
492 	cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
493 
494 #ifdef USE_JPWL
495 	if (j2k->cp->correct) {
496 		/* if JPWL is on, we check whether TX errors have damaged
497 		  too much the SIZ parameters */
498 		if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
499 			opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
500 				"JPWL: bad number of tiles (%d x %d)\n",
501 				cp->tw, cp->th);
502 			if (!JPWL_ASSUME) {
503 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
504 				return;
505 			}
506 			/* we try to correct */
507 			opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
508 			if (cp->tw < 1) {
509 				cp->tw= 1;
510 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
511 					cp->tw);
512 			}
513 			if (cp->tw > cp->max_tiles) {
514 				cp->tw= 1;
515 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
516 					"- setting %d tiles in x => HYPOTHESIS!!!\n",
517 					cp->max_tiles, cp->tw);
518 			}
519 			if (cp->th < 1) {
520 				cp->th= 1;
521 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
522 					cp->th);
523 			}
524 			if (cp->th > cp->max_tiles) {
525 				cp->th= 1;
526 				opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
527 					"- setting %d tiles in y => HYPOTHESIS!!!\n",
528 					cp->max_tiles, cp->th);
529 			}
530 		}
531 	}
532 #endif /* USE_JPWL */
533 
534 	cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
535 	cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
536 	cp->tileno_size = 0;
537 
538 #ifdef USE_JPWL
539 	if (j2k->cp->correct) {
540 		if (!cp->tcps) {
541 			opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
542 				"JPWL: could not alloc tcps field of cp\n");
543 			if (!JPWL_ASSUME || JPWL_ASSUME) {
544 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
545 				return;
546 			}
547 		}
548 	}
549 #endif /* USE_JPWL */
550 
551 	for (i = 0; i < cp->tw * cp->th; i++) {
552 		cp->tcps[i].POC = 0;
553 		cp->tcps[i].numpocs = 0;
554 		cp->tcps[i].first = 1;
555 	}
556 
557 	/* Initialization for PPM marker */
558 	cp->ppm = 0;
559 	cp->ppm_data = NULL;
560 	cp->ppm_data_first = NULL;
561 	cp->ppm_previous = 0;
562 	cp->ppm_store = 0;
563 
564 	j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
565 	for (i = 0; i < cp->tw * cp->th; i++) {
566 		cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
567 	}
568 	j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
569 	j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
570 	j2k->state = J2K_STATE_MH;
571 
572 	/* Index */
573 	if (j2k->cstr_info) {
574 		opj_codestream_info_t *cstr_info = j2k->cstr_info;
575 		cstr_info->image_w = image->x1 - image->x0;
576 		cstr_info->image_h = image->y1 - image->y0;
577 		cstr_info->numcomps = image->numcomps;
578 		cstr_info->tw = cp->tw;
579 		cstr_info->th = cp->th;
580 		cstr_info->tile_x = cp->tdx;
581 		cstr_info->tile_y = cp->tdy;
582 		cstr_info->tile_Ox = cp->tx0;
583 		cstr_info->tile_Oy = cp->ty0;
584 		cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
585 	}
586 }
587 
j2k_write_com(opj_j2k_t * j2k)588 static void j2k_write_com(opj_j2k_t *j2k) {
589 	unsigned int i;
590 	int lenp, len;
591 
592 	if(j2k->cp->comment) {
593 		opj_cio_t *cio = j2k->cio;
594 		char *comment = j2k->cp->comment;
595 
596 		cio_write(cio, J2K_MS_COM, 2);
597 		lenp = cio_tell(cio);
598 		cio_skip(cio, 2);
599 		cio_write(cio, 1, 2);		/* General use (IS 8859-15:1999 (Latin) values) */
600 		for (i = 0; i < strlen(comment); i++) {
601 			cio_write(cio, comment[i], 1);
602 		}
603 		len = cio_tell(cio) - lenp;
604 		cio_seek(cio, lenp);
605 		cio_write(cio, len, 2);
606 		cio_seek(cio, lenp + len);
607 	}
608 }
609 
j2k_read_com(opj_j2k_t * j2k)610 static void j2k_read_com(opj_j2k_t *j2k) {
611 	int len;
612 
613 	opj_cio_t *cio = j2k->cio;
614 
615 	len = cio_read(cio, 2);
616 	cio_skip(cio, len - 2);
617 }
618 
j2k_write_cox(opj_j2k_t * j2k,int compno)619 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
620 	int i;
621 
622 	opj_cp_t *cp = j2k->cp;
623 	opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
624 	opj_tccp_t *tccp = &tcp->tccps[compno];
625 	opj_cio_t *cio = j2k->cio;
626 
627 	cio_write(cio, tccp->numresolutions - 1, 1);	/* SPcox (D) */
628 	cio_write(cio, tccp->cblkw - 2, 1);				/* SPcox (E) */
629 	cio_write(cio, tccp->cblkh - 2, 1);				/* SPcox (F) */
630 	cio_write(cio, tccp->cblksty, 1);				/* SPcox (G) */
631 	cio_write(cio, tccp->qmfbid, 1);				/* SPcox (H) */
632 
633 	if (tccp->csty & J2K_CCP_CSTY_PRT) {
634 		for (i = 0; i < tccp->numresolutions; i++) {
635 			cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);	/* SPcox (I_i) */
636 		}
637 	}
638 }
639 
j2k_read_cox(opj_j2k_t * j2k,int compno)640 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
641 	int i;
642 
643 	opj_cp_t *cp = j2k->cp;
644 	opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
645 	opj_tccp_t *tccp = &tcp->tccps[compno];
646 	opj_cio_t *cio = j2k->cio;
647 
648 	tccp->numresolutions = cio_read(cio, 1) + 1;	/* SPcox (D) */
649 
650 	// If user wants to remove more resolutions than the codestream contains, return error
651 	if (cp->reduce >= tccp->numresolutions) {
652 		opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
653 					"of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
654 		j2k->state |= J2K_STATE_ERR;
655 	}
656 
657 	tccp->cblkw = cio_read(cio, 1) + 2;	/* SPcox (E) */
658 	tccp->cblkh = cio_read(cio, 1) + 2;	/* SPcox (F) */
659 	tccp->cblksty = cio_read(cio, 1);	/* SPcox (G) */
660 	tccp->qmfbid = cio_read(cio, 1);	/* SPcox (H) */
661 	if (tccp->csty & J2K_CP_CSTY_PRT) {
662 		for (i = 0; i < tccp->numresolutions; i++) {
663 			int tmp = cio_read(cio, 1);	/* SPcox (I_i) */
664 			tccp->prcw[i] = tmp & 0xf;
665 			tccp->prch[i] = tmp >> 4;
666 		}
667 	}
668 
669 	/* INDEX >> */
670 	if(j2k->cstr_info && compno == 0) {
671 		for (i = 0; i < tccp->numresolutions; i++) {
672 			if (tccp->csty & J2K_CP_CSTY_PRT) {
673 				j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
674 				j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
675 			}
676 			else {
677 				j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
678 				j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
679 			}
680 		}
681 	}
682 	/* << INDEX */
683 }
684 
j2k_write_cod(opj_j2k_t * j2k)685 static void j2k_write_cod(opj_j2k_t *j2k) {
686 	opj_cp_t *cp = NULL;
687 	opj_tcp_t *tcp = NULL;
688 	int lenp, len;
689 
690 	opj_cio_t *cio = j2k->cio;
691 
692 	cio_write(cio, J2K_MS_COD, 2);	/* COD */
693 
694 	lenp = cio_tell(cio);
695 	cio_skip(cio, 2);
696 
697 	cp = j2k->cp;
698 	tcp = &cp->tcps[j2k->curtileno];
699 
700 	cio_write(cio, tcp->csty, 1);		/* Scod */
701 	cio_write(cio, tcp->prg, 1);		/* SGcod (A) */
702 	cio_write(cio, tcp->numlayers, 2);	/* SGcod (B) */
703 	cio_write(cio, tcp->mct, 1);		/* SGcod (C) */
704 
705 	j2k_write_cox(j2k, 0);
706 	len = cio_tell(cio) - lenp;
707 	cio_seek(cio, lenp);
708 	cio_write(cio, len, 2);		/* Lcod */
709 	cio_seek(cio, lenp + len);
710 }
711 
j2k_read_cod(opj_j2k_t * j2k)712 static void j2k_read_cod(opj_j2k_t *j2k) {
713 	int len, i, pos;
714 
715 	opj_cio_t *cio = j2k->cio;
716 	opj_cp_t *cp = j2k->cp;
717 	opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
718 	opj_image_t *image = j2k->image;
719 
720 	len = cio_read(cio, 2);				/* Lcod */
721 	tcp->csty = cio_read(cio, 1);		/* Scod */
722 	tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);		/* SGcod (A) */
723 	tcp->numlayers = cio_read(cio, 2);	/* SGcod (B) */
724 	tcp->mct = cio_read(cio, 1);		/* SGcod (C) */
725 
726 	pos = cio_tell(cio);
727 	for (i = 0; i < image->numcomps; i++) {
728 		tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
729 		cio_seek(cio, pos);
730 		j2k_read_cox(j2k, i);
731 	}
732 
733 	/* Index */
734 	if (j2k->cstr_info) {
735 		opj_codestream_info_t *cstr_info = j2k->cstr_info;
736 		cstr_info->prog = tcp->prg;
737 		cstr_info->numlayers = tcp->numlayers;
738 		cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
739 		for (i = 0; i < image->numcomps; i++) {
740 			cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
741 		}
742 	}
743 }
744 
j2k_write_coc(opj_j2k_t * j2k,int compno)745 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
746 	int lenp, len;
747 
748 	opj_cp_t *cp = j2k->cp;
749 	opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
750 	opj_image_t *image = j2k->image;
751 	opj_cio_t *cio = j2k->cio;
752 
753 	cio_write(cio, J2K_MS_COC, 2);	/* COC */
754 	lenp = cio_tell(cio);
755 	cio_skip(cio, 2);
756 	cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2);	/* Ccoc */
757 	cio_write(cio, tcp->tccps[compno].csty, 1);	/* Scoc */
758 	j2k_write_cox(j2k, compno);
759 	len = cio_tell(cio) - lenp;
760 	cio_seek(cio, lenp);
761 	cio_write(cio, len, 2);			/* Lcoc */
762 	cio_seek(cio, lenp + len);
763 }
764 
j2k_read_coc(opj_j2k_t * j2k)765 static void j2k_read_coc(opj_j2k_t *j2k) {
766 	int len, compno;
767 
768 	opj_cp_t *cp = j2k->cp;
769 	opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
770 	opj_image_t *image = j2k->image;
771 	opj_cio_t *cio = j2k->cio;
772 
773 	len = cio_read(cio, 2);		/* Lcoc */
774 	compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2);	/* Ccoc */
775 	tcp->tccps[compno].csty = cio_read(cio, 1);	/* Scoc */
776 	j2k_read_cox(j2k, compno);
777 }
778 
j2k_write_qcx(opj_j2k_t * j2k,int compno)779 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
780 	int bandno, numbands;
781 	int expn, mant;
782 
783 	opj_cp_t *cp = j2k->cp;
784 	opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
785 	opj_tccp_t *tccp = &tcp->tccps[compno];
786 	opj_cio_t *cio = j2k->cio;
787 
788 	cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);	/* Sqcx */
789 	numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
790 
791 	for (bandno = 0; bandno < numbands; bandno++) {
792 		expn = tccp->stepsizes[bandno].expn;
793 		mant = tccp->stepsizes[bandno].mant;
794 
795 		if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
796 			cio_write(cio, expn << 3, 1);	/* SPqcx_i */
797 		} else {
798 			cio_write(cio, (expn << 11) + mant, 2);	/* SPqcx_i */
799 		}
800 	}
801 }
802 
j2k_read_qcx(opj_j2k_t * j2k,int compno,int len)803 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
804 	int tmp;
805 	int bandno, numbands;
806 
807 	opj_cp_t *cp = j2k->cp;
808 	opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
809 	opj_tccp_t *tccp = &tcp->tccps[compno];
810 	opj_cio_t *cio = j2k->cio;
811 
812 	tmp = cio_read(cio, 1);		/* Sqcx */
813 	tccp->qntsty = tmp & 0x1f;
814 	tccp->numgbits = tmp >> 5;
815 	numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
816 		1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
817 
818 #ifdef USE_JPWL
819 	if (j2k->cp->correct) {
820 
821 		/* if JPWL is on, we check whether there are too many subbands */
822 		if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
823 			opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
824 				"JPWL: bad number of subbands in Sqcx (%d)\n",
825 				numbands);
826 			if (!JPWL_ASSUME) {
827 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
828 				return;
829 			}
830 			/* we try to correct */
831 			numbands = 1;
832 			opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
833 				"- setting number of bands to %d => HYPOTHESIS!!!\n",
834 				numbands);
835 		};
836 
837 	};
838 #endif /* USE_JPWL */
839 
840 	for (bandno = 0; bandno < numbands; bandno++) {
841 		int expn, mant;
842 		if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
843 			expn = cio_read(cio, 1) >> 3;	/* SPqcx_i */
844 			mant = 0;
845 		} else {
846 			tmp = cio_read(cio, 2);	/* SPqcx_i */
847 			expn = tmp >> 11;
848 			mant = tmp & 0x7ff;
849 		}
850 		tccp->stepsizes[bandno].expn = expn;
851 		tccp->stepsizes[bandno].mant = mant;
852 	}
853 
854 	/* Add Antonin : if scalar_derived -> compute other stepsizes */
855 	if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
856 		for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
857 			tccp->stepsizes[bandno].expn =
858 				((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
859 					(tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
860 			tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
861 		}
862 	}
863 	/* ddA */
864 }
865 
j2k_write_qcd(opj_j2k_t * j2k)866 static void j2k_write_qcd(opj_j2k_t *j2k) {
867 	int lenp, len;
868 
869 	opj_cio_t *cio = j2k->cio;
870 
871 	cio_write(cio, J2K_MS_QCD, 2);	/* QCD */
872 	lenp = cio_tell(cio);
873 	cio_skip(cio, 2);
874 	j2k_write_qcx(j2k, 0);
875 	len = cio_tell(cio) - lenp;
876 	cio_seek(cio, lenp);
877 	cio_write(cio, len, 2);			/* Lqcd */
878 	cio_seek(cio, lenp + len);
879 }
880 
j2k_read_qcd(opj_j2k_t * j2k)881 static void j2k_read_qcd(opj_j2k_t *j2k) {
882 	int len, i, pos;
883 
884 	opj_cio_t *cio = j2k->cio;
885 	opj_image_t *image = j2k->image;
886 
887 	len = cio_read(cio, 2);		/* Lqcd */
888 	pos = cio_tell(cio);
889 	for (i = 0; i < image->numcomps; i++) {
890 		cio_seek(cio, pos);
891 		j2k_read_qcx(j2k, i, len - 2);
892 	}
893 }
894 
j2k_write_qcc(opj_j2k_t * j2k,int compno)895 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
896 	int lenp, len;
897 
898 	opj_cio_t *cio = j2k->cio;
899 
900 	cio_write(cio, J2K_MS_QCC, 2);	/* QCC */
901 	lenp = cio_tell(cio);
902 	cio_skip(cio, 2);
903 	cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);	/* Cqcc */
904 	j2k_write_qcx(j2k, compno);
905 	len = cio_tell(cio) - lenp;
906 	cio_seek(cio, lenp);
907 	cio_write(cio, len, 2);			/* Lqcc */
908 	cio_seek(cio, lenp + len);
909 }
910 
j2k_read_qcc(opj_j2k_t * j2k)911 static void j2k_read_qcc(opj_j2k_t *j2k) {
912 	int len, compno;
913 	int numcomp = j2k->image->numcomps;
914 	opj_cio_t *cio = j2k->cio;
915 
916 	len = cio_read(cio, 2);	/* Lqcc */
917 	compno = cio_read(cio, numcomp <= 256 ? 1 : 2);	/* Cqcc */
918 
919 #ifdef USE_JPWL
920 	if (j2k->cp->correct) {
921 
922 		static int backup_compno = 0;
923 
924 		/* compno is negative or larger than the number of components!!! */
925 		if ((compno < 0) || (compno >= numcomp)) {
926 			opj_event_msg(j2k->cinfo, EVT_ERROR,
927 				"JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
928 				compno, numcomp);
929 			if (!JPWL_ASSUME) {
930 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
931 				return;
932 			}
933 			/* we try to correct */
934 			compno = backup_compno % numcomp;
935 			opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
936 				"- setting component number to %d\n",
937 				compno);
938 		}
939 
940 		/* keep your private count of tiles */
941 		backup_compno++;
942 	};
943 #endif /* USE_JPWL */
944 
945 	j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
946 }
947 
j2k_write_poc(opj_j2k_t * j2k)948 static void j2k_write_poc(opj_j2k_t *j2k) {
949 	int len, numpchgs, i;
950 
951 	int numcomps = j2k->image->numcomps;
952 
953 	opj_cp_t *cp = j2k->cp;
954 	opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
955 	opj_tccp_t *tccp = &tcp->tccps[0];
956 	opj_cio_t *cio = j2k->cio;
957 
958 	numpchgs = 1 + tcp->numpocs;
959 	cio_write(cio, J2K_MS_POC, 2);	/* POC  */
960 	len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
961 	cio_write(cio, len, 2);		/* Lpoc */
962 	for (i = 0; i < numpchgs; i++) {
963 		opj_poc_t *poc = &tcp->pocs[i];
964 		cio_write(cio, poc->resno0, 1);	/* RSpoc_i */
965 		cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));	/* CSpoc_i */
966 		cio_write(cio, poc->layno1, 2);	/* LYEpoc_i */
967 		poc->layno1 = int_min(poc->layno1, tcp->numlayers);
968 		cio_write(cio, poc->resno1, 1);	/* REpoc_i */
969 		poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
970 		cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));	/* CEpoc_i */
971 		poc->compno1 = int_min(poc->compno1, numcomps);
972 		cio_write(cio, poc->prg, 1);	/* Ppoc_i */
973 	}
974 }
975 
j2k_read_poc(opj_j2k_t * j2k)976 static void j2k_read_poc(opj_j2k_t *j2k) {
977 	int len, numpchgs, i, old_poc;
978 
979 	int numcomps = j2k->image->numcomps;
980 
981 	opj_cp_t *cp = j2k->cp;
982 	opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
983 	opj_cio_t *cio = j2k->cio;
984 
985 	old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
986 	tcp->POC = 1;
987 	len = cio_read(cio, 2);		/* Lpoc */
988 	numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
989 
990 	for (i = old_poc; i < numpchgs + old_poc; i++) {
991 		opj_poc_t *poc;
992 		poc = &tcp->pocs[i];
993 		poc->resno0 = cio_read(cio, 1);	/* RSpoc_i */
994 		poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);	/* CSpoc_i */
995 		poc->layno1 = cio_read(cio, 2);    /* LYEpoc_i */
996 		poc->resno1 = cio_read(cio, 1);    /* REpoc_i */
997 		poc->compno1 = int_min(
998 			cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);	/* CEpoc_i */
999 		poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);	/* Ppoc_i */
1000 	}
1001 
1002 	tcp->numpocs = numpchgs + old_poc - 1;
1003 }
1004 
j2k_read_crg(opj_j2k_t * j2k)1005 static void j2k_read_crg(opj_j2k_t *j2k) {
1006 	int len, i, Xcrg_i, Ycrg_i;
1007 
1008 	opj_cio_t *cio = j2k->cio;
1009 	int numcomps = j2k->image->numcomps;
1010 
1011 	len = cio_read(cio, 2);			/* Lcrg */
1012 	for (i = 0; i < numcomps; i++) {
1013 		Xcrg_i = cio_read(cio, 2);	/* Xcrg_i */
1014 		Ycrg_i = cio_read(cio, 2);	/* Ycrg_i */
1015 	}
1016 }
1017 
j2k_read_tlm(opj_j2k_t * j2k)1018 static void j2k_read_tlm(opj_j2k_t *j2k) {
1019 	int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1020 	long int Ttlm_i, Ptlm_i;
1021 
1022 	opj_cio_t *cio = j2k->cio;
1023 
1024 	len = cio_read(cio, 2);		/* Ltlm */
1025 	Ztlm = cio_read(cio, 1);	/* Ztlm */
1026 	Stlm = cio_read(cio, 1);	/* Stlm */
1027 	ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1028 	SP = (Stlm >> 6) & 0x01;
1029 	tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1030 	for (i = 0; i < tile_tlm; i++) {
1031 		Ttlm_i = cio_read(cio, ST);	/* Ttlm_i */
1032 		Ptlm_i = cio_read(cio, SP ? 4 : 2);	/* Ptlm_i */
1033 	}
1034 }
1035 
j2k_read_plm(opj_j2k_t * j2k)1036 static void j2k_read_plm(opj_j2k_t *j2k) {
1037 	int len, i, Zplm, Nplm, add, packet_len = 0;
1038 
1039 	opj_cio_t *cio = j2k->cio;
1040 
1041 	len = cio_read(cio, 2);		/* Lplm */
1042 	Zplm = cio_read(cio, 1);	/* Zplm */
1043 	len -= 3;
1044 	while (len > 0) {
1045 		Nplm = cio_read(cio, 4);		/* Nplm */
1046 		len -= 4;
1047 		for (i = Nplm; i > 0; i--) {
1048 			add = cio_read(cio, 1);
1049 			len--;
1050 			packet_len = (packet_len << 7) + add;	/* Iplm_ij */
1051 			if ((add & 0x80) == 0) {
1052 				/* New packet */
1053 				packet_len = 0;
1054 			}
1055 			if (len <= 0)
1056 				break;
1057 		}
1058 	}
1059 }
1060 
j2k_read_plt(opj_j2k_t * j2k)1061 static void j2k_read_plt(opj_j2k_t *j2k) {
1062 	int len, i, Zplt, packet_len = 0, add;
1063 
1064 	opj_cio_t *cio = j2k->cio;
1065 
1066 	len = cio_read(cio, 2);		/* Lplt */
1067 	Zplt = cio_read(cio, 1);	/* Zplt */
1068 	for (i = len - 3; i > 0; i--) {
1069 		add = cio_read(cio, 1);
1070 		packet_len = (packet_len << 7) + add;	/* Iplt_i */
1071 		if ((add & 0x80) == 0) {
1072 			/* New packet */
1073 			packet_len = 0;
1074 		}
1075 	}
1076 }
1077 
j2k_read_ppm(opj_j2k_t * j2k)1078 static void j2k_read_ppm(opj_j2k_t *j2k) {
1079 	int len, Z_ppm, i, j;
1080 	int N_ppm;
1081 
1082 	opj_cp_t *cp = j2k->cp;
1083 	opj_cio_t *cio = j2k->cio;
1084 
1085 	len = cio_read(cio, 2);
1086 	cp->ppm = 1;
1087 
1088 	Z_ppm = cio_read(cio, 1);	/* Z_ppm */
1089 	len -= 3;
1090 	while (len > 0) {
1091 		if (cp->ppm_previous == 0) {
1092 			N_ppm = cio_read(cio, 4);	/* N_ppm */
1093 			len -= 4;
1094 		} else {
1095 			N_ppm = cp->ppm_previous;
1096 		}
1097 		j = cp->ppm_store;
1098 		if (Z_ppm == 0) {	/* First PPM marker */
1099 			cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1100 			cp->ppm_data_first = cp->ppm_data;
1101 			cp->ppm_len = N_ppm;
1102 		} else {			/* NON-first PPM marker */
1103 			cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +	cp->ppm_store) * sizeof(unsigned char));
1104 
1105 #ifdef USE_JPWL
1106 			/* this memory allocation check could be done even in non-JPWL cases */
1107 			if (cp->correct) {
1108 				if (!cp->ppm_data) {
1109 					opj_event_msg(j2k->cinfo, EVT_ERROR,
1110 						"JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1111 						cio_tell(cio));
1112 					if (!JPWL_ASSUME || JPWL_ASSUME) {
1113 						opj_free(cp->ppm_data);
1114 						opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1115 						return;
1116 					}
1117 				}
1118 			}
1119 #endif
1120 
1121 			cp->ppm_data_first = cp->ppm_data;
1122 			cp->ppm_len = N_ppm + cp->ppm_store;
1123 		}
1124 		for (i = N_ppm; i > 0; i--) {	/* Read packet header */
1125 			cp->ppm_data[j] = cio_read(cio, 1);
1126 			j++;
1127 			len--;
1128 			if (len == 0)
1129 				break;			/* Case of non-finished packet header in present marker but finished in next one */
1130 		}
1131 		cp->ppm_previous = i - 1;
1132 		cp->ppm_store = j;
1133 	}
1134 }
1135 
j2k_read_ppt(opj_j2k_t * j2k)1136 static void j2k_read_ppt(opj_j2k_t *j2k) {
1137 	int len, Z_ppt, i, j = 0;
1138 
1139 	opj_cp_t *cp = j2k->cp;
1140 	opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1141 	opj_cio_t *cio = j2k->cio;
1142 
1143 	len = cio_read(cio, 2);
1144 	Z_ppt = cio_read(cio, 1);
1145 	tcp->ppt = 1;
1146 	if (Z_ppt == 0) {		/* First PPT marker */
1147 		tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1148 		tcp->ppt_data_first = tcp->ppt_data;
1149 		tcp->ppt_store = 0;
1150 		tcp->ppt_len = len - 3;
1151 	} else {			/* NON-first PPT marker */
1152 		tcp->ppt_data =	(unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1153 		tcp->ppt_data_first = tcp->ppt_data;
1154 		tcp->ppt_len = len - 3 + tcp->ppt_store;
1155 	}
1156 	j = tcp->ppt_store;
1157 	for (i = len - 3; i > 0; i--) {
1158 		tcp->ppt_data[j] = cio_read(cio, 1);
1159 		j++;
1160 	}
1161 	tcp->ppt_store = j;
1162 }
1163 
j2k_write_tlm(opj_j2k_t * j2k)1164 static void j2k_write_tlm(opj_j2k_t *j2k){
1165 	int lenp;
1166 	opj_cio_t *cio = j2k->cio;
1167 	j2k->tlm_start = cio_tell(cio);
1168 	cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1169 	lenp = 4 + (5*j2k->totnum_tp);
1170 	cio_write(cio,lenp,2);				/* Ltlm */
1171 	cio_write(cio, 0,1);					/* Ztlm=0*/
1172 	cio_write(cio,80,1);					/* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1173 	cio_skip(cio,5*j2k->totnum_tp);
1174 }
1175 
j2k_write_sot(opj_j2k_t * j2k)1176 static void j2k_write_sot(opj_j2k_t *j2k) {
1177 	int lenp, len;
1178 
1179 	opj_cio_t *cio = j2k->cio;
1180 
1181 	j2k->sot_start = cio_tell(cio);
1182 	cio_write(cio, J2K_MS_SOT, 2);		/* SOT */
1183 	lenp = cio_tell(cio);
1184 	cio_skip(cio, 2);					/* Lsot (further) */
1185 	cio_write(cio, j2k->curtileno, 2);	/* Isot */
1186 	cio_skip(cio, 4);					/* Psot (further in j2k_write_sod) */
1187 	cio_write(cio, j2k->cur_tp_num , 1);	/* TPsot */
1188 	cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1);		/* TNsot */
1189 	len = cio_tell(cio) - lenp;
1190 	cio_seek(cio, lenp);
1191 	cio_write(cio, len, 2);				/* Lsot */
1192 	cio_seek(cio, lenp + len);
1193 
1194 	/* UniPG>> */
1195 #ifdef USE_JPWL
1196 	/* update markers struct */
1197 	j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1198 #endif /* USE_JPWL */
1199 	/* <<UniPG */
1200 }
1201 
j2k_read_sot(opj_j2k_t * j2k)1202 static void j2k_read_sot(opj_j2k_t *j2k) {
1203 	int len, tileno, totlen, partno, numparts, i;
1204 	opj_tcp_t *tcp = NULL;
1205 	char status = 0;
1206 
1207 	opj_cp_t *cp = j2k->cp;
1208 	opj_cio_t *cio = j2k->cio;
1209 
1210 	len = cio_read(cio, 2);
1211 	tileno = cio_read(cio, 2);
1212 
1213 #ifdef USE_JPWL
1214 	if (j2k->cp->correct) {
1215 
1216 		static int backup_tileno = 0;
1217 
1218 		/* tileno is negative or larger than the number of tiles!!! */
1219 		if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1220 			opj_event_msg(j2k->cinfo, EVT_ERROR,
1221 				"JPWL: bad tile number (%d out of a maximum of %d)\n",
1222 				tileno, (cp->tw * cp->th));
1223 			if (!JPWL_ASSUME) {
1224 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1225 				return;
1226 			}
1227 			/* we try to correct */
1228 			tileno = backup_tileno;
1229 			opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1230 				"- setting tile number to %d\n",
1231 				tileno);
1232 		}
1233 
1234 		/* keep your private count of tiles */
1235 		backup_tileno++;
1236 	}
1237   else
1238 #endif /* USE_JPWL */
1239   {
1240     /* tileno is negative or larger than the number of tiles!!! */
1241     if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1242       opj_event_msg(j2k->cinfo, EVT_ERROR,
1243         "JPWL: bad tile number (%d out of a maximum of %d)\n",
1244         tileno, (cp->tw * cp->th));
1245       return;
1246     }
1247   }
1248 
1249 	if (cp->tileno_size == 0) {
1250 		cp->tileno[cp->tileno_size] = tileno;
1251 		cp->tileno_size++;
1252 	} else {
1253 		i = 0;
1254 		while (i < cp->tileno_size && status == 0) {
1255 			status = cp->tileno[i] == tileno ? 1 : 0;
1256 			i++;
1257 		}
1258 		if (status == 0) {
1259 			cp->tileno[cp->tileno_size] = tileno;
1260 			cp->tileno_size++;
1261 		}
1262 	}
1263 
1264 	totlen = cio_read(cio, 4);
1265 
1266 #ifdef USE_JPWL
1267 	if (j2k->cp->correct) {
1268 
1269 		/* totlen is negative or larger than the bytes left!!! */
1270 		if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1271 			opj_event_msg(j2k->cinfo, EVT_ERROR,
1272 				"JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1273 				totlen, cio_numbytesleft(cio) + 8);
1274 			if (!JPWL_ASSUME) {
1275 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1276 				return;
1277 			}
1278 			/* we try to correct */
1279 			totlen = 0;
1280 			opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1281 				"- setting Psot to %d => assuming it is the last tile\n",
1282 				totlen);
1283 		}
1284 
1285 	}
1286   else
1287 #endif /* USE_JPWL */
1288   {
1289     /* totlen is negative or larger than the bytes left!!! */
1290     if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1291       opj_event_msg(j2k->cinfo, EVT_ERROR,
1292         "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1293         totlen, cio_numbytesleft(cio) + 8);
1294       return;
1295     }
1296   }
1297 
1298 	if (!totlen)
1299 		totlen = cio_numbytesleft(cio) + 8;
1300 
1301 	partno = cio_read(cio, 1);
1302 	numparts = cio_read(cio, 1);
1303 
1304 	j2k->curtileno = tileno;
1305 	j2k->cur_tp_num = partno;
1306 	j2k->eot = cio_getbp(cio) - 12 + totlen;
1307 	j2k->state = J2K_STATE_TPH;
1308 	tcp = &cp->tcps[j2k->curtileno];
1309 
1310 	/* Index */
1311 	if (j2k->cstr_info) {
1312 		if (tcp->first) {
1313 			if (tileno == 0)
1314 				j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1315 			j2k->cstr_info->tile[tileno].tileno = tileno;
1316 			j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1317 			j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
1318 			j2k->cstr_info->tile[tileno].num_tps = numparts;
1319 			if (numparts)
1320 				j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
1321 			else
1322 				j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
1323 		}
1324 		else {
1325 			j2k->cstr_info->tile[tileno].end_pos += totlen;
1326 		}
1327 		j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1328 		j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1329 			j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1330 	}
1331 
1332 	if (tcp->first == 1) {
1333 		/* Initialization PPT */
1334 		opj_tccp_t *tmp = tcp->tccps;
1335 		memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1336 		tcp->ppt = 0;
1337 		tcp->ppt_data = NULL;
1338 		tcp->ppt_data_first = NULL;
1339 		tcp->tccps = tmp;
1340 
1341 		for (i = 0; i < j2k->image->numcomps; i++) {
1342 			tcp->tccps[i] = j2k->default_tcp->tccps[i];
1343 		}
1344 		cp->tcps[j2k->curtileno].first = 0;
1345 	}
1346 }
1347 
j2k_write_sod(opj_j2k_t * j2k,void * tile_coder)1348 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
1349 	int l, layno;
1350 	int totlen;
1351 	opj_tcp_t *tcp = NULL;
1352 	opj_codestream_info_t *cstr_info = NULL;
1353 
1354 	opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;	/* cast is needed because of conflicts in header inclusions */
1355 	opj_cp_t *cp = j2k->cp;
1356 	opj_cio_t *cio = j2k->cio;
1357 
1358 	tcd->tp_num = j2k->tp_num ;
1359 	tcd->cur_tp_num = j2k->cur_tp_num;
1360 
1361 	cio_write(cio, J2K_MS_SOD, 2);
1362 	if (j2k->curtileno == 0) {
1363 		j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1364 	}
1365 
1366 	/* INDEX >> */
1367 	cstr_info = j2k->cstr_info;
1368 	if (cstr_info) {
1369 		if (!j2k->cur_tp_num ) {
1370 			cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1371 			j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1372 		}
1373 		else{
1374 			if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
1375 				cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
1376 		}
1377 		/* UniPG>> */
1378 #ifdef USE_JPWL
1379 		/* update markers struct */
1380 		j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1381 #endif /* USE_JPWL */
1382 		/* <<UniPG */
1383 	}
1384 	/* << INDEX */
1385 
1386 	tcp = &cp->tcps[j2k->curtileno];
1387 	for (layno = 0; layno < tcp->numlayers; layno++) {
1388 		if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
1389 			tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
1390 		} else if (tcp->rates[layno]) {
1391 			tcp->rates[layno]=1;
1392 		}
1393 	}
1394 	if(j2k->cur_tp_num == 0){
1395 		tcd->tcd_image->tiles->packno = 0;
1396 		if(cstr_info)
1397 			cstr_info->packno = 0;
1398 	}
1399 
1400 	l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
1401 
1402 	/* Writing Psot in SOT marker */
1403 	totlen = cio_tell(cio) + l - j2k->sot_start;
1404 	cio_seek(cio, j2k->sot_start + 6);
1405 	cio_write(cio, totlen, 4);
1406 	cio_seek(cio, j2k->sot_start + totlen);
1407 	/* Writing Ttlm and Ptlm in TLM marker */
1408 	if(cp->cinema){
1409 		cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
1410 		cio_write(cio, j2k->curtileno, 1);
1411 		cio_write(cio, totlen, 4);
1412 	}
1413 	cio_seek(cio, j2k->sot_start + totlen);
1414 }
1415 
j2k_read_sod(opj_j2k_t * j2k)1416 static void j2k_read_sod(opj_j2k_t *j2k) {
1417 	int len, truncate = 0, i;
1418 	unsigned char *data = NULL, *data_ptr = NULL;
1419 
1420 	opj_cio_t *cio = j2k->cio;
1421 	int curtileno = j2k->curtileno;
1422 
1423 	/* Index */
1424 	if (j2k->cstr_info) {
1425 		j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1426 			cio_tell(cio) + j2k->pos_correction - 1;
1427 		if (j2k->cur_tp_num == 0)
1428 			j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1429 		j2k->cstr_info->packno = 0;
1430 	}
1431 
1432 	len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1433 
1434 	if (len == cio_numbytesleft(cio) + 1) {
1435 		truncate = 1;		/* Case of a truncate codestream */
1436 	}
1437 
1438 	data = j2k->tile_data[curtileno];
1439 	data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1440 
1441 	data_ptr = data + j2k->tile_len[curtileno];
1442 	for (i = 0; i < len; i++) {
1443 		data_ptr[i] = cio_read(cio, 1);
1444 	}
1445 
1446 	j2k->tile_len[curtileno] += len;
1447 	j2k->tile_data[curtileno] = data;
1448 
1449 	if (!truncate) {
1450 		j2k->state = J2K_STATE_TPHSOT;
1451 	} else {
1452 		j2k->state = J2K_STATE_NEOC;	/* RAJOUTE !! */
1453 	}
1454 	j2k->cur_tp_num++;
1455 }
1456 
j2k_write_rgn(opj_j2k_t * j2k,int compno,int tileno)1457 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1458 	opj_cp_t *cp = j2k->cp;
1459 	opj_tcp_t *tcp = &cp->tcps[tileno];
1460 	opj_cio_t *cio = j2k->cio;
1461 	int numcomps = j2k->image->numcomps;
1462 
1463 	cio_write(cio, J2K_MS_RGN, 2);						/* RGN  */
1464 	cio_write(cio, numcomps <= 256 ? 5 : 6, 2);			/* Lrgn */
1465 	cio_write(cio, compno, numcomps <= 256 ? 1 : 2);	/* Crgn */
1466 	cio_write(cio, 0, 1);								/* Srgn */
1467 	cio_write(cio, tcp->tccps[compno].roishift, 1);		/* SPrgn */
1468 }
1469 
j2k_read_rgn(opj_j2k_t * j2k)1470 static void j2k_read_rgn(opj_j2k_t *j2k) {
1471 	int len, compno, roisty;
1472 
1473 	opj_cp_t *cp = j2k->cp;
1474 	opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1475 	opj_cio_t *cio = j2k->cio;
1476 	int numcomps = j2k->image->numcomps;
1477 
1478 	len = cio_read(cio, 2);										/* Lrgn */
1479 	compno = cio_read(cio, numcomps <= 256 ? 1 : 2);			/* Crgn */
1480 	roisty = cio_read(cio, 1);									/* Srgn */
1481 
1482 #ifdef USE_JPWL
1483 	if (j2k->cp->correct) {
1484 		/* totlen is negative or larger than the bytes left!!! */
1485 		if (compno >= numcomps) {
1486 			opj_event_msg(j2k->cinfo, EVT_ERROR,
1487 				"JPWL: bad component number in RGN (%d when there are only %d)\n",
1488 				compno, numcomps);
1489 			if (!JPWL_ASSUME || JPWL_ASSUME) {
1490 				opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1491 				return;
1492 			}
1493 		}
1494 	};
1495 #endif /* USE_JPWL */
1496 
1497 	tcp->tccps[compno].roishift = cio_read(cio, 1);				/* SPrgn */
1498 }
1499 
j2k_write_eoc(opj_j2k_t * j2k)1500 static void j2k_write_eoc(opj_j2k_t *j2k) {
1501 	opj_cio_t *cio = j2k->cio;
1502 	/* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1503 	cio_write(cio, J2K_MS_EOC, 2);
1504 
1505 /* UniPG>> */
1506 #ifdef USE_JPWL
1507 	/* update markers struct */
1508 	j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1509 #endif /* USE_JPWL */
1510 /* <<UniPG */
1511 }
1512 
j2k_read_eoc(opj_j2k_t * j2k)1513 static void j2k_read_eoc(opj_j2k_t *j2k) {
1514 	int i, tileno;
1515 	bool success = false;
1516 
1517 	/* if packets should be decoded */
1518 	if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1519 		opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1520 		tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1521 		for (i = 0; i < j2k->cp->tileno_size; i++) {
1522 			tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1523 			if (j2k->cp->tileno[i] != -1)
1524 			{
1525 				tileno = j2k->cp->tileno[i];
1526 				success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
1527 				opj_free(j2k->tile_data[tileno]);
1528 				j2k->tile_data[tileno] = NULL;
1529 				tcd_free_decode_tile(tcd, i);
1530 			}
1531 			else
1532 				success = false;
1533 			if (success == false) {
1534 				j2k->state |= J2K_STATE_ERR;
1535 				break;
1536 			}
1537 		}
1538 		tcd_free_decode(tcd);
1539 		tcd_destroy(tcd);
1540 	}
1541 	/* if packets should not be decoded  */
1542 	else {
1543 		for (i = 0; i < j2k->cp->tileno_size; i++) {
1544 			tileno = j2k->cp->tileno[i];
1545 			opj_free(j2k->tile_data[tileno]);
1546 			j2k->tile_data[tileno] = NULL;
1547 		}
1548 	}
1549 	if (j2k->state & J2K_STATE_ERR)
1550 		j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1551 	else
1552 		j2k->state = J2K_STATE_MT;
1553 }
1554 
1555 typedef struct opj_dec_mstabent {
1556 	/** marker value */
1557 	int id;
1558 	/** value of the state when the marker can appear */
1559 	int states;
1560 	/** action linked to the marker */
1561 	void (*handler) (opj_j2k_t *j2k);
1562 } opj_dec_mstabent_t;
1563 
1564 opj_dec_mstabent_t j2k_dec_mstab[] = {
1565   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1566   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1567   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1568   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1569   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1570   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1571   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1572   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1573   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1574   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1575   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1576   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1577   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1578   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1579   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1580   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1581   {J2K_MS_SOP, 0, 0},
1582   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1583   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1584 
1585 #ifdef USE_JPWL
1586   {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1587   {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1588   {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1589   {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1590 #endif /* USE_JPWL */
1591 #ifdef USE_JPSEC
1592   {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1593   {J2K_MS_INSEC, 0, j2k_read_insec},
1594 #endif /* USE_JPSEC */
1595 
1596   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1597 };
1598 
j2k_read_unk(opj_j2k_t * j2k)1599 static void j2k_read_unk(opj_j2k_t *j2k) {
1600 	opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1601 
1602 #ifdef USE_JPWL
1603 	if (j2k->cp->correct) {
1604 		int m = 0, id, i;
1605 		int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1606 		cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1607 		id = cio_read(j2k->cio, 2);
1608 		opj_event_msg(j2k->cinfo, EVT_ERROR,
1609 			"JPWL: really don't know this marker %x\n",
1610 			id);
1611 		if (!JPWL_ASSUME) {
1612 			opj_event_msg(j2k->cinfo, EVT_ERROR,
1613 				"- possible synch loss due to uncorrectable codestream errors => giving up\n");
1614 			return;
1615 		}
1616 		/* OK, activate this at your own risk!!! */
1617 		/* we look for the marker at the minimum hamming distance from this */
1618 		while (j2k_dec_mstab[m].id) {
1619 
1620 			/* 1's where they differ */
1621 			tmp_id = j2k_dec_mstab[m].id ^ id;
1622 
1623 			/* compute the hamming distance between our id and the current */
1624 			cur_dist = 0;
1625 			for (i = 0; i < 16; i++) {
1626 				if ((tmp_id >> i) & 0x0001) {
1627 					cur_dist++;
1628 				}
1629 			}
1630 
1631 			/* if current distance is smaller, set the minimum */
1632 			if (cur_dist < min_dist) {
1633 				min_dist = cur_dist;
1634 				min_id = j2k_dec_mstab[m].id;
1635 			}
1636 
1637 			/* jump to the next marker */
1638 			m++;
1639 		}
1640 
1641 		/* do we substitute the marker? */
1642 		if (min_dist < JPWL_MAXIMUM_HAMMING) {
1643 			opj_event_msg(j2k->cinfo, EVT_ERROR,
1644 				"- marker %x is at distance %d from the read %x\n",
1645 				min_id, min_dist, id);
1646 			opj_event_msg(j2k->cinfo, EVT_ERROR,
1647 				"- trying to substitute in place and crossing fingers!\n");
1648 			cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1649 			cio_write(j2k->cio, min_id, 2);
1650 
1651 			/* rewind */
1652 			cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1653 
1654 		}
1655 
1656 	};
1657 #endif /* USE_JPWL */
1658 
1659 }
1660 
1661 /**
1662 Read the lookup table containing all the marker, status and action
1663 @param id Marker value
1664 */
j2k_dec_mstab_lookup(int id)1665 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1666 	opj_dec_mstabent_t *e;
1667 	for (e = j2k_dec_mstab; e->id != 0; e++) {
1668 		if (e->id == id) {
1669 			break;
1670 		}
1671 	}
1672 	return e;
1673 }
1674 
1675 /* ----------------------------------------------------------------------- */
1676 /* J2K / JPT decoder interface                                             */
1677 /* ----------------------------------------------------------------------- */
1678 
j2k_create_decompress(opj_common_ptr cinfo)1679 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1680 	opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1681 	if(!j2k)
1682 		return NULL;
1683 
1684 	j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1685 	if(!j2k->default_tcp) {
1686 		opj_free(j2k);
1687 		return NULL;
1688 	}
1689 
1690 	j2k->cinfo = cinfo;
1691 	j2k->tile_data = NULL;
1692 
1693 	return j2k;
1694 }
1695 
j2k_destroy_decompress(opj_j2k_t * j2k)1696 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1697 	int i = 0;
1698 
1699 	if(j2k->tile_len != NULL) {
1700 		opj_free(j2k->tile_len);
1701 	}
1702 	if(j2k->tile_data != NULL) {
1703 		opj_free(j2k->tile_data);
1704 	}
1705 	if(j2k->default_tcp != NULL) {
1706 		opj_tcp_t *default_tcp = j2k->default_tcp;
1707 		if(default_tcp->ppt_data_first != NULL) {
1708 			opj_free(default_tcp->ppt_data_first);
1709 		}
1710 		if(j2k->default_tcp->tccps != NULL) {
1711 			opj_free(j2k->default_tcp->tccps);
1712 		}
1713 		opj_free(j2k->default_tcp);
1714 	}
1715 	if(j2k->cp != NULL) {
1716 		opj_cp_t *cp = j2k->cp;
1717 		if(cp->tcps != NULL) {
1718 			for(i = 0; i < cp->tw * cp->th; i++) {
1719 				if(cp->tcps[i].ppt_data_first != NULL) {
1720 					opj_free(cp->tcps[i].ppt_data_first);
1721 				}
1722 				if(cp->tcps[i].tccps != NULL) {
1723 					opj_free(cp->tcps[i].tccps);
1724 				}
1725 			}
1726 			opj_free(cp->tcps);
1727 		}
1728 		if(cp->ppm_data_first != NULL) {
1729 			opj_free(cp->ppm_data_first);
1730 		}
1731 		if(cp->tileno != NULL) {
1732 			opj_free(cp->tileno);
1733 		}
1734 		if(cp->comment != NULL) {
1735 			opj_free(cp->comment);
1736 		}
1737 
1738 		opj_free(cp);
1739 	}
1740 	opj_free(j2k);
1741 }
1742 
j2k_setup_decoder(opj_j2k_t * j2k,opj_dparameters_t * parameters)1743 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1744 	if(j2k && parameters) {
1745 		/* create and initialize the coding parameters structure */
1746 		opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1747 		cp->reduce = parameters->cp_reduce;
1748 		cp->layer = parameters->cp_layer;
1749 		cp->limit_decoding = parameters->cp_limit_decoding;
1750 
1751 #ifdef USE_JPWL
1752 		cp->correct = parameters->jpwl_correct;
1753 		cp->exp_comps = parameters->jpwl_exp_comps;
1754 		cp->max_tiles = parameters->jpwl_max_tiles;
1755 #endif /* USE_JPWL */
1756 
1757 
1758 		/* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1759 		j2k->cp = cp;
1760 	}
1761 }
1762 
j2k_decode(opj_j2k_t * j2k,opj_cio_t * cio,opj_codestream_info_t * cstr_info)1763 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1764 	opj_image_t *image = NULL;
1765 
1766 	opj_common_ptr cinfo = j2k->cinfo;
1767 
1768 	j2k->cio = cio;
1769 	j2k->cstr_info = cstr_info;
1770 	if (cstr_info)
1771 		memset(cstr_info, 0, sizeof(opj_codestream_info_t));
1772 
1773 	/* create an empty image */
1774 	image = opj_image_create0();
1775 	j2k->image = image;
1776 
1777 	j2k->state = J2K_STATE_MHSOC;
1778 
1779 	for (;;) {
1780 		opj_dec_mstabent_t *e;
1781 		int id = cio_read(cio, 2);
1782 
1783 #ifdef USE_JPWL
1784 		/* we try to honor JPWL correction power */
1785 		if (j2k->cp->correct) {
1786 
1787 			int orig_pos = cio_tell(cio);
1788 			bool status;
1789 
1790 			/* call the corrector */
1791 			status = jpwl_correct(j2k);
1792 
1793 			/* go back to where you were */
1794 			cio_seek(cio, orig_pos - 2);
1795 
1796 			/* re-read the marker */
1797 			id = cio_read(cio, 2);
1798 
1799 			/* check whether it begins with ff */
1800 			if (id >> 8 != 0xff) {
1801 				opj_event_msg(cinfo, EVT_ERROR,
1802 					"JPWL: possible bad marker %x at %d\n",
1803 					id, cio_tell(cio) - 2);
1804 				if (!JPWL_ASSUME) {
1805 					opj_image_destroy(image);
1806 					opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
1807 					return 0;
1808 				}
1809 				/* we try to correct */
1810 				id = id | 0xff00;
1811 				cio_seek(cio, cio_tell(cio) - 2);
1812 				cio_write(cio, id, 2);
1813 				opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
1814 					"- setting marker to %x\n",
1815 					id);
1816 			}
1817 
1818 		}
1819 #endif /* USE_JPWL */
1820 
1821 		if (id >> 8 != 0xff) {
1822 			opj_image_destroy(image);
1823 			opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1824 			return 0;
1825 		}
1826 		e = j2k_dec_mstab_lookup(id);
1827 		// Check if the marker is known
1828 		if (!(j2k->state & e->states)) {
1829 			opj_image_destroy(image);
1830 			opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1831 			return 0;
1832 		}
1833 		// Check if the decoding is limited to the main header
1834 		if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
1835 			opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
1836 			return image;
1837 		}
1838 
1839 		if (e->handler) {
1840 			(*e->handler)(j2k);
1841 		}
1842 		if (j2k->state & J2K_STATE_ERR)
1843 			return NULL;
1844 
1845 		if (j2k->state == J2K_STATE_MT) {
1846 			break;
1847 		}
1848 		if (j2k->state == J2K_STATE_NEOC) {
1849 			break;
1850 		}
1851 	}
1852 	if (j2k->state == J2K_STATE_NEOC) {
1853 		j2k_read_eoc(j2k);
1854 	}
1855 
1856 	if (j2k->state != J2K_STATE_MT) {
1857 		opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1858 	}
1859 
1860 	return image;
1861 }
1862 
1863 /*
1864 * Read a JPT-stream and decode file
1865 *
1866 */
j2k_decode_jpt_stream(opj_j2k_t * j2k,opj_cio_t * cio,opj_codestream_info_t * cstr_info)1867 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,  opj_codestream_info_t *cstr_info) {
1868 	opj_image_t *image = NULL;
1869 	opj_jpt_msg_header_t header;
1870 	int position;
1871 
1872 	opj_common_ptr cinfo = j2k->cinfo;
1873 
1874 	j2k->cio = cio;
1875 
1876 	/* create an empty image */
1877 	image = opj_image_create0();
1878 	j2k->image = image;
1879 
1880 	j2k->state = J2K_STATE_MHSOC;
1881 
1882 	/* Initialize the header */
1883 	jpt_init_msg_header(&header);
1884 	/* Read the first header of the message */
1885 	jpt_read_msg_header(cinfo, cio, &header);
1886 
1887 	position = cio_tell(cio);
1888 	if (header.Class_Id != 6) {	/* 6 : Main header data-bin message */
1889 		opj_image_destroy(image);
1890 		opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1891 		return 0;
1892 	}
1893 
1894 	for (;;) {
1895 		opj_dec_mstabent_t *e = NULL;
1896 		int id;
1897 
1898 		if (!cio_numbytesleft(cio)) {
1899 			j2k_read_eoc(j2k);
1900 			return image;
1901 		}
1902 		/* data-bin read -> need to read a new header */
1903 		if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1904 			jpt_read_msg_header(cinfo, cio, &header);
1905 			position = cio_tell(cio);
1906 			if (header.Class_Id != 4) {	/* 4 : Tile data-bin message */
1907 				opj_image_destroy(image);
1908 				opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1909 				return 0;
1910 			}
1911 		}
1912 
1913 		id = cio_read(cio, 2);
1914 		if (id >> 8 != 0xff) {
1915 			opj_image_destroy(image);
1916 			opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1917 			return 0;
1918 		}
1919 		e = j2k_dec_mstab_lookup(id);
1920 		if (!(j2k->state & e->states)) {
1921 			opj_image_destroy(image);
1922 			opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1923 			return 0;
1924 		}
1925 		if (e->handler) {
1926 			(*e->handler)(j2k);
1927 		}
1928 		if (j2k->state == J2K_STATE_MT) {
1929 			break;
1930 		}
1931 		if (j2k->state == J2K_STATE_NEOC) {
1932 			break;
1933 		}
1934 	}
1935 	if (j2k->state == J2K_STATE_NEOC) {
1936 		j2k_read_eoc(j2k);
1937 	}
1938 
1939 	if (j2k->state != J2K_STATE_MT) {
1940 		opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1941 	}
1942 
1943 	return image;
1944 }
1945 
1946 /* ----------------------------------------------------------------------- */
1947 /* J2K encoder interface                                                       */
1948 /* ----------------------------------------------------------------------- */
1949 
j2k_create_compress(opj_common_ptr cinfo)1950 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1951 	opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1952 	if(j2k) {
1953 		j2k->cinfo = cinfo;
1954 	}
1955 	return j2k;
1956 }
1957 
j2k_destroy_compress(opj_j2k_t * j2k)1958 void j2k_destroy_compress(opj_j2k_t *j2k) {
1959 	int tileno;
1960 
1961 	if(!j2k) return;
1962 	if(j2k->cp != NULL) {
1963 		opj_cp_t *cp = j2k->cp;
1964 
1965 		if(cp->comment) {
1966 			opj_free(cp->comment);
1967 		}
1968 		if(cp->matrice) {
1969 			opj_free(cp->matrice);
1970 		}
1971 		for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1972 			opj_free(cp->tcps[tileno].tccps);
1973 		}
1974 		opj_free(cp->tcps);
1975 		opj_free(cp);
1976 	}
1977 
1978 	opj_free(j2k);
1979 }
1980 
j2k_setup_encoder(opj_j2k_t * j2k,opj_cparameters_t * parameters,opj_image_t * image)1981 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1982 	int i, j, tileno, numpocs_tile;
1983 	opj_cp_t *cp = NULL;
1984 
1985 	if(!j2k || !parameters || ! image) {
1986 		return;
1987 	}
1988 
1989 	/* create and initialize the coding parameters structure */
1990 	cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1991 
1992 	/* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1993 	j2k->cp = cp;
1994 
1995 	/* set default values for cp */
1996 	cp->tw = 1;
1997 	cp->th = 1;
1998 
1999 	/*
2000 	copy user encoding parameters
2001 	*/
2002 	cp->cinema = parameters->cp_cinema;
2003 	cp->max_comp_size =	parameters->max_comp_size;
2004 	cp->rsiz   = parameters->cp_rsiz;
2005 	cp->disto_alloc = parameters->cp_disto_alloc;
2006 	cp->fixed_alloc = parameters->cp_fixed_alloc;
2007 	cp->fixed_quality = parameters->cp_fixed_quality;
2008 
2009 	/* mod fixed_quality */
2010 	if(parameters->cp_matrice) {
2011 		size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
2012 		cp->matrice = (int *) opj_malloc(array_size);
2013 		memcpy(cp->matrice, parameters->cp_matrice, array_size);
2014 	}
2015 
2016 	/* tiles */
2017 	cp->tdx = parameters->cp_tdx;
2018 	cp->tdy = parameters->cp_tdy;
2019 
2020 	/* tile offset */
2021 	cp->tx0 = parameters->cp_tx0;
2022 	cp->ty0 = parameters->cp_ty0;
2023 
2024 	/* comment string */
2025 	if(parameters->cp_comment) {
2026 		cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2027 		if(cp->comment) {
2028 			strcpy(cp->comment, parameters->cp_comment);
2029 		}
2030 	}
2031 
2032 	/*
2033 	calculate other encoding parameters
2034 	*/
2035 
2036 	if (parameters->tile_size_on) {
2037 		cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2038 		cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2039 	} else {
2040 		cp->tdx = image->x1 - cp->tx0;
2041 		cp->tdy = image->y1 - cp->ty0;
2042 	}
2043 
2044 	if(parameters->tp_on){
2045 		cp->tp_flag = parameters->tp_flag;
2046 		cp->tp_on = 1;
2047 	}
2048 
2049 	cp->img_size = 0;
2050 	for(i=0;i<image->numcomps ;i++){
2051 	cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
2052 	}
2053 
2054 
2055 #ifdef USE_JPWL
2056 	/*
2057 	calculate JPWL encoding parameters
2058 	*/
2059 
2060 	if (parameters->jpwl_epc_on) {
2061 		int i;
2062 
2063 		/* set JPWL on */
2064 		cp->epc_on = true;
2065 		cp->info_on = false; /* no informative technique */
2066 
2067 		/* set EPB on */
2068 		if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2069 			cp->epb_on = true;
2070 
2071 			cp->hprot_MH = parameters->jpwl_hprot_MH;
2072 			for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2073 				cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2074 				cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2075 			}
2076 			/* if tile specs are not specified, copy MH specs */
2077 			if (cp->hprot_TPH[0] == -1) {
2078 				cp->hprot_TPH_tileno[0] = 0;
2079 				cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2080 			}
2081 			for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2082 				cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2083 				cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2084 				cp->pprot[i] = parameters->jpwl_pprot[i];
2085 			}
2086 		}
2087 
2088 		/* set ESD writing */
2089 		if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2090 			cp->esd_on = true;
2091 
2092 			cp->sens_size = parameters->jpwl_sens_size;
2093 			cp->sens_addr = parameters->jpwl_sens_addr;
2094 			cp->sens_range = parameters->jpwl_sens_range;
2095 
2096 			cp->sens_MH = parameters->jpwl_sens_MH;
2097 			for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2098 				cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2099 				cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2100 			}
2101 		}
2102 
2103 		/* always set RED writing to false: we are at the encoder */
2104 		cp->red_on = false;
2105 
2106 	} else {
2107 		cp->epc_on = false;
2108 	}
2109 #endif /* USE_JPWL */
2110 
2111 
2112 	/* initialize the mutiple tiles */
2113 	/* ---------------------------- */
2114 	cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2115 
2116 	for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2117 		opj_tcp_t *tcp = &cp->tcps[tileno];
2118 		tcp->numlayers = parameters->tcp_numlayers;
2119 		for (j = 0; j < tcp->numlayers; j++) {
2120 			if(cp->cinema){
2121 				if (cp->fixed_quality) {
2122 					tcp->distoratio[j] = parameters->tcp_distoratio[j];
2123 				}
2124 				tcp->rates[j] = parameters->tcp_rates[j];
2125 			}else{
2126 				if (cp->fixed_quality) {	/* add fixed_quality */
2127 					tcp->distoratio[j] = parameters->tcp_distoratio[j];
2128 				} else {
2129 					tcp->rates[j] = parameters->tcp_rates[j];
2130 				}
2131 			}
2132 		}
2133 		tcp->csty = parameters->csty;
2134 		tcp->prg = parameters->prog_order;
2135 		tcp->mct = parameters->tcp_mct;
2136 
2137 		numpocs_tile = 0;
2138 		tcp->POC = 0;
2139 		if (parameters->numpocs) {
2140 			/* initialisation of POC */
2141 			tcp->POC = 1;
2142 			for (i = 0; i < parameters->numpocs; i++) {
2143 				if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2144 					opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2145 					tcp_poc->resno0		= parameters->POC[numpocs_tile].resno0;
2146 					tcp_poc->compno0	= parameters->POC[numpocs_tile].compno0;
2147 					tcp_poc->layno1		= parameters->POC[numpocs_tile].layno1;
2148 					tcp_poc->resno1		= parameters->POC[numpocs_tile].resno1;
2149 					tcp_poc->compno1	= parameters->POC[numpocs_tile].compno1;
2150 					tcp_poc->prg1		= parameters->POC[numpocs_tile].prg1;
2151 					tcp_poc->tile		= parameters->POC[numpocs_tile].tile;
2152 					numpocs_tile++;
2153 				}
2154 			}
2155 			tcp->numpocs = numpocs_tile -1 ;
2156 		}else{
2157 			tcp->numpocs = 0;
2158 		}
2159 
2160 		tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2161 
2162 		for (i = 0; i < image->numcomps; i++) {
2163 			opj_tccp_t *tccp = &tcp->tccps[i];
2164 			tccp->csty = parameters->csty & 0x01;	/* 0 => one precinct || 1 => custom precinct  */
2165 			tccp->numresolutions = parameters->numresolution;
2166 			tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2167 			tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2168 			tccp->cblksty = parameters->mode;
2169 			tccp->qmfbid = parameters->irreversible ? 0 : 1;
2170 			tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
2171 			tccp->numgbits = 2;
2172 			if (i == parameters->roi_compno) {
2173 				tccp->roishift = parameters->roi_shift;
2174 			} else {
2175 				tccp->roishift = 0;
2176 			}
2177 
2178 			if(parameters->cp_cinema)
2179 			{
2180 				//Precinct size for lowest frequency subband=128
2181 				tccp->prcw[0] = 7;
2182 				tccp->prch[0] = 7;
2183 				//Precinct size at all other resolutions = 256
2184 				for (j = 1; j < tccp->numresolutions; j++) {
2185 					tccp->prcw[j] = 8;
2186 					tccp->prch[j] = 8;
2187 				}
2188 			}else{
2189 				if (parameters->csty & J2K_CCP_CSTY_PRT) {
2190 					int p = 0;
2191 					for (j = tccp->numresolutions - 1; j >= 0; j--) {
2192 						if (p < parameters->res_spec) {
2193 
2194 							if (parameters->prcw_init[p] < 1) {
2195 								tccp->prcw[j] = 1;
2196 							} else {
2197 								tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2198 							}
2199 
2200 							if (parameters->prch_init[p] < 1) {
2201 								tccp->prch[j] = 1;
2202 							}else {
2203 								tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2204 							}
2205 
2206 						} else {
2207 							int res_spec = parameters->res_spec;
2208 							int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2209 							int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2210 
2211 							if (size_prcw < 1) {
2212 								tccp->prcw[j] = 1;
2213 							} else {
2214 								tccp->prcw[j] = int_floorlog2(size_prcw);
2215 							}
2216 
2217 							if (size_prch < 1) {
2218 								tccp->prch[j] = 1;
2219 							} else {
2220 								tccp->prch[j] = int_floorlog2(size_prch);
2221 							}
2222 						}
2223 						p++;
2224 						/*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2225 					}	//end for
2226 				} else {
2227 					for (j = 0; j < tccp->numresolutions; j++) {
2228 						tccp->prcw[j] = 15;
2229 						tccp->prch[j] = 15;
2230 					}
2231 				}
2232 			}
2233 
2234 			dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2235 		}
2236 	}
2237 }
2238 
j2k_encode(opj_j2k_t * j2k,opj_cio_t * cio,opj_image_t * image,opj_codestream_info_t * cstr_info)2239 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2240 	int tileno, compno;
2241 	opj_cp_t *cp = NULL;
2242 
2243 	opj_tcd_t *tcd = NULL;	/* TCD component */
2244 
2245 	j2k->cio = cio;
2246 	j2k->image = image;
2247 
2248 	cp = j2k->cp;
2249 
2250 	/* INDEX >> */
2251 	j2k->cstr_info = cstr_info;
2252 	if (cstr_info) {
2253 		int compno;
2254 		cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2255 		cstr_info->image_w = image->x1 - image->x0;
2256 		cstr_info->image_h = image->y1 - image->y0;
2257 		cstr_info->prog = (&cp->tcps[0])->prg;
2258 		cstr_info->tw = cp->tw;
2259 		cstr_info->th = cp->th;
2260 		cstr_info->tile_x = cp->tdx;	/* new version parser */
2261 		cstr_info->tile_y = cp->tdy;	/* new version parser */
2262 		cstr_info->tile_Ox = cp->tx0;	/* new version parser */
2263 		cstr_info->tile_Oy = cp->ty0;	/* new version parser */
2264 		cstr_info->numcomps = image->numcomps;
2265 		cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2266 		cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2267 		for (compno=0; compno < image->numcomps; compno++) {
2268 			cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2269 		}
2270 		cstr_info->D_max = 0.0;		/* ADD Marcela */
2271 		cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2272 		cstr_info->maxmarknum = 100;
2273 		cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
2274 		cstr_info->marknum = 0;
2275 	}
2276 	/* << INDEX */
2277 
2278 	j2k_write_soc(j2k);
2279 	j2k_write_siz(j2k);
2280 	j2k_write_cod(j2k);
2281 	j2k_write_qcd(j2k);
2282 
2283 	if(cp->cinema){
2284 		for (compno = 1; compno < image->numcomps; compno++) {
2285 			j2k_write_coc(j2k, compno);
2286 			j2k_write_qcc(j2k, compno);
2287 		}
2288 	}
2289 
2290 	for (compno = 0; compno < image->numcomps; compno++) {
2291 		opj_tcp_t *tcp = &cp->tcps[0];
2292 		if (tcp->tccps[compno].roishift)
2293 			j2k_write_rgn(j2k, compno, 0);
2294 	}
2295 	if (cp->comment != NULL) {
2296 		j2k_write_com(j2k);
2297 	}
2298 
2299 	j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2300 	/* TLM Marker*/
2301 	if(cp->cinema){
2302 		j2k_write_tlm(j2k);
2303 		if (cp->cinema == CINEMA4K_24) {
2304 			j2k_write_poc(j2k);
2305 		}
2306 	}
2307 
2308 	/* uncomment only for testing JPSEC marker writing */
2309 	/* j2k_write_sec(j2k); */
2310 
2311 	/* INDEX >> */
2312 	if(cstr_info) {
2313 		cstr_info->main_head_end = cio_tell(cio) - 1;
2314 	}
2315 	/* << INDEX */
2316 	/**** Main Header ENDS here ***/
2317 
2318 	/* create the tile encoder */
2319 	tcd = tcd_create(j2k->cinfo);
2320 
2321 	/* encode each tile */
2322 	for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2323 		int pino;
2324 		int tilepartno=0;
2325 		/* UniPG>> */
2326 		int acc_pack_num = 0;
2327 		/* <<UniPG */
2328 
2329 
2330 		opj_tcp_t *tcp = &cp->tcps[tileno];
2331 		opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2332 
2333 		j2k->curtileno = tileno;
2334 		j2k->cur_tp_num = 0;
2335 		tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2336 		/* initialisation before tile encoding  */
2337 		if (tileno == 0) {
2338 			tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2339 		} else {
2340 			tcd_init_encode(tcd, image, cp, j2k->curtileno);
2341 		}
2342 
2343 		/* INDEX >> */
2344 		if(cstr_info) {
2345 			cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2346 		}
2347 		/* << INDEX */
2348 
2349 		for(pino = 0; pino <= tcp->numpocs; pino++) {
2350 			int tot_num_tp;
2351 			tcd->cur_pino=pino;
2352 
2353 			/*Get number of tile parts*/
2354 			tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2355 			tcd->tp_pos = cp->tp_pos;
2356 
2357 			for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2358 				j2k->tp_num = tilepartno;
2359 				/* INDEX >> */
2360 				if(cstr_info)
2361 					cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2362 					cio_tell(cio) + j2k->pos_correction;
2363 				/* << INDEX */
2364 				j2k_write_sot(j2k);
2365 
2366 				if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2367 					for (compno = 1; compno < image->numcomps; compno++) {
2368 						j2k_write_coc(j2k, compno);
2369 						j2k_write_qcc(j2k, compno);
2370 					}
2371 					if (cp->tcps[tileno].numpocs) {
2372 						j2k_write_poc(j2k);
2373 					}
2374 				}
2375 
2376 				/* INDEX >> */
2377 				if(cstr_info)
2378 					cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2379 					cio_tell(cio) + j2k->pos_correction + 1;
2380 				/* << INDEX */
2381 
2382 				j2k_write_sod(j2k, tcd);
2383 
2384 				/* INDEX >> */
2385 				if(cstr_info) {
2386 					cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2387 						cio_tell(cio) + j2k->pos_correction - 1;
2388 					cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2389 						acc_pack_num;
2390 					cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2391 						cstr_info->packno - acc_pack_num;
2392 					acc_pack_num = cstr_info->packno;
2393 				}
2394 				/* << INDEX */
2395 
2396 				j2k->cur_tp_num++;
2397 			}
2398 		}
2399 		if(cstr_info) {
2400 			cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2401 		}
2402 
2403 
2404 		/*
2405 		if (tile->PPT) { // BAD PPT !!!
2406 		FILE *PPT_file;
2407 		int i;
2408 		PPT_file=fopen("PPT","rb");
2409 		fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2410 		for (i=0;i<tile->len_ppt;i++) {
2411 		unsigned char elmt;
2412 		fread(&elmt, 1, 1, PPT_file);
2413 		fwrite(&elmt,1,1,f);
2414 		}
2415 		fclose(PPT_file);
2416 		unlink("PPT");
2417 		}
2418 		*/
2419 
2420 	}
2421 
2422 	/* destroy the tile encoder */
2423 	tcd_free_encode(tcd);
2424 	tcd_destroy(tcd);
2425 
2426 	opj_free(j2k->cur_totnum_tp);
2427 
2428 	j2k_write_eoc(j2k);
2429 
2430 	if(cstr_info) {
2431 		cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2432 		/* UniPG>> */
2433 		/* The following adjustment is done to adjust the codestream size */
2434 		/* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2435 		/* the first bunch of bytes is not in the codestream              */
2436 		cstr_info->codestream_size -= cstr_info->main_head_start;
2437 		/* <<UniPG */
2438 	}
2439 
2440 #ifdef USE_JPWL
2441 	/*
2442 	preparation of JPWL marker segments
2443 	*/
2444 	if(cp->epc_on) {
2445 
2446 		/* encode according to JPWL */
2447 		jpwl_encode(j2k, cio, image);
2448 
2449 	}
2450 #endif /* USE_JPWL */
2451 
2452 	return true;
2453 }
2454 
2455 
2456 
2457 
2458 
2459 
2460