sz_compat.c 7.19 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 6
#if HAVE_CONFIG_H
#  include <config.h>
#endif
7
#include "szlib.h"
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
8

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

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
21
    for (int i = 1; i < NOPTS; i <<= 1)
22 23
        if (sz_opts & i)
            opts |= co[i];
24 25 26 27

    return opts;
}

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

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

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

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

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

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

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

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

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

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

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

133
    pixel_size = bits_to_bytes(strm.bits_per_sample);
134

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
148 149 150 151 152 153
    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
154

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

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

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

184 185 186 187 188 189 190
    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;

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
202

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

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

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

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

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
252
    return status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
253
}
254 255 256 257 258

int SZ_encoder_enabled(void)
{
    return 1;
}
259 260 261

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