forked from python-pillow/Pillow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_image_access.py
396 lines (319 loc) · 11.8 KB
/
test_image_access.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import sys
from PIL import Image
from .helper import PillowTestCase, hopper, on_ci, unittest
# CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2
# https://github.com/eliben/pycparser/pull/198#issuecomment-317001670
if os.environ.get("PYTHONOPTIMIZE") == "2":
cffi = None
else:
try:
from PIL import PyAccess
import cffi
except ImportError:
cffi = None
class AccessTest(PillowTestCase):
# initial value
_init_cffi_access = Image.USE_CFFI_ACCESS
_need_cffi_access = False
@classmethod
def setUpClass(cls):
Image.USE_CFFI_ACCESS = cls._need_cffi_access
@classmethod
def tearDownClass(cls):
Image.USE_CFFI_ACCESS = cls._init_cffi_access
class TestImagePutPixel(AccessTest):
def test_sanity(self):
im1 = hopper()
im2 = Image.new(im1.mode, im1.size, 0)
for y in range(im1.size[1]):
for x in range(im1.size[0]):
pos = x, y
im2.putpixel(pos, im1.getpixel(pos))
self.assert_image_equal(im1, im2)
im2 = Image.new(im1.mode, im1.size, 0)
im2.readonly = 1
for y in range(im1.size[1]):
for x in range(im1.size[0]):
pos = x, y
im2.putpixel(pos, im1.getpixel(pos))
self.assertFalse(im2.readonly)
self.assert_image_equal(im1, im2)
im2 = Image.new(im1.mode, im1.size, 0)
pix1 = im1.load()
pix2 = im2.load()
for y in range(im1.size[1]):
for x in range(im1.size[0]):
pix2[x, y] = pix1[x, y]
self.assert_image_equal(im1, im2)
def test_sanity_negative_index(self):
im1 = hopper()
im2 = Image.new(im1.mode, im1.size, 0)
width, height = im1.size
self.assertEqual(im1.getpixel((0, 0)), im1.getpixel((-width, -height)))
self.assertEqual(im1.getpixel((-1, -1)), im1.getpixel((width - 1, height - 1)))
for y in range(-1, -im1.size[1] - 1, -1):
for x in range(-1, -im1.size[0] - 1, -1):
pos = x, y
im2.putpixel(pos, im1.getpixel(pos))
self.assert_image_equal(im1, im2)
im2 = Image.new(im1.mode, im1.size, 0)
im2.readonly = 1
for y in range(-1, -im1.size[1] - 1, -1):
for x in range(-1, -im1.size[0] - 1, -1):
pos = x, y
im2.putpixel(pos, im1.getpixel(pos))
self.assertFalse(im2.readonly)
self.assert_image_equal(im1, im2)
im2 = Image.new(im1.mode, im1.size, 0)
pix1 = im1.load()
pix2 = im2.load()
for y in range(-1, -im1.size[1] - 1, -1):
for x in range(-1, -im1.size[0] - 1, -1):
pix2[x, y] = pix1[x, y]
self.assert_image_equal(im1, im2)
class TestImageGetPixel(AccessTest):
@staticmethod
def color(mode):
bands = Image.getmodebands(mode)
if bands == 1:
return 1
else:
return tuple(range(1, bands + 1))
def check(self, mode, c=None):
if not c:
c = self.color(mode)
# check putpixel
im = Image.new(mode, (1, 1), None)
im.putpixel((0, 0), c)
self.assertEqual(
im.getpixel((0, 0)),
c,
"put/getpixel roundtrip failed for mode %s, color %s" % (mode, c),
)
# check putpixel negative index
im.putpixel((-1, -1), c)
self.assertEqual(
im.getpixel((-1, -1)),
c,
"put/getpixel roundtrip negative index failed"
" for mode %s, color %s" % (mode, c),
)
# Check 0
im = Image.new(mode, (0, 0), None)
with self.assertRaises(IndexError):
im.putpixel((0, 0), c)
with self.assertRaises(IndexError):
im.getpixel((0, 0))
# Check 0 negative index
with self.assertRaises(IndexError):
im.putpixel((-1, -1), c)
with self.assertRaises(IndexError):
im.getpixel((-1, -1))
# check initial color
im = Image.new(mode, (1, 1), c)
self.assertEqual(
im.getpixel((0, 0)),
c,
"initial color failed for mode %s, color %s " % (mode, c),
)
# check initial color negative index
self.assertEqual(
im.getpixel((-1, -1)),
c,
"initial color failed with negative index"
"for mode %s, color %s " % (mode, c),
)
# Check 0
im = Image.new(mode, (0, 0), c)
with self.assertRaises(IndexError):
im.getpixel((0, 0))
# Check 0 negative index
with self.assertRaises(IndexError):
im.getpixel((-1, -1))
def test_basic(self):
for mode in (
"1",
"L",
"LA",
"I",
"I;16",
"I;16B",
"F",
"P",
"PA",
"RGB",
"RGBA",
"RGBX",
"CMYK",
"YCbCr",
):
self.check(mode)
def test_signedness(self):
# see https://github.com/python-pillow/Pillow/issues/452
# pixelaccess is using signed int* instead of uint*
for mode in ("I;16", "I;16B"):
self.check(mode, 2 ** 15 - 1)
self.check(mode, 2 ** 15)
self.check(mode, 2 ** 15 + 1)
self.check(mode, 2 ** 16 - 1)
def test_p_putpixel_rgb_rgba(self):
for color in [(255, 0, 0), (255, 0, 0, 255)]:
im = Image.new("P", (1, 1), 0)
im.putpixel((0, 0), color)
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
@unittest.skipIf(cffi is None, "No cffi")
class TestCffiPutPixel(TestImagePutPixel):
_need_cffi_access = True
@unittest.skipIf(cffi is None, "No cffi")
class TestCffiGetPixel(TestImageGetPixel):
_need_cffi_access = True
@unittest.skipIf(cffi is None, "No cffi")
class TestCffi(AccessTest):
_need_cffi_access = True
def _test_get_access(self, im):
"""Do we get the same thing as the old pixel access
Using private interfaces, forcing a capi access and
a pyaccess for the same image"""
caccess = im.im.pixel_access(False)
access = PyAccess.new(im, False)
w, h = im.size
for x in range(0, w, 10):
for y in range(0, h, 10):
self.assertEqual(access[(x, y)], caccess[(x, y)])
# Access an out-of-range pixel
self.assertRaises(
ValueError, lambda: access[(access.xsize + 1, access.ysize + 1)]
)
def test_get_vs_c(self):
rgb = hopper("RGB")
rgb.load()
self._test_get_access(rgb)
self._test_get_access(hopper("RGBA"))
self._test_get_access(hopper("L"))
self._test_get_access(hopper("LA"))
self._test_get_access(hopper("1"))
self._test_get_access(hopper("P"))
# self._test_get_access(hopper('PA')) # PA -- how do I make a PA image?
self._test_get_access(hopper("F"))
im = Image.new("I;16", (10, 10), 40000)
self._test_get_access(im)
im = Image.new("I;16L", (10, 10), 40000)
self._test_get_access(im)
im = Image.new("I;16B", (10, 10), 40000)
self._test_get_access(im)
im = Image.new("I", (10, 10), 40000)
self._test_get_access(im)
# These don't actually appear to be modes that I can actually make,
# as unpack sets them directly into the I mode.
# im = Image.new('I;32L', (10, 10), -2**10)
# self._test_get_access(im)
# im = Image.new('I;32B', (10, 10), 2**10)
# self._test_get_access(im)
def _test_set_access(self, im, color):
"""Are we writing the correct bits into the image?
Using private interfaces, forcing a capi access and
a pyaccess for the same image"""
caccess = im.im.pixel_access(False)
access = PyAccess.new(im, False)
w, h = im.size
for x in range(0, w, 10):
for y in range(0, h, 10):
access[(x, y)] = color
self.assertEqual(color, caccess[(x, y)])
# Attempt to set the value on a read-only image
access = PyAccess.new(im, True)
with self.assertRaises(ValueError):
access[(0, 0)] = color
def test_set_vs_c(self):
rgb = hopper("RGB")
rgb.load()
self._test_set_access(rgb, (255, 128, 0))
self._test_set_access(hopper("RGBA"), (255, 192, 128, 0))
self._test_set_access(hopper("L"), 128)
self._test_set_access(hopper("LA"), (128, 128))
self._test_set_access(hopper("1"), 255)
self._test_set_access(hopper("P"), 128)
# self._test_set_access(i, (128, 128)) #PA -- undone how to make
self._test_set_access(hopper("F"), 1024.0)
im = Image.new("I;16", (10, 10), 40000)
self._test_set_access(im, 45000)
im = Image.new("I;16L", (10, 10), 40000)
self._test_set_access(im, 45000)
im = Image.new("I;16B", (10, 10), 40000)
self._test_set_access(im, 45000)
im = Image.new("I", (10, 10), 40000)
self._test_set_access(im, 45000)
# im = Image.new('I;32L', (10, 10), -(2**10))
# self._test_set_access(im, -(2**13)+1)
# im = Image.new('I;32B', (10, 10), 2**10)
# self._test_set_access(im, 2**13-1)
def test_not_implemented(self):
self.assertIsNone(PyAccess.new(hopper("BGR;15")))
# ref https://github.com/python-pillow/Pillow/pull/2009
def test_reference_counting(self):
size = 10
for _ in range(10):
# Do not save references to the image, only to the access object
px = Image.new("L", (size, 1), 0).load()
for i in range(size):
# pixels can contain garbage if image is released
self.assertEqual(px[i, 0], 0)
def test_p_putpixel_rgb_rgba(self):
for color in [(255, 0, 0), (255, 0, 0, 255)]:
im = Image.new("P", (1, 1), 0)
access = PyAccess.new(im, False)
access.putpixel((0, 0), color)
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (255, 0, 0))
class TestEmbeddable(unittest.TestCase):
@unittest.skipIf(
not sys.platform.startswith("win32") or on_ci(),
"Failing on AppVeyor / GitHub Actions when run from subprocess, not from shell",
)
def test_embeddable(self):
import subprocess
import ctypes
from distutils import ccompiler, sysconfig
with open("embed_pil.c", "w") as fh:
fh.write(
"""
#include "Python.h"
int main(int argc, char* argv[])
{
char *home = "%s";
#if PY_MAJOR_VERSION >= 3
wchar_t *whome = Py_DecodeLocale(home, NULL);
Py_SetPythonHome(whome);
#else
Py_SetPythonHome(home);
#endif
Py_InitializeEx(0);
Py_DECREF(PyImport_ImportModule("PIL.Image"));
Py_Finalize();
Py_InitializeEx(0);
Py_DECREF(PyImport_ImportModule("PIL.Image"));
Py_Finalize();
#if PY_MAJOR_VERSION >= 3
PyMem_RawFree(whome);
#endif
return 0;
}
"""
% sys.prefix.replace("\\", "\\\\")
)
compiler = ccompiler.new_compiler()
compiler.add_include_dir(sysconfig.get_python_inc())
libdir = sysconfig.get_config_var(
"LIBDIR"
) or sysconfig.get_python_inc().replace("include", "libs")
print(libdir)
compiler.add_library_dir(libdir)
objects = compiler.compile(["embed_pil.c"])
compiler.link_executable(objects, "embed_pil")
env = os.environ.copy()
env["PATH"] = sys.prefix + ";" + env["PATH"]
# do not display the Windows Error Reporting dialog
ctypes.windll.kernel32.SetErrorMode(0x0002)
process = subprocess.Popen(["embed_pil.exe"], env=env)
process.communicate()
self.assertEqual(process.returncode, 0)