54 unsigned char *cpack,
g2int *lcpack,
int verbose)
57 static float alog2 =
ALOG2;
58 g2int j, nbits, imin, imax, maxdif;
60 float bscale, dscale, rmax, rmin, temp;
67 LOG((2,
"pngpack_int fld_is_double %d width %ld height %ld idrstmpl[1] %d",
68 fld_is_double, width, height, idrstmpl[1]));
70 ndpts = width * height;
73 LOG((3,
"ndpts %d bscale %g dscale %g", ndpts, bscale, dscale));
84 for (j = 1; j < ndpts; j++)
91 maxdif = (
g2int)rint((rmaxd - rmind) * dscale * bscale);
95 for (j = 1; j < ndpts; j++)
102 maxdif = (
g2int)rint((rmax - rmin) * dscale * bscale);
104 LOG((3,
"rmax %g rmaxd %g rmin %g rmind %g", rmax, rmaxd, rmin, rmind));
109 if (((fld_is_double && rmind != rmaxd) || (!fld_is_double && rmin != rmax)) && maxdif != 0)
111 ifld = malloc(ndpts *
sizeof(
g2int));
115 if (idrstmpl[1] == 0)
119 imin = (
g2int)rint((fld_is_double ? rmind : rmin) * dscale);
120 imax = (
g2int)rint((fld_is_double ? rmaxd : rmax) * dscale);
121 maxdif = imax - imin;
122 temp = log((
double)(maxdif + 1)) / alog2;
123 nbits = (
g2int)ceil(temp);
128 for (j = 0; j < ndpts; j++)
129 ifld[j] = (
g2int)rint(dfld[j] * dscale) - imin;
134 for (j = 0; j < ndpts; j++)
135 ifld[j] = (
g2int)rint(ffld[j] * dscale) - imin;
144 rmind = rmind * dscale;
145 rmaxd = rmaxd * dscale;
146 maxdif = (
g2int)rint((rmaxd - rmind) * bscale);
150 rmin = rmin * dscale;
151 rmax = rmax * dscale;
152 maxdif = (
g2int)rint((rmax - rmin) * bscale);
154 temp = log((
double)(maxdif + 1)) / alog2;
155 nbits = (
g2int)ceil(temp);
159 for (j = 0; j < ndpts; j++)
160 ifld[j] = (
g2int)rint(((dfld[j] * dscale) - rmind) * bscale);
164 for (j = 0; j < ndpts; j++)
165 ifld[j] = (
g2int)rint(((ffld[j] * dscale) - rmin) * bscale);
179 else if (nbits <= 16)
181 else if (nbits <= 24)
186 int bytes_per_row = (nbits * width) / 8;
187 if ((width * nbits) % 8 != 0)
191 nbytes = bytes_per_row * height;
192 ctemp = calloc(nbytes, 1);
193 for (j = 0; j < height; j++)
194 sbits(ctemp + (j * bytes_per_row), ifld + (j * width), 0, nbits, 0, width);
197 if ((*lcpack = (
g2int)
enc_png(ctemp, width, height, nbits, cpack)) <= 0)
200 printf(
"pngpack: ERROR Packing PNG = %d\n", (
int)*lcpack);
214 mkieee(&rmin, idrstmpl, 1);
255 unsigned char *cpack,
g2int *lcpack)
258 pngpack_int(fld, 0, width, height, idrstmpl, cpack, lcpack, 1);
295 unsigned char *cpack,
int *lcpack)
297 g2int width8 = width, height8 = height, lcpack8 = *lcpack;
302 idrstmpl8[i] = idrstmpl[i];
304 ret =
pngpack_int(fld, 0, width8, height8, idrstmpl8, cpack, &lcpack8, 0);
309 idrstmpl[i] = (
int)idrstmpl8[i];
310 *lcpack = (
g2int)lcpack8;
349 unsigned char *cpack,
int *lcpack)
351 g2int width8 = width, height8 = height, lcpack8 = *lcpack;
356 idrstmpl8[i] = idrstmpl[i];
358 ret =
pngpack_int(fld, 1, width8, height8, idrstmpl8, cpack, &lcpack8, 0);
363 idrstmpl[i] = (
int)idrstmpl8[i];
364 *lcpack = (
g2int)lcpack8;
int enc_png(unsigned char *data, g2int width, g2int height, g2int nbits, unsigned char *pngbuf)
Encode PNG.
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits, g2int nskip, g2int n)
Store arbitrary size values into a packed bit string, taking the low order bits from each value in th...
#define G2C_EPNG
Error encoding/decoding PNG data.
#define G2C_PNG_DRS_TEMPLATE_LEN
Length of the idrstmpl array for PNG packing.
int64_t g2int
Long integer type.
#define G2C_NOERROR
No error.
Header file with internal function prototypes NCEPLIBS-g2c library.
void mkieee(float *a, g2int *rieee, g2int num)
Store a list of real values in 32-bit IEEE floating point format.
double int_power(double x, g2int y)
Function similar to C pow() power function.
#define LOG(e)
Ignore logging to stdout.
void pngpack(float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
This subroutine packs up a float data field into PNG image format.
int g2c_pngpackd(double *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, int *lcpack)
This subroutine packs up a double data field into PNG image format.
int g2c_pngpackf(float *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, int *lcpack)
This subroutine packs up a float data field into PNG image format.
static int pngpack_int(void *fld, int fld_is_double, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack, int verbose)
Packs float or double data into PNG image format.