aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/libImaging/BitDecode.c
blob: 28baa8b7ea8342faf70d2db4d607e8c5dd83f9de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * The Python Imaging Library.
 * $Id$
 *
 * decoder for packed bitfields (converts to floating point)
 *
 * history:
 * 97-05-31 fl created (much more than originally intended)
 *
 * Copyright (c) Fredrik Lundh 1997.
 * Copyright (c) Secret Labs AB 1997.
 *
 * See the README file for information on usage and redistribution.
 */

#include "Imaging.h"

#include "Bit.h"

int
ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
    BITSTATE *bitstate = state->context;
    UINT8 *ptr;

    if (state->state == 0) {
        /* Initialize context variables */

        /* this decoder only works for float32 image buffers */
        if (im->type != IMAGING_TYPE_FLOAT32) {
            state->errcode = IMAGING_CODEC_CONFIG;
            return -1;
        }

        /* sanity check */
        if (bitstate->bits < 1 || bitstate->bits >= 32) {
            state->errcode = IMAGING_CODEC_CONFIG;
            return -1;
        }

        bitstate->mask = (1 << bitstate->bits) - 1;

        if (bitstate->sign) {
            bitstate->signmask = (1 << (bitstate->bits - 1));
        }

        /* check image orientation */
        if (state->ystep < 0) {
            state->y = state->ysize - 1;
            state->ystep = -1;
        } else {
            state->ystep = 1;
        }

        state->state = 1;
    }

    ptr = buf;

    while (bytes > 0) {
        UINT8 byte = *ptr;

        ptr++;
        bytes--;

        /* get a byte from the input stream and insert in the bit buffer */
        if (bitstate->fill & 1) {
            /* fill MSB first */
            bitstate->bitbuffer |= (unsigned long)byte << bitstate->bitcount;
        } else {
            /* fill LSB first */
            bitstate->bitbuffer = (bitstate->bitbuffer << 8) | byte;
        }

        bitstate->bitcount += 8;

        while (bitstate->bitcount >= bitstate->bits) {
            /* get a pixel from the bit buffer */
            unsigned long data;
            FLOAT32 pixel;

            if (bitstate->fill & 2) {
                /* store LSB first */
                data = bitstate->bitbuffer & bitstate->mask;
                if (bitstate->bitcount > 32) {
                    /* bitbuffer overflow; restore it from last input byte */
                    bitstate->bitbuffer =
                        byte >> (8 - (bitstate->bitcount - bitstate->bits));
                } else {
                    bitstate->bitbuffer >>= bitstate->bits;
                }
            } else {
                /* store MSB first */
                data = (bitstate->bitbuffer >> (bitstate->bitcount - bitstate->bits)) &
                       bitstate->mask;
            }

            bitstate->bitcount -= bitstate->bits;

            if (bitstate->lutsize > 0) {
                /* map through lookup table */
                if (data <= 0) {
                    pixel = bitstate->lut[0];
                } else if (data >= bitstate->lutsize) {
                    pixel = bitstate->lut[bitstate->lutsize - 1];
                } else {
                    pixel = bitstate->lut[data];
                }
            } else {
                /* convert */
                if (data & bitstate->signmask) {
                    /* image memory contains signed data */
                    pixel = (FLOAT32)(INT32)(data | ~bitstate->mask);
                } else {
                    pixel = (FLOAT32)data;
                }
            }

            *(FLOAT32 *)(&im->image32[state->y][state->x]) = pixel;

            /* step forward */
            if (++state->x >= state->xsize) {
                /* new line */
                state->y += state->ystep;
                if (state->y < 0 || state->y >= state->ysize) {
                    /* end of file (errcode = 0) */
                    return -1;
                }
                state->x = 0;
                /* reset bit buffer */
                if (bitstate->pad > 0) {
                    bitstate->bitcount = 0;
                }
            }
        }
    }

    return ptr - buf;
}