Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added g2c_ versions of png and openjpeg enc/dec functions #545

Merged
merged 14 commits into from
Nov 1, 2024
72 changes: 72 additions & 0 deletions src/decenc_openjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 Alyson Stahl
*/
int
g2c_dec_jpeg2000(char *injpc, size_t bufsize, int *outfld)
{
return dec_jpeg2000(injpc, bufsize, (g2int *)outfld);
}

/**
* This Function decodes a JPEG2000 code stream specified in the
* JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using OpenJPEG.
Expand Down Expand Up @@ -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 Alyson Stahl
*/
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)
Expand Down
41 changes: 41 additions & 0 deletions src/decenc_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ 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 Alyson Stahl
*/
int
g2c_dec_png(unsigned char *pngbuf, int *width, int *height,
unsigned char *cout)
{
return dec_png(pngbuf, (g2int *)&width, (g2int *)&height, cout);
}

/**
* Decode PNG.
*
Expand Down Expand Up @@ -190,6 +209,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 Alyson Stahl
*/
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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/grib2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/tst_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading