NCEPLIBS-g2c  1.7.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 
31 static g2int
32 pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
33  void *fld, int fld_is_double)
34 {
35  g2int *ifld;
36  g2int j, nbits, width, height;
37  float ref, bscale, dscale;
38  unsigned char *ctemp;
39  float *ffld = fld;
40  double *dfld = fld;
41 
42  LOG((2, "pngunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
43 
44  rdieee(idrstmpl, &ref, 1);
45  bscale = int_power(2.0, idrstmpl[1]);
46  dscale = int_power(10.0, -idrstmpl[2]);
47  nbits = idrstmpl[3];
48  LOG((2, "bscale %g dscale %g nbits %ld", bscale, dscale, nbits));
49 
50  /* If nbits equals 0, we have a constant field where the reference
51  * value is the data value at each gridpoint. */
52  if (nbits != 0)
53  {
54  ifld = calloc(ndpts, sizeof(g2int));
55  ctemp = calloc(ndpts * 4, 1);
56  if (!ifld || !ctemp)
57  {
58  fprintf(stderr,"Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
59  return G2_JPCUNPACK_MEM;
60  }
61  dec_png(cpack, &width, &height, ctemp);
62  gbits(ctemp, ifld, 0, nbits, 0, ndpts);
63  for (j = 0; j < ndpts; j++)
64  {
65  if (fld_is_double)
66  dfld[j] = (((double)ifld[j] * bscale) + ref) * dscale;
67  else
68  ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
69  }
70  free(ctemp);
71  free(ifld);
72  }
73  else
74  {
75  for (j = 0; j < ndpts; j++)
76  {
77  if (fld_is_double)
78  dfld[j] = ref;
79  else
80  ffld[j] = ref;
81  }
82  }
83 
84  return 0;
85 }
86 
104 g2int
105 pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
106  float *fld)
107 {
108  return pngunpack_int(cpack, len, idrstmpl, ndpts, fld, 0);
109 }
110 
127 g2int
128 pngunpackd(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
129  double *fld)
130 {
131  return pngunpack_int(cpack, len, idrstmpl, ndpts, fld, 1);
132 }
dec_png
int dec_png(unsigned char *pngbuf, g2int *width, g2int *height, unsigned char *cout)
Decode PNG.
Definition: dec_png.c:61
pngunpackd
g2int pngunpackd(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int 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:128
rdieee
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
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:138
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
gbits
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
G2_JPCUNPACK_MEM
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition: grib2.h:320
pngunpack_int
static g2int pngunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition: pngunpack.c:32
pngunpack
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:105