sz_compat.c 3.31 KB
Newer Older
1
#include <stdio.h>
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
2
#include <stddef.h>
3 4
#include <string.h>
#include <stdlib.h>
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
5
#include "szlib.h"
6
#include "libaec.h"
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#define NOPTS 129

static int convert_options(int sz_opts)
{
    int co[NOPTS];
    int i;
    int opts = 0;

    memset(co, 0, sizeof(int) * NOPTS);
    co[SZ_MSB_OPTION_MASK] = AEC_DATA_MSB;
    co[SZ_NN_OPTION_MASK] = AEC_DATA_PREPROCESS;

    for (i = 1; i < NOPTS; i <<= 1)
        opts |= co[i];

    return opts;
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
static void interleave_buffer(unsigned char *dest, unsigned char *src,
                              size_t n, int wordsize)
{
    size_t i, j;

    for (i = 0; i < n / wordsize; i++)
        for (j = 0; j < wordsize; j++)
            dest[j * (n / wordsize) + i] = src[i * wordsize + j];
}

static void deinterleave_buffer(unsigned char *dest, unsigned char *src,
                              size_t n, int wordsize)
{
    size_t i, j;

    for (i = 0; i < n / wordsize; i++)
        for (j = 0; j < wordsize; j++)
            dest[i * wordsize + j] = src[j * (n / wordsize) + i];
}

46 47 48
int SZ_BufftoBuffCompress(void *dest, size_t *destLen,
                          const void *source, size_t sourceLen,
                          SZ_com_t *param)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
49 50
{
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
51
    struct aec_stream strm;
52 53 54 55 56 57 58 59 60 61 62 63 64 65
    unsigned char *buf;

    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
        buf = (unsigned char *)malloc(sourceLen);
        if (buf == NULL)
            return SZ_MEM_ERROR;

        interleave_buffer(buf, source, sourceLen, param->bits_per_pixel / 8);
        strm.bit_per_sample = 8;
        strm.next_in = buf;
    } else {
        strm.next_in = source;
        strm.bit_per_sample = param->bits_per_pixel;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
66

67
    strm.avail_in = sourceLen;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
68
    strm.block_size = param->pixels_per_block;
69
    strm.rsi = param->pixels_per_scanline / param->pixels_per_block;
70
    strm.flags = convert_options(param->options_mask);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
71 72 73
    strm.avail_out = *destLen;
    strm.next_out = dest;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
74
    status = aec_buffer_encode(&strm);
75
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
76 77
        return status;

78 79 80 81
    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
        free(buf);
    }

82
    *destLen = strm.total_out;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
83 84 85
    return SZ_OK;
}

86 87 88
int SZ_BufftoBuffDecompress(void *dest, size_t *destLen,
                            const void *source, size_t sourceLen,
                            SZ_com_t *param)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
89 90
{
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
91
    struct aec_stream strm;
92 93 94 95 96 97 98 99 100 101 102 103 104
    unsigned char *buf;

    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
        buf = (unsigned char *)malloc(*destLen);
        if (buf == NULL)
            return SZ_MEM_ERROR;

        strm.bit_per_sample = 8;
        strm.next_out = buf;
    } else {
        strm.next_out = dest;
        strm.bit_per_sample = param->bits_per_pixel;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
105 106

    strm.block_size = param->pixels_per_block;
107
    strm.rsi = param->pixels_per_scanline / param->pixels_per_block;
108
    strm.flags = convert_options(param->options_mask);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
109 110 111 112
    strm.avail_in = sourceLen;
    strm.avail_out = *destLen;
    strm.next_in = source;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
113
    status = aec_buffer_decode(&strm);
114
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
115 116
        return status;

117
    *destLen = strm.total_out;
118 119 120 121 122 123

    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
        free(buf);
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
124 125
    return SZ_OK;
}
126 127 128 129 130

int SZ_encoder_enabled(void)
{
    return 1;
}