Pillow/PIL/PyAccess.py

180 lines
5.0 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.
# * Fill.c uses the integer form, but it's still going to use the old Access.c implementation.
#
from __future__ import print_function
from cffi import FFI
DEBUG = 0
defs = """
struct Pixel_RGBA {
unsigned char r,g,b,a;
};
"""
ffi = FFI()
ffi.cdef(defs)
class PyAccess(object):
def __init__(self, img, readonly = False):
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-01-05 22:41:25 +04:00
if DEBUG:
print (vals)
self._post_init()
2014-01-05 22:41:25 +04:00
def _post_init(): 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).
:param value: The pixel value.
"""
2014-01-07 09:19:58 +04:00
if self.readonly: raise ValueError('Attempt to putpixel a read only image')
2014-01-05 22:41:25 +04:00
(x,y) = self.check_xy(xy)
return self.set_pixel(x,y,color)
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).
"""
(x,y) = self.check_xy(xy)
return self.get_pixel(x,y)
putpixel = __setitem__
getpixel = __getitem__
def check_xy(self, xy):
(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-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)
def get_pixel(self, x,y):
pixel = self.pixels[y][x]
return (pixel.r, pixel.a)
2014-01-05 22:41:25 +04:00
2014-01-07 10:51:31 +04:00
def set_pixel(self, x,y, color):
pixel = self.pixels[y][x]
# tuple
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 """
def _post_init(self, *args, **kwargs):
self.pixels = ffi.cast("struct Pixel_RGBA **", self.image32)
2014-01-05 22:41:25 +04:00
def get_pixel(self, x,y):
pixel = self.pixels[y][x]
return (pixel.r, pixel.g, pixel.b)
def set_pixel(self, x,y, color):
pixel = self.pixels[y][x]
# tuple
2014-01-07 09:20:19 +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-01-05 22:41:25 +04:00
def get_pixel(self, x,y):
pixel = self.pixels[y][x]
return (pixel.r, pixel.g, pixel.b, pixel.a)
def set_pixel(self, x,y, color):
pixel = self.pixels[y][x]
# tuple
2014-01-07 09:20:19 +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-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-01-05 22:41:25 +04:00
def get_pixel(self, x,y):
return self.pixels[y][x]
def set_pixel(self, x,y, color):
try:
# integer
self.pixels[y][x] = min(color,255)
2014-01-05 22:41:25 +04:00
except:
# tuple
self.pixels[y][x] = min(color[0],255)
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,
'PA': _PyAccess32_2,
2014-01-05 22:41:25 +04:00
'RGB': _PyAccess32_3,
'LAB': _PyAccess32_3,
'YCbCr': _PyAccess32_3,
'RGBA': _PyAccess32_4,
'RGBa': _PyAccess32_4,
'RGBX': _PyAccess32_4,
'CMYK': _PyAccess32_4,
}
def new(img, readonly=False):
access_type = mode_map.get(img.mode, None)
if not access_type:
if DEBUG: print ("PyAccess Not Implemented: %s" % img.mode)
return None
if DEBUG: print ("New PyAccess: %s" % img.mode)
return access_type(img, readonly)