NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
jpcunpack.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stdlib.h>
8#include "grib2_int.h"
9
43static int
44jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
45 void *fld, int fld_is_double, int verbose)
46{
47 g2int *ifld;
48 g2int j, nbits;
49 float ref, bscale, dscale;
50 float *ffld = fld;
51 double *dfld = fld;
52
53 LOG((2, "jpcunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
54
55 rdieee(idrstmpl, &ref, 1);
56 bscale = int_power(2.0, idrstmpl[1]);
57 dscale = int_power(10.0, -idrstmpl[2]);
58 nbits = idrstmpl[3];
59
60 /* If nbits equals 0, we have a constant field where the reference
61 * value is the data value at each gridpoint. */
62 if (nbits != 0)
63 {
64 if (!(ifld = calloc(ndpts, sizeof(g2int))))
65 {
66 if (verbose)
67 fprintf(stderr, "Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
68 return G2C_ENOMEM;
69 }
70 dec_jpeg2000((char *)cpack, len, ifld);
71 if (fld_is_double)
72 {
73 for (j = 0; j < ndpts; j++)
74 dfld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
75 }
76 else
77 {
78 for (j = 0; j < ndpts; j++)
79 ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
80 }
81 free(ifld);
82 }
83 else
84 {
85 if (fld_is_double)
86 {
87 for (j = 0; j < ndpts; j++)
88 dfld[j] = ref;
89 }
90 else
91 {
92 for (j = 0; j < ndpts; j++)
93 ffld[j] = ref;
94 }
95 }
96
97 return G2C_NOERROR;
98}
99
122g2int
123jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
124 float *fld)
125{
126 int ret;
127
128 LOG((2, "g2c_jpcunpack len %lld ndpts %lld", len, ndpts));
129
130 if ((ret = jpcunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2_JPCUNPACK_MEM)
131 return G2_JPCUNPACK_MEM;
132
133 return ret;
134}
135
158int
159g2c_jpcunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
160 float *fld)
161{
163 g2int len8 = len, ndpts8 = ndpts;
164 int i;
165
166 LOG((2, "g2c_jpcunpackf len %d ndpts %lld", len, ndpts));
167
168 for (i = 0; i < G2C_JPEG_DRS_TEMPLATE_LEN; i++)
169 idrstmpl8[i] = idrstmpl[i];
170
171 return jpcunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
172}
173
198int
199g2c_jpcunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
200 double *fld)
201{
203 g2int len8 = len, ndpts8 = ndpts;
204 int i;
205
206 LOG((2, "g2c_jpcunpackd len %lld ndpts %lld", len, ndpts));
207
208 for (i = 0; i < G2C_JPEG_DRS_TEMPLATE_LEN; i++)
209 idrstmpl8[i] = idrstmpl[i];
210
211 return jpcunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
212}
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...
#define G2C_JPEG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for JPEG packing.
Definition grib2.h:420
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:500
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition grib2.h:483
int64_t g2int
Long integer type.
Definition grib2.h:32
#define G2C_NOERROR
No error.
Definition grib2.h:491
Header file with internal function prototypes NCEPLIBS-g2c library.
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition int_power.c:18
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:426
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
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
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
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
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