fixed 24-bit reading and writing

This commit is contained in:
Bob Polis 2021-11-14 14:22:00 +01:00
parent 9e5b5fe36c
commit cf654441be

View File

@ -31,11 +31,12 @@ std::string data_streamer::get4chars(std::istream& file) const {
uint32_t data_streamer::getui24(std::istream& file) const {
char buf[4] = {0};
file.read(&buf[1], 3);
int i = cur_byte_order() == byte_order::little_endian ? 0 : 1;
file.read(&buf[i], 3);
if (cur_byte_order() != _target_byte_order) {
char tmp = buf[1];
buf[1] = buf[3];
buf[3] = tmp;
char tmp = buf[i];
buf[i] = buf[i + 2];
buf[i + 2] = tmp;
}
return *reinterpret_cast<uint32_t*>(buf);
}
@ -62,12 +63,13 @@ void data_streamer::put4chars(const std::string& s, std::ostream& file) const {
void data_streamer::putui24(uint32_t val, std::ostream& file) const {
char* buf = reinterpret_cast<char*>(&val);
int i = cur_byte_order() == byte_order::little_endian ? 0 : 1;
if (cur_byte_order() != _target_byte_order) {
char tmp = buf[1];
buf[1] = buf[3];
buf[3] = tmp;
char tmp = buf[i];
buf[i] = buf[i + 2];
buf[i + 2] = tmp;
}
file.write(&buf[1], 3);
file.write(&buf[i], 3);
}
void data_streamer::putsi24(int32_t val, std::ostream& file) const {