sz_compat.c 7.43 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
#define NOPTS 129
11
#define MIN(a, b) (((a) < (b))? (a): (b))
12 13 14 15 16 17 18 19 20 21 22 23

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)
24 25
        if (sz_opts & i)
            opts |= co[i];
26 27 28 29

    return opts;
}

30 31 32 33 34 35 36 37 38 39
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
40
static void interleave_buffer(void *dest, const void *src,
41 42
                              size_t n, int wordsize)
{
43 44
    size_t i;
    int 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[j * (n / wordsize) + i] = src8[i * wordsize + j];
54 55
}

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

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
66 67 68

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

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

    pixel = zero_pixel;
    j = 0;
82 83 84 85 86 87
    i = 0;
    while (i < total) {
        ls = MIN(total - i, line_size);
        memcpy((char *)dest + j, (char *)src + i, ls);
        j += ls;
        i += ls;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
88
        if (pp)
89 90 91
            pixel = (char *)src + i - pixel_size;
        ps = line_size + padding_size - ls;
        for (k = 0; k < ps; k += pixel_size)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
92
            memcpy((char *)dest + j + k, pixel, pixel_size);
93
        j += ps;
94 95 96
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
97 98 99
static void remove_padding(void *buf, size_t total,
                           size_t line_size, size_t padding_size,
                           int pixel_size)
100 101
{
    size_t i, j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
102
    size_t padded_line_size = line_size + padding_size;
103

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
104 105
    i = line_size;
    for (j = padded_line_size; j < total; j += padded_line_size) {
106
        memmove((char *)buf + i, (char *)buf + j, line_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107
        i += line_size;
108 109 110
    }
}

111 112 113
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
114
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
115
    struct aec_stream strm;
116
    int status;
117
    int aec_status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
118 119
    void *padbuf;
    void *buf;
120 121
    size_t padding_size;
    size_t scanlines;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
122
    size_t padbuf_size;
123 124 125 126 127 128 129
    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;
130
    strm.flags = AEC_NOT_ENFORCE | convert_options(param->options_mask);
131 132
    strm.avail_out = *destLen;
    strm.next_out = dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
133 134
    buf = 0;
    padbuf = 0;
135

136
    interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
137
    pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
138
    if (interleave) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
139
        strm.bits_per_sample = 8;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
140
        buf = malloc(sourceLen);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
141 142 143 144
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
145 146
        interleave_buffer(buf, source, sourceLen, param->bits_per_pixel / 8);
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
147
        strm.bits_per_sample = param->bits_per_pixel;
148
        buf = (void *)source;
149
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
150

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
151
    pixel_size = bits_to_bytes(strm.bits_per_sample);
152 153

    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
154
        scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1)
155
            / param->pixels_per_scanline;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
156 157 158 159 160 161
        padbuf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
        padbuf = malloc(padbuf_size);
        if (padbuf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
162

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
163 164 165
        padding_size =
            (strm.rsi * strm.block_size - param->pixels_per_scanline)
            * pixel_size;
166

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
167 168 169 170
        add_padding(padbuf, buf, sourceLen,
                    param->pixels_per_scanline * pixel_size,
                    padding_size, pixel_size,
                    strm.flags & AEC_DATA_PREPROCESS);
171
        strm.next_in = padbuf;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
172
        strm.avail_in = padbuf_size;
173 174 175 176
    } else {
        strm.next_in = buf;
        strm.avail_in = sourceLen;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
177

178 179 180 181 182
    aec_status = aec_buffer_encode(&strm);
    if (aec_status == AEC_STREAM_ERROR)
        status = SZ_OUTBUFF_FULL;
    else
        status = aec_status;
183 184
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
185
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
186
    if (pad_scanline && padbuf)
187
        free(padbuf);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
188
    if (interleave && buf)
189
        free(buf);
190
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191 192
}

193 194 195
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
196
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
197
    struct aec_stream strm;
198
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
199
    void *buf;
200 201 202 203 204 205 206
    size_t padding_size;
    size_t scanlines;
    size_t buf_size, total_out;
    int pixel_size;
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
207

208 209 210 211 212 213
    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
214
    buf = 0;
215 216 217 218

    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;
219

220
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
221
        strm.bits_per_sample = 8;
222
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
223
        strm.bits_per_sample = param->bits_per_pixel;
224

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
225
    pixel_size = bits_to_bytes(strm.bits_per_sample);
226 227 228 229 230

    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
231
            buf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
232 233 234 235
        } else {
            buf_size = *destLen;
        }
        buf = malloc(buf_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
236 237 238 239
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
240
        strm.next_out = buf;
241
        strm.avail_out = buf_size;
242 243
    } else {
        strm.next_out = dest;
244
        strm.avail_out = *destLen;
245
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
246

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
247
    status = aec_buffer_decode(&strm);
248
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
249
        goto CLEANUP;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
250

251
    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
252 253 254 255 256 257 258
        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;
259 260 261 262 263 264
    } else {
        total_out = strm.total_out;
    }

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

266
    if (deinterleave)
267
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
268 269 270
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
271
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
272
    if (extra_buffer && buf)
273 274
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
275
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
276
}
277 278 279 280 281

int SZ_encoder_enabled(void)
{
    return 1;
}