sz_compat.c 7.27 KB
Newer Older
1
#include <stdio.h>
2
#include <stdlib.h>
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 21 22

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 src_length,
72 73
                        size_t line_size, size_t padding_size,
                        int pixel_size, int pp)
74
{
75
    size_t i, j, k, ls, ps;
76
    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

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

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

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

110 111 112
int SZ_BufftoBuffCompress(void *dest, size_t *destLen,
                          const void *source, size_t sourceLen,
                          SZ_com_t *param)
113
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
114
    struct aec_stream strm;
115
    int status;
116
    int aec_status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
117 118
    void *padbuf;
    void *buf;
119 120
    size_t padding_size;
    size_t scanlines;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
121
    size_t padbuf_size;
122 123 124 125 126 127
    int pixel_size;
    int interleave;

    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
128
    strm.flags = AEC_NOT_ENFORCE | convert_options(param->options_mask);
129 130
    strm.avail_out = *destLen;
    strm.next_out = dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
131 132
    buf = 0;
    padbuf = 0;
133

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
150 151 152 153 154 155 156 157
    scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1)
        / 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;
    }
158

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
163 164 165 166 167 168
    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;
169

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

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

185 186 187
int SZ_BufftoBuffDecompress(void *dest, size_t *destLen,
                            const void *source, size_t sourceLen,
                            SZ_com_t *param)
188
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
189
    struct aec_stream strm;
190
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191
    void *buf;
192 193 194 195 196 197 198
    size_t padding_size;
    size_t scanlines;
    size_t buf_size, total_out;
    int pixel_size;
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
199

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

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

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

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

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

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

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

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

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
267
    return status;
268
}
269 270 271 272 273

int SZ_encoder_enabled(void)
{
    return 1;
}
274 275 276

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