Loading...
Searching...
No Matches
pngunpack.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
static int
pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
void *fld, int fld_is_double, int verbose)
{
g2int *ifld;
g2int j, nbits, width, height;
float ref, bscale, dscale;
unsigned char *ctemp;
float *ffld = fld;
double *dfld = fld;
LOG((2, "pngunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
rdieee(idrstmpl, &ref, 1);
bscale = int_power(2.0, idrstmpl[1]);
dscale = int_power(10.0, -idrstmpl[2]);
nbits = idrstmpl[3];
LOG((2, "bscale %g dscale %g nbits %ld", bscale, dscale, nbits));
/* If nbits equals 0, we have a constant field where the reference
* value is the data value at each gridpoint. */
if (nbits != 0)
{
ifld = calloc(ndpts, sizeof(g2int));
ctemp = calloc(ndpts * 4, 1);
if (!ifld || !ctemp)
{
if (verbose)
fprintf(stderr, "Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
return G2C_ENOMEM;
}
dec_png(cpack, &width, &height, ctemp);
int bytes_per_row = (nbits * width) / 8;
if ((width * nbits) % 8 != 0)
{
bytes_per_row++;
}
for (j = 0; j < height; j++)
{
gbits(ctemp + (j * bytes_per_row), ifld + (j * width), 0, nbits, 0, width);
}
for (j = 0; j < ndpts; j++)
{
if (fld_is_double)
dfld[j] = (((double)ifld[j] * bscale) + ref) * dscale;
else
ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}
free(ctemp);
free(ifld);
}
else
{
for (j = 0; j < ndpts; j++)
{
if (fld_is_double)
dfld[j] = ref;
else
ffld[j] = ref;
}
}
return 0;
}
g2int
pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
float *fld)
{
int ret;
if ((ret = pngunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2C_ENOMEM)
return G2_JPCUNPACK_MEM;
return ret;
}
int
g2c_pngunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
float *fld)
{
g2int idrstmpl8[G2C_PNG_DRS_TEMPLATE_LEN];
g2int len8 = len, ndpts8 = ndpts;
int i;
for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
idrstmpl8[i] = idrstmpl[i];
return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
}
int
g2c_pngunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
double *fld)
{
g2int idrstmpl8[G2C_PNG_DRS_TEMPLATE_LEN];
g2int len8 = len, ndpts8 = ndpts;
int i;
for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
idrstmpl8[i] = idrstmpl[i];
return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
}
dec_png
int dec_png(unsigned char *pngbuf, g2int *width, g2int *height, unsigned char *cout)
Decode PNG.
Definition decenc_png.c:127
gbits
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits, g2int nskip, g2int n)
Unpack arbitrary size values from a packed bit string, right justifying each value in the unpacked io...
Definition gbits.c:57
G2C_ENOMEM
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:493
G2C_PNG_DRS_TEMPLATE_LEN
#define G2C_PNG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for PNG packing.
Definition grib2.h:412
G2_JPCUNPACK_MEM
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition grib2.h:476
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
int_power
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition int_power.c:18
LOG
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428
rdieee
void rdieee(g2int *rieee, float *a, g2int num)
Read a list of real values in 32-bit IEEE floating point format.
Definition rdieee.c:20
pngunpack
g2int pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition pngunpack.c:126
g2c_pngunpackd
int g2c_pngunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, double *fld)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition pngunpack.c:188
g2c_pngunpackf
int g2c_pngunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, float *fld)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition pngunpack.c:156
pngunpack_int
static int pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double, int verbose)
Unpack a data field that was packed into a PNG image format using info from the GRIB2 Data Representa...
Definition pngunpack.c:40