From 0cd0f2752a55e0208ba2a7a3d3977affb5ab9585 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:01:19 -0700 Subject: [PATCH 01/13] added g2c_ functions with int inputs for png and openjpeg enc/dec functions --- src/decenc_openjpeg.c | 74 ++++++++++++++++++++++++++++++++++++++++++- src/decenc_png.c | 45 +++++++++++++++++++++++++- src/grib2.h.in | 6 +++- tests/tst_png.c | 30 +++++++++++++++++- 4 files changed, 151 insertions(+), 4 deletions(-) diff --git a/src/decenc_openjpeg.c b/src/decenc_openjpeg.c index 124d7831..311e696c 100644 --- a/src/decenc_openjpeg.c +++ b/src/decenc_openjpeg.c @@ -236,6 +236,33 @@ opj_stream_create_default_memory_stream(opj_memory_stream *memoryStream, OPJ_BOO return stream; } +/** + * This Function decodes a JPEG2000 code stream specified in the + * JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using OpenJPEG. + * + * PROGRAM HISTORY LOG: + * - 2002-12-02 Gilbert + * - 2016-06-08 Jovic + * + * @param injpc Input JPEG2000 code stream. + * @param bufsize Length (in bytes) of the input JPEG2000 code stream. + * @param outfld Output matrix of grayscale image values. + * + * @return + * - 0 Successful decode + * - -3 Error decode jpeg2000 code stream. + * - -5 decoded image had multiple color components. Only grayscale is expected. + * + * @note Requires OpenJPEG Version 2. + * + * @author Stephen Gilbert, Jovic + */ +int +g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld) +{ + return int_dec_jpeg2000(injpc, bufsize, outfld, 0); +} + /** * This Function decodes a JPEG2000 code stream specified in the * JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using OpenJPEG. @@ -342,6 +369,51 @@ dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld) return iret; } +/** + * This Function encodes a grayscale image into a JPEG2000 code stream + * specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) + * using OpenJPEG library. + * + * PROGRAM HISTORY LOG: + * - 2002-12-02 Gilbert + * - 2016-06-08 Jovic + * + * @param cin Packed matrix of Grayscale image values to encode. + * @param width width of image + * @param height height of image + * @param nbits depth (in bits) of image.i.e number of bits used to + * hold each data value + * @param ltype indicator of lossless or lossy compression = 1, for + * lossy compression != 1, for lossless compression. + * @param ratio target compression ratio. (ratio:1) Used only when + * ltype == 1. + * @param retry Pointer to option type. 1 = try increasing number of + * guard bits otherwise, no additional options. + * @param outjpc Output encoded JPEG2000 code stream. + * @param jpclen Number of bytes allocated for new JPEG2000 code + * stream in outjpc. + * + * @return + * - > 0 Length in bytes of encoded JPEG2000 code stream + * - -3 Error decode jpeg2000 code stream. + * - -5 decoded image had multiple color components. Only grayscale is expected. + * + * @note Requires OpenJPEG Version 2. + * + * @author Stephen Gilbert, Jovic + */ +int +g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, + int ltype, int ratio, int retry, char *outjpc, + size_t jpclen) +{ + g2int width8 = width, height8 = height, nbits8 = nbits, ltype8 = ltype; + g2int ratio8 = ratio, retry8 = retry, jpclen8 = jpclen; + + return enc_jpeg2000(cin, width8, height8, nbits8, ltype8, ratio8, retry8, + outjpc, jpclen8); +} + /** * This Function encodes a grayscale image into a JPEG2000 code stream * specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) @@ -512,4 +584,4 @@ enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, return iret; } -#endif +#endif \ No newline at end of file diff --git a/src/decenc_png.c b/src/decenc_png.c index 98d7e03c..d9d5a311 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -84,6 +84,27 @@ user_flush_data(png_structp png_ptr) { } +/** + * Decode PNG. + * + * @param pngbuf Pointer to PNG buffer. + * @param width Pointer to width. + * @param height Pointer to height. + * @param cout Output buffer. + * + * @return 0 for success, error code otherwise. + * + * @author Stephen Gilbert + */ +int +g2c_dec_png(unsigned char *pngbuf, int *width, int *height, + unsigned char *cout) +{ + g2int width8 = width, height8 = height; + + return dec_png(pngbuf, width8, height8, cout); +} + /** * Decode PNG. * @@ -190,6 +211,28 @@ dec_png(unsigned char *pngbuf, g2int *width, g2int *height, return 0; } +/** + * Encode PNG. + * + * @param data data. + * @param width width. + * @param height height. + * @param nbits number of bits. + * @param pngbuf PNG buffer. + * + * @return PNG length, or negative number for error. + * + * @author Stephen Gilbert + */ +int +g2c_enc_png(unsigned char *data, int width, int height, int nbits, + unsigned char *pngbuf) +{ + g2int width8 = width, height8 = height, nbits8 = nbits; + + return enc_png(data, width8, height8, nbits8, pngbuf); +} + /** * Encode PNG. * @@ -277,4 +320,4 @@ enc_png(unsigned char *data, g2int width, g2int height, g2int nbits, free(row_pointers); pnglen = write_io_ptr.stream_len; return pnglen; -} +} \ No newline at end of file diff --git a/src/grib2.h.in b/src/grib2.h.in index 0f80388d..d18a61e6 100644 --- a/src/grib2.h.in +++ b/src/grib2.h.in @@ -376,6 +376,10 @@ int g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, int ltype, int ratio, int retry, char *outjpc, size_t jpclen); int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld); +int g2c_enc_png(unsigned char *data, int width, int height, int nbits, + unsigned char *pngbuf); +int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, + unsigned char *cout); int g2c_aecpackf(float *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, size_t *lcpack); int g2c_aecpackd(double *fld, size_t width, size_t height, int *idrstmpl, @@ -502,4 +506,4 @@ int g2c_param_all(int param_idx, int *g1ver, int *g1val, int *g2disc, int *g2cat #define G2C_EAEC (-74) /**< Error encoding/decoding AEC data. */ #define G2C_ECSV (-75) /**< CSV error. */ -#endif /* _grib2_H */ +#endif /* _grib2_H */ \ No newline at end of file diff --git a/tests/tst_png.c b/tests/tst_png.c index 370b5a96..363b1c55 100644 --- a/tests/tst_png.c +++ b/tests/tst_png.c @@ -44,6 +44,34 @@ main() return G2C_ERROR; } printf("ok!\n"); + printf("Testing g2c_enc_png()/g2c_dec_png() calls..."); + { + unsigned char data[4] = {1, 2, 3, 4}; + int width = 1, height = 1, nbits = 32; + int width_in, height_in; + unsigned char pngbuf[200]; + unsigned char cout[200]; + int i, ret; + + /* Encode some data. */ + if ((ret = g2c_enc_png(data, width, height, nbits, pngbuf)) != 70) + { + printf("%d\n", ret); + return G2C_ERROR; + } + + /* Now decode it. */ + if ((ret = g2c_dec_png((unsigned char *)pngbuf, &width_in, &height_in, cout))) + { + printf("%d\n", ret); + return G2C_ERROR; + } + + for (i = 0; i < 4; i++) + if (cout[i] != data[i]) + return G2C_ERROR; + } + printf("ok!\n"); printf("Testing pngpack()/pngunpack() calls..."); { g2int height = 2, width = 2, ndpts = DATA_LEN, len = PACKED_LEN; @@ -311,4 +339,4 @@ main() printf("ok!\n"); printf("SUCCESS!\n"); return 0; -} +} \ No newline at end of file From 5e8408bbe941a63d7b3e6fdd5e085512e089a81a Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:29:28 -0700 Subject: [PATCH 02/13] fixing some inputs --- src/decenc_png.c | 2 +- src/grib2.h.in | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index d9d5a311..43fc2e71 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -97,7 +97,7 @@ user_flush_data(png_structp png_ptr) * @author Stephen Gilbert */ int -g2c_dec_png(unsigned char *pngbuf, int *width, int *height, +g2c_dec_png(unsigned char *pngbuf, int width, int height, unsigned char *cout) { g2int width8 = width, height8 = height; diff --git a/src/grib2.h.in b/src/grib2.h.in index d18a61e6..12d2033a 100644 --- a/src/grib2.h.in +++ b/src/grib2.h.in @@ -378,7 +378,7 @@ int g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld); int g2c_enc_png(unsigned char *data, int width, int height, int nbits, unsigned char *pngbuf); -int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, +int g2c_dec_png(unsigned char *pngbuf, int width, int height, unsigned char *cout); int g2c_aecpackf(float *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, size_t *lcpack); From 1cf0ce72e8e82b515a8b15bca08c8f2648ba5308 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:37:01 -0700 Subject: [PATCH 03/13] debug --- src/decenc_openjpeg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decenc_openjpeg.c b/src/decenc_openjpeg.c index 311e696c..71acd0e8 100644 --- a/src/decenc_openjpeg.c +++ b/src/decenc_openjpeg.c @@ -260,7 +260,7 @@ opj_stream_create_default_memory_stream(opj_memory_stream *memoryStream, OPJ_BOO int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld) { - return int_dec_jpeg2000(injpc, bufsize, outfld, 0); + return dec_jpeg2000(injpc, bufsize, (g2int *)outfld); } /** From a501efd16d234256eb81247698ed1b04fdc8e108 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:39:48 -0700 Subject: [PATCH 04/13] debug --- src/decenc_png.c | 2 +- tests/tst_png.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index 43fc2e71..421529f2 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -102,7 +102,7 @@ g2c_dec_png(unsigned char *pngbuf, int width, int height, { g2int width8 = width, height8 = height; - return dec_png(pngbuf, width8, height8, cout); + return dec_png(pngbuf, &width8, &height8, cout); } /** diff --git a/tests/tst_png.c b/tests/tst_png.c index 363b1c55..8453bdbc 100644 --- a/tests/tst_png.c +++ b/tests/tst_png.c @@ -61,7 +61,7 @@ main() } /* Now decode it. */ - if ((ret = g2c_dec_png((unsigned char *)pngbuf, &width_in, &height_in, cout))) + if ((ret = g2c_dec_png((unsigned char *)pngbuf, width_in, height_in, cout))) { printf("%d\n", ret); return G2C_ERROR; From 6ac1437d9fcd503ee63d2e21309797c47084fd50 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:42:12 -0700 Subject: [PATCH 05/13] Update tst_png.c --- tests/tst_png.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tst_png.c b/tests/tst_png.c index 8453bdbc..363b1c55 100644 --- a/tests/tst_png.c +++ b/tests/tst_png.c @@ -61,7 +61,7 @@ main() } /* Now decode it. */ - if ((ret = g2c_dec_png((unsigned char *)pngbuf, width_in, height_in, cout))) + if ((ret = g2c_dec_png((unsigned char *)pngbuf, &width_in, &height_in, cout))) { printf("%d\n", ret); return G2C_ERROR; From 707ef6e097683ca1f14b7c883591ca5ce7e5050a Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:57:53 -0700 Subject: [PATCH 06/13] debugging --- src/decenc_png.c | 4 ++-- src/grib2.h.in | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index 421529f2..dd081cfb 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -97,10 +97,10 @@ user_flush_data(png_structp png_ptr) * @author Stephen Gilbert */ int -g2c_dec_png(unsigned char *pngbuf, int width, int height, +g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout) { - g2int width8 = width, height8 = height; + g2int* width8 = width, height8 = height; return dec_png(pngbuf, &width8, &height8, cout); } diff --git a/src/grib2.h.in b/src/grib2.h.in index 12d2033a..d18a61e6 100644 --- a/src/grib2.h.in +++ b/src/grib2.h.in @@ -378,7 +378,7 @@ int g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld); int g2c_enc_png(unsigned char *data, int width, int height, int nbits, unsigned char *pngbuf); -int g2c_dec_png(unsigned char *pngbuf, int width, int height, +int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout); int g2c_aecpackf(float *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, size_t *lcpack); From 6b959c3ed763ff244a475e7c625e9ed7b5a79da9 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:00:53 -0700 Subject: [PATCH 07/13] debugging casting pointers --- src/decenc_png.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index dd081cfb..b8c83c53 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -100,7 +100,7 @@ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout) { - g2int* width8 = width, height8 = height; + g2int *width8 = &width, *height8 = &height; return dec_png(pngbuf, &width8, &height8, cout); } From aa783a8cb9569edeb9b7d2c5a2746bf4b2ce7389 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:04:21 -0700 Subject: [PATCH 08/13] debug --- src/decenc_png.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index b8c83c53..75dbb19a 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -100,7 +100,7 @@ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout) { - g2int *width8 = &width, *height8 = &height; + g2int width8 = &width, height8 = &height; return dec_png(pngbuf, &width8, &height8, cout); } From c2d8ef1a2b100a8f55554a43e9ed860cc97f3f03 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:11:31 -0700 Subject: [PATCH 09/13] debugging --- src/decenc_png.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index 75dbb19a..51ba3001 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -100,9 +100,9 @@ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout) { - g2int width8 = &width, height8 = &height; + g2int *width8 = width, *height8 = height; - return dec_png(pngbuf, &width8, &height8, cout); + return dec_png(pngbuf, width8, height8, cout); } /** From 6f978f0c059f6afa87d398e26bdd4420b05484e3 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:15:29 -0700 Subject: [PATCH 10/13] more pointer debugging --- src/decenc_png.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index 51ba3001..1e007ed6 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -100,7 +100,7 @@ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout) { - g2int *width8 = width, *height8 = height; + g2int* width8 = (g2int)&width, height8 = (g2int)&height; return dec_png(pngbuf, width8, height8, cout); } From 186247050f01e72c3c259c3a744f9480972b1154 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:17:19 -0700 Subject: [PATCH 11/13] more debug --- src/decenc_png.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index 1e007ed6..72a55e28 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -100,9 +100,9 @@ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, unsigned char *cout) { - g2int* width8 = (g2int)&width, height8 = (g2int)&height; + // g2int* width8 = (g2int)&width, height8 = (g2int)&height; - return dec_png(pngbuf, width8, height8, cout); + return dec_png(pngbuf, (g2int *)&width, (g2int *)&height, cout); } /** From 376438aa9e932416dc6a9d0a623496f81579ecc8 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:21:46 -0700 Subject: [PATCH 12/13] Update decenc_png.c --- src/decenc_png.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/decenc_png.c b/src/decenc_png.c index 72a55e28..854c4bc2 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -98,10 +98,8 @@ user_flush_data(png_structp png_ptr) */ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, - unsigned char *cout) + unsigned char *cout) { - // g2int* width8 = (g2int)&width, height8 = (g2int)&height; - return dec_png(pngbuf, (g2int *)&width, (g2int *)&height, cout); } @@ -226,7 +224,7 @@ dec_png(unsigned char *pngbuf, g2int *width, g2int *height, */ int g2c_enc_png(unsigned char *data, int width, int height, int nbits, - unsigned char *pngbuf) + unsigned char *pngbuf) { g2int width8 = width, height8 = height, nbits8 = nbits; From 80178b65b74636448ea10e4e583d2fbc074c0ba3 Mon Sep 17 00:00:00 2001 From: AlysonStahl-NOAA <166434581+AlysonStahl-NOAA@users.noreply.github.com> Date: Fri, 1 Nov 2024 08:34:27 -0700 Subject: [PATCH 13/13] applying requested changes --- src/decenc_openjpeg.c | 6 +++--- src/decenc_png.c | 6 +++--- src/grib2.h.in | 2 +- tests/tst_png.c | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/decenc_openjpeg.c b/src/decenc_openjpeg.c index 71acd0e8..a1097104 100644 --- a/src/decenc_openjpeg.c +++ b/src/decenc_openjpeg.c @@ -255,7 +255,7 @@ opj_stream_create_default_memory_stream(opj_memory_stream *memoryStream, OPJ_BOO * * @note Requires OpenJPEG Version 2. * - * @author Stephen Gilbert, Jovic + * @author Alyson Stahl */ int g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld) @@ -400,7 +400,7 @@ dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld) * * @note Requires OpenJPEG Version 2. * - * @author Stephen Gilbert, Jovic + * @author Alyson Stahl */ int g2c_enc_jpeg2000(unsigned char *cin, int width, int height, int nbits, @@ -584,4 +584,4 @@ enc_jpeg2000(unsigned char *cin, g2int width, g2int height, g2int nbits, return iret; } -#endif \ No newline at end of file +#endif diff --git a/src/decenc_png.c b/src/decenc_png.c index 854c4bc2..a69ab558 100644 --- a/src/decenc_png.c +++ b/src/decenc_png.c @@ -94,7 +94,7 @@ user_flush_data(png_structp png_ptr) * * @return 0 for success, error code otherwise. * - * @author Stephen Gilbert + * @author Alyson Stahl */ int g2c_dec_png(unsigned char *pngbuf, int *width, int *height, @@ -220,7 +220,7 @@ dec_png(unsigned char *pngbuf, g2int *width, g2int *height, * * @return PNG length, or negative number for error. * - * @author Stephen Gilbert + * @author Alyson Stahl */ int g2c_enc_png(unsigned char *data, int width, int height, int nbits, @@ -318,4 +318,4 @@ enc_png(unsigned char *data, g2int width, g2int height, g2int nbits, free(row_pointers); pnglen = write_io_ptr.stream_len; return pnglen; -} \ No newline at end of file +} diff --git a/src/grib2.h.in b/src/grib2.h.in index d18a61e6..3f12474c 100644 --- a/src/grib2.h.in +++ b/src/grib2.h.in @@ -506,4 +506,4 @@ int g2c_param_all(int param_idx, int *g1ver, int *g1val, int *g2disc, int *g2cat #define G2C_EAEC (-74) /**< Error encoding/decoding AEC data. */ #define G2C_ECSV (-75) /**< CSV error. */ -#endif /* _grib2_H */ \ No newline at end of file +#endif /* _grib2_H */ diff --git a/tests/tst_png.c b/tests/tst_png.c index 363b1c55..434dc427 100644 --- a/tests/tst_png.c +++ b/tests/tst_png.c @@ -339,4 +339,4 @@ main() printf("ok!\n"); printf("SUCCESS!\n"); return 0; -} \ No newline at end of file +}