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.
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.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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.
37 #include "opj_includes.h"
39 /** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
42 /** @name Funciones locales */
46 Write the SOC marker (Start Of Codestream)
49 static void j3d_write_soc(opj_j3d_t *j3d);
51 Read the SOC marker (Start of Codestream)
54 static void j3d_read_soc(opj_j3d_t *j3d);
56 Write the SIZ marker (2D volume and tile size)
59 static void j3d_write_siz(opj_j3d_t *j3d);
61 Read the SIZ marker (2D volume and tile size)
64 static void j3d_read_siz(opj_j3d_t *j3d);
66 Write the NSI marker (3rd volume and tile size)
69 static void j3d_write_nsi(opj_j3d_t *j3d);
71 Read the NSI marker (3rd volume and tile size)
74 static void j3d_read_nsi(opj_j3d_t *j3d);
76 Write the COM marker (comment)
79 static void j3d_write_com(opj_j3d_t *j3d);
81 Read the COM marker (comment)
84 static void j3d_read_com(opj_j3d_t *j3d);
86 Write the value concerning the specified component in the marker COD and COC
88 @param compno Number of the component concerned by the information written
90 static void j3d_write_cox(opj_j3d_t *j3d, int compno);
92 Read the value concerning the specified component in the marker COD and COC
94 @param compno Number of the component concerned by the information read
96 static void j3d_read_cox(opj_j3d_t *j3d, int compno);
98 Write the COD marker (coding style default)
101 static void j3d_write_cod(opj_j3d_t *j3d);
103 Read the COD marker (coding style default)
104 @param j3d J3D handle
106 static void j3d_read_cod(opj_j3d_t *j3d);
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
112 static void j3d_write_coc(opj_j3d_t *j3d, int compno);
114 Read the COC marker (coding style component)
115 @param j3d J3D handle
117 static void j3d_read_coc(opj_j3d_t *j3d);
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
123 static void j3d_write_qcx(opj_j3d_t *j3d, int compno);
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
130 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len);
132 Write the QCD marker (quantization default)
133 @param j3d J3D handle
135 static void j3d_write_qcd(opj_j3d_t *j3d);
137 Read the QCD marker (quantization default)
138 @param j3d J3D handle
140 static void j3d_read_qcd(opj_j3d_t *j3d);
142 Write the QCC marker (quantization component)
143 @param j3d J3D handle
144 @param compno Number of the component concerned by the information written
146 static void j3d_write_qcc(opj_j3d_t *j3d, int compno);
148 Read the QCC marker (quantization component)
149 @param j3d J3D handle
151 static void j3d_read_qcc(opj_j3d_t *j3d);
153 Write the POC marker (progression order change)
154 @param j3d J3D handle
156 static void j3d_write_poc(opj_j3d_t *j3d);
158 Read the POC marker (progression order change)
159 @param j3d J3D handle
161 static void j3d_read_poc(opj_j3d_t *j3d);
163 Read the CRG marker (component registration)
164 @param j3d J3D handle
166 static void j3d_read_crg(opj_j3d_t *j3d);
168 Read the TLM marker (tile-part lengths)
169 @param j3d J3D handle
171 static void j3d_read_tlm(opj_j3d_t *j3d);
173 Read the PLM marker (packet length, main header)
174 @param j3d J3D handle
176 static void j3d_read_plm(opj_j3d_t *j3d);
178 Read the PLT marker (packet length, tile-part header)
179 @param j3d J3D handle
181 static void j3d_read_plt(opj_j3d_t *j3d);
183 Read the PPM marker (packet packet headers, main header)
184 @param j3d J3D handle
186 static void j3d_read_ppm(opj_j3d_t *j3d);
188 Read the PPT marker (packet packet headers, tile-part header)
189 @param j3d J3D handle
191 static void j3d_read_ppt(opj_j3d_t *j3d);
193 Write the SOT marker (start of tile-part)
194 @param j3d J3D handle
196 static void j3d_write_sot(opj_j3d_t *j3d);
198 Read the SOT marker (start of tile-part)
199 @param j3d J3D handle
201 static void j3d_read_sot(opj_j3d_t *j3d);
203 Write the SOD marker (start of data)
204 @param j3d J3D handle
205 @param tile_coder Pointer to a TCD handle
207 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder);
209 Read the SOD marker (start of data)
210 @param j3d J3D handle
212 static void j3d_read_sod(opj_j3d_t *j3d);
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
219 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno);
221 Read the RGN marker (region-of-interest)
222 @param j3d J3D handle
224 static void j3d_read_rgn(opj_j3d_t *j3d);
226 Write the EOC marker (end of codestream)
227 @param j3d J3D handle
229 static void j3d_write_eoc(opj_j3d_t *j3d);
231 Read the EOC marker (end of codestream)
232 @param j3d J3D handle
234 static void j3d_read_eoc(opj_j3d_t *j3d);
236 Read an unknown marker
237 @param j3d J3D handle
239 static void j3d_read_unk(opj_j3d_t *j3d);
241 Write the CAP marker (extended capabilities)
242 @param j3d J3D handle
244 static void j3d_write_cap(opj_j3d_t *j3d);
246 Read the CAP marker (extended capabilities)
247 @param j3d J3D handle
249 static void j3d_read_cap(opj_j3d_t *j3d);
251 Write the DCO marker (Variable DC offset)
252 @param j3d J3D handle
254 static void j3d_write_dco(opj_j3d_t *j3d);
256 Read the DCO marker (Variable DC offset)
257 @param j3d J3D handle
259 static void j3d_read_dco(opj_j3d_t *j3d);
261 Write the ATK marker (arbitrary transformation kernel)
262 @param j3d J3D handle
264 static void j3d_write_atk(opj_j3d_t *j3d);
266 Read the ATK marker (arbitrary transformation kernel)
267 @param j3d J3D handle
269 static void j3d_read_atk(opj_j3d_t *j3d);
271 Write the CBD marker (component bit depth definition)
272 @param j3d J3D handle
274 static void j3d_write_cbd(opj_j3d_t *j3d);
276 Read the CBD marker (component bit depth definition)
277 @param j3d J3D handle
279 static void j3d_read_cbd(opj_j3d_t *j3d);
281 Write the MCT marker (multiple component transfomation definition)
282 @param j3d J3D handle
284 static void j3d_write_mct(opj_j3d_t *j3d);
286 Read the MCT marker (multiple component transfomation definition)
287 @param j3d J3D handle
289 static void j3d_read_mct(opj_j3d_t *j3d);
291 Write the MCC marker (multiple component transfomation collection)
292 @param j3d J3D handle
294 static void j3d_write_mcc(opj_j3d_t *j3d);
296 Read the MCC marker (multiple component transfomation collection)
297 @param j3d J3D handle
299 static void j3d_read_mcc(opj_j3d_t *j3d);
301 Write the MCO marker (multiple component transfomation ordering)
302 @param j3d J3D handle
304 static void j3d_write_mco(opj_j3d_t *j3d);
306 Read the MCO marker (multiple component transfomation ordering)
307 @param j3d J3D handle
309 static void j3d_read_mco(opj_j3d_t *j3d);
311 Write the NLT marker (non-linearity point transformation)
312 @param j3d J3D handle
314 static void j3d_write_nlt(opj_j3d_t *j3d);
316 Read the NLT marker (non-linearity point transformation)
317 @param j3d J3D handle
319 static void j3d_read_nlt(opj_j3d_t *j3d);
322 /* ----------------------------------------------------------------------- */
324 static void j3d_dump_volume(FILE *fd, opj_volume_t * vol) {
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);
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]);
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);
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);
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]);
396 fprintf(fd, " prch=");
397 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
398 fprintf(fd, "%d ", tccp->prctsiz[1][resno]);
401 fprintf(fd, " prcl=");
402 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
403 fprintf(fd, "%d ", tccp->prctsiz[2][resno]);
414 /* -----------------------------------------------------------------------
415 Extended capabilities
416 ------------------------------------------------------------------------*/
418 static void j3d_write_cap(opj_j3d_t *j3d){
421 opj_cio_t *cio = j3d->cio;
422 cio_write(cio, J3D_MS_CAP, 2); /* CAP */
423 lenp = cio_tell(cio);
425 cio_write(cio,J3D_CAP_10, 4);
428 cio_write(cio, 0x0, 2);
430 len = cio_tell(cio) - lenp;
432 cio_write(cio, len, 2); /* Lsiz */
433 cio_seek(cio, lenp + len);
436 static void j3d_read_cap(opj_j3d_t *j3d){
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);
445 assert( len == 2 + 4 + 2 );
447 static void j3d_write_nsi(opj_j3d_t *j3d) {
452 opj_cio_t *cio = j3d->cio;
453 opj_volume_t *volume = j3d->volume;
454 opj_cp_t *cp = j3d->cp;
456 cio_write(cio, J3D_MS_NSI, 2); /* NSI */
457 lenp = cio_tell(cio);
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 */
467 len = cio_tell(cio) - lenp;
469 cio_write(cio, len, 2); /* Lsiz */
470 cio_seek(cio, lenp + len);
473 static void j3d_read_nsi(opj_j3d_t *j3d) {
477 opj_cio_t *cio = j3d->cio;
478 opj_volume_t *volume = j3d->volume;
479 opj_cp_t *cp = j3d->cp;
481 len = cio_read(cio, 2); /* Lnsi */
482 ndim = cio_read(cio, 1); /* Ndim */
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 */
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));
500 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
502 cp->tcps[i].numpocs = 0;
503 cp->tcps[i].first = 1;
506 /* Initialization for PPM marker (Packets header)*/
509 cp->ppm_data_first = NULL;
510 cp->ppm_previous = 0;
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);
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;
522 static void j3d_write_dco(opj_j3d_t *j3d){
526 opj_cio_t *cio = j3d->cio;
527 opj_volume_t *volume = j3d->volume;
528 opj_cp_t *cp = j3d->cp;
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);
534 cio_write(cio, dcotype, 1);
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);
544 len = cio_tell(cio) - lenp;
546 cio_write(cio, len, 2); /* Ldco */
547 cio_seek(cio, lenp + len);
550 static void j3d_read_dco(opj_j3d_t *j3d){
554 opj_cio_t *cio = j3d->cio;
555 opj_volume_t *volume = j3d->volume;
556 opj_cp_t *cp = j3d->cp;
558 len = cio_read(cio, 2); /* Lsiz */
559 dcotype = cio_read(cio, 1); /*offset 8bit unsigned / 16bit signed integers*/
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;
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;
575 static void j3d_write_atk(opj_j3d_t *j3d){
578 opj_cio_t *cio = j3d->cio;
579 opj_volume_t *volume = j3d->volume;
580 opj_atk_t *atk = j3d->cp->tcps->tccps->atk;
582 cio_write(cio, J3D_MS_ATK, 2); /* ATK */
583 lenp = cio_tell(cio);
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);
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);
601 len = cio_tell(cio) - lenp;
603 cio_write(cio, len, 2); /* Latk */
604 cio_seek(cio, lenp + len);
606 static void j3d_read_atk(opj_j3d_t *j3d){
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;
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);
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);
637 static void j3d_write_cbd(opj_j3d_t *j3d){
639 static void j3d_read_cbd(opj_j3d_t *j3d){
641 static void j3d_write_mct(opj_j3d_t *j3d){
643 static void j3d_read_mct(opj_j3d_t *j3d){
645 static void j3d_write_mcc(opj_j3d_t *j3d){
647 static void j3d_read_mcc(opj_j3d_t *j3d){
649 static void j3d_write_mco(opj_j3d_t *j3d){
651 static void j3d_read_mco(opj_j3d_t *j3d){
653 static void j3d_write_nlt(opj_j3d_t *j3d){
655 static void j3d_read_nlt(opj_j3d_t *j3d){
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);
665 static void j3d_read_soc(opj_j3d_t *j3d) {
666 j3d->state = J3D_STATE_MHSIZ;
669 static void j3d_write_siz(opj_j3d_t *j3d) {
674 opj_cio_t *cio = j3d->cio;
675 opj_volume_t *volume = j3d->volume;
676 opj_cp_t *cp = j3d->cp;
678 cio_write(cio, J3D_MS_SIZ, 2); /* SIZ */
679 lenp = cio_tell(cio);
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 */
698 len = cio_tell(cio) - lenp;
700 cio_write(cio, len, 2); /* Lsiz */
701 cio_seek(cio, lenp + len);
704 static void j3d_read_siz(opj_j3d_t *j3d) {
707 opj_cio_t *cio = j3d->cio;
708 opj_volume_t *volume = j3d->volume;
709 opj_cp_t *cp = j3d->cp;
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 */
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++) {
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 */
737 if (j3d->cinfo->codec_format == CODEC_J2K){
740 volume->numslices = 1;
743 for (i = 0; i < volume->numcomps; i++)
744 volume->comps[i].dz = 1;
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));
754 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
756 cp->tcps[i].numpocs = 0;
757 cp->tcps[i].first = 1;
760 /* Initialization for PPM marker (Packets header)*/
763 cp->ppm_data_first = NULL;
764 cp->ppm_previous = 0;
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);
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;
779 static void j3d_write_com(opj_j3d_t *j3d) {
783 opj_cio_t *cio = j3d->cio;
785 cio_write(cio, J3D_MS_COM, 2);
786 lenp = cio_tell(cio);
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);
796 len = cio_tell(cio) - lenp;
798 cio_write(cio, len, 2);
799 cio_seek(cio, lenp + len);
802 static void j3d_read_com(opj_j3d_t *j3d) {
804 opj_cio_t *cio = j3d->cio;
806 len = cio_read(cio, 2);
807 cio_read(cio, 2); // read registration
809 /*opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);*/
811 cio_skip(cio, len - 4); /*posible comments*/
814 static void j3d_write_cox(opj_j3d_t *j3d, int compno) {
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;
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*/
828 if (j3d->cinfo->codec_format == CODEC_J3D) {
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*/
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*/
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*/
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*/
853 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1); /* SPcox (N_i) Table A9*/ }
857 static void j3d_read_cox(opj_j3d_t *j3d, int compno) {
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;
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;
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]);
879 if (j3d->cinfo->codec_format == CODEC_J3D) {
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) */
888 tccp->cblk[2] = tccp->cblk[0];
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) */
896 tccp->dwtid[1] = tccp->dwtid[0]; /* SPcox (L) */
897 tccp->dwtid[2] = tccp->dwtid[0]; /* SPcox (M) */
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;
910 static void j3d_write_cod(opj_j3d_t *j3d) {
912 opj_tcp_t *tcp = NULL;
915 opj_cio_t *cio = j3d->cio;
917 cio_write(cio, J3D_MS_COD, 2); /* COD */
919 lenp = cio_tell(cio);
923 tcp = &cp->tcps[j3d->curtileno];
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;
935 cio_write(cio, len, 2); /* Lcod */
936 cio_seek(cio, lenp + len);
939 static void j3d_read_cod(opj_j3d_t *j3d) {
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;
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);
957 for (i = 0; i < volume->numcomps; i++) {
958 tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;
960 j3d_read_cox(j3d, i);
964 static void j3d_write_coc(opj_j3d_t *j3d, int compno) {
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;
972 cio_write(cio, J3D_MS_COC, 2); /* COC */
973 lenp = cio_tell(cio);
975 cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */
976 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
978 j3d_write_cox(j3d, compno);
980 len = cio_tell(cio) - lenp;
982 cio_write(cio, len, 2); /* Lcoc */
983 cio_seek(cio, lenp + len);
986 static void j3d_read_coc(opj_j3d_t *j3d) {
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;
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);
1000 static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {
1001 int bandno, numbands;
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;
1009 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx : Table A28 de 15444-1*/
1011 if (j3d->cinfo->codec_format == CODEC_J2K)
1012 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2;
1013 else if (j3d->cinfo->codec_format == CODEC_J3D) {
1014 int diff = tccp->numresolution[0] - tccp->numresolution[2];
1015 numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
1018 for (bandno = 0; bandno < numbands; bandno++) {
1019 expn = tccp->stepsizes[bandno].expn;
1020 mant = tccp->stepsizes[bandno].mant;
1022 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1023 cio_write(cio, expn << 3, 1); /* SPqcx_i */
1025 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1030 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
1032 int bandno, numbands;
1034 opj_cp_t *cp = j3d->cp;
1035 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1036 opj_tccp_t *tccp = &tcp->tccps[compno];
1037 opj_cio_t *cio = j3d->cio;
1039 tmp = cio_read(cio, 1); /* Sqcx */
1040 tccp->qntsty = tmp & 0x1f;
1041 tccp->numgbits = tmp >> 5;
1043 /*Numbands = 1 si SIQNT
1045 (len - 1) / 2 si SEQNT */
1046 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1048 for (bandno = 0; bandno < numbands; bandno++) {
1050 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1051 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
1054 tmp = cio_read(cio, 2); /* SPqcx_i */
1058 tccp->stepsizes[bandno].expn = expn;
1059 tccp->stepsizes[bandno].mant = mant;
1062 /* Add Antonin : if scalar_derived -> compute other stepsizes */
1063 if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
1064 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
1065 int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
1066 tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
1067 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1073 static void j3d_write_qcd(opj_j3d_t *j3d) {
1076 opj_cio_t *cio = j3d->cio;
1078 cio_write(cio, J3D_MS_QCD, 2); /* QCD */
1079 lenp = cio_tell(cio);
1081 j3d_write_qcx(j3d, 0); /* Sqcd*/
1082 len = cio_tell(cio) - lenp;
1083 cio_seek(cio, lenp);
1084 cio_write(cio, len, 2); /* Lqcd */
1085 cio_seek(cio, lenp + len);
1088 static void j3d_read_qcd(opj_j3d_t *j3d) {
1091 opj_cio_t *cio = j3d->cio;
1092 opj_volume_t *volume = j3d->volume;
1094 len = cio_read(cio, 2); /* Lqcd */
1095 pos = cio_tell(cio);
1096 for (i = 0; i < volume->numcomps; i++) {
1098 j3d_read_qcx(j3d, i, len - 2);
1102 static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
1105 opj_cio_t *cio = j3d->cio;
1107 cio_write(cio, J3D_MS_QCC, 2); /* QCC */
1108 lenp = cio_tell(cio);
1110 cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2); /* Cqcc */
1111 j3d_write_qcx(j3d, compno);
1112 len = cio_tell(cio) - lenp;
1113 cio_seek(cio, lenp);
1114 cio_write(cio, len, 2); /* Lqcc */
1115 cio_seek(cio, lenp + len);
1118 static void j3d_read_qcc(opj_j3d_t *j3d) {
1120 int numcomp = j3d->volume->numcomps;
1121 opj_cio_t *cio = j3d->cio;
1123 len = cio_read(cio, 2); /* Lqcc */
1124 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1125 j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1128 static void j3d_write_poc(opj_j3d_t *j3d) {
1129 int len, numpchgs, i;
1131 int numcomps = j3d->volume->numcomps;
1133 opj_cp_t *cp = j3d->cp;
1134 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1135 opj_tccp_t *tccp = &tcp->tccps[0];
1136 opj_cio_t *cio = j3d->cio;
1138 numpchgs = tcp->numpocs;
1139 cio_write(cio, J3D_MS_POC, 2); /* POC */
1140 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1141 cio_write(cio, len, 2); /* Lpoc */
1142 for (i = 0; i < numpchgs; i++) {
1143 opj_poc_t *poc = &tcp->pocs[i];
1144 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1145 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1146 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1147 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1148 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1149 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
1150 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1151 poc->compno1 = int_min(poc->compno1, numcomps);
1152 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1156 static void j3d_read_poc(opj_j3d_t *j3d) {
1157 int len, numpchgs, i, old_poc;
1159 int numcomps = j3d->volume->numcomps;
1161 opj_cp_t *cp = j3d->cp;
1162 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1163 opj_tccp_t *tccp = &tcp->tccps[0];
1164 opj_cio_t *cio = j3d->cio;
1166 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1168 len = cio_read(cio, 2); /* Lpoc */
1169 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1171 for (i = old_poc; i < numpchgs + old_poc; i++) {
1173 poc = &tcp->pocs[i];
1174 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1175 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1176 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
1177 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
1178 poc->compno1 = int_min(
1179 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1180 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1183 tcp->numpocs = numpchgs + old_poc - 1;
1186 static void j3d_read_crg(opj_j3d_t *j3d) {
1187 int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
1189 opj_cio_t *cio = j3d->cio;
1190 int numcomps = j3d->volume->numcomps;
1192 len = cio_read(cio, 2); /* Lcrg */
1193 for (i = 0; i < numcomps; i++) {
1194 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1195 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1196 Zcrg_i = cio_read(cio, 2); /* Zcrg_i */
1200 static void j3d_read_tlm(opj_j3d_t *j3d) {
1201 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1202 long int Ttlm_i, Ptlm_i;
1204 opj_cio_t *cio = j3d->cio;
1206 len = cio_read(cio, 2); /* Ltlm */
1207 Ztlm = cio_read(cio, 1); /* Ztlm */
1208 Stlm = cio_read(cio, 1); /* Stlm */
1209 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1210 SP = (Stlm >> 6) & 0x01;
1211 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1212 for (i = 0; i < tile_tlm; i++) {
1213 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1214 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1218 static void j3d_read_plm(opj_j3d_t *j3d) {
1219 int len, i, Zplm, Nplm, add, packet_len = 0;
1221 opj_cio_t *cio = j3d->cio;
1223 len = cio_read(cio, 2); /* Lplm */
1224 Zplm = cio_read(cio, 1); /* Zplm */
1227 Nplm = cio_read(cio, 4); /* Nplm */
1229 for (i = Nplm; i > 0; i--) {
1230 add = cio_read(cio, 1);
1232 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1233 if ((add & 0x80) == 0) {
1243 static void j3d_read_plt(opj_j3d_t *j3d) {
1244 int len, i, Zplt, packet_len = 0, add;
1246 opj_cio_t *cio = j3d->cio;
1248 len = cio_read(cio, 2); /* Lplt */
1249 Zplt = cio_read(cio, 1); /* Zplt */
1250 for (i = len - 3; i > 0; i--) {
1251 add = cio_read(cio, 1);
1252 packet_len = (packet_len << 7) + add; /* Iplt_i */
1253 if ((add & 0x80) == 0) {
1260 static void j3d_read_ppm(opj_j3d_t *j3d) {
1261 int len, Z_ppm, i, j;
1264 opj_cp_t *cp = j3d->cp;
1265 opj_cio_t *cio = j3d->cio;
1267 len = cio_read(cio, 2);
1270 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1273 if (cp->ppm_previous == 0) {
1274 N_ppm = cio_read(cio, 4); /* N_ppm */
1277 N_ppm = cp->ppm_previous;
1280 if (Z_ppm == 0) { /* First PPM marker */
1281 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1282 cp->ppm_data_first = cp->ppm_data;
1283 cp->ppm_len = N_ppm;
1284 } else { /* NON-first PPM marker */
1285 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1286 cp->ppm_data_first = cp->ppm_data;
1287 cp->ppm_len = N_ppm + cp->ppm_store;
1289 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1290 cp->ppm_data[j] = cio_read(cio, 1);
1294 break; /* Case of non-finished packet header in present marker but finished in next one */
1296 cp->ppm_previous = i - 1;
1301 static void j3d_read_ppt(opj_j3d_t *j3d) {
1302 int len, Z_ppt, i, j = 0;
1304 opj_cp_t *cp = j3d->cp;
1305 opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
1306 opj_cio_t *cio = j3d->cio;
1308 len = cio_read(cio, 2);
1309 Z_ppt = cio_read(cio, 1);
1311 if (Z_ppt == 0) { /* First PPT marker */
1312 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1313 tcp->ppt_data_first = tcp->ppt_data;
1315 tcp->ppt_len = len - 3;
1316 } else { /* NON-first PPT marker */
1317 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1318 tcp->ppt_data_first = tcp->ppt_data;
1319 tcp->ppt_len = len - 3 + tcp->ppt_store;
1322 for (i = len - 3; i > 0; i--) {
1323 tcp->ppt_data[j] = cio_read(cio, 1);
1329 static void j3d_write_sot(opj_j3d_t *j3d) {
1332 opj_cio_t *cio = j3d->cio;
1334 j3d->sot_start = cio_tell(cio);
1335 cio_write(cio, J3D_MS_SOT, 2); /* SOT */
1336 lenp = cio_tell(cio);
1337 cio_skip(cio, 2); /* Lsot (further) */
1338 cio_write(cio, j3d->curtileno, 2); /* Isot */
1339 cio_skip(cio, 4); /* Psot (further in j3d_write_sod) */
1340 cio_write(cio, 0, 1); /* TPsot */
1341 cio_write(cio, 1, 1); /* TNsot (no of tile-parts of this tile in this codestream)*/
1342 len = cio_tell(cio) - lenp;
1343 cio_seek(cio, lenp);
1344 cio_write(cio, len, 2); /* Lsot */
1345 cio_seek(cio, lenp + len);
1348 static void j3d_read_sot(opj_j3d_t *j3d) {
1349 int len, tileno, totlen, partno, numparts, i;
1350 opj_tcp_t *tcp = NULL;
1353 opj_cp_t *cp = j3d->cp;
1354 opj_cio_t *cio = j3d->cio;
1356 len = cio_read(cio, 2);
1357 tileno = cio_read(cio, 2);
1359 if (cp->tileno_size == 0) {
1360 cp->tileno[cp->tileno_size] = tileno;
1364 while (i < cp->tileno_size && status == 0) {
1365 status = cp->tileno[i] == tileno ? 1 : 0;
1369 cp->tileno[cp->tileno_size] = tileno;
1374 totlen = cio_read(cio, 4);
1376 totlen = cio_numbytesleft(cio) + 8;
1378 partno = cio_read(cio, 1);
1379 numparts = cio_read(cio, 1);
1381 j3d->curtileno = tileno;
1382 j3d->eot = cio_getbp(cio) - 12 + totlen;
1383 j3d->state = J3D_STATE_TPH;
1384 tcp = &cp->tcps[j3d->curtileno];
1386 if (tcp->first == 1) {
1388 /* Initialization PPT */
1389 opj_tccp_t *tmp = tcp->tccps;
1390 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
1392 tcp->ppt_data = NULL;
1393 tcp->ppt_data_first = NULL;
1396 for (i = 0; i < j3d->volume->numcomps; i++) {
1397 tcp->tccps[i] = j3d->default_tcp->tccps[i];
1399 cp->tcps[j3d->curtileno].first = 0;
1403 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
1406 opj_tcp_t *tcp = NULL;
1407 opj_volume_info_t *volume_info = NULL;
1409 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1410 opj_cp_t *cp = j3d->cp;
1411 opj_cio_t *cio = j3d->cio;
1413 cio_write(cio, J3D_MS_SOD, 2);
1414 if (j3d->curtileno == 0) {
1415 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
1419 volume_info = j3d->volume_info;
1420 if (volume_info && volume_info->index_on) {
1421 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
1425 tcp = &cp->tcps[j3d->curtileno];
1426 for (layno = 0; layno < tcp->numlayers; layno++) {
1427 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
1431 volume_info->num = 0;
1434 l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
1436 /* Writing Psot in SOT marker */
1437 totlen = cio_tell(cio) + l - j3d->sot_start;
1438 cio_seek(cio, j3d->sot_start + 6);
1439 cio_write(cio, totlen, 4);
1440 cio_seek(cio, j3d->sot_start + totlen);
1443 static void j3d_read_sod(opj_j3d_t *j3d) {
1444 int len, truncate = 0, i;
1445 unsigned char *data = NULL, *data_ptr = NULL;
1447 opj_cio_t *cio = j3d->cio;
1448 int curtileno = j3d->curtileno;
1450 len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1452 if (len == cio_numbytesleft(cio) + 1) {
1453 truncate = 1; /* Case of a truncate codestream */
1456 data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
1458 for (i = 0; i < j3d->tile_len[curtileno]; i++) {
1459 data[i] = j3d->tile_data[curtileno][i];
1462 data_ptr = data + j3d->tile_len[curtileno];
1463 for (i = 0; i < len; i++) {
1464 data_ptr[i] = cio_read(cio, 1);
1467 j3d->tile_len[curtileno] += len;
1468 opj_free(j3d->tile_data[curtileno]);
1469 j3d->tile_data[curtileno] = data;
1472 j3d->state = J3D_STATE_TPHSOT;
1474 j3d->state = J3D_STATE_NEOC; /* RAJOUTE !! */
1478 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
1480 opj_cp_t *cp = j3d->cp;
1481 opj_tcp_t *tcp = &cp->tcps[tileno];
1482 opj_cio_t *cio = j3d->cio;
1483 int numcomps = j3d->volume->numcomps;
1485 cio_write(cio, J3D_MS_RGN, 2); /* RGN */
1486 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1487 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1488 cio_write(cio, 0, 1); /* Srgn */
1489 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1492 static void j3d_read_rgn(opj_j3d_t *j3d) {
1493 int len, compno, roisty;
1495 opj_cp_t *cp = j3d->cp;
1496 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1497 opj_cio_t *cio = j3d->cio;
1498 int numcomps = j3d->volume->numcomps;
1500 len = cio_read(cio, 2); /* Lrgn */
1501 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1502 roisty = cio_read(cio, 1); /* Srgn */
1503 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1506 static void j3d_write_eoc(opj_j3d_t *j3d) {
1507 opj_cio_t *cio = j3d->cio;
1508 /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
1509 cio_write(cio, J3D_MS_EOC, 2);
1512 static void j3d_read_eoc(opj_j3d_t *j3d) {
1515 #ifndef NO_PACKETS_DECODING
1516 opj_tcd_t *tcd = tcd_create(j3d->cinfo);
1517 tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
1518 /*j3d_dump_volume(stdout, tcd->volume);
1519 j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
1520 for (i = 0; i < j3d->cp->tileno_size; i++) {
1521 tileno = j3d->cp->tileno[i];
1522 /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/
1523 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
1524 opj_free(j3d->tile_data[tileno]);
1525 j3d->tile_data[tileno] = NULL;
1527 tcd_free_decode(tcd);
1530 for (i = 0; i < j3d->cp->tileno_size; i++) {
1531 tileno = j3d->cp->tileno[i];
1532 opj_free(j3d->tile_data[tileno]);
1533 j3d->tile_data[tileno] = NULL;
1537 j3d->state = J3D_STATE_MT;
1540 static void j3d_read_unk(opj_j3d_t *j3d) {
1541 opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
1544 static opj_atk_t atk_info_wt[] = {
1545 {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/
1546 {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/
1547 {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/
1548 {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/
1549 {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/
1550 {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/
1551 {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},
1552 {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/
1553 {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}}, /* WT 5-3 IRR*/
1554 {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}} /* WT 13-7 REV*/
1557 typedef struct opj_dec_mstabent {
1560 /** value of the state when the marker can appear */
1562 /** action linked to the marker */
1563 void (*handler) (opj_j3d_t *j3d);
1564 } opj_dec_mstabent_t;
1566 opj_dec_mstabent_t j3d_dec_mstab[] = {
1567 {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
1568 {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
1569 {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
1570 {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
1571 {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
1572 {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
1573 {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},
1574 {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
1575 {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
1576 {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
1577 {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
1578 {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
1579 {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
1580 {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
1581 {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
1582 {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
1583 {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
1584 {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
1586 {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
1587 {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
1588 {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
1589 {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
1590 {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
1591 /*, -->must define the j3d_read functions
1592 {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
1593 {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
1594 {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
1595 {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
1596 {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
1597 {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
1598 {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
1599 {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
1600 {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
1601 {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
1605 Read the lookup table containing all the marker, status and action
1606 @param id Marker value
1608 static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
1609 opj_dec_mstabent_t *e;
1610 for (e = j3d_dec_mstab; e->id != 0; e++) {
1618 /* ----------------------------------------------------------------------- */
1619 /* J3D / JPT decoder interface */
1620 /* ----------------------------------------------------------------------- */
1622 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
1623 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1626 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1627 if(!j3d->default_tcp) {
1635 void j3d_destroy_decompress(opj_j3d_t *j3d) {
1638 if(j3d->tile_len != NULL) {
1639 opj_free(j3d->tile_len);
1641 if(j3d->tile_data != NULL) {
1642 opj_free(j3d->tile_data);
1644 if(j3d->default_tcp != NULL) {
1645 opj_tcp_t *default_tcp = j3d->default_tcp;
1646 if(default_tcp->ppt_data_first != NULL) {
1647 opj_free(default_tcp->ppt_data_first);
1649 if(j3d->default_tcp->tccps != NULL) {
1650 opj_free(j3d->default_tcp->tccps);
1652 opj_free(j3d->default_tcp);
1654 if(j3d->cp != NULL) {
1655 opj_cp_t *cp = j3d->cp;
1656 if(cp->tcps != NULL) {
1657 for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
1658 if(cp->tcps[i].ppt_data_first != NULL) {
1659 opj_free(cp->tcps[i].ppt_data_first);
1661 if(cp->tcps[i].tccps != NULL) {
1662 opj_free(cp->tcps[i].tccps);
1667 if(cp->ppm_data_first != NULL) {
1668 opj_free(cp->ppm_data_first);
1670 if(cp->tileno != NULL) {
1671 opj_free(cp->tileno);
1673 if(cp->comment != NULL) {
1674 opj_free(cp->comment);
1683 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
1684 if(j3d && parameters) {
1685 /* create and initialize the coding parameters structure */
1686 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1687 cp->reduce[0] = parameters->cp_reduce[0];
1688 cp->reduce[1] = parameters->cp_reduce[1];
1689 cp->reduce[2] = parameters->cp_reduce[2];
1690 cp->layer = parameters->cp_layer;
1691 cp->bigendian = parameters->bigendian;
1693 /* MM: Settings of the following two member variables would take
1694 place during j3d_read_com. FIXME */
1695 cp->encoding_format = ENCOD_3EB;
1696 cp->transform_format = TRF_2D_DWT;
1698 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
1703 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
1704 opj_volume_t *volume = NULL;
1706 opj_common_ptr cinfo = j3d->cinfo;
1710 /* create an empty volume */
1711 volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
1712 j3d->volume = volume;
1714 j3d->state = J3D_STATE_MHSOC;
1717 opj_dec_mstabent_t *e;
1718 int id = cio_read(cio, 2);
1719 if (id >> 8 != 0xff) {
1720 opj_volume_destroy(volume);
1721 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1724 e = j3d_dec_mstab_lookup(id);
1725 /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/
1726 if (!(j3d->state & e->states)) {
1727 opj_volume_destroy(volume);
1728 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1734 /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/
1735 if (j3d->state == J3D_STATE_MT) {
1738 if (j3d->state == J3D_STATE_NEOC) {
1742 if (j3d->state == J3D_STATE_NEOC) {
1746 if (j3d->state != J3D_STATE_MT) {
1747 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1753 /* ----------------------------------------------------------------------- */
1754 /* J3D encoder interface */
1755 /* ----------------------------------------------------------------------- */
1757 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
1758 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1765 void j3d_destroy_compress(opj_j3d_t *j3d) {
1770 if(j3d->volume_info != NULL) {
1771 opj_volume_info_t *volume_info = j3d->volume_info;
1772 if (volume_info->index_on && j3d->cp) {
1773 opj_cp_t *cp = j3d->cp;
1774 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1775 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
1776 opj_free(tile_info->thresh);
1777 opj_free(tile_info->packet);
1779 opj_free(volume_info->tile);
1781 opj_free(volume_info);
1783 if(j3d->cp != NULL) {
1784 opj_cp_t *cp = j3d->cp;
1787 opj_free(cp->comment);
1790 opj_free(cp->matrice);
1792 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1793 opj_free(cp->tcps[tileno].tccps);
1802 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
1803 int i, j, tileno, numpocs_tile;
1804 opj_cp_t *cp = NULL;
1806 if(!j3d || !parameters || ! volume) {
1810 /* create and initialize the coding parameters structure */
1811 cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1813 /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
1816 /* set default values for cp */
1821 /* copy user encoding parameters */
1822 cp->disto_alloc = parameters->cp_disto_alloc;
1823 cp->fixed_alloc = parameters->cp_fixed_alloc;
1824 cp->fixed_quality = parameters->cp_fixed_quality;
1826 /* transform and coding method */
1827 cp->transform_format = parameters->transform_format;
1828 cp->encoding_format = parameters->encoding_format;
1830 /* mod fixed_quality */
1831 if(parameters->cp_matrice) {
1832 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
1833 cp->matrice = (int *) opj_malloc(array_size);
1834 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1837 /* creation of an index file ? */
1838 cp->index_on = parameters->index_on;
1840 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
1844 cp->tdx = parameters->cp_tdx;
1845 cp->tdy = parameters->cp_tdy;
1846 cp->tdz = parameters->cp_tdz;
1848 cp->tx0 = parameters->cp_tx0;
1849 cp->ty0 = parameters->cp_ty0;
1850 cp->tz0 = parameters->cp_tz0;
1851 /* comment string */
1852 if(parameters->cp_comment) {
1853 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1855 strcpy(cp->comment, parameters->cp_comment);
1859 /*calculate other encoding parameters*/
1860 if (parameters->tile_size_on) {
1861 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
1862 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
1863 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
1865 cp->tdx = volume->x1 - cp->tx0;
1866 cp->tdy = volume->y1 - cp->ty0;
1867 cp->tdz = volume->z1 - cp->tz0;
1870 /* initialize the multiple tiles */
1871 /* ---------------------------- */
1872 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
1874 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
1875 opj_tcp_t *tcp = &cp->tcps[tileno];
1876 tcp->numlayers = parameters->tcp_numlayers;
1877 for (j = 0; j < tcp->numlayers; j++) {
1878 if (cp->fixed_quality) { /* add fixed_quality */
1879 tcp->distoratio[j] = parameters->tcp_distoratio[j];
1881 tcp->rates[j] = parameters->tcp_rates[j];
1884 tcp->csty = parameters->csty;
1885 tcp->prg = parameters->prog_order;
1886 tcp->mct = volume->numcomps == 3 ? 1 : 0;
1890 if (parameters->numpocs) {
1891 /* initialisation of POC */
1893 for (i = 0; i < parameters->numpocs; i++) {
1894 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1895 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1896 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
1897 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
1898 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
1899 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
1900 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
1901 tcp_poc->prg = parameters->POC[numpocs_tile].prg;
1902 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
1907 tcp->numpocs = numpocs_tile;
1909 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
1911 for (i = 0; i < volume->numcomps; i++) {
1912 opj_tccp_t *tccp = &tcp->tccps[i];
1913 tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT; /* 0 => standard precint || 1 => custom-defined precinct */
1914 tccp->numresolution[0] = parameters->numresolution[0];
1915 tccp->numresolution[1] = parameters->numresolution[1];
1916 tccp->numresolution[2] = parameters->numresolution[2];
1917 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
1918 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
1919 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
1920 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
1921 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
1922 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
1923 tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]);
1924 tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]);
1925 tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]);
1926 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
1927 tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/
1929 /*ATK / transform */
1930 tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
1931 for (j = 0; j < 3; j++) {
1932 tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
1935 /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
1936 tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
1938 if (i == parameters->roi_compno) {
1939 tccp->roishift = parameters->roi_shift;
1943 /* Custom defined precints */
1944 if (parameters->csty & J3D_CCP_CSTY_PRT) {
1946 for (k = 0; k < 3; k++) {
1948 for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
1949 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
1950 if (parameters->prct_init[k][p] < 1) {
1951 tccp->prctsiz[k][j] = 1;
1953 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
1956 int res_spec = parameters->res_spec;
1957 int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
1958 if (size_prct < 1) {
1959 tccp->prctsiz[k][j] = 1;
1961 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
1969 for (k = 0; k < 3; k++) {
1970 for (j = 0; j < tccp->numresolution[k]; j++) {
1971 tccp->prctsiz[k][j] = 15;
1975 /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/
1976 dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
1982 Create an index file
1986 @param index Index filename
1987 @return Returns 1 if successful, returns 0 otherwise
1989 static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
1991 int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
1992 FILE *stream = NULL;
1993 double total_disto = 0;
1995 volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
1997 stream = fopen(index, "w");
1999 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
2003 fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
2004 fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
2005 fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
2006 fprintf(stream, "PROG\t%d\n", volume_info->prog);
2007 fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
2008 fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
2009 fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
2010 fprintf(stream, "LAYER\t%d\n", volume_info->layer);
2011 fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
2013 fprintf(stream, "Precint sizes for each resolution:\n");
2014 for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
2015 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
2016 (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno])); /* based on tile 0 */
2018 fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
2019 fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
2020 fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
2021 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2022 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
2023 volume_info->tile[tileno].num_tile,
2024 volume_info->tile[tileno].start_pos,
2025 volume_info->tile[tileno].end_header,
2026 volume_info->tile[tileno].end_pos,
2027 volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
2028 volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
2031 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2032 int start_pos, end_pos;
2035 if (volume_info->prog == LRCP) { /* LRCP */
2036 fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n");
2037 for (layno = 0; layno < volume_info->layer; layno++) {
2038 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2039 for (compno = 0; compno < volume_info->comp; compno++) {
2040 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2041 for (precno = 0; precno < prec_max; precno++) {
2042 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2043 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2044 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2045 fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2046 total_disto += disto;
2053 else if (volume_info->prog == RLCP) { /* RLCP */
2055 fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto");
2057 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2058 for (layno = 0; layno < volume_info->layer; layno++) {
2059 for (compno = 0; compno < volume_info->comp; compno++) {
2060 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
2061 for (precno = 0; precno < prec_max; precno++) {
2062 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2063 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2064 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2065 fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2066 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2067 total_disto += disto;
2074 else if (volume_info->prog == RPCL) { /* RPCL */
2076 fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
2078 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2079 /* I suppose components have same XRsiz, YRsiz */
2080 /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/
2081 /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/
2082 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
2083 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
2084 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
2085 int x1 = x0 + volume_info->tile_x;
2086 int y1 = y0 + volume_info->tile_y;
2087 int z1 = z0 + volume_info->tile_z;
2088 for(z = z0; z < z1; z++) {
2089 for(y = y0; y < y1; y++) {
2090 for(x = x0; x < x1; x++) {
2091 for (compno = 0; compno < volume_info->comp; compno++) {
2092 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2093 for (precno = 0; precno < prec_max; precno++) {
2094 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2095 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2096 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2097 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2098 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2099 int precno_y = (int) floor( (float)precno/(float)pcnx );
2100 if (precno_y*pcy == y ) {
2101 if (precno_x*pcx == x ) {
2102 for (layno = 0; layno < volume_info->layer; layno++) {
2103 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2104 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2105 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2106 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2107 pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
2108 total_disto += disto;
2120 else if (volume_info->prog == PCRL) { /* PCRL */
2121 /* I suppose components have same XRsiz, YRsiz */
2122 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2123 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2124 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2125 int x1 = x0 + volume_info->tile_x;
2126 int y1 = y0 + volume_info->tile_y;
2127 int z1 = z0 + volume_info->tile_z;
2129 fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
2131 for(z = z0; z < z1; z++) {
2132 for(y = y0; y < y1; y++) {
2133 for(x = x0; x < x1; x++) {
2134 for (compno = 0; compno < volume_info->comp; compno++) {
2135 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2136 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
2137 for (precno = 0; precno < prec_max; precno++) {
2138 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2139 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2140 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2141 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2142 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2143 int precno_y = (int) floor( (float)precno/(float)pcnx );
2144 int precno_z = (int) floor( (float)precno/(float)pcnx );
2145 if (precno_z*pcz == z ) {
2146 if (precno_y*pcy == y ) {
2147 if (precno_x*pcx == x ) {
2148 for (layno = 0; layno < volume_info->layer; layno++) {
2149 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2150 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2151 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2152 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2153 pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
2154 total_disto += disto;
2169 fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
2171 for (compno = 0; compno < volume_info->comp; compno++) {
2172 /* I suppose components have same XRsiz, YRsiz */
2173 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2174 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2175 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2176 int x1 = x0 + volume_info->tile_x;
2177 int y1 = y0 + volume_info->tile_y;
2178 int z1 = z0 + volume_info->tile_z;
2179 for(z = z0; z < z1; z++) {
2180 for(y = y0; y < y1; y++) {
2181 for(x = x0; x < x1; x++) {
2182 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2183 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2184 for (precno = 0; precno < prec_max; precno++) {
2185 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2186 int pcny = volume_info->tile[tileno].prctno[1][resno];
2187 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2188 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2189 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2190 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2191 int precno_y = (int) floor( (float)precno/(float)pcnx );
2192 int precno_z = 0; /*???*/
2193 if (precno_z*pcz == z ) {
2194 if (precno_y*pcy == y ) {
2195 if (precno_x*pcx == x ) {
2196 for (layno = 0; layno < volume_info->layer; layno++) {
2197 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2198 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2199 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2200 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2201 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
2202 total_disto += disto;
2217 fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max); /* SE max */
2218 fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto); /* SE totale */
2226 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
2228 opj_volume_info_t *volume_info = NULL;
2229 opj_cp_t *cp = NULL;
2230 opj_tcd_t *tcd = NULL; /* TCD component */
2233 j3d->volume = volume;
2236 /*j3d_dump_volume(stdout, volume);
2237 j3d_dump_cp(stdout, volume, cp);*/
2240 volume_info = j3d->volume_info;
2241 if (volume_info && cp->index_on) {
2242 volume_info->index_on = cp->index_on;
2243 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
2244 volume_info->volume_w = volume->x1 - volume->x0;
2245 volume_info->volume_h = volume->y1 - volume->y0;
2246 volume_info->volume_l = volume->z1 - volume->z0;
2247 volume_info->prog = (&cp->tcps[0])->prg;
2248 volume_info->tw = cp->tw;
2249 volume_info->th = cp->th;
2250 volume_info->tl = cp->tl;
2251 volume_info->tile_x = cp->tdx; /* new version parser */
2252 volume_info->tile_y = cp->tdy; /* new version parser */
2253 volume_info->tile_z = cp->tdz; /* new version parser */
2254 volume_info->tile_Ox = cp->tx0; /* new version parser */
2255 volume_info->tile_Oy = cp->ty0; /* new version parser */
2256 volume_info->tile_Oz = cp->tz0; /* new version parser */
2257 volume_info->transform_format = cp->transform_format;
2258 volume_info->encoding_format = cp->encoding_format;
2259 volume_info->comp = volume->numcomps;
2260 volume_info->layer = (&cp->tcps[0])->numlayers;
2261 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
2262 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
2263 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
2264 volume_info->D_max = 0; /* ADD Marcela */
2270 if (j3d->cinfo->codec_format == CODEC_J3D) {
2275 /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/
2280 for (compno = 0; compno < volume->numcomps; compno++) {
2281 opj_tcp_t *tcp = &cp->tcps[0];
2282 if (tcp->tccps[compno].roishift)
2283 j3d_write_rgn(j3d, compno, 0);
2285 /*Optional 15444-2 markers*/
2286 if (j3d->cp->tcps->tccps[0].atk != NULL)
2288 if (j3d->volume->comps[0].dcoffset != 0)
2292 if(volume_info && volume_info->index_on) {
2293 volume_info->main_head_end = cio_tell(cio) - 1;
2297 /* create the tile encoder */
2298 tcd = tcd_create(j3d->cinfo);
2300 /* encode each tile */
2301 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
2302 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
2304 j3d->curtileno = tileno;
2306 /* initialisation before tile encoding */
2308 tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
2310 tcd_init_encode(tcd, volume, cp, j3d->curtileno);
2314 if(volume_info && volume_info->index_on) {
2315 volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
2316 volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
2322 for (compno = 1; compno < volume->numcomps; compno++) {
2323 j3d_write_coc(j3d, compno);
2324 j3d_write_qcc(j3d, compno);
2327 if (cp->tcps[tileno].numpocs) {
2330 j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/
2333 if(volume_info && volume_info->index_on) {
2334 volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
2339 /* destroy the tile encoder */
2340 tcd_free_encode(tcd);
2345 /* Creation of the index file */
2346 if(volume_info && volume_info->index_on) {
2347 if(!j3d_create_index(j3d, cio, volume_info, index)) {
2348 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2356 /* ----------------------------------------------------------------------- */