-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathPcxDecode.c
More file actions
132 lines (117 loc) · 3.25 KB
/
Copy pathPcxDecode.c
File metadata and controls
132 lines (117 loc) · 3.25 KB
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
/*
* The Python Imaging Library.
* $Id$
*
* decoder for PCX image data.
*
* history:
* 95-09-14 fl Created
*
* Copyright (c) Fredrik Lundh 1995.
* Copyright (c) Secret Labs AB 1997.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
void
unpackP2L(UINT8 *out, const UINT8 *in, int pixels) {
int i, j, m, s;
/* bit layers */
m = 128;
s = (pixels + 7) / 8;
for (i = j = 0; i < pixels; i++) {
out[i] = ((in[j] & m) ? 1 : 0) + ((in[j + s] & m) ? 2 : 0);
if ((m >>= 1) == 0) {
m = 128;
j++;
}
}
}
void
unpackP4L(UINT8 *out, const UINT8 *in, int pixels) {
int i, j, m, s;
/* bit layers (trust the optimizer ;-) */
m = 128;
s = (pixels + 7) / 8;
for (i = j = 0; i < pixels; i++) {
out[i] = ((in[j] & m) ? 1 : 0) + ((in[j + s] & m) ? 2 : 0) +
((in[j + 2 * s] & m) ? 4 : 0) + ((in[j + 3 * s] & m) ? 8 : 0);
if ((m >>= 1) == 0) {
m = 128;
j++;
}
}
}
int
ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
UINT8 n;
UINT8 *ptr;
if ((state->xsize * state->bits + 7) / 8 > state->bytes) {
state->errcode = IMAGING_CODEC_OVERRUN;
return -1;
}
ptr = buf;
for (;;) {
if (bytes < 1) {
return ptr - buf;
}
if ((*ptr & 0xC0) == 0xC0) {
/* Run */
if (bytes < 2) {
return ptr - buf;
}
n = ptr[0] & 0x3F;
while (n > 0) {
if (state->x >= state->bytes) {
state->errcode = IMAGING_CODEC_OVERRUN;
break;
}
state->buffer[state->x++] = ptr[1];
n--;
}
ptr += 2;
bytes -= 2;
} else {
/* Literal */
state->buffer[state->x++] = ptr[0];
ptr++;
bytes--;
}
if (state->x >= state->bytes) {
int bands;
int xsize = 0;
int stride = 0;
if (state->bits == 2 || state->bits == 4) {
xsize = (state->xsize + 7) / 8;
bands = state->bits;
stride = state->bytes / state->bits;
} else {
xsize = state->xsize;
bands = state->bytes / state->xsize;
if (bands != 0) {
stride = state->bytes / bands;
}
}
if (stride > xsize) {
int i;
for (i = 1; i < bands; i++) { // note -- skipping first band
memmove(
&state->buffer[i * xsize], &state->buffer[i * stride], xsize
);
}
}
/* Got a full line, unpack it */
state->shuffle(
(UINT8 *)im->image[state->y + state->yoff] +
state->xoff * im->pixelsize,
state->buffer,
state->xsize
);
state->x = 0;
if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */
return -1;
}
}
}
}