Debian Codegraph @

frmts/grib/degrib/g2clib/pngpack.c

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