Pillow/src/PIL/PyAccess.py

354 lines
9.4 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
#
import logging
2014-01-07 11:15:00 +04:00
import sys
2014-01-05 22:41:25 +04:00
2020-06-14 09:35:55 +03:00
try:
from cffi import FFI
defs = """
struct Pixel_RGBA {
unsigned char r,g,b,a;
};
struct Pixel_I16 {
unsigned char l,r;
};
"""
ffi = FFI()
ffi.cdef(defs)
except ImportError as ex:
# Allow error import for doc purposes, but error out when accessing
# anything in core.
from ._util import deferred_error
FFI = ffi = deferred_error(ex)
logger = logging.getLogger(__name__)
2014-08-26 17:47:10 +04:00
class PyAccess:
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
2019-03-21 16:28:20 +03:00
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, self.ysize = img.im.size
self._img = img
2014-08-26 17:47:10 +04:00
# Keep pointer to im object to prevent dereferencing.
self._im = img.im
2018-12-31 03:35:15 +03:00
if self._im.mode == "P":
self._palette = img.palette
# 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). See
:ref:`coordinate-system`.
2017-09-29 14:08:49 +03:00
:param color: The pixel value.
2014-01-05 22:41:25 +04:00
"""
2014-08-26 17:47:10 +04:00
if self.readonly:
2019-03-21 16:28:20 +03:00
raise ValueError("Attempt to putpixel a read only image")
(x, y) = xy
if x < 0:
x = self.xsize + x
if y < 0:
y = self.ysize + y
(x, y) = self.check_xy((x, y))
2018-12-31 03:35:15 +03:00
2019-03-21 16:28:20 +03:00
if (
self._im.mode == "P"
and isinstance(color, (list, tuple))
and len(color) in [3, 4]
):
2018-12-31 05:37:04 +03:00
# RGB or RGBA value for a P image
color = self._palette.getcolor(color, self._img)
2018-12-31 03:35:15 +03:00
2014-08-26 17:47:10 +04:00
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). See
:ref:`coordinate-system`.
:returns: a pixel value for single band images, a tuple of
pixel values for multiband images.
2014-01-05 22:41:25 +04:00
"""
2018-10-14 17:12:58 +03:00
(x, y) = xy
if x < 0:
2018-10-14 17:12:58 +03:00
x = self.xsize + x
if y < 0:
2018-10-14 17:12:58 +03:00
y = self.ysize + y
(x, y) = self.check_xy((x, y))
2014-08-26 17:47:10 +04:00
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):
2019-03-21 16:28:20 +03:00
raise ValueError("pixel location out of range")
2014-01-07 09:19:58 +04:00
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):
2021-08-12 14:50:09 +03:00
"""PA, LA, stored in first and last bytes of a 32 bit word"""
2019-03-21 16:28:20 +03:00
2014-01-07 10:51:31 +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-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):
2021-08-12 14:50:09 +03: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)
2017-08-24 01:45:24 +03:00
pixel.a = 255
2014-08-26 17:47:10 +04:00
2014-01-05 22:41:25 +04:00
class _PyAccess32_4(PyAccess):
2021-08-12 14:50:09 +03:00
"""RGBA etc, all 4 bytes of a 32 bit word"""
2019-03-21 16:28:20 +03: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, 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):
2021-08-12 14:50:09 +03:00
"""1, L, P, 8 bit images stored as uint8"""
2019-03-21 16:28:20 +03:00
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)
2017-03-01 12:20:18 +03:00
except TypeError:
2014-01-05 22:41:25 +04:00
# 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):
2021-08-12 14:50:09 +03:00
"""I;16 access, native bitendian without conversion"""
2019-03-21 16:28:20 +03:00
2014-01-07 11:15:00 +04:00
def _post_init(self, *args, **kwargs):
2019-03-21 16:28:20 +03:00
self.pixels = ffi.cast("unsigned short **", self.image)
2014-01-07 11:15:00 +04:00
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)
2017-03-01 12:20:18 +03:00
except TypeError:
2014-01-07 11:15:00 +04:00
# 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):
2021-08-12 14:50:09 +03:00
"""I;16L access, with conversion"""
2019-03-21 16:28:20 +03:00
2014-01-07 11:15:00 +04:00
def _post_init(self, *args, **kwargs):
2019-03-21 16:28:20 +03:00
self.pixels = ffi.cast("struct Pixel_I16 **", self.image)
2014-01-07 11:15:00 +04:00
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 # noqa: E741
2014-01-07 11:15:00 +04:00
pixel.r = color >> 8
2014-08-26 17:47:10 +04:00
2014-01-07 11:15:00 +04:00
class _PyAccessI16_B(PyAccess):
2021-08-12 14:50:09 +03:00
"""I;16B access, with conversion"""
2019-03-21 16:28:20 +03:00
2014-01-07 11:15:00 +04:00
def _post_init(self, *args, **kwargs):
2019-03-21 16:28:20 +03:00
self.pixels = ffi.cast("struct Pixel_I16 **", self.image)
2014-01-07 11:15:00 +04:00
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 Exception:
2014-01-07 11:15:00 +04:00
color = min(color[0], 65535)
pixel.l = color >> 8 # noqa: E741
2014-01-07 11:15:00 +04:00
pixel.r = color & 0xFF
2014-01-05 22:41:25 +04:00
2014-08-26 17:47:10 +04:00
class _PyAccessI32_N(PyAccess):
2021-08-12 14:50:09 +03:00
"""Signed Int32 access, native endian"""
2019-03-21 16:28:20 +03:00
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):
2021-08-12 14:50:09 +03:00
"""I;32L/B access, with byteswapping conversion"""
2019-03-21 16:28:20 +03:00
def _post_init(self, *args, **kwargs):
self.pixels = self.image32
def reverse(self, i):
2019-03-21 16:28:20 +03:00
orig = ffi.new("int *", i)
chars = ffi.cast("unsigned char *", orig)
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):
2021-08-12 14:50:09 +03:00
"""32 bit float access"""
2019-03-21 16:28:20 +03:00
2014-01-09 08:23:20 +04:00
def _post_init(self, *args, **kwargs):
2019-03-21 16:28:20 +03:00
self.pixels = ffi.cast("float **", self.image32)
2014-01-09 08:23:20 +04:00
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
2017-03-01 12:20:18 +03:00
except TypeError:
2014-01-09 08:23:20 +04:00
# tuple
self.pixels[y][x] = color[0]
2019-03-21 16:28:20 +03:00
mode_map = {
"1": _PyAccess8,
"L": _PyAccess8,
"P": _PyAccess8,
"LA": _PyAccess32_2,
"La": _PyAccess32_2,
"PA": _PyAccess32_2,
"RGB": _PyAccess32_3,
"LAB": _PyAccess32_3,
"HSV": _PyAccess32_3,
"YCbCr": _PyAccess32_3,
"RGBA": _PyAccess32_4,
"RGBa": _PyAccess32_4,
"RGBX": _PyAccess32_4,
"CMYK": _PyAccess32_4,
"F": _PyAccessF,
"I": _PyAccessI32_N,
}
if sys.byteorder == "little":
mode_map["I;16"] = _PyAccessI16_N
mode_map["I;16L"] = _PyAccessI16_N
mode_map["I;16B"] = _PyAccessI16_B
mode_map["I;32L"] = _PyAccessI32_N
mode_map["I;32B"] = _PyAccessI32_Swap
2014-01-07 11:15:00 +04:00
else:
2019-03-21 16:28:20 +03:00
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
2019-03-21 16:28:20 +03: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)