sz_compat.c 3.48 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;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
26
static void interleave_buffer(void *dest, const void *src,
27 28 29
                              size_t n, int wordsize)
{
    size_t i, j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
30 31 32 33 34
    const unsigned char *src8;
    unsigned char *dest8;

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
35 36 37

    for (i = 0; i < n / wordsize; i++)
        for (j = 0; j < wordsize; j++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
38
            dest8[j * (n / wordsize) + i] = src8[i * wordsize + j];
39 40
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
41 42
static void deinterleave_buffer(void *dest, const void *src,
                                size_t n, int wordsize)
43 44
{
    size_t i, j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
45 46 47 48 49
    const unsigned char *src8;
    unsigned char *dest8;

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
50 51 52

    for (i = 0; i < n / wordsize; i++)
        for (j = 0; j < wordsize; j++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
53
            dest8[i * wordsize + j] = src8[j * (n / wordsize) + i];
54 55
}

56 57 58
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
59 60
{
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
61
    struct aec_stream strm;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
62
    void *buf;
63 64

    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
65
        buf = malloc(sourceLen);
66 67 68 69 70 71 72 73 74 75
        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
76

77
    strm.avail_in = sourceLen;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
78
    strm.block_size = param->pixels_per_block;
79
    strm.rsi = param->pixels_per_scanline / param->pixels_per_block;
80
    strm.flags = convert_options(param->options_mask);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
81 82 83
    strm.avail_out = *destLen;
    strm.next_out = dest;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
84
    status = aec_buffer_encode(&strm);
85
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
86 87
        return status;

88 89 90 91
    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
        free(buf);
    }

92
    *destLen = strm.total_out;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
93 94 95
    return SZ_OK;
}

96 97 98
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
99 100
{
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
101
    struct aec_stream strm;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
102
    void *buf;
103 104

    if (param->bits_per_pixel == 32 || param->bits_per_pixel == 64) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
105
        buf = malloc(*destLen);
106 107 108 109 110 111 112 113 114
        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
115 116

    strm.block_size = param->pixels_per_block;
117
    strm.rsi = param->pixels_per_scanline / param->pixels_per_block;
118
    strm.flags = convert_options(param->options_mask);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
119 120 121 122
    strm.avail_in = sourceLen;
    strm.avail_out = *destLen;
    strm.next_in = source;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
123
    status = aec_buffer_decode(&strm);
124
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
125 126
        return status;

127
    *destLen = strm.total_out;
128 129 130 131 132 133

    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
134 135
    return SZ_OK;
}
136 137 138 139 140

int SZ_encoder_enabled(void)
{
    return 1;
}