Fix crash in IEEE -> PNG conversion

This commit is contained in:
Shahram Najm 2021-02-17 16:40:45 +00:00
parent 189de0025f
commit ad238cb45e
2 changed files with 38 additions and 10 deletions

View File

@ -365,11 +365,26 @@ cleanup:
return err; return err;
} }
static int is_constant(const double* values, size_t n_vals)
{
int isConstant = 1;
double v = 0;
size_t i;
for (i = 0; i < n_vals; i++) {
if (i == 0) v = values[i];
else if (v != values[i]) {
isConstant = 0;
break;
}
}
return isConstant;
}
static int pack_double(grib_accessor* a, const double* val, size_t* len) static int pack_double(grib_accessor* a, const double* val, size_t* len)
{ {
grib_accessor_data_png_packing* self = (grib_accessor_data_png_packing*)a; grib_accessor_data_png_packing* self = (grib_accessor_data_png_packing*)a;
int err = GRIB_SUCCESS; int err = GRIB_SUCCESS, is_constant_field = 0;
int i, j; int i, j;
size_t buflen = 0; size_t buflen = 0;
@ -421,12 +436,18 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len)
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
if (bits_per_value == 0) { is_constant_field = is_constant(val, n_vals);
int i; if (!is_constant_field && bits_per_value==0) {
/* constant field */ /* A non-constant field with bitsPerValue==0! */
for (i = 1; i < n_vals; i++) bits_per_value = 24; /* Set sane value */
Assert(val[i] == val[0]); }
if (is_constant_field) {
#ifdef DEBUG
for (i = 1; i < n_vals; i++) {
Assert(val[i] == val[0]);
}
#endif
if ((err = grib_set_double_internal(grib_handle_of_accessor(a), self->reference_value, val[0])) != GRIB_SUCCESS) if ((err = grib_set_double_internal(grib_handle_of_accessor(a), self->reference_value, val[0])) != GRIB_SUCCESS)
return err; return err;
{ {
@ -491,10 +512,8 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len)
max = val[0]; max = val[0];
min = max; min = max;
for (i = 1; i < n_vals; i++) { for (i = 1; i < n_vals; i++) {
if (val[i] > max) if (val[i] > max) max = val[i];
max = val[i]; else if (val[i] < min) min = val[i];
else if (val[i] < min)
min = val[i];
} }
min *= d; min *= d;
max *= d; max *= d;

View File

@ -42,3 +42,12 @@ for file in $files; do
rm -f $temp $temp1 $temp2 rm -f $temp $temp1 $temp2
done done
# Conversion from IEEE to PNG
infile=${data_dir}/grid_ieee.grib
${tools_dir}/grib_set -r -s packingType=grid_png $infile $temp
# TODO: check results
grib_check_key_equals $temp packingType grid_png
rm -f $temp