Skip to content

Commit

Permalink
pass through image allocation errors instead of just returning NULL (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Nov 16, 2024
1 parent 4ddf749 commit abacda1
Show file tree
Hide file tree
Showing 28 changed files with 459 additions and 340 deletions.
23 changes: 8 additions & 15 deletions libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1806,11 +1806,8 @@ int heif_image_has_channel(const struct heif_image* img, enum heif_channel chann
struct heif_error heif_image_add_plane(struct heif_image* image,
heif_channel channel, int width, int height, int bit_depth)
{
if (!image->image->add_plane(channel, width, height, bit_depth)) {
struct heif_error err = {heif_error_Memory_allocation_error,
heif_suberror_Unspecified,
"Cannot allocate memory for image plane"};
return err;
if (auto err = image->image->add_plane2(channel, width, height, bit_depth)) {
return err.error_struct(image->image.get());
}
else {
return heif_error_success;
Expand Down Expand Up @@ -1997,11 +1994,9 @@ int heif_image_is_premultiplied_alpha(struct heif_image* image)

struct heif_error heif_image_extend_padding_to_size(struct heif_image* image, int min_physical_width, int min_physical_height)
{
bool mem_alloc_success = image->image->extend_padding_to_size(min_physical_width, min_physical_height);
if (!mem_alloc_success) {
return heif_error{heif_error_Memory_allocation_error,
heif_suberror_Unspecified,
"Cannot allocate image memory."};
Error err = image->image->extend_padding_to_size2(min_physical_width, min_physical_height);
if (err) {
return err.error_struct(image->image.get());
}
else {
return heif_error_success;
Expand Down Expand Up @@ -2031,11 +2026,9 @@ struct heif_error heif_image_scale_image(const struct heif_image* input,
struct heif_error heif_image_extend_to_size_fill_with_zero(struct heif_image* image,
uint32_t width, uint32_t height)
{
bool success = image->image->extend_to_size_with_zero(width, height);
if (!success) {
return heif_error{heif_error_Memory_allocation_error,
heif_suberror_Unspecified,
"Not enough memory to extend image size."};
Error err = image->image->extend_to_size_with_zero2(width, height);
if (err) {
return err.error_struct(image->image.get());
}

return heif_error_ok;
Expand Down
8 changes: 6 additions & 2 deletions libheif/codecs/uncompressed/unc_codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,14 @@ Result<std::shared_ptr<HeifPixelImage>> UncompressedImageCodec::create_image(con
}

if ((channel == heif_channel_Cb) || (channel == heif_channel_Cr)) {
img->add_plane(channel, (width / chroma_h_subsampling(chroma)), (height / chroma_v_subsampling(chroma)), component.component_bit_depth);
if (auto err = img->add_plane2(channel, (width / chroma_h_subsampling(chroma)), (height / chroma_v_subsampling(chroma)), component.component_bit_depth)) {
return err;
}
}
else {
img->add_plane(channel, width, height, component.component_bit_depth);
if (auto err = img->add_plane2(channel, width, height, component.component_bit_depth)) {
return err;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libheif/color-conversion/alpha.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Op_drop_alpha_plane::state_after_conversion(const ColorState& input_state,
}


std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
Op_drop_alpha_plane::convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand Down
2 changes: 1 addition & 1 deletion libheif/color-conversion/alpha.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Op_drop_alpha_plane : public ColorConversionOperation
const ColorState& target_state,
const heif_color_conversion_options& options) const override;

std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand Down
80 changes: 40 additions & 40 deletions libheif/color-conversion/chroma_sampling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Op_YCbCr444_to_YCbCr420_average<Pixel>::state_after_conversion(const ColorState&


template<class Pixel>
std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
Op_YCbCr444_to_YCbCr420_average<Pixel>::convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -98,22 +98,22 @@ Op_YCbCr444_to_YCbCr420_average<Pixel>::convert_colorspace(const std::shared_ptr
if (bpp_y > 8 ||
bpp_cb > 8 ||
bpp_cr > 8) {
return nullptr;
return Error::InternalError;
}
}
else {
if (bpp_y <= 8 ||
bpp_cb <= 8 ||
bpp_cr <= 8) {
return nullptr;
return Error::InternalError;
}
}


if (bpp_y != bpp_cb ||
bpp_y != bpp_cr) {
// TODO: test with varying bit depths when we have a test image
return nullptr;
return Error::InternalError;
}


Expand All @@ -129,15 +129,15 @@ Op_YCbCr444_to_YCbCr420_average<Pixel>::convert_colorspace(const std::shared_ptr
uint32_t cwidth = (width + 1) / 2;
uint32_t cheight = (height + 1) / 2;

if (!outimg->add_plane(heif_channel_Y, width, height, bpp_y) ||
!outimg->add_plane(heif_channel_Cb, cwidth, cheight, bpp_cb) ||
!outimg->add_plane(heif_channel_Cr, cwidth, cheight, bpp_cr)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Y, width, height, bpp_y) ||
outimg->add_plane2(heif_channel_Cb, cwidth, cheight, bpp_cb) ||
outimg->add_plane2(heif_channel_Cr, cwidth, cheight, bpp_cr)) {
return err;
}

if (has_alpha) {
if (!outimg->add_plane(heif_channel_Alpha, width, height, bpp_a)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) {
return err;
}
}

Expand Down Expand Up @@ -293,7 +293,7 @@ Op_YCbCr444_to_YCbCr422_average<Pixel>::state_after_conversion(const ColorState&


template<class Pixel>
std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
Op_YCbCr444_to_YCbCr422_average<Pixel>::convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -316,22 +316,22 @@ Op_YCbCr444_to_YCbCr422_average<Pixel>::convert_colorspace(const std::shared_ptr
if (bpp_y > 8 ||
bpp_cb > 8 ||
bpp_cr > 8) {
return nullptr;
return Error::InternalError;
}
}
else {
if (bpp_y <= 8 ||
bpp_cb <= 8 ||
bpp_cr <= 8) {
return nullptr;
return Error::InternalError;
}
}


if (bpp_y != bpp_cb ||
bpp_y != bpp_cr) {
// TODO: test with varying bit depths when we have a test image
return nullptr;
return Error::InternalError;
}


Expand All @@ -347,15 +347,15 @@ Op_YCbCr444_to_YCbCr422_average<Pixel>::convert_colorspace(const std::shared_ptr
uint32_t cwidth = (width + 1) / 2;
uint32_t cheight = height;

if (!outimg->add_plane(heif_channel_Y, width, height, bpp_y) ||
!outimg->add_plane(heif_channel_Cb, cwidth, cheight, bpp_cb) ||
!outimg->add_plane(heif_channel_Cr, cwidth, cheight, bpp_cr)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Y, width, height, bpp_y) ||
outimg->add_plane2(heif_channel_Cb, cwidth, cheight, bpp_cb) ||
outimg->add_plane2(heif_channel_Cr, cwidth, cheight, bpp_cr)) {
return err;
}

if (has_alpha) {
if (!outimg->add_plane(heif_channel_Alpha, width, height, bpp_a)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) {
return err;
}
}

Expand Down Expand Up @@ -487,7 +487,7 @@ Op_YCbCr420_bilinear_to_YCbCr444<Pixel>::state_after_conversion(const ColorState


template<class Pixel>
std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
Op_YCbCr420_bilinear_to_YCbCr444<Pixel>::convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -510,22 +510,22 @@ Op_YCbCr420_bilinear_to_YCbCr444<Pixel>::convert_colorspace(const std::shared_pt
if (bpp_y > 8 ||
bpp_cb > 8 ||
bpp_cr > 8) {
return nullptr;
return Error::InternalError;
}
}
else {
if (bpp_y <= 8 ||
bpp_cb <= 8 ||
bpp_cr <= 8) {
return nullptr;
return Error::InternalError;
}
}


if (bpp_y != bpp_cb ||
bpp_y != bpp_cr) {
// TODO: test with varying bit depths when we have a test image
return nullptr;
return Error::InternalError;
}


Expand All @@ -538,15 +538,15 @@ Op_YCbCr420_bilinear_to_YCbCr444<Pixel>::convert_colorspace(const std::shared_pt

outimg->create(width, height, heif_colorspace_YCbCr, heif_chroma_444);

if (!outimg->add_plane(heif_channel_Y, width, height, bpp_y) ||
!outimg->add_plane(heif_channel_Cb, width, height, bpp_cb) ||
!outimg->add_plane(heif_channel_Cr, width, height, bpp_cr)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Y, width, height, bpp_y) ||
outimg->add_plane2(heif_channel_Cb, width, height, bpp_cb) ||
outimg->add_plane2(heif_channel_Cr, width, height, bpp_cr)) {
return err;
}

if (has_alpha) {
if (!outimg->add_plane(heif_channel_Alpha, width, height, bpp_a)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) {
return err;
}
}

Expand Down Expand Up @@ -764,7 +764,7 @@ Op_YCbCr422_bilinear_to_YCbCr444<Pixel>::state_after_conversion(const ColorState


template<class Pixel>
std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
Op_YCbCr422_bilinear_to_YCbCr444<Pixel>::convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -787,22 +787,22 @@ Op_YCbCr422_bilinear_to_YCbCr444<Pixel>::convert_colorspace(const std::shared_pt
if (bpp_y > 8 ||
bpp_cb > 8 ||
bpp_cr > 8) {
return nullptr;
return Error::InternalError;
}
}
else {
if (bpp_y <= 8 ||
bpp_cb <= 8 ||
bpp_cr <= 8) {
return nullptr;
return Error::InternalError;
}
}


if (bpp_y != bpp_cb ||
bpp_y != bpp_cr) {
// TODO: test with varying bit depths when we have a test image
return nullptr;
return Error::InternalError;
}


Expand All @@ -815,15 +815,15 @@ Op_YCbCr422_bilinear_to_YCbCr444<Pixel>::convert_colorspace(const std::shared_pt

outimg->create(width, height, heif_colorspace_YCbCr, heif_chroma_444);

if (!outimg->add_plane(heif_channel_Y, width, height, bpp_y) ||
!outimg->add_plane(heif_channel_Cb, width, height, bpp_cb) ||
!outimg->add_plane(heif_channel_Cr, width, height, bpp_cr)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Y, width, height, bpp_y) ||
outimg->add_plane2(heif_channel_Cb, width, height, bpp_cb) ||
outimg->add_plane2(heif_channel_Cr, width, height, bpp_cr)) {
return err;
}

if (has_alpha) {
if (!outimg->add_plane(heif_channel_Alpha, width, height, bpp_a)) {
return nullptr;
if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) {
return err;
}
}

Expand Down
8 changes: 4 additions & 4 deletions libheif/color-conversion/chroma_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Op_YCbCr444_to_YCbCr420_average : public ColorConversionOperation
const ColorState& target_state,
const heif_color_conversion_options& options) const override;

std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -54,7 +54,7 @@ class Op_YCbCr444_to_YCbCr422_average : public ColorConversionOperation
const ColorState& target_state,
const heif_color_conversion_options& options) const override;

std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -73,7 +73,7 @@ class Op_YCbCr420_bilinear_to_YCbCr444 : public ColorConversionOperation
const ColorState& target_state,
const heif_color_conversion_options& options) const override;

std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand All @@ -89,7 +89,7 @@ class Op_YCbCr422_bilinear_to_YCbCr444 : public ColorConversionOperation
const ColorState& target_state,
const heif_color_conversion_options& options) const override;

std::shared_ptr<HeifPixelImage>
Result<std::shared_ptr<HeifPixelImage>>
convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input,
const ColorState& input_state,
const ColorState& target_state,
Expand Down
Loading

0 comments on commit abacda1

Please sign in to comment.