]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/libopenjpeg/j2k.c
ENH: This time for real. Install is ok. STYLE: some minor stuff
[gdcm.git] / src / gdcmopenjpeg / libopenjpeg / j2k.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5  * Copyright (c) 2005, HervĂ© Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "opj_includes.h"
32
33 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
34 /*@{*/
35
36 /** @name Local static functions */
37 /*@{*/
38
39 /**
40 Write the SOC marker (Start Of Codestream)
41 @param j2k J2K handle
42 */
43 static void j2k_write_soc(opj_j2k_t *j2k);
44 /**
45 Read the SOC marker (Start of Codestream)
46 @param j2k J2K handle
47 */
48 static void j2k_read_soc(opj_j2k_t *j2k);
49 /**
50 Write the SIZ marker (image and tile size)
51 @param j2k J2K handle
52 */
53 static void j2k_write_siz(opj_j2k_t *j2k);
54 /**
55 Read the SIZ marker (image and tile size)
56 @param j2k J2K handle
57 */
58 static void j2k_read_siz(opj_j2k_t *j2k);
59 /**
60 Write the COM marker (comment)
61 @param j2k J2K handle
62 */
63 static void j2k_write_com(opj_j2k_t *j2k);
64 /**
65 Read the COM marker (comment)
66 @param j2k J2K handle
67 */
68 static void j2k_read_com(opj_j2k_t *j2k);
69 /**
70 Write the value concerning the specified component in the marker COD and COC
71 @param j2k J2K handle
72 @param compno Number of the component concerned by the information written
73 */
74 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
75 /**
76 Read the value concerning the specified component in the marker COD and COC
77 @param j2k J2K handle
78 @param compno Number of the component concerned by the information read
79 */
80 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
81 /**
82 Write the COD marker (coding style default)
83 @param j2k J2K handle
84 */
85 static void j2k_write_cod(opj_j2k_t *j2k);
86 /**
87 Read the COD marker (coding style default)
88 @param j2k J2K handle
89 */
90 static void j2k_read_cod(opj_j2k_t *j2k);
91 /**
92 Write the COC marker (coding style component)
93 @param j2k J2K handle
94 @param compno Number of the component concerned by the information written
95 */
96 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
97 /**
98 Read the COC marker (coding style component)
99 @param j2k J2K handle
100 */
101 static void j2k_read_coc(opj_j2k_t *j2k);
102 /**
103 Write the value concerning the specified component in the marker QCD and QCC
104 @param j2k J2K handle
105 @param compno Number of the component concerned by the information written
106 */
107 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
108 /**
109 Read the value concerning the specified component in the marker QCD and QCC
110 @param j2k J2K handle
111 @param compno Number of the component concern by the information read
112 @param len Length of the information in the QCX part of the marker QCD/QCC
113 */
114 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
115 /**
116 Write the QCD marker (quantization default)
117 @param j2k J2K handle
118 */
119 static void j2k_write_qcd(opj_j2k_t *j2k);
120 /**
121 Read the QCD marker (quantization default)
122 @param j2k J2K handle
123 */
124 static void j2k_read_qcd(opj_j2k_t *j2k);
125 /**
126 Write the QCC marker (quantization component)
127 @param j2k J2K handle
128 @param compno Number of the component concerned by the information written
129 */
130 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
131 /**
132 Read the QCC marker (quantization component)
133 @param j2k J2K handle
134 */
135 static void j2k_read_qcc(opj_j2k_t *j2k);
136 /**
137 Write the POC marker (progression order change)
138 @param j2k J2K handle
139 */
140 static void j2k_write_poc(opj_j2k_t *j2k);
141 /**
142 Read the POC marker (progression order change)
143 @param j2k J2K handle
144 */
145 static void j2k_read_poc(opj_j2k_t *j2k);
146 /**
147 Read the CRG marker (component registration)
148 @param j2k J2K handle
149 */
150 static void j2k_read_crg(opj_j2k_t *j2k);
151 /**
152 Read the TLM marker (tile-part lengths)
153 @param j2k J2K handle
154 */
155 static void j2k_read_tlm(opj_j2k_t *j2k);
156 /**
157 Read the PLM marker (packet length, main header)
158 @param j2k J2K handle
159 */
160 static void j2k_read_plm(opj_j2k_t *j2k);
161 /**
162 Read the PLT marker (packet length, tile-part header)
163 @param j2k J2K handle
164 */
165 static void j2k_read_plt(opj_j2k_t *j2k);
166 /**
167 Read the PPM marker (packet packet headers, main header)
168 @param j2k J2K handle
169 */
170 static void j2k_read_ppm(opj_j2k_t *j2k);
171 /**
172 Read the PPT marker (packet packet headers, tile-part header)
173 @param j2k J2K handle
174 */
175 static void j2k_read_ppt(opj_j2k_t *j2k);
176 /**
177 Write the SOT marker (start of tile-part)
178 @param j2k J2K handle
179 */
180 static void j2k_write_sot(opj_j2k_t *j2k);
181 /**
182 Read the SOT marker (start of tile-part)
183 @param j2k J2K handle
184 */
185 static void j2k_read_sot(opj_j2k_t *j2k);
186 /**
187 Write the SOD marker (start of data)
188 @param j2k J2K handle
189 @param tile_coder Pointer to a TCD handle
190 */
191 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
192 /**
193 Read the SOD marker (start of data)
194 @param j2k J2K handle
195 */
196 static void j2k_read_sod(opj_j2k_t *j2k);
197 /**
198 Write the RGN marker (region-of-interest)
199 @param j2k J2K handle
200 @param compno Number of the component concerned by the information written
201 @param tileno Number of the tile concerned by the information written
202 */
203 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
204 /**
205 Read the RGN marker (region-of-interest)
206 @param j2k J2K handle
207 */
208 static void j2k_read_rgn(opj_j2k_t *j2k);
209 /**
210 Write the EOC marker (end of codestream)
211 @param j2k J2K handle
212 */
213 static void j2k_write_eoc(opj_j2k_t *j2k);
214 /**
215 Read the EOC marker (end of codestream)
216 @param j2k J2K handle
217 */
218 static void j2k_read_eoc(opj_j2k_t *j2k);
219 /**
220 Read an unknown marker
221 @param j2k J2K handle
222 */
223 static void j2k_read_unk(opj_j2k_t *j2k);
224
225 /*@}*/
226
227 /*@}*/
228
229 /* ----------------------------------------------------------------------- */
230
231 void j2k_dump_image(FILE *fd, opj_image_t * img) {
232   int compno;
233   fprintf(fd, "image {\n");
234   fprintf(fd, "  x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
235   fprintf(fd, "  numcomps=%d\n", img->numcomps);
236   for (compno = 0; compno < img->numcomps; compno++) {
237     opj_image_comp_t *comp = &img->comps[compno];
238     fprintf(fd, "  comp %d {\n", compno);
239     fprintf(fd, "    dx=%d, dy=%d\n", comp->dx, comp->dy);
240     fprintf(fd, "    prec=%d\n", comp->prec);
241     fprintf(fd, "    sgnd=%d\n", comp->sgnd);
242     fprintf(fd, "  }\n");
243   }
244   fprintf(fd, "}\n");
245 }
246
247 void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) {
248   int tileno, compno, layno, bandno, resno, numbands;
249   fprintf(fd, "coding parameters {\n");
250   fprintf(fd, "  tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
251   fprintf(fd, "  tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
252   fprintf(fd, "  tw=%d, th=%d\n", cp->tw, cp->th);
253   for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
254     opj_tcp_t *tcp = &cp->tcps[tileno];
255     fprintf(fd, "  tile %d {\n", tileno);
256     fprintf(fd, "    csty=%x\n", tcp->csty);
257     fprintf(fd, "    prg=%d\n", tcp->prg);
258     fprintf(fd, "    numlayers=%d\n", tcp->numlayers);
259     fprintf(fd, "    mct=%d\n", tcp->mct);
260     fprintf(fd, "    rates=");
261     for (layno = 0; layno < tcp->numlayers; layno++) {
262       fprintf(fd, "%d ", tcp->rates[layno]);
263     }
264     fprintf(fd, "\n");
265     for (compno = 0; compno < img->numcomps; compno++) {
266       opj_tccp_t *tccp = &tcp->tccps[compno];
267       fprintf(fd, "    comp %d {\n", compno);
268       fprintf(fd, "      csty=%x\n", tccp->csty);
269       fprintf(fd, "      numresolutions=%d\n", tccp->numresolutions);
270       fprintf(fd, "      cblkw=%d\n", tccp->cblkw);
271       fprintf(fd, "      cblkh=%d\n", tccp->cblkh);
272       fprintf(fd, "      cblksty=%x\n", tccp->cblksty);
273       fprintf(fd, "      qmfbid=%d\n", tccp->qmfbid);
274       fprintf(fd, "      qntsty=%d\n", tccp->qntsty);
275       fprintf(fd, "      numgbits=%d\n", tccp->numgbits);
276       fprintf(fd, "      roishift=%d\n", tccp->roishift);
277       fprintf(fd, "      stepsizes=");
278       numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
279       for (bandno = 0; bandno < numbands; bandno++) {
280         fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
281           tccp->stepsizes[bandno].expn);
282       }
283       fprintf(fd, "\n");
284       
285       if (tccp->csty & J2K_CCP_CSTY_PRT) {
286         fprintf(fd, "      prcw=");
287         for (resno = 0; resno < tccp->numresolutions; resno++) {
288           fprintf(fd, "%d ", tccp->prcw[resno]);
289         }
290         fprintf(fd, "\n");
291         fprintf(fd, "      prch=");
292         for (resno = 0; resno < tccp->numresolutions; resno++) {
293           fprintf(fd, "%d ", tccp->prch[resno]);
294         }
295         fprintf(fd, "\n");
296       }
297       fprintf(fd, "    }\n");
298     }
299     fprintf(fd, "  }\n");
300   }
301   fprintf(fd, "}\n");
302 }
303
304 /* ----------------------------------------------------------------------- */
305
306 static void j2k_write_soc(opj_j2k_t *j2k) {
307   opj_cio_t *cio = j2k->cio;
308   cio_write(cio, J2K_MS_SOC, 2);
309 }
310
311 static void j2k_read_soc(opj_j2k_t *j2k) {
312   j2k->state = J2K_STATE_MHSIZ;
313 }
314
315 static void j2k_write_siz(opj_j2k_t *j2k) {
316   int i;
317   int lenp, len;
318
319   opj_cio_t *cio = j2k->cio;
320   opj_image_t *image = j2k->image;
321   opj_cp_t *cp = j2k->cp;
322   
323   cio_write(cio, J2K_MS_SIZ, 2);  /* SIZ */
324   lenp = cio_tell(cio);
325   cio_skip(cio, 2);
326   cio_write(cio, 0, 2);      /* Rsiz (capabilities) */
327   cio_write(cio, image->x1, 4);  /* Xsiz */
328   cio_write(cio, image->y1, 4);  /* Ysiz */
329   cio_write(cio, image->x0, 4);  /* X0siz */
330   cio_write(cio, image->y0, 4);  /* Y0siz */
331   cio_write(cio, cp->tdx, 4);    /* XTsiz */
332   cio_write(cio, cp->tdy, 4);    /* YTsiz */
333   cio_write(cio, cp->tx0, 4);    /* XT0siz */
334   cio_write(cio, cp->ty0, 4);    /* YT0siz */
335   cio_write(cio, image->numcomps, 2);  /* Csiz */
336   for (i = 0; i < image->numcomps; i++) {
337     cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1);  /* Ssiz_i */
338     cio_write(cio, image->comps[i].dx, 1);  /* XRsiz_i */
339     cio_write(cio, image->comps[i].dy, 1);  /* YRsiz_i */
340   }
341   len = cio_tell(cio) - lenp;
342   cio_seek(cio, lenp);
343   cio_write(cio, len, 2);    /* Lsiz */
344   cio_seek(cio, lenp + len);
345 }
346
347 static void j2k_read_siz(opj_j2k_t *j2k) {
348   int len, i;
349   
350   opj_cio_t *cio = j2k->cio;
351   opj_image_t *image = j2k->image;
352   opj_cp_t *cp = j2k->cp;
353   
354   len = cio_read(cio, 2);      /* Lsiz */
355   cio_read(cio, 2);        /* Rsiz (capabilities) */
356   image->x1 = cio_read(cio, 4);  /* Xsiz */
357   image->y1 = cio_read(cio, 4);  /* Ysiz */
358   image->x0 = cio_read(cio, 4);  /* X0siz */
359   image->y0 = cio_read(cio, 4);  /* Y0siz */
360   cp->tdx = cio_read(cio, 4);    /* XTsiz */
361   cp->tdy = cio_read(cio, 4);    /* YTsiz */
362   cp->tx0 = cio_read(cio, 4);    /* XT0siz */
363   cp->ty0 = cio_read(cio, 4);    /* YT0siz */
364   
365   image->numcomps = cio_read(cio, 2);  /* Csiz */
366   image->comps = (opj_image_comp_t *) opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
367   for (i = 0; i < image->numcomps; i++) {
368     int tmp, w, h;
369     tmp = cio_read(cio, 1);    /* Ssiz_i */
370     image->comps[i].prec = (tmp & 0x7f) + 1;
371     image->comps[i].sgnd = tmp >> 7;
372     image->comps[i].dx = cio_read(cio, 1);  /* XRsiz_i */
373     image->comps[i].dy = cio_read(cio, 1);  /* YRsiz_i */
374     
375     /* TODO: unused ? */
376     w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
377     h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
378
379     image->comps[i].resno_decoded = 0;  /* number of resolution decoded */
380     image->comps[i].factor = 0;      /* reducing factor per component */
381   }
382   
383   cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
384   cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
385   cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
386   cp->tileno = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
387   cp->tileno_size = 0;
388   
389   for (i = 0; i < cp->tw * cp->th; i++) {
390     cp->tcps[i].POC = 0;
391     cp->tcps[i].numpocs = 0;
392     cp->tcps[i].first = 1;
393   }
394   
395   /* Initialization for PPM marker */
396   cp->ppm = 0;
397   cp->ppm_data = NULL;
398   cp->ppm_data_first = NULL;
399   cp->ppm_previous = 0;
400   cp->ppm_store = 0;
401   
402   j2k->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
403   for (i = 0; i < cp->tw * cp->th; i++) {
404     cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
405   }
406   j2k->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * sizeof(unsigned char *));
407   j2k->tile_len = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
408   j2k->state = J2K_STATE_MH;
409 }
410
411 static void j2k_write_com(opj_j2k_t *j2k) {
412   unsigned int i;
413   int lenp, len;
414
415   if(j2k->cp->comment) {
416     opj_cio_t *cio = j2k->cio;
417     char *comment = j2k->cp->comment;
418
419     cio_write(cio, J2K_MS_COM, 2);
420     lenp = cio_tell(cio);
421     cio_skip(cio, 2);
422     cio_write(cio, 0, 2);
423     for (i = 0; i < strlen(comment); i++) {
424       cio_write(cio, comment[i], 1);
425     }
426     len = cio_tell(cio) - lenp;
427     cio_seek(cio, lenp);
428     cio_write(cio, len, 2);
429     cio_seek(cio, lenp + len);
430   }
431 }
432
433 static void j2k_read_com(opj_j2k_t *j2k) {
434   int len;
435   
436   opj_cio_t *cio = j2k->cio;
437
438   len = cio_read(cio, 2);
439   cio_skip(cio, len - 2);  
440 }
441
442 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
443   int i;
444
445   opj_cp_t *cp = j2k->cp;
446   opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
447   opj_tccp_t *tccp = &tcp->tccps[compno];
448   opj_cio_t *cio = j2k->cio;
449   
450   cio_write(cio, tccp->numresolutions - 1, 1);  /* SPcox (D) */
451   cio_write(cio, tccp->cblkw - 2, 1);        /* SPcox (E) */
452   cio_write(cio, tccp->cblkh - 2, 1);        /* SPcox (F) */
453   cio_write(cio, tccp->cblksty, 1);        /* SPcox (G) */
454   cio_write(cio, tccp->qmfbid, 1);        /* SPcox (H) */
455   
456   if (tccp->csty & J2K_CCP_CSTY_PRT) {
457     for (i = 0; i < tccp->numresolutions; i++) {
458       cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1);  /* SPcox (I_i) */
459     }
460   }
461 }
462
463 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
464   int i;
465
466   opj_cp_t *cp = j2k->cp;
467   opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
468   opj_tccp_t *tccp = &tcp->tccps[compno];
469   opj_cio_t *cio = j2k->cio;
470
471   tccp->numresolutions = cio_read(cio, 1) + 1;  /* SPcox (D) */
472
473   /* check the reduce value */
474   cp->reduce = int_min((tccp->numresolutions)-1, cp->reduce);
475   tccp->cblkw = cio_read(cio, 1) + 2;  /* SPcox (E) */
476   tccp->cblkh = cio_read(cio, 1) + 2;  /* SPcox (F) */
477   tccp->cblksty = cio_read(cio, 1);  /* SPcox (G) */
478   tccp->qmfbid = cio_read(cio, 1);  /* SPcox (H) */
479   if (tccp->csty & J2K_CP_CSTY_PRT) {
480     for (i = 0; i < tccp->numresolutions; i++) {
481       int tmp = cio_read(cio, 1);  /* SPcox (I_i) */
482       tccp->prcw[i] = tmp & 0xf;
483       tccp->prch[i] = tmp >> 4;
484     }
485   }
486 }
487
488 static void j2k_write_cod(opj_j2k_t *j2k) {
489   opj_cp_t *cp = NULL;
490   opj_tcp_t *tcp = NULL;
491   int lenp, len;
492
493   opj_cio_t *cio = j2k->cio;
494   
495   cio_write(cio, J2K_MS_COD, 2);  /* COD */
496   
497   lenp = cio_tell(cio);
498   cio_skip(cio, 2);
499   
500   cp = j2k->cp;
501   tcp = &cp->tcps[j2k->curtileno];
502
503   cio_write(cio, tcp->csty, 1);    /* Scod */
504   cio_write(cio, tcp->prg, 1);    /* SGcod (A) */
505   cio_write(cio, tcp->numlayers, 2);  /* SGcod (B) */
506   cio_write(cio, tcp->mct, 1);    /* SGcod (C) */
507   
508   j2k_write_cox(j2k, 0);
509   len = cio_tell(cio) - lenp;
510   cio_seek(cio, lenp);
511   cio_write(cio, len, 2);    /* Lcod */
512   cio_seek(cio, lenp + len);
513 }
514
515 static void j2k_read_cod(opj_j2k_t *j2k) {
516   int len, i, pos;
517   
518   opj_cio_t *cio = j2k->cio;
519   opj_cp_t *cp = j2k->cp;
520   opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
521   opj_image_t *image = j2k->image;
522   
523   len = cio_read(cio, 2);        /* Lcod */
524   tcp->csty = cio_read(cio, 1);    /* Scod */
525   tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* SGcod (A) */
526   tcp->numlayers = cio_read(cio, 2);  /* SGcod (B) */
527   tcp->mct = cio_read(cio, 1);    /* SGcod (C) */
528   
529   pos = cio_tell(cio);
530   for (i = 0; i < image->numcomps; i++) {
531     tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
532     cio_seek(cio, pos);
533     j2k_read_cox(j2k, i);
534   }
535 }
536
537 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
538   int lenp, len;
539
540   opj_cp_t *cp = j2k->cp;
541   opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
542   opj_image_t *image = j2k->image;
543   opj_cio_t *cio = j2k->cio;
544   
545   cio_write(cio, J2K_MS_COC, 2);  /* COC */
546   lenp = cio_tell(cio);
547   cio_skip(cio, 2);
548   cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2);  /* Ccoc */
549   cio_write(cio, tcp->tccps[compno].csty, 1);  /* Scoc */
550   j2k_write_cox(j2k, compno);
551   len = cio_tell(cio) - lenp;
552   cio_seek(cio, lenp);
553   cio_write(cio, len, 2);      /* Lcoc */
554   cio_seek(cio, lenp + len);
555 }
556
557 static void j2k_read_coc(opj_j2k_t *j2k) {
558   int len, compno;
559
560   opj_cp_t *cp = j2k->cp;
561   opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
562   opj_image_t *image = j2k->image;
563   opj_cio_t *cio = j2k->cio;
564   
565   len = cio_read(cio, 2);    /* Lcoc */
566   compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2);  /* Ccoc */
567   tcp->tccps[compno].csty = cio_read(cio, 1);  /* Scoc */
568   j2k_read_cox(j2k, compno);
569 }
570
571 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
572   int bandno, numbands;
573   int expn, mant;
574   
575   opj_cp_t *cp = j2k->cp;
576   opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
577   opj_tccp_t *tccp = &tcp->tccps[compno];
578   opj_cio_t *cio = j2k->cio;
579   
580   cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);  /* Sqcx */
581   numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
582   
583   for (bandno = 0; bandno < numbands; bandno++) {
584     expn = tccp->stepsizes[bandno].expn;
585     mant = tccp->stepsizes[bandno].mant;
586     
587     if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
588       cio_write(cio, expn << 3, 1);  /* SPqcx_i */
589     } else {
590       cio_write(cio, (expn << 11) + mant, 2);  /* SPqcx_i */
591     }
592   }
593 }
594
595 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
596   int tmp;
597   int bandno, numbands;
598
599   opj_cp_t *cp = j2k->cp;
600   opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
601   opj_tccp_t *tccp = &tcp->tccps[compno];
602   opj_cio_t *cio = j2k->cio;
603
604   tmp = cio_read(cio, 1);    /* Sqcx */
605   tccp->qntsty = tmp & 0x1f;
606   tccp->numgbits = tmp >> 5;
607   numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 
608     1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
609   for (bandno = 0; bandno < numbands; bandno++) {
610     int expn, mant;
611     if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
612       expn = cio_read(cio, 1) >> 3;  /* SPqcx_i */
613       mant = 0;
614     } else {
615       tmp = cio_read(cio, 2);  /* SPqcx_i */
616       expn = tmp >> 11;
617       mant = tmp & 0x7ff;
618     }
619     tccp->stepsizes[bandno].expn = expn;
620     tccp->stepsizes[bandno].mant = mant;
621   }
622   
623   /* Add Antonin : if scalar_derived -> compute other stepsizes */
624   if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
625     for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
626       tccp->stepsizes[bandno].expn = 
627         ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ? 
628           (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
629       tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
630     }
631   }
632   /* ddA */
633 }
634
635 static void j2k_write_qcd(opj_j2k_t *j2k) {
636   int lenp, len;
637
638   opj_cio_t *cio = j2k->cio;
639   
640   cio_write(cio, J2K_MS_QCD, 2);  /* QCD */
641   lenp = cio_tell(cio);
642   cio_skip(cio, 2);
643   j2k_write_qcx(j2k, 0);
644   len = cio_tell(cio) - lenp;
645   cio_seek(cio, lenp);
646   cio_write(cio, len, 2);      /* Lqcd */
647   cio_seek(cio, lenp + len);
648 }
649
650 static void j2k_read_qcd(opj_j2k_t *j2k) {
651   int len, i, pos;
652
653   opj_cio_t *cio = j2k->cio;
654   opj_image_t *image = j2k->image;
655   
656   len = cio_read(cio, 2);    /* Lqcd */
657   pos = cio_tell(cio);
658   for (i = 0; i < image->numcomps; i++) {
659     cio_seek(cio, pos);
660     j2k_read_qcx(j2k, i, len - 2);
661   }
662 }
663
664 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
665   int lenp, len;
666
667   opj_cio_t *cio = j2k->cio;
668   
669   cio_write(cio, J2K_MS_QCC, 2);  /* QCC */
670   lenp = cio_tell(cio);
671   cio_skip(cio, 2);
672   cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2);  /* Cqcc */
673   j2k_write_qcx(j2k, compno);
674   len = cio_tell(cio) - lenp;
675   cio_seek(cio, lenp);
676   cio_write(cio, len, 2);      /* Lqcc */
677   cio_seek(cio, lenp + len);
678 }
679
680 static void j2k_read_qcc(opj_j2k_t *j2k) {
681   int len, compno;
682   int numcomp = j2k->image->numcomps;
683   opj_cio_t *cio = j2k->cio;
684   
685   len = cio_read(cio, 2);  /* Lqcc */
686   compno = cio_read(cio, numcomp <= 256 ? 1 : 2);  /* Cqcc */
687   j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
688 }
689
690 static void j2k_write_poc(opj_j2k_t *j2k) {
691   int len, numpchgs, i;
692
693   int numcomps = j2k->image->numcomps;
694   
695   opj_cp_t *cp = j2k->cp;
696   opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
697   opj_tccp_t *tccp = &tcp->tccps[0];
698   opj_cio_t *cio = j2k->cio;
699
700   numpchgs = tcp->numpocs;
701   cio_write(cio, J2K_MS_POC, 2);  /* POC  */
702   len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
703   cio_write(cio, len, 2);    /* Lpoc */
704   for (i = 0; i < numpchgs; i++) {
705     opj_poc_t *poc = &tcp->pocs[i];
706     cio_write(cio, poc->resno0, 1);  /* RSpoc_i */
707     cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));  /* CSpoc_i */
708     cio_write(cio, poc->layno1, 2);  /* LYEpoc_i */
709     poc->layno1 = int_min(poc->layno1, tcp->numlayers);
710     cio_write(cio, poc->resno1, 1);  /* REpoc_i */
711     poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
712     cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));  /* CEpoc_i */
713     poc->compno1 = int_min(poc->compno1, numcomps);
714     cio_write(cio, poc->prg, 1);  /* Ppoc_i */
715   }
716 }
717
718 static void j2k_read_poc(opj_j2k_t *j2k) {
719   int len, numpchgs, i, old_poc;
720
721   int numcomps = j2k->image->numcomps;
722   
723   opj_cp_t *cp = j2k->cp;
724   opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
725   opj_tccp_t *tccp = &tcp->tccps[0];
726   opj_cio_t *cio = j2k->cio;
727   
728   old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
729   tcp->POC = 1;
730   len = cio_read(cio, 2);    /* Lpoc */
731   numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
732   
733   for (i = old_poc; i < numpchgs + old_poc; i++) {
734     opj_poc_t *poc;
735     poc = &tcp->pocs[i];
736     poc->resno0 = cio_read(cio, 1);  /* RSpoc_i */
737     poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
738     poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers);  /* LYEpoc_i */
739     poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolutions);  /* REpoc_i */
740     poc->compno1 = int_min(
741       cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);  /* CEpoc_i */
742     poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);  /* Ppoc_i */
743   }
744   
745   tcp->numpocs = numpchgs + old_poc - 1;
746 }
747
748 static void j2k_read_crg(opj_j2k_t *j2k) {
749   int len, i, Xcrg_i, Ycrg_i;
750   
751   opj_cio_t *cio = j2k->cio;
752   int numcomps = j2k->image->numcomps;
753   
754   len = cio_read(cio, 2);      /* Lcrg */
755   for (i = 0; i < numcomps; i++) {
756     Xcrg_i = cio_read(cio, 2);  /* Xcrg_i */
757     Ycrg_i = cio_read(cio, 2);  /* Ycrg_i */
758   }
759 }
760
761 static void j2k_read_tlm(opj_j2k_t *j2k) {
762   int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
763   long int Ttlm_i, Ptlm_i;
764
765   opj_cio_t *cio = j2k->cio;
766   
767   len = cio_read(cio, 2);    /* Ltlm */
768   Ztlm = cio_read(cio, 1);  /* Ztlm */
769   Stlm = cio_read(cio, 1);  /* Stlm */
770   ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
771   SP = (Stlm >> 6) & 0x01;
772   tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
773   for (i = 0; i < tile_tlm; i++) {
774     Ttlm_i = cio_read(cio, ST);  /* Ttlm_i */
775     Ptlm_i = cio_read(cio, SP ? 4 : 2);  /* Ptlm_i */
776   }
777 }
778
779 static void j2k_read_plm(opj_j2k_t *j2k) {
780   int len, i, Zplm, Nplm, add, packet_len = 0;
781   
782   opj_cio_t *cio = j2k->cio;
783
784   len = cio_read(cio, 2);    /* Lplm */
785   Zplm = cio_read(cio, 1);  /* Zplm */
786   len -= 3;
787   while (len > 0) {
788     Nplm = cio_read(cio, 4);    /* Nplm */
789     len -= 4;
790     for (i = Nplm; i > 0; i--) {
791       add = cio_read(cio, 1);
792       len--;
793       packet_len = (packet_len << 7) + add;  /* Iplm_ij */
794       if ((add & 0x80) == 0) {
795         /* New packet */
796         packet_len = 0;
797       }
798       if (len <= 0)
799         break;
800     }
801   }
802 }
803
804 static void j2k_read_plt(opj_j2k_t *j2k) {
805   int len, i, Zplt, packet_len = 0, add;
806   
807   opj_cio_t *cio = j2k->cio;
808   
809   len = cio_read(cio, 2);    /* Lplt */
810   Zplt = cio_read(cio, 1);  /* Zplt */
811   for (i = len - 3; i > 0; i--) {
812     add = cio_read(cio, 1);
813     packet_len = (packet_len << 7) + add;  /* Iplt_i */
814     if ((add & 0x80) == 0) {
815       /* New packet */
816       packet_len = 0;
817     }
818   }
819 }
820
821 static void j2k_read_ppm(opj_j2k_t *j2k) {
822   int len, Z_ppm, i, j;
823   int N_ppm;
824
825   opj_cp_t *cp = j2k->cp;
826   opj_cio_t *cio = j2k->cio;
827   
828   len = cio_read(cio, 2);
829   cp->ppm = 1;
830   
831   Z_ppm = cio_read(cio, 1);  /* Z_ppm */
832   len -= 3;
833   while (len > 0) {
834     if (cp->ppm_previous == 0) {
835       N_ppm = cio_read(cio, 4);  /* N_ppm */
836       len -= 4;
837     } else {
838       N_ppm = cp->ppm_previous;
839     }
840     j = cp->ppm_store;
841     if (Z_ppm == 0) {  /* First PPM marker */
842       cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
843       cp->ppm_data_first = cp->ppm_data;
844       cp->ppm_len = N_ppm;
845     } else {      /* NON-first PPM marker */
846       cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +  cp->ppm_store) * sizeof(unsigned char));
847       cp->ppm_data_first = cp->ppm_data;
848       cp->ppm_len = N_ppm + cp->ppm_store;
849     }
850     for (i = N_ppm; i > 0; i--) {  /* Read packet header */
851       cp->ppm_data[j] = cio_read(cio, 1);
852       j++;
853       len--;
854       if (len == 0)
855         break;      /* Case of non-finished packet header in present marker but finished in next one */
856     }
857     cp->ppm_previous = i - 1;
858     cp->ppm_store = j;
859   }
860 }
861
862 static void j2k_read_ppt(opj_j2k_t *j2k) {
863   int len, Z_ppt, i, j = 0;
864
865   opj_cp_t *cp = j2k->cp;
866   opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
867   opj_cio_t *cio = j2k->cio;
868
869   len = cio_read(cio, 2);
870   Z_ppt = cio_read(cio, 1);
871   tcp->ppt = 1;
872   if (Z_ppt == 0) {    /* First PPT marker */
873     tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
874     tcp->ppt_data_first = tcp->ppt_data;
875     tcp->ppt_store = 0;
876     tcp->ppt_len = len - 3;
877   } else {      /* NON-first PPT marker */
878     tcp->ppt_data =  (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
879     tcp->ppt_data_first = tcp->ppt_data;
880     tcp->ppt_len = len - 3 + tcp->ppt_store;
881   }
882   j = tcp->ppt_store;
883   for (i = len - 3; i > 0; i--) {
884     tcp->ppt_data[j] = cio_read(cio, 1);
885     j++;
886   }
887   tcp->ppt_store = j;
888 }
889
890 static void j2k_write_sot(opj_j2k_t *j2k) {
891   int lenp, len;
892
893   opj_cio_t *cio = j2k->cio;
894
895   j2k->sot_start = cio_tell(cio);
896   cio_write(cio, J2K_MS_SOT, 2);    /* SOT */
897   lenp = cio_tell(cio);
898   cio_skip(cio, 2);          /* Lsot (further) */
899   cio_write(cio, j2k->curtileno, 2);  /* Isot */
900   cio_skip(cio, 4);          /* Psot (further in j2k_write_sod) */
901   cio_write(cio, 0, 1);        /* TPsot */
902   cio_write(cio, 1, 1);        /* TNsot */
903   len = cio_tell(cio) - lenp;
904   cio_seek(cio, lenp);
905   cio_write(cio, len, 2);        /* Lsot */
906   cio_seek(cio, lenp + len);
907 }
908
909 static void j2k_read_sot(opj_j2k_t *j2k) {
910   int len, tileno, totlen, partno, numparts, i;
911   opj_tcp_t *tcp = NULL;
912   char status = 0;
913
914   opj_cp_t *cp = j2k->cp;
915   opj_cio_t *cio = j2k->cio;
916   
917   len = cio_read(cio, 2);
918   tileno = cio_read(cio, 2);
919   
920   if (cp->tileno_size == 0) {
921     cp->tileno[cp->tileno_size] = tileno;
922     cp->tileno_size++;
923   } else {
924     i = 0;
925     while (i < cp->tileno_size && status == 0) {
926       status = cp->tileno[i] == tileno ? 1 : 0;
927       i++;
928     }
929     if (status == 0) {
930       cp->tileno[cp->tileno_size] = tileno;
931       cp->tileno_size++;
932     }
933   }
934   
935   totlen = cio_read(cio, 4);
936   if (!totlen)
937     totlen = cio_numbytesleft(cio) + 8;
938   
939   partno = cio_read(cio, 1);
940   numparts = cio_read(cio, 1);
941   
942   j2k->curtileno = tileno;
943   j2k->eot = cio_getbp(cio) - 12 + totlen;
944   j2k->state = J2K_STATE_TPH;
945   tcp = &cp->tcps[j2k->curtileno];
946   
947   if (tcp->first == 1) {
948     
949     /* Initialization PPT */
950     opj_tccp_t *tmp = tcp->tccps;
951     memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
952     tcp->ppt = 0;
953     tcp->ppt_data = NULL;
954     tcp->ppt_data_first = NULL;
955     tcp->tccps = tmp;
956
957     for (i = 0; i < j2k->image->numcomps; i++) {
958       tcp->tccps[i] = j2k->default_tcp->tccps[i];
959     }
960     cp->tcps[j2k->curtileno].first = 0;
961   }
962 }
963
964 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
965   int l, layno;
966   int totlen;
967   opj_tcp_t *tcp = NULL;
968   opj_image_info_t *image_info = NULL;
969   
970   opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;  /* cast is needed because of conflicts in header inclusions */
971   opj_cp_t *cp = j2k->cp;
972   opj_cio_t *cio = j2k->cio;
973   
974   cio_write(cio, J2K_MS_SOD, 2);
975   if (j2k->curtileno == 0) {
976     j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
977   }
978   
979   /* INDEX >> */
980   image_info = j2k->image_info;
981   if (image_info && image_info->index_on) {
982     image_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
983   }
984   /* << INDEX */
985   
986   tcp = &cp->tcps[j2k->curtileno];
987   for (layno = 0; layno < tcp->numlayers; layno++) {
988     tcp->rates[layno] -= tcp->rates[layno] ? (j2k->sod_start / (cp->th * cp->tw)) : 0;
989   }
990   if(image_info) {
991     image_info->num = 0;
992   }
993   
994   l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, image_info);
995   
996   /* Writing Psot in SOT marker */
997   totlen = cio_tell(cio) + l - j2k->sot_start;
998   cio_seek(cio, j2k->sot_start + 6);
999   cio_write(cio, totlen, 4);
1000   cio_seek(cio, j2k->sot_start + totlen);
1001 }
1002
1003 static void j2k_read_sod(opj_j2k_t *j2k) {
1004   int len, truncate = 0, i;
1005   unsigned char *data = NULL, *data_ptr = NULL;
1006
1007   opj_cio_t *cio = j2k->cio;
1008   int curtileno = j2k->curtileno;
1009   
1010   len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1011   
1012   if (len == cio_numbytesleft(cio) + 1) {
1013     truncate = 1;    /* Case of a truncate codestream */
1014   }
1015   
1016   data = (unsigned char *) opj_malloc((j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1017
1018   for (i = 0; i < j2k->tile_len[curtileno]; i++) {
1019     data[i] = j2k->tile_data[curtileno][i];
1020   }
1021
1022   data_ptr = data + j2k->tile_len[curtileno];
1023   for (i = 0; i < len; i++) {
1024     data_ptr[i] = cio_read(cio, 1);
1025   }
1026   
1027   j2k->tile_len[curtileno] += len;
1028   opj_free(j2k->tile_data[curtileno]);
1029   j2k->tile_data[curtileno] = data;
1030   
1031   if (!truncate) {
1032     j2k->state = J2K_STATE_TPHSOT;
1033   } else {
1034     j2k->state = J2K_STATE_NEOC;  /* RAJOUTE !! */
1035   }
1036 }
1037
1038 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1039   
1040   opj_cp_t *cp = j2k->cp;
1041   opj_tcp_t *tcp = &cp->tcps[tileno];
1042   opj_cio_t *cio = j2k->cio;
1043   int numcomps = j2k->image->numcomps;
1044   
1045   cio_write(cio, J2K_MS_RGN, 2);            /* RGN  */
1046   cio_write(cio, numcomps <= 256 ? 5 : 6, 2);      /* Lrgn */
1047   cio_write(cio, compno, numcomps <= 256 ? 1 : 2);  /* Crgn */
1048   cio_write(cio, 0, 1);                /* Srgn */
1049   cio_write(cio, tcp->tccps[compno].roishift, 1);    /* SPrgn */
1050 }
1051
1052 static void j2k_read_rgn(opj_j2k_t *j2k) {
1053   int len, compno, roisty;
1054
1055   opj_cp_t *cp = j2k->cp;
1056   opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1057   opj_cio_t *cio = j2k->cio;
1058   int numcomps = j2k->image->numcomps;
1059
1060   len = cio_read(cio, 2);                    /* Lrgn */
1061   compno = cio_read(cio, numcomps <= 256 ? 1 : 2);      /* Crgn */
1062   roisty = cio_read(cio, 1);                  /* Srgn */
1063   tcp->tccps[compno].roishift = cio_read(cio, 1);        /* SPrgn */
1064 }
1065
1066 static void j2k_write_eoc(opj_j2k_t *j2k) {
1067   opj_cio_t *cio = j2k->cio;
1068   /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1069   cio_write(cio, J2K_MS_EOC, 2);
1070 }
1071
1072 static void j2k_read_eoc(opj_j2k_t *j2k) {
1073   int i, tileno;
1074
1075 #ifndef NO_PACKETS_DECODING  
1076   opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1077   tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1078   for (i = 0; i < j2k->cp->tileno_size; i++) {
1079     tileno = j2k->cp->tileno[i];
1080     tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno);
1081     opj_free(j2k->tile_data[tileno]);
1082     j2k->tile_data[tileno] = NULL;
1083   }
1084   tcd_free_decode(tcd);
1085   tcd_destroy(tcd);
1086 #else 
1087   for (i = 0; i < j2k->cp->tileno_size; i++) {
1088     tileno = j2k->cp->tileno[i];
1089     opj_free(j2k->tile_data[tileno]);
1090     j2k->tile_data[tileno] = NULL;
1091   }
1092 #endif
1093   
1094   j2k->state = J2K_STATE_MT;
1095 }
1096
1097 static void j2k_read_unk(opj_j2k_t *j2k) {
1098   opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1099 }
1100
1101 typedef struct opj_dec_mstabent {
1102   /** marker value */
1103   int id;
1104   /** value of the state when the marker can appear */
1105   int states;
1106   /** action linked to the marker */
1107   void (*handler) (opj_j2k_t *j2k);
1108 } opj_dec_mstabent_t;
1109
1110 opj_dec_mstabent_t j2k_dec_mstab[] = {
1111   {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1112   {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1113   {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1114   {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1115   {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1116   {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1117   {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1118   {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1119   {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1120   {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1121   {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1122   {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1123   {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1124   {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1125   {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1126   {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1127   {J2K_MS_SOP, 0, 0},
1128   {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1129   {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1130   {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1131 };
1132
1133 /**
1134 Read the lookup table containing all the marker, status and action
1135 @param id Marker value
1136 */
1137 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1138   opj_dec_mstabent_t *e;
1139   for (e = j2k_dec_mstab; e->id != 0; e++) {
1140     if (e->id == id) {
1141       break;
1142     }
1143   }
1144   return e;
1145 }
1146
1147 /* ----------------------------------------------------------------------- */
1148 /* J2K / JPT decoder interface                                             */
1149 /* ----------------------------------------------------------------------- */
1150
1151 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1152   opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1153   if(j2k) {
1154     j2k->cinfo = cinfo;
1155     j2k->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1156     if(!j2k->default_tcp) {
1157       opj_free(j2k);
1158       return NULL;
1159     }
1160   }
1161   return j2k;
1162 }
1163
1164 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1165   int i = 0;
1166
1167   if(j2k->tile_len != NULL) {
1168     opj_free(j2k->tile_len);
1169   }
1170   if(j2k->tile_data != NULL) {
1171     opj_free(j2k->tile_data);
1172   }
1173   if(j2k->default_tcp != NULL) {
1174     opj_tcp_t *default_tcp = j2k->default_tcp;
1175     if(default_tcp->ppt_data_first != NULL) {
1176       opj_free(default_tcp->ppt_data_first);
1177     }
1178     if(j2k->default_tcp->tccps != NULL) {
1179       opj_free(j2k->default_tcp->tccps);
1180     }
1181     opj_free(j2k->default_tcp);
1182   }
1183   if(j2k->cp != NULL) {
1184     opj_cp_t *cp = j2k->cp;
1185     if(cp->tcps != NULL) {
1186       for(i = 0; i < cp->tw * cp->th; i++) {
1187         if(cp->tcps[i].ppt_data_first != NULL) {
1188           opj_free(cp->tcps[i].ppt_data_first);
1189         }
1190         if(cp->tcps[i].tccps != NULL) {
1191           opj_free(cp->tcps[i].tccps);
1192         }
1193       }
1194       opj_free(cp->tcps);
1195     }
1196     if(cp->ppm_data_first != NULL) {
1197       opj_free(cp->ppm_data_first);
1198     }
1199     if(cp->tileno != NULL) {
1200       opj_free(cp->tileno);  
1201     }
1202     if(cp->comment != NULL) {
1203       opj_free(cp->comment);
1204     }
1205
1206     opj_free(cp);
1207   }
1208
1209   opj_free(j2k);
1210 }
1211
1212 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1213   if(j2k && parameters) {
1214     /* create and initialize the coding parameters structure */
1215     opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1216     cp->reduce = parameters->cp_reduce;  
1217     cp->layer = parameters->cp_layer;
1218
1219     /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1220     j2k->cp = cp;
1221   }
1222 }
1223
1224 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio) {
1225   opj_image_t *image = NULL;
1226
1227   opj_common_ptr cinfo = j2k->cinfo;
1228
1229   j2k->cio = cio;
1230
1231   /* create an empty image */
1232   image = opj_image_create0();
1233   j2k->image = image;
1234
1235   j2k->state = J2K_STATE_MHSOC;
1236
1237   for (;;) {
1238     opj_dec_mstabent_t *e;
1239     int id = cio_read(cio, 2);
1240     if (id >> 8 != 0xff) {
1241       opj_image_destroy(image);
1242       opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1243       return 0;
1244     }
1245     e = j2k_dec_mstab_lookup(id);
1246     if (!(j2k->state & e->states)) {
1247       opj_image_destroy(image);
1248       opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1249       return 0;
1250     }
1251     if (e->handler) {
1252       (*e->handler)(j2k);
1253     }
1254     if (j2k->state == J2K_STATE_MT) {
1255       break;
1256     }
1257     if (j2k->state == J2K_STATE_NEOC) {
1258       break;
1259     }
1260   }
1261   if (j2k->state == J2K_STATE_NEOC) {
1262     j2k_read_eoc(j2k);
1263   }
1264
1265   if (j2k->state != J2K_STATE_MT) {
1266     opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1267   }
1268
1269   return image;
1270 }
1271
1272 /*
1273 * Read a JPT-stream and decode file
1274 *
1275 */
1276 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio) {
1277   opj_image_t *image = NULL;
1278   opj_jpt_msg_header_t header;
1279   int position;
1280
1281   opj_common_ptr cinfo = j2k->cinfo;
1282   
1283   j2k->cio = cio;
1284
1285   /* create an empty image */
1286   image = opj_image_create0();
1287
1288   j2k->state = J2K_STATE_MHSOC;
1289   
1290   /* Initialize the header */
1291   jpt_init_msg_header(&header);
1292   /* Read the first header of the message */
1293   jpt_read_msg_header(cinfo, cio, &header);
1294   
1295   position = cio_tell(cio);
1296   if (header.Class_Id != 6) {  /* 6 : Main header data-bin message */
1297     opj_image_destroy(image);
1298     opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1299     return 0;
1300   }
1301   
1302   for (;;) {
1303     opj_dec_mstabent_t *e = NULL;
1304     int id;
1305     
1306     if (!cio_numbytesleft(cio)) {
1307       j2k_read_eoc(j2k);
1308       return image;
1309     }
1310     /* data-bin read -> need to read a new header */
1311     if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1312       jpt_read_msg_header(cinfo, cio, &header);
1313       position = cio_tell(cio);
1314       if (header.Class_Id != 4) {  /* 4 : Tile data-bin message */
1315         opj_image_destroy(image);
1316         opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1317         return 0;
1318       }
1319     }
1320     
1321     id = cio_read(cio, 2);
1322     if (id >> 8 != 0xff) {
1323       opj_image_destroy(image);
1324       opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1325       return 0;
1326     }
1327     e = j2k_dec_mstab_lookup(id);
1328     if (!(j2k->state & e->states)) {
1329       opj_image_destroy(image);
1330       opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1331       return 0;
1332     }
1333     if (e->handler) {
1334       (*e->handler)(j2k);
1335     }
1336     if (j2k->state == J2K_STATE_MT) {
1337       break;
1338     }
1339     if (j2k->state == J2K_STATE_NEOC) {
1340       break;
1341     }
1342   }
1343   if (j2k->state == J2K_STATE_NEOC) {
1344     j2k_read_eoc(j2k);
1345   }
1346   
1347   if (j2k->state != J2K_STATE_MT) {
1348     opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1349   }
1350
1351   return image;
1352 }
1353
1354 /* ----------------------------------------------------------------------- */
1355 /* J2K encoder interface                                                       */
1356 /* ----------------------------------------------------------------------- */
1357
1358 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1359   opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1360   if(j2k) {
1361     j2k->cinfo = cinfo;
1362   }
1363   return j2k;
1364 }
1365
1366 void j2k_destroy_compress(opj_j2k_t *j2k) {
1367   int tileno;
1368
1369   if(!j2k) return;
1370
1371   if(j2k->image_info != NULL) {
1372     opj_image_info_t *image_info = j2k->image_info;
1373     if (image_info->index_on && j2k->cp) {
1374       opj_cp_t *cp = j2k->cp;
1375       for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1376         opj_tile_info_t *tile_info = &image_info->tile[tileno];
1377         opj_free(tile_info->thresh);
1378         opj_free(tile_info->packet);
1379       }
1380       opj_free(image_info->tile);
1381     }
1382     opj_free(image_info);
1383   }
1384   if(j2k->cp != NULL) {
1385     opj_cp_t *cp = j2k->cp;
1386
1387     if(cp->comment) {
1388       opj_free(cp->comment);
1389     }
1390     if(cp->matrice) {
1391       opj_free(cp->matrice);
1392     }
1393     for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1394       opj_free(cp->tcps[tileno].tccps);
1395     }
1396     opj_free(cp->tcps);
1397     opj_free(cp);
1398   }
1399
1400   opj_free(j2k);
1401 }
1402
1403 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1404   int i, j, tileno, numpocs_tile;
1405   opj_cp_t *cp = NULL;
1406
1407   if(!j2k || !parameters || ! image) {
1408     return;
1409   }
1410
1411   /* create and initialize the coding parameters structure */
1412   cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1413
1414   /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1415   j2k->cp = cp;
1416
1417   /* set default values for cp */
1418   cp->tw = 1;
1419   cp->th = 1;
1420
1421   /* 
1422   copy user encoding parameters 
1423   */
1424
1425   cp->disto_alloc = parameters->cp_disto_alloc;
1426   cp->fixed_alloc = parameters->cp_fixed_alloc;
1427   cp->fixed_quality = parameters->cp_fixed_quality;
1428
1429   /* mod fixed_quality */
1430   if(parameters->cp_matrice) {
1431     size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
1432     cp->matrice = (int *) opj_malloc(array_size);
1433     memcpy(cp->matrice, parameters->cp_matrice, array_size);
1434   }
1435
1436   /* creation of an index file ? */
1437   cp->index_on = parameters->index_on;
1438   if(cp->index_on) {
1439     j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t));
1440   }
1441
1442   /* tiles */
1443   cp->tdx = parameters->cp_tdx;
1444   cp->tdy = parameters->cp_tdy;
1445
1446   /* tile offset */
1447   cp->tx0 = parameters->cp_tx0;
1448   cp->ty0 = parameters->cp_ty0;
1449
1450   /* comment string */
1451   if(parameters->cp_comment) {
1452     cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1453     if(cp->comment) {
1454       strcpy(cp->comment, parameters->cp_comment);
1455     }
1456   }
1457
1458   /*
1459   calculate other encoding parameters
1460   */
1461
1462   if (parameters->tile_size_on) {
1463     cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1464     cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1465   } else {
1466     cp->tdx = image->x1 - cp->tx0;
1467     cp->tdy = image->y1 - cp->ty0;
1468   }
1469
1470   /* initialize the mutiple tiles */
1471   /* ---------------------------- */
1472   cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
1473
1474   for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1475     opj_tcp_t *tcp = &cp->tcps[tileno];
1476     tcp->numlayers = parameters->tcp_numlayers;
1477     for (j = 0; j < tcp->numlayers; j++) {
1478       if (cp->fixed_quality) {  /* add fixed_quality */
1479         tcp->distoratio[j] = parameters->tcp_distoratio[j];
1480       } else {
1481         tcp->rates[j] = parameters->tcp_rates[j];
1482       }
1483     }
1484     tcp->csty = parameters->csty;
1485     tcp->prg = parameters->prog_order;
1486     tcp->mct = image->numcomps == 3 ? 1 : 0;
1487
1488     numpocs_tile = 0;
1489     tcp->POC = 0;
1490     if (parameters->numpocs) {
1491       /* initialisation of POC */
1492       tcp->POC = 1;
1493       for (i = 0; i < parameters->numpocs; i++) {
1494         if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1495           opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1496           tcp_poc->resno0    = parameters->POC[numpocs_tile].resno0;
1497           tcp_poc->compno0  = parameters->POC[numpocs_tile].compno0;
1498           tcp_poc->layno1    = parameters->POC[numpocs_tile].layno1;
1499           tcp_poc->resno1    = parameters->POC[numpocs_tile].resno1;
1500           tcp_poc->compno1  = parameters->POC[numpocs_tile].compno1;
1501           tcp_poc->prg    = parameters->POC[numpocs_tile].prg;
1502           tcp_poc->tile    = parameters->POC[numpocs_tile].tile;
1503           numpocs_tile++;
1504         }
1505       }
1506     }
1507     tcp->numpocs = numpocs_tile;
1508
1509     tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1510     
1511     for (i = 0; i < image->numcomps; i++) {
1512       opj_tccp_t *tccp = &tcp->tccps[i];
1513       tccp->csty = parameters->csty & 0x01;  /* 0 => one precinct || 1 => custom precinct  */
1514       tccp->numresolutions = parameters->numresolution;
1515       tccp->cblkw = int_floorlog2(parameters->cblockw_init);
1516       tccp->cblkh = int_floorlog2(parameters->cblockh_init);
1517       tccp->cblksty = parameters->mode;
1518       tccp->qmfbid = parameters->irreversible ? 0 : 1;
1519       tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
1520       tccp->numgbits = 2;
1521       if (i == parameters->roi_compno) {
1522         tccp->roishift = parameters->roi_shift;
1523       } else {
1524         tccp->roishift = 0;
1525       }
1526       if (parameters->csty & J2K_CCP_CSTY_PRT) {
1527         int p = 0;
1528         for (j = tccp->numresolutions - 1; j >= 0; j--) {
1529           if (p < parameters->res_spec) {
1530             if (parameters->prcw_init[p] < 1) {
1531               tccp->prcw[j] = 1;
1532             } else {
1533               tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
1534             }
1535             if (parameters->prch_init[p] < 1) {
1536               tccp->prch[j] = 1;
1537             } else {
1538               tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
1539             }
1540           } else {
1541             int res_spec = parameters->res_spec;
1542             int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
1543             int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
1544             if (size_prcw < 1) {
1545               tccp->prcw[j] = 1;
1546             } else {
1547               tccp->prcw[j] = int_floorlog2(size_prcw);
1548             }
1549             if (size_prch < 1) {
1550               tccp->prch[j] = 1;
1551             } else {
1552               tccp->prch[j] = int_floorlog2(size_prch);
1553             }
1554           }
1555           p++;
1556           /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
1557         }
1558       } else {
1559         for (j = 0; j < tccp->numresolutions; j++) {
1560           tccp->prcw[j] = 15;
1561           tccp->prch[j] = 15;
1562         }
1563       }
1564
1565       dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
1566     }
1567   }
1568 }
1569
1570 /**
1571 Create an index file
1572 @param j2k
1573 @param cio
1574 @param image_info
1575 @param index Index filename
1576 @return Returns 1 if successful, returns 0 otherwise
1577 */
1578 static int j2k_create_index(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_info_t *image_info, char *index) {
1579   int tileno, compno, layno, resno, precno, pack_nb, x, y;
1580   FILE *stream = NULL;
1581   double total_disto = 0;
1582
1583   image_info->codestream_size = cio_tell(cio) + j2k->pos_correction;  /* Correction 14/4/03 suite rmq de Patrick */
1584
1585   stream = fopen(index, "w");
1586   if (!stream) {
1587     opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
1588     return 0;
1589   }
1590   
1591   fprintf(stream, "%d %d\n", image_info->image_w, image_info->image_h);
1592   fprintf(stream, "%d\n", image_info->prog);
1593   fprintf(stream, "%d %d\n", image_info->tile_x, image_info->tile_y);
1594   fprintf(stream, "%d %d\n", image_info->tw, image_info->th);
1595   fprintf(stream, "%d\n", image_info->comp);
1596   fprintf(stream, "%d\n", image_info->layer);
1597   fprintf(stream, "%d\n", image_info->decomposition);
1598   
1599   for (resno = image_info->decomposition; resno >= 0; resno--) {
1600     fprintf(stream, "[%d,%d] ", 
1601       (1 << image_info->tile[0].pdx[resno]), (1 << image_info->tile[0].pdx[resno]));  /* based on tile 0 */
1602   }
1603   fprintf(stream, "\n");
1604   fprintf(stream, "%d\n", image_info->main_head_end);
1605   fprintf(stream, "%d\n", image_info->codestream_size);
1606   
1607   for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
1608     fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n",
1609       image_info->tile[tileno].num_tile,
1610       image_info->tile[tileno].start_pos,
1611       image_info->tile[tileno].end_header,
1612       image_info->tile[tileno].end_pos,
1613       image_info->tile[tileno].distotile, image_info->tile[tileno].nbpix,
1614       image_info->tile[tileno].distotile / image_info->tile[tileno].nbpix);
1615   }
1616   
1617   for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
1618     int start_pos, end_pos;
1619     double disto = 0;
1620     pack_nb = 0;
1621     
1622     /*
1623     fprintf(stream, "pkno tileno layerno resno compno precno start_pos   end_pos       deltaSE        \n");
1624     */
1625     
1626     if (image_info->prog == LRCP) {  /* LRCP */
1627       /*
1628       fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto");
1629       */
1630       for (layno = 0; layno < image_info->layer; layno++) {
1631         for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1632           for (compno = 0; compno < image_info->comp; compno++) {
1633             int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1634             for (precno = 0; precno < prec_max; precno++) {
1635               start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1636               end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1637               disto = image_info->tile[tileno].packet[pack_nb].disto;
1638               fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
1639                 pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
1640               total_disto += disto;
1641               pack_nb++;
1642             }
1643           }
1644         }
1645       }
1646     } /* LRCP */
1647     else if (image_info->prog == RLCP) {  /* RLCP */
1648       /*
1649       fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");
1650       */
1651       for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1652         for (layno = 0; layno < image_info->layer; layno++) {
1653           for (compno = 0; compno < image_info->comp; compno++) {
1654             int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1655             for (precno = 0; precno < prec_max; precno++) {
1656               start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1657               end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1658               disto = image_info->tile[tileno].packet[pack_nb].disto;
1659               fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
1660                 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
1661               total_disto += disto;
1662               pack_nb++;
1663             }
1664           }
1665         }
1666       }
1667     } /* RLCP */
1668     else if (image_info->prog == RPCL) {  /* RPCL */
1669       /*
1670       fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n"); 
1671       */
1672       for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1673         /* I suppose components have same XRsiz, YRsiz */
1674         int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
1675         int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
1676         int x1 = x0 + image_info->tile_x;
1677         int y1 = y0 + image_info->tile_y;
1678         for(y = y0; y < y1; y++) {
1679           for(x = x0; x < x1; x++) {
1680             for (compno = 0; compno < image_info->comp; compno++) {
1681               int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1682               for (precno = 0; precno < prec_max; precno++) {
1683                 int pcnx = image_info->tile[tileno].pw[resno];
1684                 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
1685                 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
1686                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
1687                 int precno_y = (int) floor( (float)precno/(float)pcnx );
1688                 if (precno_y*pcy == y ) {
1689                   if (precno_x*pcx == x ) {
1690                     for (layno = 0; layno < image_info->layer; layno++) {
1691                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1692                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1693                       disto = image_info->tile[tileno].packet[pack_nb].disto;
1694                       fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
1695                         pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); 
1696                       total_disto += disto;
1697                       pack_nb++; 
1698                     }
1699                   }
1700                 }
1701               } /* precno */
1702             } /* compno */
1703           } /* x = x0..x1 */
1704         } /* y = y0..y1 */
1705       } /* resno */
1706     } /* RPCL */
1707     else if (image_info->prog == PCRL) {  /* PCRL */
1708       /* I suppose components have same XRsiz, YRsiz */
1709       int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
1710       int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
1711       int x1 = x0 + image_info->tile_x;
1712       int y1 = y0 + image_info->tile_y;
1713       /*
1714       fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n"); 
1715       */
1716       for(y = y0; y < y1; y++) {
1717         for(x = x0; x < x1; x++) {
1718           for (compno = 0; compno < image_info->comp; compno++) {
1719             for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1720               int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1721               for (precno = 0; precno < prec_max; precno++) {
1722                 int pcnx = image_info->tile[tileno].pw[resno];
1723                 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
1724                 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
1725                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
1726                 int precno_y = (int) floor( (float)precno/(float)pcnx );
1727                 if (precno_y*pcy == y ) {
1728                   if (precno_x*pcx == x ) {
1729                     for (layno = 0; layno < image_info->layer; layno++) {
1730                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1731                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1732                       disto = image_info->tile[tileno].packet[pack_nb].disto;
1733                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
1734                         pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); 
1735                       total_disto += disto;
1736                       pack_nb++; 
1737                     }
1738                   }
1739                 }
1740               } /* precno */
1741             } /* resno */
1742           } /* compno */
1743         } /* x = x0..x1 */
1744       } /* y = y0..y1 */
1745     } /* PCRL */
1746     else {  /* CPRL */
1747       /*
1748       fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n"); 
1749       */
1750       for (compno = 0; compno < image_info->comp; compno++) {
1751         /* I suppose components have same XRsiz, YRsiz */
1752         int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
1753         int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
1754         int x1 = x0 + image_info->tile_x;
1755         int y1 = y0 + image_info->tile_y;
1756         for(y = y0; y < y1; y++) {
1757           for(x = x0; x < x1; x++) {
1758             for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1759               int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1760               for (precno = 0; precno < prec_max; precno++) {
1761                 int pcnx = image_info->tile[tileno].pw[resno];
1762                 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
1763                 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
1764                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
1765                 int precno_y = (int) floor( (float)precno/(float)pcnx );
1766                 if (precno_y*pcy == y ) {
1767                   if (precno_x*pcx == x ) {
1768                     for (layno = 0; layno < image_info->layer; layno++) {
1769                       start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1770                       end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1771                       disto = image_info->tile[tileno].packet[pack_nb].disto;
1772                       fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
1773                         pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); 
1774                       total_disto += disto;
1775                       pack_nb++; 
1776                     }
1777                   }
1778                 }
1779               } /* precno */
1780             } /* resno */
1781           } /* x = x0..x1 */
1782         } /* y = y0..y1 */
1783       } /* comno */
1784     } /* CPRL */   
1785   } /* tileno */
1786   
1787   fprintf(stream, "%8e\n", image_info->D_max); /* SE max */
1788   fprintf(stream, "%.8e\n", total_disto);  /* SE totale */
1789   fclose(stream);
1790
1791   return 1;
1792 }
1793
1794 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) {
1795   int tileno, compno;
1796   opj_image_info_t *image_info = NULL;
1797   opj_cp_t *cp = NULL;
1798
1799   opj_tcd_t *tcd = NULL;  /* TCD component */
1800
1801   j2k->cio = cio;  
1802   j2k->image = image;
1803
1804   cp = j2k->cp;
1805
1806   /* j2k_dump_cp(stdout, image, cp); */
1807
1808   /* INDEX >> */
1809   image_info = j2k->image_info;
1810   if (image_info && cp->index_on) {
1811     image_info->index_on = cp->index_on;
1812     image_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
1813     image_info->image_w = image->x1 - image->x0;
1814     image_info->image_h = image->y1 - image->y0;
1815     image_info->prog = (&cp->tcps[0])->prg;
1816     image_info->tw = cp->tw;
1817     image_info->th = cp->th;
1818     image_info->tile_x = cp->tdx;  /* new version parser */
1819     image_info->tile_y = cp->tdy;  /* new version parser */
1820     image_info->tile_Ox = cp->tx0;  /* new version parser */
1821     image_info->tile_Oy = cp->ty0;  /* new version parser */
1822     image_info->comp = image->numcomps;
1823     image_info->layer = (&cp->tcps[0])->numlayers;
1824     image_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1;
1825     image_info->D_max = 0;    /* ADD Marcela */
1826   }
1827   /* << INDEX */
1828   
1829   j2k_write_soc(j2k);
1830   j2k_write_siz(j2k);
1831   j2k_write_cod(j2k);
1832   j2k_write_qcd(j2k);
1833   for (compno = 0; compno < image->numcomps; compno++) {
1834     opj_tcp_t *tcp = &cp->tcps[0];
1835     if (tcp->tccps[compno].roishift)
1836       j2k_write_rgn(j2k, compno, 0);
1837   }
1838   if (cp->comment != NULL) {
1839     j2k_write_com(j2k);
1840   }
1841   /* INDEX >> */
1842   if(image_info && image_info->index_on) {
1843     image_info->main_head_end = cio_tell(cio) - 1;
1844   }
1845   /* << INDEX */
1846
1847   /* create the tile encoder */
1848   tcd = tcd_create(j2k->cinfo);
1849
1850   /* encode each tile */
1851
1852   for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1853     opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
1854     
1855     j2k->curtileno = tileno;
1856
1857     /* initialisation before tile encoding  */
1858     if (tileno == 0) {
1859       tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
1860     } else {
1861       tcd_init_encode(tcd, image, cp, j2k->curtileno);
1862     }
1863     
1864     /* INDEX >> */
1865     if(image_info && image_info->index_on) {
1866       image_info->tile[j2k->curtileno].num_tile = j2k->curtileno;
1867       image_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
1868     }
1869     /* << INDEX */
1870
1871     j2k_write_sot(j2k);
1872     
1873     for (compno = 1; compno < image->numcomps; compno++) {
1874       j2k_write_coc(j2k, compno);
1875       j2k_write_qcc(j2k, compno);
1876     }
1877     if (cp->tcps[tileno].numpocs) {
1878       j2k_write_poc(j2k);
1879     }
1880
1881     j2k_write_sod(j2k, tcd);
1882     
1883     /* INDEX >> */
1884     if(image_info && image_info->index_on) {
1885       image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
1886     }
1887     /* << INDEX */
1888     
1889     
1890     /*
1891     if (tile->PPT) { // BAD PPT !!! 
1892       FILE *PPT_file;
1893       int i;
1894       PPT_file=fopen("PPT","rb");
1895       fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
1896       for (i=0;i<tile->len_ppt;i++) {
1897         unsigned char elmt;
1898         fread(&elmt, 1, 1, PPT_file);
1899         fwrite(&elmt,1,1,f);
1900       }
1901       fclose(PPT_file);
1902       unlink("PPT");
1903     }
1904     */
1905     
1906   }
1907   
1908   /* destroy the tile encoder */
1909   tcd_free_encode(tcd);
1910   tcd_destroy(tcd);
1911
1912   j2k_write_eoc(j2k);
1913   
1914   /* Creation of the index file */
1915   if(image_info && image_info->index_on) {
1916     if(!j2k_create_index(j2k, cio, image_info, index)) {
1917       opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
1918       return false;
1919     }
1920   }
1921     
1922   return true;
1923 }
1924