Skip to content

Commit 76dcf08

Browse files
Merge pull request #605 from AlysonStahl-NOAA/as_aec
Adding g2c_enc_aec()/g2c_dec_aec() functions for int input/output
2 parents 68cc9fe + f987a8e commit 76dcf08

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/decenc_aec.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,76 @@ enc_aec(unsigned char *data, g2int ctemplen, g2int nbits, g2int flags,
148148

149149
return ret;
150150
}
151+
152+
/**
153+
* Decode an AEC code stream specified in the [CCSDS 121.0-B-3 Blue
154+
* Book](https://public.ccsds.org/Pubs/121x0b3.pdf).
155+
*
156+
* @param cpack Pointer to buffer that holds the input AEC code
157+
* stream.
158+
* @param len Length (in bytes) of the buffer that holds the input
159+
* AEC code stream.
160+
* @param nbits CCSDS bits per sample.
161+
* @param flags CCSDS compression options mask.
162+
* @param block_size CCSDS block size.
163+
* @param rsi CCSDS reference sample interval.
164+
* @param cfld Pointer to output buffer from the AEC decoder.
165+
* @param cfldlen length of output buffer.
166+
*
167+
* @return
168+
* - >0 Length of data from AEC decoder
169+
* - 0 Successful decode (AEC_OK)
170+
* - -1 AEC_CONF_ERROR
171+
* - -2 AEC_STREAM_ERROR
172+
* - -3 AEC_DATA_ERROR
173+
* - -4 AEC_MEM_ERROR
174+
* - -5 AEC_RSI_OFFSETS_ERROR
175+
*
176+
* @author Alyson Stahl @date 10/2025
177+
*/
178+
int
179+
g2c_dec_aec(unsigned char *cpack, int len, int nbits, int flags,
180+
int block_size, int rsi, unsigned char *cfld, int cfldlen)
181+
{
182+
g2int len8 = len, nbits8 = nbits, flags8 = flags;
183+
g2int block_size8 = block_size, rsi8 = rsi, cfldlen8 = cfldlen;
184+
185+
return dec_aec(cpack, len8, nbits8, flags8, block_size8, rsi8,
186+
cfld, cfldlen8);
187+
}
188+
189+
/**
190+
* Encode data into an AEC code stream specified in the
191+
* [CCSDS 121.0-B-3 Blue Book](https://public.ccsds.org/Pubs/121x0b3.pdf).
192+
*
193+
* @param data Pointer to buffer that holds the input data.
194+
* @param ctemplen Length (in bytes) of the buffer that holds
195+
* the input data..
196+
* @param nbits CCSDS bits per sample.
197+
* @param flags CCSDS compression options mask.
198+
* @param block_size CCSDS block size.
199+
* @param rsi CCSDS reference sample interval.
200+
* @param aecbuf Pointer to buffer holding the AEC encoded stream.
201+
* @param aecbuflen Length of AEC code stream.
202+
*
203+
* @return
204+
* - >0 Exact length of AEC encoded data.
205+
* - 0 Successful decode (AEC_OK)
206+
* - -1 AEC_CONF_ERROR
207+
* - -2 AEC_STREAM_ERROR
208+
* - -3 AEC_DATA_ERROR
209+
* - -4 AEC_MEM_ERROR
210+
* - -5 AEC_RSI_OFFSETS_ERROR
211+
*
212+
* @author Alyson Stahl @date 10/2025
213+
*/
214+
int
215+
g2c_enc_aec(unsigned char *data, int ctemplen, int nbits, int flags,
216+
int block_size, int rsi, unsigned char *aecbuf, int *aecbuflen)
217+
{
218+
g2int ctemplen8 = ctemplen, nbits8 = nbits, flags8 = flags;
219+
g2int block_size8 = block_size, rsi8 = rsi, aecbuflen8 = *aecbuflen;
220+
221+
return enc_aec(data, ctemplen8, nbits8, flags8, block_size8, rsi8,
222+
aecbuf, &aecbuflen8);
223+
}

src/grib2.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ int g2c_enc_png(unsigned char *data, int width, int height, int nbits,
384384
unsigned char *pngbuf);
385385
int g2c_dec_png(unsigned char *pngbuf, int *width, int *height,
386386
unsigned char *cout);
387+
int g2c_enc_aec(unsigned char *data, int ctemplen, int nbits, int flags,
388+
int block_size, int rsi, unsigned char *aecbuf, int *aecbuflen);
389+
int g2c_dec_aec(unsigned char *cpack, int len, int nbits, int flags,
390+
int block_size, int rsi, unsigned char *cfld, int cfldlen);
387391
int g2c_aecpackf(float *fld, size_t width, size_t height, int *idrstmpl,
388392
unsigned char *cpack, size_t *lcpack);
389393
int g2c_aecpackd(double *fld, size_t width, size_t height, int *idrstmpl,

tests/tst_aec.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,44 @@ main()
154154
}
155155
}
156156
printf("ok!\n");
157+
printf("Testing g2c_enc_aec()/g2c_dec_aec() call...");
158+
{
159+
unsigned char data[4] = {1, 2, 3, 4};
160+
unsigned char cout[200];
161+
int nbits = 32;
162+
int datalen = 4;
163+
int aeclen = 200;
164+
int ccsds_flags;
165+
int ccsds_block_size;
166+
int ccsds_rsi;
167+
unsigned char *databuf;
168+
unsigned char *aecbuf;
169+
170+
int i, ret;
171+
172+
databuf = data;
173+
aecbuf = cout;
174+
175+
ccsds_flags = CCSDS_FLAGS;
176+
ccsds_block_size = 16;
177+
ccsds_rsi = 128;
178+
179+
/* Encode some data. */
180+
ret = g2c_enc_aec(databuf, datalen, nbits, ccsds_flags, ccsds_block_size,
181+
ccsds_rsi, aecbuf, &aeclen);
182+
if (ret < 0)
183+
return G2C_ERROR;
184+
185+
/* Now decode it. */
186+
ret = g2c_dec_aec(aecbuf, aeclen, nbits, ccsds_flags, ccsds_block_size,
187+
ccsds_rsi, cout, datalen);
188+
if (ret < 0)
189+
return G2C_ERROR;
190+
191+
for (i = 0; i < datalen; i++)
192+
if (cout[i] != data[i])
193+
return G2C_ERROR;
194+
}
157195
printf("SUCCESS!\n");
158196
return 0;
159197
}

0 commit comments

Comments
 (0)