2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herve Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
37 /** @name Funciones locales */
41 Write the SOC marker (Start Of Codestream)
44 static void j3d_write_soc(opj_j3d_t *j3d);
46 Read the SOC marker (Start of Codestream)
49 static void j3d_read_soc(opj_j3d_t *j3d);
51 Write the SIZ marker (2D volume and tile size)
54 static void j3d_write_siz(opj_j3d_t *j3d);
56 Read the SIZ marker (2D volume and tile size)
59 static void j3d_read_siz(opj_j3d_t *j3d);
61 Write the NSI marker (3rd volume and tile size)
64 static void j3d_write_nsi(opj_j3d_t *j3d);
66 Read the NSI marker (3rd volume and tile size)
69 static void j3d_read_nsi(opj_j3d_t *j3d);
71 Write the COM marker (comment)
74 static void j3d_write_com(opj_j3d_t *j3d);
76 Read the COM marker (comment)
79 static void j3d_read_com(opj_j3d_t *j3d);
81 Write the value concerning the specified component in the marker COD and COC
83 @param compno Number of the component concerned by the information written
85 static void j3d_write_cox(opj_j3d_t *j3d, int compno);
87 Read the value concerning the specified component in the marker COD and COC
89 @param compno Number of the component concerned by the information read
91 static void j3d_read_cox(opj_j3d_t *j3d, int compno);
93 Write the COD marker (coding style default)
96 static void j3d_write_cod(opj_j3d_t *j3d);
98 Read the COD marker (coding style default)
101 static void j3d_read_cod(opj_j3d_t *j3d);
103 Write the COC marker (coding style component)
104 @param j3d J3D handle
105 @param compno Number of the component concerned by the information written
107 static void j3d_write_coc(opj_j3d_t *j3d, int compno);
109 Read the COC marker (coding style component)
110 @param j3d J3D handle
112 static void j3d_read_coc(opj_j3d_t *j3d);
114 Write the value concerning the specified component in the marker QCD and QCC
115 @param j3d J3D handle
116 @param compno Number of the component concerned by the information written
118 static void j3d_write_qcx(opj_j3d_t *j3d, int compno);
120 Read the value concerning the specified component in the marker QCD and QCC
121 @param j3d J3D handle
122 @param compno Number of the component concern by the information read
123 @param len Length of the information in the QCX part of the marker QCD/QCC
125 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len);
127 Write the QCD marker (quantization default)
128 @param j3d J3D handle
130 static void j3d_write_qcd(opj_j3d_t *j3d);
132 Read the QCD marker (quantization default)
133 @param j3d J3D handle
135 static void j3d_read_qcd(opj_j3d_t *j3d);
137 Write the QCC marker (quantization component)
138 @param j3d J3D handle
139 @param compno Number of the component concerned by the information written
141 static void j3d_write_qcc(opj_j3d_t *j3d, int compno);
143 Read the QCC marker (quantization component)
144 @param j3d J3D handle
146 static void j3d_read_qcc(opj_j3d_t *j3d);
148 Write the POC marker (progression order change)
149 @param j3d J3D handle
151 static void j3d_write_poc(opj_j3d_t *j3d);
153 Read the POC marker (progression order change)
154 @param j3d J3D handle
156 static void j3d_read_poc(opj_j3d_t *j3d);
158 Read the CRG marker (component registration)
159 @param j3d J3D handle
161 static void j3d_read_crg(opj_j3d_t *j3d);
163 Read the TLM marker (tile-part lengths)
164 @param j3d J3D handle
166 static void j3d_read_tlm(opj_j3d_t *j3d);
168 Read the PLM marker (packet length, main header)
169 @param j3d J3D handle
171 static void j3d_read_plm(opj_j3d_t *j3d);
173 Read the PLT marker (packet length, tile-part header)
174 @param j3d J3D handle
176 static void j3d_read_plt(opj_j3d_t *j3d);
178 Read the PPM marker (packet packet headers, main header)
179 @param j3d J3D handle
181 static void j3d_read_ppm(opj_j3d_t *j3d);
183 Read the PPT marker (packet packet headers, tile-part header)
184 @param j3d J3D handle
186 static void j3d_read_ppt(opj_j3d_t *j3d);
188 Write the SOT marker (start of tile-part)
189 @param j3d J3D handle
191 static void j3d_write_sot(opj_j3d_t *j3d);
193 Read the SOT marker (start of tile-part)
194 @param j3d J3D handle
196 static void j3d_read_sot(opj_j3d_t *j3d);
198 Write the SOD marker (start of data)
199 @param j3d J3D handle
200 @param tile_coder Pointer to a TCD handle
202 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder);
204 Read the SOD marker (start of data)
205 @param j3d J3D handle
207 static void j3d_read_sod(opj_j3d_t *j3d);
209 Write the RGN marker (region-of-interest)
210 @param j3d J3D handle
211 @param compno Number of the component concerned by the information written
212 @param tileno Number of the tile concerned by the information written
214 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno);
216 Read the RGN marker (region-of-interest)
217 @param j3d J3D handle
219 static void j3d_read_rgn(opj_j3d_t *j3d);
221 Write the EOC marker (end of codestream)
222 @param j3d J3D handle
224 static void j3d_write_eoc(opj_j3d_t *j3d);
226 Read the EOC marker (end of codestream)
227 @param j3d J3D handle
229 static void j3d_read_eoc(opj_j3d_t *j3d);
231 Read an unknown marker
232 @param j3d J3D handle
234 static void j3d_read_unk(opj_j3d_t *j3d);
236 Write the CAP marker (extended capabilities)
237 @param j3d J3D handle
239 static void j3d_write_cap(opj_j3d_t *j3d);
241 Read the CAP marker (extended capabilities)
242 @param j3d J3D handle
244 static void j3d_read_cap(opj_j3d_t *j3d);
246 Write the DCO marker (Variable DC offset)
247 @param j3d J3D handle
249 static void j3d_write_dco(opj_j3d_t *j3d);
251 Read the DCO marker (Variable DC offset)
252 @param j3d J3D handle
254 static void j3d_read_dco(opj_j3d_t *j3d);
256 Write the ATK marker (arbitrary transformation kernel)
257 @param j3d J3D handle
259 static void j3d_write_atk(opj_j3d_t *j3d);
261 Read the ATK marker (arbitrary transformation kernel)
262 @param j3d J3D handle
264 static void j3d_read_atk(opj_j3d_t *j3d);
266 Write the CBD marker (component bit depth definition)
267 @param j3d J3D handle
269 static void j3d_write_cbd(opj_j3d_t *j3d);
271 Read the CBD marker (component bit depth definition)
272 @param j3d J3D handle
274 static void j3d_read_cbd(opj_j3d_t *j3d);
276 Write the MCT marker (multiple component transfomation definition)
277 @param j3d J3D handle
279 static void j3d_write_mct(opj_j3d_t *j3d);
281 Read the MCT marker (multiple component transfomation definition)
282 @param j3d J3D handle
284 static void j3d_read_mct(opj_j3d_t *j3d);
286 Write the MCC marker (multiple component transfomation collection)
287 @param j3d J3D handle
289 static void j3d_write_mcc(opj_j3d_t *j3d);
291 Read the MCC marker (multiple component transfomation collection)
292 @param j3d J3D handle
294 static void j3d_read_mcc(opj_j3d_t *j3d);
296 Write the MCO marker (multiple component transfomation ordering)
297 @param j3d J3D handle
299 static void j3d_write_mco(opj_j3d_t *j3d);
301 Read the MCO marker (multiple component transfomation ordering)
302 @param j3d J3D handle
304 static void j3d_read_mco(opj_j3d_t *j3d);
306 Write the NLT marker (non-linearity point transformation)
307 @param j3d J3D handle
309 static void j3d_write_nlt(opj_j3d_t *j3d);
311 Read the NLT marker (non-linearity point transformation)
312 @param j3d J3D handle
314 static void j3d_read_nlt(opj_j3d_t *j3d);
317 /* ----------------------------------------------------------------------- */
319 static void j3d_dump_volume(FILE *fd, opj_volume_t * vol) {
321 fprintf(fd, "volume {\n");
322 fprintf(fd, " x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1, vol->z1);
323 fprintf(fd, " numcomps=%d\n", vol->numcomps);
324 for (compno = 0; compno < vol->numcomps; compno++) {
325 opj_volume_comp_t *comp = &vol->comps[compno];
326 fprintf(fd, " comp %d {\n", compno);
327 fprintf(fd, " dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);
328 fprintf(fd, " prec=%d\n", comp->prec);
329 fprintf(fd, " sgnd=%d\n", comp->sgnd);
335 static void j3d_dump_cp(FILE *fd, opj_volume_t * vol, opj_cp_t * cp) {
336 int tileno, compno, layno, bandno, resno, numbands;
337 fprintf(fd, "coding parameters {\n");
338 fprintf(fd, " tx0=%d, ty0=%d, tz0=%d\n", cp->tx0, cp->ty0, cp->tz0);
339 fprintf(fd, " tdx=%d, tdy=%d, tdz=%d\n", cp->tdx, cp->tdy, cp->tdz);
340 fprintf(fd, " tw=%d, th=%d, tl=%d\n", cp->tw, cp->th, cp->tl);
341 fprintf(fd, " transform format: %d\n", cp->transform_format);
342 fprintf(fd, " encoding format: %d\n", cp->encoding_format);
343 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
344 opj_tcp_t *tcp = &cp->tcps[tileno];
345 fprintf(fd, " tile %d {\n", tileno);
346 fprintf(fd, " csty=%x\n", tcp->csty);
347 fprintf(fd, " prg=%d\n", tcp->prg);
348 fprintf(fd, " numlayers=%d\n", tcp->numlayers);
349 fprintf(fd, " mct=%d\n", tcp->mct);
350 fprintf(fd, " rates=");
351 for (layno = 0; layno < tcp->numlayers; layno++) {
352 fprintf(fd, "%f ", tcp->rates[layno]);
355 fprintf(fd, " first=%d\n", tcp->first);
356 for (compno = 0; compno < vol->numcomps; compno++) {
357 opj_tccp_t *tccp = &tcp->tccps[compno];
358 fprintf(fd, " comp %d {\n", compno);
359 fprintf(fd, " csty=%x\n", tccp->csty);
360 fprintf(fd, " numresx=%d, numresy=%d, numresz=%d\n", tccp->numresolution[0], tccp->numresolution[1], tccp->numresolution[2]);
361 fprintf(fd, " cblkw=%d, cblkh=%d, cblkl=%d\n", tccp->cblk[0], tccp->cblk[1], tccp->cblk[2]);
362 fprintf(fd, " cblksty=%x\n", tccp->cblksty);
363 fprintf(fd, " qntsty=%d\n", tccp->qntsty);
364 fprintf(fd, " numgbits=%d\n", tccp->numgbits);
365 fprintf(fd, " roishift=%d\n", tccp->roishift);
366 fprintf(fd, " reversible=%d\n", tccp->reversible);
367 fprintf(fd, " dwtidx=%d dwtidy=%d dwtidz=%d\n", tccp->dwtid[0], tccp->dwtid[1], tccp->dwtid[2]);
368 if (tccp->atk != NULL) {
369 fprintf(fd, " atk.index=%d\n", tccp->atk->index);
370 fprintf(fd, " atk.coeff_typ=%d\n", tccp->atk->coeff_typ);
371 fprintf(fd, " atk.filt_cat=%d\n", tccp->atk->filt_cat);
372 fprintf(fd, " atk.exten=%d\n", tccp->atk->exten);
373 fprintf(fd, " atk.minit=%d\n", tccp->atk->minit);
374 fprintf(fd, " atk.wt_typ=%d\n", tccp->atk->wt_typ);
376 fprintf(fd, " stepsizes of bands=");
377 numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 :
378 ( (cp->transform_format == TRF_2D_DWT) ? (tccp->numresolution[0] * 3 - 2) :
379 (tccp->numresolution[0] * 7 - 6) - 4 *(tccp->numresolution[0] - tccp->numresolution[2]) );
380 for (bandno = 0; bandno < numbands; bandno++) {
381 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,tccp->stepsizes[bandno].expn);
385 if (tccp->csty & J3D_CCP_CSTY_PRT) {
386 fprintf(fd, " prcw=");
387 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
388 fprintf(fd, "%d ", tccp->prctsiz[0][resno]);
391 fprintf(fd, " prch=");
392 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
393 fprintf(fd, "%d ", tccp->prctsiz[1][resno]);
396 fprintf(fd, " prcl=");
397 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
398 fprintf(fd, "%d ", tccp->prctsiz[2][resno]);
409 /* -----------------------------------------------------------------------
410 Extended capabilities
411 ------------------------------------------------------------------------*/
413 static void j3d_write_cap(opj_j3d_t *j3d){
416 opj_cio_t *cio = j3d->cio;
417 cio_write(cio, J3D_MS_CAP, 2); /* CAP */
418 lenp = cio_tell(cio);
420 cio_write(cio,J3D_CAP_10, 4);
423 cio_write(cio, 0x0, 2);
425 len = cio_tell(cio) - lenp;
427 cio_write(cio, len, 2); /* Lsiz */
428 cio_seek(cio, lenp + len);
431 static void j3d_read_cap(opj_j3d_t *j3d){
433 opj_cio_t *cio = j3d->cio;
434 /*cio_read(cio, 2); CAP */
435 len = cio_read(cio, 2);
436 Cap = cio_read(cio, 4);
440 assert( len == 2 + 4 + 2 );
442 static void j3d_write_nsi(opj_j3d_t *j3d) {
447 opj_cio_t *cio = j3d->cio;
448 opj_volume_t *volume = j3d->volume;
449 opj_cp_t *cp = j3d->cp;
451 cio_write(cio, J3D_MS_NSI, 2); /* NSI */
452 lenp = cio_tell(cio);
454 cio_write(cio, ndim, 1); /* Ndim */
455 cio_write(cio, volume->z1, 4); /* Zsiz */
456 cio_write(cio, volume->z0, 4); /* Z0siz */
457 cio_write(cio, cp->tdz, 4); /* ZTsiz */
458 cio_write(cio, cp->tz0, 4); /* ZT0siz */
459 for (i = 0; i < volume->numcomps; i++) {
460 cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */
462 len = cio_tell(cio) - lenp;
464 cio_write(cio, len, 2); /* Lsiz */
465 cio_seek(cio, lenp + len);
468 static void j3d_read_nsi(opj_j3d_t *j3d) {
472 opj_cio_t *cio = j3d->cio;
473 opj_volume_t *volume = j3d->volume;
474 opj_cp_t *cp = j3d->cp;
476 len = cio_read(cio, 2); /* Lnsi */
477 ndim = cio_read(cio, 1); /* Ndim */
479 volume->z1 = cio_read(cio, 4); /* Zsiz */
480 volume->z0 = cio_read(cio, 4); /* Z0siz */
481 cp->tdz = cio_read(cio, 4); /* ZTsiz */
482 cp->tz0 = cio_read(cio, 4); /* ZT0siz */
483 for (i = 0; i < volume->numcomps; i++) {
484 volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */
487 /*Initialization of volume*/
488 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
489 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
490 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
491 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
492 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
495 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
497 cp->tcps[i].numpocs = 0;
498 cp->tcps[i].first = 1;
501 /* Initialization for PPM marker (Packets header)*/
504 cp->ppm_data_first = NULL;
505 cp->ppm_previous = 0;
508 j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
509 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
510 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
512 j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
513 j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
514 j3d->state = J3D_STATE_MH;
517 static void j3d_write_dco(opj_j3d_t *j3d){
521 opj_cio_t *cio = j3d->cio;
522 opj_volume_t *volume = j3d->volume;
523 opj_cp_t *cp = j3d->cp;
525 dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */
526 cio_write(cio, J3D_MS_DCO, 2); /* DCO */
527 lenp = cio_tell(cio);
529 cio_write(cio, dcotype, 1);
531 for (i = 0; i < volume->numcomps; i++)
532 cio_write(cio, volume->comps[i].dcoffset, 1); /* SPdco_i */
533 } else if (dcotype == 1) {
534 for (i = 0; i < volume->numcomps; i++){
535 cio_write(cio, volume->comps[i].dcoffset, 1); /* SPdco_i */
536 opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset);
539 len = cio_tell(cio) - lenp;
541 cio_write(cio, len, 2); /* Ldco */
542 cio_seek(cio, lenp + len);
545 static void j3d_read_dco(opj_j3d_t *j3d){
549 opj_cio_t *cio = j3d->cio;
550 opj_volume_t *volume = j3d->volume;
551 opj_cp_t *cp = j3d->cp;
553 len = cio_read(cio, 2); /* Lsiz */
554 dcotype = cio_read(cio, 1); /*offset 8bit unsigned / 16bit signed integers*/
556 for (i = 0; i < volume->numcomps; i++) {
557 volume->comps[i].dcoffset = cio_read(cio, 1);
558 if (volume->comps[i].dcoffset > 128)
559 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
561 } else if (dcotype == 1) {
562 for (i = 0; i < volume->numcomps; i++) {
563 volume->comps[i].dcoffset = cio_read(cio, 1);
564 if (volume->comps[i].dcoffset > 128)
565 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
570 static void j3d_write_atk(opj_j3d_t *j3d){
573 opj_cio_t *cio = j3d->cio;
574 opj_volume_t *volume = j3d->volume;
575 opj_atk_t *atk = j3d->cp->tcps->tccps->atk;
577 cio_write(cio, J3D_MS_ATK, 2); /* ATK */
578 lenp = cio_tell(cio);
580 cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11)
581 + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2); /* Satk */
582 if (atk->wt_typ == J3D_ATK_IRR)
583 cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ);
584 cio_write(cio, atk->Natk, 1);
585 for (s = 0; s < atk->Natk; s++){
586 if (atk->filt_cat == J3D_ATK_ARB)
587 cio_write(cio, atk->Oatk[s], 1);
588 if (atk->wt_typ == J3D_ATK_REV){
589 cio_write(cio, atk->Eatk[s], 1);
590 cio_write(cio, atk->Batk[s], 1);
592 cio_write(cio, atk->LCatk[s], 1);
593 for (k = 0; k < atk->LCatk[s]; k++)
594 cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ);
596 len = cio_tell(cio) - lenp;
598 cio_write(cio, len, 2); /* Latk */
599 cio_seek(cio, lenp + len);
601 static void j3d_read_atk(opj_j3d_t *j3d){
604 opj_cio_t *cio = j3d->cio;
605 opj_volume_t *volume = j3d->volume;
606 opj_cp_t *cp = j3d->cp;
607 opj_atk_t *atk = cp->tcps->tccps->atk;
609 len = cio_read(cio, 2); /* Latk */
610 Satk = cio_read(cio, 2);
611 atk->index = Satk & 0x00ff;
612 atk->coeff_typ = Satk >> 8 & 0x0007;
613 atk->filt_cat = Satk >> 11 & 0x0001;
614 atk->wt_typ = Satk >> 12 & 0x0001;
615 atk->minit = Satk >> 13 & 0x0001;
616 atk->exten = Satk >> 14 & 0x0001;
617 if (atk->wt_typ == J3D_ATK_IRR)
618 atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
619 atk->Natk = cio_read(cio, 1);
620 for (i = 0; i < atk->Natk; i++) {
621 if (atk->filt_cat == J3D_ATK_ARB)
622 atk->Oatk[i] = cio_read(cio, 1);
623 if (atk->wt_typ == J3D_ATK_REV){
624 atk->Eatk[i] = cio_read(cio, 1);
625 atk->Batk[i] = cio_read(cio, 1);
627 atk->LCatk[i] = cio_read(cio, 1);
628 for (k = 0; k < atk->LCatk[i]; k++)
629 atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
632 static void j3d_write_cbd(opj_j3d_t *j3d){
634 static void j3d_read_cbd(opj_j3d_t *j3d){
636 static void j3d_write_mct(opj_j3d_t *j3d){
638 static void j3d_read_mct(opj_j3d_t *j3d){
640 static void j3d_write_mcc(opj_j3d_t *j3d){
642 static void j3d_read_mcc(opj_j3d_t *j3d){
644 static void j3d_write_mco(opj_j3d_t *j3d){
646 static void j3d_read_mco(opj_j3d_t *j3d){
648 static void j3d_write_nlt(opj_j3d_t *j3d){
650 static void j3d_read_nlt(opj_j3d_t *j3d){
652 /* -----------------------------------------------------------------------
653 15444-1 codestream syntax
654 ------------------------------------------------------------------------*/
655 static void j3d_write_soc(opj_j3d_t *j3d) {
656 opj_cio_t *cio = j3d->cio;
657 cio_write(cio, J3D_MS_SOC, 2);
660 static void j3d_read_soc(opj_j3d_t *j3d) {
661 j3d->state = J3D_STATE_MHSIZ;
664 static void j3d_write_siz(opj_j3d_t *j3d) {
669 opj_cio_t *cio = j3d->cio;
670 opj_volume_t *volume = j3d->volume;
671 opj_cp_t *cp = j3d->cp;
673 cio_write(cio, J3D_MS_SIZ, 2); /* SIZ */
674 lenp = cio_tell(cio);
676 /*cio_write(cio, 0, 2);*/ /* Rsiz (capabilities of 15444-1 only) */
677 Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/
678 cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/
679 cio_write(cio, volume->x1, 4); /* Xsiz */
680 cio_write(cio, volume->y1, 4); /* Ysiz */
681 cio_write(cio, volume->x0, 4); /* X0siz */
682 cio_write(cio, volume->y0, 4); /* Y0siz */
683 cio_write(cio, cp->tdx, 4); /* XTsiz */
684 cio_write(cio, cp->tdy, 4); /* YTsiz */
685 cio_write(cio, cp->tx0, 4); /* XT0siz */
686 cio_write(cio, cp->ty0, 4); /* YT0siz */
687 cio_write(cio, volume->numcomps, 2); /* Csiz */
688 for (i = 0; i < volume->numcomps; i++) {
689 cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1); /* Ssiz_i */
690 cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */
691 cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */
693 len = cio_tell(cio) - lenp;
695 cio_write(cio, len, 2); /* Lsiz */
696 cio_seek(cio, lenp + len);
699 static void j3d_read_siz(opj_j3d_t *j3d) {
702 opj_cio_t *cio = j3d->cio;
703 opj_volume_t *volume = j3d->volume;
704 opj_cp_t *cp = j3d->cp;
706 len = cio_read(cio, 2); /* Lsiz */
707 cp->rsiz = cio_read(cio, 2); /* Rsiz (capabilities) */
708 volume->x1 = cio_read(cio, 4); /* Xsiz */
709 volume->y1 = cio_read(cio, 4); /* Ysiz */
710 volume->x0 = cio_read(cio, 4); /* X0siz */
711 volume->y0 = cio_read(cio, 4); /* Y0siz */
712 cp->tdx = cio_read(cio, 4); /* XTsiz */
713 cp->tdy = cio_read(cio, 4); /* YTsiz */
714 cp->tx0 = cio_read(cio, 4); /* XT0siz */
715 cp->ty0 = cio_read(cio, 4); /* YT0siz */
717 volume->numcomps = cio_read(cio, 2); /* Csiz */
718 volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
719 for (i = 0; i < volume->numcomps; i++) {
721 tmp = cio_read(cio, 1); /* Ssiz_i */
722 volume->comps[i].prec = (tmp & 0x7f) + 1;
723 volume->comps[i].sgnd = tmp >> 7;
724 volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
725 volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
726 for (j = 0; j < 3; j++) {
727 volume->comps[i].resno_decoded[j] = 0; /* number of resolution decoded */
728 volume->comps[i].factor[j] = 0; /* reducing factor per component */
732 if (j3d->cinfo->codec_format == CODEC_J2K){
735 volume->numslices = 1;
738 for (i = 0; i < volume->numcomps; i++)
739 volume->comps[i].dz = 1;
741 /*Initialization of volume*/
742 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
743 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
744 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
745 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
746 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
749 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
751 cp->tcps[i].numpocs = 0;
752 cp->tcps[i].first = 1;
755 /* Initialization for PPM marker (Packets header)*/
758 cp->ppm_data_first = NULL;
759 cp->ppm_previous = 0;
762 j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
763 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
764 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
766 j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
767 j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
768 j3d->state = J3D_STATE_MH;
774 static void j3d_write_com(opj_j3d_t *j3d) {
778 opj_cio_t *cio = j3d->cio;
780 cio_write(cio, J3D_MS_COM, 2);
781 lenp = cio_tell(cio);
783 cio_write(cio, 1, 2);
784 /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
785 if (j3d->cp->comment != NULL) {
786 char *comment = j3d->cp->comment;
787 for (i = 0; i < strlen(comment); i++) {
788 cio_write(cio, comment[i], 1);
791 len = cio_tell(cio) - lenp;
793 cio_write(cio, len, 2);
794 cio_seek(cio, lenp + len);
797 static void j3d_read_com(opj_j3d_t *j3d) {
799 opj_cio_t *cio = j3d->cio;
801 len = cio_read(cio, 2);
802 cio_read(cio, 2); // read registration
804 /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
806 cio_skip(cio, len - 4); /*posible comments*/
809 static void j3d_write_cox(opj_j3d_t *j3d, int compno) {
813 opj_cp_t *cp = j3d->cp;
814 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
815 opj_tccp_t *tccp = &tcp->tccps[compno];
816 opj_cio_t *cio = j3d->cio;
818 cio_write(cio, tccp->numresolution[0] - 1, 1); /* SPcox (D) No of decomposition levels in x-axis*/
819 if (j3d->cinfo->codec_format == CODEC_J3D) {
820 cio_write(cio, tccp->numresolution[1] - 1, 1); /* SPcox (E) No of decomposition levels in y-axis*/
821 cio_write(cio, tccp->numresolution[2] - 1, 1); /* SPcox (F) No of decomposition levels in z-axis*/
823 if (j3d->cinfo->codec_format == CODEC_J3D) {
827 /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/
828 cio_write(cio, tccp->cblk[0] - shift, 1); /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/
829 cio_write(cio, tccp->cblk[1] - shift, 1); /* SPcox (H) Cblk height*/
830 if (j3d->cinfo->codec_format == CODEC_J3D) {
831 cio_write(cio, tccp->cblk[2] - shift, 1); /* SPcox (I) Cblk depth*/
833 cio_write(cio, tccp->cblksty, 1); /* SPcox (J) Cblk style*/
834 cio_write(cio, tccp->dwtid[0], 1); /* SPcox (K) WT in x-axis 15444-2 Table A10*/
835 if (j3d->cinfo->codec_format == CODEC_J3D) {
836 cio_write(cio, tccp->dwtid[1], 1); /* SPcox (L) WT in y-axis 15444-2 Table A10*/
837 cio_write(cio, tccp->dwtid[2], 1); /* SPcox (M) WT in z-axis 15444-2 Table A10*/
840 if (tccp->csty & J3D_CCP_CSTY_PRT) {
841 for (i = 0; i < tccp->numresolution[0]; i++) {
842 if (i < tccp->numresolution[2])
843 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2); /* SPcox (N_i) Table A9*/
845 if (j3d->cinfo->codec_format == CODEC_J3D)
846 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2); /* SPcox (N_i) Table A9*/
848 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1); /* SPcox (N_i) Table A9*/ }
852 static void j3d_read_cox(opj_j3d_t *j3d, int compno) {
856 opj_cp_t *cp = j3d->cp;
857 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
858 opj_tccp_t *tccp = &tcp->tccps[compno];
859 opj_cio_t *cio = j3d->cio;
861 tccp->numresolution[0] = cio_read(cio, 1) + 1; /* SPcox (D) No of decomposition levels in x-axis*/
862 if (j3d->cinfo->codec_format == CODEC_J3D) {
863 tccp->numresolution[1] = cio_read(cio, 1) + 1; /* SPcox (E) No of decomposition levels in y-axis*/
864 tccp->numresolution[2] = cio_read(cio, 1) + 1; /* SPcox (F) No of decomposition levels in z-axis*/
865 }else if (j3d->cinfo->codec_format == CODEC_J2K) {
866 tccp->numresolution[1] = tccp->numresolution[0];
867 tccp->numresolution[2] = 1;
869 /* check the reduce value */
870 cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]);
871 cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]);
872 cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]);
874 if (j3d->cinfo->codec_format == CODEC_J3D) {
878 tccp->cblk[0] = cio_read(cio, 1) + shift; /* SPcox (G) */
879 tccp->cblk[1] = cio_read(cio, 1) + shift; /* SPcox (H) */
880 if (j3d->cinfo->codec_format == CODEC_J3D)
881 tccp->cblk[2] = cio_read(cio, 1) + shift; /* SPcox (I) */
883 tccp->cblk[2] = tccp->cblk[0];
885 tccp->cblksty = cio_read(cio, 1); /* SPcox (J) */
886 tccp->dwtid[0] = cio_read(cio, 1); /* SPcox (K) */
887 if (j3d->cinfo->codec_format == CODEC_J3D) {
888 tccp->dwtid[1] = cio_read(cio, 1); /* SPcox (L) */
889 tccp->dwtid[2] = cio_read(cio, 1); /* SPcox (M) */
891 tccp->dwtid[1] = tccp->dwtid[0]; /* SPcox (L) */
892 tccp->dwtid[2] = tccp->dwtid[0]; /* SPcox (M) */
894 tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); /*TODO: only valid for irreversible 9x7 WTs*/
895 if (tccp->csty & J3D_CP_CSTY_PRT) {
896 for (i = 0; i < tccp->numresolution[0]; i++) {
897 int tmp = cio_read(cio, 2); /* SPcox (N_i) */
898 tccp->prctsiz[0][i] = tmp & 0xf;
899 tccp->prctsiz[1][i] = tmp >> 4;
900 tccp->prctsiz[2][i] = tmp >> 8;
905 static void j3d_write_cod(opj_j3d_t *j3d) {
907 opj_tcp_t *tcp = NULL;
910 opj_cio_t *cio = j3d->cio;
912 cio_write(cio, J3D_MS_COD, 2); /* COD */
914 lenp = cio_tell(cio);
918 tcp = &cp->tcps[j3d->curtileno];
920 /* Scod : Table A-4*/
921 cio_write(cio, tcp->csty, 1); /* Scod : Coding style parameters */
922 /* SGcod : Table A-5*/
923 cio_write(cio, tcp->prg, 1); /* SGcod (A) : Progression order */
924 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) : No of layers */
925 cio_write(cio, tcp->mct, 1); /* SGcod (C) : Multiple component transformation usage */
926 /* SPcod : Table A-6*/
927 j3d_write_cox(j3d, 0);
928 len = cio_tell(cio) - lenp;
930 cio_write(cio, len, 2); /* Lcod */
931 cio_seek(cio, lenp + len);
934 static void j3d_read_cod(opj_j3d_t *j3d) {
937 opj_cio_t *cio = j3d->cio;
938 opj_cp_t *cp = j3d->cp;
939 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
940 opj_volume_t *volume = j3d->volume;
943 len = cio_read(cio, 2);
944 /* Scod : Table A-4*/
945 tcp->csty = cio_read(cio, 1);
946 /* SGcod : Table A-5*/
947 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);
948 tcp->numlayers = cio_read(cio, 2);
949 tcp->mct = cio_read(cio, 1);
952 for (i = 0; i < volume->numcomps; i++) {
953 tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;
955 j3d_read_cox(j3d, i);
959 static void j3d_write_coc(opj_j3d_t *j3d, int compno) {
962 opj_cp_t *cp = j3d->cp;
963 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
964 opj_volume_t *volume = j3d->volume;
965 opj_cio_t *cio = j3d->cio;
967 cio_write(cio, J3D_MS_COC, 2); /* COC */
968 lenp = cio_tell(cio);
970 cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */
971 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
973 j3d_write_cox(j3d, compno);
975 len = cio_tell(cio) - lenp;
977 cio_write(cio, len, 2); /* Lcoc */
978 cio_seek(cio, lenp + len);
981 static void j3d_read_coc(opj_j3d_t *j3d) {
984 opj_cp_t *cp = j3d->cp;
985 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
986 opj_volume_t *volume = j3d->volume;
987 opj_cio_t *cio = j3d->cio;
989 len = cio_read(cio, 2); /* Lcoc */
990 compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */
991 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
992 j3d_read_cox(j3d, compno);
995 static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {
996 int bandno, numbands;
999 opj_cp_t *cp = j3d->cp;
1000 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1001 opj_tccp_t *tccp = &tcp->tccps[compno];
1002 opj_cio_t *cio = j3d->cio;
1004 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx : Table A28 de 15444-1*/
1006 if (j3d->cinfo->codec_format == CODEC_J2K)
1007 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2;
1008 else if (j3d->cinfo->codec_format == CODEC_J3D) {
1009 int diff = tccp->numresolution[0] - tccp->numresolution[2];
1010 numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
1013 for (bandno = 0; bandno < numbands; bandno++) {
1014 expn = tccp->stepsizes[bandno].expn;
1015 mant = tccp->stepsizes[bandno].mant;
1017 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1018 cio_write(cio, expn << 3, 1); /* SPqcx_i */
1020 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1025 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
1027 int bandno, numbands;
1029 opj_cp_t *cp = j3d->cp;
1030 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1031 opj_tccp_t *tccp = &tcp->tccps[compno];
1032 opj_cio_t *cio = j3d->cio;
1034 tmp = cio_read(cio, 1); /* Sqcx */
1035 tccp->qntsty = tmp & 0x1f;
1036 tccp->numgbits = tmp >> 5;
1038 /*Numbands = 1 si SIQNT
1040 (len - 1) / 2 si SEQNT */
1041 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1043 for (bandno = 0; bandno < numbands; bandno++) {
1045 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1046 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
1049 tmp = cio_read(cio, 2); /* SPqcx_i */
1053 tccp->stepsizes[bandno].expn = expn;
1054 tccp->stepsizes[bandno].mant = mant;
1057 /* Add Antonin : if scalar_derived -> compute other stepsizes */
1058 if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
1059 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
1060 int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
1061 tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
1062 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1068 static void j3d_write_qcd(opj_j3d_t *j3d) {
1071 opj_cio_t *cio = j3d->cio;
1073 cio_write(cio, J3D_MS_QCD, 2); /* QCD */
1074 lenp = cio_tell(cio);
1076 j3d_write_qcx(j3d, 0); /* Sqcd*/
1077 len = cio_tell(cio) - lenp;
1078 cio_seek(cio, lenp);
1079 cio_write(cio, len, 2); /* Lqcd */
1080 cio_seek(cio, lenp + len);
1083 static void j3d_read_qcd(opj_j3d_t *j3d) {
1086 opj_cio_t *cio = j3d->cio;
1087 opj_volume_t *volume = j3d->volume;
1089 len = cio_read(cio, 2); /* Lqcd */
1090 pos = cio_tell(cio);
1091 for (i = 0; i < volume->numcomps; i++) {
1093 j3d_read_qcx(j3d, i, len - 2);
1097 static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
1100 opj_cio_t *cio = j3d->cio;
1102 cio_write(cio, J3D_MS_QCC, 2); /* QCC */
1103 lenp = cio_tell(cio);
1105 cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2); /* Cqcc */
1106 j3d_write_qcx(j3d, compno);
1107 len = cio_tell(cio) - lenp;
1108 cio_seek(cio, lenp);
1109 cio_write(cio, len, 2); /* Lqcc */
1110 cio_seek(cio, lenp + len);
1113 static void j3d_read_qcc(opj_j3d_t *j3d) {
1115 int numcomp = j3d->volume->numcomps;
1116 opj_cio_t *cio = j3d->cio;
1118 len = cio_read(cio, 2); /* Lqcc */
1119 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1120 j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1123 static void j3d_write_poc(opj_j3d_t *j3d) {
1124 int len, numpchgs, i;
1126 int numcomps = j3d->volume->numcomps;
1128 opj_cp_t *cp = j3d->cp;
1129 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1130 opj_tccp_t *tccp = &tcp->tccps[0];
1131 opj_cio_t *cio = j3d->cio;
1133 numpchgs = tcp->numpocs;
1134 cio_write(cio, J3D_MS_POC, 2); /* POC */
1135 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1136 cio_write(cio, len, 2); /* Lpoc */
1137 for (i = 0; i < numpchgs; i++) {
1138 opj_poc_t *poc = &tcp->pocs[i];
1139 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1140 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1141 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1142 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1143 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1144 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
1145 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1146 poc->compno1 = int_min(poc->compno1, numcomps);
1147 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1151 static void j3d_read_poc(opj_j3d_t *j3d) {
1152 int len, numpchgs, i, old_poc;
1154 int numcomps = j3d->volume->numcomps;
1156 opj_cp_t *cp = j3d->cp;
1157 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1158 opj_tccp_t *tccp = &tcp->tccps[0];
1159 opj_cio_t *cio = j3d->cio;
1161 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1163 len = cio_read(cio, 2); /* Lpoc */
1164 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1166 for (i = old_poc; i < numpchgs + old_poc; i++) {
1168 poc = &tcp->pocs[i];
1169 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1170 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1171 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
1172 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
1173 poc->compno1 = int_min(
1174 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1175 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1178 tcp->numpocs = numpchgs + old_poc - 1;
1181 static void j3d_read_crg(opj_j3d_t *j3d) {
1182 int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
1184 opj_cio_t *cio = j3d->cio;
1185 int numcomps = j3d->volume->numcomps;
1187 len = cio_read(cio, 2); /* Lcrg */
1188 for (i = 0; i < numcomps; i++) {
1189 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1190 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1191 Zcrg_i = cio_read(cio, 2); /* Zcrg_i */
1195 static void j3d_read_tlm(opj_j3d_t *j3d) {
1196 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1197 long int Ttlm_i, Ptlm_i;
1199 opj_cio_t *cio = j3d->cio;
1201 len = cio_read(cio, 2); /* Ltlm */
1202 Ztlm = cio_read(cio, 1); /* Ztlm */
1203 Stlm = cio_read(cio, 1); /* Stlm */
1204 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1205 SP = (Stlm >> 6) & 0x01;
1206 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1207 for (i = 0; i < tile_tlm; i++) {
1208 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1209 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1213 static void j3d_read_plm(opj_j3d_t *j3d) {
1214 int len, i, Zplm, Nplm, add, packet_len = 0;
1216 opj_cio_t *cio = j3d->cio;
1218 len = cio_read(cio, 2); /* Lplm */
1219 Zplm = cio_read(cio, 1); /* Zplm */
1222 Nplm = cio_read(cio, 4); /* Nplm */
1224 for (i = Nplm; i > 0; i--) {
1225 add = cio_read(cio, 1);
1227 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1228 if ((add & 0x80) == 0) {
1238 static void j3d_read_plt(opj_j3d_t *j3d) {
1239 int len, i, Zplt, packet_len = 0, add;
1241 opj_cio_t *cio = j3d->cio;
1243 len = cio_read(cio, 2); /* Lplt */
1244 Zplt = cio_read(cio, 1); /* Zplt */
1245 for (i = len - 3; i > 0; i--) {
1246 add = cio_read(cio, 1);
1247 packet_len = (packet_len << 7) + add; /* Iplt_i */
1248 if ((add & 0x80) == 0) {
1255 static void j3d_read_ppm(opj_j3d_t *j3d) {
1256 int len, Z_ppm, i, j;
1259 opj_cp_t *cp = j3d->cp;
1260 opj_cio_t *cio = j3d->cio;
1262 len = cio_read(cio, 2);
1265 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1268 if (cp->ppm_previous == 0) {
1269 N_ppm = cio_read(cio, 4); /* N_ppm */
1272 N_ppm = cp->ppm_previous;
1275 if (Z_ppm == 0) { /* First PPM marker */
1276 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1277 cp->ppm_data_first = cp->ppm_data;
1278 cp->ppm_len = N_ppm;
1279 } else { /* NON-first PPM marker */
1280 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1281 cp->ppm_data_first = cp->ppm_data;
1282 cp->ppm_len = N_ppm + cp->ppm_store;
1284 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1285 cp->ppm_data[j] = cio_read(cio, 1);
1289 break; /* Case of non-finished packet header in present marker but finished in next one */
1291 cp->ppm_previous = i - 1;
1296 static void j3d_read_ppt(opj_j3d_t *j3d) {
1297 int len, Z_ppt, i, j = 0;
1299 opj_cp_t *cp = j3d->cp;
1300 opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
1301 opj_cio_t *cio = j3d->cio;
1303 len = cio_read(cio, 2);
1304 Z_ppt = cio_read(cio, 1);
1306 if (Z_ppt == 0) { /* First PPT marker */
1307 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1308 tcp->ppt_data_first = tcp->ppt_data;
1310 tcp->ppt_len = len - 3;
1311 } else { /* NON-first PPT marker */
1312 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1313 tcp->ppt_data_first = tcp->ppt_data;
1314 tcp->ppt_len = len - 3 + tcp->ppt_store;
1317 for (i = len - 3; i > 0; i--) {
1318 tcp->ppt_data[j] = cio_read(cio, 1);
1324 static void j3d_write_sot(opj_j3d_t *j3d) {
1327 opj_cio_t *cio = j3d->cio;
1329 j3d->sot_start = cio_tell(cio);
1330 cio_write(cio, J3D_MS_SOT, 2); /* SOT */
1331 lenp = cio_tell(cio);
1332 cio_skip(cio, 2); /* Lsot (further) */
1333 cio_write(cio, j3d->curtileno, 2); /* Isot */
1334 cio_skip(cio, 4); /* Psot (further in j3d_write_sod) */
1335 cio_write(cio, 0, 1); /* TPsot */
1336 cio_write(cio, 1, 1); /* TNsot (no of tile-parts of this tile in this codestream)*/
1337 len = cio_tell(cio) - lenp;
1338 cio_seek(cio, lenp);
1339 cio_write(cio, len, 2); /* Lsot */
1340 cio_seek(cio, lenp + len);
1343 static void j3d_read_sot(opj_j3d_t *j3d) {
1344 int len, tileno, totlen, partno, numparts, i;
1345 opj_tcp_t *tcp = NULL;
1348 opj_cp_t *cp = j3d->cp;
1349 opj_cio_t *cio = j3d->cio;
1351 len = cio_read(cio, 2);
1352 tileno = cio_read(cio, 2);
1354 if (cp->tileno_size == 0) {
1355 cp->tileno[cp->tileno_size] = tileno;
1359 while (i < cp->tileno_size && status == 0) {
1360 status = cp->tileno[i] == tileno ? 1 : 0;
1364 cp->tileno[cp->tileno_size] = tileno;
1369 totlen = cio_read(cio, 4);
1371 totlen = cio_numbytesleft(cio) + 8;
1373 partno = cio_read(cio, 1);
1374 numparts = cio_read(cio, 1);
1376 j3d->curtileno = tileno;
1377 j3d->eot = cio_getbp(cio) - 12 + totlen;
1378 j3d->state = J3D_STATE_TPH;
1379 tcp = &cp->tcps[j3d->curtileno];
1381 if (tcp->first == 1) {
1383 /* Initialization PPT */
1384 opj_tccp_t *tmp = tcp->tccps;
1385 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
1387 tcp->ppt_data = NULL;
1388 tcp->ppt_data_first = NULL;
1391 for (i = 0; i < j3d->volume->numcomps; i++) {
1392 tcp->tccps[i] = j3d->default_tcp->tccps[i];
1394 cp->tcps[j3d->curtileno].first = 0;
1398 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
1401 opj_tcp_t *tcp = NULL;
1402 opj_volume_info_t *volume_info = NULL;
1404 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1405 opj_cp_t *cp = j3d->cp;
1406 opj_cio_t *cio = j3d->cio;
1408 cio_write(cio, J3D_MS_SOD, 2);
1409 if (j3d->curtileno == 0) {
1410 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
1414 volume_info = j3d->volume_info;
1415 if (volume_info && volume_info->index_on) {
1416 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
1420 tcp = &cp->tcps[j3d->curtileno];
1421 for (layno = 0; layno < tcp->numlayers; layno++) {
1422 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
1426 volume_info->num = 0;
1429 l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
1431 /* Writing Psot in SOT marker */
1432 totlen = cio_tell(cio) + l - j3d->sot_start;
1433 cio_seek(cio, j3d->sot_start + 6);
1434 cio_write(cio, totlen, 4);
1435 cio_seek(cio, j3d->sot_start + totlen);
1438 static void j3d_read_sod(opj_j3d_t *j3d) {
1439 int len, truncate = 0, i;
1440 unsigned char *data = NULL, *data_ptr = NULL;
1442 opj_cio_t *cio = j3d->cio;
1443 int curtileno = j3d->curtileno;
1445 len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1447 if (len == cio_numbytesleft(cio) + 1) {
1448 truncate = 1; /* Case of a truncate codestream */
1451 data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
1453 for (i = 0; i < j3d->tile_len[curtileno]; i++) {
1454 data[i] = j3d->tile_data[curtileno][i];
1457 data_ptr = data + j3d->tile_len[curtileno];
1458 for (i = 0; i < len; i++) {
1459 data_ptr[i] = cio_read(cio, 1);
1462 j3d->tile_len[curtileno] += len;
1463 opj_free(j3d->tile_data[curtileno]);
1464 j3d->tile_data[curtileno] = data;
1467 j3d->state = J3D_STATE_TPHSOT;
1469 j3d->state = J3D_STATE_NEOC; /* RAJOUTE !! */
1473 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
1475 opj_cp_t *cp = j3d->cp;
1476 opj_tcp_t *tcp = &cp->tcps[tileno];
1477 opj_cio_t *cio = j3d->cio;
1478 int numcomps = j3d->volume->numcomps;
1480 cio_write(cio, J3D_MS_RGN, 2); /* RGN */
1481 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1482 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1483 cio_write(cio, 0, 1); /* Srgn */
1484 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1487 static void j3d_read_rgn(opj_j3d_t *j3d) {
1488 int len, compno, roisty;
1490 opj_cp_t *cp = j3d->cp;
1491 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1492 opj_cio_t *cio = j3d->cio;
1493 int numcomps = j3d->volume->numcomps;
1495 len = cio_read(cio, 2); /* Lrgn */
1496 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1497 roisty = cio_read(cio, 1); /* Srgn */
1498 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1501 static void j3d_write_eoc(opj_j3d_t *j3d) {
1502 opj_cio_t *cio = j3d->cio;
1503 /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
1504 cio_write(cio, J3D_MS_EOC, 2);
1507 static void j3d_read_eoc(opj_j3d_t *j3d) {
1510 #ifndef NO_PACKETS_DECODING
1511 opj_tcd_t *tcd = tcd_create(j3d->cinfo);
1512 tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
1513 /*j3d_dump_volume(stdout, tcd->volume);
1514 j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
1515 for (i = 0; i < j3d->cp->tileno_size; i++) {
1516 tileno = j3d->cp->tileno[i];
1517 /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/
1518 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
1519 opj_free(j3d->tile_data[tileno]);
1520 j3d->tile_data[tileno] = NULL;
1522 tcd_free_decode(tcd);
1525 for (i = 0; i < j3d->cp->tileno_size; i++) {
1526 tileno = j3d->cp->tileno[i];
1527 opj_free(j3d->tile_data[tileno]);
1528 j3d->tile_data[tileno] = NULL;
1532 j3d->state = J3D_STATE_MT;
1535 static void j3d_read_unk(opj_j3d_t *j3d) {
1536 opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
1539 static opj_atk_t atk_info_wt[] = {
1540 {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/
1541 {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/
1542 {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/
1543 {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/
1544 {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/
1545 {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/
1546 {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},
1547 {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/
1548 {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}}, /* WT 5-3 IRR*/
1549 {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}} /* WT 13-7 REV*/
1552 typedef struct opj_dec_mstabent {
1555 /** value of the state when the marker can appear */
1557 /** action linked to the marker */
1558 void (*handler) (opj_j3d_t *j3d);
1559 } opj_dec_mstabent_t;
1561 opj_dec_mstabent_t j3d_dec_mstab[] = {
1562 {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
1563 {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
1564 {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
1565 {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
1566 {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
1567 {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
1568 {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},
1569 {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
1570 {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
1571 {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
1572 {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
1573 {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
1574 {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
1575 {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
1576 {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
1577 {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
1578 {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
1579 {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
1581 {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
1582 {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
1583 {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
1584 {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
1585 {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
1586 /*, -->must define the j3d_read functions
1587 {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
1588 {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
1589 {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
1590 {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
1591 {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
1592 {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
1593 {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
1594 {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
1595 {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
1596 {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
1600 Read the lookup table containing all the marker, status and action
1601 @param id Marker value
1603 static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
1604 opj_dec_mstabent_t *e;
1605 for (e = j3d_dec_mstab; e->id != 0; e++) {
1613 /* ----------------------------------------------------------------------- */
1614 /* J3D / JPT decoder interface */
1615 /* ----------------------------------------------------------------------- */
1617 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
1618 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1621 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1622 if(!j3d->default_tcp) {
1630 void j3d_destroy_decompress(opj_j3d_t *j3d) {
1633 if(j3d->tile_len != NULL) {
1634 opj_free(j3d->tile_len);
1636 if(j3d->tile_data != NULL) {
1637 opj_free(j3d->tile_data);
1639 if(j3d->default_tcp != NULL) {
1640 opj_tcp_t *default_tcp = j3d->default_tcp;
1641 if(default_tcp->ppt_data_first != NULL) {
1642 opj_free(default_tcp->ppt_data_first);
1644 if(j3d->default_tcp->tccps != NULL) {
1645 opj_free(j3d->default_tcp->tccps);
1647 opj_free(j3d->default_tcp);
1649 if(j3d->cp != NULL) {
1650 opj_cp_t *cp = j3d->cp;
1651 if(cp->tcps != NULL) {
1652 for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
1653 if(cp->tcps[i].ppt_data_first != NULL) {
1654 opj_free(cp->tcps[i].ppt_data_first);
1656 if(cp->tcps[i].tccps != NULL) {
1657 opj_free(cp->tcps[i].tccps);
1662 if(cp->ppm_data_first != NULL) {
1663 opj_free(cp->ppm_data_first);
1665 if(cp->tileno != NULL) {
1666 opj_free(cp->tileno);
1668 if(cp->comment != NULL) {
1669 opj_free(cp->comment);
1678 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
1679 if(j3d && parameters) {
1680 /* create and initialize the coding parameters structure */
1681 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1682 cp->reduce[0] = parameters->cp_reduce[0];
1683 cp->reduce[1] = parameters->cp_reduce[1];
1684 cp->reduce[2] = parameters->cp_reduce[2];
1685 cp->layer = parameters->cp_layer;
1686 cp->bigendian = parameters->bigendian;
1688 /* MM: Settings of the following two member variables would take
1689 place during j3d_read_com. FIXME */
1690 cp->encoding_format = ENCOD_3EB;
1691 cp->transform_format = TRF_2D_DWT;
1693 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
1698 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
1699 opj_volume_t *volume = NULL;
1701 opj_common_ptr cinfo = j3d->cinfo;
1705 /* create an empty volume */
1706 volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
1707 j3d->volume = volume;
1709 j3d->state = J3D_STATE_MHSOC;
1712 opj_dec_mstabent_t *e;
1713 int id = cio_read(cio, 2);
1714 if (id >> 8 != 0xff) {
1715 opj_volume_destroy(volume);
1716 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1719 e = j3d_dec_mstab_lookup(id);
1720 /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/
1721 if (!(j3d->state & e->states)) {
1722 opj_volume_destroy(volume);
1723 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1729 /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/
1730 if (j3d->state == J3D_STATE_MT) {
1733 if (j3d->state == J3D_STATE_NEOC) {
1737 if (j3d->state == J3D_STATE_NEOC) {
1741 if (j3d->state != J3D_STATE_MT) {
1742 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1748 /* ----------------------------------------------------------------------- */
1749 /* J3D encoder interface */
1750 /* ----------------------------------------------------------------------- */
1752 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
1753 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1760 void j3d_destroy_compress(opj_j3d_t *j3d) {
1765 if(j3d->volume_info != NULL) {
1766 opj_volume_info_t *volume_info = j3d->volume_info;
1767 if (volume_info->index_on && j3d->cp) {
1768 opj_cp_t *cp = j3d->cp;
1769 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1770 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
1771 opj_free(tile_info->thresh);
1772 opj_free(tile_info->packet);
1774 opj_free(volume_info->tile);
1776 opj_free(volume_info);
1778 if(j3d->cp != NULL) {
1779 opj_cp_t *cp = j3d->cp;
1782 opj_free(cp->comment);
1785 opj_free(cp->matrice);
1787 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1788 opj_free(cp->tcps[tileno].tccps);
1797 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
1798 int i, j, tileno, numpocs_tile;
1799 opj_cp_t *cp = NULL;
1801 if(!j3d || !parameters || ! volume) {
1805 /* create and initialize the coding parameters structure */
1806 cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1808 /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
1811 /* set default values for cp */
1816 /* copy user encoding parameters */
1817 cp->disto_alloc = parameters->cp_disto_alloc;
1818 cp->fixed_alloc = parameters->cp_fixed_alloc;
1819 cp->fixed_quality = parameters->cp_fixed_quality;
1821 /* transform and coding method */
1822 cp->transform_format = parameters->transform_format;
1823 cp->encoding_format = parameters->encoding_format;
1825 /* mod fixed_quality */
1826 if(parameters->cp_matrice) {
1827 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
1828 cp->matrice = (int *) opj_malloc(array_size);
1829 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1832 /* creation of an index file ? */
1833 cp->index_on = parameters->index_on;
1835 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
1839 cp->tdx = parameters->cp_tdx;
1840 cp->tdy = parameters->cp_tdy;
1841 cp->tdz = parameters->cp_tdz;
1843 cp->tx0 = parameters->cp_tx0;
1844 cp->ty0 = parameters->cp_ty0;
1845 cp->tz0 = parameters->cp_tz0;
1846 /* comment string */
1847 if(parameters->cp_comment) {
1848 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1850 strcpy(cp->comment, parameters->cp_comment);
1854 /*calculate other encoding parameters*/
1855 if (parameters->tile_size_on) {
1856 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
1857 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
1858 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
1860 cp->tdx = volume->x1 - cp->tx0;
1861 cp->tdy = volume->y1 - cp->ty0;
1862 cp->tdz = volume->z1 - cp->tz0;
1865 /* initialize the multiple tiles */
1866 /* ---------------------------- */
1867 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
1869 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
1870 opj_tcp_t *tcp = &cp->tcps[tileno];
1871 tcp->numlayers = parameters->tcp_numlayers;
1872 for (j = 0; j < tcp->numlayers; j++) {
1873 if (cp->fixed_quality) { /* add fixed_quality */
1874 tcp->distoratio[j] = parameters->tcp_distoratio[j];
1876 tcp->rates[j] = parameters->tcp_rates[j];
1879 tcp->csty = parameters->csty;
1880 tcp->prg = parameters->prog_order;
1881 tcp->mct = volume->numcomps == 3 ? 1 : 0;
1885 if (parameters->numpocs) {
1886 /* initialisation of POC */
1888 for (i = 0; i < parameters->numpocs; i++) {
1889 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1890 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1891 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
1892 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
1893 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
1894 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
1895 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
1896 tcp_poc->prg = parameters->POC[numpocs_tile].prg;
1897 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
1902 tcp->numpocs = numpocs_tile;
1904 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
1906 for (i = 0; i < volume->numcomps; i++) {
1907 opj_tccp_t *tccp = &tcp->tccps[i];
1908 tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT; /* 0 => standard precint || 1 => custom-defined precinct */
1909 tccp->numresolution[0] = parameters->numresolution[0];
1910 tccp->numresolution[1] = parameters->numresolution[1];
1911 tccp->numresolution[2] = parameters->numresolution[2];
1912 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
1913 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
1914 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
1915 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
1916 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
1917 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
1918 tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]);
1919 tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]);
1920 tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]);
1921 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
1922 tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/
1924 /*ATK / transform */
1925 tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
1926 for (j = 0; j < 3; j++) {
1927 tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
1930 /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
1931 tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
1933 if (i == parameters->roi_compno) {
1934 tccp->roishift = parameters->roi_shift;
1938 /* Custom defined precints */
1939 if (parameters->csty & J3D_CCP_CSTY_PRT) {
1941 for (k = 0; k < 3; k++) {
1943 for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
1944 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
1945 if (parameters->prct_init[k][p] < 1) {
1946 tccp->prctsiz[k][j] = 1;
1948 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
1951 int res_spec = parameters->res_spec;
1952 int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
1953 if (size_prct < 1) {
1954 tccp->prctsiz[k][j] = 1;
1956 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
1964 for (k = 0; k < 3; k++) {
1965 for (j = 0; j < tccp->numresolution[k]; j++) {
1966 tccp->prctsiz[k][j] = 15;
1970 /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/
1971 dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
1977 Create an index file
1981 @param index Index filename
1982 @return Returns 1 if successful, returns 0 otherwise
1984 static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
1986 int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
1987 FILE *stream = NULL;
1988 double total_disto = 0;
1990 volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
1992 stream = fopen(index, "w");
1994 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
1998 fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
1999 fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
2000 fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
2001 fprintf(stream, "PROG\t%d\n", volume_info->prog);
2002 fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
2003 fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
2004 fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
2005 fprintf(stream, "LAYER\t%d\n", volume_info->layer);
2006 fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
2008 fprintf(stream, "Precint sizes for each resolution:\n");
2009 for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
2010 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
2011 (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno])); /* based on tile 0 */
2013 fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
2014 fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
2015 fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
2016 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2017 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
2018 volume_info->tile[tileno].num_tile,
2019 volume_info->tile[tileno].start_pos,
2020 volume_info->tile[tileno].end_header,
2021 volume_info->tile[tileno].end_pos,
2022 volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
2023 volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
2026 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2027 int start_pos, end_pos;
2030 if (volume_info->prog == LRCP) { /* LRCP */
2031 fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n");
2032 for (layno = 0; layno < volume_info->layer; layno++) {
2033 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2034 for (compno = 0; compno < volume_info->comp; compno++) {
2035 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2036 for (precno = 0; precno < prec_max; precno++) {
2037 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2038 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2039 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2040 fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2041 total_disto += disto;
2048 else if (volume_info->prog == RLCP) { /* RLCP */
2050 fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto");
2052 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2053 for (layno = 0; layno < volume_info->layer; layno++) {
2054 for (compno = 0; compno < volume_info->comp; compno++) {
2055 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
2056 for (precno = 0; precno < prec_max; precno++) {
2057 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2058 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2059 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2060 fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2061 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2062 total_disto += disto;
2069 else if (volume_info->prog == RPCL) { /* RPCL */
2071 fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
2073 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2074 /* I suppose components have same XRsiz, YRsiz */
2075 /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/
2076 /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/
2077 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
2078 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
2079 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
2080 int x1 = x0 + volume_info->tile_x;
2081 int y1 = y0 + volume_info->tile_y;
2082 int z1 = z0 + volume_info->tile_z;
2083 for(z = z0; z < z1; z++) {
2084 for(y = y0; y < y1; y++) {
2085 for(x = x0; x < x1; x++) {
2086 for (compno = 0; compno < volume_info->comp; compno++) {
2087 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2088 for (precno = 0; precno < prec_max; precno++) {
2089 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2090 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2091 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2092 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2093 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2094 int precno_y = (int) floor( (float)precno/(float)pcnx );
2095 if (precno_y*pcy == y ) {
2096 if (precno_x*pcx == x ) {
2097 for (layno = 0; layno < volume_info->layer; layno++) {
2098 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2099 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2100 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2101 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2102 pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
2103 total_disto += disto;
2115 else if (volume_info->prog == PCRL) { /* PCRL */
2116 /* I suppose components have same XRsiz, YRsiz */
2117 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2118 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2119 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2120 int x1 = x0 + volume_info->tile_x;
2121 int y1 = y0 + volume_info->tile_y;
2122 int z1 = z0 + volume_info->tile_z;
2124 fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
2126 for(z = z0; z < z1; z++) {
2127 for(y = y0; y < y1; y++) {
2128 for(x = x0; x < x1; x++) {
2129 for (compno = 0; compno < volume_info->comp; compno++) {
2130 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2131 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
2132 for (precno = 0; precno < prec_max; precno++) {
2133 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2134 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2135 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2136 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2137 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2138 int precno_y = (int) floor( (float)precno/(float)pcnx );
2139 int precno_z = (int) floor( (float)precno/(float)pcnx );
2140 if (precno_z*pcz == z ) {
2141 if (precno_y*pcy == y ) {
2142 if (precno_x*pcx == x ) {
2143 for (layno = 0; layno < volume_info->layer; layno++) {
2144 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2145 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2146 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2147 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2148 pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
2149 total_disto += disto;
2164 fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
2166 for (compno = 0; compno < volume_info->comp; compno++) {
2167 /* I suppose components have same XRsiz, YRsiz */
2168 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2169 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2170 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2171 int x1 = x0 + volume_info->tile_x;
2172 int y1 = y0 + volume_info->tile_y;
2173 int z1 = z0 + volume_info->tile_z;
2174 for(z = z0; z < z1; z++) {
2175 for(y = y0; y < y1; y++) {
2176 for(x = x0; x < x1; x++) {
2177 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2178 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2179 for (precno = 0; precno < prec_max; precno++) {
2180 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2181 int pcny = volume_info->tile[tileno].prctno[1][resno];
2182 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2183 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2184 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2185 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2186 int precno_y = (int) floor( (float)precno/(float)pcnx );
2187 int precno_z = 0; /*???*/
2188 if (precno_z*pcz == z ) {
2189 if (precno_y*pcy == y ) {
2190 if (precno_x*pcx == x ) {
2191 for (layno = 0; layno < volume_info->layer; layno++) {
2192 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2193 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2194 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2195 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2196 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
2197 total_disto += disto;
2212 fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max); /* SE max */
2213 fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto); /* SE totale */
2221 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
2223 opj_volume_info_t *volume_info = NULL;
2224 opj_cp_t *cp = NULL;
2225 opj_tcd_t *tcd = NULL; /* TCD component */
2228 j3d->volume = volume;
2231 /*j3d_dump_volume(stdout, volume);
2232 j3d_dump_cp(stdout, volume, cp);*/
2235 volume_info = j3d->volume_info;
2236 if (volume_info && cp->index_on) {
2237 volume_info->index_on = cp->index_on;
2238 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
2239 volume_info->volume_w = volume->x1 - volume->x0;
2240 volume_info->volume_h = volume->y1 - volume->y0;
2241 volume_info->volume_l = volume->z1 - volume->z0;
2242 volume_info->prog = (&cp->tcps[0])->prg;
2243 volume_info->tw = cp->tw;
2244 volume_info->th = cp->th;
2245 volume_info->tl = cp->tl;
2246 volume_info->tile_x = cp->tdx; /* new version parser */
2247 volume_info->tile_y = cp->tdy; /* new version parser */
2248 volume_info->tile_z = cp->tdz; /* new version parser */
2249 volume_info->tile_Ox = cp->tx0; /* new version parser */
2250 volume_info->tile_Oy = cp->ty0; /* new version parser */
2251 volume_info->tile_Oz = cp->tz0; /* new version parser */
2252 volume_info->transform_format = cp->transform_format;
2253 volume_info->encoding_format = cp->encoding_format;
2254 volume_info->comp = volume->numcomps;
2255 volume_info->layer = (&cp->tcps[0])->numlayers;
2256 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
2257 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
2258 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
2259 volume_info->D_max = 0; /* ADD Marcela */
2265 if (j3d->cinfo->codec_format == CODEC_J3D) {
2270 /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/
2275 for (compno = 0; compno < volume->numcomps; compno++) {
2276 opj_tcp_t *tcp = &cp->tcps[0];
2277 if (tcp->tccps[compno].roishift)
2278 j3d_write_rgn(j3d, compno, 0);
2280 /*Optional 15444-2 markers*/
2281 if (j3d->cp->tcps->tccps[0].atk != NULL)
2283 if (j3d->volume->comps[0].dcoffset != 0)
2287 if(volume_info && volume_info->index_on) {
2288 volume_info->main_head_end = cio_tell(cio) - 1;
2292 /* create the tile encoder */
2293 tcd = tcd_create(j3d->cinfo);
2295 /* encode each tile */
2296 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
2297 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
2299 j3d->curtileno = tileno;
2301 /* initialisation before tile encoding */
2303 tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
2305 tcd_init_encode(tcd, volume, cp, j3d->curtileno);
2309 if(volume_info && volume_info->index_on) {
2310 volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
2311 volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
2317 for (compno = 1; compno < volume->numcomps; compno++) {
2318 j3d_write_coc(j3d, compno);
2319 j3d_write_qcc(j3d, compno);
2322 if (cp->tcps[tileno].numpocs) {
2325 j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/
2328 if(volume_info && volume_info->index_on) {
2329 volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
2334 /* destroy the tile encoder */
2335 tcd_free_encode(tcd);
2340 /* Creation of the index file */
2341 if(volume_info && volume_info->index_on) {
2342 if(!j3d_create_index(j3d, cio, volume_info, index)) {
2343 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2351 /* ----------------------------------------------------------------------- */