PyAccess.py 9.4 KB

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