NCEPLIBS-g2c 1.9.0
Loading...
Searching...
No Matches
pngunpack.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stdlib.h>
8#include "grib2_int.h"
9
39static int
40pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
41 void *fld, int fld_is_double, int verbose)
42{
43 g2int *ifld;
44 g2int j, nbits, width, height;
45 float ref, bscale, dscale;
46 unsigned char *ctemp;
47 float *ffld = fld;
48 double *dfld = fld;
49
50 LOG((2, "pngunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
51
52 rdieee(idrstmpl, &ref, 1);
53 bscale = int_power(2.0, idrstmpl[1]);
54 dscale = int_power(10.0, -idrstmpl[2]);
55 nbits = idrstmpl[3];
56 LOG((2, "bscale %g dscale %g nbits %ld", bscale, dscale, nbits));
57
58 /* If nbits equals 0, we have a constant field where the reference
59 * value is the data value at each gridpoint. */
60 if (nbits != 0)
61 {
62 ifld = calloc(ndpts, sizeof(g2int));
63 ctemp = calloc(ndpts * 4, 1);
64 if (!ifld || !ctemp)
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_png(cpack, &width, &height, ctemp);
71 gbits(ctemp, ifld, 0, nbits, 0, ndpts);
72 for (j = 0; j < ndpts; j++)
73 {
74 if (fld_is_double)
75 dfld[j] = (((double)ifld[j] * bscale) + ref) * dscale;
76 else
77 ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
78 }
79 free(ctemp);
80 free(ifld);
81 }
82 else
83 {
84 for (j = 0; j < ndpts; j++)
85 {
86 if (fld_is_double)
87 dfld[j] = ref;
88 else
89 ffld[j] = ref;
90 }
91 }
92
93 return 0;
94}
95
115g2int
116pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
117 float *fld)
118{
119 int ret;
120
121 if ((ret = pngunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2C_ENOMEM)
122 return G2_JPCUNPACK_MEM;
123
124 return ret;
125}
126
145int
146g2c_pngunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
147 float *fld)
148{
150 g2int len8 = len, ndpts8 = ndpts;
151 int i;
152
153 for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
154 idrstmpl8[i] = idrstmpl[i];
155
156 return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
157}
158
177int
178g2c_pngunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
179 double *fld)
180{
182 g2int len8 = len, ndpts8 = ndpts;
183 int i;
184
185 for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
186 idrstmpl8[i] = idrstmpl[i];
187
188 return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
189}
int dec_png(unsigned char *pngbuf, g2int *width, g2int *height, unsigned char *cout)
Decode PNG.
Definition dec_png.c:61
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
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:500
#define G2C_PNG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for PNG packing.
Definition grib2.h:421
#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
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 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:116
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:178
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:146
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