NCEPLIBS-g2c  1.6.4
jpcpack.c
Go to the documentation of this file.
1 
5 #include <stdlib.h>
6 #include <math.h>
7 #include "grib2.h"
8 
9 int enc_jpeg2000(unsigned char *, g2int, g2int, g2int,
10  g2int, g2int, g2int, char *, g2int);
11 
52 void
53 jpcpack(g2float *fld, g2int width, g2int height, g2int *idrstmpl,
54  unsigned char *cpack, g2int *lcpack)
55 {
56  g2int *ifld;
57  static g2float alog2 = 0.69314718; /* ln(2.0) */
58  g2int j, nbits, imin, imax, maxdif;
59  g2int ndpts, nbytes, nsize, retry;
60  g2float bscale, dscale, rmax, rmin, temp;
61  unsigned char *ctemp;
62 
63  ifld = 0;
64  ndpts = width * height;
65  bscale = int_power(2.0, -idrstmpl[1]);
66  dscale = int_power(10.0, idrstmpl[2]);
67 
68  /* Find max and min values in the data. */
69  rmax = fld[0];
70  rmin = fld[0];
71  for (j = 1; j < ndpts; j++)
72  {
73  if (fld[j] > rmax)
74  rmax = fld[j];
75  if (fld[j] < rmin)
76  rmin = fld[j];
77  }
78  if (idrstmpl[1] == 0)
79  maxdif = (g2int)(rint(rmax * dscale) - rint(rmin * dscale));
80  else
81  maxdif = (g2int)rint((rmax - rmin) * dscale * bscale);
82 
83  /* If max and min values are not equal, pack up field. If they are
84  * equal, we have a constant field, and the reference value (rmin)
85  * is the value for each point in the field and set nbits to 0. */
86  if (rmin != rmax && maxdif != 0)
87  {
88  ifld = malloc(ndpts * sizeof(g2int));
89 
90  /* Determine which algorithm to use based on user-supplied
91  * binary scale factor and number of bits. */
92  if (idrstmpl[1] == 0)
93  {
94  /* No binary scaling and calculate minumum number of bits
95  * in which the data will fit. */
96  imin = (g2int)rint(rmin * dscale);
97  imax = (g2int)rint(rmax * dscale);
98  maxdif = imax - imin;
99  temp = log((double)(maxdif + 1)) / alog2;
100  nbits = (g2int)ceil(temp);
101  rmin = (g2float)imin;
102  /* scale data */
103  for(j = 0; j < ndpts; j++)
104  ifld[j] = (g2int)rint(fld[j] * dscale) - imin;
105  }
106  else
107  {
108  /* Use binary scaling factor and calculate minumum number
109  * of bits in which the data will fit. */
110  rmin = rmin * dscale;
111  rmax = rmax * dscale;
112  maxdif = (g2int)rint((rmax - rmin) * bscale);
113  temp = log((double)(maxdif + 1)) / alog2;
114  nbits = (g2int)ceil(temp);
115  /* scale data */
116  for (j = 0; j < ndpts; j++)
117  ifld[j] = (g2int)rint(((fld[j] * dscale) - rmin) * bscale);
118  }
119 
120  /* Pack data into full octets, then do JPEG 2000 encode and
121  * calculate the length of the packed data in bytes. */
122  retry = 0;
123  nbytes = (nbits + 7) / 8;
124  nsize = *lcpack; /* needed for input to enc_jpeg2000 */
125  ctemp = calloc(ndpts, nbytes);
126  sbits(ctemp, ifld, 0, nbytes * 8, 0, ndpts);
127  if ((*lcpack = (g2int)enc_jpeg2000(ctemp, width, height, nbits, idrstmpl[5],
128  idrstmpl[6], retry, (char *)cpack, nsize)) <= 0)
129  {
130  printf("jpcpack: ERROR Packing JPC = %d\n", (int)*lcpack);
131  if (*lcpack == -3)
132  {
133  retry = 1;
134  if ((*lcpack = (g2int)enc_jpeg2000(ctemp, width, height, nbits, idrstmpl[5],
135  idrstmpl[6], retry, (char *)cpack, nsize)) <= 0)
136  printf("jpcpack: Retry Failed.\n");
137  else
138  printf("jpcpack: Retry Successful.\n");
139  }
140  }
141  free(ctemp);
142  }
143  else
144  {
145  nbits = 0;
146  *lcpack = 0;
147  }
148 
149  /* Fill in ref value and number of bits in Template 5.0. */
150  mkieee(&rmin, idrstmpl, 1); /* ensure reference value is IEEE format. */
151  idrstmpl[3] = nbits;
152  idrstmpl[4] = 0; /* original data were reals */
153  if (idrstmpl[5] == 0)
154  idrstmpl[6] = 255; /* lossy not used */
155  if (ifld)
156  free(ifld);
157 }
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbyte, g2int nskip, g2int n)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
Definition: gbits.c:114
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 enc_jpeg2000(unsigned char *, g2int, g2int, g2int, g2int, g2int, g2int, char *, g2int)
This Function encodes a grayscale image into a JPEG2000 code stream specified in the JPEG2000 Part-1 ...
Definition: enc_jpeg2000.c:57
void jpcpack(g2float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
This subroutine packs up a data field into a JPEG2000 code stream.
Definition: jpcpack.c:53
void mkieee(g2float *a, g2int *rieee, g2int num)
This subroutine stores a list of real values in 32-bit IEEE floating point format.
Definition: mkieee.c:21