aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/libImaging/TgaRleEncode.c
blob: aa7e7b96d81d79269fe8459c9ff2a56fe8ef7a57 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "Imaging.h"

#include <assert.h>
#include <string.h>

static int
comparePixels(const UINT8 *buf, int x, int bytesPerPixel) {
    buf += x * bytesPerPixel;
    return memcmp(buf, buf + bytesPerPixel, bytesPerPixel) == 0;
}

int
ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
    UINT8 *dst;
    int bytesPerPixel;

    if (state->state == 0) {
        if (state->ystep < 0) {
            state->ystep = -1;
            state->y = state->ysize - 1;
        } else {
            state->ystep = 1;
        }

        state->state = 1;
    }

    dst = buf;
    bytesPerPixel = (state->bits + 7) / 8;

    while (1) {
        int flushCount;

        /*
         * state->count is the numbers of bytes in the packet,
         * excluding the 1-byte descriptor.
         */
        if (state->count == 0) {
            UINT8 *row;
            UINT8 descriptor;
            int startX;

            assert(state->x <= state->xsize);

            /* Make sure we have space for the descriptor. */
            if (bytes < 1) {
                break;
            }

            if (state->x == state->xsize) {
                state->x = 0;

                state->y += state->ystep;
                if (state->y < 0 || state->y >= state->ysize) {
                    state->errcode = IMAGING_CODEC_END;
                    break;
                }
            }

            if (state->x == 0) {
                state->shuffle(
                    state->buffer,
                    (UINT8 *)im->image[state->y + state->yoff] +
                        state->xoff * im->pixelsize,
                    state->xsize);
            }

            row = state->buffer;

            /* Start with a raw packet for 1 px. */
            descriptor = 0;
            startX = state->x;
            state->count = bytesPerPixel;

            if (state->x + 1 < state->xsize) {
                int maxLookup;
                int isRaw;

                isRaw = !comparePixels(row, state->x, bytesPerPixel);
                ++state->x;

                /*
                 * A packet can contain up to 128 pixels;
                 * 2 are already behind (state->x points to
                 * the second one).
                 */
                maxLookup = state->x + 126;
                /* A packet must not span multiple rows. */
                if (maxLookup > state->xsize - 1) {
                    maxLookup = state->xsize - 1;
                }

                if (isRaw) {
                    while (state->x < maxLookup) {
                        if (!comparePixels(row, state->x, bytesPerPixel)) {
                            ++state->x;
                        } else {
                            /* Two identical pixels will go to RLE packet. */
                            --state->x;
                            break;
                        }
                    }

                    state->count += (state->x - startX) * bytesPerPixel;
                } else {
                    descriptor |= 0x80;

                    while (state->x < maxLookup) {
                        if (comparePixels(row, state->x, bytesPerPixel)) {
                            ++state->x;
                        } else {
                            break;
                        }
                    }
                }
            }

            /*
             * state->x currently points to the last pixel to be
             * included in the packet. The pixel count in the
             * descriptor is 1 less than actual number of pixels in
             * the packet, that is, state->x == startX if we encode
             * only 1 pixel.
             */
            descriptor += state->x - startX;
            *dst++ = descriptor;
            --bytes;

            /* Advance to past-the-last encoded pixel. */
            ++state->x;
        }

        assert(bytes >= 0);
        assert(state->count > 0);
        assert(state->x > 0);
        assert(state->count <= state->x * bytesPerPixel);

        if (bytes == 0) {
            break;
        }

        flushCount = state->count;
        if (flushCount > bytes) {
            flushCount = bytes;
        }

        memcpy(
            dst, state->buffer + (state->x * bytesPerPixel - state->count), flushCount);
        dst += flushCount;
        bytes -= flushCount;

        state->count -= flushCount;
    }

    return dst - buf;
}