From 297b492820bd7e76d46868f8bfa9160e229b5905 Mon Sep 17 00:00:00 2001 From: Dusan Jovic Date: Thu, 19 Sep 2024 12:03:48 -0400 Subject: [PATCH 1/2] Use scaled integer array (ifld) in OpenJPEG version of enc_jpeg2000 routine --- src/decenc_openjpeg.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/decenc_openjpeg.c b/src/decenc_openjpeg.c index d2d1bff6..15bf88ee 100644 --- a/src/decenc_openjpeg.c +++ b/src/decenc_openjpeg.c @@ -361,7 +361,9 @@ int enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, { (void) retry; int iret = 0; + int nbytes = 0; const int numcomps = 1; + g2int *ifld = NULL; opj_codec_t *codec = NULL; opj_image_t *image = NULL; @@ -412,9 +414,14 @@ int enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, assert(cmptparm.prec <= sizeof(image->comps[0].data[0])*8 - 1); /* BR: -1 because I don't know what happens if the sign bit is set */ + ifld = malloc(width * height * sizeof(g2int)); + nbytes = (nbits + 7) / 8; + gbits(cin, ifld, 0, nbytes * 8, 0, width * height); /* Simple packing */ - for (int i = 0; i < width * height; i++) - image->comps[0].data[i] = cin[i]; + for (int i = 0; i < width * height; i++) { + image->comps[0].data[i] = ifld[i]; + } + free(ifld); /* get a J2K compressor handle */ codec = opj_create_compress(OPJ_CODEC_J2K); From 48d43fbb9ee2198fa9e079e9d374170897273725 Mon Sep 17 00:00:00 2001 From: Dusan Jovic Date: Wed, 25 Sep 2024 08:45:55 -0400 Subject: [PATCH 2/2] Update tst_jpeg.c --- tests/tst_jpeg.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/tst_jpeg.c b/tests/tst_jpeg.c index 40789ced..ca243787 100644 --- a/tests/tst_jpeg.c +++ b/tests/tst_jpeg.c @@ -80,7 +80,7 @@ main() printf("Testing jpcpack()/jpcunpack() call..."); { - float fld[DATA_LEN] = {1.0, 2.0, 3.0, 0.0}; + float fld[DATA_LEN] = {100.0, 200.0, 300.0, 0.0}; float fld_in[DATA_LEN]; g2int lcpack = PACKED_LEN; g2int idrstmpl[7] = {0, 1, 1, 16, 0, 0, 0}; @@ -103,7 +103,7 @@ main() printf("ok!\n"); printf("Testing g2c_jpcpackd()/g2c_jpcunpackd() call..."); { - double fld[DATA_LEN] = {1.0, 2.0, 3.0, 0.0}; + double fld[DATA_LEN] = {10000.0, 20000.0, 30000.0, 0.0}; double fld_in[DATA_LEN]; size_t lcpack_st = PACKED_LEN; int idrstmpl[7] = {0, 1, 1, 16, 0, 0, 0}; @@ -125,7 +125,7 @@ main() printf("ok!\n"); printf("Testing g2c_jpcpackf()/g2c_jpcunpackf() call..."); { - float fld[DATA_LEN] = {1.0, 2.0, 3.0, 0.0}; + float fld[DATA_LEN] = {1000.0, 2000.0, 3000.0, 0.0}; float fld_in[DATA_LEN]; size_t lcpack_st = PACKED_LEN; int idrstmpl[7] = {0, 1, 1, 16, 0, 0, 0};