Skip to content

Commit 0ddbfc3

Browse files
committed
Added BGR;15, BGR;16 and BGR;24 access
1 parent 3c5324b commit 0ddbfc3

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

Tests/test_image_access.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,17 @@ def color(mode):
130130
bands = Image.getmodebands(mode)
131131
if bands == 1:
132132
return 1
133+
if mode in ("BGR;15", "BGR;16"):
134+
# These modes have less than 8 bits per band
135+
# So (1, 2, 3) cannot be roundtripped
136+
return (16, 32, 49)
133137
return tuple(range(1, bands + 1))
134138

135139
def check(self, mode, expected_color=None):
140+
if self._need_cffi_access and mode.startswith("BGR;"):
141+
pytest.skip("Support not added to deprecated module for BGR;* modes")
142+
return
143+
136144
if not expected_color:
137145
expected_color = self.color(mode)
138146

@@ -203,6 +211,9 @@ def check(self, mode, expected_color=None):
203211
"F",
204212
"P",
205213
"PA",
214+
"BGR;15",
215+
"BGR;16",
216+
"BGR;24",
206217
"RGB",
207218
"RGBA",
208219
"RGBX",

src/_imaging.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,10 @@ getpixel(Imaging im, ImagingAccess access, int x, int y) {
475475
case IMAGING_TYPE_FLOAT32:
476476
return PyFloat_FromDouble(pixel.f);
477477
case IMAGING_TYPE_SPECIAL:
478-
if (strncmp(im->mode, "I;16", 4) == 0) {
478+
if (im->bands == 1) {
479479
return PyLong_FromLong(pixel.h);
480+
} else {
481+
return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]);
480482
}
481483
break;
482484
}
@@ -599,7 +601,7 @@ getink(PyObject *color, Imaging im, char *ink) {
599601
} else if (tupleSize != 3) {
600602
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or three elements");
601603
return NULL;
602-
} else if (!PyArg_ParseTuple(color, "Lii", &r, &g, &b)) {
604+
} else if (!PyArg_ParseTuple(color, "iiL", &b, &g, &r)) {
603605
return NULL;
604606
}
605607
if (!strcmp(im->mode, "BGR;15")) {

src/libImaging/Access.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "Imaging.h"
1313

1414
/* use make_hash.py from the pillow-scripts repository to calculate these values */
15-
#define ACCESS_TABLE_SIZE 27
16-
#define ACCESS_TABLE_HASH 33051
15+
#define ACCESS_TABLE_SIZE 35
16+
#define ACCESS_TABLE_HASH 8940
1717

1818
static struct ImagingAccessInstance access_table[ACCESS_TABLE_SIZE];
1919

@@ -87,6 +87,31 @@ get_pixel_16(Imaging im, int x, int y, void *color) {
8787
memcpy(color, in, sizeof(UINT16));
8888
}
8989

90+
static void
91+
get_pixel_BGR15(Imaging im, int x, int y, void *color) {
92+
UINT8 *in = (UINT8 *)&im->image8[y][x * 2];
93+
UINT16 pixel = in[0] + (in[1] << 8);
94+
char *out = color;
95+
out[0] = (pixel & 31) * 255 / 31;
96+
out[1] = ((pixel >> 5) & 31) * 255 / 31;
97+
out[2] = ((pixel >> 10) & 31) * 255 / 31;
98+
}
99+
100+
static void
101+
get_pixel_BGR16(Imaging im, int x, int y, void *color) {
102+
UINT8 *in = (UINT8 *)&im->image8[y][x * 2];
103+
UINT16 pixel = in[0] + (in[1] << 8);
104+
char *out = color;
105+
out[0] = (pixel & 31) * 255 / 31;
106+
out[1] = ((pixel >> 5) & 63) * 255 / 63;
107+
out[2] = ((pixel >> 11) & 31) * 255 / 31;
108+
}
109+
110+
static void
111+
get_pixel_BGR24(Imaging im, int x, int y, void *color) {
112+
memcpy(color, &im->image8[y][x * 3], sizeof(UINT8) * 3);
113+
}
114+
90115
static void
91116
get_pixel_32(Imaging im, int x, int y, void *color) {
92117
memcpy(color, &im->image32[y][x], sizeof(INT32));
@@ -134,6 +159,16 @@ put_pixel_16B(Imaging im, int x, int y, const void *color) {
134159
out[1] = in[0];
135160
}
136161

162+
static void
163+
put_pixel_BGR1516(Imaging im, int x, int y, const void *color) {
164+
memcpy(&im->image8[y][x * 2], color, 2);
165+
}
166+
167+
static void
168+
put_pixel_BGR24(Imaging im, int x, int y, const void *color) {
169+
memcpy(&im->image8[y][x * 3], color, 3);
170+
}
171+
137172
static void
138173
put_pixel_32L(Imaging im, int x, int y, const void *color) {
139174
memcpy(&im->image8[y][x * 4], color, 4);
@@ -178,6 +213,9 @@ ImagingAccessInit() {
178213
ADD("F", get_pixel_32, put_pixel_32);
179214
ADD("P", get_pixel_8, put_pixel_8);
180215
ADD("PA", get_pixel_32_2bands, put_pixel_32);
216+
ADD("BGR;15", get_pixel_BGR15, put_pixel_BGR1516);
217+
ADD("BGR;16", get_pixel_BGR16, put_pixel_BGR1516);
218+
ADD("BGR;24", get_pixel_BGR24, put_pixel_BGR24);
181219
ADD("RGB", get_pixel_32, put_pixel_32);
182220
ADD("RGBA", get_pixel_32, put_pixel_32);
183221
ADD("RGBa", get_pixel_32, put_pixel_32);

0 commit comments

Comments
 (0)