Skip to content

Commit

Permalink
simplify reading/writing floats
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 15, 2024
1 parent d476a79 commit df56a03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 67 deletions.
65 changes: 5 additions & 60 deletions libheif/bitstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(i); // this works directly on the value layout, thus we do not have to worry about memory layout
}


Expand Down Expand Up @@ -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<float*>(buf);
}


std::string BitstreamRange::read_string()
{
std::string str;
Expand Down Expand Up @@ -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<uint32_t>(v)); // this works directly on the value layout, thus we do not have to worry about memory layout
}


Expand Down Expand Up @@ -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<float*>(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) {
Expand Down
10 changes: 3 additions & 7 deletions libheif/bitstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -442,7 +440,7 @@ class StreamWriter

void write64(uint64_t);

void writeFloat32(float);
void write_float32(float);

void write64(int64_t);

Expand All @@ -454,8 +452,6 @@ class StreamWriter

void write(const StreamWriter&);

void write_float32(float);

void skip(int n);

void insert(int nBytes);
Expand Down

0 comments on commit df56a03

Please sign in to comment.