mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Save test_cffi for another day
This commit is contained in:
parent
2833cb42b4
commit
07aa1a56bb
99
Tests/test_cffi.py
Normal file
99
Tests/test_cffi.py
Normal file
|
@ -0,0 +1,99 @@
|
|||
from tester import *
|
||||
|
||||
try:
|
||||
import cffi
|
||||
except:
|
||||
skip()
|
||||
|
||||
from PIL import Image, PyAccess
|
||||
|
||||
import test_image_putpixel as put
|
||||
import test_image_getpixel as get
|
||||
|
||||
|
||||
Image.USE_CFFI_ACCESS = True
|
||||
|
||||
def test_put():
|
||||
put.test_sanity()
|
||||
|
||||
def test_get():
|
||||
get.test_basic()
|
||||
get.test_signedness()
|
||||
|
||||
def _test_get_access(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):
|
||||
assert_equal(access[(x,y)], caccess[(x,y)])
|
||||
|
||||
def test_get_vs_c():
|
||||
_test_get_access(lena('RGB'))
|
||||
_test_get_access(lena('RGBA'))
|
||||
_test_get_access(lena('L'))
|
||||
_test_get_access(lena('LA'))
|
||||
_test_get_access(lena('1'))
|
||||
_test_get_access(lena('P'))
|
||||
#_test_get_access(lena('PA')) # PA -- how do I make a PA image???
|
||||
_test_get_access(lena('F'))
|
||||
|
||||
im = Image.new('I;16', (10,10), 40000)
|
||||
_test_get_access(im)
|
||||
im = Image.new('I;16L', (10,10), 40000)
|
||||
_test_get_access(im)
|
||||
im = Image.new('I;16B', (10,10), 40000)
|
||||
_test_get_access(im)
|
||||
|
||||
im = Image.new('I', (10,10), 40000)
|
||||
_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)
|
||||
#_test_get_access(im)
|
||||
#im = Image.new('I;32B', (10,10), 2**10)
|
||||
#_test_get_access(im)
|
||||
|
||||
|
||||
|
||||
def _test_set_access(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
|
||||
assert_equal(color, caccess[(x,y)])
|
||||
|
||||
def test_set_vs_c():
|
||||
_test_set_access(lena('RGB'), (255, 128,0) )
|
||||
_test_set_access(lena('RGBA'), (255, 192, 128, 0))
|
||||
_test_set_access(lena('L'), 128)
|
||||
_test_set_access(lena('LA'), (128,128))
|
||||
_test_set_access(lena('1'), 255)
|
||||
_test_set_access(lena('P') , 128)
|
||||
##_test_set_access(i, (128,128)) #PA -- undone how to make
|
||||
_test_set_access(lena('F'), 1024.0)
|
||||
|
||||
im = Image.new('I;16', (10,10), 40000)
|
||||
_test_set_access(im, 45000)
|
||||
im = Image.new('I;16L', (10,10), 40000)
|
||||
_test_set_access(im, 45000)
|
||||
im = Image.new('I;16B', (10,10), 40000)
|
||||
_test_set_access(im, 45000)
|
||||
|
||||
|
||||
im = Image.new('I', (10,10), 40000)
|
||||
_test_set_access(im, 45000)
|
||||
# im = Image.new('I;32L', (10,10), -(2**10))
|
||||
# _test_set_access(im, -(2**13)+1)
|
||||
#im = Image.new('I;32B', (10,10), 2**10)
|
||||
#_test_set_access(im, 2**13-1)
|
49
Tests/test_image_getpixel.py
Normal file
49
Tests/test_image_getpixel.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
from tester import *
|
||||
|
||||
from PIL import Image
|
||||
|
||||
Image.USE_CFFI_ACCESS=False
|
||||
|
||||
def color(mode):
|
||||
bands = Image.getmodebands(mode)
|
||||
if bands == 1:
|
||||
return 1
|
||||
else:
|
||||
return tuple(range(1, bands+1))
|
||||
|
||||
|
||||
|
||||
def check(mode, c=None):
|
||||
if not c:
|
||||
c = color(mode)
|
||||
|
||||
#check putpixel
|
||||
im = Image.new(mode, (1, 1), None)
|
||||
im.putpixel((0, 0), c)
|
||||
assert_equal(im.getpixel((0, 0)), c,
|
||||
"put/getpixel roundtrip failed for mode %s, color %s" %
|
||||
(mode, c))
|
||||
|
||||
# check inital color
|
||||
im = Image.new(mode, (1, 1), c)
|
||||
assert_equal(im.getpixel((0, 0)), c,
|
||||
"initial color failed for mode %s, color %s " %
|
||||
(mode, color))
|
||||
|
||||
def test_basic():
|
||||
for mode in ("1", "L", "LA", "I", "I;16", "I;16B", "F",
|
||||
"P", "PA", "RGB", "RGBA", "RGBX", "CMYK","YCbCr"):
|
||||
check(mode)
|
||||
|
||||
def test_signedness():
|
||||
# see https://github.com/python-pillow/Pillow/issues/452
|
||||
# pixelaccess is using signed int* instead of uint*
|
||||
for mode in ("I;16", "I;16B"):
|
||||
check(mode, 2**15-1)
|
||||
check(mode, 2**15)
|
||||
check(mode, 2**15+1)
|
||||
check(mode, 2**16-1)
|
||||
|
||||
|
||||
|
||||
|
45
Tests/test_image_putpixel.py
Normal file
45
Tests/test_image_putpixel.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
from tester import *
|
||||
|
||||
from PIL import Image
|
||||
|
||||
Image.USE_CFFI_ACCESS=False
|
||||
|
||||
def test_sanity():
|
||||
|
||||
im1 = lena()
|
||||
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))
|
||||
|
||||
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))
|
||||
|
||||
assert_false(im2.readonly)
|
||||
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]
|
||||
|
||||
assert_image_equal(im1, im2)
|
||||
|
||||
|
||||
|
||||
|
||||
# see test_image_getpixel for more tests
|
||||
|
|
@ -1,113 +0,0 @@
|
|||
from helper import unittest, PillowTestCase, lena
|
||||
|
||||
try:
|
||||
import cffi
|
||||
|
||||
from PIL import Image, PyAccess
|
||||
|
||||
from test_image_putpixel import TestImagePutPixel
|
||||
from test_image_getpixel import TestImageGetPixel
|
||||
|
||||
Image.USE_CFFI_ACCESS = True
|
||||
|
||||
class TestCffiPutPixel(TestImagePutPixel):
|
||||
|
||||
def test_put(self):
|
||||
self.test_sanity()
|
||||
|
||||
class TestCffiGetPixel(TestImageGetPixel):
|
||||
|
||||
def test_get(self):
|
||||
self.test_basic()
|
||||
self.test_signedness()
|
||||
|
||||
class TestCffi(PillowTestCase):
|
||||
|
||||
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)])
|
||||
|
||||
def test_get_vs_c(self):
|
||||
self._test_get_access(lena('RGB'))
|
||||
self._test_get_access(lena('RGBA'))
|
||||
self._test_get_access(lena('L'))
|
||||
self._test_get_access(lena('LA'))
|
||||
self._test_get_access(lena('1'))
|
||||
self._test_get_access(lena('P'))
|
||||
# PA -- how do I make a PA image???
|
||||
# self._test_get_access(lena('PA'))
|
||||
self._test_get_access(lena('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)])
|
||||
|
||||
def test_set_vs_c(self):
|
||||
self._test_set_access(lena('RGB'), (255, 128, 0))
|
||||
self._test_set_access(lena('RGBA'), (255, 192, 128, 0))
|
||||
self._test_set_access(lena('L'), 128)
|
||||
self._test_set_access(lena('LA'), (128, 128))
|
||||
self._test_set_access(lena('1'), 255)
|
||||
self._test_set_access(lena('P'), 128)
|
||||
# self._test_set_access(i, (128, 128)) #PA -- undone how to make
|
||||
self._test_set_access(lena('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)
|
||||
|
||||
except ImportError:
|
||||
class TestCffi(PillowTestCase):
|
||||
def test_skip(self):
|
||||
self.skipTest("ImportError")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# End of file
|
|
@ -1,54 +0,0 @@
|
|||
from helper import unittest, PillowTestCase
|
||||
|
||||
from PIL import Image
|
||||
|
||||
Image.USE_CFFI_ACCESS = False
|
||||
|
||||
|
||||
class TestImageGetPixel(PillowTestCase):
|
||||
|
||||
def color(self, 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 inital color
|
||||
im = Image.new(mode, (1, 1), c)
|
||||
self.assertEqual(
|
||||
im.getpixel((0, 0)), c,
|
||||
"initial color failed for mode %s, color %s " %
|
||||
(mode, self.color))
|
||||
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# End of file
|
|
@ -1,50 +0,0 @@
|
|||
from helper import unittest, PillowTestCase, lena
|
||||
|
||||
from PIL import Image
|
||||
|
||||
Image.USE_CFFI_ACCESS = False
|
||||
|
||||
|
||||
class TestImagePutPixel(PillowTestCase):
|
||||
|
||||
def test_sanity(self):
|
||||
|
||||
im1 = lena()
|
||||
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)
|
||||
|
||||
# see test_image_getpixel for more tests
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# End of file
|
Loading…
Reference in New Issue
Block a user