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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "opj_includes.h"
35 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
38 /** @name Local static functions */
42 Write the SOC marker (Start Of Codestream)
45 static void j2k_write_soc(opj_j2k_t *j2k);
47 Read the SOC marker (Start of Codestream)
50 static void j2k_read_soc(opj_j2k_t *j2k);
52 Write the SIZ marker (image and tile size)
55 static void j2k_write_siz(opj_j2k_t *j2k);
57 Read the SIZ marker (image and tile size)
60 static void j2k_read_siz(opj_j2k_t *j2k);
62 Write the COM marker (comment)
65 static void j2k_write_com(opj_j2k_t *j2k);
67 Read the COM marker (comment)
70 static void j2k_read_com(opj_j2k_t *j2k);
72 Write the value concerning the specified component in the marker COD and COC
74 @param compno Number of the component concerned by the information written
76 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
78 Read the value concerning the specified component in the marker COD and COC
80 @param compno Number of the component concerned by the information read
82 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
84 Write the COD marker (coding style default)
87 static void j2k_write_cod(opj_j2k_t *j2k);
89 Read the COD marker (coding style default)
92 static void j2k_read_cod(opj_j2k_t *j2k);
94 Write the COC marker (coding style component)
96 @param compno Number of the component concerned by the information written
98 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
100 Read the COC marker (coding style component)
101 @param j2k J2K handle
103 static void j2k_read_coc(opj_j2k_t *j2k);
105 Write the value concerning the specified component in the marker QCD and QCC
106 @param j2k J2K handle
107 @param compno Number of the component concerned by the information written
109 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
111 Read the value concerning the specified component in the marker QCD and QCC
112 @param j2k J2K handle
113 @param compno Number of the component concern by the information read
114 @param len Length of the information in the QCX part of the marker QCD/QCC
116 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
118 Write the QCD marker (quantization default)
119 @param j2k J2K handle
121 static void j2k_write_qcd(opj_j2k_t *j2k);
123 Read the QCD marker (quantization default)
124 @param j2k J2K handle
126 static void j2k_read_qcd(opj_j2k_t *j2k);
128 Write the QCC marker (quantization component)
129 @param j2k J2K handle
130 @param compno Number of the component concerned by the information written
132 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
134 Read the QCC marker (quantization component)
135 @param j2k J2K handle
137 static void j2k_read_qcc(opj_j2k_t *j2k);
139 Write the POC marker (progression order change)
140 @param j2k J2K handle
142 static void j2k_write_poc(opj_j2k_t *j2k);
144 Read the POC marker (progression order change)
145 @param j2k J2K handle
147 static void j2k_read_poc(opj_j2k_t *j2k);
149 Read the CRG marker (component registration)
150 @param j2k J2K handle
152 static void j2k_read_crg(opj_j2k_t *j2k);
154 Read the TLM marker (tile-part lengths)
155 @param j2k J2K handle
157 static void j2k_read_tlm(opj_j2k_t *j2k);
159 Read the PLM marker (packet length, main header)
160 @param j2k J2K handle
162 static void j2k_read_plm(opj_j2k_t *j2k);
164 Read the PLT marker (packet length, tile-part header)
165 @param j2k J2K handle
167 static void j2k_read_plt(opj_j2k_t *j2k);
169 Read the PPM marker (packet packet headers, main header)
170 @param j2k J2K handle
172 static void j2k_read_ppm(opj_j2k_t *j2k);
174 Read the PPT marker (packet packet headers, tile-part header)
175 @param j2k J2K handle
177 static void j2k_read_ppt(opj_j2k_t *j2k);
179 Write the TLM marker (Mainheader)
180 @param j2k J2K handle
182 static void j2k_write_tlm(opj_j2k_t *j2k);
184 Write the SOT marker (start of tile-part)
185 @param j2k J2K handle
187 static void j2k_write_sot(opj_j2k_t *j2k);
189 Read the SOT marker (start of tile-part)
190 @param j2k J2K handle
192 static void j2k_read_sot(opj_j2k_t *j2k);
194 Write the SOD marker (start of data)
195 @param j2k J2K handle
196 @param tile_coder Pointer to a TCD handle
198 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
200 Read the SOD marker (start of data)
201 @param j2k J2K handle
203 static void j2k_read_sod(opj_j2k_t *j2k);
205 Write the RGN marker (region-of-interest)
206 @param j2k J2K handle
207 @param compno Number of the component concerned by the information written
208 @param tileno Number of the tile concerned by the information written
210 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
212 Read the RGN marker (region-of-interest)
213 @param j2k J2K handle
215 static void j2k_read_rgn(opj_j2k_t *j2k);
217 Write the EOC marker (end of codestream)
218 @param j2k J2K handle
220 static void j2k_write_eoc(opj_j2k_t *j2k);
222 Read the EOC marker (end of codestream)
223 @param j2k J2K handle
225 static void j2k_read_eoc(opj_j2k_t *j2k);
227 Read an unknown marker
228 @param j2k J2K handle
230 static void j2k_read_unk(opj_j2k_t *j2k);
236 /* ----------------------------------------------------------------------- */
237 typedef struct j2k_prog_order{
238 OPJ_PROG_ORDER enum_prog;
242 j2k_prog_order_t j2k_prog_order_list[] = {
248 {(OPJ_PROG_ORDER)-1, ""}
251 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
252 j2k_prog_order_t *po;
253 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
254 if(po->enum_prog == prg_order){
261 /* ----------------------------------------------------------------------- */
262 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
266 opj_tcp_t *tcp = &cp->tcps[tileno];
267 prog = j2k_convert_progression_order(tcp->prg);
272 if( cp->tp_flag == prog[i] ){
273 tpend=1;cp->tp_pos=i;
277 tpnum= tpnum * tcp->pocs[pino].compE;
280 tpnum= tpnum * tcp->pocs[pino].resE;
283 tpnum= tpnum * tcp->pocs[pino].prcE;
286 tpnum= tpnum * tcp->pocs[pino].layE;
297 /** mem allocation for TLM marker*/
298 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
299 int pino,tileno,totnum_tp=0;
301 OPJ_ARG_NOT_USED(img_numcomp);
303 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
304 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
305 int cur_totnum_tp = 0;
306 opj_tcp_t *tcp = &cp->tcps[tileno];
307 for(pino = 0; pino <= tcp->numpocs; pino++) {
309 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
310 if(!pi) { return -1;}
311 tp_num = j2k_get_num_tp(cp,pino,tileno);
312 totnum_tp = totnum_tp + tp_num;
313 cur_totnum_tp = cur_totnum_tp + tp_num;
314 pi_destroy(pi, cp, tileno);
316 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
318 if (j2k->cstr_info) {
319 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
320 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
327 static void j2k_write_soc(opj_j2k_t *j2k) {
328 opj_cio_t *cio = j2k->cio;
329 cio_write(cio, J2K_MS_SOC, 2);
334 /* update markers struct */
335 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
337 #endif /* USE_JPWL */
341 static void j2k_read_soc(opj_j2k_t *j2k) {
342 j2k->state = J2K_STATE_MHSIZ;
344 if (j2k->cstr_info) {
345 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
346 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
350 static void j2k_write_siz(opj_j2k_t *j2k) {
354 opj_cio_t *cio = j2k->cio;
355 opj_image_t *image = j2k->image;
356 opj_cp_t *cp = j2k->cp;
358 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
359 lenp = cio_tell(cio);
361 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
362 cio_write(cio, image->x1, 4); /* Xsiz */
363 cio_write(cio, image->y1, 4); /* Ysiz */
364 cio_write(cio, image->x0, 4); /* X0siz */
365 cio_write(cio, image->y0, 4); /* Y0siz */
366 cio_write(cio, cp->tdx, 4); /* XTsiz */
367 cio_write(cio, cp->tdy, 4); /* YTsiz */
368 cio_write(cio, cp->tx0, 4); /* XT0siz */
369 cio_write(cio, cp->ty0, 4); /* YT0siz */
370 cio_write(cio, image->numcomps, 2); /* Csiz */
371 for (i = 0; i < image->numcomps; i++) {
372 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
373 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
374 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
376 len = cio_tell(cio) - lenp;
378 cio_write(cio, len, 2); /* Lsiz */
379 cio_seek(cio, lenp + len);
382 static void j2k_read_siz(opj_j2k_t *j2k) {
385 opj_cio_t *cio = j2k->cio;
386 opj_image_t *image = j2k->image;
387 opj_cp_t *cp = j2k->cp;
389 len = cio_read(cio, 2); /* Lsiz */
390 cio_read(cio, 2); /* Rsiz (capabilities) */
391 image->x1 = cio_read(cio, 4); /* Xsiz */
392 image->y1 = cio_read(cio, 4); /* Ysiz */
393 image->x0 = cio_read(cio, 4); /* X0siz */
394 image->y0 = cio_read(cio, 4); /* Y0siz */
395 cp->tdx = cio_read(cio, 4); /* XTsiz */
396 cp->tdy = cio_read(cio, 4); /* YTsiz */
397 cp->tx0 = cio_read(cio, 4); /* XT0siz */
398 cp->ty0 = cio_read(cio, 4); /* YT0siz */
400 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
401 opj_event_msg(j2k->cinfo, EVT_ERROR,
402 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
403 image->x0,image->x1,image->y0,image->y1);
407 image->numcomps = cio_read(cio, 2); /* Csiz */
410 if (j2k->cp->correct) {
411 /* if JPWL is on, we check whether TX errors have damaged
412 too much the SIZ parameters */
413 if (!(image->x1 * image->y1)) {
414 opj_event_msg(j2k->cinfo, EVT_ERROR,
415 "JPWL: bad image size (%d x %d)\n",
416 image->x1, image->y1);
417 if (!JPWL_ASSUME || JPWL_ASSUME) {
418 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
422 if (image->numcomps != ((len - 38) / 3)) {
423 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
424 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
425 image->numcomps, ((len - 38) / 3));
427 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
430 /* we try to correct */
431 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
432 if (image->numcomps < ((len - 38) / 3)) {
433 len = 38 + 3 * image->numcomps;
434 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
437 image->numcomps = ((len - 38) / 3);
438 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
443 /* update components number in the jpwl_exp_comps filed */
444 cp->exp_comps = image->numcomps;
446 #endif /* USE_JPWL */
448 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
449 for (i = 0; i < image->numcomps; i++) {
451 tmp = cio_read(cio, 1); /* Ssiz_i */
452 image->comps[i].prec = (tmp & 0x7f) + 1;
453 image->comps[i].sgnd = tmp >> 7;
454 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
455 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
458 if (j2k->cp->correct) {
459 /* if JPWL is on, we check whether TX errors have damaged
460 too much the SIZ parameters, again */
461 if (!(image->comps[i].dx * image->comps[i].dy)) {
462 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
463 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
464 i, i, image->comps[i].dx, image->comps[i].dy);
466 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
469 /* we try to correct */
470 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
471 if (!image->comps[i].dx) {
472 image->comps[i].dx = 1;
473 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
474 i, image->comps[i].dx);
476 if (!image->comps[i].dy) {
477 image->comps[i].dy = 1;
478 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
479 i, image->comps[i].dy);
484 #endif /* USE_JPWL */
487 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
488 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
490 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
491 image->comps[i].factor = cp->reduce; /* reducing factor per component */
494 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
495 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
498 if (j2k->cp->correct) {
499 /* if JPWL is on, we check whether TX errors have damaged
500 too much the SIZ parameters */
501 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
502 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
503 "JPWL: bad number of tiles (%d x %d)\n",
506 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
509 /* we try to correct */
510 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
513 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
516 if (cp->tw > cp->max_tiles) {
518 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
519 "- setting %d tiles in x => HYPOTHESIS!!!\n",
520 cp->max_tiles, cp->tw);
524 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
527 if (cp->th > cp->max_tiles) {
529 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
530 "- setting %d tiles in y => HYPOTHESIS!!!\n",
531 cp->max_tiles, cp->th);
535 #endif /* USE_JPWL */
537 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
538 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
542 if (j2k->cp->correct) {
544 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
545 "JPWL: could not alloc tcps field of cp\n");
546 if (!JPWL_ASSUME || JPWL_ASSUME) {
547 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
552 #endif /* USE_JPWL */
554 for (i = 0; i < cp->tw * cp->th; i++) {
556 cp->tcps[i].numpocs = 0;
557 cp->tcps[i].first = 1;
560 /* Initialization for PPM marker */
563 cp->ppm_data_first = NULL;
564 cp->ppm_previous = 0;
567 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
568 for (i = 0; i < cp->tw * cp->th; i++) {
569 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
571 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
572 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
573 j2k->state = J2K_STATE_MH;
576 if (j2k->cstr_info) {
577 opj_codestream_info_t *cstr_info = j2k->cstr_info;
578 cstr_info->image_w = image->x1 - image->x0;
579 cstr_info->image_h = image->y1 - image->y0;
580 cstr_info->numcomps = image->numcomps;
581 cstr_info->tw = cp->tw;
582 cstr_info->th = cp->th;
583 cstr_info->tile_x = cp->tdx;
584 cstr_info->tile_y = cp->tdy;
585 cstr_info->tile_Ox = cp->tx0;
586 cstr_info->tile_Oy = cp->ty0;
587 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
591 static void j2k_write_com(opj_j2k_t *j2k) {
595 if(j2k->cp->comment) {
596 opj_cio_t *cio = j2k->cio;
597 char *comment = j2k->cp->comment;
599 cio_write(cio, J2K_MS_COM, 2);
600 lenp = cio_tell(cio);
602 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
603 for (i = 0; i < strlen(comment); i++) {
604 cio_write(cio, comment[i], 1);
606 len = cio_tell(cio) - lenp;
608 cio_write(cio, len, 2);
609 cio_seek(cio, lenp + len);
613 static void j2k_read_com(opj_j2k_t *j2k) {
616 opj_cio_t *cio = j2k->cio;
618 len = cio_read(cio, 2);
619 cio_skip(cio, len - 2);
622 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
625 opj_cp_t *cp = j2k->cp;
626 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
627 opj_tccp_t *tccp = &tcp->tccps[compno];
628 opj_cio_t *cio = j2k->cio;
630 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
631 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
632 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
633 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
634 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
636 if (tccp->csty & J2K_CCP_CSTY_PRT) {
637 for (i = 0; i < tccp->numresolutions; i++) {
638 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
643 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
646 opj_cp_t *cp = j2k->cp;
647 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
648 opj_tccp_t *tccp = &tcp->tccps[compno];
649 opj_cio_t *cio = j2k->cio;
651 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
653 // If user wants to remove more resolutions than the codestream contains, return error
654 if (cp->reduce >= tccp->numresolutions) {
655 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
656 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
657 j2k->state |= J2K_STATE_ERR;
660 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
661 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
662 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
663 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
664 if (tccp->csty & J2K_CP_CSTY_PRT) {
665 for (i = 0; i < tccp->numresolutions; i++) {
666 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
667 tccp->prcw[i] = tmp & 0xf;
668 tccp->prch[i] = tmp >> 4;
673 if(j2k->cstr_info && compno == 0) {
674 for (i = 0; i < tccp->numresolutions; i++) {
675 if (tccp->csty & J2K_CP_CSTY_PRT) {
676 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
677 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
680 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
681 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
688 static void j2k_write_cod(opj_j2k_t *j2k) {
690 opj_tcp_t *tcp = NULL;
693 opj_cio_t *cio = j2k->cio;
695 cio_write(cio, J2K_MS_COD, 2); /* COD */
697 lenp = cio_tell(cio);
701 tcp = &cp->tcps[j2k->curtileno];
703 cio_write(cio, tcp->csty, 1); /* Scod */
704 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
705 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
706 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
708 j2k_write_cox(j2k, 0);
709 len = cio_tell(cio) - lenp;
711 cio_write(cio, len, 2); /* Lcod */
712 cio_seek(cio, lenp + len);
715 static void j2k_read_cod(opj_j2k_t *j2k) {
718 opj_cio_t *cio = j2k->cio;
719 opj_cp_t *cp = j2k->cp;
720 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
721 opj_image_t *image = j2k->image;
723 len = cio_read(cio, 2); /* Lcod */
724 tcp->csty = cio_read(cio, 1); /* Scod */
725 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
726 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
727 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
730 for (i = 0; i < image->numcomps; i++) {
731 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
733 j2k_read_cox(j2k, i);
737 if (j2k->cstr_info) {
738 opj_codestream_info_t *cstr_info = j2k->cstr_info;
739 cstr_info->prog = tcp->prg;
740 cstr_info->numlayers = tcp->numlayers;
741 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
742 for (i = 0; i < image->numcomps; i++) {
743 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
748 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
751 opj_cp_t *cp = j2k->cp;
752 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
753 opj_image_t *image = j2k->image;
754 opj_cio_t *cio = j2k->cio;
756 cio_write(cio, J2K_MS_COC, 2); /* COC */
757 lenp = cio_tell(cio);
759 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
760 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
761 j2k_write_cox(j2k, compno);
762 len = cio_tell(cio) - lenp;
764 cio_write(cio, len, 2); /* Lcoc */
765 cio_seek(cio, lenp + len);
768 static void j2k_read_coc(opj_j2k_t *j2k) {
771 opj_cp_t *cp = j2k->cp;
772 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
773 opj_image_t *image = j2k->image;
774 opj_cio_t *cio = j2k->cio;
776 len = cio_read(cio, 2); /* Lcoc */
777 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
778 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
779 j2k_read_cox(j2k, compno);
782 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
783 int bandno, numbands;
786 opj_cp_t *cp = j2k->cp;
787 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
788 opj_tccp_t *tccp = &tcp->tccps[compno];
789 opj_cio_t *cio = j2k->cio;
791 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
792 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
794 for (bandno = 0; bandno < numbands; bandno++) {
795 expn = tccp->stepsizes[bandno].expn;
796 mant = tccp->stepsizes[bandno].mant;
798 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
799 cio_write(cio, expn << 3, 1); /* SPqcx_i */
801 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
806 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
808 int bandno, numbands;
810 opj_cp_t *cp = j2k->cp;
811 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
812 opj_tccp_t *tccp = &tcp->tccps[compno];
813 opj_cio_t *cio = j2k->cio;
815 tmp = cio_read(cio, 1); /* Sqcx */
816 tccp->qntsty = tmp & 0x1f;
817 tccp->numgbits = tmp >> 5;
818 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
819 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
822 if (j2k->cp->correct) {
824 /* if JPWL is on, we check whether there are too many subbands */
825 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
826 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
827 "JPWL: bad number of subbands in Sqcx (%d)\n",
830 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
833 /* we try to correct */
835 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
836 "- setting number of bands to %d => HYPOTHESIS!!!\n",
841 #endif /* USE_JPWL */
843 for (bandno = 0; bandno < numbands; bandno++) {
845 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
846 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
849 tmp = cio_read(cio, 2); /* SPqcx_i */
853 tccp->stepsizes[bandno].expn = expn;
854 tccp->stepsizes[bandno].mant = mant;
857 /* Add Antonin : if scalar_derived -> compute other stepsizes */
858 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
859 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
860 tccp->stepsizes[bandno].expn =
861 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
862 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
863 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
869 static void j2k_write_qcd(opj_j2k_t *j2k) {
872 opj_cio_t *cio = j2k->cio;
874 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
875 lenp = cio_tell(cio);
877 j2k_write_qcx(j2k, 0);
878 len = cio_tell(cio) - lenp;
880 cio_write(cio, len, 2); /* Lqcd */
881 cio_seek(cio, lenp + len);
884 static void j2k_read_qcd(opj_j2k_t *j2k) {
887 opj_cio_t *cio = j2k->cio;
888 opj_image_t *image = j2k->image;
890 len = cio_read(cio, 2); /* Lqcd */
892 for (i = 0; i < image->numcomps; i++) {
894 j2k_read_qcx(j2k, i, len - 2);
898 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
901 opj_cio_t *cio = j2k->cio;
903 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
904 lenp = cio_tell(cio);
906 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
907 j2k_write_qcx(j2k, compno);
908 len = cio_tell(cio) - lenp;
910 cio_write(cio, len, 2); /* Lqcc */
911 cio_seek(cio, lenp + len);
914 static void j2k_read_qcc(opj_j2k_t *j2k) {
916 int numcomp = j2k->image->numcomps;
917 opj_cio_t *cio = j2k->cio;
919 len = cio_read(cio, 2); /* Lqcc */
920 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
923 if (j2k->cp->correct) {
925 static int backup_compno = 0;
927 /* compno is negative or larger than the number of components!!! */
928 if ((compno < 0) || (compno >= numcomp)) {
929 opj_event_msg(j2k->cinfo, EVT_ERROR,
930 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
933 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
936 /* we try to correct */
937 compno = backup_compno % numcomp;
938 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
939 "- setting component number to %d\n",
943 /* keep your private count of tiles */
946 #endif /* USE_JPWL */
948 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
951 static void j2k_write_poc(opj_j2k_t *j2k) {
952 int len, numpchgs, i;
954 int numcomps = j2k->image->numcomps;
956 opj_cp_t *cp = j2k->cp;
957 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
958 opj_tccp_t *tccp = &tcp->tccps[0];
959 opj_cio_t *cio = j2k->cio;
961 numpchgs = 1 + tcp->numpocs;
962 cio_write(cio, J2K_MS_POC, 2); /* POC */
963 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
964 cio_write(cio, len, 2); /* Lpoc */
965 for (i = 0; i < numpchgs; i++) {
966 opj_poc_t *poc = &tcp->pocs[i];
967 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
968 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
969 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
970 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
971 cio_write(cio, poc->resno1, 1); /* REpoc_i */
972 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
973 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
974 poc->compno1 = int_min(poc->compno1, numcomps);
975 cio_write(cio, poc->prg, 1); /* Ppoc_i */
979 static void j2k_read_poc(opj_j2k_t *j2k) {
980 int len, numpchgs, i, old_poc;
982 int numcomps = j2k->image->numcomps;
984 opj_cp_t *cp = j2k->cp;
985 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
986 opj_cio_t *cio = j2k->cio;
988 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
990 len = cio_read(cio, 2); /* Lpoc */
991 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
993 for (i = old_poc; i < numpchgs + old_poc; i++) {
996 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
997 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
998 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
999 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
1000 poc->compno1 = int_min(
1001 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1002 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1005 tcp->numpocs = numpchgs + old_poc - 1;
1008 static void j2k_read_crg(opj_j2k_t *j2k) {
1009 int len, i, Xcrg_i, Ycrg_i;
1011 opj_cio_t *cio = j2k->cio;
1012 int numcomps = j2k->image->numcomps;
1014 len = cio_read(cio, 2); /* Lcrg */
1015 for (i = 0; i < numcomps; i++) {
1016 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1017 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1021 static void j2k_read_tlm(opj_j2k_t *j2k) {
1022 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1023 long int Ttlm_i, Ptlm_i;
1025 opj_cio_t *cio = j2k->cio;
1027 len = cio_read(cio, 2); /* Ltlm */
1028 Ztlm = cio_read(cio, 1); /* Ztlm */
1029 Stlm = cio_read(cio, 1); /* Stlm */
1030 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1031 SP = (Stlm >> 6) & 0x01;
1032 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1033 for (i = 0; i < tile_tlm; i++) {
1034 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1035 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1039 static void j2k_read_plm(opj_j2k_t *j2k) {
1040 int len, i, Zplm, Nplm, add, packet_len = 0;
1042 opj_cio_t *cio = j2k->cio;
1044 len = cio_read(cio, 2); /* Lplm */
1045 Zplm = cio_read(cio, 1); /* Zplm */
1048 Nplm = cio_read(cio, 4); /* Nplm */
1050 for (i = Nplm; i > 0; i--) {
1051 add = cio_read(cio, 1);
1053 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1054 if ((add & 0x80) == 0) {
1064 static void j2k_read_plt(opj_j2k_t *j2k) {
1065 int len, i, Zplt, packet_len = 0, add;
1067 opj_cio_t *cio = j2k->cio;
1069 len = cio_read(cio, 2); /* Lplt */
1070 Zplt = cio_read(cio, 1); /* Zplt */
1071 for (i = len - 3; i > 0; i--) {
1072 add = cio_read(cio, 1);
1073 packet_len = (packet_len << 7) + add; /* Iplt_i */
1074 if ((add & 0x80) == 0) {
1081 static void j2k_read_ppm(opj_j2k_t *j2k) {
1082 int len, Z_ppm, i, j;
1085 opj_cp_t *cp = j2k->cp;
1086 opj_cio_t *cio = j2k->cio;
1088 len = cio_read(cio, 2);
1091 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1094 if (cp->ppm_previous == 0) {
1095 N_ppm = cio_read(cio, 4); /* N_ppm */
1098 N_ppm = cp->ppm_previous;
1101 if (Z_ppm == 0) { /* First PPM marker */
1102 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1103 cp->ppm_data_first = cp->ppm_data;
1104 cp->ppm_len = N_ppm;
1105 } else { /* NON-first PPM marker */
1106 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1109 /* this memory allocation check could be done even in non-JPWL cases */
1111 if (!cp->ppm_data) {
1112 opj_event_msg(j2k->cinfo, EVT_ERROR,
1113 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1115 if (!JPWL_ASSUME || JPWL_ASSUME) {
1116 opj_free(cp->ppm_data);
1117 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1124 cp->ppm_data_first = cp->ppm_data;
1125 cp->ppm_len = N_ppm + cp->ppm_store;
1127 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1128 cp->ppm_data[j] = cio_read(cio, 1);
1132 break; /* Case of non-finished packet header in present marker but finished in next one */
1134 cp->ppm_previous = i - 1;
1139 static void j2k_read_ppt(opj_j2k_t *j2k) {
1140 int len, Z_ppt, i, j = 0;
1142 opj_cp_t *cp = j2k->cp;
1143 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1144 opj_cio_t *cio = j2k->cio;
1146 len = cio_read(cio, 2);
1147 Z_ppt = cio_read(cio, 1);
1149 if (Z_ppt == 0) { /* First PPT marker */
1150 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1151 tcp->ppt_data_first = tcp->ppt_data;
1153 tcp->ppt_len = len - 3;
1154 } else { /* NON-first PPT marker */
1155 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1156 tcp->ppt_data_first = tcp->ppt_data;
1157 tcp->ppt_len = len - 3 + tcp->ppt_store;
1160 for (i = len - 3; i > 0; i--) {
1161 tcp->ppt_data[j] = cio_read(cio, 1);
1167 static void j2k_write_tlm(opj_j2k_t *j2k){
1169 opj_cio_t *cio = j2k->cio;
1170 j2k->tlm_start = cio_tell(cio);
1171 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1172 lenp = 4 + (5*j2k->totnum_tp);
1173 cio_write(cio,lenp,2); /* Ltlm */
1174 cio_write(cio, 0,1); /* Ztlm=0*/
1175 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1176 cio_skip(cio,5*j2k->totnum_tp);
1179 static void j2k_write_sot(opj_j2k_t *j2k) {
1182 opj_cio_t *cio = j2k->cio;
1184 j2k->sot_start = cio_tell(cio);
1185 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1186 lenp = cio_tell(cio);
1187 cio_skip(cio, 2); /* Lsot (further) */
1188 cio_write(cio, j2k->curtileno, 2); /* Isot */
1189 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1190 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
1191 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1192 len = cio_tell(cio) - lenp;
1193 cio_seek(cio, lenp);
1194 cio_write(cio, len, 2); /* Lsot */
1195 cio_seek(cio, lenp + len);
1199 /* update markers struct */
1200 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1201 #endif /* USE_JPWL */
1205 static void j2k_read_sot(opj_j2k_t *j2k) {
1206 int len, tileno, totlen, partno, numparts, i;
1207 opj_tcp_t *tcp = NULL;
1210 opj_cp_t *cp = j2k->cp;
1211 opj_cio_t *cio = j2k->cio;
1213 len = cio_read(cio, 2);
1214 tileno = cio_read(cio, 2);
1217 if (j2k->cp->correct) {
1219 static int backup_tileno = 0;
1221 /* tileno is negative or larger than the number of tiles!!! */
1222 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
1223 opj_event_msg(j2k->cinfo, EVT_ERROR,
1224 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1225 tileno, (cp->tw * cp->th));
1227 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1230 /* we try to correct */
1231 tileno = backup_tileno;
1232 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1233 "- setting tile number to %d\n",
1237 /* keep your private count of tiles */
1240 #endif /* USE_JPWL */
1242 if (cp->tileno_size == 0) {
1243 cp->tileno[cp->tileno_size] = tileno;
1247 while (i < cp->tileno_size && status == 0) {
1248 status = cp->tileno[i] == tileno ? 1 : 0;
1252 cp->tileno[cp->tileno_size] = tileno;
1257 totlen = cio_read(cio, 4);
1260 if (j2k->cp->correct) {
1262 /* totlen is negative or larger than the bytes left!!! */
1263 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1264 opj_event_msg(j2k->cinfo, EVT_ERROR,
1265 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1266 totlen, cio_numbytesleft(cio) + 8);
1268 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1271 /* we try to correct */
1273 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1274 "- setting Psot to %d => assuming it is the last tile\n",
1279 #endif /* USE_JPWL */
1282 totlen = cio_numbytesleft(cio) + 8;
1284 partno = cio_read(cio, 1);
1285 numparts = cio_read(cio, 1);
1287 if (partno >= numparts) {
1288 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);
1289 numparts = partno+1;
1292 j2k->curtileno = tileno;
1293 j2k->cur_tp_num = partno;
1294 j2k->eot = cio_getbp(cio) - 12 + totlen;
1295 j2k->state = J2K_STATE_TPH;
1296 tcp = &cp->tcps[j2k->curtileno];
1299 if (j2k->cstr_info) {
1302 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1303 j2k->cstr_info->tile[tileno].tileno = tileno;
1304 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1305 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
1307 j2k->cstr_info->tile[tileno].end_pos += totlen;
1309 j2k->cstr_info->tile[tileno].num_tps = numparts;
1311 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
1313 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)
1314 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1315 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1316 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1319 if (tcp->first == 1) {
1320 /* Initialization PPT */
1321 opj_tccp_t *tmp = tcp->tccps;
1322 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1324 tcp->ppt_data = NULL;
1325 tcp->ppt_data_first = NULL;
1328 for (i = 0; i < j2k->image->numcomps; i++) {
1329 tcp->tccps[i] = j2k->default_tcp->tccps[i];
1331 cp->tcps[j2k->curtileno].first = 0;
1335 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
1338 opj_tcp_t *tcp = NULL;
1339 opj_codestream_info_t *cstr_info = NULL;
1341 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1342 opj_cp_t *cp = j2k->cp;
1343 opj_cio_t *cio = j2k->cio;
1345 tcd->tp_num = j2k->tp_num ;
1346 tcd->cur_tp_num = j2k->cur_tp_num;
1348 cio_write(cio, J2K_MS_SOD, 2);
1349 if (j2k->curtileno == 0) {
1350 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1354 cstr_info = j2k->cstr_info;
1356 if (!j2k->cur_tp_num ) {
1357 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1358 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1361 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
1362 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
1366 /* update markers struct */
1367 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1368 #endif /* USE_JPWL */
1373 tcp = &cp->tcps[j2k->curtileno];
1374 for (layno = 0; layno < tcp->numlayers; layno++) {
1375 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
1376 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
1377 } else if (tcp->rates[layno]) {
1378 tcp->rates[layno]=1;
1381 if(j2k->cur_tp_num == 0){
1382 tcd->tcd_image->tiles->packno = 0;
1384 cstr_info->packno = 0;
1387 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
1389 /* Writing Psot in SOT marker */
1390 totlen = cio_tell(cio) + l - j2k->sot_start;
1391 cio_seek(cio, j2k->sot_start + 6);
1392 cio_write(cio, totlen, 4);
1393 cio_seek(cio, j2k->sot_start + totlen);
1394 /* Writing Ttlm and Ptlm in TLM marker */
1396 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
1397 cio_write(cio, j2k->curtileno, 1);
1398 cio_write(cio, totlen, 4);
1400 cio_seek(cio, j2k->sot_start + totlen);
1403 static void j2k_read_sod(opj_j2k_t *j2k) {
1404 int len, truncate = 0, i;
1405 unsigned char *data = NULL, *data_ptr = NULL;
1407 opj_cio_t *cio = j2k->cio;
1408 int curtileno = j2k->curtileno;
1411 if (j2k->cstr_info) {
1412 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1413 cio_tell(cio) + j2k->pos_correction - 1;
1414 if (j2k->cur_tp_num == 0)
1415 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1416 j2k->cstr_info->packno = 0;
1419 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1421 if (len == cio_numbytesleft(cio) + 1) {
1422 truncate = 1; /* Case of a truncate codestream */
1425 data = j2k->tile_data[curtileno];
1426 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1428 data_ptr = data + j2k->tile_len[curtileno];
1429 for (i = 0; i < len; i++) {
1430 data_ptr[i] = cio_read(cio, 1);
1433 j2k->tile_len[curtileno] += len;
1434 j2k->tile_data[curtileno] = data;
1437 j2k->state = J2K_STATE_TPHSOT;
1439 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1444 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1445 opj_cp_t *cp = j2k->cp;
1446 opj_tcp_t *tcp = &cp->tcps[tileno];
1447 opj_cio_t *cio = j2k->cio;
1448 int numcomps = j2k->image->numcomps;
1450 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1451 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1452 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1453 cio_write(cio, 0, 1); /* Srgn */
1454 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1457 static void j2k_read_rgn(opj_j2k_t *j2k) {
1458 int len, compno, roisty;
1460 opj_cp_t *cp = j2k->cp;
1461 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1462 opj_cio_t *cio = j2k->cio;
1463 int numcomps = j2k->image->numcomps;
1465 len = cio_read(cio, 2); /* Lrgn */
1466 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1467 roisty = cio_read(cio, 1); /* Srgn */
1470 if (j2k->cp->correct) {
1471 /* totlen is negative or larger than the bytes left!!! */
1472 if (compno >= numcomps) {
1473 opj_event_msg(j2k->cinfo, EVT_ERROR,
1474 "JPWL: bad component number in RGN (%d when there are only %d)\n",
1476 if (!JPWL_ASSUME || JPWL_ASSUME) {
1477 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1482 #endif /* USE_JPWL */
1484 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1487 static void j2k_write_eoc(opj_j2k_t *j2k) {
1488 opj_cio_t *cio = j2k->cio;
1489 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1490 cio_write(cio, J2K_MS_EOC, 2);
1494 /* update markers struct */
1495 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1496 #endif /* USE_JPWL */
1500 static void j2k_read_eoc(opj_j2k_t *j2k) {
1504 /* if packets should be decoded */
1505 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1506 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1507 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1508 for (i = 0; i < j2k->cp->tileno_size; i++) {
1509 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1510 tileno = j2k->cp->tileno[i];
1511 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
1512 opj_free(j2k->tile_data[tileno]);
1513 j2k->tile_data[tileno] = NULL;
1514 tcd_free_decode_tile(tcd, i);
1515 if (success == OPJ_FALSE) {
1516 j2k->state |= J2K_STATE_ERR;
1520 tcd_free_decode(tcd);
1523 /* if packets should not be decoded */
1525 for (i = 0; i < j2k->cp->tileno_size; i++) {
1526 tileno = j2k->cp->tileno[i];
1527 opj_free(j2k->tile_data[tileno]);
1528 j2k->tile_data[tileno] = NULL;
1531 if (j2k->state & J2K_STATE_ERR)
1532 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1534 j2k->state = J2K_STATE_MT;
1537 typedef struct opj_dec_mstabent {
1540 /** value of the state when the marker can appear */
1542 /** action linked to the marker */
1543 void (*handler) (opj_j2k_t *j2k);
1544 } opj_dec_mstabent_t;
1546 opj_dec_mstabent_t j2k_dec_mstab[] = {
1547 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1548 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1549 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1550 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1551 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1552 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1553 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1554 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1555 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1556 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1557 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1558 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1559 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1560 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1561 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1562 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1564 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1565 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1568 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1569 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1570 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1571 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1572 #endif /* USE_JPWL */
1574 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1575 {J2K_MS_INSEC, 0, j2k_read_insec},
1576 #endif /* USE_JPSEC */
1578 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1581 static void j2k_read_unk(opj_j2k_t *j2k) {
1582 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1585 if (j2k->cp->correct) {
1587 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1588 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1589 id = cio_read(j2k->cio, 2);
1590 opj_event_msg(j2k->cinfo, EVT_ERROR,
1591 "JPWL: really don't know this marker %x\n",
1594 opj_event_msg(j2k->cinfo, EVT_ERROR,
1595 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
1598 /* OK, activate this at your own risk!!! */
1599 /* we look for the marker at the minimum hamming distance from this */
1600 while (j2k_dec_mstab[m].id) {
1602 /* 1's where they differ */
1603 tmp_id = j2k_dec_mstab[m].id ^ id;
1605 /* compute the hamming distance between our id and the current */
1607 for (i = 0; i < 16; i++) {
1608 if ((tmp_id >> i) & 0x0001) {
1613 /* if current distance is smaller, set the minimum */
1614 if (cur_dist < min_dist) {
1615 min_dist = cur_dist;
1616 min_id = j2k_dec_mstab[m].id;
1619 /* jump to the next marker */
1623 /* do we substitute the marker? */
1624 if (min_dist < JPWL_MAXIMUM_HAMMING) {
1625 opj_event_msg(j2k->cinfo, EVT_ERROR,
1626 "- marker %x is at distance %d from the read %x\n",
1627 min_id, min_dist, id);
1628 opj_event_msg(j2k->cinfo, EVT_ERROR,
1629 "- trying to substitute in place and crossing fingers!\n");
1630 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1631 cio_write(j2k->cio, min_id, 2);
1634 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1639 #endif /* USE_JPWL */
1644 Read the lookup table containing all the marker, status and action
1645 @param id Marker value
1647 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1648 opj_dec_mstabent_t *e;
1649 for (e = j2k_dec_mstab; e->id != 0; e++) {
1657 /* ----------------------------------------------------------------------- */
1658 /* J2K / JPT decoder interface */
1659 /* ----------------------------------------------------------------------- */
1661 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1662 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1666 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1667 if(!j2k->default_tcp) {
1673 j2k->tile_data = NULL;
1678 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1681 if(j2k->tile_len != NULL) {
1682 opj_free(j2k->tile_len);
1684 if(j2k->tile_data != NULL) {
1685 opj_free(j2k->tile_data);
1687 if(j2k->default_tcp != NULL) {
1688 opj_tcp_t *default_tcp = j2k->default_tcp;
1689 if(default_tcp->ppt_data_first != NULL) {
1690 opj_free(default_tcp->ppt_data_first);
1692 if(j2k->default_tcp->tccps != NULL) {
1693 opj_free(j2k->default_tcp->tccps);
1695 opj_free(j2k->default_tcp);
1697 if(j2k->cp != NULL) {
1698 opj_cp_t *cp = j2k->cp;
1699 if(cp->tcps != NULL) {
1700 for(i = 0; i < cp->tw * cp->th; i++) {
1701 if(cp->tcps[i].ppt_data_first != NULL) {
1702 opj_free(cp->tcps[i].ppt_data_first);
1704 if(cp->tcps[i].tccps != NULL) {
1705 opj_free(cp->tcps[i].tccps);
1710 if(cp->ppm_data_first != NULL) {
1711 opj_free(cp->ppm_data_first);
1713 if(cp->tileno != NULL) {
1714 opj_free(cp->tileno);
1716 if(cp->comment != NULL) {
1717 opj_free(cp->comment);
1725 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1726 if(j2k && parameters) {
1727 /* create and initialize the coding parameters structure */
1728 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1729 cp->reduce = parameters->cp_reduce;
1730 cp->layer = parameters->cp_layer;
1731 cp->limit_decoding = parameters->cp_limit_decoding;
1734 cp->correct = parameters->jpwl_correct;
1735 cp->exp_comps = parameters->jpwl_exp_comps;
1736 cp->max_tiles = parameters->jpwl_max_tiles;
1737 #endif /* USE_JPWL */
1740 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1745 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1746 opj_image_t *image = NULL;
1748 opj_common_ptr cinfo = j2k->cinfo;
1751 j2k->cstr_info = cstr_info;
1753 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
1755 /* create an empty image */
1756 image = opj_image_create0();
1759 j2k->state = J2K_STATE_MHSOC;
1762 opj_dec_mstabent_t *e;
1763 int id = cio_read(cio, 2);
1766 /* we try to honor JPWL correction power */
1767 if (j2k->cp->correct) {
1769 int orig_pos = cio_tell(cio);
1772 /* call the corrector */
1773 status = jpwl_correct(j2k);
1775 /* go back to where you were */
1776 cio_seek(cio, orig_pos - 2);
1778 /* re-read the marker */
1779 id = cio_read(cio, 2);
1781 /* check whether it begins with ff */
1782 if (id >> 8 != 0xff) {
1783 opj_event_msg(cinfo, EVT_ERROR,
1784 "JPWL: possible bad marker %x at %d\n",
1785 id, cio_tell(cio) - 2);
1787 opj_image_destroy(image);
1788 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
1791 /* we try to correct */
1793 cio_seek(cio, cio_tell(cio) - 2);
1794 cio_write(cio, id, 2);
1795 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
1796 "- setting marker to %x\n",
1801 #endif /* USE_JPWL */
1803 if (id >> 8 != 0xff) {
1804 opj_image_destroy(image);
1805 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1808 e = j2k_dec_mstab_lookup(id);
1809 // Check if the marker is known
1810 if (!(j2k->state & e->states)) {
1811 opj_image_destroy(image);
1812 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1815 // Check if the decoding is limited to the main header
1816 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
1817 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
1824 if (j2k->state & J2K_STATE_ERR)
1827 if (j2k->state == J2K_STATE_MT) {
1830 if (j2k->state == J2K_STATE_NEOC) {
1834 if (j2k->state == J2K_STATE_NEOC) {
1838 if (j2k->state != J2K_STATE_MT) {
1839 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1846 * Read a JPT-stream and decode file
1849 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1850 opj_image_t *image = NULL;
1851 opj_jpt_msg_header_t header;
1854 OPJ_ARG_NOT_USED(cstr_info);
1856 opj_common_ptr cinfo = j2k->cinfo;
1859 /* create an empty image */
1860 image = opj_image_create0();
1863 j2k->state = J2K_STATE_MHSOC;
1865 /* Initialize the header */
1866 jpt_init_msg_header(&header);
1867 /* Read the first header of the message */
1868 jpt_read_msg_header(cinfo, cio, &header);
1870 position = cio_tell(cio);
1871 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
1872 opj_image_destroy(image);
1873 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1878 opj_dec_mstabent_t *e = NULL;
1881 if (!cio_numbytesleft(cio)) {
1885 /* data-bin read -> need to read a new header */
1886 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1887 jpt_read_msg_header(cinfo, cio, &header);
1888 position = cio_tell(cio);
1889 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
1890 opj_image_destroy(image);
1891 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1896 id = cio_read(cio, 2);
1897 if (id >> 8 != 0xff) {
1898 opj_image_destroy(image);
1899 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1902 e = j2k_dec_mstab_lookup(id);
1903 if (!(j2k->state & e->states)) {
1904 opj_image_destroy(image);
1905 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1911 if (j2k->state == J2K_STATE_MT) {
1914 if (j2k->state == J2K_STATE_NEOC) {
1918 if (j2k->state == J2K_STATE_NEOC) {
1922 if (j2k->state != J2K_STATE_MT) {
1923 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1929 /* ----------------------------------------------------------------------- */
1930 /* J2K encoder interface */
1931 /* ----------------------------------------------------------------------- */
1933 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1934 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1941 void j2k_destroy_compress(opj_j2k_t *j2k) {
1945 if(j2k->cp != NULL) {
1946 opj_cp_t *cp = j2k->cp;
1949 opj_free(cp->comment);
1952 opj_free(cp->matrice);
1954 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1955 opj_free(cp->tcps[tileno].tccps);
1964 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1965 int i, j, tileno, numpocs_tile;
1966 opj_cp_t *cp = NULL;
1968 if(!j2k || !parameters || ! image) {
1972 /* create and initialize the coding parameters structure */
1973 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1975 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1978 /* set default values for cp */
1983 copy user encoding parameters
1985 cp->cinema = parameters->cp_cinema;
1986 cp->max_comp_size = parameters->max_comp_size;
1987 cp->rsiz = parameters->cp_rsiz;
1988 cp->disto_alloc = parameters->cp_disto_alloc;
1989 cp->fixed_alloc = parameters->cp_fixed_alloc;
1990 cp->fixed_quality = parameters->cp_fixed_quality;
1992 /* mod fixed_quality */
1993 if(parameters->cp_matrice) {
1994 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
1995 cp->matrice = (int *) opj_malloc(array_size);
1996 memcpy(cp->matrice, parameters->cp_matrice, array_size);
2000 cp->tdx = parameters->cp_tdx;
2001 cp->tdy = parameters->cp_tdy;
2004 cp->tx0 = parameters->cp_tx0;
2005 cp->ty0 = parameters->cp_ty0;
2007 /* comment string */
2008 if(parameters->cp_comment) {
2009 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2011 strcpy(cp->comment, parameters->cp_comment);
2016 calculate other encoding parameters
2019 if (parameters->tile_size_on) {
2020 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2021 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2023 cp->tdx = image->x1 - cp->tx0;
2024 cp->tdy = image->y1 - cp->ty0;
2027 if(parameters->tp_on){
2028 cp->tp_flag = parameters->tp_flag;
2033 for(i=0;i<image->numcomps ;i++){
2034 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
2040 calculate JPWL encoding parameters
2043 if (parameters->jpwl_epc_on) {
2047 cp->epc_on = OPJ_TRUE;
2048 cp->info_on = OPJ_FALSE; /* no informative technique */
2051 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2052 cp->epb_on = OPJ_TRUE;
2054 cp->hprot_MH = parameters->jpwl_hprot_MH;
2055 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2056 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2057 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2059 /* if tile specs are not specified, copy MH specs */
2060 if (cp->hprot_TPH[0] == -1) {
2061 cp->hprot_TPH_tileno[0] = 0;
2062 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2064 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2065 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2066 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2067 cp->pprot[i] = parameters->jpwl_pprot[i];
2071 /* set ESD writing */
2072 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2073 cp->esd_on = OPJ_TRUE;
2075 cp->sens_size = parameters->jpwl_sens_size;
2076 cp->sens_addr = parameters->jpwl_sens_addr;
2077 cp->sens_range = parameters->jpwl_sens_range;
2079 cp->sens_MH = parameters->jpwl_sens_MH;
2080 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2081 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2082 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2086 /* always set RED writing to false: we are at the encoder */
2087 cp->red_on = OPJ_FALSE;
2090 cp->epc_on = OPJ_FALSE;
2092 #endif /* USE_JPWL */
2095 /* initialize the mutiple tiles */
2096 /* ---------------------------- */
2097 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2099 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2100 opj_tcp_t *tcp = &cp->tcps[tileno];
2101 tcp->numlayers = parameters->tcp_numlayers;
2102 for (j = 0; j < tcp->numlayers; j++) {
2104 if (cp->fixed_quality) {
2105 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2107 tcp->rates[j] = parameters->tcp_rates[j];
2109 if (cp->fixed_quality) { /* add fixed_quality */
2110 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2112 tcp->rates[j] = parameters->tcp_rates[j];
2116 tcp->csty = parameters->csty;
2117 tcp->prg = parameters->prog_order;
2118 tcp->mct = parameters->tcp_mct;
2122 if (parameters->numpocs) {
2123 /* initialisation of POC */
2125 for (i = 0; i < parameters->numpocs; i++) {
2126 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2127 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2128 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2129 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2130 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2131 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2132 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2133 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2134 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2138 tcp->numpocs = numpocs_tile -1 ;
2143 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2145 for (i = 0; i < image->numcomps; i++) {
2146 opj_tccp_t *tccp = &tcp->tccps[i];
2147 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
2148 tccp->numresolutions = parameters->numresolution;
2149 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2150 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2151 tccp->cblksty = parameters->mode;
2152 tccp->qmfbid = parameters->irreversible ? 0 : 1;
2153 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
2155 if (i == parameters->roi_compno) {
2156 tccp->roishift = parameters->roi_shift;
2161 if(parameters->cp_cinema)
2163 //Precinct size for lowest frequency subband=128
2166 //Precinct size at all other resolutions = 256
2167 for (j = 1; j < tccp->numresolutions; j++) {
2172 if (parameters->csty & J2K_CCP_CSTY_PRT) {
2174 for (j = tccp->numresolutions - 1; j >= 0; j--) {
2175 if (p < parameters->res_spec) {
2177 if (parameters->prcw_init[p] < 1) {
2180 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2183 if (parameters->prch_init[p] < 1) {
2186 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2190 int res_spec = parameters->res_spec;
2191 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2192 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2194 if (size_prcw < 1) {
2197 tccp->prcw[j] = int_floorlog2(size_prcw);
2200 if (size_prch < 1) {
2203 tccp->prch[j] = int_floorlog2(size_prch);
2207 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2210 for (j = 0; j < tccp->numresolutions; j++) {
2217 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2222 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2224 opj_cp_t *cp = NULL;
2226 opj_tcd_t *tcd = NULL; /* TCD component */
2234 j2k->cstr_info = cstr_info;
2237 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2238 cstr_info->image_w = image->x1 - image->x0;
2239 cstr_info->image_h = image->y1 - image->y0;
2240 cstr_info->prog = (&cp->tcps[0])->prg;
2241 cstr_info->tw = cp->tw;
2242 cstr_info->th = cp->th;
2243 cstr_info->tile_x = cp->tdx; /* new version parser */
2244 cstr_info->tile_y = cp->tdy; /* new version parser */
2245 cstr_info->tile_Ox = cp->tx0; /* new version parser */
2246 cstr_info->tile_Oy = cp->ty0; /* new version parser */
2247 cstr_info->numcomps = image->numcomps;
2248 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2249 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2250 for (compno=0; compno < image->numcomps; compno++) {
2251 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2253 cstr_info->D_max = 0.0; /* ADD Marcela */
2254 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2255 cstr_info->maxmarknum = 100;
2256 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
2257 cstr_info->marknum = 0;
2267 for (compno = 1; compno < image->numcomps; compno++) {
2268 j2k_write_coc(j2k, compno);
2269 j2k_write_qcc(j2k, compno);
2273 for (compno = 0; compno < image->numcomps; compno++) {
2274 opj_tcp_t *tcp = &cp->tcps[0];
2275 if (tcp->tccps[compno].roishift)
2276 j2k_write_rgn(j2k, compno, 0);
2278 if (cp->comment != NULL) {
2282 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2286 if (cp->cinema == CINEMA4K_24) {
2291 /* uncomment only for testing JPSEC marker writing */
2292 /* j2k_write_sec(j2k); */
2296 cstr_info->main_head_end = cio_tell(cio) - 1;
2299 /**** Main Header ENDS here ***/
2301 /* create the tile encoder */
2302 tcd = tcd_create(j2k->cinfo);
2304 /* encode each tile */
2305 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2309 int acc_pack_num = 0;
2313 opj_tcp_t *tcp = &cp->tcps[tileno];
2314 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2316 j2k->curtileno = tileno;
2317 j2k->cur_tp_num = 0;
2318 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2319 /* initialisation before tile encoding */
2321 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2323 tcd_init_encode(tcd, image, cp, j2k->curtileno);
2328 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2332 for(pino = 0; pino <= tcp->numpocs; pino++) {
2336 /*Get number of tile parts*/
2337 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2338 tcd->tp_pos = cp->tp_pos;
2340 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2341 j2k->tp_num = tilepartno;
2344 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2345 cio_tell(cio) + j2k->pos_correction;
2349 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2350 for (compno = 1; compno < image->numcomps; compno++) {
2351 j2k_write_coc(j2k, compno);
2352 j2k_write_qcc(j2k, compno);
2354 if (cp->tcps[tileno].numpocs) {
2361 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2362 cio_tell(cio) + j2k->pos_correction + 1;
2365 j2k_write_sod(j2k, tcd);
2369 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2370 cio_tell(cio) + j2k->pos_correction - 1;
2371 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2373 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2374 cstr_info->packno - acc_pack_num;
2375 acc_pack_num = cstr_info->packno;
2383 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2388 if (tile->PPT) { // BAD PPT !!!
2391 PPT_file=fopen("PPT","rb");
2392 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2393 for (i=0;i<tile->len_ppt;i++) {
2395 fread(&elmt, 1, 1, PPT_file);
2396 fwrite(&elmt,1,1,f);
2405 /* destroy the tile encoder */
2406 tcd_free_encode(tcd);
2409 opj_free(j2k->cur_totnum_tp);
2414 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2416 /* The following adjustment is done to adjust the codestream size */
2417 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2418 /* the first bunch of bytes is not in the codestream */
2419 cstr_info->codestream_size -= cstr_info->main_head_start;
2425 preparation of JPWL marker segments
2429 /* encode according to JPWL */
2430 jpwl_encode(j2k, cio, image);
2433 #endif /* USE_JPWL */