Skip to content

Commit dc1cdc3

Browse files
committed
fix binary image for endianness
1 parent e81a492 commit dc1cdc3

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/fabio/binaryimage.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
__contact__ = "[email protected]"
3939
__license__ = "MIT"
4040
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
41-
__version__ = "17/10/2012"
41+
__date__ = "30/05/2024"
4242

4343
import io
4444
from .fabioimage import FabioImage
@@ -84,17 +84,21 @@ def read(self, fname, dim1, dim2, offset=0, bytecode="int32", endian="<"):
8484
:param int dim2: image dimensions (Slow index)
8585
:param int offset: starting position of the data-block. If negative, starts at the end.
8686
:param bytecode: can be "int8","int16","int32","int64","uint8","uint16","uint32","uint64","float32","float64",...
87-
:param endian: among short or long endian ("<" or ">")
87+
:param endian: among litte or big endian ("<" or ">")
8888
8989
"""
90+
assert endian in ('<', '>', '=')
91+
bytecode = numpy.dtype(bytecode)
92+
if not bytecode.str.startswith(endian):
93+
bytecode = numpy.dtype(endian + bytecode.str[1:])
9094
self.filename = fname
9195
self._shape = dim2, dim1
9296
self._bytecode = bytecode
93-
with open(self.filename, "rb") as f:
97+
with open(self.filename, "rb") as f:
9498
dims = [dim2, dim1]
9599
bpp = numpy.dtype(bytecode).itemsize
96100
size = dims[0] * dims[1] * bpp
97-
101+
98102
if offset >= 0:
99103
f.seek(offset)
100104
else:
@@ -107,8 +111,6 @@ def read(self, fname, dim1, dim2, offset=0, bytecode="int32", endian="<"):
107111
logger.error('Uncommon error encountered when reading file')
108112
rawData = f.read(size)
109113
data = numpy.frombuffer(rawData, bytecode).copy().reshape(tuple(dims))
110-
if self.swap_needed(endian):
111-
data.byteswap(True)
112114
self.data = data
113115
self._shape = None
114116
return self

src/fabio/test/codecs/test_binaryimage.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,16 @@ def test_read(self):
6464
self.assertEqual(e.shape, f.shape)
6565
self.assertEqual(e.bpp, f.bpp, "bpp OK")
6666
print(self.fn3)
67-
self.assertEqual(abs(e.data-f.data).max(), 0, "data OK")
68-
69-
67+
self.assertEqual(abs(e.data - f.data).max(), 0, "data OK")
7068

7169
def test_write(self):
7270
fn = os.path.join(UtilsTest.tempdir, "binary_write.h5")
7371
ary = numpy.random.randint(0, 100, size=self.shape)
74-
e = BinaryImage(data = ary)
72+
e = BinaryImage(data=ary)
7573
e.save(fn)
7674
self.assertTrue(os.path.exists(fn), "file exists")
7775
f = BinaryImage()
78-
f.read(fn, self.shape[1], self.shape[0], offset=0, bytecode=ary.dtype)
76+
f.read(fn, self.shape[1], self.shape[0], offset=0, bytecode=ary.dtype, endian=ary.dtype.str[0])
7977
self.assertEqual(str(f.__class__.__name__), "BinaryImage", "Used the write reader")
8078
self.assertEqual(self.shape, f.shape, "shape matches")
8179
self.assertEqual(abs(f.data - ary).max(), 0, "first frame matches")

0 commit comments

Comments
 (0)