2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
9 * Copyright (c) 2010-2011, Kaori Hagihara
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include "opj_includes.h"
36 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
39 /** @name Local static functions */
43 Write the SOC marker (Start Of Codestream)
46 static void j2k_write_soc(opj_j2k_t *j2k);
48 Read the SOC marker (Start of Codestream)
51 static void j2k_read_soc(opj_j2k_t *j2k);
53 Write the SIZ marker (image and tile size)
56 static void j2k_write_siz(opj_j2k_t *j2k);
58 Read the SIZ marker (image and tile size)
61 static void j2k_read_siz(opj_j2k_t *j2k);
63 Write the COM marker (comment)
66 static void j2k_write_com(opj_j2k_t *j2k);
68 Read the COM marker (comment)
71 static void j2k_read_com(opj_j2k_t *j2k);
73 Write the value concerning the specified component in the marker COD and COC
75 @param compno Number of the component concerned by the information written
77 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
79 Read the value concerning the specified component in the marker COD and COC
81 @param compno Number of the component concerned by the information read
83 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
85 Write the COD marker (coding style default)
88 static void j2k_write_cod(opj_j2k_t *j2k);
90 Read the COD marker (coding style default)
93 static void j2k_read_cod(opj_j2k_t *j2k);
95 Write the COC marker (coding style component)
97 @param compno Number of the component concerned by the information written
99 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
101 Read the COC marker (coding style component)
102 @param j2k J2K handle
104 static void j2k_read_coc(opj_j2k_t *j2k);
106 Write the value concerning the specified component in the marker QCD and QCC
107 @param j2k J2K handle
108 @param compno Number of the component concerned by the information written
110 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
112 Read the value concerning the specified component in the marker QCD and QCC
113 @param j2k J2K handle
114 @param compno Number of the component concern by the information read
115 @param len Length of the information in the QCX part of the marker QCD/QCC
117 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
119 Write the QCD marker (quantization default)
120 @param j2k J2K handle
122 static void j2k_write_qcd(opj_j2k_t *j2k);
124 Read the QCD marker (quantization default)
125 @param j2k J2K handle
127 static void j2k_read_qcd(opj_j2k_t *j2k);
129 Write the QCC marker (quantization component)
130 @param j2k J2K handle
131 @param compno Number of the component concerned by the information written
133 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
135 Read the QCC marker (quantization component)
136 @param j2k J2K handle
138 static void j2k_read_qcc(opj_j2k_t *j2k);
140 Write the POC marker (progression order change)
141 @param j2k J2K handle
143 static void j2k_write_poc(opj_j2k_t *j2k);
145 Read the POC marker (progression order change)
146 @param j2k J2K handle
148 static void j2k_read_poc(opj_j2k_t *j2k);
150 Read the CRG marker (component registration)
151 @param j2k J2K handle
153 static void j2k_read_crg(opj_j2k_t *j2k);
155 Read the TLM marker (tile-part lengths)
156 @param j2k J2K handle
158 static void j2k_read_tlm(opj_j2k_t *j2k);
160 Read the PLM marker (packet length, main header)
161 @param j2k J2K handle
163 static void j2k_read_plm(opj_j2k_t *j2k);
165 Read the PLT marker (packet length, tile-part header)
166 @param j2k J2K handle
168 static void j2k_read_plt(opj_j2k_t *j2k);
170 Read the PPM marker (packet packet headers, main header)
171 @param j2k J2K handle
173 static void j2k_read_ppm(opj_j2k_t *j2k);
175 Read the PPT marker (packet packet headers, tile-part header)
176 @param j2k J2K handle
178 static void j2k_read_ppt(opj_j2k_t *j2k);
180 Write the TLM marker (Mainheader)
181 @param j2k J2K handle
183 static void j2k_write_tlm(opj_j2k_t *j2k);
185 Write the SOT marker (start of tile-part)
186 @param j2k J2K handle
188 static void j2k_write_sot(opj_j2k_t *j2k);
190 Read the SOT marker (start of tile-part)
191 @param j2k J2K handle
193 static void j2k_read_sot(opj_j2k_t *j2k);
195 Write the SOD marker (start of data)
196 @param j2k J2K handle
197 @param tile_coder Pointer to a TCD handle
199 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
201 Read the SOD marker (start of data)
202 @param j2k J2K handle
204 static void j2k_read_sod(opj_j2k_t *j2k);
206 Write the RGN marker (region-of-interest)
207 @param j2k J2K handle
208 @param compno Number of the component concerned by the information written
209 @param tileno Number of the tile concerned by the information written
211 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
213 Read the RGN marker (region-of-interest)
214 @param j2k J2K handle
216 static void j2k_read_rgn(opj_j2k_t *j2k);
218 Write the EOC marker (end of codestream)
219 @param j2k J2K handle
221 static void j2k_write_eoc(opj_j2k_t *j2k);
223 Read the EOC marker (end of codestream)
224 @param j2k J2K handle
226 static void j2k_read_eoc(opj_j2k_t *j2k);
228 Read an unknown marker
229 @param j2k J2K handle
231 static void j2k_read_unk(opj_j2k_t *j2k);
233 Add main header marker information
234 @param cstr_info Codestream information structure
235 @param type marker type
236 @param pos byte offset of marker segment
237 @param len length of marker segment
239 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
241 Add tile header marker information
242 @param tileno tile index number
243 @param cstr_info Codestream information structure
244 @param type marker type
245 @param pos byte offset of marker segment
246 @param len length of marker segment
248 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
254 /* ----------------------------------------------------------------------- */
255 typedef struct j2k_prog_order{
256 OPJ_PROG_ORDER enum_prog;
260 j2k_prog_order_t j2k_prog_order_list[] = {
266 {(OPJ_PROG_ORDER)-1, ""}
269 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
270 j2k_prog_order_t *po;
271 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
272 if(po->enum_prog == prg_order){
279 /* ----------------------------------------------------------------------- */
280 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
284 opj_tcp_t *tcp = &cp->tcps[tileno];
285 prog = j2k_convert_progression_order(tcp->prg);
290 if( cp->tp_flag == prog[i] ){
291 tpend=1;cp->tp_pos=i;
295 tpnum= tpnum * tcp->pocs[pino].compE;
298 tpnum= tpnum * tcp->pocs[pino].resE;
301 tpnum= tpnum * tcp->pocs[pino].prcE;
304 tpnum= tpnum * tcp->pocs[pino].layE;
315 /** mem allocation for TLM marker*/
316 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
317 int pino,tileno,totnum_tp=0;
319 OPJ_ARG_NOT_USED(img_numcomp);
321 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
322 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
323 int cur_totnum_tp = 0;
324 opj_tcp_t *tcp = &cp->tcps[tileno];
325 for(pino = 0; pino <= tcp->numpocs; pino++) {
327 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
328 if(!pi) { return -1;}
329 tp_num = j2k_get_num_tp(cp,pino,tileno);
330 totnum_tp = totnum_tp + tp_num;
331 cur_totnum_tp = cur_totnum_tp + tp_num;
332 pi_destroy(pi, cp, tileno);
334 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
336 if (j2k->cstr_info) {
337 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
338 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
345 static void j2k_write_soc(opj_j2k_t *j2k) {
346 opj_cio_t *cio = j2k->cio;
347 cio_write(cio, J2K_MS_SOC, 2);
350 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
355 /* update markers struct */
356 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
357 #endif /* USE_JPWL */
361 static void j2k_read_soc(opj_j2k_t *j2k) {
362 j2k->state = J2K_STATE_MHSIZ;
364 if (j2k->cstr_info) {
365 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
366 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
370 static void j2k_write_siz(opj_j2k_t *j2k) {
374 opj_cio_t *cio = j2k->cio;
375 opj_image_t *image = j2k->image;
376 opj_cp_t *cp = j2k->cp;
378 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
379 lenp = cio_tell(cio);
381 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
382 cio_write(cio, image->x1, 4); /* Xsiz */
383 cio_write(cio, image->y1, 4); /* Ysiz */
384 cio_write(cio, image->x0, 4); /* X0siz */
385 cio_write(cio, image->y0, 4); /* Y0siz */
386 cio_write(cio, cp->tdx, 4); /* XTsiz */
387 cio_write(cio, cp->tdy, 4); /* YTsiz */
388 cio_write(cio, cp->tx0, 4); /* XT0siz */
389 cio_write(cio, cp->ty0, 4); /* YT0siz */
390 cio_write(cio, image->numcomps, 2); /* Csiz */
391 for (i = 0; i < image->numcomps; i++) {
392 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
393 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
394 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
396 len = cio_tell(cio) - lenp;
398 cio_write(cio, len, 2); /* Lsiz */
399 cio_seek(cio, lenp + len);
402 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
405 static void j2k_read_siz(opj_j2k_t *j2k) {
408 opj_cio_t *cio = j2k->cio;
409 opj_image_t *image = j2k->image;
410 opj_cp_t *cp = j2k->cp;
412 len = cio_read(cio, 2); /* Lsiz */
413 cio_read(cio, 2); /* Rsiz (capabilities) */
414 image->x1 = cio_read(cio, 4); /* Xsiz */
415 image->y1 = cio_read(cio, 4); /* Ysiz */
416 image->x0 = cio_read(cio, 4); /* X0siz */
417 image->y0 = cio_read(cio, 4); /* Y0siz */
418 cp->tdx = cio_read(cio, 4); /* XTsiz */
419 cp->tdy = cio_read(cio, 4); /* YTsiz */
420 cp->tx0 = cio_read(cio, 4); /* XT0siz */
421 cp->ty0 = cio_read(cio, 4); /* YT0siz */
423 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
424 opj_event_msg(j2k->cinfo, EVT_ERROR,
425 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
426 image->x0,image->x1,image->y0,image->y1);
430 image->numcomps = cio_read(cio, 2); /* Csiz */
433 if (j2k->cp->correct) {
434 /* if JPWL is on, we check whether TX errors have damaged
435 too much the SIZ parameters */
436 if (!(image->x1 * image->y1)) {
437 opj_event_msg(j2k->cinfo, EVT_ERROR,
438 "JPWL: bad image size (%d x %d)\n",
439 image->x1, image->y1);
440 if (!JPWL_ASSUME || JPWL_ASSUME) {
441 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
445 if (image->numcomps != ((len - 38) / 3)) {
446 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
447 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
448 image->numcomps, ((len - 38) / 3));
450 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
453 /* we try to correct */
454 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
455 if (image->numcomps < ((len - 38) / 3)) {
456 len = 38 + 3 * image->numcomps;
457 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
460 image->numcomps = ((len - 38) / 3);
461 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
466 /* update components number in the jpwl_exp_comps filed */
467 cp->exp_comps = image->numcomps;
471 #endif /* USE_JPWL */
473 /* prevent division by zero */
474 if (!(cp->tdx * cp->tdy)) {
475 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: invalid tile size (tdx: %d, tdy: %d)\n", cp->tdx, cp->tdy);
479 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
480 for (i = 0; i < image->numcomps; i++) {
482 tmp = cio_read(cio, 1); /* Ssiz_i */
483 image->comps[i].prec = (tmp & 0x7f) + 1;
484 image->comps[i].sgnd = tmp >> 7;
485 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
486 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
489 if (j2k->cp->correct) {
490 /* if JPWL is on, we check whether TX errors have damaged
491 too much the SIZ parameters, again */
492 if (!(image->comps[i].dx * image->comps[i].dy)) {
493 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
494 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
495 i, i, image->comps[i].dx, image->comps[i].dy);
497 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
500 /* we try to correct */
501 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
502 if (!image->comps[i].dx) {
503 image->comps[i].dx = 1;
504 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
505 i, image->comps[i].dx);
507 if (!image->comps[i].dy) {
508 image->comps[i].dy = 1;
509 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
510 i, image->comps[i].dy);
515 #endif /* USE_JPWL */
517 /* prevent division by zero */
518 if (!(image->comps[i].dx * image->comps[i].dy)) {
519 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: invalid component size (dx: %d, dy: %d)\n", image->comps[i].dx, image->comps[i].dy);
523 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
524 image->comps[i].factor = cp->reduce; /* reducing factor per component */
527 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
528 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
531 if (j2k->cp->correct) {
532 /* if JPWL is on, we check whether TX errors have damaged
533 too much the SIZ parameters */
534 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
535 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
536 "JPWL: bad number of tiles (%d x %d)\n",
539 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
542 /* we try to correct */
543 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
546 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
549 if (cp->tw > cp->max_tiles) {
551 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
552 "- setting %d tiles in x => HYPOTHESIS!!!\n",
553 cp->max_tiles, cp->tw);
557 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
560 if (cp->th > cp->max_tiles) {
562 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
563 "- setting %d tiles in y => HYPOTHESIS!!!\n",
564 cp->max_tiles, cp->th);
568 #endif /* USE_JPWL */
570 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
571 if (cp->tcps == NULL)
573 opj_event_msg(j2k->cinfo, EVT_ERROR, "Out of memory\n");
576 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
577 if (cp->tileno == NULL)
579 opj_event_msg(j2k->cinfo, EVT_ERROR, "Out of memory\n");
585 if (j2k->cp->correct) {
587 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
588 "JPWL: could not alloc tcps field of cp\n");
589 if (!JPWL_ASSUME || JPWL_ASSUME) {
590 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
595 #endif /* USE_JPWL */
597 for (i = 0; i < cp->tw * cp->th; i++) {
599 cp->tcps[i].numpocs = 0;
600 cp->tcps[i].first = 1;
603 /* Initialization for PPM marker */
606 cp->ppm_data_first = NULL;
607 cp->ppm_previous = 0;
610 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
611 for (i = 0; i < cp->tw * cp->th; i++) {
612 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
614 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
615 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
616 j2k->state = J2K_STATE_MH;
619 if (j2k->cstr_info) {
620 opj_codestream_info_t *cstr_info = j2k->cstr_info;
621 cstr_info->image_w = image->x1 - image->x0;
622 cstr_info->image_h = image->y1 - image->y0;
623 cstr_info->numcomps = image->numcomps;
624 cstr_info->tw = cp->tw;
625 cstr_info->th = cp->th;
626 cstr_info->tile_x = cp->tdx;
627 cstr_info->tile_y = cp->tdy;
628 cstr_info->tile_Ox = cp->tx0;
629 cstr_info->tile_Oy = cp->ty0;
630 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
634 static void j2k_write_com(opj_j2k_t *j2k) {
638 if(j2k->cp->comment) {
639 opj_cio_t *cio = j2k->cio;
640 char *comment = j2k->cp->comment;
642 cio_write(cio, J2K_MS_COM, 2);
643 lenp = cio_tell(cio);
645 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
646 for (i = 0; i < strlen(comment); i++) {
647 cio_write(cio, comment[i], 1);
649 len = cio_tell(cio) - lenp;
651 cio_write(cio, len, 2);
652 cio_seek(cio, lenp + len);
656 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
661 static void j2k_read_com(opj_j2k_t *j2k) {
664 opj_cio_t *cio = j2k->cio;
666 len = cio_read(cio, 2);
667 cio_skip(cio, len - 2);
670 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
673 opj_cp_t *cp = j2k->cp;
674 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
675 opj_tccp_t *tccp = &tcp->tccps[compno];
676 opj_cio_t *cio = j2k->cio;
678 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
679 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
680 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
681 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
682 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
684 if (tccp->csty & J2K_CCP_CSTY_PRT) {
685 for (i = 0; i < tccp->numresolutions; i++) {
686 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
691 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
694 opj_cp_t *cp = j2k->cp;
695 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
696 opj_tccp_t *tccp = &tcp->tccps[compno];
697 opj_cio_t *cio = j2k->cio;
699 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
701 /* If user wants to remove more resolutions than the codestream contains, return error*/
702 if (cp->reduce >= tccp->numresolutions) {
703 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
704 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
705 j2k->state |= J2K_STATE_ERR;
707 if( tccp->numresolutions > J2K_MAXRLVLS ) {
708 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions is too big: %d vs max= %d. Truncating.\n\n",
709 compno, tccp->numresolutions, J2K_MAXRLVLS);
710 j2k->state |= J2K_STATE_ERR;
711 tccp->numresolutions = J2K_MAXRLVLS;
714 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
715 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
716 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
717 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
718 if (tccp->csty & J2K_CP_CSTY_PRT) {
719 for (i = 0; i < tccp->numresolutions; i++) {
720 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
721 tccp->prcw[i] = tmp & 0xf;
722 tccp->prch[i] = tmp >> 4;
727 if(j2k->cstr_info && compno == 0) {
728 for (i = 0; i < tccp->numresolutions; i++) {
729 if (tccp->csty & J2K_CP_CSTY_PRT) {
730 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
731 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
734 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
735 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
742 static void j2k_write_cod(opj_j2k_t *j2k) {
744 opj_tcp_t *tcp = NULL;
747 opj_cio_t *cio = j2k->cio;
749 cio_write(cio, J2K_MS_COD, 2); /* COD */
751 lenp = cio_tell(cio);
755 tcp = &cp->tcps[j2k->curtileno];
757 cio_write(cio, tcp->csty, 1); /* Scod */
758 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
759 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
760 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
762 j2k_write_cox(j2k, 0);
763 len = cio_tell(cio) - lenp;
765 cio_write(cio, len, 2); /* Lcod */
766 cio_seek(cio, lenp + len);
769 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
773 static void j2k_read_cod(opj_j2k_t *j2k) {
776 opj_cio_t *cio = j2k->cio;
777 opj_cp_t *cp = j2k->cp;
778 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
779 opj_image_t *image = j2k->image;
781 len = cio_read(cio, 2); /* Lcod */
783 tcp->csty = cio_read(cio, 1); /* Scod */
784 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
785 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
786 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
789 for (i = 0; i < image->numcomps; i++) {
790 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
792 j2k_read_cox(j2k, i);
796 if (j2k->cstr_info) {
797 opj_codestream_info_t *cstr_info = j2k->cstr_info;
798 cstr_info->prog = tcp->prg;
799 cstr_info->numlayers = tcp->numlayers;
800 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
801 for (i = 0; i < image->numcomps; i++) {
802 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
807 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
810 opj_cp_t *cp = j2k->cp;
811 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
812 opj_image_t *image = j2k->image;
813 opj_cio_t *cio = j2k->cio;
815 cio_write(cio, J2K_MS_COC, 2); /* COC */
816 lenp = cio_tell(cio);
818 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
819 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
820 j2k_write_cox(j2k, compno);
821 len = cio_tell(cio) - lenp;
823 cio_write(cio, len, 2); /* Lcoc */
824 cio_seek(cio, lenp + len);
827 static void j2k_read_coc(opj_j2k_t *j2k) {
830 opj_cp_t *cp = j2k->cp;
831 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
832 opj_image_t *image = j2k->image;
833 opj_cio_t *cio = j2k->cio;
835 len = cio_read(cio, 2); /* Lcoc */
837 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
838 if (compno >= image->numcomps) {
839 opj_event_msg(j2k->cinfo, EVT_ERROR,
840 "bad component number in COC (%d out of a maximum of %d)\n",
841 compno, image->numcomps);
844 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
845 j2k_read_cox(j2k, compno);
848 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
849 int bandno, numbands;
852 opj_cp_t *cp = j2k->cp;
853 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
854 opj_tccp_t *tccp = &tcp->tccps[compno];
855 opj_cio_t *cio = j2k->cio;
857 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
858 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
860 for (bandno = 0; bandno < numbands; bandno++) {
861 expn = tccp->stepsizes[bandno].expn;
862 mant = tccp->stepsizes[bandno].mant;
864 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
865 cio_write(cio, expn << 3, 1); /* SPqcx_i */
867 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
872 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
874 int bandno, numbands;
876 opj_cp_t *cp = j2k->cp;
877 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
878 opj_tccp_t *tccp = &tcp->tccps[compno];
879 opj_cio_t *cio = j2k->cio;
881 tmp = cio_read(cio, 1); /* Sqcx */
882 tccp->qntsty = tmp & 0x1f;
883 tccp->numgbits = tmp >> 5;
884 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
885 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
888 if (j2k->cp->correct) {
890 /* if JPWL is on, we check whether there are too many subbands */
891 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
892 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
893 "JPWL: bad number of subbands in Sqcx (%d)\n",
896 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
899 /* we try to correct */
901 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
902 "- setting number of bands to %d => HYPOTHESIS!!!\n",
909 /* We check whether there are too many subbands */
910 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
911 opj_event_msg(j2k->cinfo, EVT_WARNING ,
912 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
913 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
916 #endif /* USE_JPWL */
918 for (bandno = 0; bandno < numbands; bandno++) {
920 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
921 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
924 tmp = cio_read(cio, 2); /* SPqcx_i */
928 if (bandno < J2K_MAXBANDS){
929 tccp->stepsizes[bandno].expn = expn;
930 tccp->stepsizes[bandno].mant = mant;
934 /* Add Antonin : if scalar_derived -> compute other stepsizes */
935 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
936 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
937 tccp->stepsizes[bandno].expn =
938 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
939 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
940 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
946 static void j2k_write_qcd(opj_j2k_t *j2k) {
949 opj_cio_t *cio = j2k->cio;
951 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
952 lenp = cio_tell(cio);
954 j2k_write_qcx(j2k, 0);
955 len = cio_tell(cio) - lenp;
957 cio_write(cio, len, 2); /* Lqcd */
958 cio_seek(cio, lenp + len);
961 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
964 static void j2k_read_qcd(opj_j2k_t *j2k) {
967 opj_cio_t *cio = j2k->cio;
968 opj_image_t *image = j2k->image;
970 len = cio_read(cio, 2); /* Lqcd */
972 for (i = 0; i < image->numcomps; i++) {
974 j2k_read_qcx(j2k, i, len - 2);
978 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
981 opj_cio_t *cio = j2k->cio;
983 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
984 lenp = cio_tell(cio);
986 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
987 j2k_write_qcx(j2k, compno);
988 len = cio_tell(cio) - lenp;
990 cio_write(cio, len, 2); /* Lqcc */
991 cio_seek(cio, lenp + len);
994 static void j2k_read_qcc(opj_j2k_t *j2k) {
996 int numcomp = j2k->image->numcomps;
997 opj_cio_t *cio = j2k->cio;
999 len = cio_read(cio, 2); /* Lqcc */
1000 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
1003 if (j2k->cp->correct) {
1005 static int backup_compno = 0;
1007 /* compno is negative or larger than the number of components!!! */
1008 if ((compno < 0) || (compno >= numcomp)) {
1009 opj_event_msg(j2k->cinfo, EVT_ERROR,
1010 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
1013 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1016 /* we try to correct */
1017 compno = backup_compno % numcomp;
1018 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1019 "- setting component number to %d\n",
1023 /* keep your private count of tiles */
1026 #endif /* USE_JPWL */
1028 if ((compno < 0) || (compno >= numcomp)) {
1029 opj_event_msg(j2k->cinfo, EVT_ERROR,
1030 "bad component number in QCC (%d out of a maximum of %d)\n",
1031 compno, j2k->image->numcomps);
1035 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
1038 static void j2k_write_poc(opj_j2k_t *j2k) {
1039 int len, numpchgs, i;
1041 int numcomps = j2k->image->numcomps;
1043 opj_cp_t *cp = j2k->cp;
1044 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1045 opj_tccp_t *tccp = &tcp->tccps[0];
1046 opj_cio_t *cio = j2k->cio;
1048 numpchgs = 1 + tcp->numpocs;
1049 cio_write(cio, J2K_MS_POC, 2); /* POC */
1050 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
1051 cio_write(cio, len, 2); /* Lpoc */
1052 for (i = 0; i < numpchgs; i++) {
1053 opj_poc_t *poc = &tcp->pocs[i];
1054 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1055 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1056 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1057 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1058 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1059 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
1060 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1061 poc->compno1 = int_min(poc->compno1, numcomps);
1062 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1066 static void j2k_read_poc(opj_j2k_t *j2k) {
1067 int len, numpchgs, i, old_poc;
1069 int numcomps = j2k->image->numcomps;
1071 opj_cp_t *cp = j2k->cp;
1072 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1073 opj_cio_t *cio = j2k->cio;
1075 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1077 len = cio_read(cio, 2); /* Lpoc */
1078 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1080 for (i = old_poc; i < numpchgs + old_poc; i++) {
1082 poc = &tcp->pocs[i];
1083 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1084 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1085 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
1086 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
1087 poc->compno1 = int_min(
1088 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1089 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1092 tcp->numpocs = numpchgs + old_poc - 1;
1095 static void j2k_read_crg(opj_j2k_t *j2k) {
1096 int len, i, Xcrg_i, Ycrg_i;
1098 opj_cio_t *cio = j2k->cio;
1099 int numcomps = j2k->image->numcomps;
1101 len = cio_read(cio, 2); /* Lcrg */
1103 for (i = 0; i < numcomps; i++) {
1104 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1106 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1111 static void j2k_read_tlm(opj_j2k_t *j2k) {
1112 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1113 long int Ttlm_i, Ptlm_i;
1115 opj_cio_t *cio = j2k->cio;
1117 len = cio_read(cio, 2); /* Ltlm */
1118 Ztlm = cio_read(cio, 1); /* Ztlm */
1120 Stlm = cio_read(cio, 1); /* Stlm */
1121 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1122 SP = (Stlm >> 6) & 0x01;
1123 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1124 for (i = 0; i < tile_tlm; i++) {
1125 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1127 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1132 static void j2k_read_plm(opj_j2k_t *j2k) {
1133 int len, i, Zplm, Nplm, add, packet_len = 0;
1135 opj_cio_t *cio = j2k->cio;
1137 len = cio_read(cio, 2); /* Lplm */
1138 Zplm = cio_read(cio, 1); /* Zplm */
1142 Nplm = cio_read(cio, 4); /* Nplm */
1144 for (i = Nplm; i > 0; i--) {
1145 add = cio_read(cio, 1);
1147 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1148 if ((add & 0x80) == 0) {
1158 static void j2k_read_plt(opj_j2k_t *j2k) {
1159 int len, i, Zplt, packet_len = 0, add;
1161 opj_cio_t *cio = j2k->cio;
1163 len = cio_read(cio, 2); /* Lplt */
1164 Zplt = cio_read(cio, 1); /* Zplt */
1166 for (i = len - 3; i > 0; i--) {
1167 add = cio_read(cio, 1);
1168 packet_len = (packet_len << 7) + add; /* Iplt_i */
1169 if ((add & 0x80) == 0) {
1176 static void j2k_read_ppm(opj_j2k_t *j2k) {
1177 int len, Z_ppm, i, j;
1180 opj_cp_t *cp = j2k->cp;
1181 opj_cio_t *cio = j2k->cio;
1183 len = cio_read(cio, 2);
1186 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1189 if (cp->ppm_previous == 0) {
1190 N_ppm = cio_read(cio, 4); /* N_ppm */
1193 N_ppm = cp->ppm_previous;
1196 if (Z_ppm == 0) { /* First PPM marker */
1197 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1198 cp->ppm_data_first = cp->ppm_data;
1199 cp->ppm_len = N_ppm;
1200 } else { /* NON-first PPM marker */
1201 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1204 /* this memory allocation check could be done even in non-JPWL cases */
1206 if (!cp->ppm_data) {
1207 opj_event_msg(j2k->cinfo, EVT_ERROR,
1208 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1210 if (!JPWL_ASSUME || JPWL_ASSUME) {
1211 opj_free(cp->ppm_data);
1212 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1219 cp->ppm_data_first = cp->ppm_data;
1220 cp->ppm_len = N_ppm + cp->ppm_store;
1222 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1223 cp->ppm_data[j] = cio_read(cio, 1);
1227 break; /* Case of non-finished packet header in present marker but finished in next one */
1229 cp->ppm_previous = i - 1;
1234 static void j2k_read_ppt(opj_j2k_t *j2k) {
1235 int len, Z_ppt, i, j = 0;
1237 opj_cp_t *cp = j2k->cp;
1238 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1239 opj_cio_t *cio = j2k->cio;
1241 len = cio_read(cio, 2);
1242 Z_ppt = cio_read(cio, 1);
1244 if (Z_ppt == 0) { /* First PPT marker */
1245 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1246 tcp->ppt_data_first = tcp->ppt_data;
1248 tcp->ppt_len = len - 3;
1249 } else { /* NON-first PPT marker */
1250 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1251 tcp->ppt_data_first = tcp->ppt_data;
1252 tcp->ppt_len = len - 3 + tcp->ppt_store;
1255 for (i = len - 3; i > 0; i--) {
1256 tcp->ppt_data[j] = cio_read(cio, 1);
1262 static void j2k_write_tlm(opj_j2k_t *j2k){
1264 opj_cio_t *cio = j2k->cio;
1265 j2k->tlm_start = cio_tell(cio);
1266 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1267 lenp = 4 + (5*j2k->totnum_tp);
1268 cio_write(cio,lenp,2); /* Ltlm */
1269 cio_write(cio, 0,1); /* Ztlm=0*/
1270 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1271 cio_skip(cio,5*j2k->totnum_tp);
1274 static void j2k_write_sot(opj_j2k_t *j2k) {
1277 opj_cio_t *cio = j2k->cio;
1279 j2k->sot_start = cio_tell(cio);
1280 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1281 lenp = cio_tell(cio);
1282 cio_skip(cio, 2); /* Lsot (further) */
1283 cio_write(cio, j2k->curtileno, 2); /* Isot */
1284 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1285 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
1286 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1287 len = cio_tell(cio) - lenp;
1288 cio_seek(cio, lenp);
1289 cio_write(cio, len, 2); /* Lsot */
1290 cio_seek(cio, lenp + len);
1294 /* update markers struct */
1295 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1296 #endif /* USE_JPWL */
1299 if( j2k->cstr_info && j2k->cur_tp_num==0){
1300 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
1304 static void j2k_read_sot(opj_j2k_t *j2k) {
1305 int len, tileno, totlen, partno, numparts, i;
1306 opj_tcp_t *tcp = NULL;
1309 opj_cp_t *cp = j2k->cp;
1310 opj_cio_t *cio = j2k->cio;
1312 len = cio_read(cio, 2);
1314 tileno = cio_read(cio, 2);
1317 if (j2k->cp->correct) {
1319 static int backup_tileno = 0;
1321 /* tileno is negative or larger than the number of tiles!!! */
1322 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1323 opj_event_msg(j2k->cinfo, EVT_ERROR,
1324 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1325 tileno, (cp->tw * cp->th));
1327 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1330 /* we try to correct */
1331 tileno = backup_tileno;
1332 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1333 "- setting tile number to %d\n",
1337 /* keep your private count of tiles */
1341 #endif /* USE_JPWL */
1343 /* tileno is negative or larger than the number of tiles!!! */
1344 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
1345 opj_event_msg(j2k->cinfo, EVT_ERROR,
1346 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1347 tileno, (cp->tw * cp->th));
1352 if (cp->tileno_size == 0) {
1353 cp->tileno[cp->tileno_size] = tileno;
1357 while (i < cp->tileno_size && status == 0) {
1358 status = cp->tileno[i] == tileno ? 1 : 0;
1362 cp->tileno[cp->tileno_size] = tileno;
1367 totlen = cio_read(cio, 4);
1370 if (j2k->cp->correct) {
1372 /* totlen is negative or larger than the bytes left!!! */
1373 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1374 opj_event_msg(j2k->cinfo, EVT_ERROR,
1375 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1376 totlen, cio_numbytesleft(cio) + 8);
1378 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1381 /* we try to correct */
1383 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1384 "- setting Psot to %d => assuming it is the last tile\n",
1390 #endif /* USE_JPWL */
1392 /* totlen is negative or larger than the bytes left!!! */
1393 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1394 opj_event_msg(j2k->cinfo, EVT_ERROR,
1395 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1396 totlen, cio_numbytesleft(cio) + 8);
1402 totlen = cio_numbytesleft(cio) + 8;
1404 partno = cio_read(cio, 1);
1405 numparts = cio_read(cio, 1);
1407 if (partno >= numparts) {
1408 opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
1409 numparts = partno+1;
1412 j2k->curtileno = tileno;
1413 j2k->cur_tp_num = partno;
1414 j2k->eot = cio_getbp(cio) - 12 + totlen;
1415 j2k->state = J2K_STATE_TPH;
1416 tcp = &cp->tcps[j2k->curtileno];
1419 if (j2k->cstr_info) {
1422 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1423 j2k->cstr_info->tile[tileno].tileno = tileno;
1424 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1425 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
1427 j2k->cstr_info->tile[tileno].end_pos += totlen;
1429 j2k->cstr_info->tile[tileno].num_tps = numparts;
1431 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
1433 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
1434 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1435 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1436 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1439 if (tcp->first == 1) {
1440 /* Initialization PPT */
1441 opj_tccp_t *tmp = tcp->tccps;
1442 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1444 tcp->ppt_data = NULL;
1445 tcp->ppt_data_first = NULL;
1448 for (i = 0; i < j2k->image->numcomps; i++) {
1449 tcp->tccps[i] = j2k->default_tcp->tccps[i];
1451 cp->tcps[j2k->curtileno].first = 0;
1455 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
1458 opj_tcp_t *tcp = NULL;
1459 opj_codestream_info_t *cstr_info = NULL;
1461 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1462 opj_cp_t *cp = j2k->cp;
1463 opj_cio_t *cio = j2k->cio;
1465 tcd->tp_num = j2k->tp_num ;
1466 tcd->cur_tp_num = j2k->cur_tp_num;
1468 cio_write(cio, J2K_MS_SOD, 2);
1470 if( j2k->cstr_info && j2k->cur_tp_num==0){
1471 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
1474 if (j2k->curtileno == 0) {
1475 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1479 cstr_info = j2k->cstr_info;
1481 if (!j2k->cur_tp_num ) {
1482 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1483 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1486 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
1487 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
1491 /* update markers struct */
1492 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1493 #endif /* USE_JPWL */
1498 tcp = &cp->tcps[j2k->curtileno];
1499 for (layno = 0; layno < tcp->numlayers; layno++) {
1500 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
1501 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
1502 } else if (tcp->rates[layno]) {
1503 tcp->rates[layno]=1;
1506 if(j2k->cur_tp_num == 0){
1507 tcd->tcd_image->tiles->packno = 0;
1509 cstr_info->packno = 0;
1512 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
1514 /* Writing Psot in SOT marker */
1515 totlen = cio_tell(cio) + l - j2k->sot_start;
1516 cio_seek(cio, j2k->sot_start + 6);
1517 cio_write(cio, totlen, 4);
1518 cio_seek(cio, j2k->sot_start + totlen);
1519 /* Writing Ttlm and Ptlm in TLM marker */
1521 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
1522 cio_write(cio, j2k->curtileno, 1);
1523 cio_write(cio, totlen, 4);
1525 cio_seek(cio, j2k->sot_start + totlen);
1528 static void j2k_read_sod(opj_j2k_t *j2k) {
1529 int len, truncate = 0, i;
1530 unsigned char *data = NULL, *data_ptr = NULL;
1532 opj_cio_t *cio = j2k->cio;
1533 int curtileno = j2k->curtileno;
1536 if (j2k->cstr_info) {
1537 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1538 cio_tell(cio) + j2k->pos_correction - 1;
1539 if (j2k->cur_tp_num == 0)
1540 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1541 j2k->cstr_info->packno = 0;
1544 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1546 if (len == cio_numbytesleft(cio) + 1) {
1547 truncate = 1; /* Case of a truncate codestream */
1550 data = j2k->tile_data[curtileno];
1551 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1553 data_ptr = data + j2k->tile_len[curtileno];
1554 for (i = 0; i < len; i++) {
1555 data_ptr[i] = cio_read(cio, 1);
1558 j2k->tile_len[curtileno] += len;
1559 j2k->tile_data[curtileno] = data;
1562 j2k->state = J2K_STATE_TPHSOT;
1564 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1569 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1570 opj_cp_t *cp = j2k->cp;
1571 opj_tcp_t *tcp = &cp->tcps[tileno];
1572 opj_cio_t *cio = j2k->cio;
1573 int numcomps = j2k->image->numcomps;
1575 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1576 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1577 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1578 cio_write(cio, 0, 1); /* Srgn */
1579 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1582 static void j2k_read_rgn(opj_j2k_t *j2k) {
1583 int len, compno, roisty;
1585 opj_cp_t *cp = j2k->cp;
1586 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1587 opj_cio_t *cio = j2k->cio;
1588 int numcomps = j2k->image->numcomps;
1590 len = cio_read(cio, 2); /* Lrgn */
1592 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1593 roisty = cio_read(cio, 1); /* Srgn */
1597 if (j2k->cp->correct) {
1598 /* totlen is negative or larger than the bytes left!!! */
1599 if (compno >= numcomps) {
1600 opj_event_msg(j2k->cinfo, EVT_ERROR,
1601 "JPWL: bad component number in RGN (%d when there are only %d)\n",
1603 if (!JPWL_ASSUME || JPWL_ASSUME) {
1604 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1609 #endif /* USE_JPWL */
1611 if (compno >= numcomps) {
1612 opj_event_msg(j2k->cinfo, EVT_ERROR,
1613 "bad component number in RGN (%d out of a maximum of %d)\n",
1614 compno, j2k->image->numcomps);
1618 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1621 static void j2k_write_eoc(opj_j2k_t *j2k) {
1622 opj_cio_t *cio = j2k->cio;
1623 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1624 cio_write(cio, J2K_MS_EOC, 2);
1628 /* update markers struct */
1629 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1630 #endif /* USE_JPWL */
1634 static void j2k_read_eoc(opj_j2k_t *j2k) {
1636 opj_bool success = OPJ_FALSE;
1638 /* if packets should be decoded */
1639 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1640 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1641 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1642 for (i = 0; i < j2k->cp->tileno_size; i++) {
1643 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1644 if (j2k->cp->tileno[i] != -1)
1646 tileno = j2k->cp->tileno[i];
1647 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
1648 opj_free(j2k->tile_data[tileno]);
1649 j2k->tile_data[tileno] = NULL;
1650 tcd_free_decode_tile(tcd, i);
1653 success = OPJ_FALSE;
1654 if (success == OPJ_FALSE) {
1655 j2k->state |= J2K_STATE_ERR;
1659 tcd_free_decode(tcd);
1662 /* if packets should not be decoded */
1664 for (i = 0; i < j2k->cp->tileno_size; i++) {
1665 tileno = j2k->cp->tileno[i];
1666 opj_free(j2k->tile_data[tileno]);
1667 j2k->tile_data[tileno] = NULL;
1670 if (j2k->state & J2K_STATE_ERR)
1671 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1673 j2k->state = J2K_STATE_MT;
1676 typedef struct opj_dec_mstabent {
1679 /** value of the state when the marker can appear */
1681 /** action linked to the marker */
1682 void (*handler) (opj_j2k_t *j2k);
1683 } opj_dec_mstabent_t;
1685 opj_dec_mstabent_t j2k_dec_mstab[] = {
1686 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1687 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1688 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1689 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1690 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1691 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1692 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1693 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1694 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1695 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1696 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1697 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1698 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1699 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1700 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1701 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1703 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1704 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1707 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1708 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1709 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1710 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1711 #endif /* USE_JPWL */
1713 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1714 {J2K_MS_INSEC, 0, j2k_read_insec},
1715 #endif /* USE_JPSEC */
1717 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1720 static void j2k_read_unk(opj_j2k_t *j2k) {
1721 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1724 if (j2k->cp->correct) {
1726 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1727 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1728 id = cio_read(j2k->cio, 2);
1729 opj_event_msg(j2k->cinfo, EVT_ERROR,
1730 "JPWL: really don't know this marker %x\n",
1733 opj_event_msg(j2k->cinfo, EVT_ERROR,
1734 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
1737 /* OK, activate this at your own risk!!! */
1738 /* we look for the marker at the minimum hamming distance from this */
1739 while (j2k_dec_mstab[m].id) {
1741 /* 1's where they differ */
1742 tmp_id = j2k_dec_mstab[m].id ^ id;
1744 /* compute the hamming distance between our id and the current */
1746 for (i = 0; i < 16; i++) {
1747 if ((tmp_id >> i) & 0x0001) {
1752 /* if current distance is smaller, set the minimum */
1753 if (cur_dist < min_dist) {
1754 min_dist = cur_dist;
1755 min_id = j2k_dec_mstab[m].id;
1758 /* jump to the next marker */
1762 /* do we substitute the marker? */
1763 if (min_dist < JPWL_MAXIMUM_HAMMING) {
1764 opj_event_msg(j2k->cinfo, EVT_ERROR,
1765 "- marker %x is at distance %d from the read %x\n",
1766 min_id, min_dist, id);
1767 opj_event_msg(j2k->cinfo, EVT_ERROR,
1768 "- trying to substitute in place and crossing fingers!\n");
1769 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1770 cio_write(j2k->cio, min_id, 2);
1773 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1778 #endif /* USE_JPWL */
1783 Read the lookup table containing all the marker, status and action
1784 @param id Marker value
1786 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1787 opj_dec_mstabent_t *e;
1788 for (e = j2k_dec_mstab; e->id != 0; e++) {
1796 /* ----------------------------------------------------------------------- */
1797 /* J2K / JPT decoder interface */
1798 /* ----------------------------------------------------------------------- */
1800 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1801 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1805 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1806 if(!j2k->default_tcp) {
1812 j2k->tile_data = NULL;
1817 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1820 if(j2k->tile_len != NULL) {
1821 opj_free(j2k->tile_len);
1823 if(j2k->tile_data != NULL) {
1824 if(j2k->cp != NULL) {
1825 for (i = 0; i < j2k->cp->tileno_size; i++) {
1826 int tileno = j2k->cp->tileno[i];
1827 opj_free(j2k->tile_data[tileno]);
1828 j2k->tile_data[tileno] = NULL;
1832 opj_free(j2k->tile_data);
1834 if(j2k->default_tcp != NULL) {
1835 opj_tcp_t *default_tcp = j2k->default_tcp;
1836 if(default_tcp->ppt_data_first != NULL) {
1837 opj_free(default_tcp->ppt_data_first);
1839 if(j2k->default_tcp->tccps != NULL) {
1840 opj_free(j2k->default_tcp->tccps);
1842 opj_free(j2k->default_tcp);
1844 if(j2k->cp != NULL) {
1845 opj_cp_t *cp = j2k->cp;
1846 if(cp->tcps != NULL) {
1847 for(i = 0; i < cp->tw * cp->th; i++) {
1848 if(cp->tcps[i].ppt_data_first != NULL) {
1849 opj_free(cp->tcps[i].ppt_data_first);
1851 if(cp->tcps[i].tccps != NULL) {
1852 opj_free(cp->tcps[i].tccps);
1857 if(cp->ppm_data_first != NULL) {
1858 opj_free(cp->ppm_data_first);
1860 if(cp->tileno != NULL) {
1861 opj_free(cp->tileno);
1863 if(cp->comment != NULL) {
1864 opj_free(cp->comment);
1872 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1873 if(j2k && parameters) {
1874 /* create and initialize the coding parameters structure */
1875 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1876 cp->reduce = parameters->cp_reduce;
1877 cp->layer = parameters->cp_layer;
1878 cp->limit_decoding = parameters->cp_limit_decoding;
1881 cp->correct = parameters->jpwl_correct;
1882 cp->exp_comps = parameters->jpwl_exp_comps;
1883 cp->max_tiles = parameters->jpwl_max_tiles;
1884 #endif /* USE_JPWL */
1887 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1892 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1893 opj_image_t *image = NULL;
1895 opj_common_ptr cinfo = j2k->cinfo;
1898 j2k->cstr_info = cstr_info;
1900 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
1902 /* create an empty image */
1903 image = opj_image_create0();
1906 j2k->state = J2K_STATE_MHSOC;
1909 opj_dec_mstabent_t *e;
1910 int id = cio_read(cio, 2);
1913 /* we try to honor JPWL correction power */
1914 if (j2k->cp->correct) {
1916 int orig_pos = cio_tell(cio);
1919 /* call the corrector */
1920 status = jpwl_correct(j2k);
1922 /* go back to where you were */
1923 cio_seek(cio, orig_pos - 2);
1925 /* re-read the marker */
1926 id = cio_read(cio, 2);
1928 /* check whether it begins with ff */
1929 if (id >> 8 != 0xff) {
1930 opj_event_msg(cinfo, EVT_ERROR,
1931 "JPWL: possible bad marker %x at %d\n",
1932 id, cio_tell(cio) - 2);
1934 opj_image_destroy(image);
1935 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
1938 /* we try to correct */
1940 cio_seek(cio, cio_tell(cio) - 2);
1941 cio_write(cio, id, 2);
1942 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
1943 "- setting marker to %x\n",
1948 #endif /* USE_JPWL */
1950 if (id >> 8 != 0xff) {
1951 if(cio_numbytesleft(cio) != 0) /* not end of file reached and no EOC */
1953 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1954 opj_image_destroy(image);
1957 opj_event_msg(cinfo, EVT_WARNING, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1958 j2k->state = J2K_STATE_NEOC;
1961 e = j2k_dec_mstab_lookup(id);
1962 /* Check if the marker is known*/
1963 if (!(j2k->state & e->states)) {
1964 opj_image_destroy(image);
1965 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1968 /* Check if the decoding is limited to the main header*/
1969 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
1970 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
1977 if (j2k->state & J2K_STATE_ERR)
1979 opj_image_destroy(image);
1983 if (j2k->state == J2K_STATE_MT) {
1986 if (j2k->state == J2K_STATE_NEOC) {
1990 if (j2k->state == J2K_STATE_NEOC) {
1994 if (j2k->state != J2K_STATE_MT) {
1995 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
2001 * Read a JPT-stream and decode file
2004 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
2005 opj_image_t *image = NULL;
2006 opj_jpt_msg_header_t header;
2008 opj_common_ptr cinfo = j2k->cinfo;
2010 OPJ_ARG_NOT_USED(cstr_info);
2014 /* create an empty image */
2015 image = opj_image_create0();
2018 j2k->state = J2K_STATE_MHSOC;
2020 /* Initialize the header */
2021 jpt_init_msg_header(&header);
2022 /* Read the first header of the message */
2023 jpt_read_msg_header(cinfo, cio, &header);
2025 position = cio_tell(cio);
2026 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
2027 opj_image_destroy(image);
2028 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
2033 opj_dec_mstabent_t *e = NULL;
2036 if (!cio_numbytesleft(cio)) {
2040 /* data-bin read -> need to read a new header */
2041 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
2042 jpt_read_msg_header(cinfo, cio, &header);
2043 position = cio_tell(cio);
2044 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
2045 opj_image_destroy(image);
2046 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
2051 id = cio_read(cio, 2);
2052 if (id >> 8 != 0xff) {
2053 if(cio_numbytesleft(cio) != 0) /* no end of file reached and no EOC */
2055 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
2056 opj_image_destroy(image);
2059 opj_event_msg(cinfo, EVT_WARNING, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
2060 j2k->state = J2K_STATE_NEOC;
2063 e = j2k_dec_mstab_lookup(id);
2064 if (!(j2k->state & e->states)) {
2065 opj_image_destroy(image);
2066 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
2072 if (j2k->state == J2K_STATE_MT) {
2075 if (j2k->state == J2K_STATE_NEOC) {
2079 if (j2k->state == J2K_STATE_NEOC) {
2083 if (j2k->state != J2K_STATE_MT) {
2084 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
2090 /* ----------------------------------------------------------------------- */
2091 /* J2K encoder interface */
2092 /* ----------------------------------------------------------------------- */
2094 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
2095 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
2102 void j2k_destroy_compress(opj_j2k_t *j2k) {
2106 if(j2k->cp != NULL) {
2107 opj_cp_t *cp = j2k->cp;
2110 opj_free(cp->comment);
2113 opj_free(cp->matrice);
2115 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2116 opj_free(cp->tcps[tileno].tccps);
2125 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
2126 int i, j, tileno, numpocs_tile;
2127 opj_cp_t *cp = NULL;
2129 if(!j2k || !parameters || ! image) {
2133 /* create and initialize the coding parameters structure */
2134 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
2136 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
2139 /* set default values for cp */
2144 copy user encoding parameters
2146 cp->cinema = parameters->cp_cinema;
2147 cp->max_comp_size = parameters->max_comp_size;
2148 cp->rsiz = parameters->cp_rsiz;
2149 cp->disto_alloc = parameters->cp_disto_alloc;
2150 cp->fixed_alloc = parameters->cp_fixed_alloc;
2151 cp->fixed_quality = parameters->cp_fixed_quality;
2153 /* mod fixed_quality */
2154 if(parameters->cp_matrice) {
2155 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
2156 cp->matrice = (int *) opj_malloc(array_size);
2157 memcpy(cp->matrice, parameters->cp_matrice, array_size);
2161 cp->tdx = parameters->cp_tdx;
2162 cp->tdy = parameters->cp_tdy;
2165 cp->tx0 = parameters->cp_tx0;
2166 cp->ty0 = parameters->cp_ty0;
2168 /* comment string */
2169 if(parameters->cp_comment) {
2170 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2172 strcpy(cp->comment, parameters->cp_comment);
2177 calculate other encoding parameters
2180 if (parameters->tile_size_on) {
2181 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2182 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2184 cp->tdx = image->x1 - cp->tx0;
2185 cp->tdy = image->y1 - cp->ty0;
2188 if(parameters->tp_on){
2189 cp->tp_flag = parameters->tp_flag;
2194 for(i=0;i<image->numcomps ;i++){
2195 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
2201 calculate JPWL encoding parameters
2204 if (parameters->jpwl_epc_on) {
2208 cp->epc_on = OPJ_TRUE;
2209 cp->info_on = OPJ_FALSE; /* no informative technique */
2212 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2213 cp->epb_on = OPJ_TRUE;
2215 cp->hprot_MH = parameters->jpwl_hprot_MH;
2216 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2217 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2218 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2220 /* if tile specs are not specified, copy MH specs */
2221 if (cp->hprot_TPH[0] == -1) {
2222 cp->hprot_TPH_tileno[0] = 0;
2223 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2225 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2226 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2227 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2228 cp->pprot[i] = parameters->jpwl_pprot[i];
2232 /* set ESD writing */
2233 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2234 cp->esd_on = OPJ_TRUE;
2236 cp->sens_size = parameters->jpwl_sens_size;
2237 cp->sens_addr = parameters->jpwl_sens_addr;
2238 cp->sens_range = parameters->jpwl_sens_range;
2240 cp->sens_MH = parameters->jpwl_sens_MH;
2241 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2242 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2243 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2247 /* always set RED writing to false: we are at the encoder */
2248 cp->red_on = OPJ_FALSE;
2251 cp->epc_on = OPJ_FALSE;
2253 #endif /* USE_JPWL */
2256 /* initialize the mutiple tiles */
2257 /* ---------------------------- */
2258 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2260 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2261 opj_tcp_t *tcp = &cp->tcps[tileno];
2262 tcp->numlayers = parameters->tcp_numlayers;
2263 for (j = 0; j < tcp->numlayers; j++) {
2265 if (cp->fixed_quality) {
2266 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2268 tcp->rates[j] = parameters->tcp_rates[j];
2270 if (cp->fixed_quality) { /* add fixed_quality */
2271 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2273 tcp->rates[j] = parameters->tcp_rates[j];
2277 tcp->csty = parameters->csty;
2278 tcp->prg = parameters->prog_order;
2279 tcp->mct = parameters->tcp_mct;
2283 if (parameters->numpocs) {
2284 /* initialisation of POC */
2286 for (i = 0; i < parameters->numpocs; i++) {
2287 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2288 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2289 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2290 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2291 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2292 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2293 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2294 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2295 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2299 tcp->numpocs = numpocs_tile -1 ;
2304 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2306 for (i = 0; i < image->numcomps; i++) {
2307 opj_tccp_t *tccp = &tcp->tccps[i];
2308 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
2309 tccp->numresolutions = parameters->numresolution;
2310 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2311 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2312 tccp->cblksty = parameters->mode;
2313 tccp->qmfbid = parameters->irreversible ? 0 : 1;
2314 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
2316 if (i == parameters->roi_compno) {
2317 tccp->roishift = parameters->roi_shift;
2322 if(parameters->cp_cinema)
2324 /*Precinct size for lowest frequency subband=128*/
2327 /*Precinct size at all other resolutions = 256*/
2328 for (j = 1; j < tccp->numresolutions; j++) {
2333 if (parameters->csty & J2K_CCP_CSTY_PRT) {
2335 for (j = tccp->numresolutions - 1; j >= 0; j--) {
2336 if (p < parameters->res_spec) {
2338 if (parameters->prcw_init[p] < 1) {
2341 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2344 if (parameters->prch_init[p] < 1) {
2347 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2351 int res_spec = parameters->res_spec;
2352 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2353 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2355 if (size_prcw < 1) {
2358 tccp->prcw[j] = int_floorlog2(size_prcw);
2361 if (size_prch < 1) {
2364 tccp->prch[j] = int_floorlog2(size_prch);
2368 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2371 for (j = 0; j < tccp->numresolutions; j++) {
2378 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2383 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2385 opj_cp_t *cp = NULL;
2387 opj_tcd_t *tcd = NULL; /* TCD component */
2395 j2k->cstr_info = cstr_info;
2398 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2399 cstr_info->image_w = image->x1 - image->x0;
2400 cstr_info->image_h = image->y1 - image->y0;
2401 cstr_info->prog = (&cp->tcps[0])->prg;
2402 cstr_info->tw = cp->tw;
2403 cstr_info->th = cp->th;
2404 cstr_info->tile_x = cp->tdx; /* new version parser */
2405 cstr_info->tile_y = cp->tdy; /* new version parser */
2406 cstr_info->tile_Ox = cp->tx0; /* new version parser */
2407 cstr_info->tile_Oy = cp->ty0; /* new version parser */
2408 cstr_info->numcomps = image->numcomps;
2409 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2410 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2411 for (compno=0; compno < image->numcomps; compno++) {
2412 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2414 cstr_info->D_max = 0.0; /* ADD Marcela */
2415 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2416 cstr_info->maxmarknum = 100;
2417 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
2418 cstr_info->marknum = 0;
2428 for (compno = 1; compno < image->numcomps; compno++) {
2429 j2k_write_coc(j2k, compno);
2430 j2k_write_qcc(j2k, compno);
2434 for (compno = 0; compno < image->numcomps; compno++) {
2435 opj_tcp_t *tcp = &cp->tcps[0];
2436 if (tcp->tccps[compno].roishift)
2437 j2k_write_rgn(j2k, compno, 0);
2439 if (cp->comment != NULL) {
2443 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2447 if (cp->cinema == CINEMA4K_24) {
2452 /* uncomment only for testing JPSEC marker writing */
2453 /* j2k_write_sec(j2k); */
2457 cstr_info->main_head_end = cio_tell(cio) - 1;
2460 /**** Main Header ENDS here ***/
2462 /* create the tile encoder */
2463 tcd = tcd_create(j2k->cinfo);
2465 /* encode each tile */
2466 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2470 int acc_pack_num = 0;
2474 opj_tcp_t *tcp = &cp->tcps[tileno];
2475 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2477 j2k->curtileno = tileno;
2478 j2k->cur_tp_num = 0;
2479 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2480 /* initialisation before tile encoding */
2482 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2484 tcd_init_encode(tcd, image, cp, j2k->curtileno);
2489 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2490 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
2491 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
2492 cstr_info->tile[j2k->curtileno].marknum = 0;
2496 for(pino = 0; pino <= tcp->numpocs; pino++) {
2500 /*Get number of tile parts*/
2501 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2502 tcd->tp_pos = cp->tp_pos;
2504 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2505 j2k->tp_num = tilepartno;
2508 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2509 cio_tell(cio) + j2k->pos_correction;
2513 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2514 for (compno = 1; compno < image->numcomps; compno++) {
2515 j2k_write_coc(j2k, compno);
2516 j2k_write_qcc(j2k, compno);
2518 if (cp->tcps[tileno].numpocs) {
2525 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2526 cio_tell(cio) + j2k->pos_correction + 1;
2529 j2k_write_sod(j2k, tcd);
2533 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2534 cio_tell(cio) + j2k->pos_correction - 1;
2535 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2537 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2538 cstr_info->packno - acc_pack_num;
2539 acc_pack_num = cstr_info->packno;
2547 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2552 if (tile->PPT) { // BAD PPT !!!
2555 PPT_file=fopen("PPT","rb");
2556 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2557 for (i=0;i<tile->len_ppt;i++) {
2559 fread(&elmt, 1, 1, PPT_file);
2560 fwrite(&elmt,1,1,f);
2569 /* destroy the tile encoder */
2570 tcd_free_encode(tcd);
2573 opj_free(j2k->cur_totnum_tp);
2578 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2580 /* The following adjustment is done to adjust the codestream size */
2581 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2582 /* the first bunch of bytes is not in the codestream */
2583 cstr_info->codestream_size -= cstr_info->main_head_start;
2589 preparation of JPWL marker segments
2593 /* encode according to JPWL */
2594 jpwl_encode(j2k, cio, image);
2597 #endif /* USE_JPWL */
2602 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
2607 /* expand the list? */
2608 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
2609 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
2610 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
2613 /* add the marker */
2614 cstr_info->marker[cstr_info->marknum].type = type;
2615 cstr_info->marker[cstr_info->marknum].pos = pos;
2616 cstr_info->marker[cstr_info->marknum].len = len;
2617 cstr_info->marknum++;
2621 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
2623 opj_marker_info_t *marker;
2628 /* expand the list? */
2629 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
2630 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
2631 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
2634 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
2636 /* add the marker */
2637 marker->type = type;
2640 cstr_info->tile[tileno].marknum++;