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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
6 7 8 9
#if HAVE_CONFIG_H
#  include <config.h>
#endif

10 11 12 13 14 15 16 17 18 19 20 21 22
#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)
23 24
        if (sz_opts & i)
            opts |= co[i];
25 26 27 28

    return opts;
}

29 30 31 32 33 34 35 36 37 38
static int bits_to_bytes(int bit_length)
{
    if (bit_length > 16)
        return 4;
    else if (bit_length > 8)
        return 2;
    else
        return 1;
}

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

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
48 49 50

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
54 55
static void deinterleave_buffer(void *dest, const void *src,
                                size_t n, int wordsize)
56 57
{
    size_t i, j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58 59 60 61 62
    const unsigned char *src8;
    unsigned char *dest8;

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
63 64 65

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
69
static void add_padding(void *dest, const void *src, size_t total,
70 71 72 73 74
                          size_t line_size, size_t padding_size,
                          int pixel_size, int pp)
{
    size_t i, j, k;
    const char *pixel;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
75
    const char zero_pixel[] = {0, 0, 0, 0};
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
76 77 78 79 80 81 82

    pixel = zero_pixel;
    j = 0;
    for (i = 0; i < total; i += line_size) {
        memcpy((char *)dest + j, (char *)src + i, line_size);
        j += line_size;
        if (pp)
83
            pixel = (char *)src + i + line_size - pixel_size;
84
        for (k = 0; k < padding_size; k += pixel_size)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
85 86
            memcpy((char *)dest + j + k, pixel, pixel_size);
        j += padding_size;
87 88 89
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
90 91 92
static void remove_padding(void *buf, size_t total,
                           size_t line_size, size_t padding_size,
                           int pixel_size)
93 94
{
    size_t i, j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
95
    size_t padded_line_size = line_size + padding_size;
96

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
97 98
    i = line_size;
    for (j = padded_line_size; j < total; j += padded_line_size) {
99
        memmove((char *)buf + i, (char *)buf + j, line_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
100
        i += line_size;
101 102 103
    }
}

104 105 106
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
107
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
108
    struct aec_stream strm;
109
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
110 111
    void *padbuf;
    void *buf;
112 113
    size_t padding_size;
    size_t scanlines;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
114
    size_t padbuf_size;
115 116 117 118 119 120 121 122 123 124
    int pixel_size;
    int pad_scanline;
    int interleave;

    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
    strm.flags = convert_options(param->options_mask);
    strm.avail_out = *destLen;
    strm.next_out = dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
125 126
    buf = 0;
    padbuf = 0;
127

128
    interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
129
    pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
130
    if (interleave) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
131
        strm.bits_per_sample = 8;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
132
        buf = malloc(sourceLen);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
133 134 135 136
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
137 138
        interleave_buffer(buf, source, sourceLen, param->bits_per_pixel / 8);
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
139
        strm.bits_per_sample = param->bits_per_pixel;
140
        buf = (void *)source;
141
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
142

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
143
    pixel_size = bits_to_bytes(strm.bits_per_sample);
144 145

    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
146 147 148 149 150 151 152
        scanlines = sourceLen / param->pixels_per_scanline;
        padbuf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
        padbuf = malloc(padbuf_size);
        if (padbuf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
153

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
154 155 156
        padding_size =
            (strm.rsi * strm.block_size - param->pixels_per_scanline)
            * pixel_size;
157

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
158 159 160 161
        add_padding(padbuf, buf, sourceLen,
                    param->pixels_per_scanline * pixel_size,
                    padding_size, pixel_size,
                    strm.flags & AEC_DATA_PREPROCESS);
162
        strm.next_in = padbuf;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
163
        strm.avail_in = padbuf_size;
164 165 166 167
    } else {
        strm.next_in = buf;
        strm.avail_in = sourceLen;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
168

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
169
    status = aec_buffer_encode(&strm);
170 171
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
172
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
173
    if (pad_scanline && padbuf)
174
        free(padbuf);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
175
    if (interleave && buf)
176
        free(buf);
177
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
178 179
}

180 181 182
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
183
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
184
    struct aec_stream strm;
185
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
186
    void *buf;
187 188 189 190 191 192 193
    size_t padding_size;
    size_t scanlines;
    size_t buf_size, total_out;
    int pixel_size;
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
194

195 196 197 198 199 200
    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
    strm.flags = convert_options(param->options_mask);
    strm.avail_in = sourceLen;
    strm.next_in = source;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
201
    buf = 0;
202 203 204 205

    pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
    deinterleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64;
    extra_buffer = pad_scanline || deinterleave;
206

207
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
208
        strm.bits_per_sample = 8;
209
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
210
        strm.bits_per_sample = param->bits_per_pixel;
211

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
212
    pixel_size = bits_to_bytes(strm.bits_per_sample);
213 214 215 216 217

    if (extra_buffer) {
        if (pad_scanline) {
            scanlines = (*destLen / pixel_size + param->pixels_per_scanline - 1)
                / param->pixels_per_scanline;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
218
            buf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
219 220 221 222
        } else {
            buf_size = *destLen;
        }
        buf = malloc(buf_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
223 224 225 226
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
227
        strm.next_out = buf;
228
        strm.avail_out = buf_size;
229 230
    } else {
        strm.next_out = dest;
231
        strm.avail_out = *destLen;
232
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
233

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
234
    status = aec_buffer_decode(&strm);
235
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
236
        goto CLEANUP;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
237

238
    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
239 240 241 242 243 244 245
        padding_size =
            (strm.rsi * strm.block_size - param->pixels_per_scanline)
            * pixel_size;
        remove_padding(buf, strm.total_out,
                       param->pixels_per_scanline * pixel_size,
                       padding_size, pixel_size);
        total_out = scanlines * param->pixels_per_scanline * pixel_size;
246 247 248 249 250 251
    } else {
        total_out = strm.total_out;
    }

    if (total_out < *destLen)
        *destLen = total_out;
252

253
    if (deinterleave)
254
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
255 256 257
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
258
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
259
    if (extra_buffer && buf)
260 261
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
262
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
263
}
264 265 266 267 268

int SZ_encoder_enabled(void)
{
    return 1;
}