2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2010-2011, Kaori Hagihara
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include "opj_includes.h"
42 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
45 /** @name Local static functions */
49 Write the SOC marker (Start Of Codestream)
52 static void j2k_write_soc(opj_j2k_t *j2k);
54 Read the SOC marker (Start of Codestream)
57 static void j2k_read_soc(opj_j2k_t *j2k);
59 Write the SIZ marker (image and tile size)
62 static void j2k_write_siz(opj_j2k_t *j2k);
64 Read the SIZ marker (image and tile size)
67 static void j2k_read_siz(opj_j2k_t *j2k);
69 Write the COM marker (comment)
72 static void j2k_write_com(opj_j2k_t *j2k);
74 Read the COM marker (comment)
77 static void j2k_read_com(opj_j2k_t *j2k);
79 Write the value concerning the specified component in the marker COD and COC
81 @param compno Number of the component concerned by the information written
83 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
85 Read the value concerning the specified component in the marker COD and COC
87 @param compno Number of the component concerned by the information read
89 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
91 Write the COD marker (coding style default)
94 static void j2k_write_cod(opj_j2k_t *j2k);
96 Read the COD marker (coding style default)
99 static void j2k_read_cod(opj_j2k_t *j2k);
101 Write the COC marker (coding style component)
102 @param j2k J2K handle
103 @param compno Number of the component concerned by the information written
105 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
107 Read the COC marker (coding style component)
108 @param j2k J2K handle
110 static void j2k_read_coc(opj_j2k_t *j2k);
112 Write the value concerning the specified component in the marker QCD and QCC
113 @param j2k J2K handle
114 @param compno Number of the component concerned by the information written
116 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
118 Read the value concerning the specified component in the marker QCD and QCC
119 @param j2k J2K handle
120 @param compno Number of the component concern by the information read
121 @param len Length of the information in the QCX part of the marker QCD/QCC
123 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
125 Write the QCD marker (quantization default)
126 @param j2k J2K handle
128 static void j2k_write_qcd(opj_j2k_t *j2k);
130 Read the QCD marker (quantization default)
131 @param j2k J2K handle
133 static void j2k_read_qcd(opj_j2k_t *j2k);
135 Write the QCC marker (quantization component)
136 @param j2k J2K handle
137 @param compno Number of the component concerned by the information written
139 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
141 Read the QCC marker (quantization component)
142 @param j2k J2K handle
144 static void j2k_read_qcc(opj_j2k_t *j2k);
146 Write the POC marker (progression order change)
147 @param j2k J2K handle
149 static void j2k_write_poc(opj_j2k_t *j2k);
151 Read the POC marker (progression order change)
152 @param j2k J2K handle
154 static void j2k_read_poc(opj_j2k_t *j2k);
156 Read the CRG marker (component registration)
157 @param j2k J2K handle
159 static void j2k_read_crg(opj_j2k_t *j2k);
161 Read the TLM marker (tile-part lengths)
162 @param j2k J2K handle
164 static void j2k_read_tlm(opj_j2k_t *j2k);
166 Read the PLM marker (packet length, main header)
167 @param j2k J2K handle
169 static void j2k_read_plm(opj_j2k_t *j2k);
171 Read the PLT marker (packet length, tile-part header)
172 @param j2k J2K handle
174 static void j2k_read_plt(opj_j2k_t *j2k);
176 Read the PPM marker (packet packet headers, main header)
177 @param j2k J2K handle
179 static void j2k_read_ppm(opj_j2k_t *j2k);
181 Read the PPT marker (packet packet headers, tile-part header)
182 @param j2k J2K handle
184 static void j2k_read_ppt(opj_j2k_t *j2k);
186 Write the TLM marker (Mainheader)
187 @param j2k J2K handle
189 static void j2k_write_tlm(opj_j2k_t *j2k);
191 Write the SOT marker (start of tile-part)
192 @param j2k J2K handle
194 static void j2k_write_sot(opj_j2k_t *j2k);
196 Read the SOT marker (start of tile-part)
197 @param j2k J2K handle
199 static void j2k_read_sot(opj_j2k_t *j2k);
201 Write the SOD marker (start of data)
202 @param j2k J2K handle
203 @param tile_coder Pointer to a TCD handle
205 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
207 Read the SOD marker (start of data)
208 @param j2k J2K handle
210 static void j2k_read_sod(opj_j2k_t *j2k);
212 Write the RGN marker (region-of-interest)
213 @param j2k J2K handle
214 @param compno Number of the component concerned by the information written
215 @param tileno Number of the tile concerned by the information written
217 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
219 Read the RGN marker (region-of-interest)
220 @param j2k J2K handle
222 static void j2k_read_rgn(opj_j2k_t *j2k);
224 Write the EOC marker (end of codestream)
225 @param j2k J2K handle
227 static void j2k_write_eoc(opj_j2k_t *j2k);
229 Read the EOC marker (end of codestream)
230 @param j2k J2K handle
232 static void j2k_read_eoc(opj_j2k_t *j2k);
234 Read an unknown marker
235 @param j2k J2K handle
237 static void j2k_read_unk(opj_j2k_t *j2k);
239 Add main header marker information
240 @param cstr_info Codestream information structure
241 @param type marker type
242 @param pos byte offset of marker segment
243 @param len length of marker segment
245 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info,
246 unsigned short int type, int pos, int len);
248 Add tile header marker information
249 @param tileno tile index number
250 @param cstr_info Codestream information structure
251 @param type marker type
252 @param pos byte offset of marker segment
253 @param len length of marker segment
255 static void j2k_add_tlmarker(int tileno, opj_codestream_info_t *cstr_info,
256 unsigned short int type, int pos, int len);
262 /* ----------------------------------------------------------------------- */
263 typedef struct j2k_prog_order {
264 OPJ_PROG_ORDER enum_prog;
268 j2k_prog_order_t j2k_prog_order_list[] = {
274 {(OPJ_PROG_ORDER) - 1, ""}
277 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)
279 j2k_prog_order_t *po;
280 for (po = j2k_prog_order_list; po->enum_prog != -1; po++) {
281 if (po->enum_prog == prg_order) {
288 /* ----------------------------------------------------------------------- */
289 static int j2k_get_num_tp(opj_cp_t *cp, int pino, int tileno)
293 int tpnum = 1, tpend = 0;
294 opj_tcp_t *tcp = &cp->tcps[tileno];
295 prog = j2k_convert_progression_order(tcp->prg);
297 if (cp->tp_on == 1) {
298 for (i = 0; i < 4; i++) {
300 if (cp->tp_flag == prog[i]) {
306 tpnum = tpnum * tcp->pocs[pino].compE;
309 tpnum = tpnum * tcp->pocs[pino].resE;
312 tpnum = tpnum * tcp->pocs[pino].prcE;
315 tpnum = tpnum * tcp->pocs[pino].layE;
326 /** mem allocation for TLM marker*/
327 static int j2k_calculate_tp(opj_cp_t *cp, int img_numcomp, opj_image_t *image,
330 int pino, tileno, totnum_tp = 0;
332 OPJ_ARG_NOT_USED(img_numcomp);
334 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
335 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
336 int cur_totnum_tp = 0;
337 opj_tcp_t *tcp = &cp->tcps[tileno];
338 for (pino = 0; pino <= tcp->numpocs; pino++) {
340 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno, FINAL_PASS);
344 tp_num = j2k_get_num_tp(cp, pino, tileno);
345 totnum_tp = totnum_tp + tp_num;
346 cur_totnum_tp = cur_totnum_tp + tp_num;
347 pi_destroy(pi, cp, tileno);
349 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
351 if (j2k->cstr_info) {
352 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
353 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(
354 cur_totnum_tp * sizeof(opj_tp_info_t));
361 static void j2k_write_soc(opj_j2k_t *j2k)
363 opj_cio_t *cio = j2k->cio;
364 cio_write(cio, J2K_MS_SOC, 2);
366 if (j2k->cstr_info) {
367 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
373 /* update markers struct */
374 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
375 #endif /* USE_JPWL */
379 static void j2k_read_soc(opj_j2k_t *j2k)
381 j2k->state = J2K_STATE_MHSIZ;
383 if (j2k->cstr_info) {
384 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
385 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 -
386 j2k->cstr_info->main_head_start;
390 static void j2k_write_siz(opj_j2k_t *j2k)
395 opj_cio_t *cio = j2k->cio;
396 opj_image_t *image = j2k->image;
397 opj_cp_t *cp = j2k->cp;
399 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
400 lenp = cio_tell(cio);
402 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
403 cio_write(cio, image->x1, 4); /* Xsiz */
404 cio_write(cio, image->y1, 4); /* Ysiz */
405 cio_write(cio, image->x0, 4); /* X0siz */
406 cio_write(cio, image->y0, 4); /* Y0siz */
407 cio_write(cio, cp->tdx, 4); /* XTsiz */
408 cio_write(cio, cp->tdy, 4); /* YTsiz */
409 cio_write(cio, cp->tx0, 4); /* XT0siz */
410 cio_write(cio, cp->ty0, 4); /* YT0siz */
411 cio_write(cio, image->numcomps, 2); /* Csiz */
412 for (i = 0; i < image->numcomps; i++) {
413 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7),
415 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
416 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
418 len = cio_tell(cio) - lenp;
420 cio_write(cio, len, 2); /* Lsiz */
421 cio_seek(cio, lenp + len);
423 if (j2k->cstr_info) {
424 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
428 static void j2k_read_siz(opj_j2k_t *j2k)
432 opj_cio_t *cio = j2k->cio;
433 opj_image_t *image = j2k->image;
434 opj_cp_t *cp = j2k->cp;
436 len = cio_read(cio, 2); /* Lsiz */
437 cio_read(cio, 2); /* Rsiz (capabilities) */
438 image->x1 = cio_read(cio, 4); /* Xsiz */
439 image->y1 = cio_read(cio, 4); /* Ysiz */
440 image->x0 = cio_read(cio, 4); /* X0siz */
441 image->y0 = cio_read(cio, 4); /* Y0siz */
442 cp->tdx = cio_read(cio, 4); /* XTsiz */
443 cp->tdy = cio_read(cio, 4); /* YTsiz */
444 cp->tx0 = cio_read(cio, 4); /* XT0siz */
445 cp->ty0 = cio_read(cio, 4); /* YT0siz */
447 if ((image->x0 < 0) || (image->x1 < 0) || (image->y0 < 0) || (image->y1 < 0)) {
448 opj_event_msg(j2k->cinfo, EVT_ERROR,
449 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
450 image->x0, image->x1, image->y0, image->y1);
454 image->numcomps = cio_read(cio, 2); /* Csiz */
457 if (j2k->cp->correct) {
458 /* if JPWL is on, we check whether TX errors have damaged
459 too much the SIZ parameters */
460 if (!(image->x1 * image->y1)) {
461 opj_event_msg(j2k->cinfo, EVT_ERROR,
462 "JPWL: bad image size (%d x %d)\n",
463 image->x1, image->y1);
464 if (!JPWL_ASSUME || JPWL_ASSUME) {
465 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
469 if (image->numcomps != ((len - 38) / 3)) {
470 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
471 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
472 image->numcomps, ((len - 38) / 3));
474 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
477 /* we try to correct */
478 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
479 if (image->numcomps < ((len - 38) / 3)) {
480 len = 38 + 3 * image->numcomps;
481 opj_event_msg(j2k->cinfo, EVT_WARNING,
482 "- setting Lsiz to %d => HYPOTHESIS!!!\n",
485 image->numcomps = ((len - 38) / 3);
486 opj_event_msg(j2k->cinfo, EVT_WARNING,
487 "- setting Csiz to %d => HYPOTHESIS!!!\n",
492 /* update components number in the jpwl_exp_comps filed */
493 cp->exp_comps = image->numcomps;
495 #endif /* USE_JPWL */
497 /* prevent division by zero */
498 if (!(cp->tdx * cp->tdy)) {
499 opj_event_msg(j2k->cinfo, EVT_ERROR,
500 "JPWL: invalid tile size (tdx: %d, tdy: %d)\n", cp->tdx, cp->tdy);
504 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps,
505 sizeof(opj_image_comp_t));
506 for (i = 0; i < image->numcomps; i++) {
508 tmp = cio_read(cio, 1); /* Ssiz_i */
509 image->comps[i].prec = (tmp & 0x7f) + 1;
510 image->comps[i].sgnd = tmp >> 7;
511 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
512 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
515 if (j2k->cp->correct) {
516 /* if JPWL is on, we check whether TX errors have damaged
517 too much the SIZ parameters, again */
518 if (!(image->comps[i].dx * image->comps[i].dy)) {
519 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
520 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
521 i, i, image->comps[i].dx, image->comps[i].dy);
523 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
526 /* we try to correct */
527 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
528 if (!image->comps[i].dx) {
529 image->comps[i].dx = 1;
530 opj_event_msg(j2k->cinfo, EVT_WARNING,
531 "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
532 i, image->comps[i].dx);
534 if (!image->comps[i].dy) {
535 image->comps[i].dy = 1;
536 opj_event_msg(j2k->cinfo, EVT_WARNING,
537 "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
538 i, image->comps[i].dy);
543 #endif /* USE_JPWL */
545 /* prevent division by zero */
546 if (!(image->comps[i].dx * image->comps[i].dy)) {
547 opj_event_msg(j2k->cinfo, EVT_ERROR,
548 "JPWL: invalid component size (dx: %d, dy: %d)\n", image->comps[i].dx,
554 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
555 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
557 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
558 image->comps[i].factor = cp->reduce; /* reducing factor per component */
561 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
562 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
565 if (j2k->cp->correct) {
566 /* if JPWL is on, we check whether TX errors have damaged
567 too much the SIZ parameters */
568 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) ||
569 (cp->th > cp->max_tiles)) {
570 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
571 "JPWL: bad number of tiles (%d x %d)\n",
574 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
577 /* we try to correct */
578 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
581 opj_event_msg(j2k->cinfo, EVT_WARNING,
582 "- setting %d tiles in x => HYPOTHESIS!!!\n",
585 if (cp->tw > cp->max_tiles) {
587 opj_event_msg(j2k->cinfo, EVT_WARNING,
588 "- too large x, increase expectance of %d\n"
589 "- setting %d tiles in x => HYPOTHESIS!!!\n",
590 cp->max_tiles, cp->tw);
594 opj_event_msg(j2k->cinfo, EVT_WARNING,
595 "- setting %d tiles in y => HYPOTHESIS!!!\n",
598 if (cp->th > cp->max_tiles) {
600 opj_event_msg(j2k->cinfo, EVT_WARNING,
601 "- too large y, increase expectance of %d to continue\n",
602 "- setting %d tiles in y => HYPOTHESIS!!!\n",
603 cp->max_tiles, cp->th);
607 #endif /* USE_JPWL */
609 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
610 if (cp->tcps == NULL) {
611 opj_event_msg(j2k->cinfo, EVT_ERROR, "Out of memory\n");
614 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
615 if (cp->tileno == NULL) {
616 opj_event_msg(j2k->cinfo, EVT_ERROR, "Out of memory\n");
622 if (j2k->cp->correct) {
624 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
625 "JPWL: could not alloc tcps field of cp\n");
626 if (!JPWL_ASSUME || JPWL_ASSUME) {
627 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
632 #endif /* USE_JPWL */
634 for (i = 0; i < cp->tw * cp->th; i++) {
636 cp->tcps[i].numpocs = 0;
637 cp->tcps[i].first = 1;
640 /* Initialization for PPM marker */
643 cp->ppm_data_first = NULL;
644 cp->ppm_previous = 0;
647 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps,
649 for (i = 0; i < cp->tw * cp->th; i++) {
650 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(
653 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th,
654 sizeof(unsigned char*));
655 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
656 j2k->state = J2K_STATE_MH;
659 if (j2k->cstr_info) {
660 opj_codestream_info_t *cstr_info = j2k->cstr_info;
661 cstr_info->image_w = image->x1 - image->x0;
662 cstr_info->image_h = image->y1 - image->y0;
663 cstr_info->numcomps = image->numcomps;
664 cstr_info->tw = cp->tw;
665 cstr_info->th = cp->th;
666 cstr_info->tile_x = cp->tdx;
667 cstr_info->tile_y = cp->tdy;
668 cstr_info->tile_Ox = cp->tx0;
669 cstr_info->tile_Oy = cp->ty0;
670 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th,
671 sizeof(opj_tile_info_t));
675 static void j2k_write_com(opj_j2k_t *j2k)
680 if (j2k->cp->comment) {
681 opj_cio_t *cio = j2k->cio;
682 char *comment = j2k->cp->comment;
684 cio_write(cio, J2K_MS_COM, 2);
685 lenp = cio_tell(cio);
687 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
688 for (i = 0; i < strlen(comment); i++) {
689 cio_write(cio, comment[i], 1);
691 len = cio_tell(cio) - lenp;
693 cio_write(cio, len, 2);
694 cio_seek(cio, lenp + len);
697 if (j2k->cstr_info) {
698 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
704 static void j2k_read_com(opj_j2k_t *j2k)
708 opj_cio_t *cio = j2k->cio;
710 len = cio_read(cio, 2);
711 cio_skip(cio, len - 2);
714 static void j2k_write_cox(opj_j2k_t *j2k, int compno)
718 opj_cp_t *cp = j2k->cp;
719 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
720 opj_tccp_t *tccp = &tcp->tccps[compno];
721 opj_cio_t *cio = j2k->cio;
723 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
724 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
725 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
726 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
727 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
729 if (tccp->csty & J2K_CCP_CSTY_PRT) {
730 for (i = 0; i < tccp->numresolutions; i++) {
731 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
736 static void j2k_read_cox(opj_j2k_t *j2k, int compno)
740 opj_cp_t *cp = j2k->cp;
741 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
743 opj_tccp_t *tccp = &tcp->tccps[compno];
744 opj_cio_t *cio = j2k->cio;
746 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
748 /* If user wants to remove more resolutions than the codestream contains, return error*/
749 if (cp->reduce >= tccp->numresolutions) {
750 opj_event_msg(j2k->cinfo, EVT_ERROR,
751 "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
752 "of resolutions of this component\nModify the cp_reduce parameter.\n\n",
754 j2k->state |= J2K_STATE_ERR;
756 if (tccp->numresolutions > J2K_MAXRLVLS) {
757 opj_event_msg(j2k->cinfo, EVT_ERROR,
758 "Error decoding component %d.\nThe number of resolutions is too big: %d vs max= %d. Truncating.\n\n",
759 compno, tccp->numresolutions, J2K_MAXRLVLS);
760 j2k->state |= J2K_STATE_ERR;
761 tccp->numresolutions = J2K_MAXRLVLS;
764 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
765 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
766 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
767 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
768 if (tccp->csty & J2K_CP_CSTY_PRT) {
769 for (i = 0; i < tccp->numresolutions; i++) {
770 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
771 tccp->prcw[i] = tmp & 0xf;
772 tccp->prch[i] = tmp >> 4;
777 if (j2k->cstr_info && compno == 0) {
778 for (i = 0; i < tccp->numresolutions; i++) {
779 if (tccp->csty & J2K_CP_CSTY_PRT) {
780 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
781 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
783 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
784 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
791 static void j2k_write_cod(opj_j2k_t *j2k)
794 opj_tcp_t *tcp = NULL;
797 opj_cio_t *cio = j2k->cio;
799 cio_write(cio, J2K_MS_COD, 2); /* COD */
801 lenp = cio_tell(cio);
805 tcp = &cp->tcps[j2k->curtileno];
807 cio_write(cio, tcp->csty, 1); /* Scod */
808 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
809 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
810 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
812 j2k_write_cox(j2k, 0);
813 len = cio_tell(cio) - lenp;
815 cio_write(cio, len, 2); /* Lcod */
816 cio_seek(cio, lenp + len);
818 if (j2k->cstr_info) {
819 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
824 static void j2k_read_cod(opj_j2k_t *j2k)
828 opj_cio_t *cio = j2k->cio;
829 opj_cp_t *cp = j2k->cp;
830 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
832 opj_image_t *image = j2k->image;
834 len = cio_read(cio, 2); /* Lcod */
835 tcp->csty = cio_read(cio, 1); /* Scod */
836 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
837 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
838 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
841 for (i = 0; i < image->numcomps; i++) {
842 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
844 j2k_read_cox(j2k, i);
848 if (j2k->cstr_info) {
849 opj_codestream_info_t *cstr_info = j2k->cstr_info;
850 cstr_info->prog = tcp->prg;
851 cstr_info->numlayers = tcp->numlayers;
852 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
853 for (i = 0; i < image->numcomps; i++) {
854 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
859 static void j2k_write_coc(opj_j2k_t *j2k, int compno)
863 opj_cp_t *cp = j2k->cp;
864 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
865 opj_image_t *image = j2k->image;
866 opj_cio_t *cio = j2k->cio;
868 cio_write(cio, J2K_MS_COC, 2); /* COC */
869 lenp = cio_tell(cio);
871 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
872 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
873 j2k_write_cox(j2k, compno);
874 len = cio_tell(cio) - lenp;
876 cio_write(cio, len, 2); /* Lcoc */
877 cio_seek(cio, lenp + len);
880 static void j2k_read_coc(opj_j2k_t *j2k)
884 opj_cp_t *cp = j2k->cp;
885 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
887 opj_image_t *image = j2k->image;
888 opj_cio_t *cio = j2k->cio;
890 len = cio_read(cio, 2); /* Lcoc */
891 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
892 if (compno >= image->numcomps) {
893 opj_event_msg(j2k->cinfo, EVT_ERROR,
894 "bad component number in COC (%d out of a maximum of %d)\n",
895 compno, image->numcomps);
898 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
899 j2k_read_cox(j2k, compno);
902 static void j2k_write_qcx(opj_j2k_t *j2k, int compno)
904 int bandno, numbands;
907 opj_cp_t *cp = j2k->cp;
908 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
909 opj_tccp_t *tccp = &tcp->tccps[compno];
910 opj_cio_t *cio = j2k->cio;
912 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
913 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 -
916 for (bandno = 0; bandno < numbands; bandno++) {
917 expn = tccp->stepsizes[bandno].expn;
918 mant = tccp->stepsizes[bandno].mant;
920 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
921 cio_write(cio, expn << 3, 1); /* SPqcx_i */
923 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
928 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len)
931 int bandno, numbands;
933 opj_cp_t *cp = j2k->cp;
934 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
936 opj_tccp_t *tccp = &tcp->tccps[compno];
937 opj_cio_t *cio = j2k->cio;
939 tmp = cio_read(cio, 1); /* Sqcx */
940 tccp->qntsty = tmp & 0x1f;
941 tccp->numgbits = tmp >> 5;
942 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
943 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
946 if (j2k->cp->correct) {
948 /* if JPWL is on, we check whether there are too many subbands */
949 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
950 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
951 "JPWL: bad number of subbands in Sqcx (%d)\n",
954 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
957 /* we try to correct */
959 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
960 "- setting number of bands to %d => HYPOTHESIS!!!\n",
967 /* We check whether there are too many subbands */
968 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
969 opj_event_msg(j2k->cinfo, EVT_WARNING,
970 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
971 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n",
972 numbands, J2K_MAXBANDS);
975 #endif /* USE_JPWL */
977 for (bandno = 0; bandno < numbands; bandno++) {
979 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
980 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
983 tmp = cio_read(cio, 2); /* SPqcx_i */
987 if (bandno < J2K_MAXBANDS) {
988 tccp->stepsizes[bandno].expn = expn;
989 tccp->stepsizes[bandno].mant = mant;
993 /* Add Antonin : if scalar_derived -> compute other stepsizes */
994 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
995 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
996 tccp->stepsizes[bandno].expn =
997 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
998 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
999 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1005 static void j2k_write_qcd(opj_j2k_t *j2k)
1009 opj_cio_t *cio = j2k->cio;
1011 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
1012 lenp = cio_tell(cio);
1014 j2k_write_qcx(j2k, 0);
1015 len = cio_tell(cio) - lenp;
1016 cio_seek(cio, lenp);
1017 cio_write(cio, len, 2); /* Lqcd */
1018 cio_seek(cio, lenp + len);
1020 if (j2k->cstr_info) {
1021 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
1025 static void j2k_read_qcd(opj_j2k_t *j2k)
1029 opj_cio_t *cio = j2k->cio;
1030 opj_image_t *image = j2k->image;
1032 len = cio_read(cio, 2); /* Lqcd */
1033 pos = cio_tell(cio);
1034 for (i = 0; i < image->numcomps; i++) {
1036 j2k_read_qcx(j2k, i, len - 2);
1040 static void j2k_write_qcc(opj_j2k_t *j2k, int compno)
1044 opj_cio_t *cio = j2k->cio;
1046 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
1047 lenp = cio_tell(cio);
1049 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
1050 j2k_write_qcx(j2k, compno);
1051 len = cio_tell(cio) - lenp;
1052 cio_seek(cio, lenp);
1053 cio_write(cio, len, 2); /* Lqcc */
1054 cio_seek(cio, lenp + len);
1057 static void j2k_read_qcc(opj_j2k_t *j2k)
1060 int numcomp = j2k->image->numcomps;
1061 opj_cio_t *cio = j2k->cio;
1063 len = cio_read(cio, 2); /* Lqcc */
1064 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1067 if (j2k->cp->correct) {
1069 static int backup_compno = 0;
1071 /* compno is negative or larger than the number of components!!! */
1072 if ((compno < 0) || (compno >= numcomp)) {
1073 opj_event_msg(j2k->cinfo, EVT_ERROR,
1074 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
1077 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1080 /* we try to correct */
1081 compno = backup_compno % numcomp;
1082 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1083 "- setting component number to %d\n",
1087 /* keep your private count of tiles */
1090 #endif /* USE_JPWL */
1092 if ((compno < 0) || (compno >= numcomp)) {
1093 opj_event_msg(j2k->cinfo, EVT_ERROR,
1094 "bad component number in QCC (%d out of a maximum of %d)\n",
1095 compno, j2k->image->numcomps);
1099 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1102 static void j2k_write_poc(opj_j2k_t *j2k)
1104 int len, numpchgs, i;
1106 int numcomps = j2k->image->numcomps;
1108 opj_cp_t *cp = j2k->cp;
1109 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1110 opj_tccp_t *tccp = &tcp->tccps[0];
1111 opj_cio_t *cio = j2k->cio;
1113 numpchgs = 1 + tcp->numpocs;
1114 cio_write(cio, J2K_MS_POC, 2); /* POC */
1115 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1116 cio_write(cio, len, 2); /* Lpoc */
1117 for (i = 0; i < numpchgs; i++) {
1118 opj_poc_t *poc = &tcp->pocs[i];
1119 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1120 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1121 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1122 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1123 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1124 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
1125 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1126 poc->compno1 = int_min(poc->compno1, numcomps);
1127 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1131 static void j2k_read_poc(opj_j2k_t *j2k)
1133 int len, numpchgs, i, old_poc;
1135 int numcomps = j2k->image->numcomps;
1137 opj_cp_t *cp = j2k->cp;
1138 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
1140 opj_cio_t *cio = j2k->cio;
1142 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1144 len = cio_read(cio, 2); /* Lpoc */
1145 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1147 for (i = old_poc; i < numpchgs + old_poc; i++) {
1149 poc = &tcp->pocs[i];
1150 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1151 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1152 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
1153 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
1154 poc->compno1 = int_min(
1155 cio_read(cio, numcomps <= 256 ? 1 : 2),
1156 (unsigned int) numcomps); /* CEpoc_i */
1157 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1160 tcp->numpocs = numpchgs + old_poc - 1;
1163 static void j2k_read_crg(opj_j2k_t *j2k)
1165 int len, i, Xcrg_i, Ycrg_i;
1167 opj_cio_t *cio = j2k->cio;
1168 int numcomps = j2k->image->numcomps;
1170 len = cio_read(cio, 2); /* Lcrg */
1171 for (i = 0; i < numcomps; i++) {
1172 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1173 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1177 static void j2k_read_tlm(opj_j2k_t *j2k)
1179 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1180 long int Ttlm_i, Ptlm_i;
1182 opj_cio_t *cio = j2k->cio;
1184 len = cio_read(cio, 2); /* Ltlm */
1185 Ztlm = cio_read(cio, 1); /* Ztlm */
1186 Stlm = cio_read(cio, 1); /* Stlm */
1187 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1188 SP = (Stlm >> 6) & 0x01;
1189 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1190 for (i = 0; i < tile_tlm; i++) {
1191 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1192 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1196 static void j2k_read_plm(opj_j2k_t *j2k)
1198 int len, i, Zplm, Nplm, add, packet_len = 0;
1200 opj_cio_t *cio = j2k->cio;
1202 len = cio_read(cio, 2); /* Lplm */
1203 Zplm = cio_read(cio, 1); /* Zplm */
1206 Nplm = cio_read(cio, 4); /* Nplm */
1208 for (i = Nplm; i > 0; i--) {
1209 add = cio_read(cio, 1);
1211 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1212 if ((add & 0x80) == 0) {
1223 static void j2k_read_plt(opj_j2k_t *j2k)
1225 int len, i, Zplt, packet_len = 0, add;
1227 opj_cio_t *cio = j2k->cio;
1229 len = cio_read(cio, 2); /* Lplt */
1230 Zplt = cio_read(cio, 1); /* Zplt */
1231 for (i = len - 3; i > 0; i--) {
1232 add = cio_read(cio, 1);
1233 packet_len = (packet_len << 7) + add; /* Iplt_i */
1234 if ((add & 0x80) == 0) {
1241 static void j2k_read_ppm(opj_j2k_t *j2k)
1243 int len, Z_ppm, i, j;
1246 opj_cp_t *cp = j2k->cp;
1247 opj_cio_t *cio = j2k->cio;
1249 len = cio_read(cio, 2);
1252 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1255 if (cp->ppm_previous == 0) {
1256 N_ppm = cio_read(cio, 4); /* N_ppm */
1259 N_ppm = cp->ppm_previous;
1262 if (Z_ppm == 0) { /* First PPM marker */
1263 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1264 cp->ppm_data_first = cp->ppm_data;
1265 cp->ppm_len = N_ppm;
1266 } else { /* NON-first PPM marker */
1267 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data,
1268 (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1271 /* this memory allocation check could be done even in non-JPWL cases */
1273 if (!cp->ppm_data) {
1274 opj_event_msg(j2k->cinfo, EVT_ERROR,
1275 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1277 if (!JPWL_ASSUME || JPWL_ASSUME) {
1278 opj_free(cp->ppm_data);
1279 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1286 cp->ppm_data_first = cp->ppm_data;
1287 cp->ppm_len = N_ppm + cp->ppm_store;
1289 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1290 cp->ppm_data[j] = cio_read(cio, 1);
1294 break; /* Case of non-finished packet header in present marker but finished in next one */
1297 cp->ppm_previous = i - 1;
1302 static void j2k_read_ppt(opj_j2k_t *j2k)
1304 int len, Z_ppt, i, j = 0;
1306 opj_cp_t *cp = j2k->cp;
1307 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1308 opj_cio_t *cio = j2k->cio;
1310 len = cio_read(cio, 2);
1311 Z_ppt = cio_read(cio, 1);
1313 if (Z_ppt == 0) { /* First PPT marker */
1314 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1315 tcp->ppt_data_first = tcp->ppt_data;
1317 tcp->ppt_len = len - 3;
1318 } else { /* NON-first PPT marker */
1319 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data,
1320 (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1321 tcp->ppt_data_first = tcp->ppt_data;
1322 tcp->ppt_len = len - 3 + tcp->ppt_store;
1325 for (i = len - 3; i > 0; i--) {
1326 tcp->ppt_data[j] = cio_read(cio, 1);
1332 static void j2k_write_tlm(opj_j2k_t *j2k)
1335 opj_cio_t *cio = j2k->cio;
1336 j2k->tlm_start = cio_tell(cio);
1337 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1338 lenp = 4 + (5 * j2k->totnum_tp);
1339 cio_write(cio, lenp, 2); /* Ltlm */
1340 cio_write(cio, 0, 1); /* Ztlm=0*/
1342 1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1343 cio_skip(cio, 5 * j2k->totnum_tp);
1346 static void j2k_write_sot(opj_j2k_t *j2k)
1350 opj_cio_t *cio = j2k->cio;
1352 j2k->sot_start = cio_tell(cio);
1353 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1354 lenp = cio_tell(cio);
1355 cio_skip(cio, 2); /* Lsot (further) */
1356 cio_write(cio, j2k->curtileno, 2); /* Isot */
1357 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1358 cio_write(cio, j2k->cur_tp_num, 1); /* TPsot */
1359 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1360 len = cio_tell(cio) - lenp;
1361 cio_seek(cio, lenp);
1362 cio_write(cio, len, 2); /* Lsot */
1363 cio_seek(cio, lenp + len);
1367 /* update markers struct */
1368 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1369 #endif /* USE_JPWL */
1372 if (j2k->cstr_info && j2k->cur_tp_num == 0) {
1373 j2k_add_tlmarker(j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
1377 static void j2k_read_sot(opj_j2k_t *j2k)
1379 int len, tileno, totlen, partno, numparts, i;
1380 opj_tcp_t *tcp = NULL;
1383 opj_cp_t *cp = j2k->cp;
1384 opj_cio_t *cio = j2k->cio;
1386 len = cio_read(cio, 2);
1387 tileno = cio_read(cio, 2);
1390 if (j2k->cp->correct) {
1392 static int backup_tileno = 0;
1394 /* tileno is negative or larger than the number of tiles!!! */
1395 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1396 opj_event_msg(j2k->cinfo, EVT_ERROR,
1397 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1398 tileno, (cp->tw * cp->th));
1400 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1403 /* we try to correct */
1404 tileno = backup_tileno;
1405 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1406 "- setting tile number to %d\n",
1410 /* keep your private count of tiles */
1413 #endif /* USE_JPWL */
1415 /* tileno is negative or larger than the number of tiles!!! */
1416 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1417 opj_event_msg(j2k->cinfo, EVT_ERROR,
1418 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1419 tileno, (cp->tw * cp->th));
1424 if (cp->tileno_size == 0) {
1425 cp->tileno[cp->tileno_size] = tileno;
1429 while (i < cp->tileno_size && status == 0) {
1430 status = cp->tileno[i] == tileno ? 1 : 0;
1434 cp->tileno[cp->tileno_size] = tileno;
1439 totlen = cio_read(cio, 4);
1442 if (j2k->cp->correct) {
1444 /* totlen is negative or larger than the bytes left!!! */
1445 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1446 opj_event_msg(j2k->cinfo, EVT_ERROR,
1447 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1448 totlen, cio_numbytesleft(cio) + 8);
1450 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1453 /* we try to correct */
1455 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1456 "- setting Psot to %d => assuming it is the last tile\n",
1461 #endif /* USE_JPWL */
1463 /* totlen is negative or larger than the bytes left!!! */
1464 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1465 opj_event_msg(j2k->cinfo, EVT_ERROR,
1466 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1467 totlen, cio_numbytesleft(cio) + 8);
1473 totlen = cio_numbytesleft(cio) + 8;
1476 partno = cio_read(cio, 1);
1477 numparts = cio_read(cio, 1);
1479 if (partno >= numparts) {
1480 opj_event_msg(j2k->cinfo, EVT_WARNING,
1481 "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n",
1482 tileno, partno, numparts);
1483 numparts = partno + 1;
1486 j2k->curtileno = tileno;
1487 j2k->cur_tp_num = partno;
1488 j2k->eot = cio_getbp(cio) - 12 + totlen;
1489 j2k->state = J2K_STATE_TPH;
1490 tcp = &cp->tcps[j2k->curtileno];
1493 if (j2k->cstr_info) {
1496 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1498 j2k->cstr_info->tile[tileno].tileno = tileno;
1499 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1500 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos +
1503 j2k->cstr_info->tile[tileno].end_pos += totlen;
1505 j2k->cstr_info->tile[tileno].num_tps = numparts;
1507 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(
1508 j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
1510 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(
1511 j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
1513 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1514 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1515 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1518 if (tcp->first == 1) {
1519 /* Initialization PPT */
1520 opj_tccp_t *tmp = tcp->tccps;
1521 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1523 tcp->ppt_data = NULL;
1524 tcp->ppt_data_first = NULL;
1527 for (i = 0; i < j2k->image->numcomps; i++) {
1528 tcp->tccps[i] = j2k->default_tcp->tccps[i];
1530 cp->tcps[j2k->curtileno].first = 0;
1534 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder)
1538 opj_tcp_t *tcp = NULL;
1539 opj_codestream_info_t *cstr_info = NULL;
1541 opj_tcd_t *tcd = (opj_tcd_t*)
1542 tile_coder; /* cast is needed because of conflicts in header inclusions */
1543 opj_cp_t *cp = j2k->cp;
1544 opj_cio_t *cio = j2k->cio;
1546 tcd->tp_num = j2k->tp_num ;
1547 tcd->cur_tp_num = j2k->cur_tp_num;
1549 cio_write(cio, J2K_MS_SOD, 2);
1551 if (j2k->cstr_info && j2k->cur_tp_num == 0) {
1552 j2k_add_tlmarker(j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
1555 if (j2k->curtileno == 0) {
1556 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1560 cstr_info = j2k->cstr_info;
1562 if (!j2k->cur_tp_num) {
1563 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction
1565 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1567 if (cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos <
1569 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(
1575 /* update markers struct */
1576 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1577 #endif /* USE_JPWL */
1582 tcp = &cp->tcps[j2k->curtileno];
1583 for (layno = 0; layno < tcp->numlayers; layno++) {
1584 if (tcp->rates[layno] > (j2k->sod_start / (cp->th * cp->tw))) {
1585 tcp->rates[layno] -= (j2k->sod_start / (cp->th * cp->tw));
1586 } else if (tcp->rates[layno]) {
1587 tcp->rates[layno] = 1;
1590 if (j2k->cur_tp_num == 0) {
1591 tcd->tcd_image->tiles->packno = 0;
1593 cstr_info->packno = 0;
1597 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio),
1598 cio_numbytesleft(cio) - 2, cstr_info);
1600 /* Writing Psot in SOT marker */
1601 totlen = cio_tell(cio) + l - j2k->sot_start;
1602 cio_seek(cio, j2k->sot_start + 6);
1603 cio_write(cio, totlen, 4);
1604 cio_seek(cio, j2k->sot_start + totlen);
1605 /* Writing Ttlm and Ptlm in TLM marker */
1607 cio_seek(cio, j2k->tlm_start + 6 + (5 * j2k->cur_tp_num));
1608 cio_write(cio, j2k->curtileno, 1);
1609 cio_write(cio, totlen, 4);
1611 cio_seek(cio, j2k->sot_start + totlen);
1614 static void j2k_read_sod(opj_j2k_t *j2k)
1616 int len, truncate = 0, i;
1617 unsigned char *data = NULL, *data_ptr = NULL;
1619 opj_cio_t *cio = j2k->cio;
1620 int curtileno = j2k->curtileno;
1623 if (j2k->cstr_info) {
1624 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1625 cio_tell(cio) + j2k->pos_correction - 1;
1626 if (j2k->cur_tp_num == 0) {
1627 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(
1628 cio) + j2k->pos_correction - 1;
1630 j2k->cstr_info->packno = 0;
1633 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1635 if (len == cio_numbytesleft(cio) + 1) {
1636 truncate = 1; /* Case of a truncate codestream */
1639 data = j2k->tile_data[curtileno];
1640 data = (unsigned char*) opj_realloc(data,
1641 (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1643 data_ptr = data + j2k->tile_len[curtileno];
1644 for (i = 0; i < len; i++) {
1645 data_ptr[i] = cio_read(cio, 1);
1648 j2k->tile_len[curtileno] += len;
1649 j2k->tile_data[curtileno] = data;
1652 j2k->state = J2K_STATE_TPHSOT;
1654 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1659 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno)
1661 opj_cp_t *cp = j2k->cp;
1662 opj_tcp_t *tcp = &cp->tcps[tileno];
1663 opj_cio_t *cio = j2k->cio;
1664 int numcomps = j2k->image->numcomps;
1666 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1667 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1668 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1669 cio_write(cio, 0, 1); /* Srgn */
1670 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1673 static void j2k_read_rgn(opj_j2k_t *j2k)
1675 int len, compno, roisty;
1677 opj_cp_t *cp = j2k->cp;
1678 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] :
1680 opj_cio_t *cio = j2k->cio;
1681 int numcomps = j2k->image->numcomps;
1683 len = cio_read(cio, 2); /* Lrgn */
1684 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1685 roisty = cio_read(cio, 1); /* Srgn */
1688 if (j2k->cp->correct) {
1689 /* totlen is negative or larger than the bytes left!!! */
1690 if (compno >= numcomps) {
1691 opj_event_msg(j2k->cinfo, EVT_ERROR,
1692 "JPWL: bad component number in RGN (%d when there are only %d)\n",
1694 if (!JPWL_ASSUME || JPWL_ASSUME) {
1695 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1700 #endif /* USE_JPWL */
1702 if (compno >= numcomps) {
1703 opj_event_msg(j2k->cinfo, EVT_ERROR,
1704 "bad component number in RGN (%d out of a maximum of %d)\n",
1705 compno, j2k->image->numcomps);
1709 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1712 static void j2k_write_eoc(opj_j2k_t *j2k)
1714 opj_cio_t *cio = j2k->cio;
1715 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1716 cio_write(cio, J2K_MS_EOC, 2);
1720 /* update markers struct */
1721 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1722 #endif /* USE_JPWL */
1726 static void j2k_read_eoc(opj_j2k_t *j2k)
1729 opj_bool success = OPJ_FALSE;
1731 /* if packets should be decoded */
1732 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1733 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1734 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1735 for (i = 0; i < j2k->cp->tileno_size; i++) {
1736 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1737 if (j2k->cp->tileno[i] != -1) {
1738 tileno = j2k->cp->tileno[i];
1739 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno],
1740 tileno, j2k->cstr_info);
1741 opj_free(j2k->tile_data[tileno]);
1742 j2k->tile_data[tileno] = NULL;
1743 tcd_free_decode_tile(tcd, i);
1745 success = OPJ_FALSE;
1747 if (success == OPJ_FALSE) {
1748 j2k->state |= J2K_STATE_ERR;
1752 tcd_free_decode(tcd);
1755 /* if packets should not be decoded */
1757 for (i = 0; i < j2k->cp->tileno_size; i++) {
1758 tileno = j2k->cp->tileno[i];
1759 opj_free(j2k->tile_data[tileno]);
1760 j2k->tile_data[tileno] = NULL;
1763 if (j2k->state & J2K_STATE_ERR) {
1764 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1766 j2k->state = J2K_STATE_MT;
1770 typedef struct opj_dec_mstabent {
1773 /** value of the state when the marker can appear */
1775 /** action linked to the marker */
1776 void (*handler)(opj_j2k_t *j2k);
1777 } opj_dec_mstabent_t;
1779 opj_dec_mstabent_t j2k_dec_mstab[] = {
1780 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1781 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1782 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1783 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1784 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1785 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1786 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1787 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1788 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1789 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1790 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1791 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1792 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1793 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1794 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1795 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1797 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1798 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1801 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1802 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1803 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1804 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1805 #endif /* USE_JPWL */
1807 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1808 {J2K_MS_INSEC, 0, j2k_read_insec},
1809 #endif /* USE_JPSEC */
1811 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1814 static void j2k_read_unk(opj_j2k_t *j2k)
1816 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1819 if (j2k->cp->correct) {
1821 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1822 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1823 id = cio_read(j2k->cio, 2);
1824 opj_event_msg(j2k->cinfo, EVT_ERROR,
1825 "JPWL: really don't know this marker %x\n",
1828 opj_event_msg(j2k->cinfo, EVT_ERROR,
1829 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
1832 /* OK, activate this at your own risk!!! */
1833 /* we look for the marker at the minimum hamming distance from this */
1834 while (j2k_dec_mstab[m].id) {
1836 /* 1's where they differ */
1837 tmp_id = j2k_dec_mstab[m].id ^ id;
1839 /* compute the hamming distance between our id and the current */
1841 for (i = 0; i < 16; i++) {
1842 if ((tmp_id >> i) & 0x0001) {
1847 /* if current distance is smaller, set the minimum */
1848 if (cur_dist < min_dist) {
1849 min_dist = cur_dist;
1850 min_id = j2k_dec_mstab[m].id;
1853 /* jump to the next marker */
1857 /* do we substitute the marker? */
1858 if (min_dist < JPWL_MAXIMUM_HAMMING) {
1859 opj_event_msg(j2k->cinfo, EVT_ERROR,
1860 "- marker %x is at distance %d from the read %x\n",
1861 min_id, min_dist, id);
1862 opj_event_msg(j2k->cinfo, EVT_ERROR,
1863 "- trying to substitute in place and crossing fingers!\n");
1864 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1865 cio_write(j2k->cio, min_id, 2);
1868 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1873 #endif /* USE_JPWL */
1878 Read the lookup table containing all the marker, status and action
1879 @param id Marker value
1881 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id)
1883 opj_dec_mstabent_t *e;
1884 for (e = j2k_dec_mstab; e->id != 0; e++) {
1892 /* ----------------------------------------------------------------------- */
1893 /* J2K / JPT decoder interface */
1894 /* ----------------------------------------------------------------------- */
1896 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo)
1898 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1903 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1904 if (!j2k->default_tcp) {
1910 j2k->tile_data = NULL;
1915 void j2k_destroy_decompress(opj_j2k_t *j2k)
1919 if (j2k->tile_len != NULL) {
1920 opj_free(j2k->tile_len);
1922 if (j2k->tile_data != NULL) {
1923 if (j2k->cp != NULL) {
1924 for (i = 0; i < j2k->cp->tileno_size; i++) {
1925 int tileno = j2k->cp->tileno[i];
1926 opj_free(j2k->tile_data[tileno]);
1927 j2k->tile_data[tileno] = NULL;
1931 opj_free(j2k->tile_data);
1933 if (j2k->default_tcp != NULL) {
1934 opj_tcp_t *default_tcp = j2k->default_tcp;
1935 if (default_tcp->ppt_data_first != NULL) {
1936 opj_free(default_tcp->ppt_data_first);
1938 if (j2k->default_tcp->tccps != NULL) {
1939 opj_free(j2k->default_tcp->tccps);
1941 opj_free(j2k->default_tcp);
1943 if (j2k->cp != NULL) {
1944 opj_cp_t *cp = j2k->cp;
1945 if (cp->tcps != NULL) {
1946 for (i = 0; i < cp->tw * cp->th; i++) {
1947 if (cp->tcps[i].ppt_data_first != NULL) {
1948 opj_free(cp->tcps[i].ppt_data_first);
1950 if (cp->tcps[i].tccps != NULL) {
1951 opj_free(cp->tcps[i].tccps);
1956 if (cp->ppm_data_first != NULL) {
1957 opj_free(cp->ppm_data_first);
1959 if (cp->tileno != NULL) {
1960 opj_free(cp->tileno);
1962 if (cp->comment != NULL) {
1963 opj_free(cp->comment);
1971 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
1973 if (j2k && parameters) {
1974 /* create and initialize the coding parameters structure */
1975 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1976 cp->reduce = parameters->cp_reduce;
1977 cp->layer = parameters->cp_layer;
1978 cp->limit_decoding = parameters->cp_limit_decoding;
1981 cp->correct = parameters->jpwl_correct;
1982 cp->exp_comps = parameters->jpwl_exp_comps;
1983 cp->max_tiles = parameters->jpwl_max_tiles;
1984 #endif /* USE_JPWL */
1987 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1992 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio,
1993 opj_codestream_info_t *cstr_info)
1995 opj_image_t *image = NULL;
1997 opj_common_ptr cinfo = j2k->cinfo;
2000 j2k->cstr_info = cstr_info;
2002 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
2005 /* create an empty image */
2006 image = opj_image_create0();
2009 j2k->state = J2K_STATE_MHSOC;
2012 opj_dec_mstabent_t *e;
2013 int id = cio_read(cio, 2);
2016 /* we try to honor JPWL correction power */
2017 if (j2k->cp->correct) {
2019 int orig_pos = cio_tell(cio);
2022 /* call the corrector */
2023 status = jpwl_correct(j2k);
2025 /* go back to where you were */
2026 cio_seek(cio, orig_pos - 2);
2028 /* re-read the marker */
2029 id = cio_read(cio, 2);
2031 /* check whether it begins with ff */
2032 if (id >> 8 != 0xff) {
2033 opj_event_msg(cinfo, EVT_ERROR,
2034 "JPWL: possible bad marker %x at %d\n",
2035 id, cio_tell(cio) - 2);
2037 opj_image_destroy(image);
2038 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
2041 /* we try to correct */
2043 cio_seek(cio, cio_tell(cio) - 2);
2044 cio_write(cio, id, 2);
2045 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
2046 "- setting marker to %x\n",
2051 #endif /* USE_JPWL */
2053 if (id >> 8 != 0xff) {
2054 if (cio_numbytesleft(cio) != 0) { /* not end of file reached and no EOC */
2055 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n",
2056 cio_tell(cio) - 2, id);
2057 opj_image_destroy(image);
2060 opj_event_msg(cinfo, EVT_WARNING, "%.8x: expected a marker instead of %x\n",
2061 cio_tell(cio) - 2, id);
2062 j2k->state = J2K_STATE_NEOC;
2065 e = j2k_dec_mstab_lookup(id);
2066 /* Check if the marker is known*/
2067 if (!(j2k->state & e->states)) {
2068 opj_image_destroy(image);
2069 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n",
2070 cio_tell(cio) - 2, id);
2073 /* Check if the decoding is limited to the main header*/
2074 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
2075 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
2082 if (j2k->state & J2K_STATE_ERR) {
2083 opj_image_destroy(image);
2087 if (j2k->state == J2K_STATE_MT) {
2090 if (j2k->state == J2K_STATE_NEOC) {
2094 if (j2k->state == J2K_STATE_NEOC) {
2098 if (j2k->state != J2K_STATE_MT) {
2099 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
2105 * Read a JPT-stream and decode file
2108 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio,
2109 opj_codestream_info_t *cstr_info)
2111 opj_image_t *image = NULL;
2112 opj_jpt_msg_header_t header;
2114 opj_common_ptr cinfo = j2k->cinfo;
2116 OPJ_ARG_NOT_USED(cstr_info);
2120 /* create an empty image */
2121 image = opj_image_create0();
2124 j2k->state = J2K_STATE_MHSOC;
2126 /* Initialize the header */
2127 jpt_init_msg_header(&header);
2128 /* Read the first header of the message */
2129 jpt_read_msg_header(cinfo, cio, &header);
2131 position = cio_tell(cio);
2132 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
2133 opj_image_destroy(image);
2134 opj_event_msg(cinfo, EVT_ERROR,
2135 "[JPT-stream] : Expecting Main header first [class_Id %d] !\n",
2141 opj_dec_mstabent_t *e = NULL;
2144 if (!cio_numbytesleft(cio)) {
2148 /* data-bin read -> need to read a new header */
2149 if ((unsigned int)(cio_tell(cio) - position) == header.Msg_length) {
2150 jpt_read_msg_header(cinfo, cio, &header);
2151 position = cio_tell(cio);
2152 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
2153 opj_image_destroy(image);
2154 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
2159 id = cio_read(cio, 2);
2160 if (id >> 8 != 0xff) {
2161 if (cio_numbytesleft(cio) != 0) { /* no end of file reached and no EOC */
2162 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n",
2163 cio_tell(cio) - 2, id);
2164 opj_image_destroy(image);
2167 opj_event_msg(cinfo, EVT_WARNING, "%.8x: expected a marker instead of %x\n",
2168 cio_tell(cio) - 2, id);
2169 j2k->state = J2K_STATE_NEOC;
2172 e = j2k_dec_mstab_lookup(id);
2173 if (!(j2k->state & e->states)) {
2174 opj_image_destroy(image);
2175 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n",
2176 cio_tell(cio) - 2, id);
2182 if (j2k->state == J2K_STATE_MT) {
2185 if (j2k->state == J2K_STATE_NEOC) {
2189 if (j2k->state == J2K_STATE_NEOC) {
2193 if (j2k->state != J2K_STATE_MT) {
2194 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
2200 /* ----------------------------------------------------------------------- */
2201 /* J2K encoder interface */
2202 /* ----------------------------------------------------------------------- */
2204 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo)
2206 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
2213 void j2k_destroy_compress(opj_j2k_t *j2k)
2220 if (j2k->cp != NULL) {
2221 opj_cp_t *cp = j2k->cp;
2224 opj_free(cp->comment);
2227 opj_free(cp->matrice);
2229 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2230 opj_free(cp->tcps[tileno].tccps);
2239 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters,
2242 int i, j, tileno, numpocs_tile;
2243 opj_cp_t *cp = NULL;
2245 if (!j2k || !parameters || ! image) {
2249 /* create and initialize the coding parameters structure */
2250 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
2252 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
2255 /* set default values for cp */
2260 copy user encoding parameters
2262 cp->cinema = parameters->cp_cinema;
2263 cp->max_comp_size = parameters->max_comp_size;
2264 cp->rsiz = parameters->cp_rsiz;
2265 cp->disto_alloc = parameters->cp_disto_alloc;
2266 cp->fixed_alloc = parameters->cp_fixed_alloc;
2267 cp->fixed_quality = parameters->cp_fixed_quality;
2269 /* mod fixed_quality */
2270 if (parameters->cp_matrice) {
2271 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 *
2273 cp->matrice = (int *) opj_malloc(array_size);
2274 memcpy(cp->matrice, parameters->cp_matrice, array_size);
2278 cp->tdx = parameters->cp_tdx;
2279 cp->tdy = parameters->cp_tdy;
2282 cp->tx0 = parameters->cp_tx0;
2283 cp->ty0 = parameters->cp_ty0;
2285 /* comment string */
2286 if (parameters->cp_comment) {
2287 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2289 strcpy(cp->comment, parameters->cp_comment);
2294 calculate other encoding parameters
2297 if (parameters->tile_size_on) {
2298 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2299 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2301 cp->tdx = image->x1 - cp->tx0;
2302 cp->tdy = image->y1 - cp->ty0;
2305 if (parameters->tp_on) {
2306 cp->tp_flag = parameters->tp_flag;
2311 for (i = 0; i < image->numcomps ; i++) {
2312 cp->img_size += (image->comps[i].w * image->comps[i].h * image->comps[i].prec);
2318 calculate JPWL encoding parameters
2321 if (parameters->jpwl_epc_on) {
2325 cp->epc_on = OPJ_TRUE;
2326 cp->info_on = OPJ_FALSE; /* no informative technique */
2329 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2330 cp->epb_on = OPJ_TRUE;
2332 cp->hprot_MH = parameters->jpwl_hprot_MH;
2333 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2334 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2335 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2337 /* if tile specs are not specified, copy MH specs */
2338 if (cp->hprot_TPH[0] == -1) {
2339 cp->hprot_TPH_tileno[0] = 0;
2340 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2342 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2343 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2344 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2345 cp->pprot[i] = parameters->jpwl_pprot[i];
2349 /* set ESD writing */
2350 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2351 cp->esd_on = OPJ_TRUE;
2353 cp->sens_size = parameters->jpwl_sens_size;
2354 cp->sens_addr = parameters->jpwl_sens_addr;
2355 cp->sens_range = parameters->jpwl_sens_range;
2357 cp->sens_MH = parameters->jpwl_sens_MH;
2358 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2359 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2360 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2364 /* always set RED writing to false: we are at the encoder */
2365 cp->red_on = OPJ_FALSE;
2368 cp->epc_on = OPJ_FALSE;
2370 #endif /* USE_JPWL */
2373 /* initialize the multiple tiles */
2374 /* ---------------------------- */
2375 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2377 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2378 opj_tcp_t *tcp = &cp->tcps[tileno];
2379 tcp->numlayers = parameters->tcp_numlayers;
2380 for (j = 0; j < tcp->numlayers; j++) {
2382 if (cp->fixed_quality) {
2383 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2385 tcp->rates[j] = parameters->tcp_rates[j];
2387 if (cp->fixed_quality) { /* add fixed_quality */
2388 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2390 tcp->rates[j] = parameters->tcp_rates[j];
2394 tcp->csty = parameters->csty;
2395 tcp->prg = parameters->prog_order;
2396 tcp->mct = parameters->tcp_mct;
2400 if (parameters->numpocs) {
2401 /* initialisation of POC */
2403 for (i = 0; i < parameters->numpocs; i++) {
2404 if ((tileno == parameters->POC[i].tile - 1) ||
2405 (parameters->POC[i].tile == -1)) {
2406 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2407 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2408 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2409 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2410 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2411 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2412 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2413 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2417 tcp->numpocs = numpocs_tile - 1 ;
2422 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2424 for (i = 0; i < image->numcomps; i++) {
2425 opj_tccp_t *tccp = &tcp->tccps[i];
2426 tccp->csty = parameters->csty &
2427 0x01; /* 0 => one precinct || 1 => custom precinct */
2428 tccp->numresolutions = parameters->numresolution;
2429 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2430 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2431 tccp->cblksty = parameters->mode;
2432 tccp->qmfbid = parameters->irreversible ? 0 : 1;
2433 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT :
2434 J2K_CCP_QNTSTY_NOQNT;
2436 if (i == parameters->roi_compno) {
2437 tccp->roishift = parameters->roi_shift;
2442 if (parameters->cp_cinema) {
2443 /*Precinct size for lowest frequency subband=128*/
2446 /*Precinct size at all other resolutions = 256*/
2447 for (j = 1; j < tccp->numresolutions; j++) {
2452 if (parameters->csty & J2K_CCP_CSTY_PRT) {
2454 for (j = tccp->numresolutions - 1; j >= 0; j--) {
2455 if (p < parameters->res_spec) {
2457 if (parameters->prcw_init[p] < 1) {
2460 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2463 if (parameters->prch_init[p] < 1) {
2466 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2470 int res_spec = parameters->res_spec;
2471 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2472 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2474 if (size_prcw < 1) {
2477 tccp->prcw[j] = int_floorlog2(size_prcw);
2480 if (size_prch < 1) {
2483 tccp->prch[j] = int_floorlog2(size_prch);
2487 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2490 for (j = 0; j < tccp->numresolutions; j++) {
2497 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2502 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image,
2503 opj_codestream_info_t *cstr_info)
2506 opj_cp_t *cp = NULL;
2508 opj_tcd_t *tcd = NULL; /* TCD component */
2516 j2k->cstr_info = cstr_info;
2519 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(
2521 cstr_info->image_w = image->x1 - image->x0;
2522 cstr_info->image_h = image->y1 - image->y0;
2523 cstr_info->prog = (&cp->tcps[0])->prg;
2524 cstr_info->tw = cp->tw;
2525 cstr_info->th = cp->th;
2526 cstr_info->tile_x = cp->tdx; /* new version parser */
2527 cstr_info->tile_y = cp->tdy; /* new version parser */
2528 cstr_info->tile_Ox = cp->tx0; /* new version parser */
2529 cstr_info->tile_Oy = cp->ty0; /* new version parser */
2530 cstr_info->numcomps = image->numcomps;
2531 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2532 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2533 for (compno = 0; compno < image->numcomps; compno++) {
2534 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2536 cstr_info->D_max = 0.0; /* ADD Marcela */
2537 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2538 cstr_info->maxmarknum = 100;
2539 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum *
2540 sizeof(opj_marker_info_t));
2541 cstr_info->marknum = 0;
2551 for (compno = 1; compno < image->numcomps; compno++) {
2552 j2k_write_coc(j2k, compno);
2553 j2k_write_qcc(j2k, compno);
2557 for (compno = 0; compno < image->numcomps; compno++) {
2558 opj_tcp_t *tcp = &cp->tcps[0];
2559 if (tcp->tccps[compno].roishift) {
2560 j2k_write_rgn(j2k, compno, 0);
2563 if (cp->comment != NULL) {
2567 j2k->totnum_tp = j2k_calculate_tp(cp, image->numcomps, image, j2k);
2571 if (cp->cinema == CINEMA4K_24) {
2576 /* uncomment only for testing JPSEC marker writing */
2577 /* j2k_write_sec(j2k); */
2581 cstr_info->main_head_end = cio_tell(cio) - 1;
2584 /**** Main Header ENDS here ***/
2586 /* create the tile encoder */
2587 tcd = tcd_create(j2k->cinfo);
2589 /* encode each tile */
2590 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2594 int acc_pack_num = 0;
2598 opj_tcp_t *tcp = &cp->tcps[tileno];
2599 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1,
2602 j2k->curtileno = tileno;
2603 j2k->cur_tp_num = 0;
2604 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2605 /* initialisation before tile encoding */
2607 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2609 tcd_init_encode(tcd, image, cp, j2k->curtileno);
2614 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2615 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
2616 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(
2617 cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
2618 cstr_info->tile[j2k->curtileno].marknum = 0;
2622 for (pino = 0; pino <= tcp->numpocs; pino++) {
2624 tcd->cur_pino = pino;
2626 /*Get number of tile parts*/
2627 tot_num_tp = j2k_get_num_tp(cp, pino, tileno);
2628 tcd->tp_pos = cp->tp_pos;
2630 for (tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++) {
2631 j2k->tp_num = tilepartno;
2634 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2635 cio_tell(cio) + j2k->pos_correction;
2639 if (j2k->cur_tp_num == 0 && cp->cinema == 0) {
2640 for (compno = 1; compno < image->numcomps; compno++) {
2641 j2k_write_coc(j2k, compno);
2642 j2k_write_qcc(j2k, compno);
2644 if (cp->tcps[tileno].numpocs) {
2651 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2652 cio_tell(cio) + j2k->pos_correction + 1;
2655 j2k_write_sod(j2k, tcd);
2659 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2660 cio_tell(cio) + j2k->pos_correction - 1;
2661 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2663 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2664 cstr_info->packno - acc_pack_num;
2665 acc_pack_num = cstr_info->packno;
2673 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction -
2679 if (tile->PPT) { // BAD PPT !!!
2682 PPT_file=fopen("PPT","rb");
2683 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2684 for (i=0;i<tile->len_ppt;i++) {
2686 fread(&elmt, 1, 1, PPT_file);
2687 fwrite(&elmt,1,1,f);
2696 /* destroy the tile encoder */
2697 tcd_free_encode(tcd);
2700 opj_free(j2k->cur_totnum_tp);
2705 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2707 /* The following adjustment is done to adjust the codestream size */
2708 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2709 /* the first bunch of bytes is not in the codestream */
2710 cstr_info->codestream_size -= cstr_info->main_head_start;
2716 preparation of JPWL marker segments
2720 /* encode according to JPWL */
2721 jpwl_encode(j2k, cio, image);
2724 #endif /* USE_JPWL */
2729 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info,
2730 unsigned short int type, int pos, int len)
2737 /* expand the list? */
2738 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
2739 cstr_info->maxmarknum = 100 + (int)((float) cstr_info->maxmarknum * 1.0F);
2740 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker,
2741 cstr_info->maxmarknum);
2744 /* add the marker */
2745 cstr_info->marker[cstr_info->marknum].type = type;
2746 cstr_info->marker[cstr_info->marknum].pos = pos;
2747 cstr_info->marker[cstr_info->marknum].len = len;
2748 cstr_info->marknum++;
2752 static void j2k_add_tlmarker(int tileno, opj_codestream_info_t *cstr_info,
2753 unsigned short int type, int pos, int len)
2756 opj_marker_info_t *marker;
2762 /* expand the list? */
2763 if ((cstr_info->tile[tileno].marknum + 1) >
2764 cstr_info->tile[tileno].maxmarknum) {
2765 cstr_info->tile[tileno].maxmarknum = 100 + (int)((float)
2766 cstr_info->tile[tileno].maxmarknum * 1.0F);
2767 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(
2768 cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
2771 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
2773 /* add the marker */
2774 marker->type = type;
2777 cstr_info->tile[tileno].marknum++;