Commit 7b90370f authored by Mathis Rosenhauer's avatar Mathis Rosenhauer

imitate szlib for gribex

parent 053ffc57
CC = gcc
#CFLAGS = -g -Wall -DPROFILE
CFLAGS = -g -Wall
OBJS = aee.o aed.o
OBJS = aee.o aed.o sz_compat.o
.PHONY : all clean test
.PHONY : all clean test test_sz
all: libae.a test
all: libae.a
test_encode: test_encode.o libae.a
$(CC) $(CFLAGS) -o test_encode test_encode.o -L. -lae
......@@ -13,6 +14,9 @@ test_encode: test_encode.o libae.a
test_decode: test_decode.o libae.a
$(CC) $(CFLAGS) -o test_decode test_decode.o -L. -lae
test_szcomp: test_szcomp.o libae.a
$(CC) $(CFLAGS) -o test_szcomp test_szcomp.o -L. -lae
libae.a: $(OBJS)
$(AR) $(ARFLAGS) $@ $(OBJS)
-@ ($(RANLIB) $@ || true) >/dev/null 2>&1
......@@ -20,9 +24,15 @@ libae.a: $(OBJS)
aed.o: libae.h
aee.o: libae.h
install: libae.a
ln -sfT ../src/szlib.h ../include/szlib.h
ln -sfT ../src/libae.h ../include/libae.h
ln -sfT ../src/libae.a ../lib/libsz.a
clean:
rm -f $(OBJS) test_encode.o test_decode.o \
test_encode test_decode libae.a \
test_szcomp test_szcomp.o \
../data/test.ae ../data/test
test: test_encode test_decode
......@@ -32,3 +42,8 @@ test: test_encode test_decode
./test_encode 99 99 < ../data/example_data > ../data/test.ae
./test_decode 101 101 < ../data/test.ae > ../data/test
diff ../data/test ../data/example_data
test_sz: test_szcomp
./test_szcomp 65536 < ../data/example_data_16 > ../data/test
diff ../data/test ../data/example_data_16
......@@ -19,7 +19,7 @@ typedef struct internal_state {
int id_len; /* bit length of code option identification key */
int *id_table; /* table maps IDs to states */
void (*put_sample)(ae_streamp, int64_t);
size_t ref_int; /* reference sample is every ref_int samples */
int ref_int; /* reference sample is every ref_int samples */
int64_t last_out; /* previous output for post-processing */
int64_t xmin; /* minimum integer for post-processing */
int64_t xmax; /* maximum integer for post-processing */
......@@ -34,6 +34,7 @@ typedef struct internal_state {
int fs; /* last fundamental sequence in accumulator */
int ref; /* 1 if current block has reference sample */
int pp; /* 1 if postprocessor has to be used */
size_t samples_out;
} decode_state;
/* decoding table for the second-extension option */
......@@ -64,20 +65,48 @@ enum
M_UNCOMP_COPY,
};
#define PUTF(type) static void put_##type(ae_streamp strm, int64_t data) \
{ \
strm->avail_out--; \
strm->total_out++; \
*(type##_t *)strm->next_out = data; \
strm->next_out += sizeof(type##_t); \
}
static void put_msb_32(ae_streamp strm, int64_t data)
{
*strm->next_out++ = data >> 24;
*strm->next_out++ = data >> 16;
*strm->next_out++ = data >> 8;
*strm->next_out++ = data;
strm->avail_out -= 4;
strm->total_out += 4;
}
static void put_msb_16(ae_streamp strm, int64_t data)
{
*strm->next_out++ = data >> 8;
*strm->next_out++ = data;
strm->avail_out -= 2;
strm->total_out += 2;
}
static void put_lsb_32(ae_streamp strm, int64_t data)
{
*strm->next_out++ = data;
*strm->next_out++ = data >> 8;
*strm->next_out++ = data >> 16;
*strm->next_out++ = data >> 24;
strm->avail_out -= 4;
strm->total_out += 4;
}
PUTF(uint8)
PUTF(int8)
PUTF(uint16)
PUTF(int16)
PUTF(uint32)
PUTF(int32)
static void put_lsb_16(ae_streamp strm, int64_t data)
{
*strm->next_out++ = data;
*strm->next_out++ = data >> 8;
strm->avail_out -= 2;
strm->total_out += 2;
}
static void put_8(ae_streamp strm, int64_t data)
{
*strm->next_out++ = data;
strm->avail_out--;
strm->total_out++;
}
static inline void u_put(ae_streamp strm, int64_t sample)
{
......@@ -85,7 +114,7 @@ static inline void u_put(ae_streamp strm, int64_t sample)
decode_state *state;
state = strm->state;
if (state->pp && (strm->total_out % state->ref_int != 0))
if (state->pp && (state->samples_out % state->ref_int != 0))
{
d = sample;
x = state->last_out;
......@@ -115,6 +144,7 @@ static inline void u_put(ae_streamp strm, int64_t sample)
}
state->last_out = sample;
state->put_sample(strm, sample);
state->samples_out++;
}
static inline int64_t u_get(ae_streamp strm, unsigned int n)
......@@ -243,26 +273,24 @@ int ae_decode_init(ae_streamp strm)
if (strm->bit_per_sample > 16)
{
state->id_len = 5;
if (strm->flags & AE_DATA_SIGNED)
state->put_sample = put_int32;
if (strm->flags & AE_DATA_MSB)
state->put_sample = put_msb_32;
else
state->put_sample = put_uint32;
state->put_sample = put_lsb_32;
}
else if (strm->bit_per_sample > 8)
{
state->id_len = 4;
if (strm->flags & AE_DATA_SIGNED)
state->put_sample = put_int16;
if (strm->flags & AE_DATA_MSB)
state->put_sample = put_msb_16;
else
state->put_sample = put_uint16;
state->put_sample = put_lsb_16;
}
else
{
state->id_len = 3;
if (strm->flags & AE_DATA_SIGNED)
state->put_sample = put_int8;
else
state->put_sample = put_uint8;
state->put_sample = put_8;
}
if (strm->flags & AE_DATA_SIGNED)
......@@ -301,6 +329,7 @@ int ae_decode_init(ae_streamp strm)
strm->total_in = 0;
strm->total_out = 0;
state->samples_out = 0;
state->bitp = 0;
state->pp = strm->flags & AE_DATA_PREPROCESS;
state->mode = M_ID;
......@@ -365,7 +394,7 @@ int ae_decode(ae_streamp strm, int flush)
of the states are called. Inspired by zlib.
*/
size_t zero_blocks;
int zero_blocks;
int64_t gamma, beta, ms, delta1;
int k;
decode_state *state;
......@@ -378,7 +407,7 @@ int ae_decode(ae_streamp strm, int flush)
{
case M_ID:
if (state->pp
&& (strm->total_out / strm->block_size) % strm->segment_size == 0)
&& (state->samples_out / strm->block_size) % strm->segment_size == 0)
state->ref = 1;
else
state->ref = 0;
......@@ -461,7 +490,7 @@ int ae_decode(ae_streamp strm, int flush)
if (zero_blocks == ROS)
{
zero_blocks = strm->segment_size - (
(strm->total_out / strm->block_size)
(state->samples_out / strm->block_size)
% strm->segment_size);
}
......
This diff is collapsed.
......@@ -30,7 +30,9 @@ typedef ae_stream *ae_streamp;
/* Coder flags */
#define AE_DATA_UNSIGNED 0
#define AE_DATA_SIGNED 1
#define AE_DATA_PREPROCESS 2 /* Set if preprocessor should be used */
#define AE_DATA_LSB 8
#define AE_DATA_MSB 16
#define AE_DATA_PREPROCESS 32 /* Set if preprocessor should be used */
/* Return codes of library functions */
......
#include <stddef.h>
#include "szlib.h"
int SZ_BufftoBuffCompress(void *dest, size_t *destLen, const void *source, size_t sourceLen, SZ_com_t *param)
{
int status;
ae_stream strm;
strm.bit_per_sample = param->bits_per_pixel;
strm.block_size = param->pixels_per_block;
strm.segment_size = param->pixels_per_scanline / param->pixels_per_block;
strm.flags = param->options_mask;
strm.avail_in = sourceLen;
strm.avail_out = *destLen;
strm.next_out = dest;
strm.next_in = source;
if ((status = ae_encode_init(&strm)) != AE_OK)
return status;
if ((status = ae_encode(&strm, AE_FLUSH)) != AE_OK)
return status;
*destLen = strm.total_out;
return SZ_OK;
}
int SZ_BufftoBuffDecompress(void *dest, size_t *destLen, const void *source, size_t sourceLen, SZ_com_t *param)
{
int status;
ae_stream strm;
strm.bit_per_sample = param->bits_per_pixel;
strm.block_size = param->pixels_per_block;
strm.segment_size = param->pixels_per_scanline / param->pixels_per_block;
strm.flags = param->options_mask;
strm.avail_in = sourceLen;
strm.avail_out = *destLen;
strm.next_out = dest;
strm.next_in = source;
if ((status = ae_decode_init(&strm)) != AE_OK)
return status;
if ((status = ae_decode(&strm, AE_FLUSH)) != AE_OK)
return status;
*destLen = strm.total_out;
return SZ_OK;
}
#ifndef SZLIB_H
#define SZLIB_H
#include "libae.h"
#define SZ_OK AE_OK
#define SZ_NO_ENCODER_ERROR -1
#define SZ_PARAM_ERROR AE_ERRNO
#define SZ_MEM_ERROR AE_MEM_ERROR
#define SZ_OUTBUFF_FULL -2
#define SZ_RAW_OPTION_MASK 128
#define SZ_NN_OPTION_MASK AE_DATA_PREPROCESS
#define SZ_LSB_OPTION_MASK AE_DATA_LSB
#define SZ_MSB_OPTION_MASK AE_DATA_MSB
typedef struct SZ_com_t_s
{
int options_mask;
int bits_per_pixel;
int pixels_per_block;
int pixels_per_scanline;
} SZ_com_t;
int SZ_BufftoBuffCompress(void *dest, size_t *destLen, const void *source, size_t sourceLen, SZ_com_t *param);
int SZ_BufftoBuffDecompress(void *dest, size_t *destLen, const void *source, size_t sourceLen, SZ_com_t *param);
#endif /* SZLIB_H */
......@@ -11,7 +11,7 @@ int main(int argc, char *argv[])
{
ae_stream strm;
uint8_t *in;
uint32_t *out;
uint8_t *out;
int chunk_in, chunk_out, i, c, total_out, status;
int input_avail, output_avail;
......@@ -27,14 +27,14 @@ int main(int argc, char *argv[])
}
in = (uint8_t *)malloc(chunk_in);
out = (uint32_t *)malloc(chunk_out * sizeof(uint32_t));
out = (uint8_t *)malloc(chunk_out * sizeof(uint8_t));
if (in == NULL || out == NULL)
return 1;
strm.bit_per_sample = 17;
strm.bit_per_sample = 8;
strm.block_size = 8;
strm.segment_size = 2;
strm.flags = AE_DATA_SIGNED | AE_DATA_PREPROCESS;
strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
if (ae_decode_init(&strm) != AE_OK)
return 1;
......@@ -72,6 +72,9 @@ int main(int argc, char *argv[])
for (i=0; i < strm.total_out - total_out; i++)
{
putc(out[i], stdout);
/* putc(out[i] >> 8, stdout); */
/* putc(out[i] >> 16, stdout); */
/* putc(out[i] >> 24, stdout); */
}
total_out = strm.total_out;
output_avail = 1;
......
......@@ -11,7 +11,7 @@
int main(int argc, char *argv[])
{
ae_stream strm;
uint32_t *in;
uint8_t *in;
uint8_t *out;
int chunk_in, chunk_out, i, c, total_out, status;
int input_avail, output_avail;
......@@ -28,14 +28,14 @@ int main(int argc, char *argv[])
}
out = (uint8_t *)malloc(chunk_out);
in = (uint32_t *)malloc(chunk_in * sizeof(uint32_t));
in = (uint8_t *)malloc(chunk_in * sizeof(uint8_t));
if (in == NULL || out == NULL)
return 1;
strm.bit_per_sample = 17;
strm.bit_per_sample = 8;
strm.block_size = 8;
strm.segment_size = 2;
strm.flags = AE_DATA_SIGNED | AE_DATA_PREPROCESS;
strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
if (ae_encode_init(&strm) != AE_OK)
return 1;
......@@ -54,7 +54,13 @@ int main(int argc, char *argv[])
{
i = 0;
while(i < chunk_in && (c = getc(stdin)) != EOF)
in[i++] = c;
{
in[i] = c;
/* in[i] |= getc(stdin) << 8; */
/* in[i] |= getc(stdin) << 16; */
/* in[i] |= getc(stdin) << 24; */
i++;
}
strm.avail_in = i;
strm.next_in = (uint8_t *)in;
......
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include "szlib.h"
#define OPTIONS_MASK (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK)
#define PIXELS_PER_BLOCK (8)
#define PIXELS_PER_SCANLINE (PIXELS_PER_BLOCK*128)
int main(int argc, char *argv[])
{
int status, c;
SZ_com_t sz_param;
unsigned char *dest;
uint16_t *source;
size_t destLen, sourceLen, n;
if (argc < 2)
{
fprintf(stderr, "Input size missing!\n");
return 1;
}
sz_param.options_mask = OPTIONS_MASK;
sz_param.bits_per_pixel = 16;
sz_param.pixels_per_block = PIXELS_PER_BLOCK;
sz_param.pixels_per_scanline = PIXELS_PER_SCANLINE;
sourceLen = destLen = atoi(argv[1]);
source = (uint16_t *)malloc(sourceLen * sizeof(uint16_t));
dest = (unsigned char *)malloc(destLen);
if (source == NULL || dest == NULL)
return 1;
n = 0;
while((c = getc(stdin)) != EOF)
{
source[n] = c;
source[n] |= getc(stdin) << 8;
n++;
}
sourceLen = n * sizeof(uint16_t);
fprintf(stderr, "Uncompressed size is %li\n", sourceLen);
status = SZ_BufftoBuffCompress(dest, &destLen, source, sourceLen, &sz_param);
if (status != SZ_OK)
return status;
fprintf(stderr, "Compressed size is %li\n", destLen);
status = SZ_BufftoBuffDecompress(source, &sourceLen, dest, destLen, &sz_param);
if (status != SZ_OK)
return status;
fprintf(stderr, "Uncompressed size is %li again\n", sourceLen);
for(c = 0; c < sourceLen / sizeof(uint16_t); c++)
{
putc(source[c], stdout);
putc(source[c] >> 8, stdout);
}
free(source);
free(dest);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment