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[] = {
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 static void j2k_check_poc_val(opj_cparameters_t *parameters, int numcomps, int numlayers){
262 int index, resno, compno, layno, i;
265 int step_r = numcomps * step_c;
266 int step_l = parameters->numresolution * step_r;
267 int array_size = step_l * numlayers * sizeof(int);
268 int *packet_array = (int *) opj_malloc(array_size);
270 for (i = 0; i < parameters->numpocs ; i++) {
273 layno0 = (parameters->POC[i].layno1 > parameters->POC[i-1].layno1 )? parameters->POC[i-1].layno1 : 0;
274 for (resno = parameters->POC[i].resno0 ; resno < parameters->POC[i].resno1 ; resno++) {
275 for (compno = parameters->POC[i].compno0 ; compno < parameters->POC[i].compno1 ; compno++) {
276 for (layno = layno0; layno < parameters->POC[i].layno1 ; layno++) {
277 index = step_r * resno + step_c * compno + step_l * layno;
278 packet_array[index]= 1;
283 for (resno = 0; resno < parameters->numresolution; resno++) {
284 for (compno = 0; compno < numcomps; compno++) {
285 for (layno = 0; layno < numlayers ; layno++) {
286 index = step_r * resno + step_c * compno + step_l * layno;
287 if(!( packet_array[index]== 1)){
294 fprintf(stdout,"Missing packets possible loss of data\n");
295 opj_free(packet_array);
298 void j2k_dump_image(FILE *fd, opj_image_t * img) {
300 fprintf(fd, "image {\n");
301 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
302 fprintf(fd, " numcomps=%d\n", img->numcomps);
303 for (compno = 0; compno < img->numcomps; compno++) {
304 opj_image_comp_t *comp = &img->comps[compno];
305 fprintf(fd, " comp %d {\n", compno);
306 fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy);
307 fprintf(fd, " prec=%d\n", comp->prec);
308 fprintf(fd, " sgnd=%d\n", comp->sgnd);
314 void j2k_dump_cp(FILE *fd, opj_image_t * img, opj_cp_t * cp) {
315 int tileno, compno, layno, bandno, resno, numbands;
316 fprintf(fd, "coding parameters {\n");
317 fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
318 fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
319 fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th);
320 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
321 opj_tcp_t *tcp = &cp->tcps[tileno];
322 fprintf(fd, " tile %d {\n", tileno);
323 fprintf(fd, " csty=%x\n", tcp->csty);
324 fprintf(fd, " prg=%d\n", tcp->prg);
325 fprintf(fd, " numlayers=%d\n", tcp->numlayers);
326 fprintf(fd, " mct=%d\n", tcp->mct);
327 fprintf(fd, " rates=");
328 for (layno = 0; layno < tcp->numlayers; layno++) {
329 fprintf(fd, "%.1f ", tcp->rates[layno]);
332 for (compno = 0; compno < img->numcomps; compno++) {
333 opj_tccp_t *tccp = &tcp->tccps[compno];
334 fprintf(fd, " comp %d {\n", compno);
335 fprintf(fd, " csty=%x\n", tccp->csty);
336 fprintf(fd, " numresolutions=%d\n", tccp->numresolutions);
337 fprintf(fd, " cblkw=%d\n", tccp->cblkw);
338 fprintf(fd, " cblkh=%d\n", tccp->cblkh);
339 fprintf(fd, " cblksty=%x\n", tccp->cblksty);
340 fprintf(fd, " qmfbid=%d\n", tccp->qmfbid);
341 fprintf(fd, " qntsty=%d\n", tccp->qntsty);
342 fprintf(fd, " numgbits=%d\n", tccp->numgbits);
343 fprintf(fd, " roishift=%d\n", tccp->roishift);
344 fprintf(fd, " stepsizes=");
345 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
346 for (bandno = 0; bandno < numbands; bandno++) {
347 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
348 tccp->stepsizes[bandno].expn);
352 if (tccp->csty & J2K_CCP_CSTY_PRT) {
353 fprintf(fd, " prcw=");
354 for (resno = 0; resno < tccp->numresolutions; resno++) {
355 fprintf(fd, "%d ", tccp->prcw[resno]);
358 fprintf(fd, " prch=");
359 for (resno = 0; resno < tccp->numresolutions; resno++) {
360 fprintf(fd, "%d ", tccp->prch[resno]);
371 /* ----------------------------------------------------------------------- */
372 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
376 opj_tcp_t *tcp = &cp->tcps[tileno];
377 prog = j2k_convert_progression_order(tcp->prg);
382 if( cp->tp_flag == prog[i] ){
383 tpend=1;cp->tp_pos=i;
387 tpnum= tpnum * tcp->pocs[pino].compE;
390 tpnum= tpnum * tcp->pocs[pino].resE;
393 tpnum= tpnum * tcp->pocs[pino].prcE;
396 tpnum= tpnum * tcp->pocs[pino].layE;
407 /** mem allocation for TLM marker*/
408 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
409 int pino,tileno,maxres=0,totnum_tp=0;
410 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
411 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
412 int cur_totnum_tp = 0;
413 opj_tcp_t *tcp = &cp->tcps[tileno];
414 for(pino = 0; pino <= tcp->numpocs; pino++) {
416 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
417 if(!pi) { return -1;}
418 tp_num = j2k_get_num_tp(cp,pino,tileno);
419 totnum_tp = totnum_tp + tp_num;
420 cur_totnum_tp = cur_totnum_tp + tp_num;
421 pi_destroy(pi, cp, tileno);
423 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
428 static void j2k_write_soc(opj_j2k_t *j2k) {
429 opj_cio_t *cio = j2k->cio;
430 cio_write(cio, J2K_MS_SOC, 2);
433 static void j2k_read_soc(opj_j2k_t *j2k) {
434 j2k->state = J2K_STATE_MHSIZ;
437 static void j2k_write_siz(opj_j2k_t *j2k) {
441 opj_cio_t *cio = j2k->cio;
442 opj_image_t *image = j2k->image;
443 opj_cp_t *cp = j2k->cp;
445 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
446 lenp = cio_tell(cio);
448 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
449 cio_write(cio, image->x1, 4); /* Xsiz */
450 cio_write(cio, image->y1, 4); /* Ysiz */
451 cio_write(cio, image->x0, 4); /* X0siz */
452 cio_write(cio, image->y0, 4); /* Y0siz */
453 cio_write(cio, cp->tdx, 4); /* XTsiz */
454 cio_write(cio, cp->tdy, 4); /* YTsiz */
455 cio_write(cio, cp->tx0, 4); /* XT0siz */
456 cio_write(cio, cp->ty0, 4); /* YT0siz */
457 cio_write(cio, image->numcomps, 2); /* Csiz */
458 for (i = 0; i < image->numcomps; i++) {
459 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
460 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
461 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
463 len = cio_tell(cio) - lenp;
465 cio_write(cio, len, 2); /* Lsiz */
466 cio_seek(cio, lenp + len);
469 static void j2k_read_siz(opj_j2k_t *j2k) {
472 opj_cio_t *cio = j2k->cio;
473 opj_image_t *image = j2k->image;
474 opj_cp_t *cp = j2k->cp;
476 len = cio_read(cio, 2); /* Lsiz */
477 cio_read(cio, 2); /* Rsiz (capabilities) */
478 image->x1 = cio_read(cio, 4); /* Xsiz */
479 image->y1 = cio_read(cio, 4); /* Ysiz */
480 image->x0 = cio_read(cio, 4); /* X0siz */
481 image->y0 = cio_read(cio, 4); /* Y0siz */
482 cp->tdx = cio_read(cio, 4); /* XTsiz */
483 cp->tdy = cio_read(cio, 4); /* YTsiz */
484 cp->tx0 = cio_read(cio, 4); /* XT0siz */
485 cp->ty0 = cio_read(cio, 4); /* YT0siz */
487 image->numcomps = cio_read(cio, 2); /* Csiz */
490 if (j2k->cp->correct) {
491 /* if JPWL is on, we check whether TX errors have damaged
492 too much the SIZ parameters */
493 if (!(image->x1 * image->y1)) {
494 opj_event_msg(j2k->cinfo, EVT_ERROR,
495 "JPWL: bad image size (%d x %d)\n",
496 image->x1, image->y1);
497 if (!JPWL_ASSUME || JPWL_ASSUME) {
498 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
502 if (image->numcomps != ((len - 38) / 3)) {
503 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
504 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
505 image->numcomps, ((len - 38) / 3));
507 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
510 /* we try to correct */
511 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
512 if (image->numcomps < ((len - 38) / 3)) {
513 len = 38 + 3 * image->numcomps;
514 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
517 image->numcomps = ((len - 38) / 3);
518 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
523 /* update components number in the jpwl_exp_comps filed */
524 cp->exp_comps = image->numcomps;
526 #endif /* USE_JPWL */
528 image->comps = (opj_image_comp_t *) opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
529 for (i = 0; i < image->numcomps; i++) {
531 tmp = cio_read(cio, 1); /* Ssiz_i */
532 image->comps[i].prec = (tmp & 0x7f) + 1;
533 image->comps[i].sgnd = tmp >> 7;
534 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
535 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
538 if (j2k->cp->correct) {
539 /* if JPWL is on, we check whether TX errors have damaged
540 too much the SIZ parameters, again */
541 if (!(image->comps[i].dx * image->comps[i].dy)) {
542 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
543 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
544 i, i, image->comps[i].dx, image->comps[i].dy);
546 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
549 /* we try to correct */
550 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
551 if (!image->comps[i].dx) {
552 image->comps[i].dx = 1;
553 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
554 i, image->comps[i].dx);
556 if (!image->comps[i].dy) {
557 image->comps[i].dy = 1;
558 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
559 i, image->comps[i].dy);
564 #endif /* USE_JPWL */
568 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
569 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
571 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
572 image->comps[i].factor = 0; /* reducing factor per component */
575 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
576 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
579 if (j2k->cp->correct) {
580 /* if JPWL is on, we check whether TX errors have damaged
581 too much the SIZ parameters */
582 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
583 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
584 "JPWL: bad number of tiles (%d x %d)\n",
587 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
590 /* we try to correct */
591 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
594 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
597 if (cp->tw > cp->max_tiles) {
599 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
600 "- setting %d tiles in x => HYPOTHESIS!!!\n",
601 cp->max_tiles, cp->tw);
605 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
608 if (cp->th > cp->max_tiles) {
610 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
611 "- setting %d tiles in y => HYPOTHESIS!!!\n",
612 cp->max_tiles, cp->th);
616 #endif /* USE_JPWL */
618 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
619 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
623 if (j2k->cp->correct) {
625 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
626 "JPWL: could not alloc tcps field of cp\n");
627 if (!JPWL_ASSUME || JPWL_ASSUME) {
628 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
633 #endif /* USE_JPWL */
635 for (i = 0; i < cp->tw * cp->th; i++) {
637 cp->tcps[i].numpocs = 0;
638 cp->tcps[i].first = 1;
641 /* Initialization for PPM marker */
644 cp->ppm_data_first = NULL;
645 cp->ppm_previous = 0;
648 j2k->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
649 for (i = 0; i < cp->tw * cp->th; i++) {
650 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * image->numcomps);
652 j2k->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * sizeof(unsigned char *));
653 j2k->tile_len = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
654 j2k->state = J2K_STATE_MH;
657 static void j2k_write_com(opj_j2k_t *j2k) {
661 if(j2k->cp->comment) {
662 opj_cio_t *cio = j2k->cio;
663 char *comment = j2k->cp->comment;
665 cio_write(cio, J2K_MS_COM, 2);
666 lenp = cio_tell(cio);
668 cio_write(cio, 0, 2);
669 for (i = 0; i < strlen(comment); i++) {
670 cio_write(cio, comment[i], 1);
672 len = cio_tell(cio) - lenp;
674 cio_write(cio, len, 2);
675 cio_seek(cio, lenp + len);
679 static void j2k_read_com(opj_j2k_t *j2k) {
682 opj_cio_t *cio = j2k->cio;
684 len = cio_read(cio, 2);
685 cio_skip(cio, len - 2);
688 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
691 opj_cp_t *cp = j2k->cp;
692 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
693 opj_tccp_t *tccp = &tcp->tccps[compno];
694 opj_cio_t *cio = j2k->cio;
696 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
697 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
698 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
699 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
700 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
702 if (tccp->csty & J2K_CCP_CSTY_PRT) {
703 for (i = 0; i < tccp->numresolutions; i++) {
704 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
709 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
712 opj_cp_t *cp = j2k->cp;
713 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
714 opj_tccp_t *tccp = &tcp->tccps[compno];
715 opj_cio_t *cio = j2k->cio;
717 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
719 /* check the reduce value */
720 cp->reduce = int_min((tccp->numresolutions)-1, cp->reduce);
721 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
722 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
723 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
724 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
725 if (tccp->csty & J2K_CP_CSTY_PRT) {
726 for (i = 0; i < tccp->numresolutions; i++) {
727 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
728 tccp->prcw[i] = tmp & 0xf;
729 tccp->prch[i] = tmp >> 4;
734 static void j2k_write_cod(opj_j2k_t *j2k) {
736 opj_tcp_t *tcp = NULL;
739 opj_cio_t *cio = j2k->cio;
741 cio_write(cio, J2K_MS_COD, 2); /* COD */
743 lenp = cio_tell(cio);
747 tcp = &cp->tcps[j2k->curtileno];
749 cio_write(cio, tcp->csty, 1); /* Scod */
750 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
751 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
752 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
754 j2k_write_cox(j2k, 0);
755 len = cio_tell(cio) - lenp;
757 cio_write(cio, len, 2); /* Lcod */
758 cio_seek(cio, lenp + len);
761 static void j2k_read_cod(opj_j2k_t *j2k) {
764 opj_cio_t *cio = j2k->cio;
765 opj_cp_t *cp = j2k->cp;
766 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
767 opj_image_t *image = j2k->image;
769 len = cio_read(cio, 2); /* Lcod */
770 tcp->csty = cio_read(cio, 1); /* Scod */
771 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
772 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
773 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
776 for (i = 0; i < image->numcomps; i++) {
777 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
779 j2k_read_cox(j2k, i);
783 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
786 opj_cp_t *cp = j2k->cp;
787 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
788 opj_image_t *image = j2k->image;
789 opj_cio_t *cio = j2k->cio;
791 cio_write(cio, J2K_MS_COC, 2); /* COC */
792 lenp = cio_tell(cio);
794 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
795 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
796 j2k_write_cox(j2k, compno);
797 len = cio_tell(cio) - lenp;
799 cio_write(cio, len, 2); /* Lcoc */
800 cio_seek(cio, lenp + len);
803 static void j2k_read_coc(opj_j2k_t *j2k) {
806 opj_cp_t *cp = j2k->cp;
807 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
808 opj_image_t *image = j2k->image;
809 opj_cio_t *cio = j2k->cio;
811 len = cio_read(cio, 2); /* Lcoc */
812 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
813 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
814 j2k_read_cox(j2k, compno);
817 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
818 int bandno, numbands;
821 opj_cp_t *cp = j2k->cp;
822 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
823 opj_tccp_t *tccp = &tcp->tccps[compno];
824 opj_cio_t *cio = j2k->cio;
826 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
827 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
829 for (bandno = 0; bandno < numbands; bandno++) {
830 expn = tccp->stepsizes[bandno].expn;
831 mant = tccp->stepsizes[bandno].mant;
833 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
834 cio_write(cio, expn << 3, 1); /* SPqcx_i */
836 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
841 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
843 int bandno, numbands;
845 opj_cp_t *cp = j2k->cp;
846 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
847 opj_tccp_t *tccp = &tcp->tccps[compno];
848 opj_cio_t *cio = j2k->cio;
850 tmp = cio_read(cio, 1); /* Sqcx */
851 tccp->qntsty = tmp & 0x1f;
852 tccp->numgbits = tmp >> 5;
853 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
854 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
857 if (j2k->cp->correct) {
859 /* if JPWL is on, we check whether there are too many subbands */
860 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
861 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
862 "JPWL: bad number of subbands in Sqcx (%d)\n",
865 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
868 /* we try to correct */
870 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
871 "- setting number of bands to %d => HYPOTHESIS!!!\n",
876 #endif /* USE_JPWL */
878 for (bandno = 0; bandno < numbands; bandno++) {
880 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
881 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
884 tmp = cio_read(cio, 2); /* SPqcx_i */
888 tccp->stepsizes[bandno].expn = expn;
889 tccp->stepsizes[bandno].mant = mant;
892 /* Add Antonin : if scalar_derived -> compute other stepsizes */
893 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
894 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
895 tccp->stepsizes[bandno].expn =
896 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
897 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
898 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
904 static void j2k_write_qcd(opj_j2k_t *j2k) {
907 opj_cio_t *cio = j2k->cio;
909 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
910 lenp = cio_tell(cio);
912 j2k_write_qcx(j2k, 0);
913 len = cio_tell(cio) - lenp;
915 cio_write(cio, len, 2); /* Lqcd */
916 cio_seek(cio, lenp + len);
919 static void j2k_read_qcd(opj_j2k_t *j2k) {
922 opj_cio_t *cio = j2k->cio;
923 opj_image_t *image = j2k->image;
925 len = cio_read(cio, 2); /* Lqcd */
927 for (i = 0; i < image->numcomps; i++) {
929 j2k_read_qcx(j2k, i, len - 2);
933 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
936 opj_cio_t *cio = j2k->cio;
938 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
939 lenp = cio_tell(cio);
941 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
942 j2k_write_qcx(j2k, compno);
943 len = cio_tell(cio) - lenp;
945 cio_write(cio, len, 2); /* Lqcc */
946 cio_seek(cio, lenp + len);
949 static void j2k_read_qcc(opj_j2k_t *j2k) {
951 int numcomp = j2k->image->numcomps;
952 opj_cio_t *cio = j2k->cio;
954 len = cio_read(cio, 2); /* Lqcc */
955 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
958 if (j2k->cp->correct) {
960 static int backup_compno = 0;
962 /* compno is negative or larger than the number of components!!! */
963 if ((compno < 0) || (compno >= numcomp)) {
964 opj_event_msg(j2k->cinfo, EVT_ERROR,
965 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
968 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
971 /* we try to correct */
972 compno = backup_compno % numcomp;
973 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
974 "- setting component number to %d\n",
978 /* keep your private count of tiles */
981 #endif /* USE_JPWL */
983 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
986 static void j2k_write_poc(opj_j2k_t *j2k) {
987 int len, numpchgs, i;
989 int numcomps = j2k->image->numcomps;
991 opj_cp_t *cp = j2k->cp;
992 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
993 opj_tccp_t *tccp = &tcp->tccps[0];
994 opj_cio_t *cio = j2k->cio;
996 numpchgs = 1 + tcp->numpocs;
997 cio_write(cio, J2K_MS_POC, 2); /* POC */
998 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
999 cio_write(cio, len, 2); /* Lpoc */
1000 for (i = 0; i < numpchgs; i++) {
1001 opj_poc_t *poc = &tcp->pocs[i];
1002 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
1003 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
1004 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
1005 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
1006 cio_write(cio, poc->resno1, 1); /* REpoc_i */
1007 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
1008 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
1009 poc->compno1 = int_min(poc->compno1, numcomps);
1010 cio_write(cio, poc->prg, 1); /* Ppoc_i */
1014 static void j2k_read_poc(opj_j2k_t *j2k) {
1015 int len, numpchgs, i, old_poc;
1017 int numcomps = j2k->image->numcomps;
1019 opj_cp_t *cp = j2k->cp;
1020 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1021 opj_tccp_t *tccp = &tcp->tccps[0];
1022 opj_cio_t *cio = j2k->cio;
1024 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
1026 len = cio_read(cio, 2); /* Lpoc */
1027 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
1029 for (i = old_poc; i < numpchgs + old_poc; i++) {
1031 poc = &tcp->pocs[i];
1032 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
1033 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
1034 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
1035 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
1036 poc->compno1 = int_min(
1037 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
1038 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
1041 tcp->numpocs = numpchgs + old_poc - 1;
1044 static void j2k_read_crg(opj_j2k_t *j2k) {
1045 int len, i, Xcrg_i, Ycrg_i;
1047 opj_cio_t *cio = j2k->cio;
1048 int numcomps = j2k->image->numcomps;
1050 len = cio_read(cio, 2); /* Lcrg */
1051 for (i = 0; i < numcomps; i++) {
1052 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
1053 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
1057 static void j2k_read_tlm(opj_j2k_t *j2k) {
1058 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
1059 long int Ttlm_i, Ptlm_i;
1061 opj_cio_t *cio = j2k->cio;
1063 len = cio_read(cio, 2); /* Ltlm */
1064 Ztlm = cio_read(cio, 1); /* Ztlm */
1065 Stlm = cio_read(cio, 1); /* Stlm */
1066 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
1067 SP = (Stlm >> 6) & 0x01;
1068 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
1069 for (i = 0; i < tile_tlm; i++) {
1070 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
1071 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
1075 static void j2k_read_plm(opj_j2k_t *j2k) {
1076 int len, i, Zplm, Nplm, add, packet_len = 0;
1078 opj_cio_t *cio = j2k->cio;
1080 len = cio_read(cio, 2); /* Lplm */
1081 Zplm = cio_read(cio, 1); /* Zplm */
1084 Nplm = cio_read(cio, 4); /* Nplm */
1086 for (i = Nplm; i > 0; i--) {
1087 add = cio_read(cio, 1);
1089 packet_len = (packet_len << 7) + add; /* Iplm_ij */
1090 if ((add & 0x80) == 0) {
1100 static void j2k_read_plt(opj_j2k_t *j2k) {
1101 int len, i, Zplt, packet_len = 0, add;
1103 opj_cio_t *cio = j2k->cio;
1105 len = cio_read(cio, 2); /* Lplt */
1106 Zplt = cio_read(cio, 1); /* Zplt */
1107 for (i = len - 3; i > 0; i--) {
1108 add = cio_read(cio, 1);
1109 packet_len = (packet_len << 7) + add; /* Iplt_i */
1110 if ((add & 0x80) == 0) {
1117 static void j2k_read_ppm(opj_j2k_t *j2k) {
1118 int len, Z_ppm, i, j;
1121 opj_cp_t *cp = j2k->cp;
1122 opj_cio_t *cio = j2k->cio;
1124 len = cio_read(cio, 2);
1127 Z_ppm = cio_read(cio, 1); /* Z_ppm */
1130 if (cp->ppm_previous == 0) {
1131 N_ppm = cio_read(cio, 4); /* N_ppm */
1134 N_ppm = cp->ppm_previous;
1137 if (Z_ppm == 0) { /* First PPM marker */
1138 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
1139 cp->ppm_data_first = cp->ppm_data;
1140 cp->ppm_len = N_ppm;
1141 } else { /* NON-first PPM marker */
1142 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
1145 /* this memory allocation check could be done even in non-JPWL cases */
1147 if (!cp->ppm_data) {
1148 opj_event_msg(j2k->cinfo, EVT_ERROR,
1149 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
1151 if (!JPWL_ASSUME || JPWL_ASSUME) {
1153 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1160 cp->ppm_data_first = cp->ppm_data;
1161 cp->ppm_len = N_ppm + cp->ppm_store;
1163 for (i = N_ppm; i > 0; i--) { /* Read packet header */
1164 cp->ppm_data[j] = cio_read(cio, 1);
1168 break; /* Case of non-finished packet header in present marker but finished in next one */
1170 cp->ppm_previous = i - 1;
1175 static void j2k_read_ppt(opj_j2k_t *j2k) {
1176 int len, Z_ppt, i, j = 0;
1178 opj_cp_t *cp = j2k->cp;
1179 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
1180 opj_cio_t *cio = j2k->cio;
1182 len = cio_read(cio, 2);
1183 Z_ppt = cio_read(cio, 1);
1185 if (Z_ppt == 0) { /* First PPT marker */
1186 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
1187 tcp->ppt_data_first = tcp->ppt_data;
1189 tcp->ppt_len = len - 3;
1190 } else { /* NON-first PPT marker */
1191 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
1192 tcp->ppt_data_first = tcp->ppt_data;
1193 tcp->ppt_len = len - 3 + tcp->ppt_store;
1196 for (i = len - 3; i > 0; i--) {
1197 tcp->ppt_data[j] = cio_read(cio, 1);
1203 static void j2k_write_tlm(opj_j2k_t *j2k){
1205 opj_cio_t *cio = j2k->cio;
1206 j2k->tlm_start = cio_tell(cio);
1207 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
1208 lenp = 4 + (5*j2k->totnum_tp);
1209 cio_write(cio,lenp,2); /* Ltlm */
1210 cio_write(cio, 0,1); /* Ztlm=0*/
1211 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
1212 cio_skip(cio,5*j2k->totnum_tp);
1215 static void j2k_write_sot(opj_j2k_t *j2k) {
1218 opj_cio_t *cio = j2k->cio;
1220 j2k->sot_start = cio_tell(cio);
1221 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
1222 lenp = cio_tell(cio);
1223 cio_skip(cio, 2); /* Lsot (further) */
1224 cio_write(cio, j2k->curtileno, 2); /* Isot */
1225 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
1226 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
1227 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
1228 len = cio_tell(cio) - lenp;
1229 cio_seek(cio, lenp);
1230 cio_write(cio, len, 2); /* Lsot */
1231 cio_seek(cio, lenp + len);
1234 static void j2k_read_sot(opj_j2k_t *j2k) {
1235 int len, tileno, totlen, partno, numparts, i;
1236 opj_tcp_t *tcp = NULL;
1239 opj_cp_t *cp = j2k->cp;
1240 opj_cio_t *cio = j2k->cio;
1242 len = cio_read(cio, 2);
1243 tileno = cio_read(cio, 2);
1246 if (j2k->cp->correct) {
1248 static int backup_tileno = 0;
1250 /* tileno is negative or larger than the number of tiles!!! */
1251 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
1252 opj_event_msg(j2k->cinfo, EVT_ERROR,
1253 "JPWL: bad tile number (%d out of a maximum of %d)\n",
1254 tileno, (cp->tw * cp->th));
1256 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1259 /* we try to correct */
1260 tileno = backup_tileno;
1261 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1262 "- setting tile number to %d\n",
1266 /* keep your private count of tiles */
1269 #endif /* USE_JPWL */
1272 if (cp->tileno_size == 0) {
1273 cp->tileno[cp->tileno_size] = tileno;
1277 while (i < cp->tileno_size && status == 0) {
1278 status = cp->tileno[i] == tileno ? 1 : 0;
1282 cp->tileno[cp->tileno_size] = tileno;
1287 totlen = cio_read(cio, 4);
1290 if (j2k->cp->correct) {
1292 /* totlen is negative or larger than the bytes left!!! */
1293 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
1294 opj_event_msg(j2k->cinfo, EVT_ERROR,
1295 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
1296 totlen, cio_numbytesleft(cio) + 8);
1298 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1301 /* we try to correct */
1303 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
1304 "- setting Psot to %d => assuming it is the last tile\n",
1309 #endif /* USE_JPWL */
1312 totlen = cio_numbytesleft(cio) + 8;
1314 partno = cio_read(cio, 1);
1315 numparts = cio_read(cio, 1);
1317 j2k->curtileno = tileno;
1318 j2k->eot = cio_getbp(cio) - 12 + totlen;
1319 j2k->state = J2K_STATE_TPH;
1320 tcp = &cp->tcps[j2k->curtileno];
1322 if (tcp->first == 1) {
1324 /* Initialization PPT */
1325 opj_tccp_t *tmp = tcp->tccps;
1326 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
1328 tcp->ppt_data = NULL;
1329 tcp->ppt_data_first = NULL;
1332 for (i = 0; i < j2k->image->numcomps; i++) {
1333 tcp->tccps[i] = j2k->default_tcp->tccps[i];
1335 cp->tcps[j2k->curtileno].first = 0;
1339 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
1342 opj_tcp_t *tcp = NULL;
1343 opj_image_info_t *image_info = NULL;
1345 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
1346 opj_cp_t *cp = j2k->cp;
1347 opj_cio_t *cio = j2k->cio;
1349 tcd->tp_num = j2k->tp_num ;
1350 tcd->cur_tp_num = j2k->cur_tp_num;
1352 cio_write(cio, J2K_MS_SOD, 2);
1353 if (j2k->curtileno == 0) {
1354 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
1358 image_info = j2k->image_info;
1359 if (image_info && image_info->index_on) {
1360 if (!j2k->cur_tp_num ){
1361 image_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
1363 if(image_info->tile[j2k->curtileno].packet[image_info->num - 1].end_pos < cio_tell(cio))
1364 image_info->tile[j2k->curtileno].packet[image_info->num].start_pos = cio_tell(cio);
1369 tcp = &cp->tcps[j2k->curtileno];
1370 for (layno = 0; layno < tcp->numlayers; layno++) {
1371 tcp->rates[layno] -= tcp->rates[layno] ? (j2k->sod_start / (cp->th * cp->tw)) : 0;
1373 if(image_info && (j2k->cur_tp_num == 0)) {
1374 image_info->num = 0;
1377 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, image_info);
1379 /* Writing Psot in SOT marker */
1380 totlen = cio_tell(cio) + l - j2k->sot_start;
1381 cio_seek(cio, j2k->sot_start + 6);
1382 cio_write(cio, totlen, 4);
1383 cio_seek(cio, j2k->sot_start + totlen);
1384 /* Writing Ttlm and Ptlm in TLM marker */
1386 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
1387 cio_write(cio, j2k->curtileno, 1);
1388 cio_write(cio, totlen, 4);
1390 cio_seek(cio, j2k->sot_start + totlen);
1393 static void j2k_read_sod(opj_j2k_t *j2k) {
1394 int len, truncate = 0, i;
1395 unsigned char *data = NULL, *data_ptr = NULL;
1397 opj_cio_t *cio = j2k->cio;
1398 int curtileno = j2k->curtileno;
1400 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
1402 if (len == cio_numbytesleft(cio) + 1) {
1403 truncate = 1; /* Case of a truncate codestream */
1406 data = (unsigned char *) opj_malloc((j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
1408 for (i = 0; i < j2k->tile_len[curtileno]; i++) {
1409 data[i] = j2k->tile_data[curtileno][i];
1412 data_ptr = data + j2k->tile_len[curtileno];
1413 for (i = 0; i < len; i++) {
1414 data_ptr[i] = cio_read(cio, 1);
1417 j2k->tile_len[curtileno] += len;
1418 opj_free(j2k->tile_data[curtileno]);
1419 j2k->tile_data[curtileno] = data;
1422 j2k->state = J2K_STATE_TPHSOT;
1424 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
1428 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
1430 opj_cp_t *cp = j2k->cp;
1431 opj_tcp_t *tcp = &cp->tcps[tileno];
1432 opj_cio_t *cio = j2k->cio;
1433 int numcomps = j2k->image->numcomps;
1435 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
1436 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
1437 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
1438 cio_write(cio, 0, 1); /* Srgn */
1439 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
1442 static void j2k_read_rgn(opj_j2k_t *j2k) {
1443 int len, compno, roisty;
1445 opj_cp_t *cp = j2k->cp;
1446 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1447 opj_cio_t *cio = j2k->cio;
1448 int numcomps = j2k->image->numcomps;
1450 len = cio_read(cio, 2); /* Lrgn */
1451 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
1452 roisty = cio_read(cio, 1); /* Srgn */
1455 if (j2k->cp->correct) {
1456 /* totlen is negative or larger than the bytes left!!! */
1457 if (compno >= numcomps) {
1458 opj_event_msg(j2k->cinfo, EVT_ERROR,
1459 "JPWL: bad component number in RGN (%d when there are only %d)\n",
1461 if (!JPWL_ASSUME || JPWL_ASSUME) {
1462 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1467 #endif /* USE_JPWL */
1469 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
1472 static void j2k_write_eoc(opj_j2k_t *j2k) {
1473 opj_cio_t *cio = j2k->cio;
1474 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
1475 cio_write(cio, J2K_MS_EOC, 2);
1478 static void j2k_read_eoc(opj_j2k_t *j2k) {
1481 /* if packets should be decoded */
1482 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
1483 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
1484 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
1485 for (i = 0; i < j2k->cp->tileno_size; i++) {
1486 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i);
1487 tileno = j2k->cp->tileno[i];
1488 tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno);
1489 opj_free(j2k->tile_data[tileno]);
1490 j2k->tile_data[tileno] = NULL;
1491 tcd_free_decode_tile(tcd, i);
1493 tcd_free_decode(tcd);
1496 /* if packets should not be decoded */
1498 for (i = 0; i < j2k->cp->tileno_size; i++) {
1499 tileno = j2k->cp->tileno[i];
1500 opj_free(j2k->tile_data[tileno]);
1501 j2k->tile_data[tileno] = NULL;
1505 j2k->state = J2K_STATE_MT;
1508 typedef struct opj_dec_mstabent {
1511 /** value of the state when the marker can appear */
1513 /** action linked to the marker */
1514 void (*handler) (opj_j2k_t *j2k);
1515 } opj_dec_mstabent_t;
1517 opj_dec_mstabent_t j2k_dec_mstab[] = {
1518 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1519 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1520 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1521 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1522 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1523 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1524 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1525 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1526 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1527 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1528 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1529 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1530 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1531 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1532 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1533 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1535 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1536 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1539 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1540 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1541 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1542 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1543 #endif /* USE_JPWL */
1545 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1548 static void j2k_read_unk(opj_j2k_t *j2k) {
1549 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
1552 if (j2k->cp->correct) {
1554 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
1555 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1556 id = cio_read(j2k->cio, 2);
1557 opj_event_msg(j2k->cinfo, EVT_ERROR,
1558 "JPWL: really don't know this marker %x\n",
1561 opj_event_msg(j2k->cinfo, EVT_ERROR,
1562 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
1565 /* OK, activate this at your own risk!!! */
1566 /* we look for the marker at the minimum hamming distance from this */
1567 while (j2k_dec_mstab[m].id) {
1569 /* 1's where they differ */
1570 tmp_id = j2k_dec_mstab[m].id ^ id;
1572 /* compute the hamming distance between our id and the current */
1574 for (i = 0; i < 16; i++) {
1575 if ((tmp_id >> i) & 0x0001) {
1580 /* if current distance is smaller, set the minimum */
1581 if (cur_dist < min_dist) {
1582 min_dist = cur_dist;
1583 min_id = j2k_dec_mstab[m].id;
1586 /* jump to the next marker */
1590 /* do we substitute the marker? */
1591 if (min_dist < JPWL_MAXIMUM_HAMMING) {
1592 opj_event_msg(j2k->cinfo, EVT_ERROR,
1593 "- marker %x is at distance %d from the read %x\n",
1594 min_id, min_dist, id);
1595 opj_event_msg(j2k->cinfo, EVT_ERROR,
1596 "- trying to substitute in place and crossing fingers!\n");
1597 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1598 cio_write(j2k->cio, min_id, 2);
1601 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
1606 #endif /* USE_JPWL */
1611 Read the lookup table containing all the marker, status and action
1612 @param id Marker value
1614 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
1615 opj_dec_mstabent_t *e;
1616 for (e = j2k_dec_mstab; e->id != 0; e++) {
1624 /* ----------------------------------------------------------------------- */
1625 /* J2K / JPT decoder interface */
1626 /* ----------------------------------------------------------------------- */
1628 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
1629 opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1632 j2k->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
1633 if(!j2k->default_tcp) {
1641 void j2k_destroy_decompress(opj_j2k_t *j2k) {
1644 if(j2k->tile_len != NULL) {
1645 opj_free(j2k->tile_len);
1647 if(j2k->tile_data != NULL) {
1648 opj_free(j2k->tile_data);
1650 if(j2k->default_tcp != NULL) {
1651 opj_tcp_t *default_tcp = j2k->default_tcp;
1652 if(default_tcp->ppt_data_first != NULL) {
1653 opj_free(default_tcp->ppt_data_first);
1655 if(j2k->default_tcp->tccps != NULL) {
1656 opj_free(j2k->default_tcp->tccps);
1658 opj_free(j2k->default_tcp);
1660 if(j2k->cp != NULL) {
1661 opj_cp_t *cp = j2k->cp;
1662 if(cp->tcps != NULL) {
1663 for(i = 0; i < cp->tw * cp->th; i++) {
1664 if(cp->tcps[i].ppt_data_first != NULL) {
1665 opj_free(cp->tcps[i].ppt_data_first);
1667 if(cp->tcps[i].tccps != NULL) {
1668 opj_free(cp->tcps[i].tccps);
1673 if(cp->ppm_data_first != NULL) {
1674 opj_free(cp->ppm_data_first);
1676 if(cp->tileno != NULL) {
1677 opj_free(cp->tileno);
1679 if(cp->comment != NULL) {
1680 opj_free(cp->comment);
1689 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
1690 if(j2k && parameters) {
1691 /* create and initialize the coding parameters structure */
1692 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1693 cp->reduce = parameters->cp_reduce;
1694 cp->layer = parameters->cp_layer;
1695 cp->limit_decoding = parameters->cp_limit_decoding;
1698 cp->correct = parameters->jpwl_correct;
1699 cp->exp_comps = parameters->jpwl_exp_comps;
1700 cp->max_tiles = parameters->jpwl_max_tiles;
1701 #endif /* USE_JPWL */
1704 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
1709 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio) {
1710 opj_image_t *image = NULL;
1712 opj_common_ptr cinfo = j2k->cinfo;
1716 /* create an empty image */
1717 image = opj_image_create0();
1720 j2k->state = J2K_STATE_MHSOC;
1723 opj_dec_mstabent_t *e;
1724 int id = cio_read(cio, 2);
1728 /* we try to honor JPWL correction power */
1729 if (j2k->cp->correct) {
1731 int orig_pos = cio_tell(cio);
1734 /* call the corrector */
1735 status = jpwl_correct(j2k);
1737 /* go back to where you were */
1738 cio_seek(cio, orig_pos - 2);
1740 /* re-read the marker */
1741 id = cio_read(cio, 2);
1743 /* check whether it begins with ff */
1744 if (id >> 8 != 0xff) {
1745 opj_event_msg(cinfo, EVT_ERROR,
1746 "JPWL: possible bad marker %x at %d\n",
1747 id, cio_tell(cio) - 2);
1749 opj_image_destroy(image);
1750 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
1753 /* we try to correct */
1755 cio_seek(cio, cio_tell(cio) - 2);
1756 cio_write(cio, id, 2);
1757 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
1758 "- setting marker to %x\n",
1763 #endif /* USE_JPWL */
1765 if (id >> 8 != 0xff) {
1766 opj_image_destroy(image);
1767 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1770 e = j2k_dec_mstab_lookup(id);
1771 // Check if the marker is known
1772 if (!(j2k->state & e->states)) {
1773 opj_image_destroy(image);
1774 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1777 // Check if the decoding is limited to the main header
1778 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
1779 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
1786 if (j2k->state == J2K_STATE_MT) {
1789 if (j2k->state == J2K_STATE_NEOC) {
1793 if (j2k->state == J2K_STATE_NEOC) {
1797 if (j2k->state != J2K_STATE_MT) {
1798 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1805 * Read a JPT-stream and decode file
1808 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio) {
1809 opj_image_t *image = NULL;
1810 opj_jpt_msg_header_t header;
1813 opj_common_ptr cinfo = j2k->cinfo;
1817 /* create an empty image */
1818 image = opj_image_create0();
1820 j2k->state = J2K_STATE_MHSOC;
1822 /* Initialize the header */
1823 jpt_init_msg_header(&header);
1824 /* Read the first header of the message */
1825 jpt_read_msg_header(cinfo, cio, &header);
1827 position = cio_tell(cio);
1828 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
1829 opj_image_destroy(image);
1830 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
1835 opj_dec_mstabent_t *e = NULL;
1838 if (!cio_numbytesleft(cio)) {
1842 /* data-bin read -> need to read a new header */
1843 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
1844 jpt_read_msg_header(cinfo, cio, &header);
1845 position = cio_tell(cio);
1846 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
1847 opj_image_destroy(image);
1848 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
1853 id = cio_read(cio, 2);
1854 if (id >> 8 != 0xff) {
1855 opj_image_destroy(image);
1856 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
1859 e = j2k_dec_mstab_lookup(id);
1860 if (!(j2k->state & e->states)) {
1861 opj_image_destroy(image);
1862 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
1868 if (j2k->state == J2K_STATE_MT) {
1871 if (j2k->state == J2K_STATE_NEOC) {
1875 if (j2k->state == J2K_STATE_NEOC) {
1879 if (j2k->state != J2K_STATE_MT) {
1880 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
1886 /* ----------------------------------------------------------------------- */
1887 /* J2K encoder interface */
1888 /* ----------------------------------------------------------------------- */
1890 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
1891 opj_j2k_t *j2k = (opj_j2k_t*)opj_malloc(sizeof(opj_j2k_t));
1898 void j2k_destroy_compress(opj_j2k_t *j2k) {
1903 if(j2k->image_info != NULL) {
1904 opj_image_info_t *image_info = j2k->image_info;
1905 if (image_info->index_on && j2k->cp) {
1906 opj_cp_t *cp = j2k->cp;
1907 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1908 opj_tile_info_t *tile_info = &image_info->tile[tileno];
1909 opj_free(tile_info->thresh);
1910 opj_free(tile_info->packet);
1912 opj_free(image_info->tile);
1914 opj_free(image_info);
1916 if(j2k->cp != NULL) {
1917 opj_cp_t *cp = j2k->cp;
1920 opj_free(cp->comment);
1923 opj_free(cp->matrice);
1925 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1926 opj_free(cp->tcps[tileno].tccps);
1935 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
1936 int i, j, tileno, numpocs_tile;
1937 opj_cp_t *cp = NULL;
1939 if(!j2k || !parameters || ! image) {
1943 /* create and initialize the coding parameters structure */
1944 cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
1946 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
1949 /* set default values for cp */
1954 copy user encoding parameters
1956 cp->cinema = parameters->cp_cinema;
1957 cp->max_comp_size = parameters->max_comp_size;
1958 cp->rsiz = parameters->cp_rsiz;
1959 cp->disto_alloc = parameters->cp_disto_alloc;
1960 cp->fixed_alloc = parameters->cp_fixed_alloc;
1961 cp->fixed_quality = parameters->cp_fixed_quality;
1963 /* mod fixed_quality */
1964 if(parameters->cp_matrice) {
1965 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
1966 cp->matrice = (int *) opj_malloc(array_size);
1967 memcpy(cp->matrice, parameters->cp_matrice, array_size);
1970 /* creation of an index file ? */
1971 cp->index_on = parameters->index_on;
1973 j2k->image_info = (opj_image_info_t*)opj_malloc(sizeof(opj_image_info_t));
1977 cp->tdx = parameters->cp_tdx;
1978 cp->tdy = parameters->cp_tdy;
1981 cp->tx0 = parameters->cp_tx0;
1982 cp->ty0 = parameters->cp_ty0;
1984 /* comment string */
1985 if(parameters->cp_comment) {
1986 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
1988 strcpy(cp->comment, parameters->cp_comment);
1993 calculate other encoding parameters
1996 if (parameters->tile_size_on) {
1997 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1998 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2000 cp->tdx = image->x1 - cp->tx0;
2001 cp->tdy = image->y1 - cp->ty0;
2004 if(parameters->tp_on){
2005 cp->tp_flag = parameters->tp_flag;
2010 for(i=0;i<image->numcomps ;i++){
2011 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
2017 calculate JPWL encoding parameters
2020 if (parameters->jpwl_epc_on) {
2025 cp->info_on = false; /* no informative technique */
2028 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
2031 cp->hprot_MH = parameters->jpwl_hprot_MH;
2032 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2033 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
2034 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
2036 /* if tile specs are not specified, copy MH specs */
2037 if (cp->hprot_TPH[0] == -1) {
2038 cp->hprot_TPH_tileno[0] = 0;
2039 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
2041 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
2042 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
2043 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
2044 cp->pprot[i] = parameters->jpwl_pprot[i];
2048 /* set ESD writing */
2049 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
2052 cp->sens_size = parameters->jpwl_sens_size;
2053 cp->sens_addr = parameters->jpwl_sens_addr;
2054 cp->sens_range = parameters->jpwl_sens_range;
2056 cp->sens_MH = parameters->jpwl_sens_MH;
2057 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
2058 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
2059 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
2063 /* always set RED writing to false: we are at the encoder */
2069 #endif /* USE_JPWL */
2072 /* initialize the mutiple tiles */
2073 /* ---------------------------- */
2074 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcp_t));
2076 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2077 opj_tcp_t *tcp = &cp->tcps[tileno];
2078 tcp->numlayers = parameters->tcp_numlayers;
2079 for (j = 0; j < tcp->numlayers; j++) {
2081 if (cp->fixed_quality) {
2082 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2084 tcp->rates[j] = parameters->tcp_rates[j];
2086 if (cp->fixed_quality) { /* add fixed_quality */
2087 tcp->distoratio[j] = parameters->tcp_distoratio[j];
2089 tcp->rates[j] = parameters->tcp_rates[j];
2093 tcp->csty = parameters->csty;
2094 tcp->prg = parameters->prog_order;
2095 tcp->mct = parameters->tcp_mct;
2099 if (parameters->numpocs) {
2100 /* initialisation of POC */
2102 j2k_check_poc_val(parameters, image->numcomps, tcp->numlayers);
2103 for (i = 0; i < parameters->numpocs; i++) {
2104 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
2105 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
2106 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
2107 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
2108 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
2109 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
2110 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
2111 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
2112 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
2116 tcp->numpocs = numpocs_tile -1 ;
2121 tcp->tccps = (opj_tccp_t *) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2123 for (i = 0; i < image->numcomps; i++) {
2124 opj_tccp_t *tccp = &tcp->tccps[i];
2125 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
2126 tccp->numresolutions = parameters->numresolution;
2127 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
2128 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
2129 tccp->cblksty = parameters->mode;
2130 tccp->qmfbid = parameters->irreversible ? 0 : 1;
2131 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
2133 if (i == parameters->roi_compno) {
2134 tccp->roishift = parameters->roi_shift;
2139 if(parameters->cp_cinema)
2141 //Precinct size for lowest frequency subband=128
2144 //Precinct size at all other resolutions = 256
2145 for (j = 1; j < tccp->numresolutions; j++) {
2150 if (parameters->csty & J2K_CCP_CSTY_PRT) {
2152 for (j = tccp->numresolutions - 1; j >= 0; j--) {
2153 if (p < parameters->res_spec) {
2155 if (parameters->prcw_init[p] < 1) {
2158 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
2161 if (parameters->prch_init[p] < 1) {
2164 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
2168 int res_spec = parameters->res_spec;
2169 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
2170 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
2172 if (size_prcw < 1) {
2175 tccp->prcw[j] = int_floorlog2(size_prcw);
2178 if (size_prch < 1) {
2181 tccp->prch[j] = int_floorlog2(size_prch);
2185 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
2188 for (j = 0; j < tccp->numresolutions; j++) {
2195 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
2201 Create an index file
2205 @param index Index filename
2206 @return Returns 1 if successful, returns 0 otherwise
2208 static int j2k_create_index(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_info_t *image_info, char *index) {
2209 int tileno, compno, layno, resno, precno, pack_nb, x, y;
2210 FILE *stream = NULL;
2211 double total_disto = 0;
2213 image_info->codestream_size = cio_tell(cio) + j2k->pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
2217 /* if JPWL is enabled and the name coincides with our own set
2218 then discard the creation of the file: this was just done to
2219 enable indexing, we do not want an index file
2221 if (j2k->cp->epc_on && !strcmp(index, JPWL_PRIVATEINDEX_NAME))
2223 #endif /* USE_JPWL */
2226 stream = fopen(index, "w");
2228 opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
2232 fprintf(stream, "%d %d\n", image_info->image_w, image_info->image_h);
2233 fprintf(stream, "%d\n", image_info->prog);
2234 fprintf(stream, "%d %d\n", image_info->tile_x, image_info->tile_y);
2235 fprintf(stream, "%d %d\n", image_info->tw, image_info->th);
2236 fprintf(stream, "%d\n", image_info->comp);
2237 fprintf(stream, "%d\n", image_info->layer);
2238 fprintf(stream, "%d\n", image_info->decomposition);
2240 for (resno = image_info->decomposition; resno >= 0; resno--) {
2241 fprintf(stream, "[%d,%d] ",
2242 (1 << image_info->tile[0].pdx[resno]), (1 << image_info->tile[0].pdx[resno])); /* based on tile 0 */
2244 fprintf(stream, "\n");
2245 fprintf(stream, "%d\n", image_info->main_head_end);
2246 fprintf(stream, "%d\n", image_info->codestream_size);
2248 for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
2249 fprintf(stream, "%4d %9d %9d %9d %9e %9d %9e\n",
2250 image_info->tile[tileno].num_tile,
2251 image_info->tile[tileno].start_pos,
2252 image_info->tile[tileno].end_header,
2253 image_info->tile[tileno].end_pos,
2254 image_info->tile[tileno].distotile, image_info->tile[tileno].nbpix,
2255 image_info->tile[tileno].distotile / image_info->tile[tileno].nbpix);
2258 for (tileno = 0; tileno < image_info->tw * image_info->th; tileno++) {
2259 int start_pos, end_pos;
2263 if (image_info->prog == LRCP) { /* LRCP */
2265 fprintf(stream, "pack_nb tileno layno resno compno precno start_pos end_pos disto\n");
2267 for (layno = 0; layno < image_info->layer; layno++) {
2268 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2269 for (compno = 0; compno < image_info->comp; compno++) {
2270 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2271 for (precno = 0; precno < prec_max; precno++) {
2272 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2273 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2274 disto = image_info->tile[tileno].packet[pack_nb].disto;
2275 fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
2276 pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
2277 total_disto += disto;
2284 else if (image_info->prog == RLCP) { /* RLCP */
2286 fprintf(stream, "pack_nb tileno resno layno compno precno start_pos end_pos disto\n");
2288 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2289 for (layno = 0; layno < image_info->layer; layno++) {
2290 for (compno = 0; compno < image_info->comp; compno++) {
2291 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2292 for (precno = 0; precno < prec_max; precno++) {
2293 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2294 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2295 disto = image_info->tile[tileno].packet[pack_nb].disto;
2296 fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
2297 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
2298 total_disto += disto;
2305 else if (image_info->prog == RPCL) { /* RPCL */
2307 fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n");
2309 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2310 /* I suppose components have same XRsiz, YRsiz */
2311 int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
2312 int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
2313 int x1 = x0 + image_info->tile_x;
2314 int y1 = y0 + image_info->tile_y;
2315 for (compno = 0; compno < image_info->comp; compno++) {
2316 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2317 for (precno = 0; precno < prec_max; precno++) {
2318 int pcnx = image_info->tile[tileno].pw[resno];
2319 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
2320 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
2321 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2322 int precno_y = (int) floor( (float)precno/(float)pcnx );
2323 for(y = y0; y < y1; y++) {
2324 if (precno_y*pcy == y ) {
2325 for (x = x0; x < x1; x++) {
2326 if (precno_x*pcx == x ) {
2327 for (layno = 0; layno < image_info->layer; layno++) {
2328 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2329 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2330 disto = image_info->tile[tileno].packet[pack_nb].disto;
2331 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
2332 pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
2333 total_disto += disto;
2344 else if (image_info->prog == PCRL) { /* PCRL */
2345 /* I suppose components have same XRsiz, YRsiz */
2346 int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
2347 int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
2348 int x1 = x0 + image_info->tile_x;
2349 int y1 = y0 + image_info->tile_y;
2351 fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n");
2353 for (compno = 0; compno < image_info->comp; compno++) {
2354 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2355 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2356 for (precno = 0; precno < prec_max; precno++) {
2357 int pcnx = image_info->tile[tileno].pw[resno];
2358 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
2359 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
2360 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2361 int precno_y = (int) floor( (float)precno/(float)pcnx );
2362 for(y = y0; y < y1; y++) {
2363 if (precno_y*pcy == y ) {
2364 for (x = x0; x < x1; x++) {
2365 if (precno_x*pcx == x ) {
2366 for (layno = 0; layno < image_info->layer; layno++) {
2367 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2368 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2369 disto = image_info->tile[tileno].packet[pack_nb].disto;
2370 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2371 pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
2372 total_disto += disto;
2385 fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n");
2387 for (compno = 0; compno < image_info->comp; compno++) {
2388 /* I suppose components have same XRsiz, YRsiz */
2389 int x0 = image_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tw * image_info->tile_x;
2390 int y0 = image_info->tile_Ox + (int)floor( (float)tileno/(float)image_info->tw ) * image_info->tile_y;
2391 int x1 = x0 + image_info->tile_x;
2392 int y1 = y0 + image_info->tile_y;
2394 for (resno = 0; resno < image_info->decomposition + 1; resno++) {
2395 int prec_max = image_info->tile[tileno].pw[resno] * image_info->tile[tileno].ph[resno];
2396 for (precno = 0; precno < prec_max; precno++) {
2397 int pcnx = image_info->tile[tileno].pw[resno];
2398 int pcx = (int) pow( 2, image_info->tile[tileno].pdx[resno] + image_info->decomposition - resno );
2399 int pcy = (int) pow( 2, image_info->tile[tileno].pdy[resno] + image_info->decomposition - resno );
2400 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
2401 int precno_y = (int) floor( (float)precno/(float)pcnx );
2402 for(y = y0; y < y1; y++) {
2403 if (precno_y*pcy == y ) {
2404 for (x = x0; x < x1; x++) {
2405 if (precno_x*pcx == x ) {
2406 for (layno = 0; layno < image_info->layer; layno++) {
2407 start_pos = image_info->tile[tileno].packet[pack_nb].start_pos;
2408 end_pos = image_info->tile[tileno].packet[pack_nb].end_pos;
2409 disto = image_info->tile[tileno].packet[pack_nb].disto;
2410 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
2411 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
2412 total_disto += disto;
2425 fprintf(stream, "%8e\n", image_info->D_max); /* SE max */
2426 fprintf(stream, "%.8e\n", total_disto); /* SE totale */
2432 bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, char *index) {
2434 opj_image_info_t *image_info = NULL;
2435 opj_cp_t *cp = NULL;
2437 opj_tcd_t *tcd = NULL; /* TCD component */
2444 /* j2k_dump_cp(stdout, image, cp); */
2447 image_info = j2k->image_info;
2448 if (image_info && cp->index_on) {
2449 image_info->index_on = cp->index_on;
2450 image_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
2451 image_info->image_w = image->x1 - image->x0;
2452 image_info->image_h = image->y1 - image->y0;
2453 image_info->prog = (&cp->tcps[0])->prg;
2454 image_info->tw = cp->tw;
2455 image_info->th = cp->th;
2456 image_info->tile_x = cp->tdx; /* new version parser */
2457 image_info->tile_y = cp->tdy; /* new version parser */
2458 image_info->tile_Ox = cp->tx0; /* new version parser */
2459 image_info->tile_Oy = cp->ty0; /* new version parser */
2460 image_info->comp = image->numcomps;
2461 image_info->layer = (&cp->tcps[0])->numlayers;
2462 image_info->decomposition = (&cp->tcps[0])->tccps->numresolutions - 1;
2463 image_info->D_max = 0; /* ADD Marcela */
2473 for (compno = 1; compno < image->numcomps; compno++) {
2474 j2k_write_coc(j2k, compno);
2475 j2k_write_qcc(j2k, compno);
2479 for (compno = 0; compno < image->numcomps; compno++) {
2480 opj_tcp_t *tcp = &cp->tcps[0];
2481 if (tcp->tccps[compno].roishift)
2482 j2k_write_rgn(j2k, compno, 0);
2484 if (cp->comment != NULL) {
2488 if(image_info && image_info->index_on) {
2489 image_info->main_head_end = cio_tell(cio) - 1;
2493 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
2497 if (cp->cinema == CINEMA4K_24) {
2501 /**** Main Header ENDS here ***/
2503 /* create the tile encoder */
2504 tcd = tcd_create(j2k->cinfo);
2506 /* encode each tile */
2508 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
2512 opj_tcp_t *tcp = &cp->tcps[tileno];
2513 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
2515 j2k->curtileno = tileno;
2516 j2k->cur_tp_num = 0;
2517 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
2518 /* initialisation before tile encoding */
2520 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
2522 tcd_init_encode(tcd, image, cp, j2k->curtileno);
2526 if(image_info && image_info->index_on) {
2527 image_info->tile[j2k->curtileno].num_tile = j2k->curtileno;
2528 image_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
2532 for(pino = 0; pino <= tcp->numpocs; pino++) {
2536 /*Get number of tile parts*/
2537 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
2538 tcd->tp_pos = cp->tp_pos;
2540 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
2541 j2k->tp_num = tilepartno;
2544 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
2545 for (compno = 1; compno < image->numcomps; compno++) {
2546 j2k_write_coc(j2k, compno);
2547 j2k_write_qcc(j2k, compno);
2549 if (cp->tcps[tileno].numpocs) {
2554 j2k_write_sod(j2k, tcd);
2560 if(image_info && image_info->index_on) {
2561 image_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
2567 if (tile->PPT) { // BAD PPT !!!
2570 PPT_file=fopen("PPT","rb");
2571 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
2572 for (i=0;i<tile->len_ppt;i++) {
2574 fread(&elmt, 1, 1, PPT_file);
2575 fwrite(&elmt,1,1,f);
2584 /* destroy the tile encoder */
2585 tcd_free_encode(tcd);
2588 free(j2k->cur_totnum_tp);
2592 /* Creation of the index file */
2593 if(image_info && image_info->index_on) {
2594 if(!j2k_create_index(j2k, cio, image_info, index)) {
2595 opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
2603 preparation of JPWL marker segments: can be finalized only when the whole
2606 if(image_info && image_info->index_on && cp->epc_on) {
2608 /* let's begin creating a marker list, according to user wishes */
2609 jpwl_prepare_marks(j2k, cio, image);
2611 /* now we dump the JPWL markers on the codestream */
2612 jpwl_dump_marks(j2k, cio, image);
2614 /* do not know exactly what is this for,
2615 but it gets called during index creation */
2616 j2k->pos_correction = 0;
2618 /* Re-creation of the index file, with updated info */
2619 if(image_info && image_info->index_on) {
2620 if(!j2k_create_index(j2k, cio, image_info, index)) {
2621 opj_event_msg(j2k->cinfo, EVT_ERROR, "failed to re-create index file %s\n", index);
2626 /* now we finalize the marker contents */
2627 /*jpwl_finalize_marks(j2k, cio, image);*/
2630 #endif /* USE_JPWL */