NCEPLIBS-g2c  1.8.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 
44 static int
45 jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
46  void *fld, int fld_is_double, int verbose)
47 {
48  g2int *ifld;
49  g2int j, nbits;
50  float ref, bscale, dscale;
51  float *ffld = fld;
52  double *dfld = fld;
53 
54  LOG((2, "jpcunpack_int len %ld ndpts %ld fld_is_double %d", len, ndpts, fld_is_double));
55 
56  rdieee(idrstmpl, &ref, 1);
57  bscale = int_power(2.0, idrstmpl[1]);
58  dscale = int_power(10.0, -idrstmpl[2]);
59  nbits = idrstmpl[3];
60 
61  /* If nbits equals 0, we have a constant field where the reference
62  * value is the data value at each gridpoint. */
63  if (nbits != 0)
64  {
65  if (!(ifld = calloc(ndpts, sizeof(g2int))))
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_jpeg2000((char *)cpack, len, ifld);
72  if (fld_is_double)
73  {
74  for (j = 0; j < ndpts; j++)
75  dfld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
76  }
77  else
78  {
79  for (j = 0; j < ndpts; j++)
80  ffld[j] = (((float)ifld[j] * bscale) + ref) * dscale;
81  }
82  free(ifld);
83  }
84  else
85  {
86  if (fld_is_double)
87  {
88  for (j = 0; j < ndpts; j++)
89  dfld[j] = ref;
90  }
91  else
92  {
93  for (j = 0; j < ndpts; j++)
94  ffld[j] = ref;
95  }
96  }
97 
98  return G2C_NOERROR;
99 }
100 
123 g2int
124 jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
125  float *fld)
126 {
127  int ret;
128 
129  LOG((2, "g2c_jpcunpack len %lld ndpts %lld", len, ndpts));
130 
131  if ((ret = jpcunpack_int(cpack, len, idrstmpl, ndpts, fld, 0, 1)) == G2_JPCUNPACK_MEM)
132  return G2_JPCUNPACK_MEM;
133 
134  return ret;
135 }
136 
159 int
160 g2c_jpcunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
161  float *fld)
162 {
163  g2int idrstmpl8[G2C_JPEG_DRS_TEMPLATE_LEN];
164  g2int len8 = len, ndpts8 = ndpts;
165  int i;
166 
167  LOG((2, "g2c_jpcunpackf len %d ndpts %lld", len, ndpts));
168 
169  for (i = 0; i < G2C_JPEG_DRS_TEMPLATE_LEN; i++)
170  idrstmpl8[i] = idrstmpl[i];
171 
172  return jpcunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 0, 0);
173 }
174 
199 int
200 g2c_jpcunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts,
201  double *fld)
202 {
203  g2int idrstmpl8[G2C_JPEG_DRS_TEMPLATE_LEN];
204  g2int len8 = len, ndpts8 = ndpts;
205  int i;
206 
207  LOG((2, "g2c_jpcunpackd len %lld ndpts %lld", len, ndpts));
208 
209  for (i = 0; i < G2C_JPEG_DRS_TEMPLATE_LEN; i++)
210  idrstmpl8[i] = idrstmpl[i];
211 
212  return jpcunpack_int(cpack, len8, idrstmpl8, ndpts8, fld, 1, 0);
213 }
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:201
#define G2C_JPEG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for JPEG packing.
Definition: grib2.h:420
#define G2C_ENOMEM
Out of memory.
Definition: grib2.h:500
#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
#define G2C_NOERROR
No error.
Definition: grib2.h:491
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 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:124
int g2c_jpcunpackd(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, double *fld)
Unpack JPEG2000 compressed data into an array of doubles, using info from the GRIB2 Data Representati...
Definition: jpcunpack.c:200
int g2c_jpcunpackf(unsigned char *cpack, size_t len, int *idrstmpl, size_t ndpts, float *fld)
Unpack JPEG2000 compressed data into an array of floats, using info from the GRIB2 Data Representatio...
Definition: jpcunpack.c:160
static int jpcunpack_int(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts, void *fld, int fld_is_double, int verbose)
This internal function will unpack JPEG2000 compressed data into an array of floats or doubles,...
Definition: jpcunpack.c:45