Skip to content

Commit

Permalink
add HeifPixelImage::extend_to_size_with_zero()
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Sep 25, 2024
1 parent d3da03b commit c98a381
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
63 changes: 63 additions & 0 deletions libheif/pixelimage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,69 @@ bool HeifPixelImage::extend_padding_to_size(uint32_t width, uint32_t height, boo
}


bool HeifPixelImage::extend_to_size_with_zero(uint32_t width, uint32_t height)
{
for (auto& planeIter : m_planes) {
auto* plane = &planeIter.second;

uint32_t subsampled_width, subsampled_height;
get_subsampled_size(width, height, planeIter.first, m_chroma,
&subsampled_width, &subsampled_height);

uint32_t old_width = plane->m_width;
uint32_t old_height = plane->m_height;

int bytes_per_pixel = get_storage_bits_per_pixel(planeIter.first) / 8;

if (plane->m_mem_width < subsampled_width ||
plane->m_mem_height < subsampled_height) {

ImagePlane newPlane;
if (!newPlane.alloc(subsampled_width, subsampled_height, plane->m_datatype, plane->m_bit_depth, num_interleaved_pixels_per_plane(m_chroma))) {
return false;
}

// copy the visible part of the old plane into the new plane

for (uint32_t y = 0; y < plane->m_height; y++) {
memcpy(static_cast<uint8_t*>(newPlane.mem) + y * newPlane.stride,
static_cast<uint8_t*>(plane->mem) + y * plane->stride,
plane->m_width * bytes_per_pixel);
}

planeIter.second = newPlane;
plane = &planeIter.second;
}

// extend plane size

if (old_width != subsampled_width) {
for (uint32_t y = 0; y < old_height; y++) {
memset(static_cast<uint8_t*>(plane->mem) + y * plane->stride + old_width * bytes_per_pixel,
0,
bytes_per_pixel * (subsampled_width - old_width));
}
}

for (uint32_t y = old_height; y < subsampled_height; y++) {
memset(static_cast<uint8_t*>(plane->mem) + y * plane->stride,
0,
subsampled_width * bytes_per_pixel);
}


plane->m_width = subsampled_width;
plane->m_height = subsampled_height;
}

// don't modify the logical image size

m_width = width;
m_height = height;

return true;
}

bool HeifPixelImage::has_channel(heif_channel channel) const
{
return (m_planes.find(channel) != m_planes.end());
Expand Down
2 changes: 2 additions & 0 deletions libheif/pixelimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class HeifPixelImage : public std::enable_shared_from_this<HeifPixelImage>,

bool extend_padding_to_size(uint32_t width, uint32_t height, bool adjust_size = false);

bool extend_to_size_with_zero(uint32_t width, uint32_t height);

// --- pixel aspect ratio

bool has_nonsquare_pixel_ratio() const { return m_PixelAspectRatio_h != m_PixelAspectRatio_v; }
Expand Down

0 comments on commit c98a381

Please sign in to comment.