NCEPLIBS-g2c  1.7.0
jpcunpack.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "grib2_int.h"
9 
32 static g2int
33 jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
34  void *fld, int fld_is_double)
35 {
36  g2int *ifld;
37  g2int j, nbits;
38  float ref, bscale, dscale;
39  float *ffld = fld;
40  double *dfld = fld;
41 
42  rdieee(idrstmpl, &ref, 1);
43  bscale = int_power(2.0, idrstmpl[1]);
44  dscale = int_power(10.0, -idrstmpl[2]);
45  nbits = idrstmpl[3];
46 
47  /* If nbits equals 0, we have a constant field where the reference
48  * value is the data value at each gridpoint. */
49  if (nbits != 0)
50  {
51  if (!(ifld = calloc(ndpts, sizeof(g2int))))
52  {
53  fprintf(stderr, "Could not allocate space in jpcunpack.\n Data field NOT upacked.\n");
54  return G2_JPCUNPACK_MEM;
55  }
56  dec_jpeg2000((char *)cpack, len, ifld);
57  if (fld_is_double)
58  {
59  for (j = 0; j < ndpts; j++)
60  dfld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
61  }
62  else
63  {
64  for (j = 0; j < ndpts; j++)
65  ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
66  }
67  free(ifld);
68  }
69  else
70  {
71  if (fld_is_double)
72  {
73  for (j = 0; j < ndpts; j++)
74  dfld[j] = ref;
75  }
76  else
77  {
78  for (j = 0; j < ndpts; j++)
79  ffld[j] = ref;
80  }
81  }
82 
83  return G2_NO_ERROR;
84 }
85 
107 g2int
108 jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
109  float *fld)
110 {
111  return jpcunpack_int(cpack, len, idrstmpl, ndpts, fld, 0);
112 }
113 
134 g2int
135 jpcunpackd(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
136  double *fld)
137 {
138  return jpcunpack_int(cpack, len, idrstmpl, ndpts, fld, 1);
139 }
jpcunpack_int
static g2int jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition: jpcunpack.c:33
G2_NO_ERROR
#define G2_NO_ERROR
Function succeeded.
Definition: grib2.h:275
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
jpcunpackd
g2int jpcunpackd(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, double *fld)
Unpack JPEG2000 compressed data into an array of doubles, using info from the GRIB2 Data Representati...
Definition: jpcunpack.c:135
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
dec_jpeg2000
int dec_jpeg2000(char *injpc, g2int bufsize, g2int *outfld)
This Function decodes a JPEG2000 code stream specified in the JPEG2000 Part-1 standard (i....
Definition: dec_jpeg2000.c:38
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
G2_JPCUNPACK_MEM
#define G2_JPCUNPACK_MEM
In jpcunpack() or other unpack function: out of memory.
Definition: grib2.h:320
jpcunpack
g2int jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, float *fld)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition: jpcunpack.c:108