From 2b1dde202f3d8a6b12a4516dfeadba7cbcbe61ff Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Fri, 15 Aug 2014 15:21:24 +0200 Subject: [PATCH] freeimage: Correctly handle saving uint16 images (closes gh-1101) --- skimage/io/_plugins/freeimage_plugin.py | 27 +++++++++++++++++-------- skimage/io/tests/test_freeimage.py | 6 +++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/skimage/io/_plugins/freeimage_plugin.py b/skimage/io/_plugins/freeimage_plugin.py index 5ebe8214..c3bd4e73 100644 --- a/skimage/io/_plugins/freeimage_plugin.py +++ b/skimage/io/_plugins/freeimage_plugin.py @@ -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: diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py index a2d88db3..81f8058a 100644 --- a/skimage/io/tests/test_freeimage.py +++ b/skimage/io/tests/test_freeimage.py @@ -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: