Merge pull request #781 from hugovk/ImageWin

More tests for ImageWin
This commit is contained in:
wiredfool 2014-09-22 17:35:21 -07:00
commit f924cfc6f5
2 changed files with 120 additions and 13 deletions

View File

@ -23,23 +23,26 @@ from PIL import Image
class HDC:
"""
Wraps a HDC integer. The resulting object can be passed to the
Wraps an HDC integer. The resulting object can be passed to the
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
methods.
"""
def __init__(self, dc):
self.dc = dc
def __int__(self):
return self.dc
class HWND:
"""
Wraps a HWND integer. The resulting object can be passed to the
Wraps an HWND integer. The resulting object can be passed to the
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
methods, instead of a DC.
"""
def __init__(self, wnd):
self.wnd = wnd
def __int__(self):
return self.wnd
@ -79,13 +82,12 @@ class Dib:
if image:
self.paste(image)
def expose(self, handle):
"""
Copy the bitmap contents to a device context.
:param handle: Device context (HDC), cast to a Python integer, or a HDC
or HWND instance. In PythonWin, you can use the
:param handle: Device context (HDC), cast to a Python integer, or an
HDC or HWND instance. In PythonWin, you can use the
:py:meth:`CDC.GetHandleAttrib` to get a suitable handle.
"""
if isinstance(handle, HWND):
@ -109,7 +111,7 @@ class Dib:
necessary.
"""
if not src:
src = (0,0) + self.size
src = (0, 0) + self.size
if isinstance(handle, HWND):
dc = self.image.getdc(handle)
try:
@ -120,7 +122,6 @@ class Dib:
result = self.image.draw(handle, dst, src)
return result
def query_palette(self, handle):
"""
Installs the palette associated with the image in the given device
@ -146,7 +147,6 @@ class Dib:
result = self.image.query_palette(handle)
return result
def paste(self, im, box=None):
"""
Paste a PIL image into the bitmap image.
@ -166,7 +166,6 @@ class Dib:
else:
self.image.paste(im.im)
def frombytes(self, buffer):
"""
Load display memory contents from byte data.
@ -176,7 +175,6 @@ class Dib:
"""
return self.image.frombytes(buffer)
def tobytes(self):
"""
Copy display memory contents to bytes object.
@ -204,6 +202,7 @@ class Dib:
)
return self.tobytes()
##
# Create a Window with the given title size.
@ -235,6 +234,7 @@ class Window:
def mainloop(self):
Image.core.eventloop()
##
# Create an image window which displays the given image.

View File

@ -1,16 +1,123 @@
from helper import unittest, PillowTestCase, lena
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import ImageWin
import sys
class TestImageWin(PillowTestCase):
def test_sanity(self):
dir(Image)
dir(ImageWin)
pass
def test_hdc(self):
# Arrange
dc = 50
# Act
hdc = ImageWin.HDC(dc)
dc2 = int(hdc)
# Assert
self.assertEqual(dc2, 50)
def test_hwnd(self):
# Arrange
wnd = 50
# Act
hwnd = ImageWin.HWND(wnd)
wnd2 = int(hwnd)
# Assert
self.assertEqual(wnd2, 50)
@unittest.skipUnless(sys.platform.startswith('win32'), "Windows only")
class TestImageWinDib(PillowTestCase):
def test_dib_image(self):
# Arrange
im = hopper()
# Act
dib = ImageWin.Dib(im)
# Assert
self.assertEqual(dib.size, im.size)
def test_dib_mode_string(self):
# Arrange
mode = "RGBA"
size = (128, 128)
# Act
dib = ImageWin.Dib(mode, size)
# Assert
self.assertEqual(dib.size, (128, 128))
def test_dib_paste(self):
# Arrange
im = hopper()
mode = "RGBA"
size = (128, 128)
dib = ImageWin.Dib(mode, size)
# Act
dib.paste(im)
# Assert
self.assertEqual(dib.size, (128, 128))
def test_dib_paste_bbox(self):
# Arrange
im = hopper()
bbox = (0, 0, 10, 10)
mode = "RGBA"
size = (128, 128)
dib = ImageWin.Dib(mode, size)
# Act
dib.paste(im, bbox)
# Assert
self.assertEqual(dib.size, (128, 128))
def test_dib_frombytes_tobytes_roundtrip(self):
# Arrange
# Make two different DIB images
im = hopper()
dib1 = ImageWin.Dib(im)
mode = "RGB"
size = (128, 128)
dib2 = ImageWin.Dib(mode, size)
# Confirm they're different
self.assertNotEqual(dib1.tobytes(), dib2.tobytes())
# Act
# Make one the same as the using tobytes()/frombytes()
buffer = dib1.tobytes()
dib2.frombytes(buffer)
# Assert
# Confirm they're the same
self.assertEqual(dib1.tobytes(), dib2.tobytes())
def test_dib_fromstring_tostring_deprecated(self):
# Arrange
im = hopper()
dib = ImageWin.Dib(im)
buffer = dib.tobytes()
# Act/Assert
self.assert_warning(DeprecationWarning, lambda: dib.tostring())
self.assert_warning(DeprecationWarning, lambda: dib.fromstring(buffer))
if __name__ == '__main__':
unittest.main()