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