freeimage: Correctly handle saving uint16 images (closes gh-1101)

This commit is contained in:
Stefan van der Walt
2014-08-15 15:21:24 +02:00
parent a3638024e7
commit 2b1dde202f
2 changed files with 22 additions and 11 deletions
+19 -8
View File
@@ -518,7 +518,6 @@ def _array_from_bitmap(bitmap):
# swizzle the color components and flip the scanlines to go from
# FreeImage's BGR[A] and upside-down internal memory format to something
# more normal
def n(arr):
return arr[..., ::-1].T
if len(shape) == 3 and _FI.FreeImage_IsLittleEndian() and \
@@ -674,16 +673,28 @@ def _array_to_bitmap(array):
try:
def n(arr): # normalise to freeimage's in-memory format
return arr.T[:, ::-1]
wrapped_array = _wrap_bitmap_bits_in_array(bitmap, w_shape, dtype)
# swizzle the color components and flip the scanlines to go to
# FreeImage's BGR[A] and upside-down internal memory format
if len(shape) == 3 and _FI.FreeImage_IsLittleEndian() and \
dtype.type == numpy.uint8:
wrapped_array[0] = n(array[:, :, 2])
wrapped_array[1] = n(array[:, :, 1])
wrapped_array[2] = n(array[:, :, 0])
if shape[2] == 4:
wrapped_array[3] = n(array[:, :, 3])
if len(shape) == 3:
R = array[:, :, 0]
G = array[:, :, 1]
B = array[:, :, 2]
if _FI.FreeImage_IsLittleEndian():
if dtype.type == numpy.uint8:
wrapped_array[0] = n(B)
wrapped_array[1] = n(G)
wrapped_array[2] = n(R)
elif dtype.type == numpy.uint16:
wrapped_array[0] = n(R)
wrapped_array[1] = n(G)
wrapped_array[2] = n(B)
if shape[2] == 4:
A = array[:, :, 3]
wrapped_array[3] = n(A)
else:
wrapped_array[:] = n(array)
if len(shape) == 2 and dtype.type == numpy.uint8:
+3 -3
View File
@@ -81,15 +81,15 @@ class TestSave:
f.close()
sio.imsave(fname, x)
y = sio.imread(fname)
assert_array_equal(x, y)
assert_array_equal(y, x)
@skipif(not FI_available)
def test_imsave_roundtrip(self):
for shape, dtype, format in [
[(10, 10), (np.uint8, np.uint16), ('tif', 'png')],
[(10, 10), (np.float32,), ('tif',)],
[(10, 10, 3), (np.uint8,), ('png',)],
[(10, 10, 4), (np.uint8,), ('png',)]
[(10, 10, 3), (np.uint8, np.uint16), ('png',)],
[(10, 10, 4), (np.uint8, np.uint16), ('png',)]
]:
tests = [(d, f) for d in dtype for f in format]
for d, f in tests: