From df56a03abfcb26dee23fffce536326168af6afb8 Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Tue, 15 Oct 2024 21:20:48 +0200 Subject: [PATCH] simplify reading/writing floats --- libheif/bitstream.cc | 65 ++++---------------------------------------- libheif/bitstream.h | 10 ++----- 2 files changed, 8 insertions(+), 67 deletions(-) diff --git a/libheif/bitstream.cc b/libheif/bitstream.cc index 1b73bb0eec..cddc34d2c3 100644 --- a/libheif/bitstream.cc +++ b/libheif/bitstream.cc @@ -270,12 +270,10 @@ uint32_t BitstreamRange::read32() (buf[3])); } -float BitstreamRange::readFloat32() +float BitstreamRange::read_float32() { - int i = read32(); - float f; - memcpy(&f, &i, sizeof(float)); - return f; + uint32_t i = read32(); + return std::bit_cast(i); // this works directly on the value layout, thus we do not have to worry about memory layout } @@ -351,33 +349,6 @@ int64_t BitstreamRange::read64s() } -float BitstreamRange::read_float32() -{ - assert(sizeof(float)==4); - - if (!prepare_read(4)) { - return 0; - } - - uint8_t buf[4]; - - auto istr = get_istream(); - bool success = istr->read((char*) buf, 4); - - if (!success) { - set_eof_while_reading(); - return 0; - } - -#if !IS_BIG_ENDIAN - std::swap(buf[0],buf[3]); - std::swap(buf[1],buf[2]); -#endif - - return *reinterpret_cast(buf); -} - - std::string BitstreamRange::read_string() { std::string str; @@ -723,11 +694,9 @@ void StreamWriter::write32(uint32_t v) } -void StreamWriter::writeFloat32(float v) +void StreamWriter::write_float32(float v) { - uint32_t i; - memcpy(&i, &v, sizeof(float)); - write32(i); + write32(std::bit_cast(v)); // this works directly on the value layout, thus we do not have to worry about memory layout } @@ -770,30 +739,6 @@ void StreamWriter::write64(int64_t v) } -void StreamWriter::write_float32(float v) -{ - assert(sizeof(float)==4); - - uint8_t buf[4]; - *reinterpret_cast(buf) = v; - -#if !IS_BIG_ENDIAN - std::swap(buf[0], buf[3]); - std::swap(buf[1], buf[2]); -#endif - - if (m_position + 4 > m_data.size()) { - m_data.resize(m_position + 4); - } - - m_data[m_position++] = buf[0]; - m_data[m_position++] = buf[1]; - m_data[m_position++] = buf[2]; - m_data[m_position++] = buf[3]; -} - - - void StreamWriter::write(int size, uint64_t value) { if (size == 1) { diff --git a/libheif/bitstream.h b/libheif/bitstream.h index 5941956fb2..0f8e5a9422 100644 --- a/libheif/bitstream.h +++ b/libheif/bitstream.h @@ -270,14 +270,12 @@ class BitstreamRange /** * Read 32 bit floating point value from the bitstream. * - * The data is assumed to be in big endian format. + * The file data is assumed to be in big endian format. */ - float readFloat32(); + float read_float32(); int64_t read64s(); - float read_float32(); - std::string read_string(); bool read(uint8_t* data, size_t n); @@ -442,7 +440,7 @@ class StreamWriter void write64(uint64_t); - void writeFloat32(float); + void write_float32(float); void write64(int64_t); @@ -454,8 +452,6 @@ class StreamWriter void write(const StreamWriter&); - void write_float32(float); - void skip(int n); void insert(int nBytes);