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

fix g2c_dec_png and make png grid size configurable #558

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ if(USE_PNG)
find_package(ZLIB REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(PNG REQUIRED)
set(PNG_WIDTH_MAX 100000000 CACHE STRING "Default width of PNG grid.")
set(PNG_HEIGHT_MAX 100000 CACHE STRING "Default height of PNG grid.")
message(STATUS "\tWill build with PNG grid width = ${PNG_WIDTH_MAX} and height = ${PNG_HEIGHT_MAX}")
else()
message(STATUS "Will not build PNG support")
endif()
Expand Down
13 changes: 12 additions & 1 deletion src/decenc_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ int
g2c_dec_png(unsigned char *pngbuf, int *width, int *height,
unsigned char *cout)
{
return dec_png(pngbuf, (g2int *)&width, (g2int *)&height, cout);
g2int width8 = *width, height8 = *height;
int ret;

ret = dec_png(pngbuf, &width8, &height8, cout);

*width = (g2int)width8;
*height = (g2int)height8;

return ret;
}

/**
Expand Down Expand Up @@ -162,6 +170,9 @@ dec_png(unsigned char *pngbuf, g2int *width, g2int *height,
/* Set new custom read function. */
png_set_read_fn(png_ptr, (png_voidp)&read_io_ptr, (png_rw_ptr)user_read_data);

/* support for larger grids */
png_set_user_limits(png_ptr, G2C_PNG_WIDTH_MAX, G2C_PNG_HEIGHT_MAX);

/* Read and decode PNG stream. */
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);

Expand Down
2 changes: 2 additions & 0 deletions src/grib2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ int g2c_param_all(int param_idx, int *g1ver, int *g1val, int *g2disc, int *g2cat

#define G2C_JASPER_MAX_MEM @JASPER_MAX_MEM@ /**< Maximum size for the Jasper memory buffer. */

#define G2C_PNG_WIDTH_MAX @PNG_WIDTH_MAX@ /**< Maximum width of PNG grid */
#define G2C_PNG_HEIGHT_MAX @PNG_HEIGHT_MAX@ /**< Maximum height of PNG grid */
/* Error codes for G2 API. */
#define G2_NO_ERROR 0 /**< Function succeeded. */
#define G2_CREATE_GRIB_VERSION -1 /**< Wrong GRIB version for g2_create(), must be 2. */
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ set(REF_FILES "gdaswave.t00z.wcoast.0p16.f000.grib2.idx"
"ref_testgrib2.grb2.degrib2"
"ref_gdaswave_2.grib1.idx"
"ref_tst_jasper_warning.txt"
"ref_large_png.grib2"
)

# Copy large files if needed.
Expand Down Expand Up @@ -223,6 +224,7 @@ endif()
if(USE_PNG)
g2c_test(tst_png)
g2c_test(tst_addfield2)
g2c_test(tst_large_png)
endif()

# Run these tests only if Jasper or OpenJPEG are linked.
Expand Down
Binary file added tests/data/ref_large_png.grib2
Binary file not shown.
48 changes: 48 additions & 0 deletions tests/tst_large_png.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* This is a test for the NCEPLIBS-g2c project. This test is for
* the PNG decode function on a large test file.
*
* Alyson Stahl 11/21/24
*/

#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>

#define TEST_FILE "data/ref_large_png.grib2"

int
main()
{
printf("Testing dec_png() on large test file...\n");
{
float *data;
int g2cid, num_data_points;
int ret = G2C_NOERROR;

/* Open the data file. */
if ((ret = g2c_open(TEST_FILE, 0, &g2cid)))
return ret;

/* Get the size of the data from message 0, product 0. */
if ((ret = g2c_get_prod(g2cid, 0, 0, &num_data_points, NULL)))
return ret;

/* Allocate storage for the data. */
if (!(data = malloc(num_data_points * sizeof(float))))
return G2C_ERROR;

/* Get the data from message 0, product 0. */
if ((ret = g2c_get_prod(g2cid, 0, 0, NULL, data)))
return ret;

/* Close the data file. */
if ((ret = g2c_close(g2cid)))
return ret;

/* Free the memory allocated to hold the data. */
free(data);
}
printf("ok!\n");
printf("SUCCESS!\n");
return 0;
}
7 changes: 7 additions & 0 deletions tests/tst_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ main()
return G2C_ERROR;
}

/* Check that width and height are returned correctly. */
if (width_in != 1 || height_in != 1)
{
printf("png size mismatch: width = %d, height = %d", width_in, height_in);
return G2C_ERROR;
}

for (i = 0; i < 4; i++)
if (cout[i] != data[i])
return G2C_ERROR;
Expand Down
Loading