NCEPLIBS-g2c  1.6.4
pngunpack.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "grib2.h"
10 
11 int dec_png(unsigned char *, g2int *, g2int *, char *);
12 
29 g2int
30 pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
31  g2float *fld)
32 {
33  g2int *ifld;
34  g2int j, nbits, width, height;
35  g2float ref, bscale, dscale;
36  unsigned char *ctemp;
37 
38  rdieee(idrstmpl, &ref, 1);
39  bscale = int_power(2.0, idrstmpl[1]);
40  dscale = int_power(10.0, -idrstmpl[2]);
41  nbits = idrstmpl[3];
42 
43  /* If nbits equals 0, we have a constant field where the reference
44  * value is the data value at each gridpoint. */
45  if (nbits != 0)
46  {
47  ifld = calloc(ndpts, sizeof(g2int));
48  ctemp = calloc(ndpts * 4, 1);
49  if (!ifld || !ctemp)
50  {
51  fprintf(stderr,"Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
52  return(1);
53  }
54  dec_png(cpack, &width, &height, ctemp);
55  gbits(ctemp, ifld, 0, nbits, 0, ndpts);
56  for (j = 0; j < ndpts; j++)
57  fld[j] = (((g2float)ifld[j] * bscale) + ref) * dscale;
58  free(ctemp);
59  free(ifld);
60  }
61  else
62  {
63  for (j = 0; j < ndpts; j++)
64  fld[j] = ref;
65  }
66 
67  return(0);
68 }
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte, g2int nskip, g2int n)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:57
Header file for NCEPLIBS-g2c library.
float g2float
Float type.
Definition: grib2.h:22
int64_t g2int
Long integer type.
Definition: grib2.h:20
double int_power(double x, g2int y)
Function similar to C pow() power function.
Definition: int_power.c:17
int dec_png(unsigned char *, g2int *, g2int *, char *)
Decode PNG.
Definition: dec_png.c:71
g2int pngunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, g2float *fld)
This subroutine unpacks a data field that was packed into a PNG image format using info from the GRIB...
Definition: pngunpack.c:30
void rdieee(g2int *rieee, g2float *a, g2int num)
This subroutine reads a list of real values in 32-bit IEEE floating point format.
Definition: rdieee.c:20