In convert.c, corrected imagetobmp() conversion for grayscale. In tcd.c, corrected...
[openjpeg.git] / libopenjpeg / tcd.c
index 38f6b6ba87b6c5512b57f42dcc3047a97b92db96..01ee3950f04d397ee405f9cf0d2eb4717f23aea4 100644 (file)
@@ -418,12 +418,19 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                /* Modification of the RATE >> */
                for (j = 0; j < tcp->numlayers; j++) {
                        tcp->rates[j] = tcp->rates[j] ? 
-                                               ((float) (tile->numcomps 
-                                                               * (tile->x1 - tile->x0) 
-                                                               * (tile->y1 - tile->y0) 
-                                                               * image->comps[0].prec))/ 
-                                               (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy) 
-                                               : 0;
+                               cp->tp_on ? 
+                                       (((float) (tile->numcomps 
+                                       * (tile->x1 - tile->x0) 
+                                       * (tile->y1 - tile->y0)
+                                       * image->comps[0].prec))
+                                       /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
+                                       :
+                               ((float) (tile->numcomps 
+                                       * (tile->x1 - tile->x0) 
+                                       * (tile->y1 - tile->y0) 
+                                       * image->comps[0].prec))/ 
+                                       (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
+                                       : 0;
 
                        if (tcp->rates[j]) {
                                if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
@@ -584,7 +591,9 @@ void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int cur
                                                        cblk->y0 = int_max(cblkystart, prc->y0);
                                                        cblk->x1 = int_min(cblkxend, prc->x1);
                                                        cblk->y1 = int_min(cblkyend, prc->y1);
-                                                       cblk->data = (unsigned char*) opj_calloc(8192, sizeof(unsigned char));
+                                                       cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
+                                                       /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
+                                                       cblk->data += 2;
                                                        cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
                                                        cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
                                                }
@@ -1094,7 +1103,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre
                                
                                if (cp->fixed_quality) {        /* fixed_quality */
                                        if(cp->cinema){
-                                               l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
+                                               l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
                                                if (l == -999) {
                                                        lo = thresh;
                                                        continue;
@@ -1120,7 +1129,7 @@ bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestre
                                                lo = thresh;
                                        }
                                } else {
-                                       l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC);
+                                       l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
                                        /* TODO: what to do with l ??? seek / tell ??? */
                                        /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
                                        if (l == -999) {
@@ -1284,7 +1293,7 @@ int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, op
        }
 
        t2 = t2_create(tcd->cinfo, image, cp);
-       l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS);
+       l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
        t2_destroy(t2);
        
        /*---------------CLEAN-------------------*/
@@ -1384,9 +1393,9 @@ bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, op
                if (tcd->cp->reduce != 0) {
                        tcd->image->comps[compno].resno_decoded =
                                tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
-                       if (tcd->image->comps[compno].resno_decoded < 1) {
-                               opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove is higher than the number "
-                                       " of resolutions in the original codestream\nModify the cp_reduce parameter.\n");
+                       if (tcd->image->comps[compno].resno_decoded < 0) {                              
+                               opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
+                                       " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
                                return false;
                        }
                }
@@ -1503,3 +1512,5 @@ void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
        opj_free(tile->comps);
 }
 
+
+