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

7
#define NOPTS 129
8
#define MIN(a, b) (((a) < (b))? (a): (b))
9 10 11 12 13 14 15 16 17 18

static int convert_options(int sz_opts)
{
    int co[NOPTS];
    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;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
19
    for (int i = 1; i < NOPTS; i <<= 1)
20 21
        if (sz_opts & i)
            opts |= co[i];
22 23 24 25

    return opts;
}

26 27 28 29 30 31 32 33 34 35
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
36
static void interleave_buffer(void *dest, const void *src,
37 38
                              size_t n, int wordsize)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
39 40
    const unsigned char *src8 = (unsigned char *)src;
    unsigned char *dest8 = (unsigned char *)dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
41

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
42 43
    for (size_t i = 0; i < n / wordsize; i++)
        for (size_t j = 0; j < wordsize; j++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
44
            dest8[j * (n / wordsize) + i] = src8[i * wordsize + j];
45 46
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
47 48
static void deinterleave_buffer(void *dest, const void *src,
                                size_t n, int wordsize)
49
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
50 51
    const unsigned char *src8 = (unsigned char *)src;
    unsigned char *dest8 = (unsigned char *)dest;
52

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58
static void add_padding(void *dest, const void *src, size_t src_length,
59 60
                        size_t line_size, size_t padding_size,
                        int pixel_size, int pp)
61
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
62
    const char zero_pixel[] = {0, 0, 0, 0};
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
63

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
64 65 66
    const char *pixel = zero_pixel;
    size_t j = 0;
    size_t i = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
67
    while (i < src_length) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
68 69
        size_t ps;
        size_t ls = MIN(src_length - i, line_size);
70 71 72
        memcpy((char *)dest + j, (char *)src + i, ls);
        j += ls;
        i += ls;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
73
        if (pp)
74 75
            pixel = (char *)src + i - pixel_size;
        ps = line_size + padding_size - ls;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
76
        for (size_t k = 0; k < ps; k += pixel_size)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
77
            memcpy((char *)dest + j + k, pixel, pixel_size);
78
        j += ps;
79 80 81
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
82
static void remove_padding(void *buf, size_t buf_length,
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
83 84
                           size_t line_size, size_t padding_size,
                           int pixel_size)
85
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
86
    size_t padded_line_size = line_size + padding_size;
87

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
88 89
    size_t i = line_size;
    for (size_t j = padded_line_size; j < buf_length; j += padded_line_size) {
90
        memmove((char *)buf + i, (char *)buf + j, line_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
91
        i += line_size;
92 93 94
    }
}

95 96 97
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
98
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
99
    struct aec_stream strm;
100 101
    void *buf = 0;
    void *padbuf = 0;
102
    int status;
103 104 105 106 107 108
    int interleave;
    int pixel_size;
    int aec_status;
    size_t scanlines;
    size_t padbuf_size;
    size_t padding_size;
109 110 111 112

    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
113
    strm.flags = AEC_NOT_ENFORCE | convert_options(param->options_mask);
114 115
    strm.avail_out = *destLen;
    strm.next_out = dest;
116

117
    interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64;
118
    if (interleave) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
119
        strm.bits_per_sample = 8;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
120
        buf = malloc(sourceLen);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
121 122 123 124
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
125 126
        interleave_buffer(buf, source, sourceLen, param->bits_per_pixel / 8);
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
127
        strm.bits_per_sample = param->bits_per_pixel;
128
        buf = (void *)source;
129
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
130

131
    pixel_size = bits_to_bytes(strm.bits_per_sample);
132

133
    scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
134
        / param->pixels_per_scanline;
135
    padbuf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
136 137 138 139 140
    padbuf = malloc(padbuf_size);
    if (padbuf == NULL) {
        status = SZ_MEM_ERROR;
        goto CLEANUP;
    }
141

142
    padding_size =
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
143 144
        (strm.rsi * strm.block_size - param->pixels_per_scanline)
        * pixel_size;
145

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
146 147 148 149 150 151
    add_padding(padbuf, buf, sourceLen,
                param->pixels_per_scanline * pixel_size,
                padding_size, pixel_size,
                strm.flags & AEC_DATA_PREPROCESS);
    strm.next_in = padbuf;
    strm.avail_in = padbuf_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
152

153
    aec_status = aec_buffer_encode(&strm);
154 155 156 157
    if (aec_status == AEC_STREAM_ERROR)
        status = SZ_OUTBUFF_FULL;
    else
        status = aec_status;
158 159
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
160
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
161
    if (padbuf)
162
        free(padbuf);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
163
    if (interleave && buf)
164
        free(buf);
165
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
166 167
}

168 169 170
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
171
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
172
    struct aec_stream strm;
173
    void *buf = 0;
174
    int status;
175 176 177 178
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
    int pixel_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
179
    size_t total_out;
180
    size_t scanlines;
181

182 183 184 185 186 187 188
    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;

189 190
    pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
    deinterleave = (param->bits_per_pixel == 32
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191
                        || param->bits_per_pixel == 64);
192
    extra_buffer = pad_scanline || deinterleave;
193

194
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
195
        strm.bits_per_sample = 8;
196
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
197 198
        strm.bits_per_sample = param->bits_per_pixel;
    pixel_size = bits_to_bytes(strm.bits_per_sample);
199

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
200

201
    if (extra_buffer) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
202
        size_t buf_size;
203 204 205
        if (pad_scanline) {
            scanlines = (*destLen / pixel_size + param->pixels_per_scanline - 1)
                / param->pixels_per_scanline;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
206
            buf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
207 208 209 210
        } else {
            buf_size = *destLen;
        }
        buf = malloc(buf_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
211 212 213 214
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
215
        strm.next_out = buf;
216
        strm.avail_out = buf_size;
217 218
    } else {
        strm.next_out = dest;
219
        strm.avail_out = *destLen;
220
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
221

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
222
    status = aec_buffer_decode(&strm);
223
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
224
        goto CLEANUP;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
225

226
    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
227
        size_t padding_size =
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
228 229 230 231 232 233
            (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;
234 235 236 237 238 239
    } else {
        total_out = strm.total_out;
    }

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

241
    if (deinterleave)
242
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
243 244 245
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
246
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
247
    if (extra_buffer && buf)
248 249
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
250
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
251
}
252 253 254 255 256

int SZ_encoder_enabled(void)
{
    return 1;
}
257 258 259

/* netcdf searches for SZ_Compress in configure */
char SZ_Compress() { return SZ_OK; }