Loading...
Searching...
No Matches
jpcunpack.c
Go to the documentation of this file.
#include "grib2_int.h"
#include <stdio.h>
#include <stdlib.h>
static int
jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
void *fld, int fld_is_double, int verbose)
{
g2int *ifld;
g2int j, nbits;
float ref, bscale, dscale;
float *ffld = fld;
double *dfld = fld;
LOG((2, "jpcunpack_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];
/* If nbits equals 0, we have a constant field where the reference
* value is the data value at each gridpoint. */
if (nbits != 0)
{
if (!(ifld = calloc(ndpts, sizeof(g2int))))
{
if (verbose)
fprintf(stderr, "Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
return G2C_ENOMEM;
}
dec_jpeg2000((char *)cpack, len, ifld);
if (fld_is_double)
{
for (j = 0; j < ndpts; j++)
dfld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}
else
{
for (j = 0; j < ndpts; j++)
ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
}
free(ifld);
}
else
{
if (fld_is_double)
{
for (j = 0; j < ndpts; j++)
dfld[j] = ref;
}
else
{
for (j = 0; j < ndpts; j++)
ffld[j] = ref;
}
}
return G2C_NOERROR;
}
g2int
jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
float *fld)
{
int ret;
LOG((2, "g2c_jpcunpack len %lld ndpts %lld", len, ndpts));
if ((ret = jpcunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2_JPCUNPACK_MEM)
return G2_JPCUNPACK_MEM;
return ret;
}
int
g2c_jpcunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
float *fld)
{
g2int idrstmpl8[G2C_JPEG_DRS_TEMPLATE_LEN];
g2int len8 = len, ndpts8 = ndpts;
int i;
LOG((2, "g2c_jpcunpackf len %d ndpts %lld", len, ndpts));
for (i = 0; i < G2C_JPEG_DRS_TEMPLATE_LEN; i++)
idrstmpl8[i] = idrstmpl[i];
return jpcunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
}
int
g2c_jpcunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
double *fld)
{
g2int idrstmpl8[G2C_JPEG_DRS_TEMPLATE_LEN];
g2int len8 = len, ndpts8 = ndpts;
int i;
LOG((2, "g2c_jpcunpackd len %lld ndpts %lld", len, ndpts));
for (i = 0; i < G2C_JPEG_DRS_TEMPLATE_LEN; i++)
idrstmpl8[i] = idrstmpl[i];
return jpcunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
}
dec_jpeg2000
int dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
Decode a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i.e., ISO/IEC 15444-1) using...
Definition decenc_jpeg2000.c:377
G2C_JPEG_DRS_TEMPLATE_LEN
#define G2C_JPEG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for JPEG packing.
Definition grib2.h:411
G2C_ENOMEM
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:493
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
G2C_NOERROR
#define G2C_NOERROR
No error.
Definition grib2.h:484
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
jpcunpack
g2int jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition jpcunpack.c:123
g2c_jpcunpackd
int g2c_jpcunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, double *fld)
Unpack JPEG2000 compressed data into an array of doubles, using info from the GRIB2 Data Representati...
Definition jpcunpack.c:199
g2c_jpcunpackf
int g2c_jpcunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, float *fld)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition jpcunpack.c:159
jpcunpack_int
static int jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double, int verbose)
Unpack JPEG2000 compressed data into an array of floats or doubles, using info from the GRIB2 Data Re...
Definition jpcunpack.c:44