2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
33 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
36 /** @name Local static functions */
40 Write the SOC marker (Start Of Codestream)
43 static void j2k_write_soc(opj_j2k_t *j2k);
45 Read the SOC marker (Start of Codestream)
48 static void j2k_read_soc(opj_j2k_t *j2k);
50 Write the SIZ marker (image and tile size)
53 static void j2k_write_siz(opj_j2k_t *j2k);
55 Read the SIZ marker (image and tile size)
58 static void j2k_read_siz(opj_j2k_t *j2k);
60 Write the COM marker (comment)
63 static void j2k_write_com(opj_j2k_t *j2k);
65 Read the COM marker (comment)
68 static void j2k_read_com(opj_j2k_t *j2k);
70 Write the value concerning the specified component in the marker COD and COC
72 @param compno Number of the component concerned by the information written
74 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
76 Read the value concerning the specified component in the marker COD and COC
78 @param compno Number of the component concerned by the information read
80 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
82 Write the COD marker (coding style default)
85 static void j2k_write_cod(opj_j2k_t *j2k);
87 Read the COD marker (coding style default)
90 static void j2k_read_cod(opj_j2k_t *j2k);
92 Write the COC marker (coding style component)
94 @param compno Number of the component concerned by the information written
96 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
98 Read the COC marker (coding style component)
101 static void j2k_read_coc(opj_j2k_t *j2k);
103 Write the value concerning the specified component in the marker QCD and QCC
104 @param j2k J2K handle
105 @param compno Number of the component concerned by the information written
107 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
109 Read the value concerning the specified component in the marker QCD and QCC
110 @param j2k J2K handle
111 @param compno Number of the component concern by the information read
112 @param len Length of the information in the QCX part of the marker QCD/QCC
114 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
116 Write the QCD marker (quantization default)
117 @param j2k J2K handle
119 static void j2k_write_qcd(opj_j2k_t *j2k);
121 Read the QCD marker (quantization default)
122 @param j2k J2K handle
124 static void j2k_read_qcd(opj_j2k_t *j2k);
126 Write the QCC marker (quantization component)
127 @param j2k J2K handle
128 @param compno Number of the component concerned by the information written
130 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
132 Read the QCC marker (quantization component)
133 @param j2k J2K handle
135 static void j2k_read_qcc(opj_j2k_t *j2k);
137 Write the POC marker (progression order change)
138 @param j2k J2K handle
140 static void j2k_write_poc(opj_j2k_t *j2k);
142 Read the POC marker (progression order change)
143 @param j2k J2K handle
145 static void j2k_read_poc(opj_j2k_t *j2k);
147 Read the CRG marker (component registration)
148 @param j2k J2K handle
150 static void j2k_read_crg(opj_j2k_t *j2k);
152 Read the TLM marker (tile-part lengths)
153 @param j2k J2K handle
155 static void j2k_read_tlm(opj_j2k_t *j2k);
157 Read the PLM marker (packet length, main header)
158 @param j2k J2K handle
160 static void j2k_read_plm(opj_j2k_t *j2k);
162 Read the PLT marker (packet length, tile-part header)
163 @param j2k J2K handle
165 static void j2k_read_plt(opj_j2k_t *j2k);
167 Read the PPM marker (packet packet headers, main header)
168 @param j2k J2K handle
170 static void j2k_read_ppm(opj_j2k_t *j2k);
172 Read the PPT marker (packet packet headers, tile-part header)
173 @param j2k J2K handle
175 static void j2k_read_ppt(opj_j2k_t *j2k);
177 Write the SOT marker (start of tile-part)
178 @param j2k J2K handle
180 static void j2k_write_sot(opj_j2k_t *j2k);
182 Read the SOT marker (start of tile-part)
183 @param j2k J2K handle
185 static void j2k_read_sot(opj_j2k_t *j2k);
187 Write the SOD marker (start of data)
188 @param j2k J2K handle
189 @param tile_coder Pointer to a TCD handle
191 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
193 Read the SOD marker (start of data)
194 @param j2k J2K handle
196 static void j2k_read_sod(opj_j2k_t *j2k);
198 Write the RGN marker (region-of-interest)
199 @param j2k J2K handle
200 @param compno Number of the component concerned by the information written
201 @param tileno Number of the tile concerned by the information written
203 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
205 Read the RGN marker (region-of-interest)
206 @param j2k J2K handle
208 static void j2k_read_rgn(opj_j2k_t *j2k);
210 Write the EOC marker (end of codestream)
211 @param j2k J2K handle
213 static void j2k_write_eoc(opj_j2k_t *j2k);
215 Read the EOC marker (end of codestream)
216 @param j2k J2K handle
218 static void j2k_read_eoc(opj_j2k_t *j2k);
220 Read an unknown marker
221 @param j2k J2K handle
223 static void j2k_read_unk(opj_j2k_t *j2k);
229 /* ----------------------------------------------------------------------- */
231 void j2k_dump_image(FILE *fd, opj_image_t * img) {
233 fprintf(fd, "image {\n");
234 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
235 fprintf(fd, " numcomps=%d\n", img->numcomps);
236 for (compno = 0; compno < img->numcomps; compno++) {
237 opj_image_comp_t *comp = &img->comps[compno];
238 fprintf(fd, " comp %d {\n", compno);
239 fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy);
240 fprintf(fd, " prec=%d\n", comp->prec);
241 fprintf(fd, " sgnd=%d\n", comp->sgnd);
247 void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) {
248 int tileno, compno, layno, bandno, resno, numbands;
249 fprintf(fd, "coding parameters {\n");
250 fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
251 fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
252 fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th);
253 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
254 opj_tcp_t *tcp = &cp->tcps[tileno];
255 fprintf(fd, " tile %d {\n", tileno);
256 fprintf(fd, " csty=%x\n", tcp->csty);
257 fprintf(fd, " prg=%d\n", tcp->prg);
258 fprintf(fd, " numlayers=%d\n", tcp->numlayers);
259 fprintf(fd, " mct=%d\n", tcp->mct);
260 fprintf(fd, " rates=");
261 for (layno = 0; layno < tcp->numlayers; layno++) {
262 fprintf(fd, "%d ", tcp->rates[layno]);
265 for (compno = 0; compno < img->numcomps; compno++) {
266 opj_tccp_t *tccp = &tcp->tccps[compno];
267 fprintf(fd, " comp %d {\n", compno);
268 fprintf(fd, " csty=%x\n", tccp->csty);
269 fprintf(fd, " numresolutions=%d\n", tccp->numresolutions);
270 fprintf(fd, " cblkw=%d\n", tccp->cblkw);
271 fprintf(fd, " cblkh=%d\n", tccp->cblkh);
272 fprintf(fd, " cblksty=%x\n", tccp->cblksty);
273 fprintf(fd, " qmfbid=%d\n", tccp->qmfbid);
274 fprintf(fd, " qntsty=%d\n", tccp->qntsty);
275 fprintf(fd, " numgbits=%d\n", tccp->numgbits);
276 fprintf(fd, " roishift=%d\n", tccp->roishift);
277 fprintf(fd, " stepsizes=");
278 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
279 for (bandno = 0; bandno < numbands; bandno++) {
280 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
281 tccp->stepsizes[bandno].expn);
285 if (tccp->csty & J2K_CCP_CSTY_PRT) {
286 fprintf(fd, " prcw=");
287 for (resno = 0; resno < tccp->numresolutions; resno++) {
288 fprintf(fd, "%d ", tccp->prcw[resno]);
291 fprintf(fd, " prch=");
292 for (resno = 0; resno < tccp->numresolutions; resno++) {
293 fprintf(fd, "%d ", tccp->prch[resno]);
304 /* ----------------------------------------------------------------------- */
306 static void j2k_write_soc(opj_j2k_t *j2k) {
307 opj_cio_t *cio = j2k->cio;
308 cio_write(cio, J2K_MS_SOC, 2);
311 static void j2k_read_soc(opj_j2k_t *j2k) {
312 j2k->state = J2K_STATE_MHSIZ;
315 static void j2k_write_siz(opj_j2k_t *j2k) {
319 opj_cio_t *cio = j2k->cio;
320 opj_image_t *image = j2k->image;
321 opj_cp_t *cp = j2k->cp;
323 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
324 lenp = cio_tell(cio);
326 cio_write(cio, 0, 2); /* Rsiz (capabilities) */
327 cio_write(cio, image->x1, 4); /* Xsiz */
328 cio_write(cio, image->y1, 4); /* Ysiz */
329 cio_write(cio, image->x0, 4); /* X0siz */
330 cio_write(cio, image->y0, 4); /* Y0siz */
331 cio_write(cio, cp->tdx, 4); /* XTsiz */
332 cio_write(cio, cp->tdy, 4); /* YTsiz */
333 cio_write(cio, cp->tx0, 4); /* XT0siz */
334 cio_write(cio, cp->ty0, 4); /* YT0siz */
335 cio_write(cio, image->numcomps, 2); /* Csiz */
336 for (i = 0; i < image->numcomps; i++) {
337 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
338 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
339 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
341 len = cio_tell(cio) - lenp;
343 cio_write(cio, len, 2); /* Lsiz */
344 cio_seek(cio, lenp + len);
347 static void j2k_read_siz(opj_j2k_t *j2k) {
350 opj_cio_t *cio = j2k->cio;
351 opj_image_t *image = j2k->image;
352 opj_cp_t *cp = j2k->cp;
354 len = cio_read(cio, 2); /* Lsiz */
355 cio_read(cio, 2); /* Rsiz (capabilities) */
356 image->x1 = cio_read(cio, 4); /* Xsiz */
357 image->y1 = cio_read(cio, 4); /* Ysiz */
358 image->x0 = cio_read(cio, 4); /* X0siz */
359 image->y0 = cio_read(cio, 4); /* Y0siz */
360 cp->tdx = cio_read(cio, 4); /* XTsiz */
361 cp->tdy = cio_read(cio, 4); /* YTsiz */
362 cp->tx0 = cio_read(cio, 4); /* XT0siz */
363 cp->ty0 = cio_read(cio, 4); /* YT0siz */
365 image->numcomps = cio_read(cio, 2); /* Csiz */
366 image->comps = (opj_image_comp_t *) opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
367 for (i = 0; i < image->numcomps; i++) {
369 tmp = cio_read(cio, 1); /* Ssiz_i */
370 image->comps[i].prec = (tmp & 0x7f) + 1;
371 image->comps[i].sgnd = tmp >> 7;
372 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
373 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
376 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
377 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
379 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
380 image->comps[i].factor = 0; /* reducing factor per component */
383 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
384 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
385 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
386 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
389 for (i = 0; i < cp->tw * cp->th; i++) {
391 cp->tcps[i].numpocs = 0;
392 cp->tcps[i].first = 1;
395 /* Initialization for PPM marker */
398 cp->ppm_data_first = NULL;
399 cp->ppm_previous = 0;
402 j2k->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
403 for (i = 0; i < cp->tw * cp->th; i++) {
404 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
406 j2k->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * sizeof(unsigned char *));
407 j2k->tile_len = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
408 j2k->state = J2K_STATE_MH;
411 static void j2k_write_com(opj_j2k_t *j2k) {
415 if(j2k->cp->comment) {
416 opj_cio_t *cio = j2k->cio;
417 char *comment = j2k->cp->comment;
419 cio_write(cio, J2K_MS_COM, 2);
420 lenp = cio_tell(cio);
422 cio_write(cio, 0, 2);
423 for (i = 0; i < strlen(comment); i++) {
424 cio_write(cio, comment[i], 1);
426 len = cio_tell(cio) - lenp;
428 cio_write(cio, len, 2);
429 cio_seek(cio, lenp + len);
433 static void j2k_read_com(opj_j2k_t *j2k) {
436 opj_cio_t *cio = j2k->cio;
438 len = cio_read(cio, 2);
439 cio_skip(cio, len - 2);
442 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
445 opj_cp_t *cp = j2k->cp;
446 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
447 opj_tccp_t *tccp = &tcp->tccps[compno];
448 opj_cio_t *cio = j2k->cio;
450 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
451 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
452 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
453 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
454 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
456 if (tccp->csty & J2K_CCP_CSTY_PRT) {
457 for (i = 0; i < tccp->numresolutions; i++) {
458 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
463 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
466 opj_cp_t *cp = j2k->cp;
467 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
468 opj_tccp_t *tccp = &tcp->tccps[compno];
469 opj_cio_t *cio = j2k->cio;
471 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
473 /* check the reduce value */
474 cp->reduce = int_min((tccp->numresolutions)-1, cp->reduce);
475 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
476 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
477 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
478 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
479 if (tccp->csty & J2K_CP_CSTY_PRT) {
480 for (i = 0; i < tccp->numresolutions; i++) {
481 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
482 tccp->prcw[i] = tmp & 0xf;
483 tccp->prch[i] = tmp >> 4;
488 static void j2k_write_cod(opj_j2k_t *j2k) {
490 opj_tcp_t *tcp = NULL;
493 opj_cio_t *cio = j2k->cio;
495 cio_write(cio, J2K_MS_COD, 2); /* COD */
497 lenp = cio_tell(cio);
501 tcp = &cp->tcps[j2k->curtileno];
503 cio_write(cio, tcp->csty, 1); /* Scod */
504 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
505 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
506 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
508 j2k_write_cox(j2k, 0);
509 len = cio_tell(cio) - lenp;
511 cio_write(cio, len, 2); /* Lcod */
512 cio_seek(cio, lenp + len);
515 static void j2k_read_cod(opj_j2k_t *j2k) {
518 opj_cio_t *cio = j2k->cio;
519 opj_cp_t *cp = j2k->cp;
520 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
521 opj_image_t *image = j2k->image;
523 len = cio_read(cio, 2); /* Lcod */
524 tcp->csty = cio_read(cio, 1); /* Scod */
525 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
526 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
527 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
530 for (i = 0; i < image->numcomps; i++) {
531 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
533 j2k_read_cox(j2k, i);
537 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
540 opj_cp_t *cp = j2k->cp;
541 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
542 opj_image_t *image = j2k->image;
543 opj_cio_t *cio = j2k->cio;
545 cio_write(cio, J2K_MS_COC, 2); /* COC */
546 lenp = cio_tell(cio);
548 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
549 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
550 j2k_write_cox(j2k, compno);
551 len = cio_tell(cio) - lenp;
553 cio_write(cio, len, 2); /* Lcoc */
554 cio_seek(cio, lenp + len);
557 static void j2k_read_coc(opj_j2k_t *j2k) {
560 opj_cp_t *cp = j2k->cp;
561 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
562 opj_image_t *image = j2k->image;
563 opj_cio_t *cio = j2k->cio;
565 len = cio_read(cio, 2); /* Lcoc */
566 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
567 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
568 j2k_read_cox(j2k, compno);
571 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
572 int bandno, numbands;
575 opj_cp_t *cp = j2k->cp;
576 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
577 opj_tccp_t *tccp = &tcp->tccps[compno];
578 opj_cio_t *cio = j2k->cio;
580 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
581 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
583 for (bandno = 0; bandno < numbands; bandno++) {
584 expn = tccp->stepsizes[bandno].expn;
585 mant = tccp->stepsizes[bandno].mant;
587 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
588 cio_write(cio, expn << 3, 1); /* SPqcx_i */
590 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
595 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
597 int bandno, numbands;
599 opj_cp_t *cp = j2k->cp;
600 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
601 opj_tccp_t *tccp = &tcp->tccps[compno];
602 opj_cio_t *cio = j2k->cio;
604 tmp = cio_read(cio, 1); /* Sqcx */
605 tccp->qntsty = tmp & 0x1f;
606 tccp->numgbits = tmp >> 5;
607 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
608 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
609 for (bandno = 0; bandno < numbands; bandno++) {
611 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
612 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
615 tmp = cio_read(cio, 2); /* SPqcx_i */
619 tccp->stepsizes[bandno].expn = expn;
620 tccp->stepsizes[bandno].mant = mant;
623 /* Add Antonin : if scalar_derived -> compute other stepsizes */
624 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
625 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
626 tccp->stepsizes[bandno].expn =
627 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
628 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
629 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
635 static void j2k_write_qcd(opj_j2k_t *j2k) {
638 opj_cio_t *cio = j2k->cio;
640 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
641 lenp = cio_tell(cio);
643 j2k_write_qcx(j2k, 0);
644 len = cio_tell(cio) - lenp;
646 cio_write(cio, len, 2); /* Lqcd */
647 cio_seek(cio, lenp + len);
650 static void j2k_read_qcd(opj_j2k_t *j2k) {
653 opj_cio_t *cio = j2k->cio;
654 opj_image_t *image = j2k->image;
656 len = cio_read(cio, 2); /* Lqcd */
658 for (i = 0; i < image->numcomps; i++) {
660 j2k_read_qcx(j2k, i, len - 2);
664 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
667 opj_cio_t *cio = j2k->cio;
669 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
670 lenp = cio_tell(cio);
672 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
673 j2k_write_qcx(j2k, compno);
674 len = cio_tell(cio) - lenp;
676 cio_write(cio, len, 2); /* Lqcc */
677 cio_seek(cio, lenp + len);
680 static void j2k_read_qcc(opj_j2k_t *j2k) {
682 int numcomp = j2k->image->numcomps;
683 opj_cio_t *cio = j2k->cio;
685 len = cio_read(cio, 2); /* Lqcc */
686 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
687 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
690 static void j2k_write_poc(opj_j2k_t *j2k) {
691 int len, numpchgs, i;
693 int numcomps = j2k->image->numcomps;
695 opj_cp_t *cp = j2k->cp;
696 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
697 opj_tccp_t *tccp = &tcp->tccps[0];
698 opj_cio_t *cio = j2k->cio;
700 numpchgs = tcp->numpocs;
701 cio_write(cio, J2K_MS_POC, 2); /* POC */
702 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
703 cio_write(cio, len, 2); /* Lpoc */
704 for (i = 0; i < numpchgs; i++) {
705 opj_poc_t *poc = &tcp->pocs[i];
706 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
707 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
708 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
709 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
710 cio_write(cio, poc->resno1, 1); /* REpoc_i */
711 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
712 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
713 poc->compno1 = int_min(poc->compno1, numcomps);
714 cio_write(cio, poc->prg, 1); /* Ppoc_i */
718 static void j2k_read_poc(opj_j2k_t *j2k) {
719 int len, numpchgs, i, old_poc;
721 int numcomps = j2k->image->numcomps;
723 opj_cp_t *cp = j2k->cp;
724 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
725 opj_tccp_t *tccp = &tcp->tccps[0];
726 opj_cio_t *cio = j2k->cio;
728 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
730 len = cio_read(cio, 2); /* Lpoc */
731 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
733 for (i = old_poc; i < numpchgs + old_poc; i++) {
736 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
737 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
738 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
739 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolutions); /* REpoc_i */
740 poc->compno1 = int_min(
741 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
742 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
745 tcp->numpocs = numpchgs + old_poc - 1;
748 static void j2k_read_crg(opj_j2k_t *j2k) {
749 int len, i, Xcrg_i, Ycrg_i;
751 opj_cio_t *cio = j2k->cio;
752 int numcomps = j2k->image->numcomps;
754 len = cio_read(cio, 2); /* Lcrg */
755 for (i = 0; i < numcomps; i++) {
756 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
757 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
761 static void j2k_read_tlm(opj_j2k_t *j2k) {
762 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
763 long int Ttlm_i, Ptlm_i;
765 opj_cio_t *cio = j2k->cio;
767 len = cio_read(cio, 2); /* Ltlm */
768 Ztlm = cio_read(cio, 1); /* Ztlm */
769 Stlm = cio_read(cio, 1); /* Stlm */
770 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
771 SP = (Stlm >> 6) & 0x01;
772 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
773 for (i = 0; i < tile_tlm; i++) {
774 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
775 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
779 static void j2k_read_plm(opj_j2k_t *j2k) {
780 int len, i, Zplm, Nplm, add, packet_len = 0;
782 opj_cio_t *cio = j2k->cio;
784 len = cio_read(cio, 2); /* Lplm */
785 Zplm = cio_read(cio, 1); /* Zplm */
788 Nplm = cio_read(cio, 4); /* Nplm */
790 for (i = Nplm; i > 0; i--) {
791 add = cio_read(cio, 1);
793 packet_len = (packet_len << 7) + add; /* Iplm_ij */
794 if ((add & 0x80) == 0) {
804 static void j2k_read_plt(opj_j2k_t *j2k) {
805 int len, i, Zplt, packet_len = 0, add;
807 opj_cio_t *cio = j2k->cio;
809 len = cio_read(cio, 2); /* Lplt */
810 Zplt = cio_read(cio, 1); /* Zplt */
811 for (i = len - 3; i > 0; i--) {
812 add = cio_read(cio, 1);
813 packet_len = (packet_len << 7) + add; /* Iplt_i */
814 if ((add & 0x80) == 0) {
821 static void j2k_read_ppm(opj_j2k_t *j2k) {
822 int len, Z_ppm, i, j;
825 opj_cp_t *cp = j2k->cp;
826 opj_cio_t *cio = j2k->cio;
828 len = cio_read(cio, 2);
831 Z_ppm = cio_read(cio, 1); /* Z_ppm */
834 if (cp->ppm_previous == 0) {
835 N_ppm = cio_read(cio, 4); /* N_ppm */
838 N_ppm = cp->ppm_previous;
841 if (Z_ppm == 0) { /* First PPM marker */
842 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
843 cp->ppm_data_first = cp->ppm_data;
845 } else { /* NON-first PPM marker */
846 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
847 cp->ppm_data_first = cp->ppm_data;
848 cp->ppm_len = N_ppm + cp->ppm_store;
850 for (i = N_ppm; i > 0; i--) { /* Read packet header */
851 cp->ppm_data[j] = cio_read(cio, 1);
855 break; /* Case of non-finished packet header in present marker but finished in next one */
857 cp->ppm_previous = i - 1;
862 static void j2k_read_ppt(opj_j2k_t *j2k) {
863 int len, Z_ppt, i, j = 0;
865 opj_cp_t *cp = j2k->cp;
866 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
867 opj_cio_t *cio = j2k->cio;
869 len = cio_read(cio, 2);
870 Z_ppt = cio_read(cio, 1);
872 if (Z_ppt == 0) { /* First PPT marker */
873 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
874 tcp->ppt_data_first = tcp->ppt_data;
876 tcp->ppt_len = len - 3;
877 } else { /* NON-first PPT marker */
878 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
879 tcp->ppt_data_first = tcp->ppt_data;
880 tcp->ppt_len = len - 3 + tcp->ppt_store;
883 for (i = len - 3; i > 0; i--) {
884 tcp->ppt_data[j] = cio_read(cio, 1);
890 static void j2k_write_sot(opj_j2k_t *j2k) {
893 opj_cio_t *cio = j2k->cio;
895 j2k->sot_start = cio_tell(cio);
896 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
897 lenp = cio_tell(cio);
898 cio_skip(cio, 2); /* Lsot (further) */
899 cio_write(cio, j2k->curtileno, 2); /* Isot */
900 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
901 cio_write(cio, 0, 1); /* TPsot */
902 cio_write(cio, 1, 1); /* TNsot */
903 len = cio_tell(cio) - lenp;
905 cio_write(cio, len, 2); /* Lsot */
906 cio_seek(cio, lenp + len);
909 static void j2k_read_sot(opj_j2k_t *j2k) {
910 int len, tileno, totlen, partno, numparts, i;
911 opj_tcp_t *tcp = NULL;
914 opj_cp_t *cp = j2k->cp;
915 opj_cio_t *cio = j2k->cio;
917 len = cio_read(cio, 2);
918 tileno = cio_read(cio, 2);
920 if (cp->tileno_size == 0) {
921 cp->tileno[cp->tileno_size] = tileno;
925 while (i < cp->tileno_size && status == 0) {
926 status = cp->tileno[i] == tileno ? 1 : 0;
930 cp->tileno[cp->tileno_size] = tileno;
935 totlen = cio_read(cio, 4);
937 totlen = cio_numbytesleft(cio) + 8;
939 partno = cio_read(cio, 1);
940 numparts = cio_read(cio, 1);
942 j2k->curtileno = tileno;
943 j2k->eot = cio_getbp(cio) - 12 + totlen;
944 j2k->state = J2K_STATE_TPH;
945 tcp = &cp->tcps[j2k->curtileno];
947 if (tcp->first == 1) {
949 /* Initialization PPT */
950 opj_tccp_t *tmp = tcp->tccps;
951 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
953 tcp->ppt_data = NULL;
954 tcp->ppt_data_first = NULL;
957 for (i = 0; i < j2k->image->numcomps; i++) {
958 tcp->tccps[i] = j2k->default_tcp->tccps[i];
960 cp->tcps[j2k->curtileno].first = 0;
964 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
967 opj_tcp_t *tcp = NULL;
968 opj_image_info_t *image_info = NULL;
970 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
971 opj_cp_t *cp = j2k->cp;
972 opj_cio_t *cio = j2k->cio;
974 cio_write(cio, J2K_MS_SOD, 2);
975 if (j2k->curtileno == 0) {
976 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
980 image_info = j2k->image_info;
981 if (image_info && image_info->index_on) {
982 image_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
986 tcp = &cp->tcps[j2k->curtileno];
987 for (layno = 0; layno < tcp->numlayers; layno++) {
988 tcp->rates[layno] -= tcp->rates[layno] ? (j2k->sod_start / (cp->th * cp->tw)) : 0;
994 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, image_info);
996 /* Writing Psot in SOT marker */
997 totlen = cio_tell(cio) + l - j2k->sot_start;
998 cio_seek(cio, j2k->sot_start + 6);
999 cio_write(cio, totlen, 4);
1000 cio_seek(cio, j2k->sot_start + totlen);
1003 static void j2k_read_sod(opj_j2k_t *j2k) {
1004 int len, truncate = 0, i;
1005 unsigned char *data = NULL, *data_ptr = NULL;
1007 opj_cio_t *cio = j2k->cio;
1008 int curtileno = j2k->curtileno;
1010 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1012 if (len == cio_numbytesleft(cio) + 1) {
1013 truncate = 1; /* Case of a truncate codestream */
1016 data = (unsigned char *) opj_malloc((j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1018 for (i = 0; i < j2k->tile_len[curtileno]; i++) {
1019 data[i] = j2k->tile_data[curtileno][i];
1022 data_ptr = data + j2k->tile_len[curtileno];
1023 for (i = 0; i < len; i++) {
1024 data_ptr[i] = cio_read(cio, 1);
1027 j2k->tile_len[curtileno] += len;
1028 opj_free(j2k->tile_data[curtileno]);
1029 j2k->tile_data[curtileno] = data;
1032 j2k->state = J2K_STATE_TPHSOT;
1034 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1038 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1040 opj_cp_t *cp = j2k->cp;
1041 opj_tcp_t *tcp = &cp->tcps[tileno];
1042 opj_cio_t *cio = j2k->cio;
1043 int numcomps = j2k->image->numcomps;
1045 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1046 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1047 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1048 cio_write(cio, 0, 1); /* Srgn */
1049 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1052 static void j2k_read_rgn(opj_j2k_t *j2k) {
1053 int len, compno, roisty;
1055 opj_cp_t *cp = j2k->cp;
1056 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1057 opj_cio_t *cio = j2k->cio;
1058 int numcomps = j2k->image->numcomps;
1060 len = cio_read(cio, 2); /* Lrgn */
1061 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1062 roisty = cio_read(cio, 1); /* Srgn */
1063 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1066 static void j2k_write_eoc(opj_j2k_t *j2k) {
1067 opj_cio_t *cio = j2k->cio;
1068 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1069 cio_write(cio, J2K_MS_EOC, 2);
1072 static void j2k_read_eoc(opj_j2k_t *j2k) {
1075 #ifndef NO_PACKETS_DECODING
1076 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1077 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1078 for (i = 0; i < j2k->cp->tileno_size; i++) {
1079 tileno = j2k->cp->tileno[i];
1080 tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno);
1081 opj_free(j2k->tile_data[tileno]);
1082 j2k->tile_data[tileno] = NULL;
1084 tcd_free_decode(tcd);
1087 for (i = 0; i < j2k->cp->tileno_size; i++) {
1088 tileno = j2k->cp->tileno[i];
1089 opj_free(j2k->tile_data[tileno]);
1090 j2k->tile_data[tileno] = NULL;
1094 j2k->state = J2K_STATE_MT;
1097 static void j2k_read_unk(opj_j2k_t *j2k) {
1098 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1101 typedef struct opj_dec_mstabent {
1104 /** value of the state when the marker can appear */
1106 /** action linked to the marker */
1107 void (*handler) (opj_j2k_t *j2k);
1108 } opj_dec_mstabent_t;
1110 opj_dec_mstabent_t j2k_dec_mstab[] = {
1111 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1112 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1113 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1114 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1115 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1116 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1117 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1118 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1119 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1120 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1121 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1122 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1123 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1124 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1125 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1126 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1128 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1129 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1130 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1134 Read the lookup table containing all the marker, status and action
1135 @param id Marker value
1137 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1138 opj_dec_mstabent_t *e;
1139 for (e = j2k_dec_mstab; e->id != 0; e++) {
1147 /* ----------------------------------------------------------------------- */
1148 /* J2K / JPT decoder interface */
1149 /* ----------------------------------------------------------------------- */
1151 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1152 opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1155 j2k->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1156 if(!j2k->default_tcp) {
1164 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1167 if(j2k->tile_len != NULL) {
1168 opj_free(j2k->tile_len);
1170 if(j2k->tile_data != NULL) {
1171 opj_free(j2k->tile_data);
1173 if(j2k->default_tcp != NULL) {
1174 opj_tcp_t *default_tcp = j2k->default_tcp;
1175 if(default_tcp->ppt_data_first != NULL) {
1176 opj_free(default_tcp->ppt_data_first);
1178 if(j2k->default_tcp->tccps != NULL) {
1179 opj_free(j2k->default_tcp->tccps);
1181 opj_free(j2k->default_tcp);
1183 if(j2k->cp != NULL) {
1184 opj_cp_t *cp = j2k->cp;
1185 if(cp->tcps != NULL) {
1186 for(i = 0; i < cp->tw * cp->th; i++) {
1187 if(cp->tcps[i].ppt_data_first != NULL) {
1188 opj_free(cp->tcps[i].ppt_data_first);
1190 if(cp->tcps[i].tccps != NULL) {
1191 opj_free(cp->tcps[i].tccps);
1196 if(cp->ppm_data_first != NULL) {
1197 opj_free(cp->ppm_data_first);
1199 if(cp->tileno != NULL) {
1200 opj_free(cp->tileno);
1202 if(cp->comment != NULL) {
1203 opj_free(cp->comment);
1212 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1213 if(j2k && parameters) {
1214 /* create and initialize the coding parameters structure */
1215 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1216 cp->reduce = parameters->cp_reduce;
1217 cp->layer = parameters->cp_layer;
1219 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1224 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio) {
1225 opj_image_t *image = NULL;
1227 opj_common_ptr cinfo = j2k->cinfo;
1231 /* create an empty image */
1232 image = opj_image_create0();
1235 j2k->state = J2K_STATE_MHSOC;
1238 opj_dec_mstabent_t *e;
1239 int id = cio_read(cio, 2);
1240 if (id >> 8 != 0xff) {
1241 opj_image_destroy(image);
1242 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1245 e = j2k_dec_mstab_lookup(id);
1246 if (!(j2k->state & e->states)) {
1247 opj_image_destroy(image);
1248 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1254 if (j2k->state == J2K_STATE_MT) {
1257 if (j2k->state == J2K_STATE_NEOC) {
1261 if (j2k->state == J2K_STATE_NEOC) {
1265 if (j2k->state != J2K_STATE_MT) {
1266 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1273 * Read a JPT-stream and decode file
1276 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio) {
1277 opj_image_t *image = NULL;
1278 opj_jpt_msg_header_t header;
1281 opj_common_ptr cinfo = j2k->cinfo;
1285 /* create an empty image */
1286 image = opj_image_create0();
1288 j2k->state = J2K_STATE_MHSOC;
1290 /* Initialize the header */
1291 jpt_init_msg_header(&header);
1292 /* Read the first header of the message */
1293 jpt_read_msg_header(cinfo, cio, &header);
1295 position = cio_tell(cio);
1296 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
1297 opj_image_destroy(image);
1298 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1303 opj_dec_mstabent_t *e = NULL;
1306 if (!cio_numbytesleft(cio)) {
1310 /* data-bin read -> need to read a new header */
1311 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1312 jpt_read_msg_header(cinfo, cio, &header);
1313 position = cio_tell(cio);
1314 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
1315 opj_image_destroy(image);
1316 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1321 id = cio_read(cio, 2);
1322 if (id >> 8 != 0xff) {
1323 opj_image_destroy(image);
1324 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1327 e = j2k_dec_mstab_lookup(id);
1328 if (!(j2k->state & e->states)) {
1329 opj_image_destroy(image);
1330 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1336 if (j2k->state == J2K_STATE_MT) {
1339 if (j2k->state == J2K_STATE_NEOC) {
1343 if (j2k->state == J2K_STATE_NEOC) {
1347 if (j2k->state != J2K_STATE_MT) {
1348 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1354 /* ----------------------------------------------------------------------- */
1355 /* J2K encoder interface */
1356 /* ----------------------------------------------------------------------- */
1358 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1359 opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1366 void j2k_destroy_compress(opj_j2k_t *j2k) {
1371 if(j2k->image_info != NULL) {
1372 opj_image_info_t *image_info = j2k->image_info;
1373 if (image_info->index_on && j2k->cp) {
1374 opj_cp_t *cp = j2k->cp;
1375 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1376 opj_tile_info_t *tile_info = &image_info->tile[tileno];
1377 opj_free(tile_info->thresh);
1378 opj_free(tile_info->packet);
1380 opj_free(image_info->tile);
1382 opj_free(image_info);
1384 if(j2k->cp != NULL) {
1385 opj_cp_t *cp = j2k->cp;
1388 opj_free(cp->comment);
1391 opj_free(cp->matrice);
1393 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1394 opj_free(cp->tcps[tileno].tccps);
1403 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1404 int i, j, tileno, numpocs_tile;
1405 opj_cp_t *cp = NULL;
1407 if(!j2k || !parameters || ! image) {
1411 /* create and initialize the coding parameters structure */
1412 cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1414 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1417 /* set default values for cp */
1422 copy user encoding parameters
1425 cp->disto_alloc = parameters->cp_disto_alloc;
1426 cp->fixed_alloc = parameters->cp_fixed_alloc;
1427 cp->fixed_quality = parameters->cp_fixed_quality;
1429 /* mod fixed_quality */
1430 if(parameters->cp_matrice) {
1431 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
1432 cp->matrice = (int *) opj_malloc(array_size);
1433 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1436 /* creation of an index file ? */
1437 cp->index_on = parameters->index_on;
1439 j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t));
1443 cp->tdx = parameters->cp_tdx;
1444 cp->tdy = parameters->cp_tdy;
1447 cp->tx0 = parameters->cp_tx0;
1448 cp->ty0 = parameters->cp_ty0;
1450 /* comment string */
1451 if(parameters->cp_comment) {
1452 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1454 strcpy(cp->comment, parameters->cp_comment);
1459 calculate other encoding parameters
1462 if (parameters->tile_size_on) {
1463 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1464 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1466 cp->tdx = image->x1 - cp->tx0;
1467 cp->tdy = image->y1 - cp->ty0;
1470 /* initialize the mutiple tiles */
1471 /* ---------------------------- */
1472 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
1474 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1475 opj_tcp_t *tcp = &cp->tcps[tileno];
1476 tcp->numlayers = parameters->tcp_numlayers;
1477 for (j = 0; j < tcp->numlayers; j++) {
1478 if (cp->fixed_quality) { /* add fixed_quality */
1479 tcp->distoratio[j] = parameters->tcp_distoratio[j];
1481 tcp->rates[j] = parameters->tcp_rates[j];
1484 tcp->csty = parameters->csty;
1485 tcp->prg = parameters->prog_order;
1486 tcp->mct = image->numcomps == 3 ? 1 : 0;
1490 if (parameters->numpocs) {
1491 /* initialisation of POC */
1493 for (i = 0; i < parameters->numpocs; i++) {
1494 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
1495 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
1496 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
1497 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
1498 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
1499 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
1500 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
1501 tcp_poc->prg = parameters->POC[numpocs_tile].prg;
1502 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
1507 tcp->numpocs = numpocs_tile;
1509 tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1511 for (i = 0; i < image->numcomps; i++) {
1512 opj_tccp_t *tccp = &tcp->tccps[i];
1513 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
1514 tccp->numresolutions = parameters->numresolution;
1515 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
1516 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
1517 tccp->cblksty = parameters->mode;
1518 tccp->qmfbid = parameters->irreversible ? 0 : 1;
1519 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
1521 if (i == parameters->roi_compno) {
1522 tccp->roishift = parameters->roi_shift;
1526 if (parameters->csty & J2K_CCP_CSTY_PRT) {
1528 for (j = tccp->numresolutions - 1; j >= 0; j--) {
1529 if (p < parameters->res_spec) {
1530 if (parameters->prcw_init[p] < 1) {
1533 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
1535 if (parameters->prch_init[p] < 1) {
1538 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
1541 int res_spec = parameters->res_spec;
1542 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
1543 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
1544 if (size_prcw < 1) {
1547 tccp->prcw[j] = int_floorlog2(size_prcw);
1549 if (size_prch < 1) {
1552 tccp->prch[j] = int_floorlog2(size_prch);
1556 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
1559 for (j = 0; j < tccp->numresolutions; j++) {
1565 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
1571 Create an index file
1575 @param index Index filename
1576 @return Returns 1 if successful, returns 0 otherwise
1578 static int j2k_create_index(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_info_t *image_info, char *index) {
1579 int tileno, compno, layno, resno, precno, pack_nb, x, y;
1580 FILE *stream = NULL;
1581 double total_disto = 0;
1583 image_info->codestream_size = cio_tell(cio) + j2k->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
1585 stream = fopen(index, "w");
1587 opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
1591 fprintf(stream, "%d %d\n", image_info->image_w, image_info->image_h);
1592 fprintf(stream, "%d\n", image_info->prog);
1593 fprintf(stream, "%d %d\n", image_info->tile_x, image_info->tile_y);
1594 fprintf(stream, "%d %d\n", image_info->tw, image_info->th);
1595 fprintf(stream, "%d\n", image_info->comp);
1596 fprintf(stream, "%d\n", image_info->layer);
1597 fprintf(stream, "%d\n", image_info->decomposition);
1599 for (resno = image_info->decomposition; resno >= 0; resno--) {
1600 fprintf(stream, "[%d,%d] ",
1601 (1 << image_info->tile[0].pdx[resno]), (1 << image_info->tile[0].pdx[resno])); /* based on tile 0 */
1603 fprintf(stream, "\n");
1604 fprintf(stream, "%d\n", image_info->main_head_end);
1605 fprintf(stream, "%d\n", image_info->codestream_size);
1607 for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
1608 fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n",
1609 image_info->tile[tileno].num_tile,
1610 image_info->tile[tileno].start_pos,
1611 image_info->tile[tileno].end_header,
1612 image_info->tile[tileno].end_pos,
1613 image_info->tile[tileno].distotile, image_info->tile[tileno].nbpix,
1614 image_info->tile[tileno].distotile / image_info->tile[tileno].nbpix);
1617 for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
1618 int start_pos, end_pos;
1623 fprintf(stream, "pkno tileno layerno resno compno precno start_pos end_pos deltaSE \n");
1626 if (image_info->prog == LRCP) { /* LRCP */
1628 fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto");
1630 for (layno = 0; layno < image_info->layer; layno++) {
1631 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1632 for (compno = 0; compno < image_info->comp; compno++) {
1633 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1634 for (precno = 0; precno < prec_max; precno++) {
1635 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1636 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1637 disto = image_info->tile[tileno].packet[pack_nb].disto;
1638 fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
1639 pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
1640 total_disto += disto;
1647 else if (image_info->prog == RLCP) { /* RLCP */
1649 fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto");
1651 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1652 for (layno = 0; layno < image_info->layer; layno++) {
1653 for (compno = 0; compno < image_info->comp; compno++) {
1654 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1655 for (precno = 0; precno < prec_max; precno++) {
1656 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1657 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1658 disto = image_info->tile[tileno].packet[pack_nb].disto;
1659 fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
1660 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
1661 total_disto += disto;
1668 else if (image_info->prog == RPCL) { /* RPCL */
1670 fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
1672 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1673 /* I suppose components have same XRsiz, YRsiz */
1674 int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
1675 int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
1676 int x1 = x0 + image_info->tile_x;
1677 int y1 = y0 + image_info->tile_y;
1678 for(y = y0; y < y1; y++) {
1679 for(x = x0; x < x1; x++) {
1680 for (compno = 0; compno < image_info->comp; compno++) {
1681 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1682 for (precno = 0; precno < prec_max; precno++) {
1683 int pcnx = image_info->tile[tileno].pw[resno];
1684 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
1685 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
1686 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
1687 int precno_y = (int) floor( (float)precno/(float)pcnx );
1688 if (precno_y*pcy == y ) {
1689 if (precno_x*pcx == x ) {
1690 for (layno = 0; layno < image_info->layer; layno++) {
1691 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1692 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1693 disto = image_info->tile[tileno].packet[pack_nb].disto;
1694 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
1695 pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
1696 total_disto += disto;
1707 else if (image_info->prog == PCRL) { /* PCRL */
1708 /* I suppose components have same XRsiz, YRsiz */
1709 int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
1710 int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
1711 int x1 = x0 + image_info->tile_x;
1712 int y1 = y0 + image_info->tile_y;
1714 fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
1716 for(y = y0; y < y1; y++) {
1717 for(x = x0; x < x1; x++) {
1718 for (compno = 0; compno < image_info->comp; compno++) {
1719 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1720 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1721 for (precno = 0; precno < prec_max; precno++) {
1722 int pcnx = image_info->tile[tileno].pw[resno];
1723 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
1724 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
1725 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
1726 int precno_y = (int) floor( (float)precno/(float)pcnx );
1727 if (precno_y*pcy == y ) {
1728 if (precno_x*pcx == x ) {
1729 for (layno = 0; layno < image_info->layer; layno++) {
1730 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1731 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1732 disto = image_info->tile[tileno].packet[pack_nb].disto;
1733 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
1734 pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
1735 total_disto += disto;
1748 fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
1750 for (compno = 0; compno < image_info->comp; compno++) {
1751 /* I suppose components have same XRsiz, YRsiz */
1752 int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
1753 int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
1754 int x1 = x0 + image_info->tile_x;
1755 int y1 = y0 + image_info->tile_y;
1756 for(y = y0; y < y1; y++) {
1757 for(x = x0; x < x1; x++) {
1758 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
1759 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
1760 for (precno = 0; precno < prec_max; precno++) {
1761 int pcnx = image_info->tile[tileno].pw[resno];
1762 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
1763 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
1764 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
1765 int precno_y = (int) floor( (float)precno/(float)pcnx );
1766 if (precno_y*pcy == y ) {
1767 if (precno_x*pcx == x ) {
1768 for (layno = 0; layno < image_info->layer; layno++) {
1769 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
1770 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
1771 disto = image_info->tile[tileno].packet[pack_nb].disto;
1772 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
1773 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
1774 total_disto += disto;
1787 fprintf(stream, "%8e\n", image_info->D_max); /* SE max */
1788 fprintf(stream, "%.8e\n", total_disto); /* SE totale */
1794 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) {
1796 opj_image_info_t *image_info = NULL;
1797 opj_cp_t *cp = NULL;
1799 opj_tcd_t *tcd = NULL; /* TCD component */
1806 /* j2k_dump_cp(stdout, image, cp); */
1809 image_info = j2k->image_info;
1810 if (image_info && cp->index_on) {
1811 image_info->index_on = cp->index_on;
1812 image_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
1813 image_info->image_w = image->x1 - image->x0;
1814 image_info->image_h = image->y1 - image->y0;
1815 image_info->prog = (&cp->tcps[0])->prg;
1816 image_info->tw = cp->tw;
1817 image_info->th = cp->th;
1818 image_info->tile_x = cp->tdx; /* new version parser */
1819 image_info->tile_y = cp->tdy; /* new version parser */
1820 image_info->tile_Ox = cp->tx0; /* new version parser */
1821 image_info->tile_Oy = cp->ty0; /* new version parser */
1822 image_info->comp = image->numcomps;
1823 image_info->layer = (&cp->tcps[0])->numlayers;
1824 image_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1;
1825 image_info->D_max = 0; /* ADD Marcela */
1833 for (compno = 0; compno < image->numcomps; compno++) {
1834 opj_tcp_t *tcp = &cp->tcps[0];
1835 if (tcp->tccps[compno].roishift)
1836 j2k_write_rgn(j2k, compno, 0);
1838 if (cp->comment != NULL) {
1842 if(image_info && image_info->index_on) {
1843 image_info->main_head_end = cio_tell(cio) - 1;
1847 /* create the tile encoder */
1848 tcd = tcd_create(j2k->cinfo);
1850 /* encode each tile */
1852 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1853 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
1855 j2k->curtileno = tileno;
1857 /* initialisation before tile encoding */
1859 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
1861 tcd_init_encode(tcd, image, cp, j2k->curtileno);
1865 if(image_info && image_info->index_on) {
1866 image_info->tile[j2k->curtileno].num_tile = j2k->curtileno;
1867 image_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
1873 for (compno = 1; compno < image->numcomps; compno++) {
1874 j2k_write_coc(j2k, compno);
1875 j2k_write_qcc(j2k, compno);
1877 if (cp->tcps[tileno].numpocs) {
1881 j2k_write_sod(j2k, tcd);
1884 if(image_info && image_info->index_on) {
1885 image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
1891 if (tile->PPT) { // BAD PPT !!!
1894 PPT_file=fopen("PPT","rb");
1895 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
1896 for (i=0;i<tile->len_ppt;i++) {
1898 fread(&elmt, 1, 1, PPT_file);
1899 fwrite(&elmt,1,1,f);
1908 /* destroy the tile encoder */
1909 tcd_free_encode(tcd);
1914 /* Creation of the index file */
1915 if(image_info && image_info->index_on) {
1916 if(!j2k_create_index(j2k, cio, image_info, index)) {
1917 opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to create index file %s\n", index);