From d8a88a53902c85418d50751ee5cda84ba1705090 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sun, 5 Jan 2014 22:00:09 -0800 Subject: [PATCH] Passes tests on python 3.2 Reorganized to remove ImagingMemoryInstance struct, only saving the three pointers that we need (image, image8, image32) and the x/ysize ints. --- PIL/PyAccess.py | 79 +++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 58 deletions(-) diff --git a/PIL/PyAccess.py b/PIL/PyAccess.py index 95abbf238..8ccff11ed 100644 --- a/PIL/PyAccess.py +++ b/PIL/PyAccess.py @@ -26,34 +26,6 @@ from cffi import FFI DEBUG = 0 defs = """ -struct ImagingMemoryInstance { - - /* Format */ - char mode[7]; /* Band names ("1", "L", "P", "RGB", "RGBA", "CMYK", "YCbCr", "BGR;xy") */ - int type; /* Data type (IMAGING_TYPE_*) */ - int depth; /* Depth (ignored in this version) */ - int bands; /* Number of bands (1, 2, 3, or 4) */ - int xsize; /* Image dimension. */ - int ysize; - - /* Colour palette (for "P" images only) */ - void *palette; - - /* Data pointers */ - unsigned char **image8; /* Set for 8-bit images (pixelsize=1). */ - int **image32; /* Set for 32-bit images (pixelsize=4). */ - - /* Internals */ - char **image; /* Actual raster data. */ - char *block; /* Set if data is allocated in a single block. */ - - int pixelsize; /* Size of a pixel, in bytes (1, 2 or 4) */ - int linesize; /* Size of a line, in bytes (xsize * pixelsize) */ - - /* Virtual methods */ - void (*destroy)(int im); /*keeping this for compatibility */ -}; - struct Pixel_RGBA { unsigned char r,g,b,a; }; @@ -68,12 +40,18 @@ class PyAccess(object): def __init__(self, img, readonly = False): vals = dict(img.im.unsafe_ptrs) self.readonly = readonly - for att in ['palette', 'image8', 'image32','image', 'block', 'destroy']: - vals[att] = ffi.cast("void *", vals[att]) + 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'] + if DEBUG: print (vals) - self.im = ffi.new("struct ImagingMemoryInstance *", vals) + self._post_init() + def _post_init(): pass + def __setitem__(self, xy, color): """ Modifies the pixel at x,y. The color is given as a single @@ -104,15 +82,14 @@ class PyAccess(object): def check_xy(self, xy): (x,y) = xy - if not (0 <= x < self.im.xsize and 0 <= y < self.im.ysize): + if not (0 <= x < self.xsize and 0 <= y < self.ysize): raise Exception('ValueError- pixel location out of range') #undone return (x,y) class _PyAccess32_3(PyAccess): - def __init__(self, *args, **kwargs): - PyAccess.__init__(self, *args, **kwargs) - self.pixels = ffi.cast("struct Pixel_RGBA **", self.im.image32) + 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] @@ -120,19 +97,13 @@ class _PyAccess32_3(PyAccess): def set_pixel(self, x,y, color): pixel = self.pixels[y][x] - try: - # tuple - pixel.r, pixel.g, pixel.b = color - except: - # int, as a char[4] - pixel.r = color >> 24 - pixel.g = (color >> 16) & 0xFF - pixel.b = (color >> 8) & 0xFF + # tuple + pixel.r, pixel.g, pixel.b = color + class _PyAccess32_4(PyAccess): - def __init__(self, *args, **kwargs): - PyAccess.__init__(self, *args, **kwargs) - self.pixels = ffi.cast("struct Pixel_RGBA **", self.im.image32) + 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] @@ -140,20 +111,12 @@ class _PyAccess32_4(PyAccess): def set_pixel(self, x,y, color): pixel = self.pixels[y][x] - try: - # tuple - pixel.r, pixel.g, pixel.b, pixel.a = color - except: - # int, as a char[4] - pixel.r = color >> 24 - pixel.g = (color >> 16) & 0xFF - pixel.b = (color >> 8) & 0xFF - pixel.a = color & 0xFF + # tuple + pixel.r, pixel.g, pixel.b, pixel.a = color class _PyAccess8(PyAccess): - def __init__(self, *args, **kwargs): - PyAccess.__init__(self, *args, **kwargs) - self.pixels = self.im.image8 + def _post_init(self, *args, **kwargs): + self.pixels = self.image8 def get_pixel(self, x,y): return self.pixels[y][x]