# 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: 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()


from PIL import Image
from luma.core.render import canvas
from luma.core.device import dummy
from helpers import get_reference_image, assert_identical_image
import baseline_data
    device.hide()
    device.show()
    device.cleanup()
    device.contrast(123)
    device.command(1, 2, 4, 4)
    device.data([1, 2, 4, 4])


def test_portrait():
    img_path = get_reference_image('portrait.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)

        device = dummy(rotate=1)

        # Use the same drawing primitives as the demo
        with canvas(device) as draw:
            baseline_data.primitives(device, draw)

        assert_identical_image(reference, device.image, img_path)


def test_dither():
    img_path = get_reference_image('dither.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)
        device = dummy(mode="1")


# 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: 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: tests/test_dummy_device.py
from PIL import Image
from luma.core.render import canvas
from luma.core.device import dummy
from helpers import get_reference_image, assert_identical_image
import baseline_data

    device.hide()
    device.show()
    device.cleanup()
    device.contrast(123)
    device.command(1, 2, 4, 4)
    device.data([1, 2, 4, 4])


def test_portrait():
    img_path = get_reference_image('portrait.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)

        device = dummy(rotate=1)

        # Use the same drawing primitives as the demo
        with canvas(device) as draw:
            baseline_data.primitives(device, draw)

        assert_identical_image(reference, device.image, img_path)


def test_dither():
    img_path = get_reference_image('dither.png')

    with open(img_path, 'rb') as p:
        reference = Image.open(p)
        device = dummy(mode="1")


        with canvas(device, dither=True) as draw: