# Path: luma/core/device.py
# class dummy(device):
#     """
#     Pseudo-device that acts like a physical display, except that it does nothing
#     other than retain a copy of the displayed image. It is mostly useful for
#     testing. Supports 24-bit color depth.
#     """
# 
#     def __init__(self, width=128, height=64, rotate=0, mode="RGB", **kwargs):
#         super(dummy, self).__init__(serial_interface=noop())
#         self.capabilities(width, height, rotate, mode)
#         self.image = None
# 
#     def display(self, image):
#         """
#         Takes a :py:mod:`PIL.Image` and makes a copy of it for later
#         use/inspection.
# 
#         :param image: Image to display.
#         :type image: PIL.Image.Image
#         """
#         assert image.size == self.size
# 
#         self.image = self.preprocess(image).copy()
# 
# Path: luma/core/render.py
# class canvas(object):
#     """
#     A canvas returns a properly-sized :py:mod:`PIL.ImageDraw` object onto
#     which the caller can draw upon. As soon as the with-block completes, the
#     resultant image is flushed onto the device.
# 
#     By default, any color (other than black) will be `generally` treated as
#     white when displayed on monochrome devices. However, this behaviour can be
#     changed by adding ``dither=True`` and the image will be converted from RGB
#     space into a 1-bit monochrome image where dithering is employed to
#     differentiate colors at the expense of resolution.
#     If a ``background`` parameter is provided, the canvas is based on the given
#     background. This is useful to e.g. write text on a given background image.
#     """
#     def __init__(self, device, background=None, dither=False):
#         self.draw = None
#         if background is None:
#             self.image = Image.new("RGB" if dither else device.mode, device.size)
#         else:
#             assert(background.size == device.size)
#             self.image = background.copy()
#         self.device = device
#         self.dither = dither
# 
#     def __enter__(self):
#         self.draw = ImageDraw.Draw(self.image)
#         return self.draw
# 
#     def __exit__(self, type, value, traceback):
#         if type is None:
# 
#             if self.dither:
#                 self.image = self.image.convert(self.device.mode)
# 
#             # do the drawing onto the device
#             self.device.display(self.image)
# 
#         del self.draw   # Tidy up the resources
#         return False    # Never suppress exceptions


import pytest
from PIL import Image
from luma.core.device import dummy
from luma.core.render import canvas
from helpers import get_reference_image, assert_identical_image
# See LICENSE.rst for details.

"""
Tests for the :py:class:`luma.core.render.canvas` class.
"""






def test_canvas_background():
    img_path = get_reference_image('background.png')
    with open(get_reference_image('dither.png'), 'rb') as p1:
        with open(img_path, 'rb') as p2:
            bgnd = Image.open(p1)
            reference = Image.open(p2)
            device = dummy()

            with canvas(device, background=bgnd) as draw:
                draw.rectangle((20, 15, device.width - 20, device.height - 15),
                               fill='black', outline='white')
                draw.text((36, 22), 'Background', fill='white')
                draw.text((52, 32), 'Test', fill='white')

            assert_identical_image(reference, device.image, img_path)


def test_canvas_wrong_size():
    with pytest.raises(AssertionError):

# Path: luma/core/device.py
# class dummy(device):
#     """
#     Pseudo-device that acts like a physical display, except that it does nothing
#     other than retain a copy of the displayed image. It is mostly useful for
#     testing. Supports 24-bit color depth.
#     """
# 
#     def __init__(self, width=128, height=64, rotate=0, mode="RGB", **kwargs):
#         super(dummy, self).__init__(serial_interface=noop())
#         self.capabilities(width, height, rotate, mode)
#         self.image = None
# 
#     def display(self, image):
#         """
#         Takes a :py:mod:`PIL.Image` and makes a copy of it for later
#         use/inspection.
# 
#         :param image: Image to display.
#         :type image: PIL.Image.Image
#         """
#         assert image.size == self.size
# 
#         self.image = self.preprocess(image).copy()
# 
# Path: luma/core/render.py
# class canvas(object):
#     """
#     A canvas returns a properly-sized :py:mod:`PIL.ImageDraw` object onto
#     which the caller can draw upon. As soon as the with-block completes, the
#     resultant image is flushed onto the device.
# 
#     By default, any color (other than black) will be `generally` treated as
#     white when displayed on monochrome devices. However, this behaviour can be
#     changed by adding ``dither=True`` and the image will be converted from RGB
#     space into a 1-bit monochrome image where dithering is employed to
#     differentiate colors at the expense of resolution.
#     If a ``background`` parameter is provided, the canvas is based on the given
#     background. This is useful to e.g. write text on a given background image.
#     """
#     def __init__(self, device, background=None, dither=False):
#         self.draw = None
#         if background is None:
#             self.image = Image.new("RGB" if dither else device.mode, device.size)
#         else:
#             assert(background.size == device.size)
#             self.image = background.copy()
#         self.device = device
#         self.dither = dither
# 
#     def __enter__(self):
#         self.draw = ImageDraw.Draw(self.image)
#         return self.draw
# 
#     def __exit__(self, type, value, traceback):
#         if type is None:
# 
#             if self.dither:
#                 self.image = self.image.convert(self.device.mode)
# 
#             # do the drawing onto the device
#             self.device.display(self.image)
# 
#         del self.draw   # Tidy up the resources
#         return False    # Never suppress exceptions


# Path: tests/test_canvas.py
import pytest
from PIL import Image
from luma.core.device import dummy
from luma.core.render import canvas
from helpers import get_reference_image, assert_identical_image

# See LICENSE.rst for details.

"""
Tests for the :py:class:`luma.core.render.canvas` class.
"""






def test_canvas_background():
    img_path = get_reference_image('background.png')
    with open(get_reference_image('dither.png'), 'rb') as p1:
        with open(img_path, 'rb') as p2:
            bgnd = Image.open(p1)
            reference = Image.open(p2)
            device = dummy()

            with canvas(device, background=bgnd) as draw:
                draw.rectangle((20, 15, device.width - 20, device.height - 15),
                               fill='black', outline='white')
                draw.text((36, 22), 'Background', fill='white')
                draw.text((52, 32), 'Test', fill='white')

            assert_identical_image(reference, device.image, img_path)


def test_canvas_wrong_size():
    with pytest.raises(AssertionError):

        canvas(dummy(), background=Image.new('RGB', (23, 97)))