Pillow

Форк
0
/
PcxEncode.c 
188 строк · 6.0 Кб
1
/*
2
 * The Python Imaging Library.
3
 * $Id$
4
 *
5
 * encoder for PCX data
6
 *
7
 * history:
8
 * 99-02-07 fl  created
9
 *
10
 * Copyright (c) Fredrik Lundh 1999.
11
 * Copyright (c) Secret Labs AB 1999.
12
 *
13
 * See the README file for information on usage and redistribution.
14
 */
15

16
#include "Imaging.h"
17

18
enum { INIT, FETCH, ENCODE };
19

20
/* we're reusing "ystep" to store the last value */
21
#define LAST ystep
22

23
int
24
ImagingPcxEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
25
    UINT8 *ptr;
26
    int this;
27
    int bytes_per_line = 0;
28
    int padding = 0;
29
    int stride = 0;
30
    int bpp = 0;
31
    int planes = 1;
32
    int i;
33

34
    ptr = buf;
35

36
    if (!state->state) {
37
        /* sanity check */
38
        if (state->xsize <= 0 || state->ysize <= 0) {
39
            state->errcode = IMAGING_CODEC_END;
40
            return 0;
41
        }
42
        state->state = FETCH;
43
    }
44

45
    bpp = state->bits;
46
    if (state->bits == 24) {
47
        planes = 3;
48
        bpp = 8;
49
    }
50

51
    bytes_per_line = (state->xsize * bpp + 7) / 8;
52
    /* The stride here needs to be kept in sync with the version in
53
       PcxImagePlugin.py. If it's not, the header and the body of the
54
       image will be out of sync and bad things will happen on decode.
55
    */
56
    stride = bytes_per_line + (bytes_per_line % 2);
57

58
    padding = stride - bytes_per_line;
59

60
    for (;;) {
61
        switch (state->state) {
62
            case FETCH:
63

64
                /* get a line of data */
65
                if (state->y >= state->ysize) {
66
                    state->errcode = IMAGING_CODEC_END;
67
                    return ptr - buf;
68
                }
69

70
                state->shuffle(
71
                    state->buffer,
72
                    (UINT8 *)im->image[state->y + state->yoff] +
73
                        state->xoff * im->pixelsize,
74
                    state->xsize
75
                );
76

77
                state->y += 1;
78

79
                state->count = 1;
80
                state->LAST = state->buffer[0];
81

82
                state->x = 1;
83

84
                state->state = ENCODE;
85
                /* fall through */
86

87
            case ENCODE:
88
                /* compress this line */
89

90
                /* when we arrive here, "count" contains the number of
91
                   bytes having the value of "LAST" that we've already
92
                   seen */
93
                do {
94
                    /* If we're encoding an odd width file, and we've
95
                       got more than one plane, we need to pad each
96
                       color row with padding bytes at the end. Since
97
                       The pixels are stored RRRRRGGGGGBBBBB, so we need
98
                       to have the padding be RRRRRPGGGGGPBBBBBP. Hence
99
                       the double loop
100
                    */
101
                    while (state->x % bytes_per_line) {
102
                        if (state->count == 63) {
103
                            /* this run is full; flush it */
104
                            if (bytes < 2) {
105
                                return ptr - buf;
106
                            }
107
                            ptr[0] = 0xff;
108
                            ptr[1] = state->LAST;
109
                            ptr += 2;
110
                            bytes -= 2;
111

112
                            state->count = 0;
113
                        }
114

115
                        this = state->buffer[state->x];
116

117
                        if (this == state->LAST) {
118
                            /* extend the current run */
119
                            state->x += 1;
120
                            state->count += 1;
121

122
                        } else {
123
                            /* start a new run */
124
                            if (state->count == 1 && (state->LAST < 0xc0)) {
125
                                if (bytes < 1) {
126
                                    return ptr - buf;
127
                                }
128
                                ptr[0] = state->LAST;
129
                                ptr += 1;
130
                                bytes -= 1;
131
                            } else {
132
                                if (state->count > 0) {
133
                                    if (bytes < 2) {
134
                                        return ptr - buf;
135
                                    }
136
                                    ptr[0] = 0xc0 | state->count;
137
                                    ptr[1] = state->LAST;
138
                                    ptr += 2;
139
                                    bytes -= 2;
140
                                }
141
                            }
142

143
                            state->LAST = this;
144
                            state->count = 1;
145

146
                            state->x += 1;
147
                        }
148
                    }
149

150
                    /* end of line; flush the current run */
151
                    if (state->count == 1 && (state->LAST < 0xc0)) {
152
                        if (bytes < 1 + padding) {
153
                            return ptr - buf;
154
                        }
155
                        ptr[0] = state->LAST;
156
                        ptr += 1;
157
                        bytes -= 1;
158
                    } else {
159
                        if (state->count > 0) {
160
                            if (bytes < 2 + padding) {
161
                                return ptr - buf;
162
                            }
163
                            ptr[0] = 0xc0 | state->count;
164
                            ptr[1] = state->LAST;
165
                            ptr += 2;
166
                            bytes -= 2;
167
                        }
168
                    }
169
                    /* add the padding */
170
                    for (i = 0; i < padding; i++) {
171
                        ptr[0] = 0;
172
                        ptr += 1;
173
                        bytes -= 1;
174
                    }
175
                    /* reset for the next color plane. */
176
                    if (state->x < planes * bytes_per_line) {
177
                        state->count = 1;
178
                        state->LAST = state->buffer[state->x];
179
                        state->x += 1;
180
                    }
181
                } while (state->x < planes * bytes_per_line);
182

183
                /* read next line */
184
                state->state = FETCH;
185
                break;
186
        }
187
    }
188
}
189

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.