NCEPLIBS-g2c  1.8.0
pngunpack.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "grib2_int.h"
10 
40 static int
41 pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
42  void *fld, int fld_is_double, int verbose)
43 {
44  g2int *ifld;
45  g2int j, nbits, width, height;
46  float ref, bscale, dscale;
47  unsigned char *ctemp;
48  float *ffld = fld;
49  double *dfld = fld;
50 
51  LOG((2, "pngunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
52 
53  rdieee(idrstmpl, &ref, 1);
54  bscale = int_power(2.0, idrstmpl[1]);
55  dscale = int_power(10.0, -idrstmpl[2]);
56  nbits = idrstmpl[3];
57  LOG((2, "bscale %g dscale %g nbits %ld", bscale, dscale, nbits));
58 
59  /* If nbits equals 0, we have a constant field where the reference
60  * value is the data value at each gridpoint. */
61  if (nbits != 0)
62  {
63  ifld = calloc(ndpts, sizeof(g2int));
64  ctemp = calloc(ndpts * 4, 1);
65  if (!ifld || !ctemp)
66  {
67  if (verbose)
68  fprintf(stderr,"Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
69  return G2C_ENOMEM;
70  }
71  dec_png(cpack, &width, &height, ctemp);
72  gbits(ctemp, ifld, 0, nbits, 0, ndpts);
73  for (j = 0; j < ndpts; j++)
74  {
75  if (fld_is_double)
76  dfld[j] = (((double)ifld[j] * bscale) + ref) * dscale;
77  else
78  ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
79  }
80  free(ctemp);
81  free(ifld);
82  }
83  else
84  {
85  for (j = 0; j < ndpts; j++)
86  {
87  if (fld_is_double)
88  dfld[j] = ref;
89  else
90  ffld[j] = ref;
91  }
92  }
93 
94  return 0;
95 }
96 
116 g2int
117 pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
118  float *fld)
119 {
120  int ret;
121 
122  if ((ret = pngunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2C_ENOMEM)
123  return G2_JPCUNPACK_MEM;
124 
125  return ret;
126 }
127 
146 int
147 g2c_pngunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
148  float *fld)
149 {
150  g2int idrstmpl8[G2C_PNG_DRS_TEMPLATE_LEN];
151  g2int len8 = len, ndpts8 = ndpts;
152  int i;
153 
154  for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
155  idrstmpl8[i] = idrstmpl[i];
156 
157  return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
158 }
159 
178 int
179 g2c_pngunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
180  double *fld)
181 {
182  g2int idrstmpl8[G2C_PNG_DRS_TEMPLATE_LEN];
183  g2int len8 = len, ndpts8 = ndpts;
184  int i;
185 
186  for (i = 0; i < G2C_PNG_DRS_TEMPLATE_LEN; i++)
187  idrstmpl8[i] = idrstmpl[i];
188 
189  return pngunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
190 }
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)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
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:33
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)
This subroutine reads a list of real values in 32-bit IEEE floating point format.
Definition: rdieee.c:21
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:117
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:179
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:147
static int pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double, int verbose)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition: pngunpack.c:41