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