From de75303c38253bf5f5aeb31d20769c96167fc3a7 Mon Sep 17 00:00:00 2001 From: Antonin Descampe Date: Mon, 18 Jul 2005 15:21:56 +0000 Subject: [PATCH] Several bugs fixed when dealing with pgx-files with more than 8 bpp and the fixed_quality feature. --- codec/image_to_j2k.c | 4 ++-- codec/j2k_to_image.c | 36 ++++++++++++++++-------------------- libopenjpeg/tcd.c | 10 +++------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/codec/image_to_j2k.c b/codec/image_to_j2k.c index 132f8f88..e64616ab 100644 --- a/codec/image_to_j2k.c +++ b/codec/image_to_j2k.c @@ -659,13 +659,13 @@ int main(int argc, char **argv) if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality) && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) { fprintf(stderr, - "Error: options -r -q and -f can not be used together !!\n"); + "Error: options -r -q and -f cannot be used together !!\n"); return 1; } // mod fixed_quality /* if no rate entered, lossless by default */ if (tcp_init->numlayers == 0) { - tcp_init->rates[tcp_init->numlayers] = 0; //MOD antonin : losslessbug + tcp_init->rates[0] = 0; //MOD antonin : losslessbug tcp_init->numlayers++; cp.disto_alloc = 1; } diff --git a/codec/j2k_to_image.c b/codec/j2k_to_image.c index 00fca99c..3a6dcce1 100644 --- a/codec/j2k_to_image.c +++ b/codec/j2k_to_image.c @@ -366,25 +366,24 @@ int main(int argc, char **argv) img.comps[0].factor); - fprintf(fdest, "P6\n# %d %d %d %d %d\n%d %d\n%d\n", - cp.tcps[cp.tileno[0]].tccps[0].numresolutions, w, h, - img.comps[0].x0, img.comps[0].y0, wrr, hrr, max); + fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max); adjust = img.comps[0].prec > 8 ? img.comps[0].prec - 8 : 0; for (i = 0; i < wrr * hrr; i++) { - char r, g, b; + int r, g, b; + unsigned char rc,gc,bc; r = img.comps[0].data[i / wrr * wr + i % wrr]; r += (img.comps[0].sgnd ? 1 << (img.comps[0].prec - 1) : 0); - r = r >> adjust; + rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2)); g = img.comps[1].data[i / wrr * wr + i % wrr]; g += (img.comps[1].sgnd ? 1 << (img.comps[1].prec - 1) : 0); - g = g >> adjust; + gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2)); b = img.comps[2].data[i / wrr * wr + i % wrr]; b += (img.comps[2].sgnd ? 1 << (img.comps[2].prec - 1) : 0); - b = b >> adjust; + bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2)); - fprintf(fdest, "%c%c%c", r, g, b); + fprintf(fdest, "%c%c%c", rc, gc, bc); } free(img.comps[0].data); free(img.comps[1].data); @@ -438,19 +437,17 @@ int main(int argc, char **argv) img.comps[compno].dy), img.comps[compno].factor); - fprintf(fdest, "P5\n# %d %d %d %d %d\n%d %d\n%d\n", - cp.tcps[cp.tileno[0]].tccps[compno]. - numresolutions, w, h, img.comps[compno].x0, - img.comps[compno].y0, wrr, hrr, max); + fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max); adjust = img.comps[compno].prec > 8 ? img.comps[compno].prec - 8 : 0; for (i = 0; i < wrr * hrr; i++) { - char l; + int l; + unsigned char lc; l = img.comps[compno].data[i / wrr * wr + i % wrr]; l += (img.comps[compno]. sgnd ? 1 << (img.comps[compno].prec - 1) : 0); - l = l >> adjust; - fprintf(fdest, "%c", l); + lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2)); + fprintf(fdest, "%c", lc); } fclose(fdest); free(img.comps[compno].data); @@ -476,12 +473,11 @@ int main(int argc, char **argv) tmp--; } *tmp='\0'; - //if (img.numcomps > 1) - sprintf(name, "%s-%d.pgx", outfile, compno); - //else - - //sprintf(name, "%s.pgx", outfile); + if (img.numcomps > 1) + sprintf(name, "%s-%d.pgx", outfile, compno); + else + sprintf(name, "%s.pgx", outfile); fdest = fopen(name, "wb"); if (!fdest) { diff --git a/libopenjpeg/tcd.c b/libopenjpeg/tcd.c index 017b638e..f39d3403 100644 --- a/libopenjpeg/tcd.c +++ b/libopenjpeg/tcd.c @@ -1076,11 +1076,7 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM) } /* precno */ } /* bandno */ } /* resno */ - - maxSE += - (double) (((1 << tcd_img->comps[compno].prec) - - 1) * ((1 << tcd_img->comps[compno].prec) - - 1)) * (tilec->nbpix); + maxSE += (((double)(1 << tcd_img->comps[compno].prec) - 1.0) * ((double)(1 << tcd_img->comps[compno].prec) -1.0)) * ((double)(tilec->nbpix)); } /* compno */ /* add antonin index */ @@ -1103,8 +1099,8 @@ void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM) double distotarget; //add fixed_quality distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10)); // add fixed_quality - - if (tcd_tcp->rates[layno]) { + + if ((tcd_tcp->rates[layno]) || (tcd_cp->disto_alloc==0)) { for (i = 0; i < 32; i++) { volatile double thresh = (lo + hi) / 2; int l = 0;