[trunk] JP3D: remove old debug
[openjpeg.git] / src / lib / openjp3d / jp3d.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, Herve Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 /** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
35 /*@{*/
36
37 /** @name Funciones locales */
38 /*@{*/
39
40 /**
41 Write the SOC marker (Start Of Codestream)
42 @param j3d J3D handle
43 */
44 static void j3d_write_soc(opj_j3d_t *j3d);
45 /**
46 Read the SOC marker (Start of Codestream)
47 @param j3d J3D handle
48 */
49 static void j3d_read_soc(opj_j3d_t *j3d);
50 /**
51 Write the SIZ marker (2D volume and tile size)
52 @param j3d J3D handle
53 */
54 static void j3d_write_siz(opj_j3d_t *j3d);
55 /**
56 Read the SIZ marker (2D volume and tile size)
57 @param j3d J3D handle
58 */
59 static void j3d_read_siz(opj_j3d_t *j3d);
60 /**
61 Write the NSI marker (3rd volume and tile size)
62 @param j3d J3D handle
63 */
64 static void j3d_write_nsi(opj_j3d_t *j3d);
65 /**
66 Read the NSI marker (3rd volume and tile size)
67 @param j3d J3D handle
68 */
69 static void j3d_read_nsi(opj_j3d_t *j3d);
70 /**
71 Write the COM marker (comment)
72 @param j3d J3D handle
73 */
74 static void j3d_write_com(opj_j3d_t *j3d);
75 /**
76 Read the COM marker (comment)
77 @param j3d J3D handle
78 */
79 static void j3d_read_com(opj_j3d_t *j3d);
80 /**
81 Write the value concerning the specified component in the marker COD and COC
82 @param j3d J3D handle
83 @param compno Number of the component concerned by the information written
84 */
85 static void j3d_write_cox(opj_j3d_t *j3d, int compno);
86 /**
87 Read the value concerning the specified component in the marker COD and COC
88 @param j3d J3D handle
89 @param compno Number of the component concerned by the information read
90 */
91 static void j3d_read_cox(opj_j3d_t *j3d, int compno);
92 /**
93 Write the COD marker (coding style default)
94 @param j3d J3D handle
95 */
96 static void j3d_write_cod(opj_j3d_t *j3d);
97 /**
98 Read the COD marker (coding style default)
99 @param j3d J3D handle
100 */
101 static void j3d_read_cod(opj_j3d_t *j3d);
102 /**
103 Write the COC marker (coding style component)
104 @param j3d J3D handle
105 @param compno Number of the component concerned by the information written
106 */
107 static void j3d_write_coc(opj_j3d_t *j3d, int compno);
108 /**
109 Read the COC marker (coding style component)
110 @param j3d J3D handle
111 */
112 static void j3d_read_coc(opj_j3d_t *j3d);
113 /**
114 Write the value concerning the specified component in the marker QCD and QCC
115 @param j3d J3D handle
116 @param compno Number of the component concerned by the information written
117 */
118 static void j3d_write_qcx(opj_j3d_t *j3d, int compno);
119 /**
120 Read the value concerning the specified component in the marker QCD and QCC
121 @param j3d J3D handle
122 @param compno Number of the component concern by the information read
123 @param len Length of the information in the QCX part of the marker QCD/QCC
124 */
125 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len);
126 /**
127 Write the QCD marker (quantization default)
128 @param j3d J3D handle
129 */
130 static void j3d_write_qcd(opj_j3d_t *j3d);
131 /**
132 Read the QCD marker (quantization default)
133 @param j3d J3D handle
134 */
135 static void j3d_read_qcd(opj_j3d_t *j3d);
136 /**
137 Write the QCC marker (quantization component)
138 @param j3d J3D handle
139 @param compno Number of the component concerned by the information written
140 */
141 static void j3d_write_qcc(opj_j3d_t *j3d, int compno);
142 /**
143 Read the QCC marker (quantization component)
144 @param j3d J3D handle
145 */
146 static void j3d_read_qcc(opj_j3d_t *j3d);
147 /**
148 Write the POC marker (progression order change)
149 @param j3d J3D handle
150 */
151 static void j3d_write_poc(opj_j3d_t *j3d);
152 /**
153 Read the POC marker (progression order change)
154 @param j3d J3D handle
155 */
156 static void j3d_read_poc(opj_j3d_t *j3d);
157 /**
158 Read the CRG marker (component registration)
159 @param j3d J3D handle
160 */
161 static void j3d_read_crg(opj_j3d_t *j3d);
162 /**
163 Read the TLM marker (tile-part lengths)
164 @param j3d J3D handle
165 */
166 static void j3d_read_tlm(opj_j3d_t *j3d);
167 /**
168 Read the PLM marker (packet length, main header)
169 @param j3d J3D handle
170 */
171 static void j3d_read_plm(opj_j3d_t *j3d);
172 /**
173 Read the PLT marker (packet length, tile-part header)
174 @param j3d J3D handle
175 */
176 static void j3d_read_plt(opj_j3d_t *j3d);
177 /**
178 Read the PPM marker (packet packet headers, main header)
179 @param j3d J3D handle
180 */
181 static void j3d_read_ppm(opj_j3d_t *j3d);
182 /**
183 Read the PPT marker (packet packet headers, tile-part header)
184 @param j3d J3D handle
185 */
186 static void j3d_read_ppt(opj_j3d_t *j3d);
187 /**
188 Write the SOT marker (start of tile-part)
189 @param j3d J3D handle
190 */
191 static void j3d_write_sot(opj_j3d_t *j3d);
192 /**
193 Read the SOT marker (start of tile-part)
194 @param j3d J3D handle
195 */
196 static void j3d_read_sot(opj_j3d_t *j3d);
197 /**
198 Write the SOD marker (start of data)
199 @param j3d J3D handle
200 @param tile_coder Pointer to a TCD handle
201 */
202 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder);
203 /**
204 Read the SOD marker (start of data)
205 @param j3d J3D handle
206 */
207 static void j3d_read_sod(opj_j3d_t *j3d);
208 /**
209 Write the RGN marker (region-of-interest)
210 @param j3d J3D handle
211 @param compno Number of the component concerned by the information written
212 @param tileno Number of the tile concerned by the information written
213 */
214 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno);
215 /**
216 Read the RGN marker (region-of-interest)
217 @param j3d J3D handle
218 */
219 static void j3d_read_rgn(opj_j3d_t *j3d);
220 /**
221 Write the EOC marker (end of codestream)
222 @param j3d J3D handle
223 */
224 static void j3d_write_eoc(opj_j3d_t *j3d);
225 /**
226 Read the EOC marker (end of codestream)
227 @param j3d J3D handle
228 */
229 static void j3d_read_eoc(opj_j3d_t *j3d);
230 /**
231 Read an unknown marker
232 @param j3d J3D handle
233 */
234 static void j3d_read_unk(opj_j3d_t *j3d);
235 /**
236 Write the CAP marker (extended capabilities)
237 @param j3d J3D handle
238 */
239 static void j3d_write_cap(opj_j3d_t *j3d);
240 /**
241 Read the CAP marker (extended capabilities)
242 @param j3d J3D handle
243 */
244 static void j3d_read_cap(opj_j3d_t *j3d);
245 /**
246 Write the DCO marker (Variable DC offset)
247 @param j3d J3D handle
248 */
249 static void j3d_write_dco(opj_j3d_t *j3d);
250 /**
251 Read the DCO marker (Variable DC offset)
252 @param j3d J3D handle
253 */
254 static void j3d_read_dco(opj_j3d_t *j3d);
255 /**
256 Write the ATK marker (arbitrary transformation kernel)
257 @param j3d J3D handle
258 */
259 static void j3d_write_atk(opj_j3d_t *j3d);
260 /**
261 Read the ATK marker (arbitrary transformation kernel)
262 @param j3d J3D handle
263 */
264 static void j3d_read_atk(opj_j3d_t *j3d);
265 /**
266 Write the CBD marker (component bit depth definition)
267 @param j3d J3D handle
268 */
269 static void j3d_write_cbd(opj_j3d_t *j3d);
270 /**
271 Read the CBD marker (component bit depth definition)
272 @param j3d J3D handle
273 */
274 static void j3d_read_cbd(opj_j3d_t *j3d);
275 /**
276 Write the MCT marker (multiple component transfomation definition)
277 @param j3d J3D handle
278 */
279 static void j3d_write_mct(opj_j3d_t *j3d);
280 /**
281 Read the MCT marker (multiple component transfomation definition)
282 @param j3d J3D handle
283 */
284 static void j3d_read_mct(opj_j3d_t *j3d);
285 /**
286 Write the MCC marker (multiple component transfomation collection)
287 @param j3d J3D handle
288 */
289 static void j3d_write_mcc(opj_j3d_t *j3d);
290 /**
291 Read the MCC marker (multiple component transfomation collection)
292 @param j3d J3D handle
293 */
294 static void j3d_read_mcc(opj_j3d_t *j3d);
295 /**
296 Write the MCO marker (multiple component transfomation ordering)
297 @param j3d J3D handle
298 */
299 static void j3d_write_mco(opj_j3d_t *j3d);
300 /**
301 Read the MCO marker (multiple component transfomation ordering)
302 @param j3d J3D handle
303 */
304 static void j3d_read_mco(opj_j3d_t *j3d);
305 /**
306 Write the NLT marker (non-linearity point transformation)
307 @param j3d J3D handle
308 */
309 static void j3d_write_nlt(opj_j3d_t *j3d);
310 /**
311 Read the NLT marker (non-linearity point transformation)
312 @param j3d J3D handle
313 */
314 static void j3d_read_nlt(opj_j3d_t *j3d);
315 /*@}*/
316
317 /* ----------------------------------------------------------------------- */
318
319 static void j3d_dump_volume(FILE *fd, opj_volume_t * vol) {
320         int compno;
321         fprintf(fd, "volume {\n");
322         fprintf(fd, "  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1,  vol->z1);
323         fprintf(fd, "  numcomps=%d\n", vol->numcomps);
324         for (compno = 0; compno < vol->numcomps; compno++) {
325                 opj_volume_comp_t *comp = &vol->comps[compno];
326                 fprintf(fd, "  comp %d {\n", compno);
327                 fprintf(fd, "    dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);
328                 fprintf(fd, "    prec=%d\n", comp->prec);
329                 fprintf(fd, "    sgnd=%d\n", comp->sgnd);
330                 fprintf(fd, "  }\n");
331         }
332         fprintf(fd, "}\n");
333 }
334
335 static void j3d_dump_cp(FILE *fd, opj_volume_t * vol, opj_cp_t * cp) {
336         int tileno, compno, layno, bandno, resno, numbands;
337         fprintf(fd, "coding parameters {\n");
338         fprintf(fd, "  tx0=%d, ty0=%d, tz0=%d\n", cp->tx0, cp->ty0, cp->tz0);
339         fprintf(fd, "  tdx=%d, tdy=%d, tdz=%d\n", cp->tdx, cp->tdy, cp->tdz);
340         fprintf(fd, "  tw=%d, th=%d, tl=%d\n", cp->tw, cp->th, cp->tl);
341         fprintf(fd, "  transform format: %d\n", cp->transform_format);
342         fprintf(fd, "  encoding format: %d\n", cp->encoding_format);
343         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
344                 opj_tcp_t *tcp = &cp->tcps[tileno];
345                 fprintf(fd, "  tile %d {\n", tileno);
346                 fprintf(fd, "    csty=%x\n", tcp->csty);
347                 fprintf(fd, "    prg=%d\n", tcp->prg);
348                 fprintf(fd, "    numlayers=%d\n", tcp->numlayers);
349                 fprintf(fd, "    mct=%d\n", tcp->mct);
350                 fprintf(fd, "    rates=");
351                 for (layno = 0; layno < tcp->numlayers; layno++) {
352                         fprintf(fd, "%f ", tcp->rates[layno]);
353                 }
354                 fprintf(fd, "\n");
355                 fprintf(fd, "    first=%d\n", tcp->first);
356                 for (compno = 0; compno < vol->numcomps; compno++) {
357                         opj_tccp_t *tccp = &tcp->tccps[compno];
358                         fprintf(fd, "    comp %d {\n", compno);
359                         fprintf(fd, "      csty=%x\n", tccp->csty);
360                         fprintf(fd, "      numresx=%d, numresy=%d, numresz=%d\n", tccp->numresolution[0], tccp->numresolution[1], tccp->numresolution[2]);
361                         fprintf(fd, "      cblkw=%d, cblkh=%d, cblkl=%d\n", tccp->cblk[0], tccp->cblk[1], tccp->cblk[2]);
362                         fprintf(fd, "      cblksty=%x\n", tccp->cblksty);
363                         fprintf(fd, "      qntsty=%d\n", tccp->qntsty);
364                         fprintf(fd, "      numgbits=%d\n", tccp->numgbits);
365                         fprintf(fd, "      roishift=%d\n", tccp->roishift);
366                         fprintf(fd, "      reversible=%d\n", tccp->reversible);
367                         fprintf(fd, "      dwtidx=%d dwtidy=%d dwtidz=%d\n", tccp->dwtid[0], tccp->dwtid[1], tccp->dwtid[2]);
368                         if (tccp->atk != NULL) {
369                 fprintf(fd, "      atk.index=%d\n", tccp->atk->index);
370                                 fprintf(fd, "      atk.coeff_typ=%d\n", tccp->atk->coeff_typ);
371                                 fprintf(fd, "      atk.filt_cat=%d\n", tccp->atk->filt_cat);
372                                 fprintf(fd, "      atk.exten=%d\n", tccp->atk->exten);
373                                 fprintf(fd, "      atk.minit=%d\n", tccp->atk->minit);
374                                 fprintf(fd, "      atk.wt_typ=%d\n", tccp->atk->wt_typ);
375                         }
376                         fprintf(fd, "      stepsizes of bands=");
377             numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 :
378                         ( (cp->transform_format == TRF_2D_DWT) ? (tccp->numresolution[0] * 3 - 2) :
379                                 (tccp->numresolution[0] * 7 - 6) - 4 *(tccp->numresolution[0] - tccp->numresolution[2]) );
380                         for (bandno = 0; bandno < numbands; bandno++) {
381                                 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,tccp->stepsizes[bandno].expn);
382                         }
383                         fprintf(fd, "\n");
384                         
385                         if (tccp->csty & J3D_CCP_CSTY_PRT) {
386                                 fprintf(fd, "      prcw=");
387                                 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
388                                         fprintf(fd, "%d ", tccp->prctsiz[0][resno]);
389                                 }
390                                 fprintf(fd, "\n");
391                                 fprintf(fd, "      prch=");
392                                 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
393                                         fprintf(fd, "%d ", tccp->prctsiz[1][resno]);
394                                 }
395                                 fprintf(fd, "\n");
396                                 fprintf(fd, "      prcl=");
397                                 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
398                                         fprintf(fd, "%d ", tccp->prctsiz[2][resno]);
399                                 }
400                                 fprintf(fd, "\n");
401                         }
402                         fprintf(fd, "    }\n");
403                 }
404                 fprintf(fd, "  }\n");
405         }
406         fprintf(fd, "}\n");
407 }
408
409 /* ----------------------------------------------------------------------- 
410 Extended capabilities
411 ------------------------------------------------------------------------*/
412
413 static void j3d_write_cap(opj_j3d_t *j3d){
414         int len,lenp;
415
416         opj_cio_t *cio = j3d->cio;
417         cio_write(cio, J3D_MS_CAP, 2);  /* CAP */
418         lenp = cio_tell(cio);
419         cio_skip(cio, 2);
420         cio_write(cio,J3D_CAP_10, 4); 
421   if( J3D_CAP_10 )
422     {
423     cio_write(cio, 0x0, 2); 
424     }
425         len = cio_tell(cio) - lenp;
426         cio_seek(cio, lenp);
427         cio_write(cio, len, 2);         /* Lsiz */
428         cio_seek(cio, lenp + len);
429
430 }
431 static void j3d_read_cap(opj_j3d_t *j3d){
432         int len, Cap;
433         opj_cio_t *cio = j3d->cio;
434         /*cio_read(cio, 2);      CAP */
435         len = cio_read(cio, 2);
436         Cap = cio_read(cio, 4);
437   if(Cap) {
438     cio_read(cio, 2);
439   }
440   assert( len == 2 + 4 + 2 );
441 }
442 static void j3d_write_nsi(opj_j3d_t *j3d) {
443         int i;
444         int lenp, len;
445   int ndim = 3;
446
447         opj_cio_t *cio = j3d->cio;
448         opj_volume_t *volume = j3d->volume;
449         opj_cp_t *cp = j3d->cp;
450         
451         cio_write(cio, J3D_MS_NSI, 2);  /* NSI */
452         lenp = cio_tell(cio);
453         cio_skip(cio, 2);
454         cio_write(cio, ndim, 1);        /* Ndim */
455         cio_write(cio, volume->z1, 4);  /* Zsiz */
456         cio_write(cio, volume->z0, 4);  /* Z0siz */
457         cio_write(cio, cp->tdz, 4);             /* ZTsiz */
458         cio_write(cio, cp->tz0, 4);             /* ZT0siz */
459         for (i = 0; i < volume->numcomps; i++) {
460                 cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */
461         }
462         len = cio_tell(cio) - lenp;
463         cio_seek(cio, lenp);
464         cio_write(cio, len, 2);         /* Lsiz */
465         cio_seek(cio, lenp + len);
466 }
467
468 static void j3d_read_nsi(opj_j3d_t *j3d) {
469   int ndim;
470         int len, i;
471         
472         opj_cio_t *cio = j3d->cio;
473         opj_volume_t *volume = j3d->volume;
474         opj_cp_t *cp = j3d->cp;
475         
476         len = cio_read(cio, 2);                 /* Lnsi */
477         ndim = cio_read(cio, 1);                        /* Ndim */
478   assert( ndim == 3 );
479         volume->z1 = cio_read(cio, 4);  /* Zsiz */
480         volume->z0 = cio_read(cio, 4);  /* Z0siz */
481         cp->tdz = cio_read(cio, 4);             /* ZTsiz */
482         cp->tz0 = cio_read(cio, 4);             /* ZT0siz */
483         for (i = 0; i < volume->numcomps; i++) {
484                 volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */
485         }
486         
487         /*Initialization of volume*/
488         cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
489         cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
490         cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
491         cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
492         cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
493         cp->tileno_size = 0;
494         
495         for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
496                 cp->tcps[i].POC = 0;
497                 cp->tcps[i].numpocs = 0;
498                 cp->tcps[i].first = 1;
499         }
500         
501         /* Initialization for PPM marker (Packets header)*/
502         cp->ppm = 0;
503         cp->ppm_data = NULL;
504         cp->ppm_data_first = NULL;
505         cp->ppm_previous = 0;
506         cp->ppm_store = 0;
507         
508         j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
509         for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
510                 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
511         }
512         j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
513         j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
514         j3d->state = J3D_STATE_MH;
515         
516 }
517 static void j3d_write_dco(opj_j3d_t *j3d){
518         int lenp, len, i;
519         int dcotype;    
520
521         opj_cio_t *cio = j3d->cio;
522         opj_volume_t *volume = j3d->volume;
523         opj_cp_t *cp = j3d->cp;
524         
525         dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */
526         cio_write(cio, J3D_MS_DCO, 2);  /* DCO */
527         lenp = cio_tell(cio);
528         cio_skip(cio, 2);
529         cio_write(cio, dcotype, 1);     
530         if (dcotype == 0) {
531                 for (i = 0; i < volume->numcomps; i++) 
532                         cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */
533         } else if (dcotype == 1) {
534                 for (i = 0; i < volume->numcomps; i++){ 
535                         cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */
536                         opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset);
537                 }
538         }
539         len = cio_tell(cio) - lenp;
540         cio_seek(cio, lenp);
541         cio_write(cio, len, 2);         /* Ldco */
542         cio_seek(cio, lenp + len);
543
544 }
545 static void j3d_read_dco(opj_j3d_t *j3d){
546         int len, i;
547         int dcotype;
548
549         opj_cio_t *cio = j3d->cio;
550         opj_volume_t *volume = j3d->volume;
551         opj_cp_t *cp = j3d->cp;
552         
553         len = cio_read(cio, 2);                 /* Lsiz */
554         dcotype = cio_read(cio, 1); /*offset 8bit unsigned / 16bit signed integers*/
555         if (dcotype == 0) {
556                 for (i = 0; i < volume->numcomps; i++) {
557                         volume->comps[i].dcoffset = cio_read(cio, 1);
558                         if (volume->comps[i].dcoffset > 128) 
559                                 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
560                 }
561         } else if (dcotype == 1) {
562                 for (i = 0; i < volume->numcomps; i++) {
563                         volume->comps[i].dcoffset = cio_read(cio, 1);
564                         if (volume->comps[i].dcoffset > 128) 
565                                 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
566                 }
567         }
568         
569 }
570 static void j3d_write_atk(opj_j3d_t *j3d){
571         int lenp, len, s, k;
572
573         opj_cio_t *cio = j3d->cio;
574         opj_volume_t *volume = j3d->volume;
575         opj_atk_t *atk = j3d->cp->tcps->tccps->atk;
576         
577         cio_write(cio, J3D_MS_ATK, 2);  /* ATK */
578         lenp = cio_tell(cio);
579         cio_skip(cio, 2);                               
580         cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11) 
581                 + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2);                    /* Satk */
582     if (atk->wt_typ == J3D_ATK_IRR) 
583                 cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ);
584         cio_write(cio, atk->Natk, 1);
585         for (s = 0; s < atk->Natk; s++){
586                 if (atk->filt_cat == J3D_ATK_ARB) 
587                         cio_write(cio, atk->Oatk[s], 1);
588                 if (atk->wt_typ == J3D_ATK_REV){
589                         cio_write(cio, atk->Eatk[s], 1);
590                         cio_write(cio, atk->Batk[s], 1);
591                 }
592                 cio_write(cio, atk->LCatk[s], 1);
593                 for (k = 0; k < atk->LCatk[s]; k++)
594                         cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ);
595         }
596         len = cio_tell(cio) - lenp;
597         cio_seek(cio, lenp);
598         cio_write(cio, len, 2);         /* Latk */
599         cio_seek(cio, lenp + len);
600 }
601 static void j3d_read_atk(opj_j3d_t *j3d){
602         int len, i, Satk, k;
603         
604         opj_cio_t *cio = j3d->cio;
605         opj_volume_t *volume = j3d->volume;
606         opj_cp_t *cp = j3d->cp;
607         opj_atk_t *atk = cp->tcps->tccps->atk; 
608         
609         len = cio_read(cio, 2);                 /* Latk */
610         Satk = cio_read(cio, 2); 
611         atk->index = Satk & 0x00ff;
612         atk->coeff_typ = Satk >> 8 & 0x0007;
613         atk->filt_cat = Satk >> 11 & 0x0001;
614         atk->wt_typ = Satk >> 12  & 0x0001;
615         atk->minit = Satk >> 13 & 0x0001;
616         atk->exten = Satk >> 14 & 0x0001;
617     if (atk->wt_typ == J3D_ATK_IRR) 
618                 atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
619         atk->Natk = cio_read(cio, 1);
620         for (i = 0; i < atk->Natk; i++) {
621                 if (atk->filt_cat == J3D_ATK_ARB) 
622                         atk->Oatk[i] = cio_read(cio, 1);
623                 if (atk->wt_typ == J3D_ATK_REV){
624                         atk->Eatk[i] = cio_read(cio, 1);
625                         atk->Batk[i] = cio_read(cio, 1);
626                 }
627                 atk->LCatk[i] = cio_read(cio, 1);
628                 for (k = 0; k < atk->LCatk[i]; k++)
629                         atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
630         }
631 }
632 static void j3d_write_cbd(opj_j3d_t *j3d){
633 }
634 static void j3d_read_cbd(opj_j3d_t *j3d){
635 }
636 static void j3d_write_mct(opj_j3d_t *j3d){
637 }
638 static void j3d_read_mct(opj_j3d_t *j3d){
639 }
640 static void j3d_write_mcc(opj_j3d_t *j3d){
641 }
642 static void j3d_read_mcc(opj_j3d_t *j3d){
643 }
644 static void j3d_write_mco(opj_j3d_t *j3d){
645 }
646 static void j3d_read_mco(opj_j3d_t *j3d){
647 }
648 static void j3d_write_nlt(opj_j3d_t *j3d){
649 }
650 static void j3d_read_nlt(opj_j3d_t *j3d){
651 }
652 /* ----------------------------------------------------------------------- 
653 15444-1 codestream syntax
654 ------------------------------------------------------------------------*/
655 static void j3d_write_soc(opj_j3d_t *j3d) {
656         opj_cio_t *cio = j3d->cio;
657         cio_write(cio, J3D_MS_SOC, 2);
658 }
659
660 static void j3d_read_soc(opj_j3d_t *j3d) {
661         j3d->state = J3D_STATE_MHSIZ;
662 }
663
664 static void j3d_write_siz(opj_j3d_t *j3d) {
665         int i;
666         int lenp, len;
667         int Rsiz;
668
669         opj_cio_t *cio = j3d->cio;
670         opj_volume_t *volume = j3d->volume;
671         opj_cp_t *cp = j3d->cp;
672         
673         cio_write(cio, J3D_MS_SIZ, 2);  /* SIZ */
674         lenp = cio_tell(cio);
675         cio_skip(cio, 2);
676         /*cio_write(cio, 0, 2);*/                       /* Rsiz (capabilities of 15444-1 only) */
677         Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/
678         cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/
679         cio_write(cio, volume->x1, 4);  /* Xsiz */
680         cio_write(cio, volume->y1, 4);  /* Ysiz */
681         cio_write(cio, volume->x0, 4);  /* X0siz */
682         cio_write(cio, volume->y0, 4);  /* Y0siz */
683         cio_write(cio, cp->tdx, 4);             /* XTsiz */
684         cio_write(cio, cp->tdy, 4);             /* YTsiz */
685         cio_write(cio, cp->tx0, 4);             /* XT0siz */
686         cio_write(cio, cp->ty0, 4);             /* YT0siz */
687         cio_write(cio, volume->numcomps, 2);    /* Csiz */
688         for (i = 0; i < volume->numcomps; i++) {
689                 cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1);    /* Ssiz_i */
690                 cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */
691                 cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */
692         }
693         len = cio_tell(cio) - lenp;
694         cio_seek(cio, lenp);
695         cio_write(cio, len, 2);         /* Lsiz */
696         cio_seek(cio, lenp + len);
697 }
698
699 static void j3d_read_siz(opj_j3d_t *j3d) {
700         int len, i;
701         
702         opj_cio_t *cio = j3d->cio;
703         opj_volume_t *volume = j3d->volume;
704         opj_cp_t *cp = j3d->cp;
705         
706         len = cio_read(cio, 2);                 /* Lsiz */
707         cp->rsiz = cio_read(cio, 2);    /* Rsiz (capabilities) */
708         volume->x1 = cio_read(cio, 4);  /* Xsiz */
709         volume->y1 = cio_read(cio, 4);  /* Ysiz */
710         volume->x0 = cio_read(cio, 4);  /* X0siz */
711         volume->y0 = cio_read(cio, 4);  /* Y0siz */
712         cp->tdx = cio_read(cio, 4);             /* XTsiz */
713         cp->tdy = cio_read(cio, 4);             /* YTsiz */
714         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
715         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
716         
717         volume->numcomps = cio_read(cio, 2);    /* Csiz */
718         volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
719         for (i = 0; i < volume->numcomps; i++) {
720                 int tmp, j;
721                 tmp = cio_read(cio, 1);         /* Ssiz_i */
722                 volume->comps[i].prec = (tmp & 0x7f) + 1;
723                 volume->comps[i].sgnd = tmp >> 7;
724                 volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
725                 volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
726                 for (j = 0; j < 3; j++) {
727                         volume->comps[i].resno_decoded[j] = 0;          /* number of resolution decoded */
728                         volume->comps[i].factor[j] = 0;         /* reducing factor per component */
729                 }
730         }
731
732         if (j3d->cinfo->codec_format == CODEC_J2K){
733                 volume->z1 = 1;
734                 volume->z0 = 0;
735                 volume->numslices = 1;
736                 cp->tdz = 1;
737                 cp->tz0 = 0;
738                 for (i = 0; i < volume->numcomps; i++) 
739                         volume->comps[i].dz = 1;
740
741                 /*Initialization of volume*/
742                 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
743                 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
744                 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
745                 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
746                 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
747                 cp->tileno_size = 0;
748                 
749                 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
750                         cp->tcps[i].POC = 0;
751                         cp->tcps[i].numpocs = 0;
752                         cp->tcps[i].first = 1;
753                 }
754                 
755                 /* Initialization for PPM marker (Packets header)*/
756                 cp->ppm = 0;
757                 cp->ppm_data = NULL;
758                 cp->ppm_data_first = NULL;
759                 cp->ppm_previous = 0;
760                 cp->ppm_store = 0;
761                 
762                 j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
763                 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
764                         cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
765                 }
766                 j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
767                 j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
768                 j3d->state = J3D_STATE_MH;
769         }
770 }
771
772
773
774 static void j3d_write_com(opj_j3d_t *j3d) {
775         unsigned int i;
776         int lenp, len;
777
778         opj_cio_t *cio = j3d->cio;
779
780         cio_write(cio, J3D_MS_COM, 2);
781         lenp = cio_tell(cio);
782         cio_skip(cio, 2);
783         cio_write(cio, 1, 2);
784         /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
785         if (j3d->cp->comment != NULL) {
786                 char *comment = j3d->cp->comment;
787                 for (i = 0; i < strlen(comment); i++) {
788             cio_write(cio, comment[i], 1);
789                 }
790         }
791         len = cio_tell(cio) - lenp;
792         cio_seek(cio, lenp);
793         cio_write(cio, len, 2);
794         cio_seek(cio, lenp + len);
795 }
796
797 static void j3d_read_com(opj_j3d_t *j3d) {
798         int len;
799         opj_cio_t *cio = j3d->cio;
800
801         len = cio_read(cio, 2);
802         cio_read(cio, 2); // read registration
803         
804         /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
805
806         cio_skip(cio, len - 4);  /*posible comments*/
807 }
808
809 static void j3d_write_cox(opj_j3d_t *j3d, int compno) {
810         int i;
811   int shift = 2;
812
813         opj_cp_t *cp = j3d->cp;
814         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
815         opj_tccp_t *tccp = &tcp->tccps[compno];
816         opj_cio_t *cio = j3d->cio;
817         
818         cio_write(cio, tccp->numresolution[0] - 1, 1);  /* SPcox (D) No of decomposition levels in x-axis*/
819         if (j3d->cinfo->codec_format == CODEC_J3D) {
820                 cio_write(cio, tccp->numresolution[1] - 1, 1);  /* SPcox (E) No of decomposition levels in y-axis*/
821                 cio_write(cio, tccp->numresolution[2] - 1, 1);  /* SPcox (F) No of decomposition levels in z-axis*/
822         }
823         if (j3d->cinfo->codec_format == CODEC_J3D) {
824     /* Table A.7 */
825     shift = 0;
826   }
827         /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/
828         cio_write(cio, tccp->cblk[0] - shift, 1);                               /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/
829         cio_write(cio, tccp->cblk[1] - shift, 1);                               /* SPcox (H) Cblk height*/
830         if (j3d->cinfo->codec_format == CODEC_J3D) {
831                 cio_write(cio, tccp->cblk[2] - shift, 1);                       /* SPcox (I) Cblk depth*/
832         }
833         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (J) Cblk style*/
834         cio_write(cio, tccp->dwtid[0], 1);                              /* SPcox (K) WT in x-axis 15444-2 Table A10*/
835         if (j3d->cinfo->codec_format == CODEC_J3D) {
836                 cio_write(cio, tccp->dwtid[1], 1);                              /* SPcox (L) WT in y-axis 15444-2 Table A10*/
837                 cio_write(cio, tccp->dwtid[2], 1);                              /* SPcox (M) WT in z-axis 15444-2 Table A10*/
838         }
839         
840         if (tccp->csty & J3D_CCP_CSTY_PRT) {
841                 for (i = 0; i < tccp->numresolution[0]; i++) {
842                         if (i < tccp->numresolution[2])
843                 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2);       /* SPcox (N_i) Table A9*/
844                         else
845                                 if (j3d->cinfo->codec_format == CODEC_J3D) 
846                     cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2);        /* SPcox (N_i) Table A9*/
847                                 else
848                     cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1);        /* SPcox (N_i) Table A9*/               }
849         }
850 }
851
852 static void j3d_read_cox(opj_j3d_t *j3d, int compno) {
853         int i;
854   int shift = 2;
855
856         opj_cp_t *cp = j3d->cp;
857         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
858         opj_tccp_t *tccp = &tcp->tccps[compno];
859         opj_cio_t *cio = j3d->cio;
860
861         tccp->numresolution[0] = cio_read(cio, 1) + 1;  /* SPcox (D) No of decomposition levels in x-axis*/
862         if (j3d->cinfo->codec_format == CODEC_J3D) {
863                 tccp->numresolution[1] = cio_read(cio, 1) + 1;  /* SPcox (E) No of decomposition levels in y-axis*/
864                 tccp->numresolution[2] = cio_read(cio, 1) + 1;  /* SPcox (F) No of decomposition levels in z-axis*/
865         }else if (j3d->cinfo->codec_format == CODEC_J2K) {
866                 tccp->numresolution[1] = tccp->numresolution[0];        
867                 tccp->numresolution[2] = 1;                                                     
868         }
869         /* check the reduce value */
870         cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]);
871         cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]);
872         cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]);
873         
874   if (j3d->cinfo->codec_format == CODEC_J3D) {
875     /* Table A.7 */
876     shift = 0;
877   }
878         tccp->cblk[0] = cio_read(cio, 1) + shift;       /* SPcox (G) */
879         tccp->cblk[1] = cio_read(cio, 1) + shift;       /* SPcox (H) */
880         if (j3d->cinfo->codec_format == CODEC_J3D)
881                 tccp->cblk[2] = cio_read(cio, 1) + shift;       /* SPcox (I) */
882         else
883                 tccp->cblk[2] = tccp->cblk[0];
884
885         tccp->cblksty = cio_read(cio, 1);       /* SPcox (J) */
886         tccp->dwtid[0] = cio_read(cio, 1);      /* SPcox (K) */
887         if (j3d->cinfo->codec_format == CODEC_J3D) {
888                 tccp->dwtid[1] = cio_read(cio, 1);      /* SPcox (L) */
889                 tccp->dwtid[2] = cio_read(cio, 1);      /* SPcox (M) */
890         }else{
891                 tccp->dwtid[1] = tccp->dwtid[0];        /* SPcox (L) */
892                 tccp->dwtid[2] = tccp->dwtid[0];        /* SPcox (M) */
893         }
894         tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); /*TODO: only valid for irreversible 9x7 WTs*/
895         if (tccp->csty & J3D_CP_CSTY_PRT) {
896                 for (i = 0; i < tccp->numresolution[0]; i++) {
897                         int tmp = cio_read(cio, 2);     /* SPcox (N_i) */
898                         tccp->prctsiz[0][i] = tmp & 0xf;
899                         tccp->prctsiz[1][i] = tmp >> 4;
900                         tccp->prctsiz[2][i] = tmp >> 8;
901                 }
902         }
903 }
904
905 static void j3d_write_cod(opj_j3d_t *j3d) {
906         opj_cp_t *cp = NULL;
907         opj_tcp_t *tcp = NULL;
908         int lenp, len;
909
910         opj_cio_t *cio = j3d->cio;
911         
912         cio_write(cio, J3D_MS_COD, 2);  /* COD */
913         
914         lenp = cio_tell(cio);
915         cio_skip(cio, 2);
916         
917         cp = j3d->cp;
918         tcp = &cp->tcps[j3d->curtileno];
919
920         /* Scod : Table A-4*/
921         cio_write(cio, tcp->csty, 1);           /* Scod : Coding style parameters */
922         /* SGcod : Table A-5*/
923         cio_write(cio, tcp->prg, 1);            /* SGcod (A) : Progression order */
924         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) : No of layers */
925         cio_write(cio, tcp->mct, 1);            /* SGcod (C) : Multiple component transformation usage */
926         /* SPcod : Table A-6*/
927         j3d_write_cox(j3d, 0);                          
928         len = cio_tell(cio) - lenp;
929         cio_seek(cio, lenp);
930         cio_write(cio, len, 2);         /* Lcod */
931         cio_seek(cio, lenp + len);
932 }
933
934 static void j3d_read_cod(opj_j3d_t *j3d) {
935         int len, i, pos;
936         
937         opj_cio_t *cio = j3d->cio;
938         opj_cp_t *cp = j3d->cp;
939         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
940         opj_volume_t *volume = j3d->volume;
941
942         /* Lcod */
943         len = cio_read(cio, 2);                         
944         /* Scod : Table A-4*/
945         tcp->csty = cio_read(cio, 1);           
946         /* SGcod : Table A-5*/
947         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);
948         tcp->numlayers = cio_read(cio, 2);      
949         tcp->mct = cio_read(cio, 1);            
950         
951         pos = cio_tell(cio);
952         for (i = 0; i < volume->numcomps; i++) {
953                 tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;
954                 cio_seek(cio, pos);
955                 j3d_read_cox(j3d, i);
956         }
957 }
958
959 static void j3d_write_coc(opj_j3d_t *j3d, int compno) {
960         int lenp, len;
961
962         opj_cp_t *cp = j3d->cp;
963         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
964         opj_volume_t *volume = j3d->volume;
965         opj_cio_t *cio = j3d->cio;
966         
967         cio_write(cio, J3D_MS_COC, 2);  /* COC */
968         lenp = cio_tell(cio);
969         cio_skip(cio, 2);
970         cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */
971         cio_write(cio, tcp->tccps[compno].csty, 1);                                     /* Scoc */
972         
973         j3d_write_cox(j3d, compno);
974         
975         len = cio_tell(cio) - lenp;
976         cio_seek(cio, lenp);
977         cio_write(cio, len, 2);                 /* Lcoc */
978         cio_seek(cio, lenp + len);
979 }
980
981 static void j3d_read_coc(opj_j3d_t *j3d) {
982         int len, compno;
983
984         opj_cp_t *cp = j3d->cp;
985         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
986         opj_volume_t *volume = j3d->volume;
987         opj_cio_t *cio = j3d->cio;
988         
989         len = cio_read(cio, 2);         /* Lcoc */
990         compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */
991         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
992         j3d_read_cox(j3d, compno);
993 }
994
995 static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {
996         int bandno, numbands;
997         int expn, mant;
998         
999         opj_cp_t *cp = j3d->cp;
1000         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1001         opj_tccp_t *tccp = &tcp->tccps[compno];
1002         opj_cio_t *cio = j3d->cio;
1003         
1004         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx : Table A28 de 15444-1*/
1005         
1006         if (j3d->cinfo->codec_format == CODEC_J2K)
1007         numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2; 
1008         else if (j3d->cinfo->codec_format == CODEC_J3D) {
1009                 int diff = tccp->numresolution[0] - tccp->numresolution[2];
1010         numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
1011         }
1012         
1013         for (bandno = 0; bandno < numbands; bandno++) {
1014                 expn = tccp->stepsizes[bandno].expn;
1015                 mant = tccp->stepsizes[bandno].mant;
1016                 
1017                 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1018                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
1019                 } else {
1020                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1021                 }
1022         }
1023 }
1024
1025 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
1026         int tmp;
1027         int bandno, numbands;
1028
1029         opj_cp_t *cp = j3d->cp;
1030         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1031         opj_tccp_t *tccp = &tcp->tccps[compno];
1032         opj_cio_t *cio = j3d->cio;
1033
1034         tmp = cio_read(cio, 1);         /* Sqcx */
1035         tccp->qntsty = tmp & 0x1f;
1036         tccp->numgbits = tmp >> 5;
1037
1038         /*Numbands = 1                          si SIQNT
1039                              len - 1            si NOQNT
1040                                  (len - 1) / 2  si SEQNT */
1041         numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1042
1043         for (bandno = 0; bandno < numbands; bandno++) {
1044                 int expn, mant;
1045                 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1046                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
1047                         mant = 0;
1048                 } else {
1049                         tmp = cio_read(cio, 2);                 /* SPqcx_i */
1050                         expn = tmp >> 11;
1051                         mant = tmp & 0x7ff;
1052                 }
1053                 tccp->stepsizes[bandno].expn = expn;
1054                 tccp->stepsizes[bandno].mant = mant;
1055         }
1056         
1057         /* Add Antonin : if scalar_derived -> compute other stepsizes */
1058         if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
1059                 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
1060                         int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
1061                         tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
1062                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1063                 }
1064         }
1065         /* ddA */
1066 }
1067
1068 static void j3d_write_qcd(opj_j3d_t *j3d) {
1069         int lenp, len;
1070
1071         opj_cio_t *cio = j3d->cio;
1072         
1073         cio_write(cio, J3D_MS_QCD, 2);  /* QCD */
1074         lenp = cio_tell(cio);
1075         cio_skip(cio, 2);
1076         j3d_write_qcx(j3d, 0);                  /* Sqcd*/
1077         len = cio_tell(cio) - lenp;
1078         cio_seek(cio, lenp);
1079         cio_write(cio, len, 2);                 /* Lqcd */
1080         cio_seek(cio, lenp + len);
1081 }
1082
1083 static void j3d_read_qcd(opj_j3d_t *j3d) {
1084         int len, i, pos;
1085
1086         opj_cio_t *cio = j3d->cio;
1087         opj_volume_t *volume = j3d->volume;
1088         
1089         len = cio_read(cio, 2);         /* Lqcd */
1090         pos = cio_tell(cio);
1091         for (i = 0; i < volume->numcomps; i++) {
1092                 cio_seek(cio, pos);
1093                 j3d_read_qcx(j3d, i, len - 2);
1094         }
1095 }
1096
1097 static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
1098         int lenp, len;
1099
1100         opj_cio_t *cio = j3d->cio;
1101         
1102         cio_write(cio, J3D_MS_QCC, 2);  /* QCC */
1103         lenp = cio_tell(cio);
1104         cio_skip(cio, 2);
1105         cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2);   /* Cqcc */
1106         j3d_write_qcx(j3d, compno);
1107         len = cio_tell(cio) - lenp;
1108         cio_seek(cio, lenp);
1109         cio_write(cio, len, 2);                 /* Lqcc */
1110         cio_seek(cio, lenp + len);
1111 }
1112
1113 static void j3d_read_qcc(opj_j3d_t *j3d) {
1114         int len, compno;
1115         int numcomp = j3d->volume->numcomps;
1116         opj_cio_t *cio = j3d->cio;
1117         
1118         len = cio_read(cio, 2); /* Lqcc */
1119         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1120         j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1121 }
1122
1123 static void j3d_write_poc(opj_j3d_t *j3d) {
1124         int len, numpchgs, i;
1125
1126         int numcomps = j3d->volume->numcomps;
1127         
1128         opj_cp_t *cp = j3d->cp;
1129         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1130         opj_tccp_t *tccp = &tcp->tccps[0];
1131         opj_cio_t *cio = j3d->cio;
1132
1133         numpchgs = tcp->numpocs;
1134         cio_write(cio, J3D_MS_POC, 2);  /* POC  */
1135         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1136         cio_write(cio, len, 2);         /* Lpoc */
1137         for (i = 0; i < numpchgs; i++) {
1138                 opj_poc_t *poc = &tcp->pocs[i];
1139                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1140                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
1141                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1142                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1143                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1144                 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
1145                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
1146                 poc->compno1 = int_min(poc->compno1, numcomps);
1147                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
1148         }
1149 }
1150
1151 static void j3d_read_poc(opj_j3d_t *j3d) {
1152         int len, numpchgs, i, old_poc;
1153
1154         int numcomps = j3d->volume->numcomps;
1155         
1156         opj_cp_t *cp = j3d->cp;
1157         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1158         opj_tccp_t *tccp = &tcp->tccps[0];
1159         opj_cio_t *cio = j3d->cio;
1160         
1161         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1162         tcp->POC = 1;
1163         len = cio_read(cio, 2);         /* Lpoc */
1164         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1165         
1166         for (i = old_poc; i < numpchgs + old_poc; i++) {
1167                 opj_poc_t *poc;
1168                 poc = &tcp->pocs[i];
1169                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1170                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
1171                 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
1172                 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
1173                 poc->compno1 = int_min(
1174                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
1175                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
1176         }
1177         
1178         tcp->numpocs = numpchgs + old_poc - 1;
1179 }
1180
1181 static void j3d_read_crg(opj_j3d_t *j3d) {
1182         int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
1183         
1184         opj_cio_t *cio = j3d->cio;
1185         int numcomps = j3d->volume->numcomps;
1186         
1187         len = cio_read(cio, 2);                 /* Lcrg */
1188         for (i = 0; i < numcomps; i++) {
1189                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
1190                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
1191                 Zcrg_i = cio_read(cio, 2);      /* Zcrg_i */
1192         }
1193 }
1194
1195 static void j3d_read_tlm(opj_j3d_t *j3d) {
1196         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1197         long int Ttlm_i, Ptlm_i;
1198
1199         opj_cio_t *cio = j3d->cio;
1200         
1201         len = cio_read(cio, 2);         /* Ltlm */
1202         Ztlm = cio_read(cio, 1);        /* Ztlm */
1203         Stlm = cio_read(cio, 1);        /* Stlm */
1204         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1205         SP = (Stlm >> 6) & 0x01;
1206         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1207         for (i = 0; i < tile_tlm; i++) {
1208                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
1209                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
1210         }
1211 }
1212
1213 static void j3d_read_plm(opj_j3d_t *j3d) {
1214         int len, i, Zplm, Nplm, add, packet_len = 0;
1215         
1216         opj_cio_t *cio = j3d->cio;
1217
1218         len = cio_read(cio, 2);         /* Lplm */
1219         Zplm = cio_read(cio, 1);        /* Zplm */
1220         len -= 3;
1221         while (len > 0) {
1222                 Nplm = cio_read(cio, 4);                /* Nplm */
1223                 len -= 4;
1224                 for (i = Nplm; i > 0; i--) {
1225                         add = cio_read(cio, 1);
1226                         len--;
1227                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
1228                         if ((add & 0x80) == 0) {
1229                                 /* New packet */
1230                                 packet_len = 0;
1231                         }
1232                         if (len <= 0)
1233                                 break;
1234                 }
1235         }
1236 }
1237
1238 static void j3d_read_plt(opj_j3d_t *j3d) {
1239         int len, i, Zplt, packet_len = 0, add;
1240         
1241         opj_cio_t *cio = j3d->cio;
1242         
1243         len = cio_read(cio, 2);         /* Lplt */
1244         Zplt = cio_read(cio, 1);        /* Zplt */
1245         for (i = len - 3; i > 0; i--) {
1246                 add = cio_read(cio, 1);
1247                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
1248                 if ((add & 0x80) == 0) {
1249                         /* New packet */
1250                         packet_len = 0;
1251                 }
1252         }
1253 }
1254
1255 static void j3d_read_ppm(opj_j3d_t *j3d) {
1256         int len, Z_ppm, i, j;
1257         int N_ppm;
1258
1259         opj_cp_t *cp = j3d->cp;
1260         opj_cio_t *cio = j3d->cio;
1261         
1262         len = cio_read(cio, 2);
1263         cp->ppm = 1;
1264         
1265         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
1266         len -= 3;
1267         while (len > 0) {
1268                 if (cp->ppm_previous == 0) {
1269                         N_ppm = cio_read(cio, 4);       /* N_ppm */
1270                         len -= 4;
1271                 } else {
1272                         N_ppm = cp->ppm_previous;
1273                 }
1274                 j = cp->ppm_store;
1275                 if (Z_ppm == 0) {       /* First PPM marker */
1276                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1277                         cp->ppm_data_first = cp->ppm_data;
1278                         cp->ppm_len = N_ppm;
1279                 } else {                        /* NON-first PPM marker */
1280                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
1281                         cp->ppm_data_first = cp->ppm_data;
1282                         cp->ppm_len = N_ppm + cp->ppm_store;
1283                 }
1284                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
1285                         cp->ppm_data[j] = cio_read(cio, 1);
1286                         j++;
1287                         len--;
1288                         if (len == 0)
1289                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
1290                 }
1291                 cp->ppm_previous = i - 1;
1292                 cp->ppm_store = j;
1293         }
1294 }
1295
1296 static void j3d_read_ppt(opj_j3d_t *j3d) {
1297         int len, Z_ppt, i, j = 0;
1298
1299         opj_cp_t *cp = j3d->cp;
1300         opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
1301         opj_cio_t *cio = j3d->cio;
1302
1303         len = cio_read(cio, 2);
1304         Z_ppt = cio_read(cio, 1);
1305         tcp->ppt = 1;
1306         if (Z_ppt == 0) {               /* First PPT marker */
1307                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1308                 tcp->ppt_data_first = tcp->ppt_data;
1309                 tcp->ppt_store = 0;
1310                 tcp->ppt_len = len - 3;
1311         } else {                        /* NON-first PPT marker */
1312                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1313                 tcp->ppt_data_first = tcp->ppt_data;
1314                 tcp->ppt_len = len - 3 + tcp->ppt_store;
1315         }
1316         j = tcp->ppt_store;
1317         for (i = len - 3; i > 0; i--) {
1318                 tcp->ppt_data[j] = cio_read(cio, 1);
1319                 j++;
1320         }
1321         tcp->ppt_store = j;
1322 }
1323
1324 static void j3d_write_sot(opj_j3d_t *j3d) {
1325         int lenp, len;
1326
1327         opj_cio_t *cio = j3d->cio;
1328
1329         j3d->sot_start = cio_tell(cio);
1330         cio_write(cio, J3D_MS_SOT, 2);          /* SOT */
1331         lenp = cio_tell(cio);
1332         cio_skip(cio, 2);                                       /* Lsot (further) */
1333         cio_write(cio, j3d->curtileno, 2);      /* Isot */
1334         cio_skip(cio, 4);                                       /* Psot (further in j3d_write_sod) */
1335         cio_write(cio, 0, 1);                           /* TPsot */
1336         cio_write(cio, 1, 1);                           /* TNsot (no of tile-parts of this tile in this codestream)*/
1337         len = cio_tell(cio) - lenp;
1338         cio_seek(cio, lenp);
1339         cio_write(cio, len, 2);                         /* Lsot */
1340         cio_seek(cio, lenp + len);
1341 }
1342
1343 static void j3d_read_sot(opj_j3d_t *j3d) {
1344         int len, tileno, totlen, partno, numparts, i;
1345         opj_tcp_t *tcp = NULL;
1346         char status = 0;
1347
1348         opj_cp_t *cp = j3d->cp;
1349         opj_cio_t *cio = j3d->cio;
1350         
1351         len = cio_read(cio, 2);
1352         tileno = cio_read(cio, 2);
1353         
1354         if (cp->tileno_size == 0) {
1355                 cp->tileno[cp->tileno_size] = tileno;
1356                 cp->tileno_size++;
1357         } else {
1358                 i = 0;
1359                 while (i < cp->tileno_size && status == 0) {
1360                         status = cp->tileno[i] == tileno ? 1 : 0;
1361                         i++;
1362                 }
1363                 if (status == 0) {
1364                         cp->tileno[cp->tileno_size] = tileno;
1365                         cp->tileno_size++;
1366                 }
1367         }
1368         
1369         totlen = cio_read(cio, 4);
1370         if (!totlen)
1371                 totlen = cio_numbytesleft(cio) + 8;
1372         
1373         partno = cio_read(cio, 1);
1374         numparts = cio_read(cio, 1);
1375         
1376         j3d->curtileno = tileno;
1377         j3d->eot = cio_getbp(cio) - 12 + totlen;
1378         j3d->state = J3D_STATE_TPH;
1379         tcp = &cp->tcps[j3d->curtileno];
1380         
1381         if (tcp->first == 1) {
1382                 
1383                 /* Initialization PPT */
1384                 opj_tccp_t *tmp = tcp->tccps;
1385                 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
1386                 tcp->ppt = 0;
1387                 tcp->ppt_data = NULL;
1388                 tcp->ppt_data_first = NULL;
1389                 tcp->tccps = tmp;
1390
1391                 for (i = 0; i < j3d->volume->numcomps; i++) {
1392                         tcp->tccps[i] = j3d->default_tcp->tccps[i];
1393                 }
1394                 cp->tcps[j3d->curtileno].first = 0;
1395         }
1396 }
1397
1398 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
1399         int l, layno;
1400         int totlen;
1401         opj_tcp_t *tcp = NULL;
1402         opj_volume_info_t *volume_info = NULL;
1403         
1404         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
1405         opj_cp_t *cp = j3d->cp;
1406         opj_cio_t *cio = j3d->cio;
1407         
1408         cio_write(cio, J3D_MS_SOD, 2);
1409         if (j3d->curtileno == 0) {
1410                 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
1411         }
1412         
1413         /* INDEX >> */
1414         volume_info = j3d->volume_info;
1415         if (volume_info && volume_info->index_on) {
1416                 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
1417         }
1418         /* << INDEX */
1419         
1420         tcp = &cp->tcps[j3d->curtileno];
1421         for (layno = 0; layno < tcp->numlayers; layno++) {
1422                 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
1423         }
1424         
1425         if(volume_info) {
1426                 volume_info->num = 0;
1427         }
1428
1429         l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
1430         
1431         /* Writing Psot in SOT marker */
1432         totlen = cio_tell(cio) + l - j3d->sot_start;
1433         cio_seek(cio, j3d->sot_start + 6);
1434         cio_write(cio, totlen, 4);
1435         cio_seek(cio, j3d->sot_start + totlen);
1436 }
1437
1438 static void j3d_read_sod(opj_j3d_t *j3d) {
1439         int len, truncate = 0, i;
1440         unsigned char *data = NULL, *data_ptr = NULL;
1441
1442         opj_cio_t *cio = j3d->cio;
1443         int curtileno = j3d->curtileno;
1444         
1445         len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1446         
1447         if (len == cio_numbytesleft(cio) + 1) {
1448                 truncate = 1;           /* Case of a truncate codestream */
1449         }
1450         
1451         data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
1452
1453         for (i = 0; i < j3d->tile_len[curtileno]; i++) {
1454                 data[i] = j3d->tile_data[curtileno][i];
1455         }
1456
1457         data_ptr = data + j3d->tile_len[curtileno];
1458         for (i = 0; i < len; i++) {
1459                 data_ptr[i] = cio_read(cio, 1);
1460         }
1461         
1462         j3d->tile_len[curtileno] += len;
1463         opj_free(j3d->tile_data[curtileno]);
1464         j3d->tile_data[curtileno] = data;
1465         
1466         if (!truncate) {
1467                 j3d->state = J3D_STATE_TPHSOT;
1468         } else {
1469                 j3d->state = J3D_STATE_NEOC;    /* RAJOUTE !! */
1470         }
1471 }
1472
1473 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
1474         
1475         opj_cp_t *cp = j3d->cp;
1476         opj_tcp_t *tcp = &cp->tcps[tileno];
1477         opj_cio_t *cio = j3d->cio;
1478         int numcomps = j3d->volume->numcomps;
1479         
1480         cio_write(cio, J3D_MS_RGN, 2);                                          /* RGN  */
1481         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
1482         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
1483         cio_write(cio, 0, 1);                                                           /* Srgn */
1484         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
1485 }
1486
1487 static void j3d_read_rgn(opj_j3d_t *j3d) {
1488         int len, compno, roisty;
1489
1490         opj_cp_t *cp = j3d->cp;
1491         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1492         opj_cio_t *cio = j3d->cio;
1493         int numcomps = j3d->volume->numcomps;
1494
1495         len = cio_read(cio, 2);                                                                         /* Lrgn */
1496         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
1497         roisty = cio_read(cio, 1);                                                                      /* Srgn */
1498         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
1499 }
1500
1501 static void j3d_write_eoc(opj_j3d_t *j3d) {
1502         opj_cio_t *cio = j3d->cio;
1503         /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
1504         cio_write(cio, J3D_MS_EOC, 2);
1505 }
1506
1507 static void j3d_read_eoc(opj_j3d_t *j3d) {
1508         int i, tileno;
1509
1510 #ifndef NO_PACKETS_DECODING  
1511         opj_tcd_t *tcd = tcd_create(j3d->cinfo);
1512     tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
1513         /*j3d_dump_volume(stdout, tcd->volume);
1514         j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
1515         for (i = 0; i < j3d->cp->tileno_size; i++) {
1516                 tileno = j3d->cp->tileno[i];
1517                 /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/
1518                 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
1519                 opj_free(j3d->tile_data[tileno]);
1520                 j3d->tile_data[tileno] = NULL;
1521         }
1522         tcd_free_decode(tcd);
1523         tcd_destroy(tcd);
1524 #else 
1525         for (i = 0; i < j3d->cp->tileno_size; i++) {
1526                 tileno = j3d->cp->tileno[i];
1527                 opj_free(j3d->tile_data[tileno]);
1528                 j3d->tile_data[tileno] = NULL;
1529         }
1530 #endif
1531         
1532         j3d->state = J3D_STATE_MT;
1533 }
1534
1535 static void j3d_read_unk(opj_j3d_t *j3d) {
1536         opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
1537 }
1538
1539 static opj_atk_t atk_info_wt[] = {
1540         {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/
1541         {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/
1542         {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/
1543         {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/
1544         {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/
1545         {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/
1546         {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},
1547                 {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/
1548         {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}},       /* WT 5-3 IRR*/
1549         {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}}         /* WT 13-7 REV*/
1550 };
1551
1552 typedef struct opj_dec_mstabent {
1553         /** marker value */
1554         int id;
1555         /** value of the state when the marker can appear */
1556         int states;
1557         /** action linked to the marker */
1558         void (*handler) (opj_j3d_t *j3d);
1559 } opj_dec_mstabent_t;
1560
1561 opj_dec_mstabent_t j3d_dec_mstab[] = {
1562   {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
1563   {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
1564   {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
1565   {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
1566   {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
1567   {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
1568   {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},
1569   {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
1570   {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
1571   {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
1572   {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
1573   {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
1574   {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
1575   {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
1576   {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
1577   {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
1578   {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
1579   {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
1580   {J3D_MS_SOP, 0, 0},
1581   {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
1582   {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
1583   {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
1584   {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
1585   {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
1586   /*, -->must define the j3d_read functions
1587   {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
1588   {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
1589   {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
1590   {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
1591   {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
1592   {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
1593   {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
1594   {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
1595   {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
1596   {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
1597 };
1598
1599 /**
1600 Read the lookup table containing all the marker, status and action
1601 @param id Marker value
1602 */
1603 static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
1604         opj_dec_mstabent_t *e;
1605         for (e = j3d_dec_mstab; e->id != 0; e++) {
1606                 if (e->id == id) {
1607                         break;
1608                 }
1609         }
1610         return e;
1611 }
1612
1613 /* ----------------------------------------------------------------------- */
1614 /* J3D / JPT decoder interface                                             */
1615 /* ----------------------------------------------------------------------- */
1616
1617 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
1618         opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1619         if(j3d) {
1620                 j3d->cinfo = cinfo;
1621                 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1622                 if(!j3d->default_tcp) {
1623                         opj_free(j3d);
1624                         return NULL;
1625                 }
1626         }
1627         return j3d;
1628 }
1629
1630 void j3d_destroy_decompress(opj_j3d_t *j3d) {
1631         int i = 0;
1632
1633         if(j3d->tile_len != NULL) {
1634                 opj_free(j3d->tile_len);
1635         }
1636         if(j3d->tile_data != NULL) {
1637                 opj_free(j3d->tile_data);
1638         }
1639         if(j3d->default_tcp != NULL) {
1640                 opj_tcp_t *default_tcp = j3d->default_tcp;
1641                 if(default_tcp->ppt_data_first != NULL) {
1642                         opj_free(default_tcp->ppt_data_first);
1643                 }
1644                 if(j3d->default_tcp->tccps != NULL) {
1645                         opj_free(j3d->default_tcp->tccps);
1646                 }
1647                 opj_free(j3d->default_tcp);
1648         }
1649         if(j3d->cp != NULL) {
1650                 opj_cp_t *cp = j3d->cp;
1651                 if(cp->tcps != NULL) {
1652                         for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
1653                                 if(cp->tcps[i].ppt_data_first != NULL) {
1654                                         opj_free(cp->tcps[i].ppt_data_first);
1655                                 }
1656                                 if(cp->tcps[i].tccps != NULL) {
1657                                         opj_free(cp->tcps[i].tccps);
1658                                 }
1659                         }
1660                         opj_free(cp->tcps);
1661                 }
1662                 if(cp->ppm_data_first != NULL) {
1663                         opj_free(cp->ppm_data_first);
1664                 }
1665                 if(cp->tileno != NULL) {
1666                         opj_free(cp->tileno);  
1667                 }
1668                 if(cp->comment != NULL) {
1669                         opj_free(cp->comment);
1670                 }
1671
1672                 opj_free(cp);
1673         }
1674
1675         opj_free(j3d);
1676 }
1677
1678 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
1679         if(j3d && parameters) {
1680                 /* create and initialize the coding parameters structure */
1681                 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1682                 cp->reduce[0] = parameters->cp_reduce[0];
1683                 cp->reduce[1] = parameters->cp_reduce[1];
1684                 cp->reduce[2] = parameters->cp_reduce[2];
1685                 cp->layer = parameters->cp_layer;
1686                 cp->bigendian = parameters->bigendian;
1687
1688     /* MM: Settings of the following two member variables would take
1689       place during j3d_read_com. FIXME */
1690                 cp->encoding_format = ENCOD_3EB;
1691                 cp->transform_format = TRF_2D_DWT;
1692
1693                 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
1694                 j3d->cp = cp;
1695         }
1696 }
1697
1698 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
1699         opj_volume_t *volume = NULL;
1700
1701         opj_common_ptr cinfo = j3d->cinfo;
1702
1703         j3d->cio = cio;
1704
1705         /* create an empty volume */
1706         volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
1707         j3d->volume = volume;
1708
1709         j3d->state = J3D_STATE_MHSOC;
1710         
1711         for (;;) {
1712                 opj_dec_mstabent_t *e;
1713                 int id = cio_read(cio, 2);
1714                 if (id >> 8 != 0xff) {
1715                         opj_volume_destroy(volume);
1716                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1717                         return 0;
1718                 }
1719                 e = j3d_dec_mstab_lookup(id);
1720                 /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/
1721                 if (!(j3d->state & e->states)) {
1722                         opj_volume_destroy(volume);
1723                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1724                         return 0;
1725                 }
1726                 if (e->handler) {
1727                         (*e->handler)(j3d);
1728                 }
1729                 /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/
1730                 if (j3d->state == J3D_STATE_MT) {
1731                         break;
1732                 }
1733                 if (j3d->state == J3D_STATE_NEOC) {
1734                         break;
1735                 }
1736         }
1737         if (j3d->state == J3D_STATE_NEOC) {
1738                 j3d_read_eoc(j3d);
1739         }
1740
1741         if (j3d->state != J3D_STATE_MT) {
1742                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1743         }
1744         
1745         return volume;
1746 }
1747
1748 /* ----------------------------------------------------------------------- */
1749 /* J3D encoder interface                                                       */
1750 /* ----------------------------------------------------------------------- */
1751
1752 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
1753         opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1754         if(j3d) {
1755                 j3d->cinfo = cinfo;
1756         }
1757         return j3d;
1758 }
1759
1760 void j3d_destroy_compress(opj_j3d_t *j3d) {
1761         int tileno;
1762
1763         if(!j3d) return;
1764
1765         if(j3d->volume_info != NULL) {
1766                 opj_volume_info_t *volume_info = j3d->volume_info;
1767                 if (volume_info->index_on && j3d->cp) {
1768                         opj_cp_t *cp = j3d->cp;
1769                         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1770                                 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
1771                                 opj_free(tile_info->thresh);
1772                                 opj_free(tile_info->packet);
1773                         }
1774                         opj_free(volume_info->tile);
1775                 }
1776                 opj_free(volume_info);
1777         }
1778         if(j3d->cp != NULL) {
1779                 opj_cp_t *cp = j3d->cp;
1780
1781                 if(cp->comment) {
1782                         opj_free(cp->comment);
1783                 }
1784                 if(cp->matrice) {
1785                         opj_free(cp->matrice);
1786                 }
1787                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1788                         opj_free(cp->tcps[tileno].tccps);
1789                 }
1790                 opj_free(cp->tcps);
1791                 opj_free(cp);
1792         }
1793
1794         opj_free(j3d);
1795 }
1796
1797 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
1798         int i, j, tileno, numpocs_tile;
1799         opj_cp_t *cp = NULL;
1800
1801         if(!j3d || !parameters || ! volume) {
1802                 return;
1803         }
1804
1805         /* create and initialize the coding parameters structure */
1806         cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1807
1808         /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
1809         j3d->cp = cp;
1810
1811         /* set default values for cp */
1812         cp->tw = 1;
1813         cp->th = 1;
1814         cp->tl = 1;
1815
1816         /* copy user encoding parameters */
1817         cp->disto_alloc = parameters->cp_disto_alloc;
1818         cp->fixed_alloc = parameters->cp_fixed_alloc;
1819         cp->fixed_quality = parameters->cp_fixed_quality;
1820
1821         /* transform and coding method */
1822         cp->transform_format = parameters->transform_format;
1823         cp->encoding_format = parameters->encoding_format;
1824
1825         /* mod fixed_quality */
1826         if(parameters->cp_matrice) {
1827                 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
1828                 cp->matrice = (int *) opj_malloc(array_size);
1829                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1830         } 
1831
1832         /* creation of an index file ? */
1833         cp->index_on = parameters->index_on;
1834         if(cp->index_on) {
1835                 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
1836         }
1837         
1838         /* tiles */
1839         cp->tdx = parameters->cp_tdx;
1840         cp->tdy = parameters->cp_tdy;
1841         cp->tdz = parameters->cp_tdz;
1842         /* tile offset */
1843         cp->tx0 = parameters->cp_tx0;
1844         cp->ty0 = parameters->cp_ty0;
1845         cp->tz0 = parameters->cp_tz0;
1846         /* comment string */
1847         if(parameters->cp_comment) {
1848                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1849                 if(cp->comment) {
1850                         strcpy(cp->comment, parameters->cp_comment);
1851                 }
1852         }
1853
1854         /*calculate other encoding parameters*/
1855         if (parameters->tile_size_on) {
1856                 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
1857                 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
1858                 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
1859         } else {
1860                 cp->tdx = volume->x1 - cp->tx0;
1861                 cp->tdy = volume->y1 - cp->ty0;
1862                 cp->tdz = volume->z1 - cp->tz0;
1863         }
1864
1865         /* initialize the multiple tiles */
1866         /* ---------------------------- */
1867         cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
1868
1869         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
1870                 opj_tcp_t *tcp = &cp->tcps[tileno];
1871                 tcp->numlayers = parameters->tcp_numlayers;
1872                 for (j = 0; j < tcp->numlayers; j++) {
1873                         if (cp->fixed_quality) {        /* add fixed_quality */
1874                                 tcp->distoratio[j] = parameters->tcp_distoratio[j];
1875                         } else {
1876                                 tcp->rates[j] = parameters->tcp_rates[j];
1877                         }
1878                 }
1879                 tcp->csty = parameters->csty;
1880                 tcp->prg = parameters->prog_order;
1881                 tcp->mct = volume->numcomps == 3 ? 1 : 0;
1882
1883                 numpocs_tile = 0;
1884                 tcp->POC = 0;
1885                 if (parameters->numpocs) {
1886                         /* initialisation of POC */
1887                         tcp->POC = 1;
1888                         for (i = 0; i < parameters->numpocs; i++) {
1889                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1890                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1891                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
1892                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
1893                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
1894                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
1895                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
1896                                         tcp_poc->prg            = parameters->POC[numpocs_tile].prg;
1897                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
1898                                         numpocs_tile++;
1899                                 }
1900                         }
1901                 }
1902                 tcp->numpocs = numpocs_tile;
1903
1904                 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
1905                 
1906                 for (i = 0; i < volume->numcomps; i++) {
1907                         opj_tccp_t *tccp = &tcp->tccps[i];
1908                         tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT;       /* 0 => standard precint || 1 => custom-defined precinct  */
1909                         tccp->numresolution[0] = parameters->numresolution[0];
1910                         tccp->numresolution[1] = parameters->numresolution[1];
1911                         tccp->numresolution[2] = parameters->numresolution[2];
1912                                                 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
1913                                                 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
1914                                                 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
1915                                                 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
1916                                                 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
1917                                                 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
1918                         tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]); 
1919                         tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]); 
1920                         tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]); 
1921                                                 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
1922                         tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/
1923
1924                         /*ATK / transform */
1925                         tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
1926                         for (j = 0; j < 3; j++) {
1927                                         tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
1928                         }
1929                                                 
1930                         /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
1931                         tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
1932                         tccp->numgbits = 2;
1933                         if (i == parameters->roi_compno) {
1934                                 tccp->roishift = parameters->roi_shift;
1935                         } else {
1936                                 tccp->roishift = 0;
1937                         }
1938                         /* Custom defined precints */
1939                         if (parameters->csty & J3D_CCP_CSTY_PRT) {
1940                                 int k;
1941                                 for (k = 0; k < 3; k++) {
1942                                         int p = 0;
1943                                         for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
1944                                                 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
1945                                                         if (parameters->prct_init[k][p] < 1) {
1946                                                                 tccp->prctsiz[k][j] = 1;
1947                                                         } else {
1948                                                                 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
1949                                                         }
1950                                                 } else {
1951                                                         int res_spec = parameters->res_spec;
1952                                                         int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
1953                                                         if (size_prct < 1) {
1954                                                                 tccp->prctsiz[k][j] = 1;
1955                                                         } else {
1956                                                                 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
1957                                                         }
1958                                                 }
1959                                         }
1960                                         p++;
1961                                 }
1962                         } else {
1963                                 int k;
1964                                 for (k = 0; k < 3; k++) {
1965                     for (j = 0; j < tccp->numresolution[k]; j++) {
1966                         tccp->prctsiz[k][j] = 15;
1967                                         }
1968                                 }
1969                         }
1970                         /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/
1971                         dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
1972                 }
1973         }
1974 }
1975
1976 /**
1977 Create an index file
1978 @param j3d
1979 @param cio
1980 @param volume_info
1981 @param index Index filename
1982 @return Returns 1 if successful, returns 0 otherwise
1983 */
1984 static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
1985         
1986         int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
1987         FILE *stream = NULL;
1988         double total_disto = 0;
1989
1990         volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction;     /* Correction 14/4/03 suite rmq de Patrick */
1991
1992         stream = fopen(index, "w");
1993         if (!stream) {
1994                 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
1995                 return 0;
1996         }
1997         
1998         fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
1999         fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
2000         fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
2001         fprintf(stream, "PROG\t%d\n", volume_info->prog);
2002         fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
2003         fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
2004         fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
2005         fprintf(stream, "LAYER\t%d\n", volume_info->layer);
2006         fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
2007         
2008         fprintf(stream, "Precint sizes for each resolution:\n");
2009         for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
2010                 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
2011                         (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno]));   /* based on tile 0 */
2012         }
2013         fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
2014         fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
2015         fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
2016         for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2017                 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
2018                         volume_info->tile[tileno].num_tile,
2019                         volume_info->tile[tileno].start_pos,
2020                         volume_info->tile[tileno].end_header,
2021                         volume_info->tile[tileno].end_pos,
2022                         volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
2023                         volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
2024         }
2025         
2026         for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2027                 int start_pos, end_pos;
2028                 double disto = 0;
2029                 pack_nb = 0;
2030                 if (volume_info->prog == LRCP) {        /* LRCP */
2031                         fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto\n");
2032                         for (layno = 0; layno < volume_info->layer; layno++) {
2033                                 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2034                                         for (compno = 0; compno < volume_info->comp; compno++) {
2035                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2036                                                 for (precno = 0; precno < prec_max; precno++) {
2037                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2038                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2039                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
2040                                                         fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2041                                                         total_disto += disto;
2042                                                         pack_nb++;
2043                                                 }
2044                                         }
2045                                 }
2046                         }
2047                 } /* LRCP */
2048                 else if (volume_info->prog == RLCP) {   /* RLCP */
2049                         /*
2050                         fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");
2051                         */
2052                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2053                                 for (layno = 0; layno < volume_info->layer; layno++) {
2054                                         for (compno = 0; compno < volume_info->comp; compno++) {
2055                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
2056                                                 for (precno = 0; precno < prec_max; precno++) {
2057                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2058                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2059                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
2060                                                         fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2061                                                                 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2062                                                         total_disto += disto;
2063                                                         pack_nb++;
2064                                                 }
2065                                         }
2066                                 }
2067                         }
2068                 } /* RLCP */
2069                 else if (volume_info->prog == RPCL) {   /* RPCL */
2070                         /*
2071                         fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n"); 
2072                         */
2073                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2074                                 /* I suppose components have same XRsiz, YRsiz */
2075                                 /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/
2076                                 /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/
2077                                 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
2078                                 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
2079                                 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
2080                                 int x1 = x0 + volume_info->tile_x;
2081                                 int y1 = y0 + volume_info->tile_y;
2082                                 int z1 = z0 + volume_info->tile_z;
2083                                 for(z = z0; z < z1; z++) {
2084                                         for(y = y0; y < y1; y++) {
2085                                                 for(x = x0; x < x1; x++) {
2086                                                         for (compno = 0; compno < volume_info->comp; compno++) {
2087                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2088                                                                 for (precno = 0; precno < prec_max; precno++) {
2089                                                                         int pcnx = volume_info->tile[tileno].prctno[0][resno];
2090                                                                         int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2091                                                                         int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2092                                                                         int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2093                                                                         int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2094                                                                         int precno_y = (int) floor( (float)precno/(float)pcnx );
2095                                                                         if (precno_y*pcy == y ) {
2096                                                                                 if (precno_x*pcx == x ) {
2097                                                                                         for (layno = 0; layno < volume_info->layer; layno++) {
2098                                                                                                 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2099                                                                                                 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2100                                                                                                 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2101                                                                                                 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2102                                                                                                         pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); 
2103                                                                                                 total_disto += disto;
2104                                                                                                 pack_nb++; 
2105                                                                                         }
2106                                                                                 }
2107                                                                         }
2108                                                                 } /* precno */
2109                                                         } /* compno */
2110                                                 } /* x = x0..x1 */
2111                                         } /* y = y0..y1 */
2112                                 } /* z = z0..z1 */
2113                         } /* resno */
2114                 } /* RPCL */
2115                 else if (volume_info->prog == PCRL) {   /* PCRL */
2116                         /* I suppose components have same XRsiz, YRsiz */
2117                         int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2118                         int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2119                         int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2120                         int x1 = x0 + volume_info->tile_x;
2121                         int y1 = y0 + volume_info->tile_y;
2122                         int z1 = z0 + volume_info->tile_z;
2123                         /*
2124                         fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n"); 
2125                         */
2126                         for(z = z0; z < z1; z++) {
2127                                 for(y = y0; y < y1; y++) {
2128                                         for(x = x0; x < x1; x++) {
2129                                                 for (compno = 0; compno < volume_info->comp; compno++) {
2130                                                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2131                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
2132                                                                 for (precno = 0; precno < prec_max; precno++) {
2133                                                                 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2134                                                                 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2135                                                                 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2136                                                                 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2137                                                                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2138                                                                 int precno_y = (int) floor( (float)precno/(float)pcnx );
2139                                                                 int precno_z = (int) floor( (float)precno/(float)pcnx );
2140                                                                 if (precno_z*pcz == z ) {
2141                                                                         if (precno_y*pcy == y ) {
2142                                                                                 if (precno_x*pcx == x ) {
2143                                                                                         for (layno = 0; layno < volume_info->layer; layno++) {
2144                                                                                                 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2145                                                                                                 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2146                                                                                                 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2147                                                                                                 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2148                                                                                                         pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); 
2149                                                                                                 total_disto += disto;
2150                                                                                                 pack_nb++; 
2151                                                                                         }
2152                                                                                 }
2153                                                                         }
2154                                                                 }
2155                                                         } /* precno */
2156                                                 } /* resno */
2157                                         } /* compno */
2158                                 } /* x = x0..x1 */
2159                         } /* y = y0..y1 */
2160                         }
2161                 } /* PCRL */
2162                 else {  /* CPRL */
2163                         /*
2164                         fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n"); 
2165                         */
2166                         for (compno = 0; compno < volume_info->comp; compno++) {
2167                                 /* I suppose components have same XRsiz, YRsiz */
2168                                 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2169                                 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2170                                 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2171                                 int x1 = x0 + volume_info->tile_x;
2172                                 int y1 = y0 + volume_info->tile_y;
2173                                 int z1 = z0 + volume_info->tile_z;
2174                                 for(z = z0; z < z1; z++) {
2175                                         for(y = y0; y < y1; y++) {
2176                                                 for(x = x0; x < x1; x++) {
2177                                                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2178                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2179                                                                 for (precno = 0; precno < prec_max; precno++) {
2180                                                                         int pcnx = volume_info->tile[tileno].prctno[0][resno];
2181                                                                         int pcny = volume_info->tile[tileno].prctno[1][resno];
2182                                                                         int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2183                                                                         int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2184                                                                         int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2185                                                                         int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2186                                                                         int precno_y = (int) floor( (float)precno/(float)pcnx );
2187                                                                         int precno_z = 0; /*???*/
2188                                                                         if (precno_z*pcz == z ) {
2189                                                                                 if (precno_y*pcy == y ) {
2190                                                                                         if (precno_x*pcx == x ) {
2191                                                                                                 for (layno = 0; layno < volume_info->layer; layno++) {
2192                                                                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2193                                                                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2194                                                                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
2195                                                                                                         fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2196                                                                                                                 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); 
2197                                                                                                         total_disto += disto;
2198                                                                                                         pack_nb++; 
2199                                                                                                 }
2200                                                                                         }
2201                                                                                 }
2202                                                                         }
2203                                                                 } /* precno */
2204                                                         } /* resno */
2205                                                 } /* x = x0..x1 */
2206                                         } /* y = y0..y1 */
2207                                 } /* z = z0..z1 */
2208                         } /* comno */
2209                 } /* CPRL */   
2210         } /* tileno */
2211         
2212         fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max);   /* SE max */
2213         fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto);                       /* SE totale */
2214         
2215
2216         fclose(stream);
2217
2218         return 1;
2219 }
2220
2221 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
2222         int tileno, compno;
2223         opj_volume_info_t *volume_info = NULL;
2224         opj_cp_t *cp = NULL;
2225         opj_tcd_t *tcd = NULL;  /* TCD component */
2226
2227         j3d->cio = cio; 
2228         j3d->volume = volume;
2229         cp = j3d->cp;
2230
2231         /*j3d_dump_volume(stdout, volume);
2232         j3d_dump_cp(stdout, volume, cp);*/
2233
2234         /* INDEX >> */
2235         volume_info = j3d->volume_info;
2236         if (volume_info && cp->index_on) {
2237                 volume_info->index_on = cp->index_on;
2238                 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
2239                 volume_info->volume_w = volume->x1 - volume->x0;
2240                 volume_info->volume_h = volume->y1 - volume->y0;
2241                 volume_info->volume_l = volume->z1 - volume->z0;
2242                 volume_info->prog = (&cp->tcps[0])->prg;
2243                 volume_info->tw = cp->tw;
2244                 volume_info->th = cp->th;
2245                 volume_info->tl = cp->tl;
2246                 volume_info->tile_x = cp->tdx;  /* new version parser */
2247                 volume_info->tile_y = cp->tdy;  /* new version parser */
2248                 volume_info->tile_z = cp->tdz;  /* new version parser */
2249                 volume_info->tile_Ox = cp->tx0; /* new version parser */
2250                 volume_info->tile_Oy = cp->ty0; /* new version parser */
2251                 volume_info->tile_Oz = cp->tz0; /* new version parser */
2252                 volume_info->transform_format = cp->transform_format;
2253                 volume_info->encoding_format = cp->encoding_format;
2254                 volume_info->comp = volume->numcomps;
2255                 volume_info->layer = (&cp->tcps[0])->numlayers;
2256                 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
2257                 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
2258                 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
2259                 volume_info->D_max = 0;         /* ADD Marcela */
2260         }
2261         /* << INDEX */
2262
2263         j3d_write_soc(j3d);
2264         j3d_write_siz(j3d);
2265         if (j3d->cinfo->codec_format == CODEC_J3D) {
2266                 j3d_write_cap(j3d);
2267                 j3d_write_nsi(j3d);
2268         }
2269
2270         /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/
2271                 j3d_write_com(j3d);
2272
2273         j3d_write_cod(j3d);
2274         j3d_write_qcd(j3d);
2275         for (compno = 0; compno < volume->numcomps; compno++) {
2276                 opj_tcp_t *tcp = &cp->tcps[0];
2277                 if (tcp->tccps[compno].roishift)
2278                         j3d_write_rgn(j3d, compno, 0);                  
2279         }
2280         /*Optional 15444-2 markers*/
2281         if (j3d->cp->tcps->tccps[0].atk != NULL)
2282         j3d_write_atk(j3d);
2283         if (j3d->volume->comps[0].dcoffset != 0)
2284         j3d_write_dco(j3d);
2285
2286         /* INDEX >> */
2287         if(volume_info && volume_info->index_on) {
2288                 volume_info->main_head_end = cio_tell(cio) - 1;
2289         }
2290         /* << INDEX */
2291
2292         /* create the tile encoder */
2293         tcd = tcd_create(j3d->cinfo);
2294
2295         /* encode each tile */
2296         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
2297                 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
2298                 
2299                 j3d->curtileno = tileno;
2300
2301                 /* initialisation before tile encoding  */
2302                 if (tileno == 0) {      
2303                         tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
2304                 } else {
2305                         tcd_init_encode(tcd, volume, cp, j3d->curtileno);
2306                 }
2307                 
2308                 /* INDEX >> */
2309                 if(volume_info && volume_info->index_on) {
2310                         volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
2311                         volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
2312                 }
2313                 /* << INDEX */
2314                 
2315                 j3d_write_sot(j3d);
2316         
2317                 for (compno = 1; compno < volume->numcomps; compno++) {
2318                         j3d_write_coc(j3d, compno);
2319                         j3d_write_qcc(j3d, compno);
2320                 }
2321
2322                 if (cp->tcps[tileno].numpocs) {
2323                         j3d_write_poc(j3d);
2324                 }
2325                 j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/
2326
2327                 /* INDEX >> */
2328                 if(volume_info && volume_info->index_on) {
2329                         volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
2330                 }
2331                 /* << INDEX */          
2332         }
2333         
2334         /* destroy the tile encoder */
2335         tcd_free_encode(tcd);
2336         tcd_destroy(tcd);
2337
2338         j3d_write_eoc(j3d);
2339         
2340         /* Creation of the index file */
2341         if(volume_info && volume_info->index_on) {
2342                 if(!j3d_create_index(j3d, cio, volume_info, index)) {
2343                         opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2344                         return false;
2345                 }
2346         }
2347           
2348         return true;
2349 }
2350
2351 /* ----------------------------------------------------------------------- */
2352 /*@}*/
2353
2354 /*@}*/
2355