NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
pngunpack.c
Go to the documentation of this file.
1
6#include "grib2_int.h"
7#include <stdio.h>
8#include <stdlib.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
72 int bytes_per_row = (nbits * width) / 8;
73 if ((width * nbits) % 8 != 0)
74 {
75 bytes_per_row++;
76 }
77 for (j = 0; j < height; j++)
78 {
79 gbits(ctemp + (j * bytes_per_row), ifld + (j * width), 0, nbits, 0, width);
80 }
81
82 for (j = 0; j < ndpts; j++)
83 {
84 if (fld_is_double)
85 dfld[j] = (((double)ifld[j] * bscale) + ref) * dscale;
86 else
87 ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
88 }
89 free(ctemp);
90 free(ifld);
91 }
92 else
93 {
94 for (j = 0; j < ndpts; j++)
95 {
96 if (fld_is_double)
97 dfld[j] = ref;
98 else
99 ffld[j] = ref;
100 }
101 }
102
103 return 0;
104}
105
125g2int
126pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
127 float *fld)
128{
129 int ret;
130
131 if ((ret = pngunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2C_ENOMEM)
132 return G2_JPCUNPACK_MEM;
133
134 return ret;
135}
136
155int
156g2c_pngunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
157 float *fld)
158{
160 g2int len8 = len, ndpts8 = ndpts;
161 int i;
162
163 for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
164 idrstmpl8[i] = idrstmpl[i];
165
166 return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
167}
168
187int
188g2c_pngunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
189 double *fld)
190{
192 g2int len8 = len, ndpts8 = ndpts;
193 int i;
194
195 for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
196 idrstmpl8[i] = idrstmpl[i];
197
198 return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
199}
int dec_png(unsigned char *pngbuf, g2int *width, g2int *height, unsigned char *cout)
Decode PNG.
Definition decenc_png.c:100
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:485
#define G2C_PNG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for PNG packing.
Definition grib2.h:406
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition grib2.h:468
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:428
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:126
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
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
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