First integration of JPWL code
[openjpeg.git] / libopenjpeg / t2.c
index 43e1fe246c13d4078a0c9aeeae88d07036c1f3f2..d82d0c744009e41af73e333e577a81cdb29e25e5 100644 (file)
@@ -125,7 +125,7 @@ static int t2_getnumpasses(opj_bio_t *bio) {
        return (37 + bio_read(bio, 7));
 }
 
-static int t2_encode_packet(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) {
+static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_image_info_t * image_info, int tileno) {
        int bandno, cblkno;
        unsigned char *sop = 0, *eph = 0;
        unsigned char *c = dest;
@@ -170,7 +170,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera
        }
        
        bio = bio_create();
-       bio_init_enc(bio, c, len);
+       bio_init_enc(bio, c, length);
        bio_write(bio, 1, 1);           /* Empty header bit */
        
        /* Writing Packet header */
@@ -268,7 +268,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_itera
                        if (!layer->numpasses) {
                                continue;
                        }
-                       if (c + layer->len > dest + len) {
+                       if (c + layer->len > dest + length) {
                                return -999;
                        }
                        
@@ -519,6 +519,25 @@ static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_t
                                if (c + seg->newlen > src + len) {
                                        return -999;
                                }
+
+/* UniPG>> */
+#ifdef USE_JPWL
+                       /* we need here a j2k handle to verify if making a check to
+                       the validity of cblocks parameters is selected from user (-W) */
+
+                               /* let's check that we are not exceeding */
+                               if ((cblk->len + seg->newlen) > 8192) {
+                                       fprintf(stderr, "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n",
+                                               seg->newlen, cblkno, precno, bandno, resno, compno);
+                                       if (!JPWL_ASSUME)
+                                               exit(-1);
+                                       seg->newlen = 8192 - cblk->len;
+                                       fprintf(stderr, "      - truncating segment to %d\n", seg->newlen);
+                                       break;
+                               };
+
+#endif /* USE_JPWL */
+/* <<UniPG */
                                
                                memcpy(cblk->data + cblk->len, c, seg->newlen);
                                if (seg->numpasses == 0) {