Skip to content

Commit

Permalink
Merge pull request #781 from hugovk/ImageWin
Browse files Browse the repository at this point in the history
More tests for ImageWin
  • Loading branch information
wiredfool committed Sep 23, 2014
2 parents 4c7e20d + 165d276 commit f924cfc
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 13 deletions.
20 changes: 10 additions & 10 deletions PIL/ImageWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@

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

Expand Down Expand Up @@ -79,13 +82,12 @@ def __init__(self, image, size=None):
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):
Expand All @@ -109,7 +111,7 @@ def draw(self, handle, dst, src=None):
necessary.
"""
if not src:
src = (0,0) + self.size
src = (0, 0) + self.size
if isinstance(handle, HWND):
dc = self.image.getdc(handle)
try:
Expand All @@ -120,7 +122,6 @@ def draw(self, handle, dst, src=None):
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
Expand All @@ -146,7 +147,6 @@ def query_palette(self, handle):
result = self.image.query_palette(handle)
return result


def paste(self, im, box=None):
"""
Paste a PIL image into the bitmap image.
Expand All @@ -166,7 +166,6 @@ def paste(self, im, box=None):
else:
self.image.paste(im.im)


def frombytes(self, buffer):
"""
Load display memory contents from byte data.
Expand All @@ -176,7 +175,6 @@ def frombytes(self, buffer):
"""
return self.image.frombytes(buffer)


def tobytes(self):
"""
Copy display memory contents to bytes object.
Expand Down Expand Up @@ -204,6 +202,7 @@ def tostring(self):
)
return self.tobytes()


##
# Create a Window with the given title size.

Expand Down Expand Up @@ -235,6 +234,7 @@ def ui_handle_resize(self, width, height):
def mainloop(self):
Image.core.eventloop()


##
# Create an image window which displays the given image.

Expand Down
113 changes: 110 additions & 3 deletions Tests/test_imagewin.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit f924cfc

Please sign in to comment.