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 numbands = 0; // compiler warning
1012 if (j3d->cinfo->codec_format == CODEC_J2K)
1013 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2;
1014 else if (j3d->cinfo->codec_format == CODEC_J3D) {
1015 int diff = tccp->numresolution[0] - tccp->numresolution[2];
1016 numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
1019 for (bandno = 0; bandno < numbands; bandno++) {
1020 expn = tccp->stepsizes[bandno].expn;
1021 mant = tccp->stepsizes[bandno].mant;
1023 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1024 cio_write(cio, expn << 3, 1); /* SPqcx_i */
1026 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1031 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
1033 int bandno, numbands;
1035 opj_cp_t *cp = j3d->cp;
1036 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1037 opj_tccp_t *tccp = &tcp->tccps[compno];
1038 opj_cio_t *cio = j3d->cio;
1040 tmp = cio_read(cio, 1); /* Sqcx */
1041 tccp->qntsty = tmp & 0x1f;
1042 tccp->numgbits = tmp >> 5;
1044 /*Numbands = 1 si SIQNT
1046 (len - 1) / 2 si SEQNT */
1047 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1049 for (bandno = 0; bandno < numbands; bandno++) {
1051 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
1052 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
1055 tmp = cio_read(cio, 2); /* SPqcx_i */
1059 tccp->stepsizes[bandno].expn = expn;
1060 tccp->stepsizes[bandno].mant = mant;
1063 /* Add Antonin : if scalar_derived -> compute other stepsizes */
1064 if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
1065 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
1066 int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
1067 tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
1068 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1074 static void j3d_write_qcd(opj_j3d_t *j3d) {
1077 opj_cio_t *cio = j3d->cio;
1079 cio_write(cio, J3D_MS_QCD, 2); /* QCD */
1080 lenp = cio_tell(cio);
1082 j3d_write_qcx(j3d, 0); /* Sqcd*/
1083 len = cio_tell(cio) - lenp;
1084 cio_seek(cio, lenp);
1085 cio_write(cio, len, 2); /* Lqcd */
1086 cio_seek(cio, lenp + len);
1089 static void j3d_read_qcd(opj_j3d_t *j3d) {
1092 opj_cio_t *cio = j3d->cio;
1093 opj_volume_t *volume = j3d->volume;
1095 len = cio_read(cio, 2); /* Lqcd */
1096 pos = cio_tell(cio);
1097 for (i = 0; i < volume->numcomps; i++) {
1099 j3d_read_qcx(j3d, i, len - 2);
1103 static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
1106 opj_cio_t *cio = j3d->cio;
1108 cio_write(cio, J3D_MS_QCC, 2); /* QCC */
1109 lenp = cio_tell(cio);
1111 cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2); /* Cqcc */
1112 j3d_write_qcx(j3d, compno);
1113 len = cio_tell(cio) - lenp;
1114 cio_seek(cio, lenp);
1115 cio_write(cio, len, 2); /* Lqcc */
1116 cio_seek(cio, lenp + len);
1119 static void j3d_read_qcc(opj_j3d_t *j3d) {
1121 int numcomp = j3d->volume->numcomps;
1122 opj_cio_t *cio = j3d->cio;
1124 len = cio_read(cio, 2); /* Lqcc */
1125 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1126 j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1129 static void j3d_write_poc(opj_j3d_t *j3d) {
1130 int len, numpchgs, i;
1132 int numcomps = j3d->volume->numcomps;
1134 opj_cp_t *cp = j3d->cp;
1135 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
1136 opj_tccp_t *tccp = &tcp->tccps[0];
1137 opj_cio_t *cio = j3d->cio;
1139 numpchgs = tcp->numpocs;
1140 cio_write(cio, J3D_MS_POC, 2); /* POC */
1141 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1142 cio_write(cio, len, 2); /* Lpoc */
1143 for (i = 0; i < numpchgs; i++) {
1144 opj_poc_t *poc = &tcp->pocs[i];
1145 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1146 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1147 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1148 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1149 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1150 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
1151 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1152 poc->compno1 = int_min(poc->compno1, numcomps);
1153 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1157 static void j3d_read_poc(opj_j3d_t *j3d) {
1158 int len, numpchgs, i, old_poc;
1160 int numcomps = j3d->volume->numcomps;
1162 opj_cp_t *cp = j3d->cp;
1163 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1164 opj_tccp_t *tccp = &tcp->tccps[0];
1165 opj_cio_t *cio = j3d->cio;
1167 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1169 len = cio_read(cio, 2); /* Lpoc */
1170 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1172 for (i = old_poc; i < numpchgs + old_poc; i++) {
1174 poc = &tcp->pocs[i];
1175 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1176 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1177 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
1178 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
1179 poc->compno1 = int_min(
1180 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1181 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1184 tcp->numpocs = numpchgs + old_poc - 1;
1187 static void j3d_read_crg(opj_j3d_t *j3d) {
1188 int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
1190 opj_cio_t *cio = j3d->cio;
1191 int numcomps = j3d->volume->numcomps;
1193 len = cio_read(cio, 2); /* Lcrg */
1194 for (i = 0; i < numcomps; i++) {
1195 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1196 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1197 Zcrg_i = cio_read(cio, 2); /* Zcrg_i */
1201 static void j3d_read_tlm(opj_j3d_t *j3d) {
1202 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1203 long int Ttlm_i, Ptlm_i;
1205 opj_cio_t *cio = j3d->cio;
1207 len = cio_read(cio, 2); /* Ltlm */
1208 Ztlm = cio_read(cio, 1); /* Ztlm */
1209 Stlm = cio_read(cio, 1); /* Stlm */
1210 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1211 SP = (Stlm >> 6) & 0x01;
1212 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1213 for (i = 0; i < tile_tlm; i++) {
1214 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1215 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1219 static void j3d_read_plm(opj_j3d_t *j3d) {
1220 int len, i, Zplm, Nplm, add, packet_len = 0;
1222 opj_cio_t *cio = j3d->cio;
1224 len = cio_read(cio, 2); /* Lplm */
1225 Zplm = cio_read(cio, 1); /* Zplm */
1228 Nplm = cio_read(cio, 4); /* Nplm */
1230 for (i = Nplm; i > 0; i--) {
1231 add = cio_read(cio, 1);
1233 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1234 if ((add & 0x80) == 0) {
1244 static void j3d_read_plt(opj_j3d_t *j3d) {
1245 int len, i, Zplt, packet_len = 0, add;
1247 opj_cio_t *cio = j3d->cio;
1249 len = cio_read(cio, 2); /* Lplt */
1250 Zplt = cio_read(cio, 1); /* Zplt */
1251 for (i = len - 3; i > 0; i--) {
1252 add = cio_read(cio, 1);
1253 packet_len = (packet_len << 7) + add; /* Iplt_i */
1254 if ((add & 0x80) == 0) {
1261 static void j3d_read_ppm(opj_j3d_t *j3d) {
1262 int len, Z_ppm, i, j;
1265 opj_cp_t *cp = j3d->cp;
1266 opj_cio_t *cio = j3d->cio;
1268 len = cio_read(cio, 2);
1271 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1274 if (cp->ppm_previous == 0) {
1275 N_ppm = cio_read(cio, 4); /* N_ppm */
1278 N_ppm = cp->ppm_previous;
1281 if (Z_ppm == 0) { /* First PPM marker */
1282 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1283 cp->ppm_data_first = cp->ppm_data;
1284 cp->ppm_len = N_ppm;
1285 } else { /* NON-first PPM marker */
1286 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1287 cp->ppm_data_first = cp->ppm_data;
1288 cp->ppm_len = N_ppm + cp->ppm_store;
1290 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1291 cp->ppm_data[j] = cio_read(cio, 1);
1295 break; /* Case of non-finished packet header in present marker but finished in next one */
1297 cp->ppm_previous = i - 1;
1302 static void j3d_read_ppt(opj_j3d_t *j3d) {
1303 int len, Z_ppt, i, j = 0;
1305 opj_cp_t *cp = j3d->cp;
1306 opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
1307 opj_cio_t *cio = j3d->cio;
1309 len = cio_read(cio, 2);
1310 Z_ppt = cio_read(cio, 1);
1312 if (Z_ppt == 0) { /* First PPT marker */
1313 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1314 tcp->ppt_data_first = tcp->ppt_data;
1316 tcp->ppt_len = len - 3;
1317 } else { /* NON-first PPT marker */
1318 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1319 tcp->ppt_data_first = tcp->ppt_data;
1320 tcp->ppt_len = len - 3 + tcp->ppt_store;
1323 for (i = len - 3; i > 0; i--) {
1324 tcp->ppt_data[j] = cio_read(cio, 1);
1330 static void j3d_write_sot(opj_j3d_t *j3d) {
1333 opj_cio_t *cio = j3d->cio;
1335 j3d->sot_start = cio_tell(cio);
1336 cio_write(cio, J3D_MS_SOT, 2); /* SOT */
1337 lenp = cio_tell(cio);
1338 cio_skip(cio, 2); /* Lsot (further) */
1339 cio_write(cio, j3d->curtileno, 2); /* Isot */
1340 cio_skip(cio, 4); /* Psot (further in j3d_write_sod) */
1341 cio_write(cio, 0, 1); /* TPsot */
1342 cio_write(cio, 1, 1); /* TNsot (no of tile-parts of this tile in this codestream)*/
1343 len = cio_tell(cio) - lenp;
1344 cio_seek(cio, lenp);
1345 cio_write(cio, len, 2); /* Lsot */
1346 cio_seek(cio, lenp + len);
1349 static void j3d_read_sot(opj_j3d_t *j3d) {
1350 int len, tileno, totlen, partno, numparts, i;
1351 opj_tcp_t *tcp = NULL;
1354 opj_cp_t *cp = j3d->cp;
1355 opj_cio_t *cio = j3d->cio;
1357 len = cio_read(cio, 2);
1358 tileno = cio_read(cio, 2);
1360 if (cp->tileno_size == 0) {
1361 cp->tileno[cp->tileno_size] = tileno;
1365 while (i < cp->tileno_size && status == 0) {
1366 status = cp->tileno[i] == tileno ? 1 : 0;
1370 cp->tileno[cp->tileno_size] = tileno;
1375 totlen = cio_read(cio, 4);
1377 totlen = cio_numbytesleft(cio) + 8;
1379 partno = cio_read(cio, 1);
1380 numparts = cio_read(cio, 1);
1382 j3d->curtileno = tileno;
1383 j3d->eot = cio_getbp(cio) - 12 + totlen;
1384 j3d->state = J3D_STATE_TPH;
1385 tcp = &cp->tcps[j3d->curtileno];
1387 if (tcp->first == 1) {
1389 /* Initialization PPT */
1390 opj_tccp_t *tmp = tcp->tccps;
1391 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
1393 tcp->ppt_data = NULL;
1394 tcp->ppt_data_first = NULL;
1397 for (i = 0; i < j3d->volume->numcomps; i++) {
1398 tcp->tccps[i] = j3d->default_tcp->tccps[i];
1400 cp->tcps[j3d->curtileno].first = 0;
1404 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
1407 opj_tcp_t *tcp = NULL;
1408 opj_volume_info_t *volume_info = NULL;
1410 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1411 opj_cp_t *cp = j3d->cp;
1412 opj_cio_t *cio = j3d->cio;
1414 cio_write(cio, J3D_MS_SOD, 2);
1415 if (j3d->curtileno == 0) {
1416 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
1420 volume_info = j3d->volume_info;
1421 if (volume_info && volume_info->index_on) {
1422 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
1426 tcp = &cp->tcps[j3d->curtileno];
1427 for (layno = 0; layno < tcp->numlayers; layno++) {
1428 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
1432 volume_info->num = 0;
1435 l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
1437 /* Writing Psot in SOT marker */
1438 totlen = cio_tell(cio) + l - j3d->sot_start;
1439 cio_seek(cio, j3d->sot_start + 6);
1440 cio_write(cio, totlen, 4);
1441 cio_seek(cio, j3d->sot_start + totlen);
1444 static void j3d_read_sod(opj_j3d_t *j3d) {
1445 int len, truncate = 0, i;
1446 unsigned char *data = NULL, *data_ptr = NULL;
1448 opj_cio_t *cio = j3d->cio;
1449 int curtileno = j3d->curtileno;
1451 len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1453 if (len == cio_numbytesleft(cio) + 1) {
1454 truncate = 1; /* Case of a truncate codestream */
1457 data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
1459 for (i = 0; i < j3d->tile_len[curtileno]; i++) {
1460 data[i] = j3d->tile_data[curtileno][i];
1463 data_ptr = data + j3d->tile_len[curtileno];
1464 for (i = 0; i < len; i++) {
1465 data_ptr[i] = cio_read(cio, 1);
1468 j3d->tile_len[curtileno] += len;
1469 opj_free(j3d->tile_data[curtileno]);
1470 j3d->tile_data[curtileno] = data;
1473 j3d->state = J3D_STATE_TPHSOT;
1475 j3d->state = J3D_STATE_NEOC; /* RAJOUTE !! */
1479 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
1481 opj_cp_t *cp = j3d->cp;
1482 opj_tcp_t *tcp = &cp->tcps[tileno];
1483 opj_cio_t *cio = j3d->cio;
1484 int numcomps = j3d->volume->numcomps;
1486 cio_write(cio, J3D_MS_RGN, 2); /* RGN */
1487 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1488 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1489 cio_write(cio, 0, 1); /* Srgn */
1490 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1493 static void j3d_read_rgn(opj_j3d_t *j3d) {
1494 int len, compno, roisty;
1496 opj_cp_t *cp = j3d->cp;
1497 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
1498 opj_cio_t *cio = j3d->cio;
1499 int numcomps = j3d->volume->numcomps;
1501 len = cio_read(cio, 2); /* Lrgn */
1502 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1503 roisty = cio_read(cio, 1); /* Srgn */
1504 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1507 static void j3d_write_eoc(opj_j3d_t *j3d) {
1508 opj_cio_t *cio = j3d->cio;
1509 /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
1510 cio_write(cio, J3D_MS_EOC, 2);
1513 static void j3d_read_eoc(opj_j3d_t *j3d) {
1516 #ifndef NO_PACKETS_DECODING
1517 opj_tcd_t *tcd = tcd_create(j3d->cinfo);
1518 tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
1519 /*j3d_dump_volume(stdout, tcd->volume);
1520 j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
1521 for (i = 0; i < j3d->cp->tileno_size; i++) {
1522 tileno = j3d->cp->tileno[i];
1523 /*opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");*/
1524 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
1525 opj_free(j3d->tile_data[tileno]);
1526 j3d->tile_data[tileno] = NULL;
1528 tcd_free_decode(tcd);
1531 for (i = 0; i < j3d->cp->tileno_size; i++) {
1532 tileno = j3d->cp->tileno[i];
1533 opj_free(j3d->tile_data[tileno]);
1534 j3d->tile_data[tileno] = NULL;
1538 j3d->state = J3D_STATE_MT;
1541 static void j3d_read_unk(opj_j3d_t *j3d) {
1542 opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
1545 static opj_atk_t atk_info_wt[] = {
1546 {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/
1547 {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/
1548 {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/
1549 {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/
1550 {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/
1551 {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/
1552 {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},
1553 {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/
1554 {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}}, /* WT 5-3 IRR*/
1555 {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}} /* WT 13-7 REV*/
1558 typedef struct opj_dec_mstabent {
1561 /** value of the state when the marker can appear */
1563 /** action linked to the marker */
1564 void (*handler) (opj_j3d_t *j3d);
1565 } opj_dec_mstabent_t;
1567 opj_dec_mstabent_t j3d_dec_mstab[] = {
1568 {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
1569 {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
1570 {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
1571 {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
1572 {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
1573 {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
1574 {J3D_MS_NSI, J3D_STATE_MHSIZ, j3d_read_nsi},
1575 {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
1576 {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
1577 {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
1578 {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
1579 {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
1580 {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
1581 {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
1582 {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
1583 {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
1584 {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
1585 {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
1587 {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
1588 {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
1589 {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
1590 {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
1591 {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
1592 /*, -->must define the j3d_read functions
1593 {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
1594 {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
1595 {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
1596 {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
1597 {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
1598 {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
1599 {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
1600 {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
1601 {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
1602 {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
1606 Read the lookup table containing all the marker, status and action
1607 @param id Marker value
1609 static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
1610 opj_dec_mstabent_t *e;
1611 for (e = j3d_dec_mstab; e->id != 0; e++) {
1619 /* ----------------------------------------------------------------------- */
1620 /* J3D / JPT decoder interface */
1621 /* ----------------------------------------------------------------------- */
1623 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
1624 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1627 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1628 if(!j3d->default_tcp) {
1636 void j3d_destroy_decompress(opj_j3d_t *j3d) {
1639 if(j3d->tile_len != NULL) {
1640 opj_free(j3d->tile_len);
1642 if(j3d->tile_data != NULL) {
1643 opj_free(j3d->tile_data);
1645 if(j3d->default_tcp != NULL) {
1646 opj_tcp_t *default_tcp = j3d->default_tcp;
1647 if(default_tcp->ppt_data_first != NULL) {
1648 opj_free(default_tcp->ppt_data_first);
1650 if(j3d->default_tcp->tccps != NULL) {
1651 opj_free(j3d->default_tcp->tccps);
1653 opj_free(j3d->default_tcp);
1655 if(j3d->cp != NULL) {
1656 opj_cp_t *cp = j3d->cp;
1657 if(cp->tcps != NULL) {
1658 for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
1659 if(cp->tcps[i].ppt_data_first != NULL) {
1660 opj_free(cp->tcps[i].ppt_data_first);
1662 if(cp->tcps[i].tccps != NULL) {
1663 opj_free(cp->tcps[i].tccps);
1668 if(cp->ppm_data_first != NULL) {
1669 opj_free(cp->ppm_data_first);
1671 if(cp->tileno != NULL) {
1672 opj_free(cp->tileno);
1674 if(cp->comment != NULL) {
1675 opj_free(cp->comment);
1684 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
1685 if(j3d && parameters) {
1686 /* create and initialize the coding parameters structure */
1687 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1688 cp->reduce[0] = parameters->cp_reduce[0];
1689 cp->reduce[1] = parameters->cp_reduce[1];
1690 cp->reduce[2] = parameters->cp_reduce[2];
1691 cp->layer = parameters->cp_layer;
1692 cp->bigendian = parameters->bigendian;
1694 /* MM: Settings of the following two member variables would take
1695 place during j3d_read_com. FIXME */
1696 cp->encoding_format = ENCOD_3EB;
1697 cp->transform_format = TRF_2D_DWT;
1699 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
1704 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
1705 opj_volume_t *volume = NULL;
1707 opj_common_ptr cinfo = j3d->cinfo;
1711 /* create an empty volume */
1712 volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
1713 j3d->volume = volume;
1715 j3d->state = J3D_STATE_MHSOC;
1718 opj_dec_mstabent_t *e;
1719 int id = cio_read(cio, 2);
1720 if (id >> 8 != 0xff) {
1721 opj_volume_destroy(volume);
1722 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1725 e = j3d_dec_mstab_lookup(id);
1726 /*opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);*/
1727 if (!(j3d->state & e->states)) {
1728 opj_volume_destroy(volume);
1729 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1735 /*opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);*/
1736 if (j3d->state == J3D_STATE_MT) {
1739 if (j3d->state == J3D_STATE_NEOC) {
1743 if (j3d->state == J3D_STATE_NEOC) {
1747 if (j3d->state != J3D_STATE_MT) {
1748 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1754 /* ----------------------------------------------------------------------- */
1755 /* J3D encoder interface */
1756 /* ----------------------------------------------------------------------- */
1758 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
1759 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
1766 void j3d_destroy_compress(opj_j3d_t *j3d) {
1771 if(j3d->volume_info != NULL) {
1772 opj_volume_info_t *volume_info = j3d->volume_info;
1773 if (volume_info->index_on && j3d->cp) {
1774 opj_cp_t *cp = j3d->cp;
1775 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1776 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
1777 opj_free(tile_info->thresh);
1778 opj_free(tile_info->packet);
1780 opj_free(volume_info->tile);
1782 opj_free(volume_info);
1784 if(j3d->cp != NULL) {
1785 opj_cp_t *cp = j3d->cp;
1788 opj_free(cp->comment);
1791 opj_free(cp->matrice);
1793 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1794 opj_free(cp->tcps[tileno].tccps);
1803 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
1804 int i, j, tileno, numpocs_tile;
1805 opj_cp_t *cp = NULL;
1807 if(!j3d || !parameters || ! volume) {
1811 /* create and initialize the coding parameters structure */
1812 cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1814 /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
1817 /* set default values for cp */
1822 /* copy user encoding parameters */
1823 cp->disto_alloc = parameters->cp_disto_alloc;
1824 cp->fixed_alloc = parameters->cp_fixed_alloc;
1825 cp->fixed_quality = parameters->cp_fixed_quality;
1827 /* transform and coding method */
1828 cp->transform_format = parameters->transform_format;
1829 cp->encoding_format = parameters->encoding_format;
1831 /* mod fixed_quality */
1832 if(parameters->cp_matrice) {
1833 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
1834 cp->matrice = (int *) opj_malloc(array_size);
1835 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1838 /* creation of an index file ? */
1839 cp->index_on = parameters->index_on;
1841 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
1845 cp->tdx = parameters->cp_tdx;
1846 cp->tdy = parameters->cp_tdy;
1847 cp->tdz = parameters->cp_tdz;
1849 cp->tx0 = parameters->cp_tx0;
1850 cp->ty0 = parameters->cp_ty0;
1851 cp->tz0 = parameters->cp_tz0;
1852 /* comment string */
1853 if(parameters->cp_comment) {
1854 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1856 strcpy(cp->comment, parameters->cp_comment);
1860 /*calculate other encoding parameters*/
1861 if (parameters->tile_size_on) {
1862 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
1863 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
1864 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
1866 cp->tdx = volume->x1 - cp->tx0;
1867 cp->tdy = volume->y1 - cp->ty0;
1868 cp->tdz = volume->z1 - cp->tz0;
1871 /* initialize the multiple tiles */
1872 /* ---------------------------- */
1873 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
1875 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
1876 opj_tcp_t *tcp = &cp->tcps[tileno];
1877 tcp->numlayers = parameters->tcp_numlayers;
1878 for (j = 0; j < tcp->numlayers; j++) {
1879 if (cp->fixed_quality) { /* add fixed_quality */
1880 tcp->distoratio[j] = parameters->tcp_distoratio[j];
1882 tcp->rates[j] = parameters->tcp_rates[j];
1885 tcp->csty = parameters->csty;
1886 tcp->prg = parameters->prog_order;
1887 tcp->mct = volume->numcomps == 3 ? 1 : 0;
1891 if (parameters->numpocs) {
1892 /* initialisation of POC */
1894 for (i = 0; i < parameters->numpocs; i++) {
1895 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1896 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1897 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
1898 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
1899 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
1900 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
1901 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
1902 tcp_poc->prg = parameters->POC[numpocs_tile].prg;
1903 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
1908 tcp->numpocs = numpocs_tile;
1910 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
1912 for (i = 0; i < volume->numcomps; i++) {
1913 opj_tccp_t *tccp = &tcp->tccps[i];
1914 tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT; /* 0 => standard precint || 1 => custom-defined precinct */
1915 tccp->numresolution[0] = parameters->numresolution[0];
1916 tccp->numresolution[1] = parameters->numresolution[1];
1917 tccp->numresolution[2] = parameters->numresolution[2];
1918 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
1919 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
1920 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
1921 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
1922 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
1923 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
1924 tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]);
1925 tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]);
1926 tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]);
1927 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
1928 tccp->cblksty = parameters->mode; /*Codeblock style --> Table A.19 (default 0)*/
1930 /*ATK / transform */
1931 tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
1932 for (j = 0; j < 3; j++) {
1933 tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
1936 /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
1937 tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
1939 if (i == parameters->roi_compno) {
1940 tccp->roishift = parameters->roi_shift;
1944 /* Custom defined precints */
1945 if (parameters->csty & J3D_CCP_CSTY_PRT) {
1947 for (k = 0; k < 3; k++) {
1949 for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
1950 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
1951 if (parameters->prct_init[k][p] < 1) {
1952 tccp->prctsiz[k][j] = 1;
1954 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
1957 int res_spec = parameters->res_spec;
1958 int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
1959 if (size_prct < 1) {
1960 tccp->prctsiz[k][j] = 1;
1962 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
1970 for (k = 0; k < 3; k++) {
1971 for (j = 0; j < tccp->numresolution[k]; j++) {
1972 tccp->prctsiz[k][j] = 15;
1976 /*Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)*/
1977 dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
1983 Create an index file
1987 @param index Index filename
1988 @return Returns 1 if successful, returns 0 otherwise
1990 static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
1992 int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
1993 FILE *stream = NULL;
1994 double total_disto = 0;
1996 volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
1998 stream = fopen(index, "w");
2000 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
2004 fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
2005 fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
2006 fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
2007 fprintf(stream, "PROG\t%d\n", volume_info->prog);
2008 fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
2009 fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
2010 fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
2011 fprintf(stream, "LAYER\t%d\n", volume_info->layer);
2012 fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
2014 fprintf(stream, "Precint sizes for each resolution:\n");
2015 for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
2016 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
2017 (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno])); /* based on tile 0 */
2019 fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
2020 fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
2021 fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
2022 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2023 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
2024 volume_info->tile[tileno].num_tile,
2025 volume_info->tile[tileno].start_pos,
2026 volume_info->tile[tileno].end_header,
2027 volume_info->tile[tileno].end_pos,
2028 volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
2029 volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
2032 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
2033 int start_pos, end_pos;
2036 if (volume_info->prog == LRCP) { /* LRCP */
2037 fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n");
2038 for (layno = 0; layno < volume_info->layer; layno++) {
2039 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2040 for (compno = 0; compno < volume_info->comp; compno++) {
2041 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2042 for (precno = 0; precno < prec_max; precno++) {
2043 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2044 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2045 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2046 fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2047 total_disto += disto;
2054 else if (volume_info->prog == RLCP) { /* RLCP */
2056 fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto");
2058 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2059 for (layno = 0; layno < volume_info->layer; layno++) {
2060 for (compno = 0; compno < volume_info->comp; compno++) {
2061 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
2062 for (precno = 0; precno < prec_max; precno++) {
2063 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2064 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2065 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2066 fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2067 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2068 total_disto += disto;
2075 else if (volume_info->prog == RPCL) { /* RPCL */
2077 fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
2079 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2080 /* I suppose components have same XRsiz, YRsiz */
2081 /*int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;*/
2082 /*int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;*/
2083 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
2084 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
2085 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
2086 int x1 = x0 + volume_info->tile_x;
2087 int y1 = y0 + volume_info->tile_y;
2088 int z1 = z0 + volume_info->tile_z;
2089 for(z = z0; z < z1; z++) {
2090 for(y = y0; y < y1; y++) {
2091 for(x = x0; x < x1; x++) {
2092 for (compno = 0; compno < volume_info->comp; compno++) {
2093 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2094 for (precno = 0; precno < prec_max; precno++) {
2095 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2096 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2097 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2098 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2099 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2100 int precno_y = (int) floor( (float)precno/(float)pcnx );
2101 if (precno_y*pcy == y ) {
2102 if (precno_x*pcx == x ) {
2103 for (layno = 0; layno < volume_info->layer; layno++) {
2104 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2105 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2106 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2107 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2108 pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
2109 total_disto += disto;
2121 else if (volume_info->prog == PCRL) { /* PCRL */
2122 /* I suppose components have same XRsiz, YRsiz */
2123 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2124 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2125 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2126 int x1 = x0 + volume_info->tile_x;
2127 int y1 = y0 + volume_info->tile_y;
2128 int z1 = z0 + volume_info->tile_z;
2130 fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
2132 for(z = z0; z < z1; z++) {
2133 for(y = y0; y < y1; y++) {
2134 for(x = x0; x < x1; x++) {
2135 for (compno = 0; compno < volume_info->comp; compno++) {
2136 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2137 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
2138 for (precno = 0; precno < prec_max; precno++) {
2139 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2140 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2141 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2142 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2143 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2144 int precno_y = (int) floor( (float)precno/(float)pcnx );
2145 int precno_z = (int) floor( (float)precno/(float)pcnx );
2146 if (precno_z*pcz == z ) {
2147 if (precno_y*pcy == y ) {
2148 if (precno_x*pcx == x ) {
2149 for (layno = 0; layno < volume_info->layer; layno++) {
2150 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2151 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2152 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2153 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2154 pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
2155 total_disto += disto;
2170 fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
2172 for (compno = 0; compno < volume_info->comp; compno++) {
2173 /* I suppose components have same XRsiz, YRsiz */
2174 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
2175 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
2176 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
2177 int x1 = x0 + volume_info->tile_x;
2178 int y1 = y0 + volume_info->tile_y;
2179 int z1 = z0 + volume_info->tile_z;
2180 for(z = z0; z < z1; z++) {
2181 for(y = y0; y < y1; y++) {
2182 for(x = x0; x < x1; x++) {
2183 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
2184 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
2185 for (precno = 0; precno < prec_max; precno++) {
2186 int pcnx = volume_info->tile[tileno].prctno[0][resno];
2187 int pcny = volume_info->tile[tileno].prctno[1][resno];
2188 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
2189 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
2190 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
2191 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2192 int precno_y = (int) floor( (float)precno/(float)pcnx );
2193 int precno_z = 0; /*???*/
2194 if (precno_z*pcz == z ) {
2195 if (precno_y*pcy == y ) {
2196 if (precno_x*pcx == x ) {
2197 for (layno = 0; layno < volume_info->layer; layno++) {
2198 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
2199 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
2200 disto = volume_info->tile[tileno].packet[pack_nb].disto;
2201 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2202 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
2203 total_disto += disto;
2218 fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max); /* SE max */
2219 fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto); /* SE totale */
2227 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
2229 opj_volume_info_t *volume_info = NULL;
2230 opj_cp_t *cp = NULL;
2231 opj_tcd_t *tcd = NULL; /* TCD component */
2234 j3d->volume = volume;
2237 /*j3d_dump_volume(stdout, volume);
2238 j3d_dump_cp(stdout, volume, cp);*/
2241 volume_info = j3d->volume_info;
2242 if (volume_info && cp->index_on) {
2243 volume_info->index_on = cp->index_on;
2244 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
2245 volume_info->volume_w = volume->x1 - volume->x0;
2246 volume_info->volume_h = volume->y1 - volume->y0;
2247 volume_info->volume_l = volume->z1 - volume->z0;
2248 volume_info->prog = (&cp->tcps[0])->prg;
2249 volume_info->tw = cp->tw;
2250 volume_info->th = cp->th;
2251 volume_info->tl = cp->tl;
2252 volume_info->tile_x = cp->tdx; /* new version parser */
2253 volume_info->tile_y = cp->tdy; /* new version parser */
2254 volume_info->tile_z = cp->tdz; /* new version parser */
2255 volume_info->tile_Ox = cp->tx0; /* new version parser */
2256 volume_info->tile_Oy = cp->ty0; /* new version parser */
2257 volume_info->tile_Oz = cp->tz0; /* new version parser */
2258 volume_info->transform_format = cp->transform_format;
2259 volume_info->encoding_format = cp->encoding_format;
2260 volume_info->comp = volume->numcomps;
2261 volume_info->layer = (&cp->tcps[0])->numlayers;
2262 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
2263 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
2264 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
2265 volume_info->D_max = 0; /* ADD Marcela */
2271 if (j3d->cinfo->codec_format == CODEC_J3D) {
2276 /*if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)*/
2281 for (compno = 0; compno < volume->numcomps; compno++) {
2282 opj_tcp_t *tcp = &cp->tcps[0];
2283 if (tcp->tccps[compno].roishift)
2284 j3d_write_rgn(j3d, compno, 0);
2286 /*Optional 15444-2 markers*/
2287 if (j3d->cp->tcps->tccps[0].atk != NULL)
2289 if (j3d->volume->comps[0].dcoffset != 0)
2293 if(volume_info && volume_info->index_on) {
2294 volume_info->main_head_end = cio_tell(cio) - 1;
2298 /* create the tile encoder */
2299 tcd = tcd_create(j3d->cinfo);
2301 /* encode each tile */
2302 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
2303 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
2305 j3d->curtileno = tileno;
2307 /* initialisation before tile encoding */
2309 tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
2311 tcd_init_encode(tcd, volume, cp, j3d->curtileno);
2315 if(volume_info && volume_info->index_on) {
2316 volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
2317 volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
2323 for (compno = 1; compno < volume->numcomps; compno++) {
2324 j3d_write_coc(j3d, compno);
2325 j3d_write_qcc(j3d, compno);
2328 if (cp->tcps[tileno].numpocs) {
2331 j3d_write_sod(j3d, tcd); /*--> tcd_encode_tile*/
2334 if(volume_info && volume_info->index_on) {
2335 volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
2340 /* destroy the tile encoder */
2341 tcd_free_encode(tcd);
2346 /* Creation of the index file */
2347 if(volume_info && volume_info->index_on) {
2348 if(!j3d_create_index(j3d, cio, volume_info, index)) {
2349 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2357 /* ----------------------------------------------------------------------- */