Skip to content

Commit

Permalink
Merge pull request #1353 from strukturag/fix-unci-endianness-issue
Browse files Browse the repository at this point in the history
unci: endianness correction
  • Loading branch information
farindk authored Oct 29, 2024
2 parents 0e9743e + dc49f57 commit 3f7cf77
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 657 deletions.
26 changes: 23 additions & 3 deletions libheif/codecs/uncompressed/decoder_abstract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,32 @@ void AbstractDecoder::buildChannelList(std::shared_ptr<HeifPixelImage>& img)
}
}

void AbstractDecoder::memcpy_to_native_endian(uint8_t* dst, uint32_t value, uint32_t bytes_per_sample)
{
// TODO: this assumes that the file endianness is always big-endian. The endianness flags in the uncC header are not taken into account yet.

if (bytes_per_sample==1) {
*dst = static_cast<uint8_t>(value);
return;
}
else if (std::endian::native == std::endian::big) {
for (uint32_t i = 0; i < bytes_per_sample; i++) {
dst[bytes_per_sample - 1 - i] = static_cast<uint8_t>((value >> (i * 8)) & 0xFF);
}
}
else {
for (uint32_t i = 0; i < bytes_per_sample; i++) {
dst[i] = static_cast<uint8_t>((value >> (i * 8)) & 0xFF);
}
}
}

void AbstractDecoder::processComponentSample(UncompressedBitReader& srcBits, const ChannelListEntry& entry, uint64_t dst_row_offset, uint32_t tile_column, uint32_t tile_x)
{
uint64_t dst_col_number = static_cast<uint64_t>(tile_column) * entry.tile_width + tile_x;
uint64_t dst_column_offset = dst_col_number * entry.bytes_per_component_sample;
int val = srcBits.get_bits(entry.bits_per_component_sample);
memcpy(entry.dst_plane + dst_row_offset + dst_column_offset, &val, entry.bytes_per_component_sample);
int val = srcBits.get_bits(entry.bits_per_component_sample); // get_bits() reads input in big-endian order
memcpy_to_native_endian(entry.dst_plane + dst_row_offset + dst_column_offset, val, entry.bytes_per_component_sample);
}

// Handles the case where a row consists of a single component type
Expand All @@ -85,7 +105,7 @@ void AbstractDecoder::processComponentTileSample(UncompressedBitReader& srcBits,
{
uint64_t dst_sample_offset = uint64_t{tile_x} * entry.bytes_per_component_sample;
int val = srcBits.get_bits(entry.bits_per_component_sample);
memcpy(entry.dst_plane + dst_offset + dst_sample_offset, &val, entry.bytes_per_component_sample);
memcpy_to_native_endian(entry.dst_plane + dst_offset + dst_sample_offset, val, entry.bytes_per_component_sample);
}

// Handles the case where a row consists of a single component type
Expand Down
3 changes: 3 additions & 0 deletions libheif/codecs/uncompressed/decoder_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class AbstractDecoder
std::vector<uint8_t> compressed_data,
std::vector<uint8_t>* data) const;

protected:
void memcpy_to_native_endian(uint8_t* dst, uint32_t value, uint32_t bytes_per_sample);

private:
ChannelListEntry buildChannelListEntry(Box_uncC::Component component, std::shared_ptr<HeifPixelImage>& img);
};
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/uncompressed/decoder_mixed_interleave.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void MixedInterleaveDecoder::processTile(UncompressedBitReader& srcBits, uint32_
uint64_t dst_column_number = out_x0 + tile_x;
uint64_t dst_column_offset = dst_column_number * entry.bytes_per_component_sample;
int val = srcBits.get_bits(entry.bytes_per_component_sample * 8);
memcpy(entry.dst_plane + dst_row_offset + dst_column_offset, &val, entry.bytes_per_component_sample);
memcpy_to_native_endian(entry.dst_plane + dst_row_offset + dst_column_offset, val, entry.bytes_per_component_sample);
val = srcBits.get_bits(entry.bytes_per_component_sample * 8);
memcpy(entry.other_chroma_dst_plane + dst_row_offset + dst_column_offset, &val, entry.bytes_per_component_sample);
memcpy_to_native_endian(entry.other_chroma_dst_plane + dst_row_offset + dst_column_offset, val, entry.bytes_per_component_sample);
}
haveProcessedChromaForThisTile = true;
}
Expand Down
Loading

0 comments on commit 3f7cf77

Please sign in to comment.