2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Herv� Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include "opj_includes.h"
34 /* #define RESTART 0x04 */
36 static void t2_putcommacode(opj_bio_t *bio, int n) {
43 static int t2_getcommacode(opj_bio_t *bio) {
45 for (n = 0; bio_read(bio, 1); n++) {
51 static void t2_putnumpasses(opj_bio_t *bio, int n) {
57 bio_write(bio, 0xc | (n - 3), 4);
59 bio_write(bio, 0x1e0 | (n - 6), 9);
60 } else if (n <= 164) {
61 bio_write(bio, 0xff80 | (n - 37), 16);
65 static int t2_getnumpasses(opj_bio_t *bio) {
67 if (!bio_read(bio, 1))
69 if (!bio_read(bio, 1))
71 if ((n = bio_read(bio, 2)) != 3)
73 if ((n = bio_read(bio, 5)) != 31)
75 return (37 + bio_read(bio, 7));
78 static int t2_encode_packet(opj_t2_t* t2, opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int len, opj_image_info_t * image_info, int tileno) {
80 unsigned char *sop = 0, *eph = 0;
81 unsigned char *c = dest;
83 int compno = pi->compno; /* component value */
84 int resno = pi->resno; /* resolution level value */
85 int precno = pi->precno; /* precinct value */
86 int layno = pi->layno; /* quality layer value */
88 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
89 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
91 opj_bio_t *bio = NULL; /* BIO component */
94 if (tcp->csty & J2K_CP_CSTY_SOP) {
95 sop = (unsigned char *) opj_malloc(6 * sizeof(unsigned char));
100 sop[4] = (image_info->num % 65536) / 256;
101 sop[5] = (image_info->num % 65536) % 256;
109 for (bandno = 0; bandno < res->numbands; bandno++) {
110 opj_tcd_band_t *band = &res->bands[bandno];
111 opj_tcd_precinct_t *prc = &band->precincts[precno];
112 tgt_reset(prc->incltree);
113 tgt_reset(prc->imsbtree);
114 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
115 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
117 tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
123 bio_init_enc(bio, c, len);
124 bio_write(bio, 1, 1); /* Empty header bit */
126 /* Writing Packet header */
127 for (bandno = 0; bandno < res->numbands; bandno++) {
128 opj_tcd_band_t *band = &res->bands[bandno];
129 opj_tcd_precinct_t *prc = &band->precincts[precno];
130 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
131 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
132 opj_tcd_layer_t *layer = &cblk->layers[layno];
133 if (!cblk->numpasses && layer->numpasses) {
134 tgt_setvalue(prc->incltree, cblkno, layno);
137 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
138 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
139 opj_tcd_layer_t *layer = &cblk->layers[layno];
143 /* cblk inclusion bits */
144 if (!cblk->numpasses) {
145 tgt_encode(bio, prc->incltree, cblkno, layno + 1);
147 bio_write(bio, layer->numpasses != 0, 1);
149 /* if cblk not included, go to the next cblk */
150 if (!layer->numpasses) {
153 /* if first instance of cblk --> zero bit-planes information */
154 if (!cblk->numpasses) {
155 cblk->numlenbits = 3;
156 tgt_encode(bio, prc->imsbtree, cblkno, 999);
158 /* number of coding passes included */
159 t2_putnumpasses(bio, layer->numpasses);
161 /* computation of the increase of the length indicator and insertion in the header */
162 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
163 opj_tcd_pass_t *pass = &cblk->passes[passno];
166 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
167 increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
172 t2_putcommacode(bio, increment);
174 /* computation of the new Length indicator */
175 cblk->numlenbits += increment;
177 /* insertion of the codeword segment length */
178 for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
179 opj_tcd_pass_t *pass = &cblk->passes[passno];
182 if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
183 bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
191 if (bio_flush(bio)) {
192 return -999; /* modified to eliminate longjmp !! */
195 c += bio_numbytes(bio);
200 if (tcp->csty & J2K_CP_CSTY_EPH) {
201 eph = (unsigned char *) opj_malloc(2 * sizeof(unsigned char));
210 /* Writing the packet body */
212 for (bandno = 0; bandno < res->numbands; bandno++) {
213 opj_tcd_band_t *band = &res->bands[bandno];
214 opj_tcd_precinct_t *prc = &band->precincts[precno];
215 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
216 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
217 opj_tcd_layer_t *layer = &cblk->layers[layno];
218 if (!layer->numpasses) {
221 if (c + layer->len > dest + len) {
225 memcpy(c, layer->data, layer->len);
226 cblk->numpasses += layer->numpasses;
228 /* ADD for index Cfr. Marcela --> delta disto by packet */
229 if(image_info && image_info->index_write && image_info->index_on) {
230 opj_tile_info_t *info_TL = &image_info->tile[tileno];
231 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
232 info_PK->disto += layer->disto;
233 if (image_info->D_max < info_PK->disto) {
234 image_info->D_max = info_PK->disto;
244 static void t2_init_seg(opj_tcd_seg_t * seg, int cblksty, int first) {
247 if (cblksty & J2K_CCP_CBLKSTY_TERMALL) {
250 else if (cblksty & J2K_CCP_CBLKSTY_LAZY) {
254 seg->maxpasses = (((seg - 1)->maxpasses == 1) || ((seg - 1)->maxpasses == 10)) ? 2 : 1;
257 seg->maxpasses = 109;
261 int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) {
263 unsigned char *c = src;
265 opj_cp_t *cp = t2->cp;
267 int compno = pi->compno; /* component value */
268 int resno = pi->resno; /* resolution level value */
269 int precno = pi->precno; /* precinct value */
270 int layno = pi->layno; /* quality layer value */
272 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
273 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
275 unsigned char *hd = NULL;
278 opj_bio_t *bio = NULL; /* BIO component */
281 for (bandno = 0; bandno < res->numbands; bandno++) {
282 opj_tcd_band_t *band = &res->bands[bandno];
283 opj_tcd_precinct_t *prc = &band->precincts[precno];
285 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
287 tgt_reset(prc->incltree);
288 tgt_reset(prc->imsbtree);
289 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
290 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
298 if (tcp->csty & J2K_CP_CSTY_SOP) {
299 if ((*c) != 0xff || (*(c + 1) != 0x91)) {
300 opg_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n");
305 /** TODO : check the Nsop value */
309 When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
310 This part deal with this caracteristic
311 step 1: Read packet header in the saved structure
312 step 2: Return to codestream for decoding
317 if (cp->ppm == 1) { /* PPM */
319 bio_init_dec(bio, hd, cp->ppm_len);
320 } else if (tcp->ppt == 1) { /* PPT */
322 bio_init_dec(bio, hd, tcp->ppt_len);
323 } else { /* Normal Case */
325 bio_init_dec(bio, hd, src+len-hd);
328 present = bio_read(bio, 1);
332 hd += bio_numbytes(bio);
337 if (tcp->csty & J2K_CP_CSTY_EPH) {
338 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
339 printf("Error : expected EPH marker\n");
345 if (cp->ppm == 1) { /* PPM case */
346 cp->ppm_len += cp->ppm_data-hd;
350 if (tcp->ppt == 1) { /* PPT case */
351 tcp->ppt_len+=tcp->ppt_data-hd;
359 for (bandno = 0; bandno < res->numbands; bandno++) {
360 opj_tcd_band_t *band = &res->bands[bandno];
361 opj_tcd_precinct_t *prc = &band->precincts[precno];
363 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
365 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
366 int included, increment, n;
367 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
368 opj_tcd_seg_t *seg = NULL;
369 /* if cblk not yet included before --> inclusion tagtree */
370 if (!cblk->numsegs) {
371 included = tgt_decode(bio, prc->incltree, cblkno, layno + 1);
374 included = bio_read(bio, 1);
376 /* if cblk not included */
378 cblk->numnewpasses = 0;
381 /* if cblk not yet included --> zero-bitplane tagtree */
382 if (!cblk->numsegs) {
384 for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) {
388 cblk->numbps = band->numbps - numimsbs;
389 cblk->numlenbits = 3;
391 /* number of coding passes */
392 cblk->numnewpasses = t2_getnumpasses(bio);
393 increment = t2_getcommacode(bio);
394 /* length indicator increment */
395 cblk->numlenbits += increment;
396 if (!cblk->numsegs) {
397 seg = &cblk->segs[0];
398 t2_init_seg(seg, tcp->tccps[compno].cblksty, 1);
400 seg = &cblk->segs[cblk->numsegs - 1];
401 if (seg->numpasses == seg->maxpasses) {
402 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
405 n = cblk->numnewpasses;
408 seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n);
409 seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses));
410 n -= seg->numnewpasses;
412 t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0);
418 if (bio_inalign(bio)) {
423 hd += bio_numbytes(bio);
427 if (tcp->csty & J2K_CP_CSTY_EPH) {
428 if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
429 opg_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n");
436 cp->ppm_len+=cp->ppm_data-hd;
438 } else if (tcp->ppt == 1) {
439 tcp->ppt_len+=tcp->ppt_data-hd;
445 for (bandno = 0; bandno < res->numbands; bandno++) {
446 opj_tcd_band_t *band = &res->bands[bandno];
447 opj_tcd_precinct_t *prc = &band->precincts[precno];
449 if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
451 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
452 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
453 opj_tcd_seg_t *seg = NULL;
454 if (!cblk->numnewpasses)
456 if (!cblk->numsegs) {
457 seg = &cblk->segs[0];
461 seg = &cblk->segs[cblk->numsegs - 1];
462 if (seg->numpasses == seg->maxpasses) {
469 if (c + seg->newlen > src + len) {
473 memcpy(cblk->data + cblk->len, c, seg->newlen);
474 if (seg->numpasses == 0) {
475 seg->data = cblk->data + cblk->len;
478 cblk->len += seg->newlen;
479 seg->len += seg->newlen;
480 seg->numpasses += seg->numnewpasses;
481 cblk->numnewpasses -= seg->numnewpasses;
482 if (cblk->numnewpasses > 0) {
486 } while (cblk->numnewpasses > 0);
493 /* ----------------------------------------------------------------------- */
495 int t2_encode_packets(opj_t2_t* t2, int tileno, opj_tcd_tile_t *tile, int maxlayers, unsigned char *dest, int len, opj_image_info_t *image_info) {
496 unsigned char *c = dest;
498 opj_pi_iterator_t *pi = NULL;
501 opj_image_t *image = t2->image;
502 opj_cp_t *cp = t2->cp;
504 /* create a packet iterator */
505 pi = pi_create(image, cp, tileno);
507 /* TODO: throw an error */
511 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
512 while (pi_next(&pi[pino])) {
513 if (pi[pino].layno < maxlayers) {
514 e = t2_encode_packet(t2, tile, &cp->tcps[tileno], &pi[pino], c, dest + len - c, image_info, tileno);
522 if(image_info && image_info->index_on) {
523 if(image_info->index_write) {
524 opj_tile_info_t *info_TL = &image_info->tile[tileno];
525 opj_packet_info_t *info_PK = &info_TL->packet[image_info->num];
526 if (!image_info->num) {
527 info_PK->start_pos = info_TL->end_header + 1;
529 info_PK->start_pos = info_TL->packet[image_info->num - 1].end_pos + 1;
531 info_PK->end_pos = info_PK->start_pos + e - 1;
541 /* don't forget to release pi */
542 pi_destroy(pi, cp, tileno);
551 int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
552 unsigned char *c = src;
553 opj_pi_iterator_t *pi;
557 opj_image_t *image = t2->image;
558 opj_cp_t *cp = t2->cp;
560 /* create a packet iterator */
561 pi = pi_create(image, cp, tileno);
563 /* TODO: throw an error */
567 for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
568 while (pi_next(&pi[pino])) {
569 if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
570 e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
575 /* progression in resolution */
576 image->comps[pi[pino].compno].resno_decoded =
578 int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded)
579 : image->comps[pi[pino].compno].resno_decoded;
582 if (e == -999) { /* ADD */
590 /* don't forget to release pi */
591 pi_destroy(pi, cp, tileno);
600 /* ----------------------------------------------------------------------- */
602 opj_t2_t* t2_create(opj_common_ptr cinfo, opj_image_t *image, opj_cp_t *cp) {
603 /* create the tcd structure */
604 opj_t2_t *t2 = (opj_t2_t*)opj_malloc(sizeof(opj_t2_t));
613 void t2_destroy(opj_t2_t *t2) {