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, Herv� 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 ZSI marker (3rd volume and tile size)
\r
62 @param j3d J3D handle
\r
64 static void j3d_write_zsi(opj_j3d_t *j3d);
\r
66 Read the ZSI marker (3rd volume and tile size)
\r
67 @param j3d J3D handle
\r
69 static void j3d_read_zsi(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 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 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
421 len = cio_tell(cio) - lenp;
\r
422 cio_seek(cio, lenp);
\r
423 cio_write(cio, len, 2); /* Lsiz */
\r
424 cio_seek(cio, lenp + len);
\r
427 static void j3d_read_cap(opj_j3d_t *j3d){
\r
429 opj_cio_t *cio = j3d->cio;
\r
430 /*cio_read(cio, 2); CAP */
\r
431 len = cio_read(cio, 2);
\r
432 Cap = cio_read(cio, 4);
\r
434 static void j3d_write_zsi(opj_j3d_t *j3d) {
\r
438 opj_cio_t *cio = j3d->cio;
\r
439 opj_volume_t *volume = j3d->volume;
\r
440 opj_cp_t *cp = j3d->cp;
\r
442 cio_write(cio, J3D_MS_ZSI, 2); /* ZSI */
\r
443 lenp = cio_tell(cio);
\r
445 cio_write(cio, volume->z1, 4); /* Zsiz */
\r
446 cio_write(cio, volume->z0, 4); /* Z0siz */
\r
447 cio_write(cio, cp->tdz, 4); /* ZTsiz */
\r
448 cio_write(cio, cp->tz0, 4); /* ZT0siz */
\r
449 for (i = 0; i < volume->numcomps; i++) {
\r
450 cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */
\r
452 len = cio_tell(cio) - lenp;
\r
453 cio_seek(cio, lenp);
\r
454 cio_write(cio, len, 2); /* Lsiz */
\r
455 cio_seek(cio, lenp + len);
\r
458 static void j3d_read_zsi(opj_j3d_t *j3d) {
\r
461 opj_cio_t *cio = j3d->cio;
\r
462 opj_volume_t *volume = j3d->volume;
\r
463 opj_cp_t *cp = j3d->cp;
\r
465 len = cio_read(cio, 2); /* Lsiz */
\r
466 volume->z1 = cio_read(cio, 4); /* Zsiz */
\r
467 volume->z0 = cio_read(cio, 4); /* Z0siz */
\r
468 cp->tdz = cio_read(cio, 4); /* ZTsiz */
\r
469 cp->tz0 = cio_read(cio, 4); /* ZT0siz */
\r
470 for (i = 0; i < volume->numcomps; i++) {
\r
471 volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */
\r
474 //Initialization of volume
\r
475 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
\r
476 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
\r
477 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
\r
478 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
\r
479 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
\r
480 cp->tileno_size = 0;
\r
482 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
\r
483 cp->tcps[i].POC = 0;
\r
484 cp->tcps[i].numpocs = 0;
\r
485 cp->tcps[i].first = 1;
\r
488 /* Initialization for PPM marker (Packets header)*/
\r
490 cp->ppm_data = NULL;
\r
491 cp->ppm_data_first = NULL;
\r
492 cp->ppm_previous = 0;
\r
495 j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
\r
496 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
\r
497 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
\r
499 j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
\r
500 j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
\r
501 j3d->state = J3D_STATE_MH;
\r
504 static void j3d_write_dco(opj_j3d_t *j3d){
\r
508 opj_cio_t *cio = j3d->cio;
\r
509 opj_volume_t *volume = j3d->volume;
\r
510 opj_cp_t *cp = j3d->cp;
\r
512 dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */
\r
513 cio_write(cio, J3D_MS_DCO, 2); /* DCO */
\r
514 lenp = cio_tell(cio);
\r
516 cio_write(cio, dcotype, 1);
\r
517 if (dcotype == 0) {
\r
518 for (i = 0; i < volume->numcomps; i++)
\r
519 cio_write(cio, volume->comps[i].dcoffset, 1); /* SPdco_i */
\r
520 } else if (dcotype == 1) {
\r
521 for (i = 0; i < volume->numcomps; i++){
\r
522 cio_write(cio, volume->comps[i].dcoffset, 1); /* SPdco_i */
\r
523 opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset);
\r
526 len = cio_tell(cio) - lenp;
\r
527 cio_seek(cio, lenp);
\r
528 cio_write(cio, len, 2); /* Ldco */
\r
529 cio_seek(cio, lenp + len);
\r
532 static void j3d_read_dco(opj_j3d_t *j3d){
\r
536 opj_cio_t *cio = j3d->cio;
\r
537 opj_volume_t *volume = j3d->volume;
\r
538 opj_cp_t *cp = j3d->cp;
\r
540 len = cio_read(cio, 2); /* Lsiz */
\r
541 dcotype = cio_read(cio, 1); //offset 8bit unsigned / 16bit signed integers
\r
542 if (dcotype == 0) {
\r
543 for (i = 0; i < volume->numcomps; i++) {
\r
544 volume->comps[i].dcoffset = cio_read(cio, 1);
\r
545 if (volume->comps[i].dcoffset > 128)
\r
546 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
\r
548 } else if (dcotype == 1) {
\r
549 for (i = 0; i < volume->numcomps; i++) {
\r
550 volume->comps[i].dcoffset = cio_read(cio, 1);
\r
551 if (volume->comps[i].dcoffset > 128)
\r
552 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
\r
557 static void j3d_write_atk(opj_j3d_t *j3d){
\r
558 int lenp, len, s, k;
\r
560 opj_cio_t *cio = j3d->cio;
\r
561 opj_volume_t *volume = j3d->volume;
\r
562 opj_atk_t *atk = j3d->cp->tcps->tccps->atk;
\r
564 cio_write(cio, J3D_MS_ATK, 2); /* ATK */
\r
565 lenp = cio_tell(cio);
\r
567 cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11)
\r
568 + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2); /* Satk */
\r
569 if (atk->wt_typ == J3D_ATK_IRR)
\r
570 cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ);
\r
571 cio_write(cio, atk->Natk, 1);
\r
572 for (s = 0; s < atk->Natk; s++){
\r
573 if (atk->filt_cat == J3D_ATK_ARB)
\r
574 cio_write(cio, atk->Oatk[s], 1);
\r
575 if (atk->wt_typ == J3D_ATK_REV){
\r
576 cio_write(cio, atk->Eatk[s], 1);
\r
577 cio_write(cio, atk->Batk[s], 1);
\r
579 cio_write(cio, atk->LCatk[s], 1);
\r
580 for (k = 0; k < atk->LCatk[s]; k++)
\r
581 cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ);
\r
583 len = cio_tell(cio) - lenp;
\r
584 cio_seek(cio, lenp);
\r
585 cio_write(cio, len, 2); /* Latk */
\r
586 cio_seek(cio, lenp + len);
\r
588 static void j3d_read_atk(opj_j3d_t *j3d){
\r
589 int len, i, Satk, k;
\r
591 opj_cio_t *cio = j3d->cio;
\r
592 opj_volume_t *volume = j3d->volume;
\r
593 opj_cp_t *cp = j3d->cp;
\r
594 opj_atk_t *atk = cp->tcps->tccps->atk;
\r
596 len = cio_read(cio, 2); /* Latk */
\r
597 Satk = cio_read(cio, 2);
\r
598 atk->index = Satk & 0x00ff;
\r
599 atk->coeff_typ = Satk >> 8 & 0x0007;
\r
600 atk->filt_cat = Satk >> 11 & 0x0001;
\r
601 atk->wt_typ = Satk >> 12 & 0x0001;
\r
602 atk->minit = Satk >> 13 & 0x0001;
\r
603 atk->exten = Satk >> 14 & 0x0001;
\r
604 if (atk->wt_typ == J3D_ATK_IRR)
\r
605 atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
\r
606 atk->Natk = cio_read(cio, 1);
\r
607 for (i = 0; i < atk->Natk; i++) {
\r
608 if (atk->filt_cat == J3D_ATK_ARB)
\r
609 atk->Oatk[i] = cio_read(cio, 1);
\r
610 if (atk->wt_typ == J3D_ATK_REV){
\r
611 atk->Eatk[i] = cio_read(cio, 1);
\r
612 atk->Batk[i] = cio_read(cio, 1);
\r
614 atk->LCatk[i] = cio_read(cio, 1);
\r
615 for (k = 0; k < atk->LCatk[i]; k++)
\r
616 atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
\r
619 static void j3d_write_cbd(opj_j3d_t *j3d){
\r
621 static void j3d_read_cbd(opj_j3d_t *j3d){
\r
623 static void j3d_write_mct(opj_j3d_t *j3d){
\r
625 static void j3d_read_mct(opj_j3d_t *j3d){
\r
627 static void j3d_write_mcc(opj_j3d_t *j3d){
\r
629 static void j3d_read_mcc(opj_j3d_t *j3d){
\r
631 static void j3d_write_mco(opj_j3d_t *j3d){
\r
633 static void j3d_read_mco(opj_j3d_t *j3d){
\r
635 static void j3d_write_nlt(opj_j3d_t *j3d){
\r
637 static void j3d_read_nlt(opj_j3d_t *j3d){
\r
639 /* -----------------------------------------------------------------------
\r
640 15444-1 codestream syntax
\r
641 ------------------------------------------------------------------------*/
\r
642 static void j3d_write_soc(opj_j3d_t *j3d) {
\r
643 opj_cio_t *cio = j3d->cio;
\r
644 cio_write(cio, J3D_MS_SOC, 2);
\r
647 static void j3d_read_soc(opj_j3d_t *j3d) {
\r
648 j3d->state = J3D_STATE_MHSIZ;
\r
651 static void j3d_write_siz(opj_j3d_t *j3d) {
\r
656 opj_cio_t *cio = j3d->cio;
\r
657 opj_volume_t *volume = j3d->volume;
\r
658 opj_cp_t *cp = j3d->cp;
\r
660 cio_write(cio, J3D_MS_SIZ, 2); /* SIZ */
\r
661 lenp = cio_tell(cio);
\r
663 //cio_write(cio, 0, 2); /* Rsiz (capabilities of 15444-1 only) */
\r
664 Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/
\r
665 cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/
\r
666 cio_write(cio, volume->x1, 4); /* Xsiz */
\r
667 cio_write(cio, volume->y1, 4); /* Ysiz */
\r
668 cio_write(cio, volume->x0, 4); /* X0siz */
\r
669 cio_write(cio, volume->y0, 4); /* Y0siz */
\r
670 cio_write(cio, cp->tdx, 4); /* XTsiz */
\r
671 cio_write(cio, cp->tdy, 4); /* YTsiz */
\r
672 cio_write(cio, cp->tx0, 4); /* XT0siz */
\r
673 cio_write(cio, cp->ty0, 4); /* YT0siz */
\r
674 cio_write(cio, volume->numcomps, 2); /* Csiz */
\r
675 for (i = 0; i < volume->numcomps; i++) {
\r
676 cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1); /* Ssiz_i */
\r
677 cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */
\r
678 cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */
\r
680 len = cio_tell(cio) - lenp;
\r
681 cio_seek(cio, lenp);
\r
682 cio_write(cio, len, 2); /* Lsiz */
\r
683 cio_seek(cio, lenp + len);
\r
686 static void j3d_read_siz(opj_j3d_t *j3d) {
\r
689 opj_cio_t *cio = j3d->cio;
\r
690 opj_volume_t *volume = j3d->volume;
\r
691 opj_cp_t *cp = j3d->cp;
\r
693 len = cio_read(cio, 2); /* Lsiz */
\r
694 cp->rsiz = cio_read(cio, 2); /* Rsiz (capabilities) */
\r
695 volume->x1 = cio_read(cio, 4); /* Xsiz */
\r
696 volume->y1 = cio_read(cio, 4); /* Ysiz */
\r
697 volume->x0 = cio_read(cio, 4); /* X0siz */
\r
698 volume->y0 = cio_read(cio, 4); /* Y0siz */
\r
699 cp->tdx = cio_read(cio, 4); /* XTsiz */
\r
700 cp->tdy = cio_read(cio, 4); /* YTsiz */
\r
701 cp->tx0 = cio_read(cio, 4); /* XT0siz */
\r
702 cp->ty0 = cio_read(cio, 4); /* YT0siz */
\r
704 volume->numcomps = cio_read(cio, 2); /* Csiz */
\r
705 volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
\r
706 for (i = 0; i < volume->numcomps; i++) {
\r
708 tmp = cio_read(cio, 1); /* Ssiz_i */
\r
709 volume->comps[i].prec = (tmp & 0x7f) + 1;
\r
710 volume->comps[i].sgnd = tmp >> 7;
\r
711 volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
\r
712 volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
\r
713 for (j = 0; j < 3; j++) {
\r
714 volume->comps[i].resno_decoded[j] = 0; /* number of resolution decoded */
\r
715 volume->comps[i].factor[j] = 0; /* reducing factor per component */
\r
719 if (j3d->cinfo->codec_format == CODEC_J2K){
\r
722 volume->numslices = 1;
\r
725 for (i = 0; i < volume->numcomps; i++)
\r
726 volume->comps[i].dz = 1;
\r
728 //Initialization of volume
\r
729 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
\r
730 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
\r
731 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
\r
732 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
\r
733 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
\r
734 cp->tileno_size = 0;
\r
736 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
\r
737 cp->tcps[i].POC = 0;
\r
738 cp->tcps[i].numpocs = 0;
\r
739 cp->tcps[i].first = 1;
\r
742 /* Initialization for PPM marker (Packets header)*/
\r
744 cp->ppm_data = NULL;
\r
745 cp->ppm_data_first = NULL;
\r
746 cp->ppm_previous = 0;
\r
749 j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
\r
750 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
\r
751 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
\r
753 j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
\r
754 j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
\r
755 j3d->state = J3D_STATE_MH;
\r
761 static void j3d_write_com(opj_j3d_t *j3d) {
\r
765 opj_cio_t *cio = j3d->cio;
\r
767 cio_write(cio, J3D_MS_COM, 2);
\r
768 lenp = cio_tell(cio);
\r
770 //cio_write(cio, 0, 2);
\r
771 cio_write(cio, j3d->cp->transform_format,1);
\r
772 cio_write(cio, j3d->cp->encoding_format,1);
\r
773 //opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);
\r
774 if (j3d->cp->comment != NULL) {
\r
775 char *comment = j3d->cp->comment;
\r
776 for (i = 0; i < strlen(comment); i++) {
\r
777 cio_write(cio, comment[i], 1);
\r
780 len = cio_tell(cio) - lenp;
\r
781 cio_seek(cio, lenp);
\r
782 cio_write(cio, len, 2);
\r
783 cio_seek(cio, lenp + len);
\r
786 static void j3d_read_com(opj_j3d_t *j3d) {
\r
788 opj_cio_t *cio = j3d->cio;
\r
790 len = cio_read(cio, 2);
\r
792 j3d->cp->transform_format = (OPJ_TRANSFORM) cio_read(cio, 1);
\r
793 j3d->cp->encoding_format = (OPJ_ENTROPY_CODING) cio_read(cio, 1);
\r
794 //opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);
\r
796 cio_skip(cio, len - 4); //posible comments
\r
799 static void j3d_write_cox(opj_j3d_t *j3d, int compno) {
\r
802 opj_cp_t *cp = j3d->cp;
\r
803 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
\r
804 opj_tccp_t *tccp = &tcp->tccps[compno];
\r
805 opj_cio_t *cio = j3d->cio;
\r
807 cio_write(cio, tccp->numresolution[0] - 1, 1); /* SPcox (D) No of decomposition levels in x-axis*/
\r
808 if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
809 cio_write(cio, tccp->numresolution[1] - 1, 1); /* SPcox (E) No of decomposition levels in y-axis*/
\r
810 cio_write(cio, tccp->numresolution[2] - 1, 1); /* SPcox (F) No of decomposition levels in z-axis*/
\r
812 /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/
\r
813 cio_write(cio, tccp->cblk[0] - 2, 1); /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/
\r
814 cio_write(cio, tccp->cblk[1] - 2, 1); /* SPcox (H) Cblk height*/
\r
815 if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
816 cio_write(cio, tccp->cblk[2] - 2, 1); /* SPcox (I) Cblk depth*/
\r
818 cio_write(cio, tccp->cblksty, 1); /* SPcox (J) Cblk style*/
\r
819 cio_write(cio, tccp->dwtid[0], 1); /* SPcox (K) WT in x-axis 15444-2 Table A10*/
\r
820 if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
821 cio_write(cio, tccp->dwtid[1], 1); /* SPcox (L) WT in y-axis 15444-2 Table A10*/
\r
822 cio_write(cio, tccp->dwtid[2], 1); /* SPcox (M) WT in z-axis 15444-2 Table A10*/
\r
825 if (tccp->csty & J3D_CCP_CSTY_PRT) {
\r
826 for (i = 0; i < tccp->numresolution[0]; i++) {
\r
827 if (i < tccp->numresolution[2])
\r
828 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2); /* SPcox (N_i) Table A9*/
\r
830 if (j3d->cinfo->codec_format == CODEC_J3D)
\r
831 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2); /* SPcox (N_i) Table A9*/
\r
833 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1); /* SPcox (N_i) Table A9*/ }
\r
837 static void j3d_read_cox(opj_j3d_t *j3d, int compno) {
\r
840 opj_cp_t *cp = j3d->cp;
\r
841 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
\r
842 opj_tccp_t *tccp = &tcp->tccps[compno];
\r
843 opj_cio_t *cio = j3d->cio;
\r
845 tccp->numresolution[0] = cio_read(cio, 1) + 1; /* SPcox (D) No of decomposition levels in x-axis*/
\r
846 if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
847 tccp->numresolution[1] = cio_read(cio, 1) + 1; /* SPcox (E) No of decomposition levels in y-axis*/
\r
848 tccp->numresolution[2] = cio_read(cio, 1) + 1; /* SPcox (F) No of decomposition levels in z-axis*/
\r
849 }else if (j3d->cinfo->codec_format == CODEC_J2K) {
\r
850 tccp->numresolution[1] = tccp->numresolution[0];
\r
851 tccp->numresolution[2] = 1;
\r
853 /* check the reduce value */
\r
854 cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]);
\r
855 cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]);
\r
856 cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]);
\r
858 tccp->cblk[0] = cio_read(cio, 1) + 2; /* SPcox (G) */
\r
859 tccp->cblk[1] = cio_read(cio, 1) + 2; /* SPcox (H) */
\r
860 if (j3d->cinfo->codec_format == CODEC_J3D)
\r
861 tccp->cblk[2] = cio_read(cio, 1) + 2; /* SPcox (I) */
\r
863 tccp->cblk[2] = tccp->cblk[0];
\r
865 tccp->cblksty = cio_read(cio, 1); /* SPcox (J) */
\r
866 tccp->dwtid[0] = cio_read(cio, 1); /* SPcox (K) */
\r
867 if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
868 tccp->dwtid[1] = cio_read(cio, 1); /* SPcox (L) */
\r
869 tccp->dwtid[2] = cio_read(cio, 1); /* SPcox (M) */
\r
871 tccp->dwtid[1] = tccp->dwtid[0]; /* SPcox (L) */
\r
872 tccp->dwtid[2] = tccp->dwtid[0]; /* SPcox (M) */
\r
874 tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); //TODO: only valid for irreversible 9x7 WTs
\r
875 if (tccp->csty & J3D_CP_CSTY_PRT) {
\r
876 for (i = 0; i < tccp->numresolution[0]; i++) {
\r
877 int tmp = cio_read(cio, 2); /* SPcox (N_i) */
\r
878 tccp->prctsiz[0][i] = tmp & 0xf;
\r
879 tccp->prctsiz[1][i] = tmp >> 4;
\r
880 tccp->prctsiz[2][i] = tmp >> 8;
\r
885 static void j3d_write_cod(opj_j3d_t *j3d) {
\r
886 opj_cp_t *cp = NULL;
\r
887 opj_tcp_t *tcp = NULL;
\r
890 opj_cio_t *cio = j3d->cio;
\r
892 cio_write(cio, J3D_MS_COD, 2); /* COD */
\r
894 lenp = cio_tell(cio);
\r
898 tcp = &cp->tcps[j3d->curtileno];
\r
900 /* Scod : Table A-4*/
\r
901 cio_write(cio, tcp->csty, 1); /* Scod : Coding style parameters */
\r
902 /* SGcod : Table A-5*/
\r
903 cio_write(cio, tcp->prg, 1); /* SGcod (A) : Progression order */
\r
904 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) : No of layers */
\r
905 cio_write(cio, tcp->mct, 1); /* SGcod (C) : Multiple component transformation usage */
\r
906 /* SPcod : Table A-6*/
\r
907 j3d_write_cox(j3d, 0);
\r
908 len = cio_tell(cio) - lenp;
\r
909 cio_seek(cio, lenp);
\r
910 cio_write(cio, len, 2); /* Lcod */
\r
911 cio_seek(cio, lenp + len);
\r
914 static void j3d_read_cod(opj_j3d_t *j3d) {
\r
917 opj_cio_t *cio = j3d->cio;
\r
918 opj_cp_t *cp = j3d->cp;
\r
919 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
\r
920 opj_volume_t *volume = j3d->volume;
\r
923 len = cio_read(cio, 2);
\r
924 /* Scod : Table A-4*/
\r
925 tcp->csty = cio_read(cio, 1);
\r
926 /* SGcod : Table A-5*/
\r
927 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);
\r
928 tcp->numlayers = cio_read(cio, 2);
\r
929 tcp->mct = cio_read(cio, 1);
\r
931 pos = cio_tell(cio);
\r
932 for (i = 0; i < volume->numcomps; i++) {
\r
933 tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;
\r
934 cio_seek(cio, pos);
\r
935 j3d_read_cox(j3d, i);
\r
939 static void j3d_write_coc(opj_j3d_t *j3d, int compno) {
\r
942 opj_cp_t *cp = j3d->cp;
\r
943 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
\r
944 opj_volume_t *volume = j3d->volume;
\r
945 opj_cio_t *cio = j3d->cio;
\r
947 cio_write(cio, J3D_MS_COC, 2); /* COC */
\r
948 lenp = cio_tell(cio);
\r
950 cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */
\r
951 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
\r
953 j3d_write_cox(j3d, compno);
\r
955 len = cio_tell(cio) - lenp;
\r
956 cio_seek(cio, lenp);
\r
957 cio_write(cio, len, 2); /* Lcoc */
\r
958 cio_seek(cio, lenp + len);
\r
961 static void j3d_read_coc(opj_j3d_t *j3d) {
\r
964 opj_cp_t *cp = j3d->cp;
\r
965 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
\r
966 opj_volume_t *volume = j3d->volume;
\r
967 opj_cio_t *cio = j3d->cio;
\r
969 len = cio_read(cio, 2); /* Lcoc */
\r
970 compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2); /* Ccoc */
\r
971 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
\r
972 j3d_read_cox(j3d, compno);
\r
975 static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {
\r
976 int bandno, numbands;
\r
979 opj_cp_t *cp = j3d->cp;
\r
980 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
\r
981 opj_tccp_t *tccp = &tcp->tccps[compno];
\r
982 opj_cio_t *cio = j3d->cio;
\r
984 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx : Table A28 de 15444-1*/
\r
986 if (j3d->cinfo->codec_format == CODEC_J2K)
\r
987 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2;
\r
988 else if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
989 int diff = tccp->numresolution[0] - tccp->numresolution[2];
\r
990 numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
\r
993 for (bandno = 0; bandno < numbands; bandno++) {
\r
994 expn = tccp->stepsizes[bandno].expn;
\r
995 mant = tccp->stepsizes[bandno].mant;
\r
997 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
\r
998 cio_write(cio, expn << 3, 1); /* SPqcx_i */
\r
1000 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
\r
1005 static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
\r
1007 int bandno, numbands;
\r
1009 opj_cp_t *cp = j3d->cp;
\r
1010 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
\r
1011 opj_tccp_t *tccp = &tcp->tccps[compno];
\r
1012 opj_cio_t *cio = j3d->cio;
\r
1014 tmp = cio_read(cio, 1); /* Sqcx */
\r
1015 tccp->qntsty = tmp & 0x1f;
\r
1016 tccp->numgbits = tmp >> 5;
\r
1018 /*Numbands = 1 si SIQNT
\r
1020 (len - 1) / 2 si SEQNT */
\r
1021 numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
\r
1023 for (bandno = 0; bandno < numbands; bandno++) {
\r
1025 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
\r
1026 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
\r
1029 tmp = cio_read(cio, 2); /* SPqcx_i */
\r
1031 mant = tmp & 0x7ff;
\r
1033 tccp->stepsizes[bandno].expn = expn;
\r
1034 tccp->stepsizes[bandno].mant = mant;
\r
1037 /* Add Antonin : if scalar_derived -> compute other stepsizes */
\r
1038 if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
\r
1039 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
\r
1040 int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
\r
1041 tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
\r
1042 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
\r
1048 static void j3d_write_qcd(opj_j3d_t *j3d) {
\r
1051 opj_cio_t *cio = j3d->cio;
\r
1053 cio_write(cio, J3D_MS_QCD, 2); /* QCD */
\r
1054 lenp = cio_tell(cio);
\r
1056 j3d_write_qcx(j3d, 0); /* Sqcd*/
\r
1057 len = cio_tell(cio) - lenp;
\r
1058 cio_seek(cio, lenp);
\r
1059 cio_write(cio, len, 2); /* Lqcd */
\r
1060 cio_seek(cio, lenp + len);
\r
1063 static void j3d_read_qcd(opj_j3d_t *j3d) {
\r
1066 opj_cio_t *cio = j3d->cio;
\r
1067 opj_volume_t *volume = j3d->volume;
\r
1069 len = cio_read(cio, 2); /* Lqcd */
\r
1070 pos = cio_tell(cio);
\r
1071 for (i = 0; i < volume->numcomps; i++) {
\r
1072 cio_seek(cio, pos);
\r
1073 j3d_read_qcx(j3d, i, len - 2);
\r
1077 static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
\r
1080 opj_cio_t *cio = j3d->cio;
\r
1082 cio_write(cio, J3D_MS_QCC, 2); /* QCC */
\r
1083 lenp = cio_tell(cio);
\r
1085 cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2); /* Cqcc */
\r
1086 j3d_write_qcx(j3d, compno);
\r
1087 len = cio_tell(cio) - lenp;
\r
1088 cio_seek(cio, lenp);
\r
1089 cio_write(cio, len, 2); /* Lqcc */
\r
1090 cio_seek(cio, lenp + len);
\r
1093 static void j3d_read_qcc(opj_j3d_t *j3d) {
\r
1095 int numcomp = j3d->volume->numcomps;
\r
1096 opj_cio_t *cio = j3d->cio;
\r
1098 len = cio_read(cio, 2); /* Lqcc */
\r
1099 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
\r
1100 j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
\r
1103 static void j3d_write_poc(opj_j3d_t *j3d) {
\r
1104 int len, numpchgs, i;
\r
1106 int numcomps = j3d->volume->numcomps;
\r
1108 opj_cp_t *cp = j3d->cp;
\r
1109 opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
\r
1110 opj_tccp_t *tccp = &tcp->tccps[0];
\r
1111 opj_cio_t *cio = j3d->cio;
\r
1113 numpchgs = tcp->numpocs;
\r
1114 cio_write(cio, J3D_MS_POC, 2); /* POC */
\r
1115 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
\r
1116 cio_write(cio, len, 2); /* Lpoc */
\r
1117 for (i = 0; i < numpchgs; i++) {
\r
1118 opj_poc_t *poc = &tcp->pocs[i];
\r
1119 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
\r
1120 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
\r
1121 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
\r
1122 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
\r
1123 cio_write(cio, poc->resno1, 1); /* REpoc_i */
\r
1124 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
\r
1125 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
\r
1126 poc->compno1 = int_min(poc->compno1, numcomps);
\r
1127 cio_write(cio, poc->prg, 1); /* Ppoc_i */
\r
1131 static void j3d_read_poc(opj_j3d_t *j3d) {
\r
1132 int len, numpchgs, i, old_poc;
\r
1134 int numcomps = j3d->volume->numcomps;
\r
1136 opj_cp_t *cp = j3d->cp;
\r
1137 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
\r
1138 opj_tccp_t *tccp = &tcp->tccps[0];
\r
1139 opj_cio_t *cio = j3d->cio;
\r
1141 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
\r
1143 len = cio_read(cio, 2); /* Lpoc */
\r
1144 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
\r
1146 for (i = old_poc; i < numpchgs + old_poc; i++) {
\r
1148 poc = &tcp->pocs[i];
\r
1149 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
\r
1150 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
\r
1151 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
\r
1152 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
\r
1153 poc->compno1 = int_min(
\r
1154 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
\r
1155 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
\r
1158 tcp->numpocs = numpchgs + old_poc - 1;
\r
1161 static void j3d_read_crg(opj_j3d_t *j3d) {
\r
1162 int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
\r
1164 opj_cio_t *cio = j3d->cio;
\r
1165 int numcomps = j3d->volume->numcomps;
\r
1167 len = cio_read(cio, 2); /* Lcrg */
\r
1168 for (i = 0; i < numcomps; i++) {
\r
1169 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
\r
1170 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
\r
1171 Zcrg_i = cio_read(cio, 2); /* Zcrg_i */
\r
1175 static void j3d_read_tlm(opj_j3d_t *j3d) {
\r
1176 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
\r
1177 long int Ttlm_i, Ptlm_i;
\r
1179 opj_cio_t *cio = j3d->cio;
\r
1181 len = cio_read(cio, 2); /* Ltlm */
\r
1182 Ztlm = cio_read(cio, 1); /* Ztlm */
\r
1183 Stlm = cio_read(cio, 1); /* Stlm */
\r
1184 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
\r
1185 SP = (Stlm >> 6) & 0x01;
\r
1186 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
\r
1187 for (i = 0; i < tile_tlm; i++) {
\r
1188 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
\r
1189 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
\r
1193 static void j3d_read_plm(opj_j3d_t *j3d) {
\r
1194 int len, i, Zplm, Nplm, add, packet_len = 0;
\r
1196 opj_cio_t *cio = j3d->cio;
\r
1198 len = cio_read(cio, 2); /* Lplm */
\r
1199 Zplm = cio_read(cio, 1); /* Zplm */
\r
1202 Nplm = cio_read(cio, 4); /* Nplm */
\r
1204 for (i = Nplm; i > 0; i--) {
\r
1205 add = cio_read(cio, 1);
\r
1207 packet_len = (packet_len << 7) + add; /* Iplm_ij */
\r
1208 if ((add & 0x80) == 0) {
\r
1218 static void j3d_read_plt(opj_j3d_t *j3d) {
\r
1219 int len, i, Zplt, packet_len = 0, add;
\r
1221 opj_cio_t *cio = j3d->cio;
\r
1223 len = cio_read(cio, 2); /* Lplt */
\r
1224 Zplt = cio_read(cio, 1); /* Zplt */
\r
1225 for (i = len - 3; i > 0; i--) {
\r
1226 add = cio_read(cio, 1);
\r
1227 packet_len = (packet_len << 7) + add; /* Iplt_i */
\r
1228 if ((add & 0x80) == 0) {
\r
1235 static void j3d_read_ppm(opj_j3d_t *j3d) {
\r
1236 int len, Z_ppm, i, j;
\r
1239 opj_cp_t *cp = j3d->cp;
\r
1240 opj_cio_t *cio = j3d->cio;
\r
1242 len = cio_read(cio, 2);
\r
1245 Z_ppm = cio_read(cio, 1); /* Z_ppm */
\r
1248 if (cp->ppm_previous == 0) {
\r
1249 N_ppm = cio_read(cio, 4); /* N_ppm */
\r
1252 N_ppm = cp->ppm_previous;
\r
1254 j = cp->ppm_store;
\r
1255 if (Z_ppm == 0) { /* First PPM marker */
\r
1256 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
\r
1257 cp->ppm_data_first = cp->ppm_data;
\r
1258 cp->ppm_len = N_ppm;
\r
1259 } else { /* NON-first PPM marker */
\r
1260 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
\r
1261 cp->ppm_data_first = cp->ppm_data;
\r
1262 cp->ppm_len = N_ppm + cp->ppm_store;
\r
1264 for (i = N_ppm; i > 0; i--) { /* Read packet header */
\r
1265 cp->ppm_data[j] = cio_read(cio, 1);
\r
1269 break; /* Case of non-finished packet header in present marker but finished in next one */
\r
1271 cp->ppm_previous = i - 1;
\r
1272 cp->ppm_store = j;
\r
1276 static void j3d_read_ppt(opj_j3d_t *j3d) {
\r
1277 int len, Z_ppt, i, j = 0;
\r
1279 opj_cp_t *cp = j3d->cp;
\r
1280 opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
\r
1281 opj_cio_t *cio = j3d->cio;
\r
1283 len = cio_read(cio, 2);
\r
1284 Z_ppt = cio_read(cio, 1);
\r
1286 if (Z_ppt == 0) { /* First PPT marker */
\r
1287 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
\r
1288 tcp->ppt_data_first = tcp->ppt_data;
\r
1289 tcp->ppt_store = 0;
\r
1290 tcp->ppt_len = len - 3;
\r
1291 } else { /* NON-first PPT marker */
\r
1292 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
\r
1293 tcp->ppt_data_first = tcp->ppt_data;
\r
1294 tcp->ppt_len = len - 3 + tcp->ppt_store;
\r
1296 j = tcp->ppt_store;
\r
1297 for (i = len - 3; i > 0; i--) {
\r
1298 tcp->ppt_data[j] = cio_read(cio, 1);
\r
1301 tcp->ppt_store = j;
\r
1304 static void j3d_write_sot(opj_j3d_t *j3d) {
\r
1307 opj_cio_t *cio = j3d->cio;
\r
1309 j3d->sot_start = cio_tell(cio);
\r
1310 cio_write(cio, J3D_MS_SOT, 2); /* SOT */
\r
1311 lenp = cio_tell(cio);
\r
1312 cio_skip(cio, 2); /* Lsot (further) */
\r
1313 cio_write(cio, j3d->curtileno, 2); /* Isot */
\r
1314 cio_skip(cio, 4); /* Psot (further in j3d_write_sod) */
\r
1315 cio_write(cio, 0, 1); /* TPsot */
\r
1316 cio_write(cio, 1, 1); /* TNsot (no of tile-parts of this tile in this codestream)*/
\r
1317 len = cio_tell(cio) - lenp;
\r
1318 cio_seek(cio, lenp);
\r
1319 cio_write(cio, len, 2); /* Lsot */
\r
1320 cio_seek(cio, lenp + len);
\r
1323 static void j3d_read_sot(opj_j3d_t *j3d) {
\r
1324 int len, tileno, totlen, partno, numparts, i;
\r
1325 opj_tcp_t *tcp = NULL;
\r
1328 opj_cp_t *cp = j3d->cp;
\r
1329 opj_cio_t *cio = j3d->cio;
\r
1331 len = cio_read(cio, 2);
\r
1332 tileno = cio_read(cio, 2);
\r
1334 if (cp->tileno_size == 0) {
\r
1335 cp->tileno[cp->tileno_size] = tileno;
\r
1336 cp->tileno_size++;
\r
1339 while (i < cp->tileno_size && status == 0) {
\r
1340 status = cp->tileno[i] == tileno ? 1 : 0;
\r
1343 if (status == 0) {
\r
1344 cp->tileno[cp->tileno_size] = tileno;
\r
1345 cp->tileno_size++;
\r
1349 totlen = cio_read(cio, 4);
\r
1351 totlen = cio_numbytesleft(cio) + 8;
\r
1353 partno = cio_read(cio, 1);
\r
1354 numparts = cio_read(cio, 1);
\r
1356 j3d->curtileno = tileno;
\r
1357 j3d->eot = cio_getbp(cio) - 12 + totlen;
\r
1358 j3d->state = J3D_STATE_TPH;
\r
1359 tcp = &cp->tcps[j3d->curtileno];
\r
1361 if (tcp->first == 1) {
\r
1363 /* Initialization PPT */
\r
1364 opj_tccp_t *tmp = tcp->tccps;
\r
1365 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
\r
1367 tcp->ppt_data = NULL;
\r
1368 tcp->ppt_data_first = NULL;
\r
1371 for (i = 0; i < j3d->volume->numcomps; i++) {
\r
1372 tcp->tccps[i] = j3d->default_tcp->tccps[i];
\r
1374 cp->tcps[j3d->curtileno].first = 0;
\r
1378 static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
\r
1381 opj_tcp_t *tcp = NULL;
\r
1382 opj_volume_info_t *volume_info = NULL;
\r
1384 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
\r
1385 opj_cp_t *cp = j3d->cp;
\r
1386 opj_cio_t *cio = j3d->cio;
\r
1388 cio_write(cio, J3D_MS_SOD, 2);
\r
1389 if (j3d->curtileno == 0) {
\r
1390 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
\r
1394 volume_info = j3d->volume_info;
\r
1395 if (volume_info && volume_info->index_on) {
\r
1396 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
\r
1400 tcp = &cp->tcps[j3d->curtileno];
\r
1401 for (layno = 0; layno < tcp->numlayers; layno++) {
\r
1402 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
\r
1406 volume_info->num = 0;
\r
1409 l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
\r
1411 /* Writing Psot in SOT marker */
\r
1412 totlen = cio_tell(cio) + l - j3d->sot_start;
\r
1413 cio_seek(cio, j3d->sot_start + 6);
\r
1414 cio_write(cio, totlen, 4);
\r
1415 cio_seek(cio, j3d->sot_start + totlen);
\r
1418 static void j3d_read_sod(opj_j3d_t *j3d) {
\r
1419 int len, truncate = 0, i;
\r
1420 unsigned char *data = NULL, *data_ptr = NULL;
\r
1422 opj_cio_t *cio = j3d->cio;
\r
1423 int curtileno = j3d->curtileno;
\r
1425 len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
\r
1427 if (len == cio_numbytesleft(cio) + 1) {
\r
1428 truncate = 1; /* Case of a truncate codestream */
\r
1431 data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
\r
1433 for (i = 0; i < j3d->tile_len[curtileno]; i++) {
\r
1434 data[i] = j3d->tile_data[curtileno][i];
\r
1437 data_ptr = data + j3d->tile_len[curtileno];
\r
1438 for (i = 0; i < len; i++) {
\r
1439 data_ptr[i] = cio_read(cio, 1);
\r
1442 j3d->tile_len[curtileno] += len;
\r
1443 opj_free(j3d->tile_data[curtileno]);
\r
1444 j3d->tile_data[curtileno] = data;
\r
1447 j3d->state = J3D_STATE_TPHSOT;
\r
1449 j3d->state = J3D_STATE_NEOC; /* RAJOUTE !! */
\r
1453 static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
\r
1455 opj_cp_t *cp = j3d->cp;
\r
1456 opj_tcp_t *tcp = &cp->tcps[tileno];
\r
1457 opj_cio_t *cio = j3d->cio;
\r
1458 int numcomps = j3d->volume->numcomps;
\r
1460 cio_write(cio, J3D_MS_RGN, 2); /* RGN */
\r
1461 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
\r
1462 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
\r
1463 cio_write(cio, 0, 1); /* Srgn */
\r
1464 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
\r
1467 static void j3d_read_rgn(opj_j3d_t *j3d) {
\r
1468 int len, compno, roisty;
\r
1470 opj_cp_t *cp = j3d->cp;
\r
1471 opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
\r
1472 opj_cio_t *cio = j3d->cio;
\r
1473 int numcomps = j3d->volume->numcomps;
\r
1475 len = cio_read(cio, 2); /* Lrgn */
\r
1476 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
\r
1477 roisty = cio_read(cio, 1); /* Srgn */
\r
1478 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
\r
1481 static void j3d_write_eoc(opj_j3d_t *j3d) {
\r
1482 opj_cio_t *cio = j3d->cio;
\r
1483 /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
\r
1484 cio_write(cio, J3D_MS_EOC, 2);
\r
1487 static void j3d_read_eoc(opj_j3d_t *j3d) {
\r
1490 #ifndef NO_PACKETS_DECODING
\r
1491 opj_tcd_t *tcd = tcd_create(j3d->cinfo);
\r
1492 tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
\r
1493 /*j3d_dump_volume(stdout, tcd->volume);
\r
1494 j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
\r
1495 for (i = 0; i < j3d->cp->tileno_size; i++) {
\r
1496 tileno = j3d->cp->tileno[i];
\r
1497 //opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");
\r
1498 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
\r
1499 opj_free(j3d->tile_data[tileno]);
\r
1500 j3d->tile_data[tileno] = NULL;
\r
1502 tcd_free_decode(tcd);
\r
1505 for (i = 0; i < j3d->cp->tileno_size; i++) {
\r
1506 tileno = j3d->cp->tileno[i];
\r
1507 opj_free(j3d->tile_data[tileno]);
\r
1508 j3d->tile_data[tileno] = NULL;
\r
1512 j3d->state = J3D_STATE_MT;
\r
1515 static void j3d_read_unk(opj_j3d_t *j3d) {
\r
1516 opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
\r
1519 static opj_atk_t atk_info_wt[] = {
\r
1520 {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
1521 {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
1522 {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
1523 {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
1524 {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
1525 {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
1526 {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
1527 {-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
1528 {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
1529 {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
1532 typedef struct opj_dec_mstabent {
\r
1533 /** marker value */
\r
1535 /** value of the state when the marker can appear */
\r
1537 /** action linked to the marker */
\r
1538 void (*handler) (opj_j3d_t *j3d);
\r
1539 } opj_dec_mstabent_t;
\r
1541 opj_dec_mstabent_t j3d_dec_mstab[] = {
\r
1542 {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
\r
1543 {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
\r
1544 {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
\r
1545 {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
\r
1546 {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
\r
1547 {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
\r
1548 {J3D_MS_ZSI, J3D_STATE_MHSIZ, j3d_read_zsi},
\r
1549 {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
\r
1550 {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
\r
1551 {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
\r
1552 {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
\r
1553 {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
\r
1554 {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
\r
1555 {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
\r
1556 {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
\r
1557 {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
\r
1558 {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
\r
1559 {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
\r
1560 {J3D_MS_SOP, 0, 0},
\r
1561 {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
\r
1562 {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
\r
1563 {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
\r
1564 {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
\r
1565 {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
\r
1566 /*, -->must define the j3d_read functions
\r
1567 {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
\r
1568 {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
\r
1569 {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
\r
1570 {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
\r
1571 {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
\r
1572 {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
\r
1573 {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
\r
1574 {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
\r
1575 {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
\r
1576 {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
\r
1580 Read the lookup table containing all the marker, status and action
\r
1581 @param id Marker value
\r
1583 static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
\r
1584 opj_dec_mstabent_t *e;
\r
1585 for (e = j3d_dec_mstab; e->id != 0; e++) {
\r
1586 if (e->id == id) {
\r
1593 /* ----------------------------------------------------------------------- */
\r
1594 /* J3D / JPT decoder interface */
\r
1595 /* ----------------------------------------------------------------------- */
\r
1597 opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
\r
1598 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
\r
1600 j3d->cinfo = cinfo;
\r
1601 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
\r
1602 if(!j3d->default_tcp) {
\r
1610 void j3d_destroy_decompress(opj_j3d_t *j3d) {
\r
1613 if(j3d->tile_len != NULL) {
\r
1614 opj_free(j3d->tile_len);
\r
1616 if(j3d->tile_data != NULL) {
\r
1617 opj_free(j3d->tile_data);
\r
1619 if(j3d->default_tcp != NULL) {
\r
1620 opj_tcp_t *default_tcp = j3d->default_tcp;
\r
1621 if(default_tcp->ppt_data_first != NULL) {
\r
1622 opj_free(default_tcp->ppt_data_first);
\r
1624 if(j3d->default_tcp->tccps != NULL) {
\r
1625 opj_free(j3d->default_tcp->tccps);
\r
1627 opj_free(j3d->default_tcp);
\r
1629 if(j3d->cp != NULL) {
\r
1630 opj_cp_t *cp = j3d->cp;
\r
1631 if(cp->tcps != NULL) {
\r
1632 for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
\r
1633 if(cp->tcps[i].ppt_data_first != NULL) {
\r
1634 opj_free(cp->tcps[i].ppt_data_first);
\r
1636 if(cp->tcps[i].tccps != NULL) {
\r
1637 opj_free(cp->tcps[i].tccps);
\r
1640 opj_free(cp->tcps);
\r
1642 if(cp->ppm_data_first != NULL) {
\r
1643 opj_free(cp->ppm_data_first);
\r
1645 if(cp->tileno != NULL) {
\r
1646 opj_free(cp->tileno);
\r
1648 if(cp->comment != NULL) {
\r
1649 opj_free(cp->comment);
\r
1658 void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
\r
1659 if(j3d && parameters) {
\r
1660 /* create and initialize the coding parameters structure */
\r
1661 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
\r
1662 cp->reduce[0] = parameters->cp_reduce[0];
\r
1663 cp->reduce[1] = parameters->cp_reduce[1];
\r
1664 cp->reduce[2] = parameters->cp_reduce[2];
\r
1665 cp->layer = parameters->cp_layer;
\r
1666 cp->bigendian = parameters->bigendian;
\r
1669 cp->encoding_format = ENCOD_2EB;
\r
1670 cp->transform_format = TRF_2D_DWT;
\r
1672 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
\r
1677 opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
\r
1678 opj_volume_t *volume = NULL;
\r
1680 opj_common_ptr cinfo = j3d->cinfo;
\r
1684 /* create an empty volume */
\r
1685 volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
\r
1686 j3d->volume = volume;
\r
1688 j3d->state = J3D_STATE_MHSOC;
\r
1691 opj_dec_mstabent_t *e;
\r
1692 int id = cio_read(cio, 2);
\r
1693 if (id >> 8 != 0xff) {
\r
1694 opj_volume_destroy(volume);
\r
1695 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
\r
1698 e = j3d_dec_mstab_lookup(id);
\r
1699 //opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);
\r
1700 if (!(j3d->state & e->states)) {
\r
1701 opj_volume_destroy(volume);
\r
1702 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
\r
1706 (*e->handler)(j3d);
\r
1708 //opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);
\r
1709 if (j3d->state == J3D_STATE_MT) {
\r
1712 if (j3d->state == J3D_STATE_NEOC) {
\r
1716 if (j3d->state == J3D_STATE_NEOC) {
\r
1717 j3d_read_eoc(j3d);
\r
1720 if (j3d->state != J3D_STATE_MT) {
\r
1721 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
\r
1727 /* ----------------------------------------------------------------------- */
\r
1728 /* J3D encoder interface */
\r
1729 /* ----------------------------------------------------------------------- */
\r
1731 opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
\r
1732 opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
\r
1734 j3d->cinfo = cinfo;
\r
1739 void j3d_destroy_compress(opj_j3d_t *j3d) {
\r
1744 if(j3d->volume_info != NULL) {
\r
1745 opj_volume_info_t *volume_info = j3d->volume_info;
\r
1746 if (volume_info->index_on && j3d->cp) {
\r
1747 opj_cp_t *cp = j3d->cp;
\r
1748 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
\r
1749 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
\r
1750 opj_free(tile_info->thresh);
\r
1751 opj_free(tile_info->packet);
\r
1753 opj_free(volume_info->tile);
\r
1755 opj_free(volume_info);
\r
1757 if(j3d->cp != NULL) {
\r
1758 opj_cp_t *cp = j3d->cp;
\r
1761 opj_free(cp->comment);
\r
1764 opj_free(cp->matrice);
\r
1766 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
\r
1767 opj_free(cp->tcps[tileno].tccps);
\r
1769 opj_free(cp->tcps);
\r
1776 void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
\r
1777 int i, j, tileno, numpocs_tile;
\r
1778 opj_cp_t *cp = NULL;
\r
1780 if(!j3d || !parameters || ! volume) {
\r
1784 /* create and initialize the coding parameters structure */
\r
1785 cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
\r
1787 /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
\r
1790 /* set default values for cp */
\r
1795 /* copy user encoding parameters */
\r
1796 cp->disto_alloc = parameters->cp_disto_alloc;
\r
1797 cp->fixed_alloc = parameters->cp_fixed_alloc;
\r
1798 cp->fixed_quality = parameters->cp_fixed_quality;
\r
1800 /* transform and coding method */
\r
1801 cp->transform_format = parameters->transform_format;
\r
1802 cp->encoding_format = parameters->encoding_format;
\r
1804 /* mod fixed_quality */
\r
1805 if(parameters->cp_matrice) {
\r
1806 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
\r
1807 cp->matrice = (int *) opj_malloc(array_size);
\r
1808 memcpy(cp->matrice, parameters->cp_matrice, array_size);
\r
1811 /* creation of an index file ? */
\r
1812 cp->index_on = parameters->index_on;
\r
1813 if(cp->index_on) {
\r
1814 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
\r
1818 cp->tdx = parameters->cp_tdx;
\r
1819 cp->tdy = parameters->cp_tdy;
\r
1820 cp->tdz = parameters->cp_tdz;
\r
1822 cp->tx0 = parameters->cp_tx0;
\r
1823 cp->ty0 = parameters->cp_ty0;
\r
1824 cp->tz0 = parameters->cp_tz0;
\r
1825 /* comment string */
\r
1826 if(parameters->cp_comment) {
\r
1827 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
\r
1829 strcpy(cp->comment, parameters->cp_comment);
\r
1833 /*calculate other encoding parameters*/
\r
1834 if (parameters->tile_size_on) {
\r
1835 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
\r
1836 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
\r
1837 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
\r
1839 cp->tdx = volume->x1 - cp->tx0;
\r
1840 cp->tdy = volume->y1 - cp->ty0;
\r
1841 cp->tdz = volume->z1 - cp->tz0;
\r
1844 /* initialize the multiple tiles */
\r
1845 /* ---------------------------- */
\r
1846 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
\r
1848 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
\r
1849 opj_tcp_t *tcp = &cp->tcps[tileno];
\r
1850 tcp->numlayers = parameters->tcp_numlayers;
\r
1851 for (j = 0; j < tcp->numlayers; j++) {
\r
1852 if (cp->fixed_quality) { /* add fixed_quality */
\r
1853 tcp->distoratio[j] = parameters->tcp_distoratio[j];
\r
1855 tcp->rates[j] = parameters->tcp_rates[j];
\r
1858 tcp->csty = parameters->csty;
\r
1859 tcp->prg = parameters->prog_order;
\r
1860 tcp->mct = volume->numcomps == 3 ? 1 : 0;
\r
1864 if (parameters->numpocs) {
\r
1865 /* initialisation of POC */
\r
1867 for (i = 0; i < parameters->numpocs; i++) {
\r
1868 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
\r
1869 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
\r
1870 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
\r
1871 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
\r
1872 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
\r
1873 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
\r
1874 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
\r
1875 tcp_poc->prg = parameters->POC[numpocs_tile].prg;
\r
1876 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
\r
1881 tcp->numpocs = numpocs_tile;
\r
1883 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
\r
1885 for (i = 0; i < volume->numcomps; i++) {
\r
1886 opj_tccp_t *tccp = &tcp->tccps[i];
\r
1887 tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT; /* 0 => standard precint || 1 => custom-defined precinct */
\r
1888 tccp->numresolution[0] = parameters->numresolution[0];
\r
1889 tccp->numresolution[1] = parameters->numresolution[1];
\r
1890 tccp->numresolution[2] = parameters->numresolution[2];
\r
1891 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
\r
1892 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
\r
1893 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
\r
1894 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
\r
1895 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
\r
1896 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
\r
1897 tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]);
\r
1898 tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]);
\r
1899 tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]);
\r
1900 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
\r
1901 tccp->cblksty = parameters->mode; //Codeblock style --> Table A.19 (default 0)
\r
1903 /*ATK / transform */
\r
1904 tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
\r
1905 for (j = 0; j < 3; j++) {
\r
1906 tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3 */
\r
1909 /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
\r
1910 tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
\r
1911 tccp->numgbits = 2;
\r
1912 if (i == parameters->roi_compno) {
\r
1913 tccp->roishift = parameters->roi_shift;
\r
1915 tccp->roishift = 0;
\r
1917 /* Custom defined precints */
\r
1918 if (parameters->csty & J3D_CCP_CSTY_PRT) {
\r
1920 for (k = 0; k < 3; k++) {
\r
1922 for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
\r
1923 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
\r
1924 if (parameters->prct_init[k][p] < 1) {
\r
1925 tccp->prctsiz[k][j] = 1;
\r
1927 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
\r
1930 int res_spec = parameters->res_spec;
\r
1931 int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
\r
1932 if (size_prct < 1) {
\r
1933 tccp->prctsiz[k][j] = 1;
\r
1935 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
\r
1943 for (k = 0; k < 3; k++) {
\r
1944 for (j = 0; j < tccp->numresolution[k]; j++) {
\r
1945 tccp->prctsiz[k][j] = 15;
\r
1949 //Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)
\r
1950 dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
\r
1956 Create an index file
\r
1959 @param volume_info
\r
1960 @param index Index filename
\r
1961 @return Returns 1 if successful, returns 0 otherwise
\r
1963 static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
\r
1965 int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
\r
1966 FILE *stream = NULL;
\r
1967 double total_disto = 0;
\r
1969 volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
\r
1971 stream = fopen(index, "w");
\r
1973 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
\r
1977 fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
\r
1978 fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
\r
1979 fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
\r
1980 fprintf(stream, "PROG\t%d\n", volume_info->prog);
\r
1981 fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
\r
1982 fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
\r
1983 fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
\r
1984 fprintf(stream, "LAYER\t%d\n", volume_info->layer);
\r
1985 fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
\r
1987 fprintf(stream, "Precint sizes for each resolution:\n");
\r
1988 for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
\r
1989 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
\r
1990 (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
1992 fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
\r
1993 fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
\r
1994 fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
\r
1995 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
\r
1996 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
\r
1997 volume_info->tile[tileno].num_tile,
\r
1998 volume_info->tile[tileno].start_pos,
\r
1999 volume_info->tile[tileno].end_header,
\r
2000 volume_info->tile[tileno].end_pos,
\r
2001 volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
\r
2002 volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
\r
2005 for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
\r
2006 int start_pos, end_pos;
\r
2009 if (volume_info->prog == LRCP) { /* LRCP */
\r
2010 fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n");
\r
2011 for (layno = 0; layno < volume_info->layer; layno++) {
\r
2012 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
\r
2013 for (compno = 0; compno < volume_info->comp; compno++) {
\r
2014 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
2015 for (precno = 0; precno < prec_max; precno++) {
\r
2016 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
\r
2017 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
\r
2018 disto = volume_info->tile[tileno].packet[pack_nb].disto;
\r
2019 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
2020 total_disto += disto;
\r
2027 else if (volume_info->prog == RLCP) { /* RLCP */
\r
2029 fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto");
\r
2031 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
\r
2032 for (layno = 0; layno < volume_info->layer; layno++) {
\r
2033 for (compno = 0; compno < volume_info->comp; compno++) {
\r
2034 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
2035 for (precno = 0; precno < prec_max; precno++) {
\r
2036 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
\r
2037 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
\r
2038 disto = volume_info->tile[tileno].packet[pack_nb].disto;
\r
2039 fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
\r
2040 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
\r
2041 total_disto += disto;
\r
2048 else if (volume_info->prog == RPCL) { /* RPCL */
\r
2050 fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
\r
2052 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
\r
2053 /* I suppose components have same XRsiz, YRsiz */
\r
2054 //int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
\r
2055 //int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
\r
2056 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
\r
2057 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
\r
2058 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
\r
2059 int x1 = x0 + volume_info->tile_x;
\r
2060 int y1 = y0 + volume_info->tile_y;
\r
2061 int z1 = z0 + volume_info->tile_z;
\r
2062 for(z = z0; z < z1; z++) {
\r
2063 for(y = y0; y < y1; y++) {
\r
2064 for(x = x0; x < x1; x++) {
\r
2065 for (compno = 0; compno < volume_info->comp; compno++) {
\r
2066 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
2067 for (precno = 0; precno < prec_max; precno++) {
\r
2068 int pcnx = volume_info->tile[tileno].prctno[0][resno];
\r
2069 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
\r
2070 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
\r
2071 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
\r
2072 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
\r
2073 int precno_y = (int) floor( (float)precno/(float)pcnx );
\r
2074 if (precno_y*pcy == y ) {
\r
2075 if (precno_x*pcx == x ) {
\r
2076 for (layno = 0; layno < volume_info->layer; layno++) {
\r
2077 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
\r
2078 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
\r
2079 disto = volume_info->tile[tileno].packet[pack_nb].disto;
\r
2080 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
\r
2081 pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
\r
2082 total_disto += disto;
\r
2089 } /* x = x0..x1 */
\r
2090 } /* y = y0..y1 */
\r
2091 } /* z = z0..z1 */
\r
2094 else if (volume_info->prog == PCRL) { /* PCRL */
\r
2095 /* I suppose components have same XRsiz, YRsiz */
\r
2096 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
\r
2097 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
\r
2098 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
\r
2099 int x1 = x0 + volume_info->tile_x;
\r
2100 int y1 = y0 + volume_info->tile_y;
\r
2101 int z1 = z0 + volume_info->tile_z;
\r
2103 fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
\r
2105 for(z = z0; z < z1; z++) {
\r
2106 for(y = y0; y < y1; y++) {
\r
2107 for(x = x0; x < x1; x++) {
\r
2108 for (compno = 0; compno < volume_info->comp; compno++) {
\r
2109 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
\r
2110 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
\r
2111 for (precno = 0; precno < prec_max; precno++) {
\r
2112 int pcnx = volume_info->tile[tileno].prctno[0][resno];
\r
2113 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
\r
2114 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
\r
2115 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
\r
2116 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
\r
2117 int precno_y = (int) floor( (float)precno/(float)pcnx );
\r
2118 int precno_z = (int) floor( (float)precno/(float)pcnx );
\r
2119 if (precno_z*pcz == z ) {
\r
2120 if (precno_y*pcy == y ) {
\r
2121 if (precno_x*pcx == x ) {
\r
2122 for (layno = 0; layno < volume_info->layer; layno++) {
\r
2123 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
\r
2124 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
\r
2125 disto = volume_info->tile[tileno].packet[pack_nb].disto;
\r
2126 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
\r
2127 pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
\r
2128 total_disto += disto;
\r
2137 } /* x = x0..x1 */
\r
2138 } /* y = y0..y1 */
\r
2143 fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
\r
2145 for (compno = 0; compno < volume_info->comp; compno++) {
\r
2146 /* I suppose components have same XRsiz, YRsiz */
\r
2147 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
\r
2148 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
\r
2149 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
\r
2150 int x1 = x0 + volume_info->tile_x;
\r
2151 int y1 = y0 + volume_info->tile_y;
\r
2152 int z1 = z0 + volume_info->tile_z;
\r
2153 for(z = z0; z < z1; z++) {
\r
2154 for(y = y0; y < y1; y++) {
\r
2155 for(x = x0; x < x1; x++) {
\r
2156 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
\r
2157 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
2158 for (precno = 0; precno < prec_max; precno++) {
\r
2159 int pcnx = volume_info->tile[tileno].prctno[0][resno];
\r
2160 int pcny = volume_info->tile[tileno].prctno[1][resno];
\r
2161 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
\r
2162 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
\r
2163 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
\r
2164 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
\r
2165 int precno_y = (int) floor( (float)precno/(float)pcnx );
\r
2166 int precno_z = 0; /*???*/
\r
2167 if (precno_z*pcz == z ) {
\r
2168 if (precno_y*pcy == y ) {
\r
2169 if (precno_x*pcx == x ) {
\r
2170 for (layno = 0; layno < volume_info->layer; layno++) {
\r
2171 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
\r
2172 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
\r
2173 disto = volume_info->tile[tileno].packet[pack_nb].disto;
\r
2174 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
\r
2175 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
\r
2176 total_disto += disto;
\r
2184 } /* x = x0..x1 */
\r
2185 } /* y = y0..y1 */
\r
2186 } /* z = z0..z1 */
\r
2191 fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max); /* SE max */
\r
2192 fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto); /* SE totale */
\r
2200 bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
\r
2201 int tileno, compno;
\r
2202 opj_volume_info_t *volume_info = NULL;
\r
2203 opj_cp_t *cp = NULL;
\r
2204 opj_tcd_t *tcd = NULL; /* TCD component */
\r
2207 j3d->volume = volume;
\r
2210 /*j3d_dump_volume(stdout, volume);
\r
2211 j3d_dump_cp(stdout, volume, cp);*/
\r
2214 volume_info = j3d->volume_info;
\r
2215 if (volume_info && cp->index_on) {
\r
2216 volume_info->index_on = cp->index_on;
\r
2217 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
\r
2218 volume_info->volume_w = volume->x1 - volume->x0;
\r
2219 volume_info->volume_h = volume->y1 - volume->y0;
\r
2220 volume_info->volume_l = volume->z1 - volume->z0;
\r
2221 volume_info->prog = (&cp->tcps[0])->prg;
\r
2222 volume_info->tw = cp->tw;
\r
2223 volume_info->th = cp->th;
\r
2224 volume_info->tl = cp->tl;
\r
2225 volume_info->tile_x = cp->tdx; /* new version parser */
\r
2226 volume_info->tile_y = cp->tdy; /* new version parser */
\r
2227 volume_info->tile_z = cp->tdz; /* new version parser */
\r
2228 volume_info->tile_Ox = cp->tx0; /* new version parser */
\r
2229 volume_info->tile_Oy = cp->ty0; /* new version parser */
\r
2230 volume_info->tile_Oz = cp->tz0; /* new version parser */
\r
2231 volume_info->transform_format = cp->transform_format;
\r
2232 volume_info->encoding_format = cp->encoding_format;
\r
2233 volume_info->comp = volume->numcomps;
\r
2234 volume_info->layer = (&cp->tcps[0])->numlayers;
\r
2235 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
\r
2236 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
\r
2237 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
\r
2238 volume_info->D_max = 0; /* ADD Marcela */
\r
2242 j3d_write_soc(j3d);
\r
2243 j3d_write_siz(j3d);
\r
2244 if (j3d->cinfo->codec_format == CODEC_J3D) {
\r
2245 j3d_write_cap(j3d);
\r
2246 j3d_write_zsi(j3d);
\r
2248 j3d_write_cod(j3d);
\r
2249 j3d_write_qcd(j3d);
\r
2250 for (compno = 0; compno < volume->numcomps; compno++) {
\r
2251 opj_tcp_t *tcp = &cp->tcps[0];
\r
2252 if (tcp->tccps[compno].roishift)
\r
2253 j3d_write_rgn(j3d, compno, 0);
\r
2255 /*Optional 15444-2 markers*/
\r
2256 if (j3d->cp->tcps->tccps[0].atk != NULL)
\r
2257 j3d_write_atk(j3d);
\r
2258 if (j3d->volume->comps[0].dcoffset != 0)
\r
2259 j3d_write_dco(j3d);
\r
2261 if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)
\r
2262 j3d_write_com(j3d);
\r
2265 if(volume_info && volume_info->index_on) {
\r
2266 volume_info->main_head_end = cio_tell(cio) - 1;
\r
2270 /* create the tile encoder */
\r
2271 tcd = tcd_create(j3d->cinfo);
\r
2273 /* encode each tile */
\r
2274 for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
\r
2275 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
\r
2277 j3d->curtileno = tileno;
\r
2279 /* initialisation before tile encoding */
\r
2280 if (tileno == 0) {
\r
2281 tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
\r
2283 tcd_init_encode(tcd, volume, cp, j3d->curtileno);
\r
2287 if(volume_info && volume_info->index_on) {
\r
2288 volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
\r
2289 volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
\r
2293 j3d_write_sot(j3d);
\r
2295 for (compno = 1; compno < volume->numcomps; compno++) {
\r
2296 j3d_write_coc(j3d, compno);
\r
2297 j3d_write_qcc(j3d, compno);
\r
2300 if (cp->tcps[tileno].numpocs) {
\r
2301 j3d_write_poc(j3d);
\r
2303 j3d_write_sod(j3d, tcd); //--> tcd_encode_tile
\r
2306 if(volume_info && volume_info->index_on) {
\r
2307 volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
\r
2312 /* destroy the tile encoder */
\r
2313 tcd_free_encode(tcd);
\r
2316 j3d_write_eoc(j3d);
\r
2318 /* Creation of the index file */
\r
2319 if(volume_info && volume_info->index_on) {
\r
2320 if(!j3d_create_index(j3d, cio, volume_info, index)) {
\r
2321 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
\r