sz_compat.c 7.4 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"

6 7 8 9 10 11 12 13 14 15 16 17 18
#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)
19 20
        if (sz_opts & i)
            opts |= co[i];
21 22 23 24

    return opts;
}

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

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
44 45 46

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
50 51
static void deinterleave_buffer(void *dest, const void *src,
                                size_t n, int wordsize)
52 53
{
    size_t i, j;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
54 55 56 57 58
    const unsigned char *src8;
    unsigned char *dest8;

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
59 60 61

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

65 66 67 68 69 70 71
static size_t add_padding(void *dest, const void *src, size_t total,
                          size_t line_size, size_t padding_size,
                          int pixel_size, int pp)
{
    size_t i, j, k;
    const char *pixel;
    const char zero_pixel[] = {0, 0, 0, 0, 0, 0, 0, 0};
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
72 73 74 75 76 77 78 79
    size_t padded_line_size = line_size + padding_size;

    pixel = zero_pixel;
    j = 0;
    for (i = 0; i < total; i += line_size) {
        if (i + line_size > total) {
            line_size = total - i;
            padding_size = padded_line_size - line_size;
80
        }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
81 82 83 84 85 86 87
        memcpy((char *)dest + j, (char *)src + i, line_size);
        j += line_size;
        if (pp)
            pixel = (char *)src + i - 1;
        for (k = 0; k < padding_size / pixel_size; k += pixel_size)
            memcpy((char *)dest + j + k, pixel, pixel_size);
        j += padding_size;
88 89 90 91 92 93 94 95 96
    }
    return j;
}

static size_t remove_padding(void *buf, size_t total,
                             size_t line_size, size_t padding_size,
                             int pixel_size)
{
    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 101 102
    i = line_size;
    for (j = padded_line_size; j < total; j += padded_line_size) {
        memcpy((char *)buf + i, (char *)buf + j, line_size);
        i += line_size;
103
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
104
    return i;
105 106
}

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

130 131 132 133
    pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
    interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64;

    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);
136 137 138 139
        if (buf == NULL)
            return SZ_MEM_ERROR;
        interleave_buffer(buf, source, sourceLen, param->bits_per_pixel / 8);
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
140
        strm.bits_per_sample = param->bits_per_pixel;
141
        buf = (void *)source;
142
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
143

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
144
    pixel_size = bits_to_bytes(strm.bits_per_sample);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

    if (pad_scanline) {
        scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1)
            / param->pixels_per_scanline;
        buf_size = strm.rsi * param->pixels_per_block * pixel_size * scanlines;

        padbuf = malloc(buf_size);
        if (padbuf == NULL)
            return SZ_MEM_ERROR;

        padding_size = (
            param->pixels_per_block -
            (param->pixels_per_scanline % param->pixels_per_block)
            ) * pixel_size;

        padded_length = 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 = padded_length;
    } else {
        strm.next_in = buf;
        strm.avail_in = sourceLen;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
171

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
172
    status = aec_buffer_encode(&strm);
173
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
174 175
        return status;

176 177
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
178
    if (pad_scanline && padbuf)
179 180
        free(padbuf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
181
    if (interleave && buf)
182 183
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
184 185 186
    return SZ_OK;
}

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 = 0;
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 208 209 210 211
    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;

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
218
    pixel_size = bits_to_bytes(strm.bits_per_sample);
219 220 221 222 223 224 225 226 227 228 229 230 231

    if (extra_buffer) {
        if (pad_scanline) {
            scanlines = (*destLen / pixel_size + param->pixels_per_scanline - 1)
                / param->pixels_per_scanline;
            buf_size = strm.rsi * param->pixels_per_block
                * pixel_size * scanlines;
        } else {
            buf_size = *destLen;
        }
        buf = malloc(buf_size);
        if (buf == NULL)
            return SZ_MEM_ERROR;
232
        strm.next_out = buf;
233
        strm.avail_out = buf_size;
234 235
    } else {
        strm.next_out = dest;
236
        strm.avail_out = *destLen;
237
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
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 242
        return status;

243 244 245 246 247 248 249 250 251 252 253 254 255 256
    if (pad_scanline) {
        padding_size = (
            param->pixels_per_block -
            (param->pixels_per_scanline % param->pixels_per_block)
            ) * pixel_size;
        total_out = remove_padding(buf, strm.total_out,
                                   param->pixels_per_scanline * pixel_size,
                                   padding_size, pixel_size);
    } 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
    if (extra_buffer && buf)
264 265
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
266 267
    return SZ_OK;
}
268 269 270 271 272

int SZ_encoder_enabled(void)
{
    return 1;
}