Pillow/PIL/PyAccess.py

322 lines
8.6 KiB
Python
Raw Normal View History

2014-01-05 22:41:25 +04:00
#
# The Python Imaging Library
# Pillow fork
#
# Python implementation of the PixelAccess Object
#
# Copyright (c) 1997-2009 by Secret Labs AB. All rights reserved.
# Copyright (c) 1995-2009 by Fredrik Lundh.
# Copyright (c) 2013 Eric Soroos
#
# See the README file for information on usage and redistribution
#
# Notes:
#
# * Implements the pixel access object following Access.
# * Does not implement the line functions, as they don't appear to be used
# * Taking only the tuple form, which is used from python.
2014-08-26 17:47:10 +04:00
# * Fill.c uses the integer form, but it's still going to use the old
# Access.c implementation.
2014-01-05 22:41:25 +04:00
#
from __future__ import print_function
import logging
2014-01-07 11:15:00 +04:00
import sys
2014-01-05 22:41:25 +04:00
from cffi import FFI
logger = logging.getLogger(__name__)
2014-08-26 17:47:10 +04:00
2014-01-05 22:41:25 +04:00
defs = """
struct Pixel_RGBA {
unsigned char r,g,b,a;
};
2014-01-07 11:15:00 +04:00
struct Pixel_I16 {
unsigned char l,r;
};
2014-01-05 22:41:25 +04:00
"""
ffi = FFI()
ffi.cdef(defs)
class PyAccess(object):
2014-08-26 17:47:10 +04:00
def __init__(self, img, readonly=False):
2014-01-05 22:41:25 +04:00
vals = dict(img.im.unsafe_ptrs)
self.readonly = readonly
self.image8 = ffi.cast('unsigned char **', vals['image8'])
self.image32 = ffi.cast('int **', vals['image32'])
self.image = ffi.cast('unsigned char **', vals['image'])
self.xsize = vals['xsize']
self.ysize = vals['ysize']
2014-08-26 17:47:10 +04:00
# Keep pointer to im object to prevent dereferencing.
self._im = img.im
# Debugging is polluting test traces, only useful here
# when hacking on PyAccess
2015-12-10 01:35:35 +03:00
# logger.debug("%s", vals)
self._post_init()
2014-01-05 22:41:25 +04:00
def _post_init(self):
2014-08-26 17:47:10 +04:00
pass
2014-01-05 22:41:25 +04:00
def __setitem__(self, xy, color):
"""
Modifies the pixel at x,y. The color is given as a single
numerical value for single band images, and a tuple for
multi-band images
:param xy: The pixel coordinate, given as (x, y).
2014-08-26 17:47:10 +04:00
:param value: The pixel value.
2014-01-05 22:41:25 +04:00
"""
2014-08-26 17:47:10 +04:00
if self.readonly:
raise ValueError('Attempt to putpixel a read only image')
(x, y) = self.check_xy(xy)
return self.set_pixel(x, y, color)
2014-01-05 22:41:25 +04:00
def __getitem__(self, xy):
"""
Returns the pixel at x,y. The pixel is returned as a single
value for single band images or a tuple for multiple band
images
:param xy: The pixel coordinate, given as (x, y).
:returns: a pixel value for single band images, a tuple of
pixel values for multiband images.
2014-01-05 22:41:25 +04:00
"""
2014-08-26 17:47:10 +04:00
(x, y) = self.check_xy(xy)
return self.get_pixel(x, y)
2014-01-05 22:41:25 +04:00
putpixel = __setitem__
getpixel = __getitem__
def check_xy(self, xy):
2014-08-26 17:47:10 +04:00
(x, y) = xy
if not (0 <= x < self.xsize and 0 <= y < self.ysize):
2014-01-07 09:19:58 +04:00
raise ValueError('pixel location out of range')
return xy
2014-01-05 22:41:25 +04:00
2014-08-26 17:47:10 +04:00
2014-01-07 10:51:31 +04:00
class _PyAccess32_2(PyAccess):
""" PA, LA, stored in first and last bytes of a 32 bit word """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-07 10:51:31 +04:00
pixel = self.pixels[y][x]
return (pixel.r, pixel.a)
2014-01-05 22:41:25 +04:00
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-07 10:51:31 +04:00
pixel = self.pixels[y][x]
# tuple
2014-08-26 17:47:10 +04:00
pixel.r = min(color[0], 255)
pixel.a = min(color[1], 255)
2014-01-05 22:41:25 +04:00
class _PyAccess32_3(PyAccess):
2014-01-07 10:51:31 +04:00
""" RGB and friends, stored in the first three bytes of a 32 bit word """
2014-08-26 17:47:10 +04:00
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-05 22:41:25 +04:00
pixel = self.pixels[y][x]
return (pixel.r, pixel.g, pixel.b)
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-05 22:41:25 +04:00
pixel = self.pixels[y][x]
# tuple
2014-08-26 17:47:10 +04:00
pixel.r = min(color[0], 255)
pixel.g = min(color[1], 255)
pixel.b = min(color[2], 255)
2014-01-05 22:41:25 +04:00
class _PyAccess32_4(PyAccess):
2014-01-07 10:51:31 +04:00
""" RGBA etc, all 4 bytes of a 32 bit word """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-05 22:41:25 +04:00
pixel = self.pixels[y][x]
return (pixel.r, pixel.g, pixel.b, pixel.a)
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-05 22:41:25 +04:00
pixel = self.pixels[y][x]
# tuple
2014-08-26 17:47:10 +04:00
pixel.r = min(color[0], 255)
pixel.g = min(color[1], 255)
pixel.b = min(color[2], 255)
pixel.a = min(color[3], 255)
2014-01-07 09:20:19 +04:00
2014-01-05 22:41:25 +04:00
class _PyAccess8(PyAccess):
2014-01-07 10:51:31 +04:00
""" 1, L, P, 8 bit images stored as uint8 """
def _post_init(self, *args, **kwargs):
self.pixels = self.image8
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-05 22:41:25 +04:00
return self.pixels[y][x]
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-05 22:41:25 +04:00
try:
# integer
2014-08-26 17:47:10 +04:00
self.pixels[y][x] = min(color, 255)
2014-01-05 22:41:25 +04:00
except:
# tuple
2014-08-26 17:47:10 +04:00
self.pixels[y][x] = min(color[0], 255)
2014-01-05 22:41:25 +04:00
2014-01-07 11:15:00 +04:00
class _PyAccessI16_N(PyAccess):
""" I;16 access, native bitendian without conversion """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast('unsigned short **', self.image)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-07 11:15:00 +04:00
return self.pixels[y][x]
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-07 11:15:00 +04:00
try:
# integer
self.pixels[y][x] = min(color, 65535)
except:
# tuple
self.pixels[y][x] = min(color[0], 65535)
2014-08-26 17:47:10 +04:00
2014-01-07 11:15:00 +04:00
class _PyAccessI16_L(PyAccess):
""" I;16L access, with conversion """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast('struct Pixel_I16 **', self.image)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-07 11:15:00 +04:00
pixel = self.pixels[y][x]
return pixel.l + pixel.r * 256
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-07 11:15:00 +04:00
pixel = self.pixels[y][x]
try:
color = min(color, 65535)
2015-04-24 11:24:52 +03:00
except TypeError:
2014-01-07 11:15:00 +04:00
color = min(color[0], 65535)
pixel.l = color & 0xFF
pixel.r = color >> 8
2014-08-26 17:47:10 +04:00
2014-01-07 11:15:00 +04:00
class _PyAccessI16_B(PyAccess):
""" I;16B access, with conversion """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast('struct Pixel_I16 **', self.image)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-07 11:15:00 +04:00
pixel = self.pixels[y][x]
2014-08-26 17:47:10 +04:00
return pixel.l * 256 + pixel.r
2014-01-07 11:15:00 +04:00
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-07 11:15:00 +04:00
pixel = self.pixels[y][x]
try:
color = min(color, 65535)
except:
color = min(color[0], 65535)
pixel.l = color >> 8
pixel.r = color & 0xFF
2014-01-05 22:41:25 +04:00
2014-08-26 17:47:10 +04:00
class _PyAccessI32_N(PyAccess):
""" Signed Int32 access, native endian """
def _post_init(self, *args, **kwargs):
self.pixels = self.image32
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
return self.pixels[y][x]
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
self.pixels[y][x] = color
2014-08-26 17:47:10 +04:00
class _PyAccessI32_Swap(PyAccess):
""" I;32L/B access, with byteswapping conversion """
def _post_init(self, *args, **kwargs):
self.pixels = self.image32
def reverse(self, i):
orig = ffi.new('int *', i)
chars = ffi.cast('unsigned char *', orig)
2014-08-26 17:47:10 +04:00
chars[0], chars[1], chars[2], chars[3] = chars[3], chars[2], \
chars[1], chars[0]
return ffi.cast('int *', chars)[0]
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
return self.reverse(self.pixels[y][x])
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
self.pixels[y][x] = self.reverse(color)
2014-01-09 08:23:20 +04:00
2014-08-26 17:47:10 +04:00
2014-01-09 08:23:20 +04:00
class _PyAccessF(PyAccess):
""" 32 bit float access """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast('float **', self.image32)
2014-08-26 17:47:10 +04:00
def get_pixel(self, x, y):
2014-01-09 08:23:20 +04:00
return self.pixels[y][x]
2014-08-26 17:47:10 +04:00
def set_pixel(self, x, y, color):
2014-01-09 08:23:20 +04:00
try:
# not a tuple
self.pixels[y][x] = color
except:
# tuple
self.pixels[y][x] = color[0]
2014-01-05 22:41:25 +04:00
mode_map = {'1': _PyAccess8,
'L': _PyAccess8,
'P': _PyAccess8,
2014-01-07 10:51:31 +04:00
'LA': _PyAccess32_2,
2016-05-10 21:46:40 +03:00
'La': _PyAccess32_2,
2014-01-07 10:51:31 +04:00
'PA': _PyAccess32_2,
2014-01-05 22:41:25 +04:00
'RGB': _PyAccess32_3,
'LAB': _PyAccess32_3,
'HSV': _PyAccess32_3,
2014-01-05 22:41:25 +04:00
'YCbCr': _PyAccess32_3,
'RGBA': _PyAccess32_4,
'RGBa': _PyAccess32_4,
'RGBX': _PyAccess32_4,
'CMYK': _PyAccess32_4,
2014-01-09 08:23:20 +04:00
'F': _PyAccessF,
2014-01-09 09:44:18 +04:00
'I': _PyAccessI32_N,
2014-01-05 22:41:25 +04:00
}
2014-01-07 11:15:00 +04:00
if sys.byteorder == 'little':
mode_map['I;16'] = _PyAccessI16_N
mode_map['I;16L'] = _PyAccessI16_N
mode_map['I;16B'] = _PyAccessI16_B
2014-08-26 17:47:10 +04:00
mode_map['I;32L'] = _PyAccessI32_N
mode_map['I;32B'] = _PyAccessI32_Swap
2014-01-07 11:15:00 +04:00
else:
mode_map['I;16'] = _PyAccessI16_L
mode_map['I;16L'] = _PyAccessI16_L
mode_map['I;16B'] = _PyAccessI16_N
2014-01-09 09:44:18 +04:00
mode_map['I;32L'] = _PyAccessI32_Swap
mode_map['I;32B'] = _PyAccessI32_N
2014-01-05 22:41:25 +04:00
2014-08-26 17:47:10 +04:00
def new(img, readonly=False):
2014-01-05 22:41:25 +04:00
access_type = mode_map.get(img.mode, None)
if not access_type:
logger.debug("PyAccess Not Implemented: %s", img.mode)
2014-01-05 22:41:25 +04:00
return None
return access_type(img, readonly)
2014-08-26 17:47:10 +04:00
# End of file