Skip to content

Commit

Permalink
align 'tild' writing and reading
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Aug 15, 2024
1 parent 4c6d276 commit 6b9ca71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
34 changes: 21 additions & 13 deletions libheif/codecs/tild.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Error TildHeader::parse(const std::vector<uint8_t>& data)
}

m_parameters.with_tile_sizes = !!(flags & 0x04);
m_parameters.size_field_length = (flags & 0x08) ? 64 : 32;
m_parameters.size_field_length = (flags & 0x08) ? 32 : 24;
m_parameters.tiles_are_sequential = !!(flags % 0x10);
bool dimensions_are_64bit = (flags & 0x20);

Expand Down Expand Up @@ -202,7 +202,7 @@ std::vector<uint8_t> TildHeader::write()
bool dimensions_are_64bit = false;

if (m_parameters.image_width > 0xFFFF || m_parameters.image_height > 0xFFFF) {
flags |= 0x40;
flags |= 0x20;
dimensions_are_64bit = true;
}

Expand All @@ -226,7 +226,7 @@ std::vector<uint8_t> TildHeader::write()
if (m_parameters.with_tile_sizes) {
flags |= 0x04;

if (m_parameters.size_field_length == 64) {
if (m_parameters.size_field_length == 32) {
// TODO: check for valid values
flags |= 0x08;
}
Expand All @@ -236,31 +236,39 @@ std::vector<uint8_t> TildHeader::write()
flags |= 0x10;
}

if (m_parameters.number_of_extra_dimensions > 0) {
flags |= 0x20;
}

uint64_t nTiles = number_of_tiles();

int offset_entry_size = m_parameters.offset_field_length / 8;
if (m_parameters.with_tile_sizes) {
offset_entry_size += m_parameters.size_field_length / 8;
}

std::vector<uint8_t> data;
uint64_t size = (2 + // version, flags
(dimensions_are_64bit ? 8 : 4) * 2 + // image size
1 + // number of extra dimensions
(dimensions_are_64bit ? 8 : 4) * (2 + m_parameters.number_of_extra_dimensions) + // image size
2 * 4 + // tile size
4 + // compression type
nTiles * (m_parameters.offset_field_length / 8)); // offsets

if (m_parameters.with_tile_sizes) {
size += nTiles * (m_parameters.size_field_length / 8);
}
nTiles * offset_entry_size); // offsets

data.resize(size);
size_t idx = 0;
data[idx++] = version;
data[idx++] = flags;

if (m_parameters.number_of_extra_dimensions > 8) {
assert(false); // currently not supported
}

data[idx++] = m_parameters.number_of_extra_dimensions;

writevec(data.data(), idx, m_parameters.image_width, dimensions_are_64bit ? 8 : 4);
writevec(data.data(), idx, m_parameters.image_height, dimensions_are_64bit ? 8 : 4);

for (int i = 0; i < m_parameters.number_of_extra_dimensions; i++) {
writevec(data.data(), idx, m_parameters.extra_dimensions[i], dimensions_are_64bit ? 8 : 4);
}

writevec(data.data(), idx, m_parameters.tile_width, 4);
writevec(data.data(), idx, m_parameters.tile_height, 4);

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/tild.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TildHeader
* Flags:
* bit 0-1 - number of bits for offsets (0: 32, 1: 40, 2: 48, 3: 64)
* bit 2 - with tile sizes
* bit 3 - number of bits for size (0: 32, 1: 64)
* bit 3 - number of bits for size (0: 24, 1: 32)
* bit 4 - sequential ordering hint
* bit 5 - use 64 bit dimensions (currently unused because ispe is limited to 32 bit)
*/
Expand Down

0 comments on commit 6b9ca71

Please sign in to comment.