sz_compat.c 7.27 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
                              size_t n, int wordsize)
{
42 43
    size_t i;
    int j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
44 45 46 47 48
    const unsigned char *src8;
    unsigned char *dest8;

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

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

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

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
71
static void add_padding(void *dest, const void *src, size_t total,
72 73 74 75 76
                          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
77
    const char zero_pixel[] = {0, 0, 0, 0};
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
78 79 80 81 82 83 84

    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)
85
            pixel = (char *)src + i + line_size - pixel_size;
86
        for (k = 0; k < padding_size; k += pixel_size)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
87 88
            memcpy((char *)dest + j + k, pixel, pixel_size);
        j += padding_size;
89 90 91
    }
}

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

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

106 107 108
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
109
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
110
    struct aec_stream strm;
111
    int status;
112
    int aec_status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
113 114
    void *padbuf;
    void *buf;
115 116
    size_t padding_size;
    size_t scanlines;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
117
    size_t padbuf_size;
118 119 120 121 122 123 124 125 126 127
    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
128 129
    buf = 0;
    padbuf = 0;
130

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
146
    pixel_size = bits_to_bytes(strm.bits_per_sample);
147 148

    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
149 150 151 152 153 154 155
        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;
        }
156

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
157 158 159
        padding_size =
            (strm.rsi * strm.block_size - param->pixels_per_scanline)
            * pixel_size;
160

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

172 173 174 175 176
    aec_status = aec_buffer_encode(&strm);
    if (aec_status == AEC_STREAM_ERROR)
        status = SZ_OUTBUFF_FULL;
    else
        status = aec_status;
177 178
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
179
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
180
    if (pad_scanline && padbuf)
181
        free(padbuf);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
182
    if (interleave && buf)
183
        free(buf);
184
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
185 186
}

187 188 189
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
190
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191
    struct aec_stream strm;
192
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
193
    void *buf;
194 195 196 197 198 199 200
    size_t padding_size;
    size_t scanlines;
    size_t buf_size, total_out;
    int pixel_size;
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
201

202 203 204 205 206 207
    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
208
    buf = 0;
209 210 211 212

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

214
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
215
        strm.bits_per_sample = 8;
216
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
217
        strm.bits_per_sample = param->bits_per_pixel;
218

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
219
    pixel_size = bits_to_bytes(strm.bits_per_sample);
220 221 222 223 224

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
241
    status = aec_buffer_decode(&strm);
242
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
243
        goto CLEANUP;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
244

245
    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
246 247 248 249 250 251 252
        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;
253 254 255 256 257 258
    } else {
        total_out = strm.total_out;
    }

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

260
    if (deinterleave)
261
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
262 263 264
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
265
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
266
    if (extra_buffer && buf)
267 268
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
269
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
270
}
271 272 273 274 275

int SZ_encoder_enabled(void)
{
    return 1;
}