54 unsigned char *cpack,
g2int *lcpack,
int verbose)
60 g2int maxdif, nbits, ndpts, nbytes;
61 g2int ccsds_flags, ccsds_block_size, ccsds_rsi;
62 float bscale, dscale, rmax, rmin, temp;
64 static float alog2 =
ALOG2;
70 LOG((2,
"aecpack_int() fld_is_double %d width %ld height %ld idrstmpl[1] %d *lcpack %ld",
71 fld_is_double, width, height, idrstmpl[1], *lcpack));
72 LOG((3,
"idrstmpl: %ld %ld %ld %ld %ld %ld %ld %ld", idrstmpl[0], idrstmpl[1], idrstmpl[2],
73 idrstmpl[3], idrstmpl[4], idrstmpl[5], idrstmpl[6], idrstmpl[7]));
81 ndpts = width * height;
84 LOG((3,
"ndpts %ld bscale %g dscale %g", ndpts, bscale, dscale));
93 for (j = 1; j < ndpts; j++)
100 if (idrstmpl[1] == 0)
101 maxdif = (
g2int)(rint(rmaxd * dscale) - rint(rmind * dscale));
103 maxdif = (
g2int)rint((rmaxd - rmind) * dscale * bscale);
107 for (j = 1; j < ndpts; j++)
114 if (idrstmpl[1] == 0)
115 maxdif = (
g2int)(rint(rmax * dscale) - rint(rmin * dscale));
117 maxdif = (
g2int)rint((rmax - rmin) * dscale * bscale);
119 LOG((3,
"rmax %g rmaxd %g rmin %g rmind %g", rmax, rmaxd, rmin, rmind));
124 if (((fld_is_double && rmind != rmaxd) || (!fld_is_double && rmin != rmax)) && maxdif != 0)
127 ifld = malloc(ndpts *
sizeof(
g2int));
131 if (idrstmpl[1] == 0)
135 imin = (
g2int)rint((fld_is_double ? rmind : rmin) * dscale);
136 imax = (
g2int)rint((fld_is_double ? rmaxd : rmax) * dscale);
137 maxdif = imax - imin;
138 temp = log((
double)(maxdif + 1)) / alog2;
139 nbits = (
g2int)ceil(temp);
144 for(j = 0; j < ndpts; j++)
145 ifld[j] = (
g2int)rint(dfld[j] * dscale) - imin;
150 for(j = 0; j < ndpts; j++)
151 ifld[j] = (
g2int)rint(ffld[j] * dscale) - imin;
160 rmind = rmind * dscale;
161 rmaxd = rmaxd * dscale;
162 maxdif = (
g2int)rint((rmaxd - rmind) * bscale);
166 rmin = rmin * dscale;
167 rmax = rmax * dscale;
168 maxdif = (
g2int)rint((rmax - rmin) * bscale);
170 temp = log((
double)(maxdif + 1)) / alog2;
171 nbits = (
g2int)ceil(temp);
175 for (j = 0; j < ndpts; j++)
176 ifld[j] = (
g2int)rint(((dfld[j] * dscale) - rmind) * bscale);
180 for (j = 0; j < ndpts; j++)
181 ifld[j] = (
g2int)rint(((ffld[j] * dscale) - rmin) * bscale);
186 if (idrstmpl[3] <= 0)
188 nbits = pow(2, ceil(log(nbits)/log(2)));
193 nbits = pow(2, ceil(log(nbits)/log(2)));
195 nbits = nbits < 8 ? 8 : nbits;
197 if (idrstmpl[5] == 0)
199 ccsds_flags = AEC_DATA_SIGNED | AEC_DATA_PREPROCESS | AEC_DATA_MSB;
203 ccsds_flags = idrstmpl[5];
205 if (idrstmpl[6] == 0)
207 ccsds_block_size = 16;
211 ccsds_block_size = idrstmpl[6];
213 if (idrstmpl[7] == 0)
219 ccsds_rsi = idrstmpl[7];
224 nbytes = (nbits + 7) / 8;
225 ctemp = calloc(ndpts, nbytes);
226 ctemplen = ndpts*nbytes;
227 sbits(ctemp, ifld, 0, nbytes*8, 0, ndpts);
229 ret =
enc_aec(ctemp, ctemplen, nbits, ccsds_flags, ccsds_block_size, ccsds_rsi, cpack, lcpack);
232 if (verbose) printf(
"aecpack: ERROR Packing AEC = %d\n",ret);
249 mkieee(&rmin, idrstmpl, 1);
251 idrstmpl[5] = ccsds_flags;
252 idrstmpl[6] = ccsds_block_size;
253 idrstmpl[7] = ccsds_rsi;
298 unsigned char *cpack,
g2int *lcpack)
300 aecpack_int(fld, 0, width, height, idrstmpl, cpack, lcpack, 1);
347 unsigned char *cpack,
size_t *lcpack)
349 g2int width8 = width, height8 = height, lcpack8 = *lcpack;
354 idrstmpl8[i] = idrstmpl[i];
356 ret =
aecpack_int(fld, 0, width8, height8, idrstmpl8, cpack, &lcpack8, 0);
361 idrstmpl[i] = (
int)idrstmpl8[i];
362 *lcpack = (
g2int)lcpack8;
411 unsigned char *cpack,
size_t *lcpack)
413 g2int width8 = width, height8 = height, lcpack8 = *lcpack;
418 idrstmpl8[i] = idrstmpl[i];
420 ret =
aecpack_int(fld, 1, width8, height8, idrstmpl8, cpack, &lcpack8, 0);
425 idrstmpl[i] = (
int)idrstmpl8[i];
426 *lcpack = (
g2int)lcpack8;
int g2c_aecpackf(float *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, size_t *lcpack)
This function packs up a float array into a AEC code stream.
static int aecpack_int(void *fld, int fld_is_double, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack, int verbose)
This internal function packs up a float or double array into a AEC/CCSDS code stream.
int g2c_aecpackd(double *fld, size_t width, size_t height, int *idrstmpl, unsigned char *cpack, size_t *lcpack)
This function packs up a double array into a AEC code stream.
void aecpack(float *fld, g2int width, g2int height, g2int *idrstmpl, unsigned char *cpack, g2int *lcpack)
This function packs up a float array into a AEC code stream.
int enc_aec(unsigned char *data, g2int ctemplen, g2int nbits, g2int flags, g2int block_size, g2int rsi, unsigned char *aecbuf, g2int *aecbuflen)
This Function encodes data into an AEC code stream specified in the CCSDS 121.0-B-3 Blue Book.
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits, g2int nskip, g2int n)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
#define G2C_AEC_DRS_TEMPLATE_LEN
Length of the idrstmpl array for AEC 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)
This subroutine stores 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.