Skip to content

Commit

Permalink
revert to old float-point reader when bit_cast is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 15, 2024
1 parent 11e0b10 commit a859452
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
49 changes: 29 additions & 20 deletions libheif/bitstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(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)
{
Expand Down Expand Up @@ -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<float>(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<uint32_t>(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;
Expand Down Expand Up @@ -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<uint32_t>(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;
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/uncompressed/unc_boxes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a859452

Please sign in to comment.