You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.0 KiB

4 years ago
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # a Windows DIB display interface
  6. #
  7. # History:
  8. # 1996-05-20 fl Created
  9. # 1996-09-20 fl Fixed subregion exposure
  10. # 1997-09-21 fl Added draw primitive (for tzPrint)
  11. # 2003-05-21 fl Added experimental Window/ImageWindow classes
  12. # 2003-09-05 fl Added fromstring/tostring methods
  13. #
  14. # Copyright (c) Secret Labs AB 1997-2003.
  15. # Copyright (c) Fredrik Lundh 1996-2003.
  16. #
  17. # See the README file for information on usage and redistribution.
  18. #
  19. from . import Image
  20. class HDC(object):
  21. """
  22. Wraps an HDC integer. The resulting object can be passed to the
  23. :py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
  24. methods.
  25. """
  26. def __init__(self, dc):
  27. self.dc = dc
  28. def __int__(self):
  29. return self.dc
  30. class HWND(object):
  31. """
  32. Wraps an HWND integer. The resulting object can be passed to the
  33. :py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
  34. methods, instead of a DC.
  35. """
  36. def __init__(self, wnd):
  37. self.wnd = wnd
  38. def __int__(self):
  39. return self.wnd
  40. class Dib(object):
  41. """
  42. A Windows bitmap with the given mode and size. The mode can be one of "1",
  43. "L", "P", or "RGB".
  44. If the display requires a palette, this constructor creates a suitable
  45. palette and associates it with the image. For an "L" image, 128 greylevels
  46. are allocated. For an "RGB" image, a 6x6x6 colour cube is used, together
  47. with 20 greylevels.
  48. To make sure that palettes work properly under Windows, you must call the
  49. **palette** method upon certain events from Windows.
  50. :param image: Either a PIL image, or a mode string. If a mode string is
  51. used, a size must also be given. The mode can be one of "1",
  52. "L", "P", or "RGB".
  53. :param size: If the first argument is a mode string, this
  54. defines the size of the image.
  55. """
  56. def __init__(self, image, size=None):
  57. if hasattr(image, "mode") and hasattr(image, "size"):
  58. mode = image.mode
  59. size = image.size
  60. else:
  61. mode = image
  62. image = None
  63. if mode not in ["1", "L", "P", "RGB"]:
  64. mode = Image.getmodebase(mode)
  65. self.image = Image.core.display(mode, size)
  66. self.mode = mode
  67. self.size = size
  68. if image:
  69. self.paste(image)
  70. def expose(self, handle):
  71. """
  72. Copy the bitmap contents to a device context.
  73. :param handle: Device context (HDC), cast to a Python integer, or an
  74. HDC or HWND instance. In PythonWin, you can use the
  75. :py:meth:`CDC.GetHandleAttrib` to get a suitable handle.
  76. """
  77. if isinstance(handle, HWND):
  78. dc = self.image.getdc(handle)
  79. try:
  80. result = self.image.expose(dc)
  81. finally:
  82. self.image.releasedc(handle, dc)
  83. else:
  84. result = self.image.expose(handle)
  85. return result
  86. def draw(self, handle, dst, src=None):
  87. """
  88. Same as expose, but allows you to specify where to draw the image, and
  89. what part of it to draw.
  90. The destination and source areas are given as 4-tuple rectangles. If
  91. the source is omitted, the entire image is copied. If the source and
  92. the destination have different sizes, the image is resized as
  93. necessary.
  94. """
  95. if not src:
  96. src = (0, 0) + self.size
  97. if isinstance(handle, HWND):
  98. dc = self.image.getdc(handle)
  99. try:
  100. result = self.image.draw(dc, dst, src)
  101. finally:
  102. self.image.releasedc(handle, dc)
  103. else:
  104. result = self.image.draw(handle, dst, src)
  105. return result
  106. def query_palette(self, handle):
  107. """
  108. Installs the palette associated with the image in the given device
  109. context.
  110. This method should be called upon **QUERYNEWPALETTE** and
  111. **PALETTECHANGED** events from Windows. If this method returns a
  112. non-zero value, one or more display palette entries were changed, and
  113. the image should be redrawn.
  114. :param handle: Device context (HDC), cast to a Python integer, or an
  115. HDC or HWND instance.
  116. :return: A true value if one or more entries were changed (this
  117. indicates that the image should be redrawn).
  118. """
  119. if isinstance(handle, HWND):
  120. handle = self.image.getdc(handle)
  121. try:
  122. result = self.image.query_palette(handle)
  123. finally:
  124. self.image.releasedc(handle, handle)
  125. else:
  126. result = self.image.query_palette(handle)
  127. return result
  128. def paste(self, im, box=None):
  129. """
  130. Paste a PIL image into the bitmap image.
  131. :param im: A PIL image. The size must match the target region.
  132. If the mode does not match, the image is converted to the
  133. mode of the bitmap image.
  134. :param box: A 4-tuple defining the left, upper, right, and
  135. lower pixel coordinate. See :ref:`coordinate-system`. If
  136. None is given instead of a tuple, all of the image is
  137. assumed.
  138. """
  139. im.load()
  140. if self.mode != im.mode:
  141. im = im.convert(self.mode)
  142. if box:
  143. self.image.paste(im.im, box)
  144. else:
  145. self.image.paste(im.im)
  146. def frombytes(self, buffer):
  147. """
  148. Load display memory contents from byte data.
  149. :param buffer: A buffer containing display data (usually
  150. data returned from <b>tobytes</b>)
  151. """
  152. return self.image.frombytes(buffer)
  153. def tobytes(self):
  154. """
  155. Copy display memory contents to bytes object.
  156. :return: A bytes object containing display data.
  157. """
  158. return self.image.tobytes()
  159. class Window(object):
  160. """Create a Window with the given title size."""
  161. def __init__(self, title="PIL", width=None, height=None):
  162. self.hwnd = Image.core.createwindow(
  163. title, self.__dispatcher, width or 0, height or 0
  164. )
  165. def __dispatcher(self, action, *args):
  166. return getattr(self, "ui_handle_" + action)(*args)
  167. def ui_handle_clear(self, dc, x0, y0, x1, y1):
  168. pass
  169. def ui_handle_damage(self, x0, y0, x1, y1):
  170. pass
  171. def ui_handle_destroy(self):
  172. pass
  173. def ui_handle_repair(self, dc, x0, y0, x1, y1):
  174. pass
  175. def ui_handle_resize(self, width, height):
  176. pass
  177. def mainloop(self):
  178. Image.core.eventloop()
  179. class ImageWindow(Window):
  180. """Create an image window which displays the given image."""
  181. def __init__(self, image, title="PIL"):
  182. if not isinstance(image, Dib):
  183. image = Dib(image)
  184. self.image = image
  185. width, height = image.size
  186. Window.__init__(self, title, width=width, height=height)
  187. def ui_handle_repair(self, dc, x0, y0, x1, y1):
  188. self.image.draw(dc, (x0, y0, x1, y1))