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

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

    return opts;
}

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

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
46 47 48

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

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

    src8 = (unsigned char *)src;
    dest8 = (unsigned char *)dest;
61 62 63

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

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
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};

    for (i = 0, j = 0;
         i < total;
         i += pixel_size, j += pixel_size) {
        if (i > 0 && (i % line_size) == 0) {
            if (pp)
                pixel = (char *)src + i - 1;
            else
                pixel = zero_pixel;
            for (k = 0; k < padding_size; k += pixel_size)
                memcpy((char *)dest + j + k, pixel, pixel_size);
            j += padding_size;
        }
        memcpy((char *)dest + j, (char *)src + i, pixel_size);
    }
    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;

    for (i = 0, j = padding_size;
         i < total;
         i += pixel_size, j += pixel_size) {
        if (i % (line_size + padding_size) == 0)
            j -= padding_size;
        memcpy((char *)buf + j, (char *)buf + i, pixel_size);
    }
105
    if (i % (line_size + padding_size) == 0)
106 107 108 109
        j -= padding_size;
    return j;
}

110 111 112
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
113
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
114
    struct aec_stream strm;
115 116
    int status;
    void *padbuf;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
117
    void *buf;
118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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;
132

133 134 135 136
    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
137
        strm.bits_per_sample = 8;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
138
        buf = malloc(sourceLen);
139 140 141 142
        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
143
        strm.bits_per_sample = param->bits_per_pixel;
144
        buf = (void *)source;
145
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
146

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

    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
174

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
175
    status = aec_buffer_encode(&strm);
176
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
177 178
        return status;

179 180 181 182 183 184
    *destLen = strm.total_out;

    if (pad_scanline)
        free(padbuf);

    if (interleave)
185 186
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
187 188 189
    return SZ_OK;
}

190 191 192
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
193
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
194
    struct aec_stream strm;
195
    int status;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
196
    void *buf;
197 198 199 200 201 202 203
    size_t padding_size;
    size_t scanlines;
    size_t buf_size, total_out;
    int pixel_size;
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
204

205 206 207 208 209 210 211 212 213 214
    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;
215

216
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
217
        strm.bits_per_sample = 8;
218
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
219
        strm.bits_per_sample = param->bits_per_pixel;
220

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

    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;
235
        strm.next_out = buf;
236
        strm.avail_out = buf_size;
237 238
    } else {
        strm.next_out = dest;
239
        strm.avail_out = *destLen;
240
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
241

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
242
    status = aec_buffer_decode(&strm);
243
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
244 245
        return status;

246 247 248 249 250 251 252 253 254 255 256 257 258 259
    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;
260

261
    if (deinterleave)
262
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
263 264 265 266
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

    if (extra_buffer)
267 268
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
269 270
    return SZ_OK;
}
271 272 273 274 275

int SZ_encoder_enabled(void)
{
    return 1;
}