PyAccess.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #
  2. # The Python Imaging Library
  3. # Pillow fork
  4. #
  5. # Python implementation of the PixelAccess Object
  6. #
  7. # Copyright (c) 1997-2009 by Secret Labs AB. All rights reserved.
  8. # Copyright (c) 1995-2009 by Fredrik Lundh.
  9. # Copyright (c) 2013 Eric Soroos
  10. #
  11. # See the README file for information on usage and redistribution
  12. #
  13. # Notes:
  14. #
  15. # * Implements the pixel access object following Access.c
  16. # * Taking only the tuple form, which is used from python.
  17. # * Fill.c uses the integer form, but it's still going to use the old
  18. # Access.c implementation.
  19. #
  20. from __future__ import annotations
  21. import logging
  22. import sys
  23. from typing import TYPE_CHECKING
  24. from ._deprecate import deprecate
  25. FFI: type
  26. try:
  27. from cffi import FFI
  28. defs = """
  29. struct Pixel_RGBA {
  30. unsigned char r,g,b,a;
  31. };
  32. struct Pixel_I16 {
  33. unsigned char l,r;
  34. };
  35. """
  36. ffi = FFI()
  37. ffi.cdef(defs)
  38. except ImportError as ex:
  39. # Allow error import for doc purposes, but error out when accessing
  40. # anything in core.
  41. from ._util import DeferredError
  42. FFI = ffi = DeferredError.new(ex)
  43. logger = logging.getLogger(__name__)
  44. if TYPE_CHECKING:
  45. from . import Image
  46. class PyAccess:
  47. def __init__(self, img: Image.Image, readonly: bool = False) -> None:
  48. deprecate("PyAccess", 11)
  49. vals = dict(img.im.unsafe_ptrs)
  50. self.readonly = readonly
  51. self.image8 = ffi.cast("unsigned char **", vals["image8"])
  52. self.image32 = ffi.cast("int **", vals["image32"])
  53. self.image = ffi.cast("unsigned char **", vals["image"])
  54. self.xsize, self.ysize = img.im.size
  55. self._img = img
  56. # Keep pointer to im object to prevent dereferencing.
  57. self._im = img.im
  58. if self._im.mode in ("P", "PA"):
  59. self._palette = img.palette
  60. # Debugging is polluting test traces, only useful here
  61. # when hacking on PyAccess
  62. # logger.debug("%s", vals)
  63. self._post_init()
  64. def _post_init(self) -> None:
  65. pass
  66. def __setitem__(
  67. self,
  68. xy: tuple[int, int] | list[int],
  69. color: float | tuple[int, ...] | list[int],
  70. ) -> None:
  71. """
  72. Modifies the pixel at x,y. The color is given as a single
  73. numerical value for single band images, and a tuple for
  74. multi-band images. In addition to this, RGB and RGBA tuples
  75. are accepted for P and PA images.
  76. :param xy: The pixel coordinate, given as (x, y). See
  77. :ref:`coordinate-system`.
  78. :param color: The pixel value.
  79. """
  80. if self.readonly:
  81. msg = "Attempt to putpixel a read only image"
  82. raise ValueError(msg)
  83. (x, y) = xy
  84. if x < 0:
  85. x = self.xsize + x
  86. if y < 0:
  87. y = self.ysize + y
  88. (x, y) = self.check_xy((x, y))
  89. if (
  90. self._im.mode in ("P", "PA")
  91. and isinstance(color, (list, tuple))
  92. and len(color) in [3, 4]
  93. ):
  94. # RGB or RGBA value for a P or PA image
  95. if self._im.mode == "PA":
  96. alpha = color[3] if len(color) == 4 else 255
  97. color = color[:3]
  98. palette_index = self._palette.getcolor(color, self._img)
  99. color = (palette_index, alpha) if self._im.mode == "PA" else palette_index
  100. return self.set_pixel(x, y, color)
  101. def __getitem__(self, xy: tuple[int, int] | list[int]) -> float | tuple[int, ...]:
  102. """
  103. Returns the pixel at x,y. The pixel is returned as a single
  104. value for single band images or a tuple for multiple band
  105. images
  106. :param xy: The pixel coordinate, given as (x, y). See
  107. :ref:`coordinate-system`.
  108. :returns: a pixel value for single band images, a tuple of
  109. pixel values for multiband images.
  110. """
  111. (x, y) = xy
  112. if x < 0:
  113. x = self.xsize + x
  114. if y < 0:
  115. y = self.ysize + y
  116. (x, y) = self.check_xy((x, y))
  117. return self.get_pixel(x, y)
  118. putpixel = __setitem__
  119. getpixel = __getitem__
  120. def check_xy(self, xy: tuple[int, int]) -> tuple[int, int]:
  121. (x, y) = xy
  122. if not (0 <= x < self.xsize and 0 <= y < self.ysize):
  123. msg = "pixel location out of range"
  124. raise ValueError(msg)
  125. return xy
  126. def get_pixel(self, x: int, y: int) -> float | tuple[int, ...]:
  127. raise NotImplementedError()
  128. def set_pixel(
  129. self, x: int, y: int, color: float | tuple[int, ...] | list[int]
  130. ) -> None:
  131. raise NotImplementedError()
  132. class _PyAccess32_2(PyAccess):
  133. """PA, LA, stored in first and last bytes of a 32 bit word"""
  134. def _post_init(self, *args, **kwargs):
  135. self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
  136. def get_pixel(self, x: int, y: int) -> tuple[int, int]:
  137. pixel = self.pixels[y][x]
  138. return pixel.r, pixel.a
  139. def set_pixel(self, x, y, color):
  140. pixel = self.pixels[y][x]
  141. # tuple
  142. pixel.r = min(color[0], 255)
  143. pixel.a = min(color[1], 255)
  144. class _PyAccess32_3(PyAccess):
  145. """RGB and friends, stored in the first three bytes of a 32 bit word"""
  146. def _post_init(self, *args, **kwargs):
  147. self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
  148. def get_pixel(self, x: int, y: int) -> tuple[int, int, int]:
  149. pixel = self.pixels[y][x]
  150. return pixel.r, pixel.g, pixel.b
  151. def set_pixel(self, x, y, color):
  152. pixel = self.pixels[y][x]
  153. # tuple
  154. pixel.r = min(color[0], 255)
  155. pixel.g = min(color[1], 255)
  156. pixel.b = min(color[2], 255)
  157. pixel.a = 255
  158. class _PyAccess32_4(PyAccess):
  159. """RGBA etc, all 4 bytes of a 32 bit word"""
  160. def _post_init(self, *args, **kwargs):
  161. self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
  162. def get_pixel(self, x: int, y: int) -> tuple[int, int, int, int]:
  163. pixel = self.pixels[y][x]
  164. return pixel.r, pixel.g, pixel.b, pixel.a
  165. def set_pixel(self, x, y, color):
  166. pixel = self.pixels[y][x]
  167. # tuple
  168. pixel.r = min(color[0], 255)
  169. pixel.g = min(color[1], 255)
  170. pixel.b = min(color[2], 255)
  171. pixel.a = min(color[3], 255)
  172. class _PyAccess8(PyAccess):
  173. """1, L, P, 8 bit images stored as uint8"""
  174. def _post_init(self, *args, **kwargs):
  175. self.pixels = self.image8
  176. def get_pixel(self, x: int, y: int) -> int:
  177. return self.pixels[y][x]
  178. def set_pixel(self, x, y, color):
  179. try:
  180. # integer
  181. self.pixels[y][x] = min(color, 255)
  182. except TypeError:
  183. # tuple
  184. self.pixels[y][x] = min(color[0], 255)
  185. class _PyAccessI16_N(PyAccess):
  186. """I;16 access, native bitendian without conversion"""
  187. def _post_init(self, *args, **kwargs):
  188. self.pixels = ffi.cast("unsigned short **", self.image)
  189. def get_pixel(self, x: int, y: int) -> int:
  190. return self.pixels[y][x]
  191. def set_pixel(self, x, y, color):
  192. try:
  193. # integer
  194. self.pixels[y][x] = min(color, 65535)
  195. except TypeError:
  196. # tuple
  197. self.pixels[y][x] = min(color[0], 65535)
  198. class _PyAccessI16_L(PyAccess):
  199. """I;16L access, with conversion"""
  200. def _post_init(self, *args, **kwargs):
  201. self.pixels = ffi.cast("struct Pixel_I16 **", self.image)
  202. def get_pixel(self, x: int, y: int) -> int:
  203. pixel = self.pixels[y][x]
  204. return pixel.l + pixel.r * 256
  205. def set_pixel(self, x, y, color):
  206. pixel = self.pixels[y][x]
  207. try:
  208. color = min(color, 65535)
  209. except TypeError:
  210. color = min(color[0], 65535)
  211. pixel.l = color & 0xFF
  212. pixel.r = color >> 8
  213. class _PyAccessI16_B(PyAccess):
  214. """I;16B access, with conversion"""
  215. def _post_init(self, *args, **kwargs):
  216. self.pixels = ffi.cast("struct Pixel_I16 **", self.image)
  217. def get_pixel(self, x: int, y: int) -> int:
  218. pixel = self.pixels[y][x]
  219. return pixel.l * 256 + pixel.r
  220. def set_pixel(self, x, y, color):
  221. pixel = self.pixels[y][x]
  222. try:
  223. color = min(color, 65535)
  224. except Exception:
  225. color = min(color[0], 65535)
  226. pixel.l = color >> 8
  227. pixel.r = color & 0xFF
  228. class _PyAccessI32_N(PyAccess):
  229. """Signed Int32 access, native endian"""
  230. def _post_init(self, *args, **kwargs):
  231. self.pixels = self.image32
  232. def get_pixel(self, x: int, y: int) -> int:
  233. return self.pixels[y][x]
  234. def set_pixel(self, x, y, color):
  235. self.pixels[y][x] = color
  236. class _PyAccessI32_Swap(PyAccess):
  237. """I;32L/B access, with byteswapping conversion"""
  238. def _post_init(self, *args, **kwargs):
  239. self.pixels = self.image32
  240. def reverse(self, i):
  241. orig = ffi.new("int *", i)
  242. chars = ffi.cast("unsigned char *", orig)
  243. chars[0], chars[1], chars[2], chars[3] = chars[3], chars[2], chars[1], chars[0]
  244. return ffi.cast("int *", chars)[0]
  245. def get_pixel(self, x: int, y: int) -> int:
  246. return self.reverse(self.pixels[y][x])
  247. def set_pixel(self, x, y, color):
  248. self.pixels[y][x] = self.reverse(color)
  249. class _PyAccessF(PyAccess):
  250. """32 bit float access"""
  251. def _post_init(self, *args, **kwargs):
  252. self.pixels = ffi.cast("float **", self.image32)
  253. def get_pixel(self, x: int, y: int) -> float:
  254. return self.pixels[y][x]
  255. def set_pixel(self, x, y, color):
  256. try:
  257. # not a tuple
  258. self.pixels[y][x] = color
  259. except TypeError:
  260. # tuple
  261. self.pixels[y][x] = color[0]
  262. mode_map = {
  263. "1": _PyAccess8,
  264. "L": _PyAccess8,
  265. "P": _PyAccess8,
  266. "I;16N": _PyAccessI16_N,
  267. "LA": _PyAccess32_2,
  268. "La": _PyAccess32_2,
  269. "PA": _PyAccess32_2,
  270. "RGB": _PyAccess32_3,
  271. "LAB": _PyAccess32_3,
  272. "HSV": _PyAccess32_3,
  273. "YCbCr": _PyAccess32_3,
  274. "RGBA": _PyAccess32_4,
  275. "RGBa": _PyAccess32_4,
  276. "RGBX": _PyAccess32_4,
  277. "CMYK": _PyAccess32_4,
  278. "F": _PyAccessF,
  279. "I": _PyAccessI32_N,
  280. }
  281. if sys.byteorder == "little":
  282. mode_map["I;16"] = _PyAccessI16_N
  283. mode_map["I;16L"] = _PyAccessI16_N
  284. mode_map["I;16B"] = _PyAccessI16_B
  285. mode_map["I;32L"] = _PyAccessI32_N
  286. mode_map["I;32B"] = _PyAccessI32_Swap
  287. else:
  288. mode_map["I;16"] = _PyAccessI16_L
  289. mode_map["I;16L"] = _PyAccessI16_L
  290. mode_map["I;16B"] = _PyAccessI16_N
  291. mode_map["I;32L"] = _PyAccessI32_Swap
  292. mode_map["I;32B"] = _PyAccessI32_N
  293. def new(img: Image.Image, readonly: bool = False) -> PyAccess | None:
  294. access_type = mode_map.get(img.mode, None)
  295. if not access_type:
  296. logger.debug("PyAccess Not Implemented: %s", img.mode)
  297. return None
  298. return access_type(img, readonly)