Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

PyAccess.py 9.6KB

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