Debian Codegraph @

frmts/grib/degrib/g2clib/jpcpack.c

raw
1 #include <stdlib.h>
2 #include <math.h>
3 #include "grib2.h"
4
5 void jpcpack(g2float *fld,g2int width,g2int height,g2int *idrstmpl,
6 unsigned char *cpack,g2int *lcpack)
7 //$$$ SUBPROGRAM DOCUMENTATION BLOCK
8 // . . . .
9 // SUBPROGRAM: jpcpack
10 // PRGMMR: Gilbert ORG: W/NP11 DATE: 2003-08-17
11 //
12 // ABSTRACT: This subroutine packs up a data field into a JPEG2000 code stream.
13 // After the data field is scaled, and the reference value is subtracted out,
14 // it is treated as a grayscale image and passed to a JPEG2000 encoder.
15 // It also fills in GRIB2 Data Representation Template 5.40 or 5.40000 with
16 // the appropriate values.
17 //
18 // PROGRAM HISTORY LOG:
19 // 2003-08-17 Gilbert
20 // 2004-11-92 Gilbert - Fixed bug encountered when packing a near constant
21 // field.
22 // 2004-07-19 Gilbert - Added check on whether the jpeg2000 encoding was
23 // successful. If not, try again with different encoder
24 // options.
25 // 2005-05-10 Gilbert - Imposed minimum size on cpack, used to hold encoded
26 // bit string.
27 //
28 // USAGE: jpcpack(g2float *fld,g2int width,g2int height,g2int *idrstmpl,
29 // unsigned char *cpack,g2int *lcpack);
30 // INPUT ARGUMENT LIST:
31 // fld[] - Contains the data values to pack
32 // width - number of points in the x direction
33 // height - number of points in the y direction
34 // idrstmpl - Contains the array of values for Data Representation
35 // Template 5.40 or 5.40000
36 // [0] = Reference value - ignored on input
37 // [1] = Binary Scale Factor
38 // [2] = Decimal Scale Factor
39 // [3] = number of bits for each data value - ignored on input
40 // [4] = Original field type - currently ignored on input
41 // Data values assumed to be reals.
42 // [5] = 0 - use lossless compression
43 // = 1 - use lossy compression
44 // [6] = Desired compression ratio, if idrstmpl[5]=1.
45 // Set to 255, if idrstmpl[5]=0.
46 // lcpack - size of array cpack[]
47 //
48 // OUTPUT ARGUMENT LIST:
49 // idrstmpl - Contains the array of values for Data Representation
50 // Template 5.0
51 // [0] = Reference value - set by jpcpack routine.
52 // [1] = Binary Scale Factor - unchanged from input
53 // [2] = Decimal Scale Factor - unchanged from input
54 // [3] = Number of bits containing each grayscale pixel value
55 // [4] = Original field type - currently set = 0 on output.
56 // Data values assumed to be reals.
57 // [5] = 0 - use lossless compression
58 // = 1 - use lossy compression
59 // [6] = Desired compression ratio, if idrstmpl[5]=1
60 // cpack - The packed data field
61 // lcpack - length of packed field in cpack.
62 //
63 // REMARKS: None
64 //
65 // ATTRIBUTES:
66 // LANGUAGE: C
67 // MACHINE: IBM SP
68 //
69 //$$$
70 {
71 g2int *ifld;
72 const g2float alog2=0.69314718f; // ln(2.0)
73 g2int j,nbits,imin,imax,maxdif;
74 g2int ndpts,nbytes,nsize,retry;
75 g2float bscale,dscale,rmax,rmin,temp;
76 unsigned char *ctemp;
77
78 ifld=0;
79 ndpts=width*height;
80 bscale=(float)int_power(2.0,-idrstmpl[1]);
81 dscale=(float)int_power(10.0,idrstmpl[2]);
82 //
83 // Find max and min values in the data
84 //
85 rmax=fld[0];
86 rmin=fld[0];
87 for (j=1;j<ndpts;j++) {
88 if (fld[j] > rmax) rmax=fld[j];
89 if (fld[j] < rmin) rmin=fld[j];
90 }
91 if (idrstmpl[1] == 0)
92 maxdif = (g2int) (RINT(rmax*dscale) - RINT(rmin*dscale));
93 else
94 maxdif = (g2int)RINT( (rmax-rmin)*dscale*bscale );
95 //
96 // If max and min values are not equal, pack up field.
97 // If they are equal, we have a constant field, and the reference
98 // value (rmin) is the value for each point in the field and
99 // set nbits to 0.
100 //
101 if ( rmin != rmax && maxdif != 0 ) {
102 ifld=(g2int *)malloc(ndpts*sizeof(g2int));
103 //
104 // Determine which algorithm to use based on user-supplied
105 // binary scale factor and number of bits.
106 //
107 if (idrstmpl[1] == 0) {
108 //
109 // No binary scaling and calculate minimum number of
110 // bits in which the data will fit.
111 //
112 imin=(g2int)RINT(rmin*dscale);
113 imax=(g2int)RINT(rmax*dscale);
114 maxdif=imax-imin;
115 temp=(float)(log((double)(maxdif+1))/alog2);
116 nbits=(g2int)ceil(temp);
117 rmin=(g2float)imin;
118 // scale data
119 for(j=0;j<ndpts;j++)
120 ifld[j]=(g2int)RINT(fld[j]*dscale)-imin;
121 }
122 else {
123 //
124 // Use binary scaling factor and calculate minimum number of
125 // bits in which the data will fit.
126 //
127 rmin=rmin*dscale;
128 rmax=rmax*dscale;
129 maxdif=(g2int)RINT((rmax-rmin)*bscale);
130 temp=(float)(log((double)(maxdif+1))/alog2);
131 nbits=(g2int)ceil(temp);
132 // scale data
133 for (j=0;j<ndpts;j++)
134 ifld[j]=(g2int)RINT(((fld[j]*dscale)-rmin)*bscale);
135 }
136 //
137 // Pack data into full octets, then do JPEG 2000 encode.
138 // and calculate the length of the packed data in bytes
139 //
140 retry=0;
141 nbytes=(nbits+7)/8;
142 nsize=*lcpack; // needed for input to enc_jpeg2000
143 ctemp=calloc(ndpts,nbytes);
144 sbits(ctemp,ifld,0,nbytes*8,0,ndpts);
145 *lcpack=(g2int)enc_jpeg2000(ctemp,width,height,nbits,idrstmpl[5],idrstmpl[6],retry,(char *)cpack,nsize);
146 if (*lcpack <= 0) {
147 printf("jpcpack: ERROR Packing JPC = %d\n",(int)*lcpack);
148 if ( *lcpack == -3 ) {
149 retry=1;
150 *lcpack=(g2int)enc_jpeg2000(ctemp,width,height,nbits,idrstmpl[5],idrstmpl[6],retry,(char *)cpack,nsize);
151 if ( *lcpack <= 0 ) printf("jpcpack: Retry Failed.\n");
152 else printf("jpcpack: Retry Successful.\n");
153 }
154 }
155 free(ctemp);
156
157 }
158 else {
159 /* Force E and D to 0 to avoid compatibility issues */
160 idrstmpl[1]=0;
161 idrstmpl[2]=0;
162 if( dscale != 1.0 )
163 {
164 rmin = (float)floor(rmin * dscale) / dscale;
165 }
166 nbits=0;
167 *lcpack=0;
168 }
169
170 //
171 // Fill in ref value and number of bits in Template 5.0
172 //
173 mkieee(&rmin,idrstmpl+0,1); // ensure reference value is IEEE format
174 idrstmpl[3]=nbits;
175 idrstmpl[4]=0; // original data were reals
176 if (idrstmpl[5] == 0) idrstmpl[6]=255; // lossy not used
177 if (ifld != 0) free(ifld);
178
179 }