I32 modes, not that I can actually use the I;32L/B ones

This commit is contained in:
wiredfool 2014-01-08 21:18:38 -08:00
parent bfdc599c28
commit 79a7fdc100
2 changed files with 51 additions and 1 deletions

View File

@ -209,6 +209,33 @@ class _PyAccessI16_B(PyAccess):
pixel.l = color >> 8
pixel.r = color & 0xFF
class _PyAccessI32_N(PyAccess):
""" Signed Int32 access, native endian """
def _post_init(self, *args, **kwargs):
self.pixels = self.image32
def get_pixel(self, x,y):
return self.pixels[y][x]
def set_pixel(self, x,y, color):
self.pixels[y][x] = color
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)
chars[0],chars[1],chars[2],chars[3] = chars[3], chars[2],chars[1],chars[0]
return ffi.cast('int *', chars)[0]
def get_pixel(self, x,y):
return self.reverse(self.pixels[y][x])
def set_pixel(self, x,y, color):
self.pixels[y][x] = self.reverse(color)
class _PyAccessF(PyAccess):
""" 32 bit float access """
@ -246,11 +273,18 @@ 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'] = _PyAccessI32_N
mode_map['I;32L'] = _PyAccessI32_N
mode_map['I;32B'] = _PyAccessI32_Swap
else:
mode_map['I;16'] = _PyAccessI16_L
mode_map['I;16L'] = _PyAccessI16_L
mode_map['I;16B'] = _PyAccessI16_N
mode_map['I'] = _PyAccessI32_Swap
mode_map['I;32L'] = _PyAccessI32_Swap
mode_map['I;32B'] = _PyAccessI32_N
def new(img, readonly=False):

View File

@ -50,6 +50,15 @@ def test_get_vs_c():
im = Image.new('I;16B', (10,10), 40000)
_test_get_access(im)
im = Image.new('I', (10,10), 40000)
_test_get_access(im)
# These don't actually appear to be modes that I can actually make,
# as unpack sets them directly into the I mode.
#im = Image.new('I;32L', (10,10), -2**10)
#_test_get_access(im)
#im = Image.new('I;32B', (10,10), 2**10)
#_test_get_access(im)
def _test_set_access(im, color):
@ -82,3 +91,10 @@ def test_set_vs_c():
im = Image.new('I;16B', (10,10), 40000)
_test_set_access(im, 45000)
im = Image.new('I', (10,10), 40000)
_test_set_access(im, 45000)
# im = Image.new('I;32L', (10,10), -(2**10))
# _test_set_access(im, -(2**13)+1)
#im = Image.new('I;32B', (10,10), 2**10)
#_test_set_access(im, 2**13-1)