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.

319 lines
8.7 KiB

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