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