From a859452d1d3d7292a2165c364f7b02a65926a237 Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Tue, 15 Oct 2024 21:45:26 +0200 Subject: [PATCH] revert to old float-point reader when bit_cast is not available --- libheif/bitstream.cc | 49 ++++++++++++++---------- libheif/codecs/uncompressed/unc_boxes.cc | 4 +- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/libheif/bitstream.cc b/libheif/bitstream.cc index 8da606b611..b6cdfb6db6 100644 --- a/libheif/bitstream.cc +++ b/libheif/bitstream.cc @@ -271,16 +271,6 @@ uint32_t BitstreamRange::read32() (buf[3])); } -float BitstreamRange::read_float32() -{ -#if __cpp_lib_bit_cast >= 201806L - 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 -#else - assert(false); // compiler too old to support bit_cast -#endif -} - uint64_t BitstreamRange::read_uint(int len) { @@ -354,6 +344,35 @@ int64_t BitstreamRange::read64s() } +float BitstreamRange::read_float32() +{ +#if __cpp_lib_bit_cast >= 201806L + 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 +#else + // compiler too old to support bit_cast + + int i = read32(); + float f; + memcpy(&f, &i, sizeof(float)); + return f; +#endif +} + + +void StreamWriter::write_float32(float v) +{ +#if __cpp_lib_bit_cast >= 201806L + write32(std::bit_cast(v)); // this works directly on the value layout, thus we do not have to worry about memory layout +#else + // compiler too old to support bit_cast + uint32_t i; + memcpy(&i, &v, sizeof(float)); + write32(i); +#endif +} + + std::string BitstreamRange::read_string() { std::string str; @@ -699,16 +718,6 @@ void StreamWriter::write32(uint32_t v) } -void StreamWriter::write_float32(float v) -{ -#if __cpp_lib_bit_cast >= 201806L - write32(std::bit_cast(v)); // this works directly on the value layout, thus we do not have to worry about memory layout -#else - assert(false); // compiler too old to support bit_cast -#endif -} - - void StreamWriter::write32s(int32_t v32s) { uint32_t v; diff --git a/libheif/codecs/uncompressed/unc_boxes.cc b/libheif/codecs/uncompressed/unc_boxes.cc index 2eed7c1314..a3f9895f19 100644 --- a/libheif/codecs/uncompressed/unc_boxes.cc +++ b/libheif/codecs/uncompressed/unc_boxes.cc @@ -646,7 +646,7 @@ Error Box_cpat::parse(BitstreamRange& range, const heif_security_limits* limits) for (uint16_t j = 0; j < m_pattern_width; j++) { PatternComponent component{}; component.component_index = range.read32(); - component.component_gain = range.readFloat32(); + component.component_gain = range.read_float32(); m_components[i] = component; } } @@ -686,7 +686,7 @@ Error Box_cpat::write(StreamWriter& writer) const for (const auto& component : m_components) { writer.write32(component.component_index); - writer.writeFloat32(component.component_gain); + writer.write_float32(component.component_gain); } prepend_header(writer, box_start);