sz_compat.c 7.04 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

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)
100
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
101
    struct aec_stream strm;
102 103 104 105 106
    int status;

    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
107
    strm.flags = AEC_NOT_ENFORCE | convert_options(param->options_mask);
108 109
    strm.avail_out = *destLen;
    strm.next_out = dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
110 111
    void *buf = 0;
    void *padbuf = 0;
112

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
127
    int pixel_size = bits_to_bytes(strm.bits_per_sample);
128

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
138
    size_t padding_size =
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
139 140
        (strm.rsi * strm.block_size - param->pixels_per_scanline)
        * pixel_size;
141

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
142 143 144 145 146 147
    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;
148

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
149
    int aec_status = aec_buffer_encode(&strm);
150 151 152 153
    if (aec_status == AEC_STREAM_ERROR)
        status = SZ_OUTBUFF_FULL;
    else
        status = aec_status;
154 155
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
156
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
157
    if (padbuf)
158
        free(padbuf);
159
    if (interleave && buf)
160
        free(buf);
161
    return status;
162 163
}

164 165 166
int SZ_BufftoBuffDecompress(void *dest, size_t *destLen,
                            const void *source, size_t sourceLen,
                            SZ_com_t *param)
167
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
168
    struct aec_stream strm;
169
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
170
    size_t total_out;
171
    size_t scanlines;
172

173 174 175 176 177 178
    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
179
    void *buf = 0;
180

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
181 182 183 184 185
    int pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
    int deinterleave = (param->bits_per_pixel == 32
                        || param->bits_per_pixel == 64);
    int extra_buffer = pad_scanline || deinterleave;
    int pixel_size;
186

187
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
188
        strm.bits_per_sample = 8;
189
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
190 191
        strm.bits_per_sample = param->bits_per_pixel;
    pixel_size = bits_to_bytes(strm.bits_per_sample);
192

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
193

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
215
    status = aec_buffer_decode(&strm);
216
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
217
        goto CLEANUP;
218

219
    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
220
        size_t padding_size =
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
221 222 223 224 225 226
            (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;
227 228 229 230 231 232
    } else {
        total_out = strm.total_out;
    }

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

234
    if (deinterleave)
235
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
236 237 238
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
239
CLEANUP:
240
    if (extra_buffer && buf)
241 242
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
243
    return status;
244
}
245 246 247 248 249

int SZ_encoder_enabled(void)
{
    return 1;
}
250 251 252

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