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",
843 /* We check whether there are too many subbands */
844 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
845 opj_event_msg(j2k->cinfo, EVT_WARNING ,
846 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
847 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
850 #endif /* USE_JPWL */
852 for (bandno = 0; bandno < numbands; bandno++) {
854 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
855 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
858 tmp = cio_read(cio, 2); /* SPqcx_i */
862 if (bandno < J2K_MAXBANDS){
863 tccp->stepsizes[bandno].expn = expn;
864 tccp->stepsizes[bandno].mant = mant;
868 /* Add Antonin : if scalar_derived -> compute other stepsizes */
869 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
870 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
871 tccp->stepsizes[bandno].expn =
872 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
873 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
874 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
880 static void j2k_write_qcd(opj_j2k_t *j2k) {
883 opj_cio_t *cio = j2k->cio;
885 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
886 lenp = cio_tell(cio);
888 j2k_write_qcx(j2k, 0);
889 len = cio_tell(cio) - lenp;
891 cio_write(cio, len, 2); /* Lqcd */
892 cio_seek(cio, lenp + len);
895 static void j2k_read_qcd(opj_j2k_t *j2k) {
898 opj_cio_t *cio = j2k->cio;
899 opj_image_t *image = j2k->image;
901 len = cio_read(cio, 2); /* Lqcd */
903 for (i = 0; i < image->numcomps; i++) {
905 j2k_read_qcx(j2k, i, len - 2);
909 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
912 opj_cio_t *cio = j2k->cio;
914 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
915 lenp = cio_tell(cio);
917 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
918 j2k_write_qcx(j2k, compno);
919 len = cio_tell(cio) - lenp;
921 cio_write(cio, len, 2); /* Lqcc */
922 cio_seek(cio, lenp + len);
925 static void j2k_read_qcc(opj_j2k_t *j2k) {
927 int numcomp = j2k->image->numcomps;
928 opj_cio_t *cio = j2k->cio;
930 len = cio_read(cio, 2); /* Lqcc */
931 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
934 if (j2k->cp->correct) {
936 static int backup_compno = 0;
938 /* compno is negative or larger than the number of components!!! */
939 if ((compno < 0) || (compno >= numcomp)) {
940 opj_event_msg(j2k->cinfo, EVT_ERROR,
941 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
944 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
947 /* we try to correct */
948 compno = backup_compno % numcomp;
949 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
950 "- setting component number to %d\n",
954 /* keep your private count of tiles */
957 #endif /* USE_JPWL */
959 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
962 static void j2k_write_poc(opj_j2k_t *j2k) {
963 int len, numpchgs, i;
965 int numcomps = j2k->image->numcomps;
967 opj_cp_t *cp = j2k->cp;
968 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
969 opj_tccp_t *tccp = &tcp->tccps[0];
970 opj_cio_t *cio = j2k->cio;
972 numpchgs = 1 + tcp->numpocs;
973 cio_write(cio, J2K_MS_POC, 2); /* POC */
974 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
975 cio_write(cio, len, 2); /* Lpoc */
976 for (i = 0; i < numpchgs; i++) {
977 opj_poc_t *poc = &tcp->pocs[i];
978 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
979 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
980 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
981 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
982 cio_write(cio, poc->resno1, 1); /* REpoc_i */
983 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
984 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
985 poc->compno1 = int_min(poc->compno1, numcomps);
986 cio_write(cio, poc->prg, 1); /* Ppoc_i */
990 static void j2k_read_poc(opj_j2k_t *j2k) {
991 int len, numpchgs, i, old_poc;
993 int numcomps = j2k->image->numcomps;
995 opj_cp_t *cp = j2k->cp;
996 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
997 opj_cio_t *cio = j2k->cio;
999 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1001 len = cio_read(cio, 2); /* Lpoc */
1002 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1004 for (i = old_poc; i < numpchgs + old_poc; i++) {
1006 poc = &tcp->pocs[i];
1007 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1008 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1009 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
1010 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
1011 poc->compno1 = int_min(
1012 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1013 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1016 tcp->numpocs = numpchgs + old_poc - 1;
1019 static void j2k_read_crg(opj_j2k_t *j2k) {
1020 int len, i, Xcrg_i, Ycrg_i;
1022 opj_cio_t *cio = j2k->cio;
1023 int numcomps = j2k->image->numcomps;
1025 len = cio_read(cio, 2); /* Lcrg */
1026 for (i = 0; i < numcomps; i++) {
1027 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1028 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1032 static void j2k_read_tlm(opj_j2k_t *j2k) {
1033 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1034 long int Ttlm_i, Ptlm_i;
1036 opj_cio_t *cio = j2k->cio;
1038 len = cio_read(cio, 2); /* Ltlm */
1039 Ztlm = cio_read(cio, 1); /* Ztlm */
1040 Stlm = cio_read(cio, 1); /* Stlm */
1041 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1042 SP = (Stlm >> 6) & 0x01;
1043 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1044 for (i = 0; i < tile_tlm; i++) {
1045 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1046 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1050 static void j2k_read_plm(opj_j2k_t *j2k) {
1051 int len, i, Zplm, Nplm, add, packet_len = 0;
1053 opj_cio_t *cio = j2k->cio;
1055 len = cio_read(cio, 2); /* Lplm */
1056 Zplm = cio_read(cio, 1); /* Zplm */
1059 Nplm = cio_read(cio, 4); /* Nplm */
1061 for (i = Nplm; i > 0; i--) {
1062 add = cio_read(cio, 1);
1064 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1065 if ((add & 0x80) == 0) {
1075 static void j2k_read_plt(opj_j2k_t *j2k) {
1076 int len, i, Zplt, packet_len = 0, add;
1078 opj_cio_t *cio = j2k->cio;
1080 len = cio_read(cio, 2); /* Lplt */
1081 Zplt = cio_read(cio, 1); /* Zplt */
1082 for (i = len - 3; i > 0; i--) {
1083 add = cio_read(cio, 1);
1084 packet_len = (packet_len << 7) + add; /* Iplt_i */
1085 if ((add & 0x80) == 0) {
1092 static void j2k_read_ppm(opj_j2k_t *j2k) {
1093 int len, Z_ppm, i, j;
1096 opj_cp_t *cp = j2k->cp;
1097 opj_cio_t *cio = j2k->cio;
1099 len = cio_read(cio, 2);
1102 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1105 if (cp->ppm_previous == 0) {
1106 N_ppm = cio_read(cio, 4); /* N_ppm */
1109 N_ppm = cp->ppm_previous;
1112 if (Z_ppm == 0) { /* First PPM marker */
1113 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1114 cp->ppm_data_first = cp->ppm_data;
1115 cp->ppm_len = N_ppm;
1116 } else { /* NON-first PPM marker */
1117 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1120 /* this memory allocation check could be done even in non-JPWL cases */
1122 if (!cp->ppm_data) {
1123 opj_event_msg(j2k->cinfo, EVT_ERROR,
1124 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1126 if (!JPWL_ASSUME || JPWL_ASSUME) {
1127 opj_free(cp->ppm_data);
1128 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1135 cp->ppm_data_first = cp->ppm_data;
1136 cp->ppm_len = N_ppm + cp->ppm_store;
1138 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1139 cp->ppm_data[j] = cio_read(cio, 1);
1143 break; /* Case of non-finished packet header in present marker but finished in next one */
1145 cp->ppm_previous = i - 1;
1150 static void j2k_read_ppt(opj_j2k_t *j2k) {
1151 int len, Z_ppt, i, j = 0;
1153 opj_cp_t *cp = j2k->cp;
1154 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1155 opj_cio_t *cio = j2k->cio;
1157 len = cio_read(cio, 2);
1158 Z_ppt = cio_read(cio, 1);
1160 if (Z_ppt == 0) { /* First PPT marker */
1161 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1162 tcp->ppt_data_first = tcp->ppt_data;
1164 tcp->ppt_len = len - 3;
1165 } else { /* NON-first PPT marker */
1166 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1167 tcp->ppt_data_first = tcp->ppt_data;
1168 tcp->ppt_len = len - 3 + tcp->ppt_store;
1171 for (i = len - 3; i > 0; i--) {
1172 tcp->ppt_data[j] = cio_read(cio, 1);
1178 static void j2k_write_tlm(opj_j2k_t *j2k){
1180 opj_cio_t *cio = j2k->cio;
1181 j2k->tlm_start = cio_tell(cio);
1182 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1183 lenp = 4 + (5*j2k->totnum_tp);
1184 cio_write(cio,lenp,2); /* Ltlm */
1185 cio_write(cio, 0,1); /* Ztlm=0*/
1186 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1187 cio_skip(cio,5*j2k->totnum_tp);
1190 static void j2k_write_sot(opj_j2k_t *j2k) {
1193 opj_cio_t *cio = j2k->cio;
1195 j2k->sot_start = cio_tell(cio);
1196 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1197 lenp = cio_tell(cio);
1198 cio_skip(cio, 2); /* Lsot (further) */
1199 cio_write(cio, j2k->curtileno, 2); /* Isot */
1200 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1201 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
1202 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1203 len = cio_tell(cio) - lenp;
1204 cio_seek(cio, lenp);
1205 cio_write(cio, len, 2); /* Lsot */
1206 cio_seek(cio, lenp + len);
1210 /* update markers struct */
1211 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
1212 #endif /* USE_JPWL */
1216 static void j2k_read_sot(opj_j2k_t *j2k) {
1217 int len, tileno, totlen, partno, numparts, i;
1218 opj_tcp_t *tcp = NULL;
1221 opj_cp_t *cp = j2k->cp;
1222 opj_cio_t *cio = j2k->cio;
1224 len = cio_read(cio, 2);
1225 tileno = cio_read(cio, 2);
1228 if (j2k->cp->correct) {
1230 static int backup_tileno = 0;
1232 /* tileno is negative or larger than the number of tiles!!! */
1233 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
1234 opj_event_msg(j2k->cinfo, EVT_ERROR,
1235 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1236 tileno, (cp->tw * cp->th));
1238 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1241 /* we try to correct */
1242 tileno = backup_tileno;
1243 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1244 "- setting tile number to %d\n",
1248 /* keep your private count of tiles */
1251 #endif /* USE_JPWL */
1253 if (cp->tileno_size == 0) {
1254 cp->tileno[cp->tileno_size] = tileno;
1258 while (i < cp->tileno_size && status == 0) {
1259 status = cp->tileno[i] == tileno ? 1 : 0;
1263 cp->tileno[cp->tileno_size] = tileno;
1268 totlen = cio_read(cio, 4);
1271 if (j2k->cp->correct) {
1273 /* totlen is negative or larger than the bytes left!!! */
1274 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1275 opj_event_msg(j2k->cinfo, EVT_ERROR,
1276 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1277 totlen, cio_numbytesleft(cio) + 8);
1279 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1282 /* we try to correct */
1284 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1285 "- setting Psot to %d => assuming it is the last tile\n",
1290 #endif /* USE_JPWL */
1293 totlen = cio_numbytesleft(cio) + 8;
1295 partno = cio_read(cio, 1);
1296 numparts = cio_read(cio, 1);
1298 if (partno >= numparts) {
1299 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);
1300 numparts = partno+1;
1303 j2k->curtileno = tileno;
1304 j2k->cur_tp_num = partno;
1305 j2k->eot = cio_getbp(cio) - 12 + totlen;
1306 j2k->state = J2K_STATE_TPH;
1307 tcp = &cp->tcps[j2k->curtileno];
1310 if (j2k->cstr_info) {
1313 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
1314 j2k->cstr_info->tile[tileno].tileno = tileno;
1315 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
1316 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
1318 j2k->cstr_info->tile[tileno].end_pos += totlen;
1320 j2k->cstr_info->tile[tileno].num_tps = numparts;
1322 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
1324 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)
1325 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
1326 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
1327 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
1330 if (tcp->first == 1) {
1331 /* Initialization PPT */
1332 opj_tccp_t *tmp = tcp->tccps;
1333 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1335 tcp->ppt_data = NULL;
1336 tcp->ppt_data_first = NULL;
1339 for (i = 0; i < j2k->image->numcomps; i++) {
1340 tcp->tccps[i] = j2k->default_tcp->tccps[i];
1342 cp->tcps[j2k->curtileno].first = 0;
1346 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
1349 opj_tcp_t *tcp = NULL;
1350 opj_codestream_info_t *cstr_info = NULL;
1352 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1353 opj_cp_t *cp = j2k->cp;
1354 opj_cio_t *cio = j2k->cio;
1356 tcd->tp_num = j2k->tp_num ;
1357 tcd->cur_tp_num = j2k->cur_tp_num;
1359 cio_write(cio, J2K_MS_SOD, 2);
1360 if (j2k->curtileno == 0) {
1361 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1365 cstr_info = j2k->cstr_info;
1367 if (!j2k->cur_tp_num ) {
1368 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1369 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
1372 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
1373 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
1377 /* update markers struct */
1378 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
1379 #endif /* USE_JPWL */
1384 tcp = &cp->tcps[j2k->curtileno];
1385 for (layno = 0; layno < tcp->numlayers; layno++) {
1386 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
1387 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
1388 } else if (tcp->rates[layno]) {
1389 tcp->rates[layno]=1;
1392 if(j2k->cur_tp_num == 0){
1393 tcd->tcd_image->tiles->packno = 0;
1395 cstr_info->packno = 0;
1398 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
1400 /* Writing Psot in SOT marker */
1401 totlen = cio_tell(cio) + l - j2k->sot_start;
1402 cio_seek(cio, j2k->sot_start + 6);
1403 cio_write(cio, totlen, 4);
1404 cio_seek(cio, j2k->sot_start + totlen);
1405 /* Writing Ttlm and Ptlm in TLM marker */
1407 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
1408 cio_write(cio, j2k->curtileno, 1);
1409 cio_write(cio, totlen, 4);
1411 cio_seek(cio, j2k->sot_start + totlen);
1414 static void j2k_read_sod(opj_j2k_t *j2k) {
1415 int len, truncate = 0, i;
1416 unsigned char *data = NULL, *data_ptr = NULL;
1418 opj_cio_t *cio = j2k->cio;
1419 int curtileno = j2k->curtileno;
1422 if (j2k->cstr_info) {
1423 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
1424 cio_tell(cio) + j2k->pos_correction - 1;
1425 if (j2k->cur_tp_num == 0)
1426 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1427 j2k->cstr_info->packno = 0;
1430 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1432 if (len == cio_numbytesleft(cio) + 1) {
1433 truncate = 1; /* Case of a truncate codestream */
1436 data = j2k->tile_data[curtileno];
1437 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1439 data_ptr = data + j2k->tile_len[curtileno];
1440 for (i = 0; i < len; i++) {
1441 data_ptr[i] = cio_read(cio, 1);
1444 j2k->tile_len[curtileno] += len;
1445 j2k->tile_data[curtileno] = data;
1448 j2k->state = J2K_STATE_TPHSOT;
1450 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1455 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1456 opj_cp_t *cp = j2k->cp;
1457 opj_tcp_t *tcp = &cp->tcps[tileno];
1458 opj_cio_t *cio = j2k->cio;
1459 int numcomps = j2k->image->numcomps;
1461 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1462 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1463 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1464 cio_write(cio, 0, 1); /* Srgn */
1465 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1468 static void j2k_read_rgn(opj_j2k_t *j2k) {
1469 int len, compno, roisty;
1471 opj_cp_t *cp = j2k->cp;
1472 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1473 opj_cio_t *cio = j2k->cio;
1474 int numcomps = j2k->image->numcomps;
1476 len = cio_read(cio, 2); /* Lrgn */
1477 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1478 roisty = cio_read(cio, 1); /* Srgn */
1481 if (j2k->cp->correct) {
1482 /* totlen is negative or larger than the bytes left!!! */
1483 if (compno >= numcomps) {
1484 opj_event_msg(j2k->cinfo, EVT_ERROR,
1485 "JPWL: bad component number in RGN (%d when there are only %d)\n",
1487 if (!JPWL_ASSUME || JPWL_ASSUME) {
1488 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1493 #endif /* USE_JPWL */
1495 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1498 static void j2k_write_eoc(opj_j2k_t *j2k) {
1499 opj_cio_t *cio = j2k->cio;
1500 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1501 cio_write(cio, J2K_MS_EOC, 2);
1505 /* update markers struct */
1506 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
1507 #endif /* USE_JPWL */
1511 static void j2k_read_eoc(opj_j2k_t *j2k) {
1515 /* if packets should be decoded */
1516 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1517 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1518 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1519 for (i = 0; i < j2k->cp->tileno_size; i++) {
1520 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
1521 tileno = j2k->cp->tileno[i];
1522 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
1523 opj_free(j2k->tile_data[tileno]);
1524 j2k->tile_data[tileno] = NULL;
1525 tcd_free_decode_tile(tcd, i);
1526 if (success == OPJ_FALSE) {
1527 j2k->state |= J2K_STATE_ERR;
1531 tcd_free_decode(tcd);
1534 /* if packets should not be decoded */
1536 for (i = 0; i < j2k->cp->tileno_size; i++) {
1537 tileno = j2k->cp->tileno[i];
1538 opj_free(j2k->tile_data[tileno]);
1539 j2k->tile_data[tileno] = NULL;
1542 if (j2k->state & J2K_STATE_ERR)
1543 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
1545 j2k->state = J2K_STATE_MT;
1548 typedef struct opj_dec_mstabent {
1551 /** value of the state when the marker can appear */
1553 /** action linked to the marker */
1554 void (*handler) (opj_j2k_t *j2k);
1555 } opj_dec_mstabent_t;
1557 opj_dec_mstabent_t j2k_dec_mstab[] = {
1558 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1559 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1560 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1561 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1562 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1563 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1564 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1565 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1566 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1567 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1568 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1569 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1570 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1571 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1572 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1573 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1575 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1576 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1579 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1580 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1581 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1582 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1583 #endif /* USE_JPWL */
1585 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
1586 {J2K_MS_INSEC, 0, j2k_read_insec},
1587 #endif /* USE_JPSEC */
1589 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1592 static void j2k_read_unk(opj_j2k_t *j2k) {
1593 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1596 if (j2k->cp->correct) {
1598 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1599 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1600 id = cio_read(j2k->cio, 2);
1601 opj_event_msg(j2k->cinfo, EVT_ERROR,
1602 "JPWL: really don't know this marker %x\n",
1605 opj_event_msg(j2k->cinfo, EVT_ERROR,
1606 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
1609 /* OK, activate this at your own risk!!! */
1610 /* we look for the marker at the minimum hamming distance from this */
1611 while (j2k_dec_mstab[m].id) {
1613 /* 1's where they differ */
1614 tmp_id = j2k_dec_mstab[m].id ^ id;
1616 /* compute the hamming distance between our id and the current */
1618 for (i = 0; i < 16; i++) {
1619 if ((tmp_id >> i) & 0x0001) {
1624 /* if current distance is smaller, set the minimum */
1625 if (cur_dist < min_dist) {
1626 min_dist = cur_dist;
1627 min_id = j2k_dec_mstab[m].id;
1630 /* jump to the next marker */
1634 /* do we substitute the marker? */
1635 if (min_dist < JPWL_MAXIMUM_HAMMING) {
1636 opj_event_msg(j2k->cinfo, EVT_ERROR,
1637 "- marker %x is at distance %d from the read %x\n",
1638 min_id, min_dist, id);
1639 opj_event_msg(j2k->cinfo, EVT_ERROR,
1640 "- trying to substitute in place and crossing fingers!\n");
1641 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1642 cio_write(j2k->cio, min_id, 2);
1645 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1650 #endif /* USE_JPWL */
1655 Read the lookup table containing all the marker, status and action
1656 @param id Marker value
1658 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1659 opj_dec_mstabent_t *e;
1660 for (e = j2k_dec_mstab; e->id != 0; e++) {
1668 /* ----------------------------------------------------------------------- */
1669 /* J2K / JPT decoder interface */
1670 /* ----------------------------------------------------------------------- */
1672 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1673 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1677 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
1678 if(!j2k->default_tcp) {
1684 j2k->tile_data = NULL;
1689 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1692 if(j2k->tile_len != NULL) {
1693 opj_free(j2k->tile_len);
1695 if(j2k->tile_data != NULL) {
1696 opj_free(j2k->tile_data);
1698 if(j2k->default_tcp != NULL) {
1699 opj_tcp_t *default_tcp = j2k->default_tcp;
1700 if(default_tcp->ppt_data_first != NULL) {
1701 opj_free(default_tcp->ppt_data_first);
1703 if(j2k->default_tcp->tccps != NULL) {
1704 opj_free(j2k->default_tcp->tccps);
1706 opj_free(j2k->default_tcp);
1708 if(j2k->cp != NULL) {
1709 opj_cp_t *cp = j2k->cp;
1710 if(cp->tcps != NULL) {
1711 for(i = 0; i < cp->tw * cp->th; i++) {
1712 if(cp->tcps[i].ppt_data_first != NULL) {
1713 opj_free(cp->tcps[i].ppt_data_first);
1715 if(cp->tcps[i].tccps != NULL) {
1716 opj_free(cp->tcps[i].tccps);
1721 if(cp->ppm_data_first != NULL) {
1722 opj_free(cp->ppm_data_first);
1724 if(cp->tileno != NULL) {
1725 opj_free(cp->tileno);
1727 if(cp->comment != NULL) {
1728 opj_free(cp->comment);
1736 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1737 if(j2k && parameters) {
1738 /* create and initialize the coding parameters structure */
1739 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1740 cp->reduce = parameters->cp_reduce;
1741 cp->layer = parameters->cp_layer;
1742 cp->limit_decoding = parameters->cp_limit_decoding;
1745 cp->correct = parameters->jpwl_correct;
1746 cp->exp_comps = parameters->jpwl_exp_comps;
1747 cp->max_tiles = parameters->jpwl_max_tiles;
1748 #endif /* USE_JPWL */
1751 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1756 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1757 opj_image_t *image = NULL;
1759 opj_common_ptr cinfo = j2k->cinfo;
1762 j2k->cstr_info = cstr_info;
1764 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
1766 /* create an empty image */
1767 image = opj_image_create0();
1770 j2k->state = J2K_STATE_MHSOC;
1773 opj_dec_mstabent_t *e;
1774 int id = cio_read(cio, 2);
1777 /* we try to honor JPWL correction power */
1778 if (j2k->cp->correct) {
1780 int orig_pos = cio_tell(cio);
1783 /* call the corrector */
1784 status = jpwl_correct(j2k);
1786 /* go back to where you were */
1787 cio_seek(cio, orig_pos - 2);
1789 /* re-read the marker */
1790 id = cio_read(cio, 2);
1792 /* check whether it begins with ff */
1793 if (id >> 8 != 0xff) {
1794 opj_event_msg(cinfo, EVT_ERROR,
1795 "JPWL: possible bad marker %x at %d\n",
1796 id, cio_tell(cio) - 2);
1798 opj_image_destroy(image);
1799 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
1802 /* we try to correct */
1804 cio_seek(cio, cio_tell(cio) - 2);
1805 cio_write(cio, id, 2);
1806 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
1807 "- setting marker to %x\n",
1812 #endif /* USE_JPWL */
1814 if (id >> 8 != 0xff) {
1815 opj_image_destroy(image);
1816 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1819 e = j2k_dec_mstab_lookup(id);
1820 // Check if the marker is known
1821 if (!(j2k->state & e->states)) {
1822 opj_image_destroy(image);
1823 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1826 // Check if the decoding is limited to the main header
1827 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
1828 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
1835 if (j2k->state & J2K_STATE_ERR)
1838 if (j2k->state == J2K_STATE_MT) {
1841 if (j2k->state == J2K_STATE_NEOC) {
1845 if (j2k->state == J2K_STATE_NEOC) {
1849 if (j2k->state != J2K_STATE_MT) {
1850 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1857 * Read a JPT-stream and decode file
1860 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
1861 opj_image_t *image = NULL;
1862 opj_jpt_msg_header_t header;
1865 OPJ_ARG_NOT_USED(cstr_info);
1867 opj_common_ptr cinfo = j2k->cinfo;
1870 /* create an empty image */
1871 image = opj_image_create0();
1874 j2k->state = J2K_STATE_MHSOC;
1876 /* Initialize the header */
1877 jpt_init_msg_header(&header);
1878 /* Read the first header of the message */
1879 jpt_read_msg_header(cinfo, cio, &header);
1881 position = cio_tell(cio);
1882 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
1883 opj_image_destroy(image);
1884 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1889 opj_dec_mstabent_t *e = NULL;
1892 if (!cio_numbytesleft(cio)) {
1896 /* data-bin read -> need to read a new header */
1897 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1898 jpt_read_msg_header(cinfo, cio, &header);
1899 position = cio_tell(cio);
1900 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
1901 opj_image_destroy(image);
1902 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1907 id = cio_read(cio, 2);
1908 if (id >> 8 != 0xff) {
1909 opj_image_destroy(image);
1910 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1913 e = j2k_dec_mstab_lookup(id);
1914 if (!(j2k->state & e->states)) {
1915 opj_image_destroy(image);
1916 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1922 if (j2k->state == J2K_STATE_MT) {
1925 if (j2k->state == J2K_STATE_NEOC) {
1929 if (j2k->state == J2K_STATE_NEOC) {
1933 if (j2k->state != J2K_STATE_MT) {
1934 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1940 /* ----------------------------------------------------------------------- */
1941 /* J2K encoder interface */
1942 /* ----------------------------------------------------------------------- */
1944 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1945 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
1952 void j2k_destroy_compress(opj_j2k_t *j2k) {
1956 if(j2k->cp != NULL) {
1957 opj_cp_t *cp = j2k->cp;
1960 opj_free(cp->comment);
1963 opj_free(cp->matrice);
1965 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1966 opj_free(cp->tcps[tileno].tccps);
1975 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1976 int i, j, tileno, numpocs_tile;
1977 opj_cp_t *cp = NULL;
1979 if(!j2k || !parameters || ! image) {
1983 /* create and initialize the coding parameters structure */
1984 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
1986 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1989 /* set default values for cp */
1994 copy user encoding parameters
1996 cp->cinema = parameters->cp_cinema;
1997 cp->max_comp_size = parameters->max_comp_size;
1998 cp->rsiz = parameters->cp_rsiz;
1999 cp->disto_alloc = parameters->cp_disto_alloc;
2000 cp->fixed_alloc = parameters->cp_fixed_alloc;
2001 cp->fixed_quality = parameters->cp_fixed_quality;
2003 /* mod fixed_quality */
2004 if(parameters->cp_matrice) {
2005 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
2006 cp->matrice = (int *) opj_malloc(array_size);
2007 memcpy(cp->matrice, parameters->cp_matrice, array_size);
2011 cp->tdx = parameters->cp_tdx;
2012 cp->tdy = parameters->cp_tdy;
2015 cp->tx0 = parameters->cp_tx0;
2016 cp->ty0 = parameters->cp_ty0;
2018 /* comment string */
2019 if(parameters->cp_comment) {
2020 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
2022 strcpy(cp->comment, parameters->cp_comment);
2027 calculate other encoding parameters
2030 if (parameters->tile_size_on) {
2031 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2032 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2034 cp->tdx = image->x1 - cp->tx0;
2035 cp->tdy = image->y1 - cp->ty0;
2038 if(parameters->tp_on){
2039 cp->tp_flag = parameters->tp_flag;
2044 for(i=0;i<image->numcomps ;i++){
2045 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
2051 calculate JPWL encoding parameters
2054 if (parameters->jpwl_epc_on) {
2058 cp->epc_on = OPJ_TRUE;
2059 cp->info_on = OPJ_FALSE; /* no informative technique */
2062 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2063 cp->epb_on = OPJ_TRUE;
2065 cp->hprot_MH = parameters->jpwl_hprot_MH;
2066 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2067 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2068 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2070 /* if tile specs are not specified, copy MH specs */
2071 if (cp->hprot_TPH[0] == -1) {
2072 cp->hprot_TPH_tileno[0] = 0;
2073 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2075 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2076 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2077 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2078 cp->pprot[i] = parameters->jpwl_pprot[i];
2082 /* set ESD writing */
2083 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2084 cp->esd_on = OPJ_TRUE;
2086 cp->sens_size = parameters->jpwl_sens_size;
2087 cp->sens_addr = parameters->jpwl_sens_addr;
2088 cp->sens_range = parameters->jpwl_sens_range;
2090 cp->sens_MH = parameters->jpwl_sens_MH;
2091 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2092 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2093 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2097 /* always set RED writing to false: we are at the encoder */
2098 cp->red_on = OPJ_FALSE;
2101 cp->epc_on = OPJ_FALSE;
2103 #endif /* USE_JPWL */
2106 /* initialize the mutiple tiles */
2107 /* ---------------------------- */
2108 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2110 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2111 opj_tcp_t *tcp = &cp->tcps[tileno];
2112 tcp->numlayers = parameters->tcp_numlayers;
2113 for (j = 0; j < tcp->numlayers; j++) {
2115 if (cp->fixed_quality) {
2116 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2118 tcp->rates[j] = parameters->tcp_rates[j];
2120 if (cp->fixed_quality) { /* add fixed_quality */
2121 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2123 tcp->rates[j] = parameters->tcp_rates[j];
2127 tcp->csty = parameters->csty;
2128 tcp->prg = parameters->prog_order;
2129 tcp->mct = parameters->tcp_mct;
2133 if (parameters->numpocs) {
2134 /* initialisation of POC */
2136 for (i = 0; i < parameters->numpocs; i++) {
2137 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2138 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2139 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2140 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2141 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2142 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2143 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2144 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2145 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2149 tcp->numpocs = numpocs_tile -1 ;
2154 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2156 for (i = 0; i < image->numcomps; i++) {
2157 opj_tccp_t *tccp = &tcp->tccps[i];
2158 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
2159 tccp->numresolutions = parameters->numresolution;
2160 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2161 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2162 tccp->cblksty = parameters->mode;
2163 tccp->qmfbid = parameters->irreversible ? 0 : 1;
2164 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
2166 if (i == parameters->roi_compno) {
2167 tccp->roishift = parameters->roi_shift;
2172 if(parameters->cp_cinema)
2174 //Precinct size for lowest frequency subband=128
2177 //Precinct size at all other resolutions = 256
2178 for (j = 1; j < tccp->numresolutions; j++) {
2183 if (parameters->csty & J2K_CCP_CSTY_PRT) {
2185 for (j = tccp->numresolutions - 1; j >= 0; j--) {
2186 if (p < parameters->res_spec) {
2188 if (parameters->prcw_init[p] < 1) {
2191 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2194 if (parameters->prch_init[p] < 1) {
2197 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2201 int res_spec = parameters->res_spec;
2202 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2203 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2205 if (size_prcw < 1) {
2208 tccp->prcw[j] = int_floorlog2(size_prcw);
2211 if (size_prch < 1) {
2214 tccp->prch[j] = int_floorlog2(size_prch);
2218 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2221 for (j = 0; j < tccp->numresolutions; j++) {
2228 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2233 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
2235 opj_cp_t *cp = NULL;
2237 opj_tcd_t *tcd = NULL; /* TCD component */
2245 j2k->cstr_info = cstr_info;
2248 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2249 cstr_info->image_w = image->x1 - image->x0;
2250 cstr_info->image_h = image->y1 - image->y0;
2251 cstr_info->prog = (&cp->tcps[0])->prg;
2252 cstr_info->tw = cp->tw;
2253 cstr_info->th = cp->th;
2254 cstr_info->tile_x = cp->tdx; /* new version parser */
2255 cstr_info->tile_y = cp->tdy; /* new version parser */
2256 cstr_info->tile_Ox = cp->tx0; /* new version parser */
2257 cstr_info->tile_Oy = cp->ty0; /* new version parser */
2258 cstr_info->numcomps = image->numcomps;
2259 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
2260 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2261 for (compno=0; compno < image->numcomps; compno++) {
2262 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
2264 cstr_info->D_max = 0.0; /* ADD Marcela */
2265 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
2266 cstr_info->maxmarknum = 100;
2267 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
2268 cstr_info->marknum = 0;
2278 for (compno = 1; compno < image->numcomps; compno++) {
2279 j2k_write_coc(j2k, compno);
2280 j2k_write_qcc(j2k, compno);
2284 for (compno = 0; compno < image->numcomps; compno++) {
2285 opj_tcp_t *tcp = &cp->tcps[0];
2286 if (tcp->tccps[compno].roishift)
2287 j2k_write_rgn(j2k, compno, 0);
2289 if (cp->comment != NULL) {
2293 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2297 if (cp->cinema == CINEMA4K_24) {
2302 /* uncomment only for testing JPSEC marker writing */
2303 /* j2k_write_sec(j2k); */
2307 cstr_info->main_head_end = cio_tell(cio) - 1;
2310 /**** Main Header ENDS here ***/
2312 /* create the tile encoder */
2313 tcd = tcd_create(j2k->cinfo);
2315 /* encode each tile */
2316 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2320 int acc_pack_num = 0;
2324 opj_tcp_t *tcp = &cp->tcps[tileno];
2325 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2327 j2k->curtileno = tileno;
2328 j2k->cur_tp_num = 0;
2329 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2330 /* initialisation before tile encoding */
2332 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2334 tcd_init_encode(tcd, image, cp, j2k->curtileno);
2339 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2343 for(pino = 0; pino <= tcp->numpocs; pino++) {
2347 /*Get number of tile parts*/
2348 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2349 tcd->tp_pos = cp->tp_pos;
2351 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2352 j2k->tp_num = tilepartno;
2355 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
2356 cio_tell(cio) + j2k->pos_correction;
2360 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2361 for (compno = 1; compno < image->numcomps; compno++) {
2362 j2k_write_coc(j2k, compno);
2363 j2k_write_qcc(j2k, compno);
2365 if (cp->tcps[tileno].numpocs) {
2372 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
2373 cio_tell(cio) + j2k->pos_correction + 1;
2376 j2k_write_sod(j2k, tcd);
2380 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
2381 cio_tell(cio) + j2k->pos_correction - 1;
2382 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
2384 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
2385 cstr_info->packno - acc_pack_num;
2386 acc_pack_num = cstr_info->packno;
2394 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2399 if (tile->PPT) { // BAD PPT !!!
2402 PPT_file=fopen("PPT","rb");
2403 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2404 for (i=0;i<tile->len_ppt;i++) {
2406 fread(&elmt, 1, 1, PPT_file);
2407 fwrite(&elmt,1,1,f);
2416 /* destroy the tile encoder */
2417 tcd_free_encode(tcd);
2420 opj_free(j2k->cur_totnum_tp);
2425 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
2427 /* The following adjustment is done to adjust the codestream size */
2428 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
2429 /* the first bunch of bytes is not in the codestream */
2430 cstr_info->codestream_size -= cstr_info->main_head_start;
2436 preparation of JPWL marker segments
2440 /* encode according to JPWL */
2441 jpwl_encode(j2k, cio, image);
2444 #endif /* USE_JPWL */