2 * Copyright (c) 2001-2002, David Janssens
3 * Copyright (c) 2002-2004, Yannick Verschueren
4 * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
42 #define J2K_MS_SOC 0xff4f
43 #define J2K_MS_SOT 0xff90
44 #define J2K_MS_SOD 0xff93
45 #define J2K_MS_EOC 0xffd9
46 #define J2K_MS_SIZ 0xff51
47 #define J2K_MS_COD 0xff52
48 #define J2K_MS_COC 0xff53
49 #define J2K_MS_RGN 0xff5e
50 #define J2K_MS_QCD 0xff5c
51 #define J2K_MS_QCC 0xff5d
52 #define J2K_MS_POC 0xff5f
53 #define J2K_MS_TLM 0xff55
54 #define J2K_MS_PLM 0xff57
55 #define J2K_MS_PLT 0xff58
56 #define J2K_MS_PPM 0xff60
57 #define J2K_MS_PPT 0xff61
58 #define J2K_MS_SOP 0xff91
59 #define J2K_MS_EPH 0xff92
60 #define J2K_MS_CRG 0xff63
61 #define J2K_MS_COM 0xff64
63 #define J2K_STATE_MHSOC 0x0001
64 #define J2K_STATE_MHSIZ 0x0002
65 #define J2K_STATE_MH 0x0004
66 #define J2K_STATE_TPHSOT 0x0008
67 #define J2K_STATE_TPH 0x0010
68 #define J2K_STATE_MT 0x0020
69 #define J2K_STATE_NEOC 0x0040
75 static int j2k_curtileno;
76 j2k_tcp_t j2k_default_tcp;
77 static unsigned char *j2k_eot;
78 static int j2k_sot_start;
79 static int pos_correction;
81 static j2k_image_t *j2k_img;
82 static j2k_cp_t *j2k_cp;
84 static unsigned char **j2k_tile_data;
85 static int *j2k_tile_len;
87 static info_image info_IM;
93 #ifndef NO_PACKETS_DECODING
94 tcd_free_encode(j2k_img, j2k_cp, j2k_curtileno);
97 if (info_IM.index_on) {
98 for (tileno = 0; tileno < j2k_cp->tw * j2k_cp->th; tileno++) {
99 free(info_IM.tile[tileno].packet);
107 void j2k_dump_image(j2k_image_t * img)
110 fprintf(stdout, "image {\n");
111 fprintf(stdout, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0,
113 fprintf(stdout, " numcomps=%d\n", img->numcomps);
114 for (compno = 0; compno < img->numcomps; compno++) {
115 j2k_comp_t *comp = &img->comps[compno];
116 fprintf(stdout, " comp %d {\n", compno);
117 fprintf(stdout, " dx=%d, dy=%d\n", comp->dx, comp->dy);
118 fprintf(stdout, " prec=%d\n", comp->prec);
119 fprintf(stdout, " sgnd=%d\n", comp->sgnd);
120 fprintf(stdout, " }\n");
122 fprintf(stdout, "}\n");
125 void j2k_dump_cp(j2k_image_t * img, j2k_cp_t * cp)
127 int tileno, compno, layno, bandno, resno, numbands;
128 fprintf(stdout, "coding parameters {\n");
129 fprintf(stdout, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
130 fprintf(stdout, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
131 fprintf(stdout, " tw=%d, th=%d\n", cp->tw, cp->th);
132 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
133 j2k_tcp_t *tcp = &cp->tcps[tileno];
134 fprintf(stdout, " tile %d {\n", tileno);
135 fprintf(stdout, " csty=%x\n", tcp->csty);
136 fprintf(stdout, " prg=%d\n", tcp->prg);
137 fprintf(stdout, " numlayers=%d\n", tcp->numlayers);
138 fprintf(stdout, " mct=%d\n", tcp->mct);
139 fprintf(stdout, " rates=");
140 for (layno = 0; layno < tcp->numlayers; layno++) {
141 fprintf(stdout, "%d ", tcp->rates[layno]);
143 fprintf(stdout, "\n");
144 for (compno = 0; compno < img->numcomps; compno++) {
145 j2k_tccp_t *tccp = &tcp->tccps[compno];
146 fprintf(stdout, " comp %d {\n", compno);
147 fprintf(stdout, " csty=%x\n", tccp->csty);
148 fprintf(stdout, " numresolutions=%d\n", tccp->numresolutions);
149 fprintf(stdout, " cblkw=%d\n", tccp->cblkw);
150 fprintf(stdout, " cblkh=%d\n", tccp->cblkh);
151 fprintf(stdout, " cblksty=%x\n", tccp->cblksty);
152 fprintf(stdout, " qmfbid=%d\n", tccp->qmfbid);
153 fprintf(stdout, " qntsty=%d\n", tccp->qntsty);
154 fprintf(stdout, " numgbits=%d\n", tccp->numgbits);
155 fprintf(stdout, " roishift=%d\n", tccp->roishift);
156 fprintf(stdout, " stepsizes=");
159 J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
160 for (bandno = 0; bandno < numbands; bandno++) {
161 fprintf(stdout, "(%d,%d) ", tccp->stepsizes[bandno].mant,
162 tccp->stepsizes[bandno].expn);
164 fprintf(stdout, "\n");
166 if (tccp->csty & J2K_CCP_CSTY_PRT) {
167 fprintf(stdout, " prcw=");
168 for (resno = 0; resno < tccp->numresolutions; resno++) {
169 fprintf(stdout, "%d ", tccp->prcw[resno]);
171 fprintf(stdout, "\n");
172 fprintf(stdout, " prch=");
173 for (resno = 0; resno < tccp->numresolutions; resno++) {
174 fprintf(stdout, "%d ", tccp->prch[resno]);
176 fprintf(stdout, "\n");
178 fprintf(stdout, " }\n");
180 fprintf(stdout, " }\n");
182 fprintf(stdout, "}\n");
187 cio_write(J2K_MS_SOC, 2);
192 j2k_state = J2K_STATE_MHSIZ;
200 cio_write(J2K_MS_SIZ, 2); /* SIZ */
203 cio_write(0, 2); /* Rsiz (capabilities) */
204 cio_write(j2k_img->x1, 4); /* Xsiz */
205 cio_write(j2k_img->y1, 4); /* Ysiz */
206 cio_write(j2k_img->x0, 4); /* X0siz */
207 cio_write(j2k_img->y0, 4); /* Y0siz */
208 cio_write(j2k_cp->tdx, 4); /* XTsiz */
209 cio_write(j2k_cp->tdy, 4); /* YTsiz */
210 cio_write(j2k_cp->tx0, 4); /* XT0siz */
211 cio_write(j2k_cp->ty0, 4); /* YT0siz */
212 cio_write(j2k_img->numcomps, 2); /* Csiz */
213 for (i = 0; i < j2k_img->numcomps; i++) {
214 cio_write(j2k_img->comps[i].prec - 1 + (j2k_img->comps[i].sgnd << 7), 1); /* Ssiz_i */
215 cio_write(j2k_img->comps[i].dx, 1); /* XRsiz_i */
216 cio_write(j2k_img->comps[i].dy, 1); /* YRsiz_i */
218 len = cio_tell() - lenp;
220 cio_write(len, 2); /* Lsiz */
221 cio_seek(lenp + len);
229 len = cio_read(2); /* Lsiz */
230 cio_read(2); /* Rsiz (capabilities) */
231 j2k_img->x1 = cio_read(4); /* Xsiz */
232 j2k_img->y1 = cio_read(4); /* Ysiz */
233 j2k_img->x0 = cio_read(4); /* X0siz */
234 j2k_img->y0 = cio_read(4); /* Y0siz */
235 j2k_cp->tdx = cio_read(4); /* XTsiz */
236 j2k_cp->tdy = cio_read(4); /* YTsiz */
237 j2k_cp->tx0 = cio_read(4); /* XT0siz */
238 j2k_cp->ty0 = cio_read(4); /* YT0siz */
240 j2k_img->numcomps = cio_read(2); /* Csiz */
242 (j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t));
243 for (i = 0; i < j2k_img->numcomps; i++) {
245 tmp = cio_read(1); /* Ssiz_i */
246 j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
247 j2k_img->comps[i].sgnd = tmp >> 7;
248 j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */
249 j2k_img->comps[i].dy = cio_read(1); /* YRsiz_i */
250 w = int_ceildiv(j2k_img->x1 - j2k_img->x0, j2k_img->comps[i].dx);
251 h = int_ceildiv(j2k_img->y1 - j2k_img->y0, j2k_img->comps[i].dy);
252 j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */
253 j2k_img->comps[i].factor = 0; /* reducing factor by component */
256 j2k_cp->tw = int_ceildiv(j2k_img->x1 - j2k_cp->tx0, j2k_cp->tdx);
257 j2k_cp->th = int_ceildiv(j2k_img->y1 - j2k_cp->ty0, j2k_cp->tdy);
259 (j2k_tcp_t *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(j2k_tcp_t));
260 j2k_cp->tileno = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
261 j2k_cp->tileno_size = 0;
263 for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
264 j2k_cp->tcps[i].POC = 0;
265 j2k_cp->tcps[i].numpocs = 0;
266 j2k_cp->tcps[i].first = 1;
269 /* Initialization for PPM marker */
271 j2k_cp->ppm_data = NULL;
272 j2k_cp->ppm_data_first = NULL;
273 j2k_cp->ppm_previous = 0;
274 j2k_cp->ppm_store = 0;
276 j2k_default_tcp.tccps =
277 (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
278 for (i = 0; i < j2k_cp->tw * j2k_cp->th; i++) {
279 j2k_cp->tcps[i].tccps =
280 (j2k_tccp_t *) calloc(sizeof(j2k_tccp_t), j2k_img->numcomps);
283 (unsigned char **) calloc(j2k_cp->tw * j2k_cp->th, sizeof(char *));
284 j2k_tile_len = (int *) calloc(j2k_cp->tw * j2k_cp->th, sizeof(int));
285 j2k_state = J2K_STATE_MH;
295 sprintf(str, "%s", j2k_cp->comment);
297 cio_write(J2K_MS_COM, 2);
301 for (i = 0; i < strlen(str); i++) {
302 cio_write(str[i], 1);
304 len = cio_tell() - lenp;
307 cio_seek(lenp + len);
320 void j2k_write_cox(int compno)
325 tcp = &j2k_cp->tcps[j2k_curtileno];
326 tccp = &tcp->tccps[compno];
328 cio_write(tccp->numresolutions - 1, 1); /* SPcox (D) */
329 cio_write(tccp->cblkw - 2, 1); /* SPcox (E) */
330 cio_write(tccp->cblkh - 2, 1); /* SPcox (F) */
331 cio_write(tccp->cblksty, 1); /* SPcox (G) */
332 cio_write(tccp->qmfbid, 1); /* SPcox (H) */
334 if (tccp->csty & J2K_CCP_CSTY_PRT) {
335 for (i = 0; i < tccp->numresolutions; i++) {
336 cio_write(tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
341 void j2k_read_cox(int compno)
348 J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
349 tccp = &tcp->tccps[compno];
350 tccp->numresolutions = cio_read(1) + 1; /* SPcox (D) */
351 /*Check the reduce value*/
352 j2k_cp->reduce=int_min((tccp->numresolutions)-1,j2k_cp->reduce);
353 tccp->cblkw = cio_read(1) + 2; /* SPcox (E) */
354 tccp->cblkh = cio_read(1) + 2; /* SPcox (F) */
355 tccp->cblksty = cio_read(1); /* SPcox (G) */
356 tccp->qmfbid = cio_read(1); /* SPcox (H) */
357 if (tccp->csty & J2K_CP_CSTY_PRT) {
358 for (i = 0; i < tccp->numresolutions; i++) {
359 int tmp = cio_read(1); /* SPcox (I_i) */
360 tccp->prcw[i] = tmp & 0xf;
361 tccp->prch[i] = tmp >> 4;
371 cio_write(J2K_MS_COD, 2); /* COD */
376 tcp = &j2k_cp->tcps[j2k_curtileno];
377 cio_write(tcp->csty, 1); /* Scod */
378 cio_write(tcp->prg, 1); /* SGcod (A) */
379 cio_write(tcp->numlayers, 2); /* SGcod (B) */
380 cio_write(tcp->mct, 1); /* SGcod (C) */
383 len = cio_tell() - lenp;
385 cio_write(len, 2); /* Lcod */
386 cio_seek(lenp + len);
396 J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
397 len = cio_read(2); /* Lcod */
398 tcp->csty = cio_read(1); /* Scod */
399 tcp->prg = cio_read(1); /* SGcod (A) */
400 tcp->numlayers = cio_read(2); /* SGcod (B) */
401 tcp->mct = cio_read(1); /* SGcod (C) */
404 for (i = 0; i < j2k_img->numcomps; i++) {
405 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
411 void j2k_write_coc(int compno)
416 cio_write(J2K_MS_COC, 2); /* COC */
419 tcp = &j2k_cp->tcps[j2k_curtileno];
420 cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */
421 cio_write(tcp->tccps[compno].csty, 1); /* Scoc */
422 j2k_write_cox(compno);
423 len = cio_tell() - lenp;
425 cio_write(len, 2); /* Lcoc */
426 cio_seek(lenp + len);
436 J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
437 len = cio_read(2); /* Lcoc */
438 compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Ccoc */
439 tcp->tccps[compno].csty = cio_read(1); /* Scoc */
440 j2k_read_cox(compno);
443 void j2k_write_qcx(int compno)
447 int bandno, numbands;
450 tcp = &j2k_cp->tcps[j2k_curtileno];
451 tccp = &tcp->tccps[compno];
453 cio_write(tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
456 J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
458 for (bandno = 0; bandno < numbands; bandno++) {
459 expn = tccp->stepsizes[bandno].expn;
460 mant = tccp->stepsizes[bandno].mant;
462 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
463 cio_write(expn << 3, 1); /* SPqcx_i */
465 cio_write((expn << 11) + mant, 2); /* SPqcx_i */
471 void j2k_read_qcx(int compno, int len)
476 int bandno, numbands;
480 J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
481 tccp = &tcp->tccps[compno];
482 tmp = cio_read(1); /* Sqcx */
483 tccp->qntsty = tmp & 0x1f;
484 tccp->numgbits = tmp >> 5;
486 tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : (tccp->qntsty ==
487 J2K_CCP_QNTSTY_NOQNT ?
488 len - 1 : (len - 1) / 2);
489 for (bandno = 0; bandno < numbands; bandno++) {
491 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
492 expn = cio_read(1) >> 3; /* SPqcx_i */
495 tmp = cio_read(2); /* SPqcx_i */
499 tccp->stepsizes[bandno].expn = expn;
500 tccp->stepsizes[bandno].mant = mant;
505 /* Add Antonin : if scalar_derived -> compute other stepsizes */
509 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
511 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
513 tccp->stepsizes[bandno].expn =
514 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) >
515 0) ? (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
517 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
532 cio_write(J2K_MS_QCD, 2); /* QCD */
536 len = cio_tell() - lenp;
538 cio_write(len, 2); /* Lqcd */
539 cio_seek(lenp + len);
546 len = cio_read(2); /* Lqcd */
548 for (i = 0; i < j2k_img->numcomps; i++) {
550 j2k_read_qcx(i, len - 2);
554 void j2k_write_qcc(int compno)
558 cio_write(J2K_MS_QCC, 2); /* QCC */
561 cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */
562 j2k_write_qcx(compno);
563 len = cio_tell() - lenp;
565 cio_write(len, 2); /* Lqcc */
566 cio_seek(lenp + len);
573 len = cio_read(2); /* Lqcc */
574 compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Cqcc */
575 j2k_read_qcx(compno, len - 2 - (j2k_img->numcomps <= 256 ? 1 : 2));
580 int len, numpchgs, i;
584 tcp = &j2k_cp->tcps[j2k_curtileno];
585 tccp = &tcp->tccps[0];
586 numpchgs = tcp->numpocs;
587 cio_write(J2K_MS_POC, 2); /* POC */
588 len = 2 + (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2)) * numpchgs;
589 cio_write(len, 2); /* Lpoc */
590 for (i = 0; i < numpchgs; i++) {
594 cio_write(poc->resno0, 1); /* RSpoc_i */
595 cio_write(poc->compno0, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
596 cio_write(poc->layno1, 2); /* LYEpoc_i */
597 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
598 cio_write(poc->resno1, 1); /* REpoc_i */
599 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
600 cio_write(poc->compno1, (j2k_img->numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
601 poc->compno1 = int_min(poc->compno1, j2k_img->numcomps);
602 cio_write(poc->prg, 1); /* Ppoc_i */
608 int len, numpchgs, i, old_poc;
614 J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
616 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
618 tccp = &tcp->tccps[0];
619 len = cio_read(2); /* Lpoc */
620 numpchgs = (len - 2) / (5 + 2 * (j2k_img->numcomps <= 256 ? 1 : 2));
622 for (i = old_poc; i < numpchgs + old_poc; i++) {
625 poc->resno0 = cio_read(1); /* RSpoc_i */
626 poc->compno0 = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* CSpoc_i */
627 poc->layno1 = int_min(cio_read(2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
628 poc->resno1 = int_min(cio_read(1), (unsigned int) tccp->numresolutions); /* REpoc_i */
629 poc->compno1 = int_min(cio_read(j2k_img->numcomps <= 256 ? 1 : 2), (unsigned int) j2k_img->numcomps); /* CEpoc_i */
630 poc->prg = cio_read(1); /* Ppoc_i */
633 tcp->numpocs = numpchgs + old_poc - 1;
638 int len, i, Xcrg_i, Ycrg_i;
640 len = cio_read(2); /* Lcrg */
641 for (i = 0; i < j2k_img->numcomps; i++) {
642 Xcrg_i = cio_read(2); /* Xcrg_i */
643 Ycrg_i = cio_read(2); /* Ycrg_i */
649 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
650 long int Ttlm_i, Ptlm_i;
652 len = cio_read(2); /* Ltlm */
653 Ztlm = cio_read(1); /* Ztlm */
654 Stlm = cio_read(1); /* Stlm */
655 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
656 SP = (Stlm >> 6) & 0x01;
657 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
658 for (i = 0; i < tile_tlm; i++) {
659 Ttlm_i = cio_read(ST); /* Ttlm_i */
660 Ptlm_i = cio_read(SP ? 4 : 2); /* Ptlm_i */
666 int len, i, Zplm, Nplm, add, packet_len = 0;
668 len = cio_read(2); /* Lplm */
669 Zplm = cio_read(1); /* Zplm */
672 Nplm = cio_read(4); /* Nplm */
674 for (i = Nplm; i > 0; i--) {
677 packet_len = (packet_len << 7) + add; /* Iplm_ij */
678 if ((add & 0x80) == 0) {
690 int len, i, Zplt, packet_len = 0, add;
692 len = cio_read(2); /* Lplt */
693 Zplt = cio_read(1); /* Zplt */
694 for (i = len - 3; i > 0; i--) {
696 packet_len = (packet_len << 7) + add; /* Iplt_i */
697 if ((add & 0x80) == 0) {
706 int len, Z_ppm, i, j;
712 Z_ppm = cio_read(1); /* Z_ppm */
715 if (j2k_cp->ppm_previous == 0) {
716 N_ppm = cio_read(4); /* N_ppm */
719 N_ppm = j2k_cp->ppm_previous;
722 j = j2k_cp->ppm_store;
723 if (Z_ppm == 0) { /* First PPM marker */
725 (unsigned char *) calloc(N_ppm, sizeof(unsigned char));
726 j2k_cp->ppm_data_first = j2k_cp->ppm_data;
728 j2k_cp->ppm_len = N_ppm; /*Add antonin : ppmbug1*/
730 } else { /* NON-first PPM marker */
732 (unsigned char *) realloc(j2k_cp->ppm_data,
735 sizeof(unsigned char));
736 j2k_cp->ppm_data_first = j2k_cp->ppm_data;
738 j2k_cp->ppm_len = N_ppm + j2k_cp->ppm_store; /*Add antonin : ppmbug1*/
742 for (i = N_ppm; i > 0; i--) { /* Read packet header */
743 j2k_cp->ppm_data[j] = cio_read(1);
747 break; /* Case of non-finished packet header in present marker but finished in next one */
750 j2k_cp->ppm_previous = i - 1;
751 j2k_cp->ppm_store = j;
757 int len, Z_ppt, i, j = 0;
762 tcp = &j2k_cp->tcps[j2k_curtileno];
764 if (Z_ppt == 0) { /* First PPT marker */
766 (unsigned char *) calloc(len - 3, sizeof(unsigned char));
767 tcp->ppt_data_first = tcp->ppt_data;
770 tcp->ppt_len = len - 3; /*Add antonin : ppmbug1*/
771 } else { /* NON-first PPT marker */
773 (unsigned char *) realloc(tcp->ppt_data,
775 tcp->ppt_store) * sizeof(unsigned char));
776 tcp->ppt_data_first = tcp->ppt_data;
778 tcp->ppt_len = len - 3 + tcp->ppt_store; /*Add antonin : ppmbug1*/
783 for (i = len - 3; i > 0; i--) {
784 tcp->ppt_data[j] = cio_read(1);
794 j2k_sot_start = cio_tell();
795 cio_write(J2K_MS_SOT, 2); /* SOT */
797 cio_skip(2); /* Lsot (further) */
798 cio_write(j2k_curtileno, 2); /* Isot */
799 cio_skip(4); /* Psot (further in j2k_write_sod) */
800 cio_write(0, 1); /* TPsot */
801 cio_write(1, 1); /* TNsot */
802 len = cio_tell() - lenp;
804 cio_write(len, 2); /* Lsot */
805 cio_seek(lenp + len);
810 int len, tileno, totlen, partno, numparts, i;
816 tileno = cio_read(2);
818 if (j2k_cp->tileno_size == 0) {
819 j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
820 j2k_cp->tileno_size++;
823 while (i < j2k_cp->tileno_size && status == 0) {
824 status = j2k_cp->tileno[i] == tileno ? 1 : 0;
828 j2k_cp->tileno[j2k_cp->tileno_size] = tileno;
829 j2k_cp->tileno_size++;
833 totlen = cio_read(4);
835 totlen = cio_numbytesleft() + 8;
837 partno = cio_read(1);
838 numparts = cio_read(1);
840 j2k_curtileno = tileno;
841 j2k_eot = cio_getbp() - 12 + totlen;
842 j2k_state = J2K_STATE_TPH;
843 tcp = &j2k_cp->tcps[j2k_curtileno];
845 if (tcp->first == 1) {
847 *tcp = j2k_default_tcp;
849 /* Initialization PPT */
851 tcp->ppt_data = NULL;
852 tcp->ppt_data_first = NULL;
855 for (i = 0; i < j2k_img->numcomps; i++) {
856 tcp->tccps[i] = j2k_default_tcp.tccps[i];
858 j2k_cp->tcps[j2k_curtileno].first = 0;
867 static int j2k_sod_start;
869 cio_write(J2K_MS_SOD, 2);
870 if (j2k_curtileno == 0) {
871 j2k_sod_start = cio_tell() + pos_correction;
875 if (info_IM.index_on) {
876 info_IM.tile[j2k_curtileno].end_header =
877 cio_tell() + pos_correction - 1;
881 tcp = &j2k_cp->tcps[j2k_curtileno];
882 for (layno = 0; layno < tcp->numlayers; layno++) {
883 tcp->rates[layno] -= tcp->rates[layno] ? (j2k_sod_start / (j2k_cp->th * j2k_cp->tw)) : 0; /*Mod antonin losslessbug*/
887 if (j2k_cp->decod_format != PGX_DFMT)
888 l = tcd_encode_tile_pxm(j2k_curtileno, cio_getbp(),
889 cio_numbytesleft() - 2, &info_IM);
891 l = tcd_encode_tile_pgx(j2k_curtileno, cio_getbp(),
892 cio_numbytesleft() - 2, &info_IM);
894 /* Writing Psot in SOT marker */
895 totlen = cio_tell() + l - j2k_sot_start;
896 cio_seek(j2k_sot_start + 6);
897 cio_write(totlen, 4);
898 cio_seek(j2k_sot_start + totlen);
903 int len, truncate = 0, i;
906 len = int_min(j2k_eot - cio_getbp(), cio_numbytesleft() + 1);
908 if (len == cio_numbytesleft() + 1)
909 truncate = 1; /* Case of a truncate codestream */
912 (unsigned char *) malloc((j2k_tile_len[j2k_curtileno] + len) *
913 sizeof(unsigned char));
914 for (i = 0; i < j2k_tile_len[j2k_curtileno]; i++)
915 data[i] = j2k_tile_data[j2k_curtileno][i];
916 for (i = 0; i < len; i++)
917 data[i + j2k_tile_len[j2k_curtileno]] = cio_read(1);
919 j2k_tile_len[j2k_curtileno] += len;
920 free(j2k_tile_data[j2k_curtileno]);
921 j2k_tile_data[j2k_curtileno] = data;
925 j2k_state = J2K_STATE_TPHSOT;
927 j2k_state = J2K_STATE_NEOC; /* RAJOUTE !! */
930 void j2k_write_rgn(int compno, int tileno)
932 j2k_tcp_t *tcp = &j2k_cp->tcps[tileno];
934 cio_write(J2K_MS_RGN, 2); /* RGN */
935 cio_write(j2k_img->numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
936 cio_write(compno, j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */
937 cio_write(0, 1); /* Srgn */
938 cio_write(tcp->tccps[compno].roishift, 1); /* SPrgn */
943 int len, compno, roisty;
948 J2K_STATE_TPH ? &j2k_cp->tcps[j2k_curtileno] : &j2k_default_tcp;
949 len = cio_read(2); /* Lrgn */
950 compno = cio_read(j2k_img->numcomps <= 256 ? 1 : 2); /* Crgn */
951 roisty = cio_read(1); /* Srgn */
952 tcp->tccps[compno].roishift = cio_read(1); /* SPrgn */
957 /* fprintf(stderr, "%.8x: EOC\n", cio_tell() + pos_correction); */
958 cio_write(J2K_MS_EOC, 2);
965 #ifndef NO_PACKETS_DECODING
966 tcd_init(j2k_img, j2k_cp);
969 for (i = 0; i < j2k_cp->tileno_size; i++) {
970 tileno = j2k_cp->tileno[i];
971 #ifndef NO_PACKETS_DECODING
972 tcd_decode_tile(j2k_tile_data[tileno], j2k_tile_len[tileno], tileno);
974 free(j2k_tile_data[tileno]);
977 j2k_state = J2K_STATE_MT;
978 longjmp(j2k_error, 1);
983 fprintf(stderr, "warning: unknown marker\n");
987 j2k_encode(j2k_image_t * img, j2k_cp_t * cp, char *output,
988 int len, char *index)
990 int tileno, compno, layno, resno, precno, pack_nb, x, y;
991 unsigned char *dest = NULL;
995 if (setjmp(j2k_error)) {
999 if (cp->intermed_file == 1) {
1000 f = fopen(output, "wb");
1002 fprintf(stderr, "failed to open %s for writing\n", output);
1005 dest = (unsigned char *) malloc(len);
1006 cio_init(dest, len);
1011 /* j2k_dump_cp(j2k_img, j2k_cp); */
1014 info_IM.index_on = j2k_cp->index_on;
1015 if (info_IM.index_on) {
1017 (info_tile *) malloc(j2k_cp->tw * j2k_cp->th * sizeof(info_tile));
1018 info_IM.Im_w = j2k_img->x1 - j2k_img->x0;
1019 info_IM.Im_h = j2k_img->y1 - j2k_img->y0;
1020 info_IM.Prog = (&j2k_cp->tcps[0])->prg;
1021 info_IM.tw = j2k_cp->tw;
1022 info_IM.th = j2k_cp->th;
1023 info_IM.Tile_x = j2k_cp->tdx; /* new version parser */
1024 info_IM.Tile_y = j2k_cp->tdy; /* new version parser */
1025 info_IM.Tile_Ox = j2k_cp->tx0; /* new version parser */
1026 info_IM.Tile_Oy = j2k_cp->ty0; /* new version parser */
1027 info_IM.Comp = j2k_img->numcomps;
1028 info_IM.Layer = (&j2k_cp->tcps[0])->numlayers;
1029 info_IM.Decomposition = (&j2k_cp->tcps[0])->tccps->numresolutions - 1;
1030 info_IM.D_max = 0; /* ADD Marcela */
1038 for (compno = 0; compno < j2k_img->numcomps; compno++) {
1039 j2k_tcp_t *tcp = &j2k_cp->tcps[0];
1040 if (tcp->tccps[compno].roishift)
1041 j2k_write_rgn(compno, 0);
1043 if (j2k_cp->comment != NULL)
1046 if (cp->intermed_file == 1) {
1047 /* Writing the main header */
1048 pos_correction = cio_tell();
1049 fwrite(dest, 1, cio_tell(), f);
1053 if (info_IM.index_on) {
1054 info_IM.Main_head_end = cio_tell() - 1;
1059 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1060 fprintf(stdout, "Tile number %d / %d ", tileno + 1, cp->tw * cp->th);
1062 if (cp->intermed_file == 1) {
1063 /* new dest for each tile */
1065 dest = (unsigned char *) malloc(len);
1066 cio_init(dest, len);
1068 j2k_curtileno = tileno;
1069 /* initialisation before tile encoding */
1072 tcd_malloc_encode(j2k_img, j2k_cp, j2k_curtileno);
1074 tcd_init_encode(j2k_img, j2k_cp, j2k_curtileno);
1078 if (info_IM.index_on) {
1079 info_IM.tile[j2k_curtileno].num_tile = j2k_curtileno;
1080 info_IM.tile[j2k_curtileno].start_pos = cio_tell() + pos_correction;
1085 for (compno = 1; compno < img->numcomps; compno++) {
1086 j2k_write_coc(compno);
1087 j2k_write_qcc(compno);
1090 if (cp->tcps[tileno].numpocs)
1095 if (info_IM.index_on) {
1096 info_IM.tile[j2k_curtileno].end_pos =
1097 cio_tell() + pos_correction - 1;
1102 if (tile->PPT) BAD PPT !!!
1107 PPT_file=fopen("PPT","rb");
1108 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
1109 for (i=0;i<tile->len_ppt;i++)
1112 fread(&elmt, 1, 1, PPT_file);
1113 fwrite(&elmt,1,1,f);
1119 if (cp->intermed_file == 1) {
1120 fwrite(dest, 1, cio_tell(), f);
1121 pos_correction = cio_tell() + pos_correction;
1125 if (cp->intermed_file == 1) {
1127 dest = (unsigned char *) malloc(len);
1128 cio_init(dest, len);
1133 if (cp->intermed_file == 1) {
1134 fwrite(dest, 1, 2, f);
1136 /* closing file *.j2k */
1140 /* Creation of the index file */
1142 if (info_IM.index_on) {
1144 double DistoTotal = 0;
1146 info_IM.codestream_size = cio_tell() + pos_correction; /* Correction 14/4/03 suite rmq de Patrick */
1148 INDEX = fopen(index, "w");
1154 fprintf(stderr, "failed to open %s for writing\n", index);
1160 fprintf(INDEX, "%d %d\n", info_IM.Im_w, info_IM.Im_h);
1162 fprintf(INDEX, "%d\n", info_IM.Prog);
1164 fprintf(INDEX, "%d %d\n", info_IM.Tile_x, info_IM.Tile_y);
1166 fprintf(INDEX, "%d %d\n", info_IM.tw, info_IM.th);
1168 fprintf(INDEX, "%d\n", info_IM.Comp);
1170 fprintf(INDEX, "%d\n", info_IM.Layer);
1172 fprintf(INDEX, "%d\n", info_IM.Decomposition);
1174 for (resno = info_IM.Decomposition; resno >= 0; resno--) {
1176 fprintf(INDEX, "[%d,%d] ", (1 << info_IM.tile[0].pdx[resno]), (1 << info_IM.tile[0].pdx[resno])); /*based on tile 0*/
1180 fprintf(INDEX, "\n");
1182 fprintf(INDEX, "%d\n", info_IM.Main_head_end);
1184 fprintf(INDEX, "%d\n", info_IM.codestream_size);
1186 for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) {
1188 fprintf(INDEX, "%4d %9d %9d %9d %9e %9d %9e\n",
1189 info_IM.tile[tileno].num_tile,
1190 info_IM.tile[tileno].start_pos,
1191 info_IM.tile[tileno].end_header,
1192 info_IM.tile[tileno].end_pos,
1193 info_IM.tile[tileno].distotile, info_IM.tile[tileno].nbpix,
1194 info_IM.tile[tileno].distotile / info_IM.tile[tileno].nbpix);
1198 for (tileno = 0; tileno < info_IM.tw * info_IM.th; tileno++) {
1200 int start_pos, end_pos;
1208 "pkno tileno layerno resno compno precno start_pos end_pos deltaSE \n"); */
1210 if (info_IM.Prog == 0) { /* LRCP */
1211 /*fprintf(INDEX, "pack_nb tileno layno resno compno precno start_pos end_pos disto");*/
1212 for (layno = 0; layno < info_IM.Layer; layno++) {
1213 for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
1214 for (compno = 0; compno < info_IM.Comp; compno++) {
1217 info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];
1219 start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
1220 end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
1221 disto = info_IM.tile[tileno].packet[pack_nb].disto;
1222 fprintf(INDEX, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",
1223 pack_nb, tileno, layno, resno, compno, precno,
1224 start_pos, end_pos, disto);
1225 DistoTotal += disto;
1231 } else if (info_IM.Prog == 1) { /* RLCP */
1232 /*fprintf(INDEX, "pack_nb tileno resno layno compno precno start_pos end_pos disto");*/
1233 for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
1234 for (layno = 0; layno < info_IM.Layer; layno++) {
1235 for (compno = 0; compno < info_IM.Comp; compno++) {
1236 for (precno = 0; precno < info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno]; precno++) {
1237 start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
1238 end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
1239 disto = info_IM.tile[tileno].packet[pack_nb].disto;
1240 fprintf(INDEX, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
1241 pack_nb, tileno, resno, layno, compno, precno,
1242 start_pos, end_pos, disto);
1243 DistoTotal += disto;
1249 } else if (info_IM.Prog == 2) { /* RPCL */
1250 /*fprintf(INDEX, "\npack_nb tileno resno precno compno layno start_pos end_pos disto\n"); */
1251 for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
1252 /* I suppose components have same XRsiz, YRsiz */
1253 int x0 = info_IM.Tile_Ox + tileno - (int)floor( tileno/info_IM.tw ) * info_IM.tw * info_IM.Tile_x;
1254 int y0 = info_IM.Tile_Ox + (int)floor( tileno/info_IM.tw ) * info_IM.Tile_y;
1255 int x1 = x0 + info_IM.Tile_x;
1256 int y1 = y0 + info_IM.Tile_y;
1257 for (y=y0; y<y1; y++) {
1258 for (x=x0; x<x1; x++) {
1259 for (compno = 0; compno < info_IM.Comp; compno++) {
1260 int prec_max = info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];
1261 for (precno = 0; precno < prec_max; precno++) {
1262 int pcnx = info_IM.tile[tileno].pw[resno];
1263 int pcx = (int) pow( 2, info_IM.tile[tileno].pdx[resno] + info_IM.Decomposition - resno );
1264 int pcy = (int) pow( 2, info_IM.tile[tileno].pdy[resno] + info_IM.Decomposition - resno );
1265 int precno_x = precno - (int) floor( precno/pcnx ) * pcnx;
1266 int precno_y = (int) floor( precno/pcnx );
1267 if (precno_y*pcy == y ) {
1268 if (precno_x*pcx == x ) {
1269 for (layno = 0; layno < info_IM.Layer; layno++) {
1270 start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
1271 end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
1272 disto = info_IM.tile[tileno].packet[pack_nb].disto;
1273 fprintf(INDEX, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
1274 pack_nb, tileno, resno, precno, compno, layno,
1275 start_pos, end_pos, disto);
1276 DistoTotal += disto;
1286 } else if (info_IM.Prog == 3) { /* PCRL */
1287 /* I suppose components have same XRsiz, YRsiz*/
1288 int x0 = info_IM.Tile_Ox + tileno - (int)floor( tileno/info_IM.tw ) * info_IM.tw * info_IM.Tile_x;
1289 int y0 = info_IM.Tile_Ox + (int)floor( tileno/info_IM.tw ) * info_IM.Tile_y;
1290 int x1 = x0 + info_IM.Tile_x;
1291 int y1 = y0 + info_IM.Tile_y;
1293 /*fprintf(INDEX, "\npack_nb tileno precno compno resno layno start_pos end_pos disto\n"); */
1294 for (y=y0; y<y1; y++) {
1295 for (x=x0; x<x1; x++) {
1296 for (compno = 0; compno < info_IM.Comp; compno++) {
1297 for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
1298 int prec_max = info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];
1299 for (precno = 0; precno < prec_max; precno++) {
1300 int pcnx = info_IM.tile[tileno].pw[resno];
1301 int pcx = (int) pow( 2, info_IM.tile[tileno].pdx[resno] + info_IM.Decomposition - resno );
1302 int pcy = (int) pow( 2, info_IM.tile[tileno].pdy[resno] + info_IM.Decomposition - resno );
1303 int precno_x = precno - (int) floor( precno/pcnx ) * pcnx;
1304 int precno_y = (int) floor( precno/pcnx );
1305 if (precno_y*pcy == y ) {
1306 if (precno_x*pcx == x ) {
1307 for (layno = 0; layno < info_IM.Layer; layno++) {
1308 start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
1309 end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
1310 disto = info_IM.tile[tileno].packet[pack_nb].disto;
1311 fprintf(INDEX, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
1312 pack_nb, tileno, precno, compno, resno, layno,
1313 start_pos, end_pos, disto);
1314 DistoTotal += disto;
1325 /*fprintf(INDEX, "\npack_nb tileno compno precno resno layno start_pos end_pos disto\n"); */
1326 for (compno = 0; compno < info_IM.Comp; compno++) {
1327 /* I suppose components have same XRsiz, YRsiz */
1328 int x0 = info_IM.Tile_Ox + tileno - (int)floor( tileno/info_IM.tw ) * info_IM.tw * info_IM.Tile_x;
1329 int y0 = info_IM.Tile_Ox + (int)floor( tileno/info_IM.tw ) * info_IM.Tile_y;
1330 int x1 = x0 + info_IM.Tile_x;
1331 int y1 = y0 + info_IM.Tile_y;
1332 for (y=y0; y<y1; y++) {
1333 for (x=x0; x<x1; x++) {
1334 for (resno = 0; resno < info_IM.Decomposition + 1; resno++) {
1335 int prec_max = info_IM.tile[tileno].pw[resno] * info_IM.tile[tileno].ph[resno];
1336 for (precno = 0; precno < prec_max; precno++) {
1337 int pcnx = info_IM.tile[tileno].pw[resno];
1338 int pcx = (int) pow( 2, info_IM.tile[tileno].pdx[resno] + info_IM.Decomposition - resno );
1339 int pcy = (int) pow( 2, info_IM.tile[tileno].pdy[resno] + info_IM.Decomposition - resno );
1340 int precno_x = precno - (int) floor( precno/pcnx ) * pcnx;
1341 int precno_y = (int) floor( precno/pcnx );
1342 if (precno_y*pcy == y ) {
1343 if (precno_x*pcx == x ) {
1344 for (layno = 0; layno < info_IM.Layer; layno++) {
1345 start_pos = info_IM.tile[tileno].packet[pack_nb].start_pos;
1346 end_pos = info_IM.tile[tileno].packet[pack_nb].end_pos;
1347 disto = info_IM.tile[tileno].packet[pack_nb].disto;
1348 fprintf(INDEX, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
1349 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
1350 DistoTotal += disto;
1363 fprintf(INDEX, "%8e\n", info_IM.D_max); /*SE max*/
1365 fprintf(INDEX, "%.8e\n", DistoTotal); /* SE totale*/
1380 } j2k_dec_mstabent_t;
1382 j2k_dec_mstabent_t j2k_dec_mstab[] = {
1383 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
1384 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
1385 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
1386 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
1387 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
1388 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
1389 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
1390 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
1391 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
1392 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
1393 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
1394 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
1395 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
1396 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
1397 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
1398 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
1400 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
1401 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
1402 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
1405 j2k_dec_mstabent_t *j2k_dec_mstab_lookup(int id)
1407 j2k_dec_mstabent_t *e;
1408 for (e = j2k_dec_mstab; e->id != 0; e++) {
1419 j2k_decode(unsigned char *src, int len, j2k_image_t * img, j2k_cp_t * cp)
1422 if (setjmp(j2k_error)) {
1423 if (j2k_state != J2K_STATE_MT) {
1424 fprintf(stderr, "WARNING: incomplete bitstream\n");
1428 return cio_numbytes(); /* Correct way of ending j2k_decode */
1435 j2k_state = J2K_STATE_MHSOC;
1441 j2k_dec_mstabent_t *e;
1442 int id = cio_read(2);
1443 if (id >> 8 != 0xff) {
1444 fprintf(stderr, "%.8x: expected a marker instead of %x\n",
1445 cio_tell() - 2, id);
1448 e = j2k_dec_mstab_lookup(id);
1449 if (!(j2k_state & e->states)) {
1450 fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id);
1458 if (j2k_state == J2K_STATE_NEOC)
1459 break; /* RAJOUTE */
1461 if (j2k_state == J2K_STATE_NEOC)
1462 j2k_read_eoc(); /* RAJOUTE */
1468 * Read a JPT-stream and decode file
1472 j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
1475 jpt_msg_header_struct_t header;
1478 if (setjmp(j2k_error)) {
1479 if (j2k_state != J2K_STATE_MT) {
1480 fprintf(stderr, "WARNING: incomplete bitstream\n");
1483 return cio_numbytes();
1490 j2k_state = J2K_STATE_MHSOC;
1493 /* Initialize the header */
1494 jpt_init_Msg_Header(&header);
1495 /* Read the first header of the message */
1496 jpt_read_Msg_Header(&header);
1498 position = cio_tell();
1499 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
1501 "[JPT-stream] : Expecting Main header first [class_Id %d] !\n",
1507 j2k_dec_mstabent_t *e;
1510 if (!cio_numbytesleft()) {
1514 /* data-bin read -> need to read a new header */
1515 if ((unsigned int) (cio_tell() - position) == header.Msg_length) {
1516 jpt_read_Msg_Header(&header);
1517 position = cio_tell();
1518 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
1519 fprintf(stderr, "[JPT-stream] : Expecting Tile info !\n");
1525 if (id >> 8 != 0xff) {
1526 fprintf(stderr, "%.8x: expected a marker instead of %x\n",
1527 cio_tell() - 2, id);
1530 e = j2k_dec_mstab_lookup(id);
1531 if (!(j2k_state & e->states)) {
1532 fprintf(stderr, "%.8x: unexpected marker %x\n", cio_tell() - 2, id);
1538 if (j2k_state == J2K_STATE_NEOC)
1539 break; /* RAJOUTE */
1541 if (j2k_state == J2K_STATE_NEOC)
1542 j2k_read_eoc(); /* RAJOUTE */
1549 void j2k_dec_release()
1557 /*tcd_dec_release();*/
1561 if (j2k_tile_len!=NULL) free(j2k_tile_len);
1563 if (j2k_tile_data!=NULL) free(j2k_tile_data);
1565 if (j2k_default_tcp.ppt_data_first!=NULL) free(j2k_default_tcp.ppt_data_first);
1567 if (j2k_default_tcp.tccps!=NULL) free(j2k_default_tcp.tccps);
1569 for (i=0;i<j2k_cp->tw*j2k_cp->th;i++) {
1571 if (j2k_cp->tcps[i].ppt_data_first!=NULL) free(j2k_cp->tcps[i].ppt_data_first);
1573 if (j2k_cp->tcps[i].tccps!=NULL) free(j2k_cp->tcps[i].tccps);
1577 if (j2k_cp->ppm_data_first!=NULL) free(j2k_cp->ppm_data_first);
1579 if (j2k_cp->tcps!=NULL) free(j2k_cp->tcps);
1581 if (j2k_img->comps!=NULL) free(j2k_img->comps);
1583 if (j2k_cp->tileno!=NULL) free(j2k_cp->tileno);
1588 #include <windows.h>
1591 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
1593 switch (ul_reason_for_call) {
1594 case DLL_PROCESS_ATTACH:
1595 case DLL_THREAD_ATTACH:
1596 case DLL_THREAD_DETACH:
1597 case DLL_PROCESS_DETACH: