[vsipl++] Matlab IO
Jules Bergmann
jules at codesourcery.com
Tue Jun 20 15:51:43 UTC 2006
Assem Salama wrote:
> Everyone,
> This is the new Matlab IO patch addressing Stefan and Jule's issues.
Assem,
The corrections from the previous patch look good, however the new bits
for handling byte-ordering need a couple of changes.
First, you need to read and write the endian word as a single 16-bit
value (instead of as 2 8-bit values). This way, the system byte-order
will be reflected in the value on disk. (The current code writes the
endian word out as individual 8-bit values, which results in the same
value on disk regardless of the system byte-ordering).
To write the value:
uint16_t endian = 'M' << 8 | 'I';
o.write(&endian, sizeof(uint16_t);
Likewise, to read the value back in:
uint16_t endian;
is.read(&endian, sizeof(uint16_t));
Once you've read the value back in, you don't need to know the
byte-order of the host (the 'get_machine_endian' function isn't
necessary), you just need to know whether the byte-order is the same as
or different from the file. You check this by examining the endian word
from the header. If it is equivalent to 'MI', then the host and file
have the same byte-order (both little-endian or both big-endian). If it
is equivalent to 'IM', then the host and file have different byte
orders, and you need to byte swap.
... read in endian ...
if (endian == 'M' << 8 | 'I')
// The system and file have same byte-order,
// so we don't need to swizzle bytes.
byte_swap = 0;
else if (endian == 'I' << 8 | 'M')
// The system and file have different byte-orders,
// so we need to swizzle bytes.
byte_swap = 1;
else
// Something bad happened, we expect to see either
// 'MI' or 'IM'.
throw ...
Second, If the system and file have different byte-ordering, than you
need to swap bytes for *all* multi-byte values read in, including the
header fields for type, size, matrix dimensionality, etc, not just for
data values.
-- Jules
--
Jules Bergmann
CodeSourcery
jules at codesourcery.com
(650) 331-3385 x705
More information about the vsipl++
mailing list