Merge branch 'master' into coc-qcc
[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         numbands = 0; // compiler warning
1012         if (j3d->cinfo->codec_format == CODEC_J2K)
1013         numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2; 
1014         else if (j3d->cinfo->codec_format == CODEC_J3D) {
1015                 int diff = tccp->numresolution[0] - tccp->numresolution[2];
1016         numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
1017         }
1018         
1019         for (bandno = 0; bandno < numbands; bandno++) {
1020                 expn = tccp->stepsizes[bandno].expn;
1021                 mant = tccp->stepsizes[bandno].mant;
1022                 
1023                 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1024                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
1025                 } else {
1026                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1027                 }
1028         }
1029 }
1030
1031 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
1032         int tmp;
1033         int bandno, numbands;
1034
1035         opj_cp_t *cp = j3d->cp;
1036         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1037         opj_tccp_t *tccp = &tcp->tccps[compno];
1038         opj_cio_t *cio = j3d->cio;
1039
1040         tmp = cio_read(cio, 1);         /* Sqcx */
1041         tccp->qntsty = tmp & 0x1f;
1042         tccp->numgbits = tmp >> 5;
1043
1044         /*Numbands = 1                          si SIQNT
1045                              len - 1            si NOQNT
1046                                  (len - 1) / 2  si SEQNT */
1047         numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1048
1049         for (bandno = 0; bandno < numbands; bandno++) {
1050                 int expn, mant;
1051                 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1052                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
1053                         mant = 0;
1054                 } else {
1055                         tmp = cio_read(cio, 2);                 /* SPqcx_i */
1056                         expn = tmp >> 11;
1057                         mant = tmp & 0x7ff;
1058                 }
1059                 tccp->stepsizes[bandno].expn = expn;
1060                 tccp->stepsizes[bandno].mant = mant;
1061         }
1062         
1063         /* Add Antonin : if scalar_derived -> compute other stepsizes */
1064         if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
1065                 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
1066                         int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
1067                         tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
1068                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1069                 }
1070         }
1071         /* ddA */
1072 }
1073
1074 static void j3d_write_qcd(opj_j3d_t *j3d) {
1075         int lenp, len;
1076
1077         opj_cio_t *cio = j3d->cio;
1078         
1079         cio_write(cio, J3D_MS_QCD, 2);  /* QCD */
1080         lenp = cio_tell(cio);
1081         cio_skip(cio, 2);
1082         j3d_write_qcx(j3d, 0);                  /* Sqcd*/
1083         len = cio_tell(cio) - lenp;
1084         cio_seek(cio, lenp);
1085         cio_write(cio, len, 2);                 /* Lqcd */
1086         cio_seek(cio, lenp + len);
1087 }
1088
1089 static void j3d_read_qcd(opj_j3d_t *j3d) {
1090         int len, i, pos;
1091
1092         opj_cio_t *cio = j3d->cio;
1093         opj_volume_t *volume = j3d->volume;
1094         
1095         len = cio_read(cio, 2);         /* Lqcd */
1096         pos = cio_tell(cio);
1097         for (i = 0; i < volume->numcomps; i++) {
1098                 cio_seek(cio, pos);
1099                 j3d_read_qcx(j3d, i, len - 2);
1100         }
1101 }
1102
1103 static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
1104         int lenp, len;
1105
1106         opj_cio_t *cio = j3d->cio;
1107         
1108         cio_write(cio, J3D_MS_QCC, 2);  /* QCC */
1109         lenp = cio_tell(cio);
1110         cio_skip(cio, 2);
1111         cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2);   /* Cqcc */
1112         j3d_write_qcx(j3d, compno);
1113         len = cio_tell(cio) - lenp;
1114         cio_seek(cio, lenp);
1115         cio_write(cio, len, 2);                 /* Lqcc */
1116         cio_seek(cio, lenp + len);
1117 }
1118
1119 static void j3d_read_qcc(opj_j3d_t *j3d) {
1120         int len, compno;
1121         int numcomp = j3d->volume->numcomps;
1122         opj_cio_t *cio = j3d->cio;
1123         
1124         len = cio_read(cio, 2); /* Lqcc */
1125         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1126         j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1127 }
1128
1129 static void j3d_write_poc(opj_j3d_t *j3d) {
1130         int len, numpchgs, i;
1131
1132         int numcomps = j3d->volume->numcomps;
1133         
1134         opj_cp_t *cp = j3d->cp;
1135         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1136         opj_tccp_t *tccp = &tcp->tccps[0];
1137         opj_cio_t *cio = j3d->cio;
1138
1139         numpchgs = tcp->numpocs;
1140         cio_write(cio, J3D_MS_POC, 2);  /* POC  */
1141         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1142         cio_write(cio, len, 2);         /* Lpoc */
1143         for (i = 0; i < numpchgs; i++) {
1144                 opj_poc_t *poc = &tcp->pocs[i];
1145                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1146                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
1147                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1148                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1149                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1150                 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
1151                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
1152                 poc->compno1 = int_min(poc->compno1, numcomps);
1153                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
1154         }
1155 }
1156
1157 static void j3d_read_poc(opj_j3d_t *j3d) {
1158         int len, numpchgs, i, old_poc;
1159
1160         int numcomps = j3d->volume->numcomps;
1161         
1162         opj_cp_t *cp = j3d->cp;
1163         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1164         opj_tccp_t *tccp = &tcp->tccps[0];
1165         opj_cio_t *cio = j3d->cio;
1166         
1167         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1168         tcp->POC = 1;
1169         len = cio_read(cio, 2);         /* Lpoc */
1170         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1171         
1172         for (i = old_poc; i < numpchgs + old_poc; i++) {
1173                 opj_poc_t *poc;
1174                 poc = &tcp->pocs[i];
1175                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1176                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
1177                 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
1178                 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
1179                 poc->compno1 = int_min(
1180                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
1181                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
1182         }
1183         
1184         tcp->numpocs = numpchgs + old_poc - 1;
1185 }
1186
1187 static void j3d_read_crg(opj_j3d_t *j3d) {
1188         int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
1189         
1190         opj_cio_t *cio = j3d->cio;
1191         int numcomps = j3d->volume->numcomps;
1192         
1193         len = cio_read(cio, 2);                 /* Lcrg */
1194         for (i = 0; i < numcomps; i++) {
1195                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
1196                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
1197                 Zcrg_i = cio_read(cio, 2);      /* Zcrg_i */
1198         }
1199 }
1200
1201 static void j3d_read_tlm(opj_j3d_t *j3d) {
1202         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1203         long int Ttlm_i, Ptlm_i;
1204
1205         opj_cio_t *cio = j3d->cio;
1206         
1207         len = cio_read(cio, 2);         /* Ltlm */
1208         Ztlm = cio_read(cio, 1);        /* Ztlm */
1209         Stlm = cio_read(cio, 1);        /* Stlm */
1210         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1211         SP = (Stlm >> 6) & 0x01;
1212         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1213         for (i = 0; i < tile_tlm; i++) {
1214                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
1215                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
1216         }
1217 }
1218
1219 static void j3d_read_plm(opj_j3d_t *j3d) {
1220         int len, i, Zplm, Nplm, add, packet_len = 0;
1221         
1222         opj_cio_t *cio = j3d->cio;
1223
1224         len = cio_read(cio, 2);         /* Lplm */
1225         Zplm = cio_read(cio, 1);        /* Zplm */
1226         len -= 3;
1227         while (len > 0) {
1228                 Nplm = cio_read(cio, 4);                /* Nplm */
1229                 len -= 4;
1230                 for (i = Nplm; i > 0; i--) {
1231                         add = cio_read(cio, 1);
1232                         len--;
1233                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
1234                         if ((add & 0x80) == 0) {
1235                                 /* New packet */
1236                                 packet_len = 0;
1237                         }
1238                         if (len <= 0)
1239                                 break;
1240                 }
1241         }
1242 }
1243
1244 static void j3d_read_plt(opj_j3d_t *j3d) {
1245         int len, i, Zplt, packet_len = 0, add;
1246         
1247         opj_cio_t *cio = j3d->cio;
1248         
1249         len = cio_read(cio, 2);         /* Lplt */
1250         Zplt = cio_read(cio, 1);        /* Zplt */
1251         for (i = len - 3; i > 0; i--) {
1252                 add = cio_read(cio, 1);
1253                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
1254                 if ((add & 0x80) == 0) {
1255                         /* New packet */
1256                         packet_len = 0;
1257                 }
1258         }
1259 }
1260
1261 static void j3d_read_ppm(opj_j3d_t *j3d) {
1262         int len, Z_ppm, i, j;
1263         int N_ppm;
1264
1265         opj_cp_t *cp = j3d->cp;
1266         opj_cio_t *cio = j3d->cio;
1267         
1268         len = cio_read(cio, 2);
1269         cp->ppm = 1;
1270         
1271         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
1272         len -= 3;
1273         while (len > 0) {
1274                 if (cp->ppm_previous == 0) {
1275                         N_ppm = cio_read(cio, 4);       /* N_ppm */
1276                         len -= 4;
1277                 } else {
1278                         N_ppm = cp->ppm_previous;
1279                 }
1280                 j = cp->ppm_store;
1281                 if (Z_ppm == 0) {       /* First PPM marker */
1282                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1283                         cp->ppm_data_first = cp->ppm_data;
1284                         cp->ppm_len = N_ppm;
1285                 } else {                        /* NON-first PPM marker */
1286                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
1287                         cp->ppm_data_first = cp->ppm_data;
1288                         cp->ppm_len = N_ppm + cp->ppm_store;
1289                 }
1290                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
1291                         cp->ppm_data[j] = cio_read(cio, 1);
1292                         j++;
1293                         len--;
1294                         if (len == 0)
1295                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
1296                 }
1297                 cp->ppm_previous = i - 1;
1298                 cp->ppm_store = j;
1299         }
1300 }
1301
1302 static void j3d_read_ppt(opj_j3d_t *j3d) {
1303         int len, Z_ppt, i, j = 0;
1304
1305         opj_cp_t *cp = j3d->cp;
1306         opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
1307         opj_cio_t *cio = j3d->cio;
1308
1309         len = cio_read(cio, 2);
1310         Z_ppt = cio_read(cio, 1);
1311         tcp->ppt = 1;
1312         if (Z_ppt == 0) {               /* First PPT marker */
1313                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1314                 tcp->ppt_data_first = tcp->ppt_data;
1315                 tcp->ppt_store = 0;
1316                 tcp->ppt_len = len - 3;
1317         } else {                        /* NON-first PPT marker */
1318                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1319                 tcp->ppt_data_first = tcp->ppt_data;
1320                 tcp->ppt_len = len - 3 + tcp->ppt_store;
1321         }
1322         j = tcp->ppt_store;
1323         for (i = len - 3; i > 0; i--) {
1324                 tcp->ppt_data[j] = cio_read(cio, 1);
1325                 j++;
1326         }
1327         tcp->ppt_store = j;
1328 }
1329
1330 static void j3d_write_sot(opj_j3d_t *j3d) {
1331         int lenp, len;
1332
1333         opj_cio_t *cio = j3d->cio;
1334
1335         j3d->sot_start = cio_tell(cio);
1336         cio_write(cio, J3D_MS_SOT, 2);          /* SOT */
1337         lenp = cio_tell(cio);
1338         cio_skip(cio, 2);                                       /* Lsot (further) */
1339         cio_write(cio, j3d->curtileno, 2);      /* Isot */
1340         cio_skip(cio, 4);                                       /* Psot (further in j3d_write_sod) */
1341         cio_write(cio, 0, 1);                           /* TPsot */
1342         cio_write(cio, 1, 1);                           /* TNsot (no of tile-parts of this tile in this codestream)*/
1343         len = cio_tell(cio) - lenp;
1344         cio_seek(cio, lenp);
1345         cio_write(cio, len, 2);                         /* Lsot */
1346         cio_seek(cio, lenp + len);
1347 }
1348
1349 static void j3d_read_sot(opj_j3d_t *j3d) {
1350         int len, tileno, totlen, partno, numparts, i;
1351         opj_tcp_t *tcp = NULL;
1352         char status = 0;
1353
1354         opj_cp_t *cp = j3d->cp;
1355         opj_cio_t *cio = j3d->cio;
1356         
1357         len = cio_read(cio, 2);
1358         tileno = cio_read(cio, 2);
1359         
1360         if (cp->tileno_size == 0) {
1361                 cp->tileno[cp->tileno_size] = tileno;
1362                 cp->tileno_size++;
1363         } else {
1364                 i = 0;
1365                 while (i < cp->tileno_size && status == 0) {
1366                         status = cp->tileno[i] == tileno ? 1 : 0;
1367                         i++;
1368                 }
1369                 if (status == 0) {
1370                         cp->tileno[cp->tileno_size] = tileno;
1371                         cp->tileno_size++;
1372                 }
1373         }
1374         
1375         totlen = cio_read(cio, 4);
1376         if (!totlen)
1377                 totlen = cio_numbytesleft(cio) + 8;
1378         
1379         partno = cio_read(cio, 1);
1380         numparts = cio_read(cio, 1);
1381         
1382         j3d->curtileno = tileno;
1383         j3d->eot = cio_getbp(cio) - 12 + totlen;
1384         j3d->state = J3D_STATE_TPH;
1385         tcp = &cp->tcps[j3d->curtileno];
1386         
1387         if (tcp->first == 1) {
1388                 
1389                 /* Initialization PPT */
1390                 opj_tccp_t *tmp = tcp->tccps;
1391                 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
1392                 tcp->ppt = 0;
1393                 tcp->ppt_data = NULL;
1394                 tcp->ppt_data_first = NULL;
1395                 tcp->tccps = tmp;
1396
1397                 for (i = 0; i < j3d->volume->numcomps; i++) {
1398                         tcp->tccps[i] = j3d->default_tcp->tccps[i];
1399                 }
1400                 cp->tcps[j3d->curtileno].first = 0;
1401         }
1402 }
1403
1404 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
1405         int l, layno;
1406         int totlen;
1407         opj_tcp_t *tcp = NULL;
1408         opj_volume_info_t *volume_info = NULL;
1409         
1410         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
1411         opj_cp_t *cp = j3d->cp;
1412         opj_cio_t *cio = j3d->cio;
1413         
1414         cio_write(cio, J3D_MS_SOD, 2);
1415         if (j3d->curtileno == 0) {
1416                 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
1417         }
1418         
1419         /* INDEX >> */
1420         volume_info = j3d->volume_info;
1421         if (volume_info && volume_info->index_on) {
1422                 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
1423         }
1424         /* << INDEX */
1425         
1426         tcp = &cp->tcps[j3d->curtileno];
1427         for (layno = 0; layno < tcp->numlayers; layno++) {
1428                 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
1429         }
1430         
1431         if(volume_info) {
1432                 volume_info->num = 0;
1433         }
1434
1435         l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
1436         
1437         /* Writing Psot in SOT marker */
1438         totlen = cio_tell(cio) + l - j3d->sot_start;
1439         cio_seek(cio, j3d->sot_start + 6);
1440         cio_write(cio, totlen, 4);
1441         cio_seek(cio, j3d->sot_start + totlen);
1442 }
1443
1444 static void j3d_read_sod(opj_j3d_t *j3d) {
1445         int len, truncate = 0, i;
1446         unsigned char *data = NULL, *data_ptr = NULL;
1447
1448         opj_cio_t *cio = j3d->cio;
1449         int curtileno = j3d->curtileno;
1450         
1451         len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1452         
1453         if (len == cio_numbytesleft(cio) + 1) {
1454                 truncate = 1;           /* Case of a truncate codestream */
1455         }
1456         
1457         data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
1458
1459         for (i = 0; i < j3d->tile_len[curtileno]; i++) {
1460                 data[i] = j3d->tile_data[curtileno][i];
1461         }
1462
1463         data_ptr = data + j3d->tile_len[curtileno];
1464         for (i = 0; i < len; i++) {
1465                 data_ptr[i] = cio_read(cio, 1);
1466         }
1467         
1468         j3d->tile_len[curtileno] += len;
1469         opj_free(j3d->tile_data[curtileno]);
1470         j3d->tile_data[curtileno] = data;
1471         
1472         if (!truncate) {
1473                 j3d->state = J3D_STATE_TPHSOT;
1474         } else {
1475                 j3d->state = J3D_STATE_NEOC;    /* RAJOUTE !! */
1476         }
1477 }
1478
1479 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
1480         
1481         opj_cp_t *cp = j3d->cp;
1482         opj_tcp_t *tcp = &cp->tcps[tileno];
1483         opj_cio_t *cio = j3d->cio;
1484         int numcomps = j3d->volume->numcomps;
1485         
1486         cio_write(cio, J3D_MS_RGN, 2);                                          /* RGN  */
1487         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
1488         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
1489         cio_write(cio, 0, 1);                                                           /* Srgn */
1490         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
1491 }
1492
1493 static void j3d_read_rgn(opj_j3d_t *j3d) {
1494         int len, compno, roisty;
1495
1496         opj_cp_t *cp = j3d->cp;
1497         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1498         opj_cio_t *cio = j3d->cio;
1499         int numcomps = j3d->volume->numcomps;
1500
1501         len = cio_read(cio, 2);                                                                         /* Lrgn */
1502         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
1503         roisty = cio_read(cio, 1);                                                                      /* Srgn */
1504         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
1505 }
1506
1507 static void j3d_write_eoc(opj_j3d_t *j3d) {
1508         opj_cio_t *cio = j3d->cio;
1509         /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
1510         cio_write(cio, J3D_MS_EOC, 2);
1511 }
1512
1513 static void j3d_read_eoc(opj_j3d_t *j3d) {
1514         int i, tileno;
1515
1516 #ifndef NO_PACKETS_DECODING  
1517         opj_tcd_t *tcd = tcd_create(j3d->cinfo);
1518     tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
1519         /*j3d_dump_volume(stdout, tcd->volume);
1520         j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
1521         for (i = 0; i < j3d->cp->tileno_size; i++) {
1522                 tileno = j3d->cp->tileno[i];
1523                 /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/
1524                 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
1525                 opj_free(j3d->tile_data[tileno]);
1526                 j3d->tile_data[tileno] = NULL;
1527         }
1528         tcd_free_decode(tcd);
1529         tcd_destroy(tcd);
1530 #else 
1531         for (i = 0; i < j3d->cp->tileno_size; i++) {
1532                 tileno = j3d->cp->tileno[i];
1533                 opj_free(j3d->tile_data[tileno]);
1534                 j3d->tile_data[tileno] = NULL;
1535         }
1536 #endif
1537         
1538         j3d->state = J3D_STATE_MT;
1539 }
1540
1541 static void j3d_read_unk(opj_j3d_t *j3d) {
1542         opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
1543 }
1544
1545 static opj_atk_t atk_info_wt[] = {
1546         {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*/
1547         {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*/
1548         {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*/
1549         {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*/
1550         {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*/
1551         {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*/
1552         {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},
1553                 {-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*/
1554         {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*/
1555         {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*/
1556 };
1557
1558 typedef struct opj_dec_mstabent {
1559         /** marker value */
1560         int id;
1561         /** value of the state when the marker can appear */
1562         int states;
1563         /** action linked to the marker */
1564         void (*handler) (opj_j3d_t *j3d);
1565 } opj_dec_mstabent_t;
1566
1567 opj_dec_mstabent_t j3d_dec_mstab[] = {
1568   {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
1569   {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
1570   {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
1571   {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
1572   {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
1573   {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
1574   {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},
1575   {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
1576   {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
1577   {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
1578   {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
1579   {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
1580   {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
1581   {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
1582   {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
1583   {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
1584   {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
1585   {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
1586   {J3D_MS_SOP, 0, 0},
1587   {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
1588   {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
1589   {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
1590   {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
1591   {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
1592   /*, -->must define the j3d_read functions
1593   {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
1594   {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
1595   {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
1596   {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
1597   {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
1598   {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
1599   {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
1600   {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
1601   {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
1602   {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
1603 };
1604
1605 /**
1606 Read the lookup table containing all the marker, status and action
1607 @param id Marker value
1608 */
1609 static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
1610         opj_dec_mstabent_t *e;
1611         for (e = j3d_dec_mstab; e->id != 0; e++) {
1612                 if (e->id == id) {
1613                         break;
1614                 }
1615         }
1616         return e;
1617 }
1618
1619 /* ----------------------------------------------------------------------- */
1620 /* J3D / JPT decoder interface                                             */
1621 /* ----------------------------------------------------------------------- */
1622
1623 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
1624         opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1625         if(j3d) {
1626                 j3d->cinfo = cinfo;
1627                 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1628                 if(!j3d->default_tcp) {
1629                         opj_free(j3d);
1630                         return NULL;
1631                 }
1632         }
1633         return j3d;
1634 }
1635
1636 void j3d_destroy_decompress(opj_j3d_t *j3d) {
1637         int i = 0;
1638
1639         if(j3d->tile_len != NULL) {
1640                 opj_free(j3d->tile_len);
1641         }
1642         if(j3d->tile_data != NULL) {
1643                 opj_free(j3d->tile_data);
1644         }
1645         if(j3d->default_tcp != NULL) {
1646                 opj_tcp_t *default_tcp = j3d->default_tcp;
1647                 if(default_tcp->ppt_data_first != NULL) {
1648                         opj_free(default_tcp->ppt_data_first);
1649                 }
1650                 if(j3d->default_tcp->tccps != NULL) {
1651                         opj_free(j3d->default_tcp->tccps);
1652                 }
1653                 opj_free(j3d->default_tcp);
1654         }
1655         if(j3d->cp != NULL) {
1656                 opj_cp_t *cp = j3d->cp;
1657                 if(cp->tcps != NULL) {
1658                         for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
1659                                 if(cp->tcps[i].ppt_data_first != NULL) {
1660                                         opj_free(cp->tcps[i].ppt_data_first);
1661                                 }
1662                                 if(cp->tcps[i].tccps != NULL) {
1663                                         opj_free(cp->tcps[i].tccps);
1664                                 }
1665                         }
1666                         opj_free(cp->tcps);
1667                 }
1668                 if(cp->ppm_data_first != NULL) {
1669                         opj_free(cp->ppm_data_first);
1670                 }
1671                 if(cp->tileno != NULL) {
1672                         opj_free(cp->tileno);  
1673                 }
1674                 if(cp->comment != NULL) {
1675                         opj_free(cp->comment);
1676                 }
1677
1678                 opj_free(cp);
1679         }
1680
1681         opj_free(j3d);
1682 }
1683
1684 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
1685         if(j3d && parameters) {
1686                 /* create and initialize the coding parameters structure */
1687                 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1688                 cp->reduce[0] = parameters->cp_reduce[0];
1689                 cp->reduce[1] = parameters->cp_reduce[1];
1690                 cp->reduce[2] = parameters->cp_reduce[2];
1691                 cp->layer = parameters->cp_layer;
1692                 cp->bigendian = parameters->bigendian;
1693
1694     /* MM: Settings of the following two member variables would take
1695       place during j3d_read_com. FIXME */
1696                 cp->encoding_format = ENCOD_3EB;
1697                 cp->transform_format = TRF_2D_DWT;
1698
1699                 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
1700                 j3d->cp = cp;
1701         }
1702 }
1703
1704 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
1705         opj_volume_t *volume = NULL;
1706
1707         opj_common_ptr cinfo = j3d->cinfo;
1708
1709         j3d->cio = cio;
1710
1711         /* create an empty volume */
1712         volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
1713         j3d->volume = volume;
1714
1715         j3d->state = J3D_STATE_MHSOC;
1716         
1717         for (;;) {
1718                 opj_dec_mstabent_t *e;
1719                 int id = cio_read(cio, 2);
1720                 if (id >> 8 != 0xff) {
1721                         opj_volume_destroy(volume);
1722                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1723                         return 0;
1724                 }
1725                 e = j3d_dec_mstab_lookup(id);
1726                 /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/
1727                 if (!(j3d->state & e->states)) {
1728                         opj_volume_destroy(volume);
1729                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1730                         return 0;
1731                 }
1732                 if (e->handler) {
1733                         (*e->handler)(j3d);
1734                 }
1735                 /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/
1736                 if (j3d->state == J3D_STATE_MT) {
1737                         break;
1738                 }
1739                 if (j3d->state == J3D_STATE_NEOC) {
1740                         break;
1741                 }
1742         }
1743         if (j3d->state == J3D_STATE_NEOC) {
1744                 j3d_read_eoc(j3d);
1745         }
1746
1747         if (j3d->state != J3D_STATE_MT) {
1748                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1749         }
1750         
1751         return volume;
1752 }
1753
1754 /* ----------------------------------------------------------------------- */
1755 /* J3D encoder interface                                                       */
1756 /* ----------------------------------------------------------------------- */
1757
1758 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
1759         opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1760         if(j3d) {
1761                 j3d->cinfo = cinfo;
1762         }
1763         return j3d;
1764 }
1765
1766 void j3d_destroy_compress(opj_j3d_t *j3d) {
1767         int tileno;
1768
1769         if(!j3d) return;
1770
1771         if(j3d->volume_info != NULL) {
1772                 opj_volume_info_t *volume_info = j3d->volume_info;
1773                 if (volume_info->index_on && j3d->cp) {
1774                         opj_cp_t *cp = j3d->cp;
1775                         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1776                                 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
1777                                 opj_free(tile_info->thresh);
1778                                 opj_free(tile_info->packet);
1779                         }
1780                         opj_free(volume_info->tile);
1781                 }
1782                 opj_free(volume_info);
1783         }
1784         if(j3d->cp != NULL) {
1785                 opj_cp_t *cp = j3d->cp;
1786
1787                 if(cp->comment) {
1788                         opj_free(cp->comment);
1789                 }
1790                 if(cp->matrice) {
1791                         opj_free(cp->matrice);
1792                 }
1793                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1794                         opj_free(cp->tcps[tileno].tccps);
1795                 }
1796                 opj_free(cp->tcps);
1797                 opj_free(cp);
1798         }
1799
1800         opj_free(j3d);
1801 }
1802
1803 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
1804         int i, j, tileno, numpocs_tile;
1805         opj_cp_t *cp = NULL;
1806
1807         if(!j3d || !parameters || ! volume) {
1808                 return;
1809         }
1810
1811         /* create and initialize the coding parameters structure */
1812         cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1813
1814         /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
1815         j3d->cp = cp;
1816
1817         /* set default values for cp */
1818         cp->tw = 1;
1819         cp->th = 1;
1820         cp->tl = 1;
1821
1822         /* copy user encoding parameters */
1823         cp->disto_alloc = parameters->cp_disto_alloc;
1824         cp->fixed_alloc = parameters->cp_fixed_alloc;
1825         cp->fixed_quality = parameters->cp_fixed_quality;
1826
1827         /* transform and coding method */
1828         cp->transform_format = parameters->transform_format;
1829         cp->encoding_format = parameters->encoding_format;
1830
1831         /* mod fixed_quality */
1832         if(parameters->cp_matrice) {
1833                 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
1834                 cp->matrice = (int *) opj_malloc(array_size);
1835                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1836         } 
1837
1838         /* creation of an index file ? */
1839         cp->index_on = parameters->index_on;
1840         if(cp->index_on) {
1841                 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
1842         }
1843         
1844         /* tiles */
1845         cp->tdx = parameters->cp_tdx;
1846         cp->tdy = parameters->cp_tdy;
1847         cp->tdz = parameters->cp_tdz;
1848         /* tile offset */
1849         cp->tx0 = parameters->cp_tx0;
1850         cp->ty0 = parameters->cp_ty0;
1851         cp->tz0 = parameters->cp_tz0;
1852         /* comment string */
1853         if(parameters->cp_comment) {
1854                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1855                 if(cp->comment) {
1856                         strcpy(cp->comment, parameters->cp_comment);
1857                 }
1858         }
1859
1860         /*calculate other encoding parameters*/
1861         if (parameters->tile_size_on) {
1862                 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
1863                 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
1864                 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
1865         } else {
1866                 cp->tdx = volume->x1 - cp->tx0;
1867                 cp->tdy = volume->y1 - cp->ty0;
1868                 cp->tdz = volume->z1 - cp->tz0;
1869         }
1870
1871         /* initialize the multiple tiles */
1872         /* ---------------------------- */
1873         cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
1874
1875         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
1876                 opj_tcp_t *tcp = &cp->tcps[tileno];
1877                 tcp->numlayers = parameters->tcp_numlayers;
1878                 for (j = 0; j < tcp->numlayers; j++) {
1879                         if (cp->fixed_quality) {        /* add fixed_quality */
1880                                 tcp->distoratio[j] = parameters->tcp_distoratio[j];
1881                         } else {
1882                                 tcp->rates[j] = parameters->tcp_rates[j];
1883                         }
1884                 }
1885                 tcp->csty = parameters->csty;
1886                 tcp->prg = parameters->prog_order;
1887                 tcp->mct = volume->numcomps == 3 ? 1 : 0;
1888
1889                 numpocs_tile = 0;
1890                 tcp->POC = 0;
1891                 if (parameters->numpocs) {
1892                         /* initialisation of POC */
1893                         tcp->POC = 1;
1894                         for (i = 0; i < parameters->numpocs; i++) {
1895                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1896                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1897                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
1898                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
1899                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
1900                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
1901                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
1902                                         tcp_poc->prg            = parameters->POC[numpocs_tile].prg;
1903                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
1904                                         numpocs_tile++;
1905                                 }
1906                         }
1907                 }
1908                 tcp->numpocs = numpocs_tile;
1909
1910                 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
1911                 
1912                 for (i = 0; i < volume->numcomps; i++) {
1913                         opj_tccp_t *tccp = &tcp->tccps[i];
1914                         tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT;       /* 0 => standard precint || 1 => custom-defined precinct  */
1915                         tccp->numresolution[0] = parameters->numresolution[0];
1916                         tccp->numresolution[1] = parameters->numresolution[1];
1917                         tccp->numresolution[2] = parameters->numresolution[2];
1918                                                 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
1919                                                 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
1920                                                 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
1921                                                 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
1922                                                 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
1923                                                 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
1924                         tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]); 
1925                         tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]); 
1926                         tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]); 
1927                                                 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
1928                         tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/
1929
1930                         /*ATK / transform */
1931                         tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
1932                         for (j = 0; j < 3; j++) {
1933                                         tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
1934                         }
1935                                                 
1936                         /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
1937                         tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
1938                         tccp->numgbits = 2;
1939                         if (i == parameters->roi_compno) {
1940                                 tccp->roishift = parameters->roi_shift;
1941                         } else {
1942                                 tccp->roishift = 0;
1943                         }
1944                         /* Custom defined precints */
1945                         if (parameters->csty & J3D_CCP_CSTY_PRT) {
1946                                 int k;
1947                                 for (k = 0; k < 3; k++) {
1948                                         int p = 0;
1949                                         for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
1950                                                 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
1951                                                         if (parameters->prct_init[k][p] < 1) {
1952                                                                 tccp->prctsiz[k][j] = 1;
1953                                                         } else {
1954                                                                 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
1955                                                         }
1956                                                 } else {
1957                                                         int res_spec = parameters->res_spec;
1958                                                         int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
1959                                                         if (size_prct < 1) {
1960                                                                 tccp->prctsiz[k][j] = 1;
1961                                                         } else {
1962                                                                 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
1963                                                         }
1964                                                 }
1965                                         }
1966                                         p++;
1967                                 }
1968                         } else {
1969                                 int k;
1970                                 for (k = 0; k < 3; k++) {
1971                     for (j = 0; j < tccp->numresolution[k]; j++) {
1972                         tccp->prctsiz[k][j] = 15;
1973                                         }
1974                                 }
1975                         }
1976                         /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/
1977                         dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
1978                 }
1979         }
1980 }
1981
1982 /**
1983 Create an index file
1984 @param j3d
1985 @param cio
1986 @param volume_info
1987 @param index Index filename
1988 @return Returns 1 if successful, returns 0 otherwise
1989 */
1990 static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
1991         
1992         int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
1993         FILE *stream = NULL;
1994         double total_disto = 0;
1995
1996         volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction;     /* Correction 14/4/03 suite rmq de Patrick */
1997
1998         stream = fopen(index, "w");
1999         if (!stream) {
2000                 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
2001                 return 0;
2002         }
2003         
2004         fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
2005         fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
2006         fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
2007         fprintf(stream, "PROG\t%d\n", volume_info->prog);
2008         fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
2009         fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
2010         fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
2011         fprintf(stream, "LAYER\t%d\n", volume_info->layer);
2012         fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
2013         
2014         fprintf(stream, "Precint sizes for each resolution:\n");
2015         for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
2016                 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
2017                         (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 */
2018         }
2019         fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
2020         fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
2021         fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
2022         for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2023                 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
2024                         volume_info->tile[tileno].num_tile,
2025                         volume_info->tile[tileno].start_pos,
2026                         volume_info->tile[tileno].end_header,
2027                         volume_info->tile[tileno].end_pos,
2028                         volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
2029                         volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
2030         }
2031         
2032         for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2033                 int start_pos, end_pos;
2034                 double disto = 0;
2035                 pack_nb = 0;
2036                 if (volume_info->prog == LRCP) {        /* LRCP */
2037                         fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto\n");
2038                         for (layno = 0; layno < volume_info->layer; layno++) {
2039                                 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2040                                         for (compno = 0; compno < volume_info->comp; compno++) {
2041                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2042                                                 for (precno = 0; precno < prec_max; precno++) {
2043                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2044                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2045                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
2046                                                         fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2047                                                         total_disto += disto;
2048                                                         pack_nb++;
2049                                                 }
2050                                         }
2051                                 }
2052                         }
2053                 } /* LRCP */
2054                 else if (volume_info->prog == RLCP) {   /* RLCP */
2055                         /*
2056                         fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");
2057                         */
2058                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2059                                 for (layno = 0; layno < volume_info->layer; layno++) {
2060                                         for (compno = 0; compno < volume_info->comp; compno++) {
2061                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
2062                                                 for (precno = 0; precno < prec_max; precno++) {
2063                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2064                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2065                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
2066                                                         fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2067                                                                 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2068                                                         total_disto += disto;
2069                                                         pack_nb++;
2070                                                 }
2071                                         }
2072                                 }
2073                         }
2074                 } /* RLCP */
2075                 else if (volume_info->prog == RPCL) {   /* RPCL */
2076                         /*
2077                         fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n"); 
2078                         */
2079                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2080                                 /* I suppose components have same XRsiz, YRsiz */
2081                                 /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/
2082                                 /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/
2083                                 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
2084                                 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
2085                                 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
2086                                 int x1 = x0 + volume_info->tile_x;
2087                                 int y1 = y0 + volume_info->tile_y;
2088                                 int z1 = z0 + volume_info->tile_z;
2089                                 for(z = z0; z < z1; z++) {
2090                                         for(y = y0; y < y1; y++) {
2091                                                 for(x = x0; x < x1; x++) {
2092                                                         for (compno = 0; compno < volume_info->comp; compno++) {
2093                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2094                                                                 for (precno = 0; precno < prec_max; precno++) {
2095                                                                         int pcnx = volume_info->tile[tileno].prctno[0][resno];
2096                                                                         int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2097                                                                         int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2098                                                                         int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2099                                                                         int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2100                                                                         int precno_y = (int) floor( (float)precno/(float)pcnx );
2101                                                                         if (precno_y*pcy == y ) {
2102                                                                                 if (precno_x*pcx == x ) {
2103                                                                                         for (layno = 0; layno < volume_info->layer; layno++) {
2104                                                                                                 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2105                                                                                                 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2106                                                                                                 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2107                                                                                                 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2108                                                                                                         pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto); 
2109                                                                                                 total_disto += disto;
2110                                                                                                 pack_nb++; 
2111                                                                                         }
2112                                                                                 }
2113                                                                         }
2114                                                                 } /* precno */
2115                                                         } /* compno */
2116                                                 } /* x = x0..x1 */
2117                                         } /* y = y0..y1 */
2118                                 } /* z = z0..z1 */
2119                         } /* resno */
2120                 } /* RPCL */
2121                 else if (volume_info->prog == PCRL) {   /* PCRL */
2122                         /* I suppose components have same XRsiz, YRsiz */
2123                         int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2124                         int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2125                         int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2126                         int x1 = x0 + volume_info->tile_x;
2127                         int y1 = y0 + volume_info->tile_y;
2128                         int z1 = z0 + volume_info->tile_z;
2129                         /*
2130                         fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n"); 
2131                         */
2132                         for(z = z0; z < z1; z++) {
2133                                 for(y = y0; y < y1; y++) {
2134                                         for(x = x0; x < x1; x++) {
2135                                                 for (compno = 0; compno < volume_info->comp; compno++) {
2136                                                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2137                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
2138                                                                 for (precno = 0; precno < prec_max; precno++) {
2139                                                                 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2140                                                                 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2141                                                                 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2142                                                                 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2143                                                                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2144                                                                 int precno_y = (int) floor( (float)precno/(float)pcnx );
2145                                                                 int precno_z = (int) floor( (float)precno/(float)pcnx );
2146                                                                 if (precno_z*pcz == z ) {
2147                                                                         if (precno_y*pcy == y ) {
2148                                                                                 if (precno_x*pcx == x ) {
2149                                                                                         for (layno = 0; layno < volume_info->layer; layno++) {
2150                                                                                                 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2151                                                                                                 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2152                                                                                                 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2153                                                                                                 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2154                                                                                                         pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto); 
2155                                                                                                 total_disto += disto;
2156                                                                                                 pack_nb++; 
2157                                                                                         }
2158                                                                                 }
2159                                                                         }
2160                                                                 }
2161                                                         } /* precno */
2162                                                 } /* resno */
2163                                         } /* compno */
2164                                 } /* x = x0..x1 */
2165                         } /* y = y0..y1 */
2166                         }
2167                 } /* PCRL */
2168                 else {  /* CPRL */
2169                         /*
2170                         fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n"); 
2171                         */
2172                         for (compno = 0; compno < volume_info->comp; compno++) {
2173                                 /* I suppose components have same XRsiz, YRsiz */
2174                                 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2175                                 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2176                                 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2177                                 int x1 = x0 + volume_info->tile_x;
2178                                 int y1 = y0 + volume_info->tile_y;
2179                                 int z1 = z0 + volume_info->tile_z;
2180                                 for(z = z0; z < z1; z++) {
2181                                         for(y = y0; y < y1; y++) {
2182                                                 for(x = x0; x < x1; x++) {
2183                                                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2184                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2185                                                                 for (precno = 0; precno < prec_max; precno++) {
2186                                                                         int pcnx = volume_info->tile[tileno].prctno[0][resno];
2187                                                                         int pcny = volume_info->tile[tileno].prctno[1][resno];
2188                                                                         int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2189                                                                         int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2190                                                                         int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2191                                                                         int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2192                                                                         int precno_y = (int) floor( (float)precno/(float)pcnx );
2193                                                                         int precno_z = 0; /*???*/
2194                                                                         if (precno_z*pcz == z ) {
2195                                                                                 if (precno_y*pcy == y ) {
2196                                                                                         if (precno_x*pcx == x ) {
2197                                                                                                 for (layno = 0; layno < volume_info->layer; layno++) {
2198                                                                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2199                                                                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2200                                                                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
2201                                                                                                         fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2202                                                                                                                 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto); 
2203                                                                                                         total_disto += disto;
2204                                                                                                         pack_nb++; 
2205                                                                                                 }
2206                                                                                         }
2207                                                                                 }
2208                                                                         }
2209                                                                 } /* precno */
2210                                                         } /* resno */
2211                                                 } /* x = x0..x1 */
2212                                         } /* y = y0..y1 */
2213                                 } /* z = z0..z1 */
2214                         } /* comno */
2215                 } /* CPRL */   
2216         } /* tileno */
2217         
2218         fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max);   /* SE max */
2219         fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto);                       /* SE totale */
2220         
2221
2222         fclose(stream);
2223
2224         return 1;
2225 }
2226
2227 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
2228         int tileno, compno;
2229         opj_volume_info_t *volume_info = NULL;
2230         opj_cp_t *cp = NULL;
2231         opj_tcd_t *tcd = NULL;  /* TCD component */
2232
2233         j3d->cio = cio; 
2234         j3d->volume = volume;
2235         cp = j3d->cp;
2236
2237         /*j3d_dump_volume(stdout, volume);
2238         j3d_dump_cp(stdout, volume, cp);*/
2239
2240         /* INDEX >> */
2241         volume_info = j3d->volume_info;
2242         if (volume_info && cp->index_on) {
2243                 volume_info->index_on = cp->index_on;
2244                 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
2245                 volume_info->volume_w = volume->x1 - volume->x0;
2246                 volume_info->volume_h = volume->y1 - volume->y0;
2247                 volume_info->volume_l = volume->z1 - volume->z0;
2248                 volume_info->prog = (&cp->tcps[0])->prg;
2249                 volume_info->tw = cp->tw;
2250                 volume_info->th = cp->th;
2251                 volume_info->tl = cp->tl;
2252                 volume_info->tile_x = cp->tdx;  /* new version parser */
2253                 volume_info->tile_y = cp->tdy;  /* new version parser */
2254                 volume_info->tile_z = cp->tdz;  /* new version parser */
2255                 volume_info->tile_Ox = cp->tx0; /* new version parser */
2256                 volume_info->tile_Oy = cp->ty0; /* new version parser */
2257                 volume_info->tile_Oz = cp->tz0; /* new version parser */
2258                 volume_info->transform_format = cp->transform_format;
2259                 volume_info->encoding_format = cp->encoding_format;
2260                 volume_info->comp = volume->numcomps;
2261                 volume_info->layer = (&cp->tcps[0])->numlayers;
2262                 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
2263                 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
2264                 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
2265                 volume_info->D_max = 0;         /* ADD Marcela */
2266         }
2267         /* << INDEX */
2268
2269         j3d_write_soc(j3d);
2270         j3d_write_siz(j3d);
2271         if (j3d->cinfo->codec_format == CODEC_J3D) {
2272                 j3d_write_cap(j3d);
2273                 j3d_write_nsi(j3d);
2274         }
2275
2276         /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/
2277                 j3d_write_com(j3d);
2278
2279         j3d_write_cod(j3d);
2280         j3d_write_qcd(j3d);
2281         for (compno = 0; compno < volume->numcomps; compno++) {
2282                 opj_tcp_t *tcp = &cp->tcps[0];
2283                 if (tcp->tccps[compno].roishift)
2284                         j3d_write_rgn(j3d, compno, 0);                  
2285         }
2286         /*Optional 15444-2 markers*/
2287         if (j3d->cp->tcps->tccps[0].atk != NULL)
2288         j3d_write_atk(j3d);
2289         if (j3d->volume->comps[0].dcoffset != 0)
2290         j3d_write_dco(j3d);
2291
2292         /* INDEX >> */
2293         if(volume_info && volume_info->index_on) {
2294                 volume_info->main_head_end = cio_tell(cio) - 1;
2295         }
2296         /* << INDEX */
2297
2298         /* create the tile encoder */
2299         tcd = tcd_create(j3d->cinfo);
2300
2301         /* encode each tile */
2302         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
2303                 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
2304                 
2305                 j3d->curtileno = tileno;
2306
2307                 /* initialisation before tile encoding  */
2308                 if (tileno == 0) {      
2309                         tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
2310                 } else {
2311                         tcd_init_encode(tcd, volume, cp, j3d->curtileno);
2312                 }
2313                 
2314                 /* INDEX >> */
2315                 if(volume_info && volume_info->index_on) {
2316                         volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
2317                         volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
2318                 }
2319                 /* << INDEX */
2320                 
2321                 j3d_write_sot(j3d);
2322         
2323                 for (compno = 1; compno < volume->numcomps; compno++) {
2324                         j3d_write_coc(j3d, compno);
2325                         j3d_write_qcc(j3d, compno);
2326                 }
2327
2328                 if (cp->tcps[tileno].numpocs) {
2329                         j3d_write_poc(j3d);
2330                 }
2331                 j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/
2332
2333                 /* INDEX >> */
2334                 if(volume_info && volume_info->index_on) {
2335                         volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
2336                 }
2337                 /* << INDEX */          
2338         }
2339         
2340         /* destroy the tile encoder */
2341         tcd_free_encode(tcd);
2342         tcd_destroy(tcd);
2343
2344         j3d_write_eoc(j3d);
2345         
2346         /* Creation of the index file */
2347         if(volume_info && volume_info->index_on) {
2348                 if(!j3d_create_index(j3d, cio, volume_info, index)) {
2349                         opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2350                         return false;
2351                 }
2352         }
2353           
2354         return true;
2355 }
2356
2357 /* ----------------------------------------------------------------------- */
2358 /*@}*/
2359
2360 /*@}*/
2361