From 56323abbdfa9aa39ff72ea1d09f31ea565fb76f4 Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Sat, 16 Nov 2024 21:33:21 +0100 Subject: [PATCH] pass security limits down into image allocation and color conversion (#1389) --- fuzzing/color_conversion_fuzzer.cc | 7 +- libheif/api/libheif/heif.cc | 13 +- libheif/codecs/uncompressed/unc_codec.cc | 12 +- libheif/codecs/uncompressed/unc_codec.h | 3 +- libheif/color-conversion/alpha.cc | 5 +- libheif/color-conversion/alpha.h | 3 +- libheif/color-conversion/chroma_sampling.cc | 44 +++--- libheif/color-conversion/chroma_sampling.h | 12 +- libheif/color-conversion/colorconversion.cc | 15 +- libheif/color-conversion/colorconversion.h | 12 +- libheif/color-conversion/hdr_sdr.cc | 14 +- libheif/color-conversion/hdr_sdr.h | 6 +- libheif/color-conversion/monochrome.cc | 16 ++- libheif/color-conversion/monochrome.h | 6 +- libheif/color-conversion/rgb2rgb.cc | 46 ++++--- libheif/color-conversion/rgb2rgb.h | 18 ++- libheif/color-conversion/rgb2yuv.cc | 44 +++--- libheif/color-conversion/rgb2yuv.h | 12 +- libheif/color-conversion/rgb2yuv_sharp.cc | 11 +- libheif/color-conversion/rgb2yuv_sharp.h | 3 +- libheif/color-conversion/yuv2rgb.cc | 28 ++-- libheif/color-conversion/yuv2rgb.h | 12 +- libheif/context.cc | 13 +- libheif/image-items/grid.cc | 2 +- libheif/image-items/image_item.cc | 12 +- libheif/image-items/mask_image.cc | 3 +- libheif/image-items/overlay.cc | 9 +- libheif/pixelimage.cc | 143 ++++++++++++-------- libheif/pixelimage.h | 38 ++++-- tests/conversion.cc | 22 +-- 30 files changed, 344 insertions(+), 240 deletions(-) diff --git a/fuzzing/color_conversion_fuzzer.cc b/fuzzing/color_conversion_fuzzer.cc index 170ac6790c..b75a99ffa9 100644 --- a/fuzzing/color_conversion_fuzzer.cc +++ b/fuzzing/color_conversion_fuzzer.cc @@ -70,7 +70,7 @@ static bool read_plane(BitstreamRange* range, if (!range->prepare_read(static_cast(width) * height)) { return false; } - if (auto err = image->add_plane2(channel, width, height, bit_depth)) { + if (auto err = image->add_plane(channel, width, height, bit_depth, heif_get_disabled_security_limits())) { return false; } uint32_t stride; @@ -96,7 +96,7 @@ static bool read_plane_interleaved(BitstreamRange* range, if (!range->prepare_read(static_cast(width) * height * comps)) { return false; } - if (auto err = image->add_plane2(channel, width, height, bit_depth)) { + if (auto err = image->add_plane(channel, width, height, bit_depth, heif_get_disabled_security_limits())) { return false; } uint32_t stride; @@ -255,7 +255,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) static_cast(out_chroma), nullptr, output_bpp, - options->color_conversion_options); + options->color_conversion_options, + heif_get_disabled_security_limits()); heif_encoding_options_free(options); diff --git a/libheif/api/libheif/heif.cc b/libheif/api/libheif/heif.cc index ad62025ddd..3b7ce8532f 100644 --- a/libheif/api/libheif/heif.cc +++ b/libheif/api/libheif/heif.cc @@ -1774,7 +1774,7 @@ heif_error heif_image_crop(struct heif_image* img, "Image size exceeds maximum supported size"}; } - auto cropResult = img->image->crop(left, static_cast(w) - 1 - right, top, static_cast(h) - 1 - bottom); + auto cropResult = img->image->crop(left, static_cast(w) - 1 - right, top, static_cast(h) - 1 - bottom, nullptr); if (cropResult.error) { return cropResult.error.error_struct(img->image.get()); } @@ -1806,7 +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 (auto err = image->image->add_plane2(channel, width, height, bit_depth)) { + // Note: no security limit, because this is explicitly requested by the user. + if (auto err = image->image->add_plane(channel, width, height, bit_depth, nullptr)) { return err.error_struct(image->image.get()); } else { @@ -1820,7 +1821,7 @@ struct heif_error heif_image_add_channel(struct heif_image* image, int width, int height, heif_channel_datatype datatype, int bit_depth) { - if (!image->image->add_channel(channel, width, height, datatype, bit_depth)) { + if (!image->image->add_channel(channel, width, height, datatype, bit_depth, nullptr)) { struct heif_error err = {heif_error_Memory_allocation_error, heif_suberror_Unspecified, "Cannot allocate memory for image plane"}; @@ -1994,7 +1995,7 @@ 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) { - Error err = image->image->extend_padding_to_size2(min_physical_width, min_physical_height); + Error err = image->image->extend_padding_to_size(min_physical_width, min_physical_height, false, nullptr); if (err) { return err.error_struct(image->image.get()); } @@ -2011,7 +2012,7 @@ struct heif_error heif_image_scale_image(const struct heif_image* input, { std::shared_ptr out_img; - Error err = input->image->scale_nearest_neighbor(out_img, width, height); + Error err = input->image->scale_nearest_neighbor(out_img, width, height, nullptr); if (err) { return err.error_struct(input->image.get()); } @@ -2026,7 +2027,7 @@ 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) { - Error err = image->image->extend_to_size_with_zero2(width, height); + Error err = image->image->extend_to_size_with_zero(width, height, nullptr); if (err) { return err.error_struct(image->image.get()); } diff --git a/libheif/codecs/uncompressed/unc_codec.cc b/libheif/codecs/uncompressed/unc_codec.cc index b46a49029f..08aa7b537e 100644 --- a/libheif/codecs/uncompressed/unc_codec.cc +++ b/libheif/codecs/uncompressed/unc_codec.cc @@ -441,7 +441,8 @@ static AbstractDecoder* makeDecoder(uint32_t width, uint32_t height, const std:: Result> UncompressedImageCodec::create_image(const std::shared_ptr cmpd, const std::shared_ptr uncC, uint32_t width, - uint32_t height) + uint32_t height, + const heif_security_limits* limits) { auto img = std::make_shared(); heif_chroma chroma = heif_chroma_undefined; @@ -465,12 +466,13 @@ Result> UncompressedImageCodec::create_image(con } if ((channel == heif_channel_Cb) || (channel == heif_channel_Cr)) { - if (auto err = img->add_plane2(channel, (width / chroma_h_subsampling(chroma)), (height / chroma_v_subsampling(chroma)), component.component_bit_depth)) { + if (auto err = img->add_plane(channel, (width / chroma_h_subsampling(chroma)), (height / chroma_v_subsampling(chroma)), component.component_bit_depth, + limits)) { return err; } } else { - if (auto err = img->add_plane2(channel, width, height, component.component_bit_depth)) { + if (auto err = img->add_plane(channel, width, height, component.component_bit_depth, limits)) { return err; } } @@ -505,7 +507,7 @@ Error UncompressedImageCodec::decode_uncompressed_image_tile(const HeifContext* uint32_t tile_width = ispe->get_width() / uncC->get_number_of_tile_columns(); uint32_t tile_height = ispe->get_height() / uncC->get_number_of_tile_rows(); - Result> createImgResult = create_image(cmpd, uncC, tile_width, tile_height); + Result> createImgResult = create_image(cmpd, uncC, tile_width, tile_height, context->get_security_limits()); if (createImgResult.error) { return createImgResult.error; } @@ -629,7 +631,7 @@ Error UncompressedImageCodec::decode_uncompressed_image(const HeifContext* conte return error; } - Result> createImgResult = create_image(cmpd, uncC, width, height); + Result> createImgResult = create_image(cmpd, uncC, width, height, context->get_security_limits()); if (createImgResult.error) { return createImgResult.error; } diff --git a/libheif/codecs/uncompressed/unc_codec.h b/libheif/codecs/uncompressed/unc_codec.h index b244c90eba..24ca3241dc 100644 --- a/libheif/codecs/uncompressed/unc_codec.h +++ b/libheif/codecs/uncompressed/unc_codec.h @@ -68,7 +68,8 @@ class UncompressedImageCodec static Result> create_image(std::shared_ptr, std::shared_ptr, uint32_t width, - uint32_t height); + uint32_t height, + const heif_security_limits* limits); static Error check_header_validity(std::optional>, const std::shared_ptr&, diff --git a/libheif/color-conversion/alpha.cc b/libheif/color-conversion/alpha.cc index 93d2cccd65..9f743feb5d 100644 --- a/libheif/color-conversion/alpha.cc +++ b/libheif/color-conversion/alpha.cc @@ -56,7 +56,8 @@ Result> Op_drop_alpha_plane::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { uint32_t width = input->get_width(); uint32_t height = input->get_height(); @@ -74,7 +75,7 @@ Op_drop_alpha_plane::convert_colorspace(const std::shared_ptrhas_channel(channel)) { - outimg->copy_new_plane_from(input, channel, channel); + outimg->copy_new_plane_from(input, channel, channel, limits); } } diff --git a/libheif/color-conversion/alpha.h b/libheif/color-conversion/alpha.h index e398f1d03b..1bf6c27a45 100644 --- a/libheif/color-conversion/alpha.h +++ b/libheif/color-conversion/alpha.h @@ -38,7 +38,8 @@ class Op_drop_alpha_plane : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; #endif //LIBHEIF_COLORCONVERSION_ALPHA_H diff --git a/libheif/color-conversion/chroma_sampling.cc b/libheif/color-conversion/chroma_sampling.cc index 106a97495d..47b63d3cdf 100644 --- a/libheif/color-conversion/chroma_sampling.cc +++ b/libheif/color-conversion/chroma_sampling.cc @@ -79,7 +79,8 @@ Result> Op_YCbCr444_to_YCbCr420_average::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool hdr = !std::is_same::value; @@ -129,14 +130,14 @@ Op_YCbCr444_to_YCbCr420_average::convert_colorspace(const std::shared_ptr uint32_t cwidth = (width + 1) / 2; uint32_t cheight = (height + 1) / 2; - 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)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, bpp_y, limits) || + outimg->add_plane(heif_channel_Cb, cwidth, cheight, bpp_cb, limits) || + outimg->add_plane(heif_channel_Cr, cwidth, cheight, bpp_cr, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp_a, limits)) { return err; } } @@ -297,7 +298,8 @@ Result> Op_YCbCr444_to_YCbCr422_average::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool hdr = !std::is_same::value; @@ -347,14 +349,14 @@ Op_YCbCr444_to_YCbCr422_average::convert_colorspace(const std::shared_ptr uint32_t cwidth = (width + 1) / 2; uint32_t cheight = height; - 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)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, bpp_y, limits) || + outimg->add_plane(heif_channel_Cb, cwidth, cheight, bpp_cb, limits) || + outimg->add_plane(heif_channel_Cr, cwidth, cheight, bpp_cr, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp_a, limits)) { return err; } } @@ -491,7 +493,8 @@ Result> Op_YCbCr420_bilinear_to_YCbCr444::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool hdr = !std::is_same::value; @@ -538,14 +541,14 @@ Op_YCbCr420_bilinear_to_YCbCr444::convert_colorspace(const std::shared_pt outimg->create(width, height, heif_colorspace_YCbCr, heif_chroma_444); - 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)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, bpp_y, limits) || + outimg->add_plane(heif_channel_Cb, width, height, bpp_cb, limits) || + outimg->add_plane(heif_channel_Cr, width, height, bpp_cr, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp_a, limits)) { return err; } } @@ -768,7 +771,8 @@ Result> Op_YCbCr422_bilinear_to_YCbCr444::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool hdr = !std::is_same::value; @@ -815,14 +819,14 @@ Op_YCbCr422_bilinear_to_YCbCr444::convert_colorspace(const std::shared_pt outimg->create(width, height, heif_colorspace_YCbCr, heif_chroma_444); - 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)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, bpp_y, limits) || + outimg->add_plane(heif_channel_Cb, width, height, bpp_cb, limits) || + outimg->add_plane(heif_channel_Cr, width, height, bpp_cr, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp_a, limits)) { return err; } } diff --git a/libheif/color-conversion/chroma_sampling.h b/libheif/color-conversion/chroma_sampling.h index b0f8e831b8..548e067a91 100644 --- a/libheif/color-conversion/chroma_sampling.h +++ b/libheif/color-conversion/chroma_sampling.h @@ -41,7 +41,8 @@ class Op_YCbCr444_to_YCbCr420_average : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -58,7 +59,8 @@ class Op_YCbCr444_to_YCbCr422_average : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -77,7 +79,8 @@ class Op_YCbCr420_bilinear_to_YCbCr444 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; template @@ -93,7 +96,8 @@ class Op_YCbCr422_bilinear_to_YCbCr444 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; #endif //LIBHEIF_CHROMA_SAMPLING_H diff --git a/libheif/color-conversion/colorconversion.cc b/libheif/color-conversion/colorconversion.cc index 1673534dd0..8be750cd73 100644 --- a/libheif/color-conversion/colorconversion.cc +++ b/libheif/color-conversion/colorconversion.cc @@ -427,7 +427,8 @@ std::string ColorConversionPipeline::debug_dump_pipeline() const } -Result> ColorConversionPipeline::convert_image(const std::shared_ptr& input) +Result> ColorConversionPipeline::convert_image(const std::shared_ptr& input, + const heif_security_limits* limits) { std::shared_ptr in = input; std::shared_ptr out = in; @@ -439,7 +440,7 @@ Result> ColorConversionPipeline::convert_image(c print_spec(std::cerr, in); #endif - auto outResult = step.operation->convert_colorspace(in, step.input_state, step.output_state, m_options); + auto outResult = step.operation->convert_colorspace(in, step.input_state, step.output_state, m_options, limits); if (outResult.error) { return outResult.error; } @@ -487,7 +488,8 @@ Result> convert_colorspace(const std::shared_ptr heif_chroma target_chroma, const std::shared_ptr& target_profile, int output_bpp, - const heif_color_conversion_options& options) + const heif_color_conversion_options& options, + const heif_security_limits* limits) { // --- check that input image is valid @@ -595,7 +597,7 @@ Result> convert_colorspace(const std::shared_ptr return input; } else { - return pipeline.convert_image(input); + return pipeline.convert_image(input, limits); } } @@ -605,11 +607,12 @@ Result> convert_colorspace(const std::shar heif_chroma chroma, const std::shared_ptr& target_profile, int output_bpp, - const heif_color_conversion_options& options) + const heif_color_conversion_options& options, + const heif_security_limits* limits) { std::shared_ptr non_const_input = std::const_pointer_cast(input); - auto result = convert_colorspace(non_const_input, colorspace, chroma, target_profile, output_bpp, options); + auto result = convert_colorspace(non_const_input, colorspace, chroma, target_profile, output_bpp, options, limits); if (result.error) { return result.error; } diff --git a/libheif/color-conversion/colorconversion.h b/libheif/color-conversion/colorconversion.h index d25a2f3636..562f8b186d 100644 --- a/libheif/color-conversion/colorconversion.h +++ b/libheif/color-conversion/colorconversion.h @@ -89,7 +89,8 @@ class ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const = 0; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const = 0; }; @@ -105,7 +106,8 @@ class ColorConversionPipeline const ColorState& target_state, const heif_color_conversion_options& options); - Result> convert_image(const std::shared_ptr& input); + Result> convert_image(const std::shared_ptr& input, + const heif_security_limits* limits); std::string debug_dump_pipeline() const; @@ -131,13 +133,15 @@ Result> convert_colorspace(const std::shared_ptr heif_chroma chroma, const std::shared_ptr& target_profile, int output_bpp, - const heif_color_conversion_options& options); + const heif_color_conversion_options& options, + const heif_security_limits* limits); Result> convert_colorspace(const std::shared_ptr& input, heif_colorspace colorspace, heif_chroma chroma, const std::shared_ptr& target_profile, int output_bpp, - const heif_color_conversion_options& options); + const heif_color_conversion_options& options, + const heif_security_limits* limits); #endif diff --git a/libheif/color-conversion/hdr_sdr.cc b/libheif/color-conversion/hdr_sdr.cc index 83db410ff5..30d64fafa1 100644 --- a/libheif/color-conversion/hdr_sdr.cc +++ b/libheif/color-conversion/hdr_sdr.cc @@ -54,7 +54,8 @@ Result> Op_to_hdr_planes::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { auto outimg = std::make_shared(); @@ -73,7 +74,7 @@ Op_to_hdr_planes::convert_colorspace(const std::shared_ptr if (input->has_channel(channel)) { uint32_t width = input->get_width(channel); uint32_t height = input->get_height(channel); - if (auto err = outimg->add_plane2(channel, width, height, target_state.bits_per_pixel)) { + if (auto err = outimg->add_plane(channel, width, height, target_state.bits_per_pixel, limits)) { return err; } @@ -141,7 +142,8 @@ Result> Op_to_sdr_planes::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { auto outimg = std::make_shared(); @@ -164,7 +166,7 @@ Op_to_sdr_planes::convert_colorspace(const std::shared_ptr if (input_bits > 8) { uint32_t width = input->get_width(channel); uint32_t height = input->get_height(channel); - if (auto err = outimg->add_plane2(channel, width, height, 8)) { + if (auto err = outimg->add_plane(channel, width, height, 8, limits)) { return err; } @@ -187,7 +189,7 @@ Op_to_sdr_planes::convert_colorspace(const std::shared_ptr } else if (input_bits < 8) { uint32_t width = input->get_width(channel); uint32_t height = input->get_height(channel); - if (auto err = outimg->add_plane2(channel, width, height, 8)) { + if (auto err = outimg->add_plane(channel, width, height, 8, limits)) { return err; } @@ -227,7 +229,7 @@ Op_to_sdr_planes::convert_colorspace(const std::shared_ptr p_out[y * stride_out + x] = (uint8_t) ((in * mulFactor) >> 8); } } else { - outimg->copy_new_plane_from(input, channel, channel); + outimg->copy_new_plane_from(input, channel, channel, limits); } } } diff --git a/libheif/color-conversion/hdr_sdr.h b/libheif/color-conversion/hdr_sdr.h index e0f892769f..a497135b00 100644 --- a/libheif/color-conversion/hdr_sdr.h +++ b/libheif/color-conversion/hdr_sdr.h @@ -38,7 +38,8 @@ class Op_to_hdr_planes : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -54,7 +55,8 @@ class Op_to_sdr_planes : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; #endif //LIBHEIF_COLORCONVERSION_HDR_SDR_H diff --git a/libheif/color-conversion/monochrome.cc b/libheif/color-conversion/monochrome.cc index ad33dd32f8..73bed99814 100644 --- a/libheif/color-conversion/monochrome.cc +++ b/libheif/color-conversion/monochrome.cc @@ -53,7 +53,8 @@ Result> Op_mono_to_YCbCr420::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { auto outimg = std::make_shared(); @@ -67,9 +68,9 @@ Op_mono_to_YCbCr420::convert_colorspace(const std::shared_ptradd_plane2(heif_channel_Y, width, height, input_bpp) || - outimg->add_plane2(heif_channel_Cb, chroma_width, chroma_height, input_bpp) || - outimg->add_plane2(heif_channel_Cr, chroma_width, chroma_height, input_bpp)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, input_bpp, limits) || + outimg->add_plane(heif_channel_Cb, chroma_width, chroma_height, input_bpp, limits) || + outimg->add_plane(heif_channel_Cr, chroma_width, chroma_height, input_bpp, limits)) { return err; } @@ -77,7 +78,7 @@ Op_mono_to_YCbCr420::convert_colorspace(const std::shared_ptrhas_channel(heif_channel_Alpha); if (has_alpha) { alpha_bpp = input->get_bits_per_pixel(heif_channel_Alpha); - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, alpha_bpp)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, alpha_bpp, limits)) { return err; } } @@ -204,7 +205,8 @@ Result> Op_mono_to_RGB24_32::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { uint32_t width = input->get_width(); uint32_t height = input->get_height(); @@ -224,7 +226,7 @@ Op_mono_to_RGB24_32::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, heif_chroma_interleaved_24bit); } - if (auto err = outimg->add_plane2(heif_channel_interleaved, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, 8, limits)) { return err; } diff --git a/libheif/color-conversion/monochrome.h b/libheif/color-conversion/monochrome.h index 1de56a1e16..d603b77e90 100644 --- a/libheif/color-conversion/monochrome.h +++ b/libheif/color-conversion/monochrome.h @@ -38,7 +38,8 @@ class Op_mono_to_YCbCr420 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -54,7 +55,8 @@ class Op_mono_to_RGB24_32 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; #endif //LIBHEIF_COLORCONVERSION_MONOCHROME_H diff --git a/libheif/color-conversion/rgb2rgb.cc b/libheif/color-conversion/rgb2rgb.cc index dc5bb6aa52..eb4d7ba659 100644 --- a/libheif/color-conversion/rgb2rgb.cc +++ b/libheif/color-conversion/rgb2rgb.cc @@ -67,7 +67,8 @@ Result> Op_RGB_to_RGB24_32::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool has_alpha = input->has_channel(heif_channel_Alpha); bool want_alpha = target_state.has_alpha; @@ -90,7 +91,7 @@ Op_RGB_to_RGB24_32::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, want_alpha ? heif_chroma_interleaved_32bit : heif_chroma_interleaved_24bit); - if (auto err = outimg->add_plane2(heif_channel_interleaved, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, 8, limits)) { return err; } @@ -190,7 +191,8 @@ Result> Op_RGB_HDR_to_RRGGBBaa_BE::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { if (input->get_bits_per_pixel(heif_channel_R) <= 8 || input->get_bits_per_pixel(heif_channel_G) <= 8 || @@ -221,7 +223,7 @@ Op_RGB_HDR_to_RRGGBBaa_BE::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, output_has_alpha ? heif_chroma_interleaved_RRGGBBAA_BE : heif_chroma_interleaved_RRGGBB_BE); - if (auto err = outimg->add_plane2(heif_channel_interleaved, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, bpp, limits)) { return err; } @@ -324,7 +326,8 @@ Result> Op_RGB_to_RRGGBBaa_BE::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { if (input->get_bits_per_pixel(heif_channel_R) != 8 || input->get_bits_per_pixel(heif_channel_G) != 8 || @@ -349,7 +352,7 @@ Op_RGB_to_RRGGBBaa_BE::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, output_has_alpha ? heif_chroma_interleaved_RRGGBBAA_BE : heif_chroma_interleaved_RRGGBB_BE); - if (auto err = outimg->add_plane2(heif_channel_interleaved, width, height, input->get_bits_per_pixel(heif_channel_R))) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, input->get_bits_per_pixel(heif_channel_R), limits)) { return err; } @@ -441,7 +444,8 @@ Result> Op_RRGGBBaa_BE_to_RGB_HDR::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool has_alpha = (input->get_chroma_format() == heif_chroma_interleaved_RRGGBBAA_LE || input->get_chroma_format() == heif_chroma_interleaved_RRGGBBAA_BE); @@ -455,14 +459,14 @@ Op_RRGGBBaa_BE_to_RGB_HDR::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, heif_chroma_444); - if (auto err = outimg->add_plane2(heif_channel_R, width, height, bpp) || - outimg->add_plane2(heif_channel_G, width, height, bpp) || - outimg->add_plane2(heif_channel_B, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_R, width, height, bpp, limits) || + outimg->add_plane(heif_channel_G, width, height, bpp, limits) || + outimg->add_plane(heif_channel_B, width, height, bpp, limits)) { return err; } if (want_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp, limits)) { return err; } } @@ -553,8 +557,9 @@ Op_RGB24_32_to_RGB::state_after_conversion(const ColorState& input_state, Result> Op_RGB24_32_to_RGB::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, - const ColorState& target_state, - const heif_color_conversion_options& options) const + const ColorState& target_state, + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool has_alpha = input->get_chroma_format() == heif_chroma_interleaved_RGBA; bool want_alpha = target_state.has_alpha; @@ -566,14 +571,14 @@ Op_RGB24_32_to_RGB::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, heif_chroma_444); - if (auto err = outimg->add_plane2(heif_channel_R, width, height, 8) || - outimg->add_plane2(heif_channel_G, width, height, 8) || - outimg->add_plane2(heif_channel_B, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_R, width, height, 8, limits) || + outimg->add_plane(heif_channel_G, width, height, 8, limits) || + outimg->add_plane(heif_channel_B, width, height, 8, limits)) { return err; } if (want_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, 8, limits)) { return err; } } @@ -680,7 +685,8 @@ Result> Op_RRGGBBaa_swap_endianness::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { auto outimg = std::make_shared(); @@ -704,8 +710,8 @@ Op_RRGGBBaa_swap_endianness::convert_colorspace(const std::shared_ptradd_plane2(heif_channel_interleaved, width, height, - input->get_bits_per_pixel(heif_channel_interleaved))) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, + input->get_bits_per_pixel(heif_channel_interleaved), limits)) { return err; } diff --git a/libheif/color-conversion/rgb2rgb.h b/libheif/color-conversion/rgb2rgb.h index c6f46fe552..014ab1c5ef 100644 --- a/libheif/color-conversion/rgb2rgb.h +++ b/libheif/color-conversion/rgb2rgb.h @@ -38,7 +38,8 @@ class Op_RGB_to_RGB24_32 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -54,7 +55,8 @@ class Op_RGB_HDR_to_RRGGBBaa_BE : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -70,7 +72,8 @@ class Op_RGB_to_RRGGBBaa_BE : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -86,7 +89,8 @@ class Op_RRGGBBaa_BE_to_RGB_HDR : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -102,7 +106,8 @@ class Op_RGB24_32_to_RGB : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -118,7 +123,8 @@ class Op_RRGGBBaa_swap_endianness : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; diff --git a/libheif/color-conversion/rgb2yuv.cc b/libheif/color-conversion/rgb2yuv.cc index 3dc34f8964..6867c39727 100644 --- a/libheif/color-conversion/rgb2yuv.cc +++ b/libheif/color-conversion/rgb2yuv.cc @@ -94,7 +94,8 @@ Result> Op_RGB_to_YCbCr::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool hdr = !std::is_same::value; @@ -123,14 +124,14 @@ Op_RGB_to_YCbCr::convert_colorspace(const std::shared_ptradd_plane2(heif_channel_Y, width, height, bpp) || - outimg->add_plane2(heif_channel_Cb, cwidth, cheight, bpp) || - outimg->add_plane2(heif_channel_Cr, cwidth, cheight, bpp)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, bpp, limits) || + outimg->add_plane(heif_channel_Cb, cwidth, cheight, bpp, limits) || + outimg->add_plane(heif_channel_Cr, cwidth, cheight, bpp, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp, limits)) { return err; } } @@ -339,7 +340,8 @@ Result> Op_RRGGBBxx_HDR_to_YCbCr420::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { uint32_t width = input->get_width(); uint32_t height = input->get_height(); @@ -358,14 +360,14 @@ Op_RRGGBBxx_HDR_to_YCbCr420::convert_colorspace(const std::shared_ptradd_plane2(heif_channel_Y, width, height, bpp) || - outimg->add_plane2(heif_channel_Cb, cwidth, cheight, bpp) || - outimg->add_plane2(heif_channel_Cr, cwidth, cheight, bpp)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, bpp, limits) || + outimg->add_plane(heif_channel_Cb, cwidth, cheight, bpp, limits) || + outimg->add_plane(heif_channel_Cr, cwidth, cheight, bpp, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp, limits)) { return err; } } @@ -545,7 +547,8 @@ Result> Op_RGB24_32_to_YCbCr::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { uint32_t width = input->get_width(); uint32_t height = input->get_height(); @@ -564,14 +567,14 @@ Op_RGB24_32_to_YCbCr::convert_colorspace(const std::shared_ptrget_chroma_format() == heif_chroma_interleaved_32bit); const bool want_alpha = target_state.has_alpha; - if (auto err = outimg->add_plane2(heif_channel_Y, width, height, 8) || - outimg->add_plane2(heif_channel_Cb, chroma_width, chroma_height, 8) || - outimg->add_plane2(heif_channel_Cr, chroma_width, chroma_height, 8)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, 8, limits) || + outimg->add_plane(heif_channel_Cb, chroma_width, chroma_height, 8, limits) || + outimg->add_plane(heif_channel_Cr, chroma_width, chroma_height, 8, limits)) { return err; } if (want_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, 8, limits)) { return err; } } @@ -817,7 +820,8 @@ Result> Op_RGB24_32_to_YCbCr444_GBR::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { uint32_t width = input->get_width(); uint32_t height = input->get_height(); @@ -829,14 +833,14 @@ Op_RGB24_32_to_YCbCr444_GBR::convert_colorspace(const std::shared_ptrget_chroma_format() == heif_chroma_interleaved_32bit); const bool want_alpha = target_state.has_alpha; - if (auto err = outimg->add_plane2(heif_channel_Y, width, height, 8) || - outimg->add_plane2(heif_channel_Cb, width, height, 8) || - outimg->add_plane2(heif_channel_Cr, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, 8, limits) || + outimg->add_plane(heif_channel_Cb, width, height, 8, limits) || + outimg->add_plane(heif_channel_Cr, width, height, 8, limits)) { return err; } if (want_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, 8, limits)) { return err; } } diff --git a/libheif/color-conversion/rgb2yuv.h b/libheif/color-conversion/rgb2yuv.h index 7ab2a91fc8..6b4da84798 100644 --- a/libheif/color-conversion/rgb2yuv.h +++ b/libheif/color-conversion/rgb2yuv.h @@ -39,7 +39,8 @@ class Op_RGB_to_YCbCr : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -56,7 +57,8 @@ class Op_RRGGBBxx_HDR_to_YCbCr420 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -72,7 +74,8 @@ class Op_RGB24_32_to_YCbCr : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -88,7 +91,8 @@ class Op_RGB24_32_to_YCbCr444_GBR : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; #endif //LIBHEIF_COLORCONVERSION_RGB2YUV_H diff --git a/libheif/color-conversion/rgb2yuv_sharp.cc b/libheif/color-conversion/rgb2yuv_sharp.cc index 2326ca4715..13ea950cb7 100644 --- a/libheif/color-conversion/rgb2yuv_sharp.cc +++ b/libheif/color-conversion/rgb2yuv_sharp.cc @@ -126,7 +126,8 @@ Op_Any_RGB_to_YCbCr_420_Sharp::convert_colorspace( const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { #ifdef HAVE_LIBSHARPYUV uint32_t width = input->get_width(); @@ -154,14 +155,14 @@ Op_Any_RGB_to_YCbCr_420_Sharp::convert_colorspace( bool want_alpha = target_state.has_alpha; int output_bits = target_state.bits_per_pixel; - if (auto err = outimg->add_plane2(heif_channel_Y, width, height, output_bits) || - outimg->add_plane2(heif_channel_Cb, chroma_width, chroma_height, output_bits) || - outimg->add_plane2(heif_channel_Cr, chroma_width, chroma_height, output_bits)) { + if (auto err = outimg->add_plane(heif_channel_Y, width, height, output_bits, limits) || + outimg->add_plane(heif_channel_Cb, chroma_width, chroma_height, output_bits, limits) || + outimg->add_plane(heif_channel_Cr, chroma_width, chroma_height, output_bits, limits)) { return err; } if (want_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, output_bits)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, output_bits, limits)) { return err; } } diff --git a/libheif/color-conversion/rgb2yuv_sharp.h b/libheif/color-conversion/rgb2yuv_sharp.h index ba49939257..449637bac0 100644 --- a/libheif/color-conversion/rgb2yuv_sharp.h +++ b/libheif/color-conversion/rgb2yuv_sharp.h @@ -38,7 +38,8 @@ class Op_Any_RGB_to_YCbCr_420_Sharp : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; diff --git a/libheif/color-conversion/yuv2rgb.cc b/libheif/color-conversion/yuv2rgb.cc index dc33cd109f..8c689a57c2 100644 --- a/libheif/color-conversion/yuv2rgb.cc +++ b/libheif/color-conversion/yuv2rgb.cc @@ -86,7 +86,8 @@ Result> Op_YCbCr_to_RGB::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { bool hdr = !std::is_same::value; @@ -135,14 +136,14 @@ Op_YCbCr_to_RGB::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, heif_chroma_444); - if (auto err = outimg->add_plane2(heif_channel_R, width, height, bpp_y) || - outimg->add_plane2(heif_channel_G, width, height, bpp_y) || - outimg->add_plane2(heif_channel_B, width, height, bpp_y)) { + if (auto err = outimg->add_plane(heif_channel_R, width, height, bpp_y, limits) || + outimg->add_plane(heif_channel_G, width, height, bpp_y, limits) || + outimg->add_plane(heif_channel_B, width, height, bpp_y, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp_a)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp_a, limits)) { return err; } } @@ -312,7 +313,8 @@ Result> Op_YCbCr420_to_RGB24::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { if (input->get_bits_per_pixel(heif_channel_Y) != 8 || input->get_bits_per_pixel(heif_channel_Cb) != 8 || @@ -327,7 +329,7 @@ Op_YCbCr420_to_RGB24::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, heif_chroma_interleaved_24bit); - if (auto err = outimg->add_plane2(heif_channel_interleaved, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, 8, limits)) { return err; } @@ -422,7 +424,8 @@ Result> Op_YCbCr420_to_RGB32::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { if (input->get_bits_per_pixel(heif_channel_Y) != 8 || input->get_bits_per_pixel(heif_channel_Cb) != 8 || @@ -437,7 +440,7 @@ Op_YCbCr420_to_RGB32::convert_colorspace(const std::shared_ptrcreate(width, height, heif_colorspace_RGB, heif_chroma_interleaved_32bit); - if (auto err = outimg->add_plane2(heif_channel_interleaved, width, height, 8)) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, 8, limits)) { return err; } @@ -556,7 +559,8 @@ Result> Op_YCbCr420_to_RRGGBBaa::convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const + const heif_color_conversion_options& options, + const heif_security_limits* limits) const { uint32_t width = input->get_width(); uint32_t height = input->get_height(); @@ -572,12 +576,12 @@ Op_YCbCr420_to_RRGGBBaa::convert_colorspace(const std::shared_ptradd_plane2(heif_channel_interleaved, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_interleaved, width, height, bpp, limits)) { return err; } if (has_alpha) { - if (auto err = outimg->add_plane2(heif_channel_Alpha, width, height, bpp)) { + if (auto err = outimg->add_plane(heif_channel_Alpha, width, height, bpp, limits)) { return err; } } diff --git a/libheif/color-conversion/yuv2rgb.h b/libheif/color-conversion/yuv2rgb.h index c61331635c..d34d44737b 100644 --- a/libheif/color-conversion/yuv2rgb.h +++ b/libheif/color-conversion/yuv2rgb.h @@ -40,7 +40,8 @@ class Op_YCbCr_to_RGB : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -56,7 +57,8 @@ class Op_YCbCr420_to_RGB24 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -72,7 +74,8 @@ class Op_YCbCr420_to_RGB32 : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; @@ -88,7 +91,8 @@ class Op_YCbCr420_to_RRGGBBaa : public ColorConversionOperation convert_colorspace(const std::shared_ptr& input, const ColorState& input_state, const ColorState& target_state, - const heif_color_conversion_options& options) const override; + const heif_color_conversion_options& options, + const heif_security_limits* limits) const override; }; #endif //LIBHEIF_COLORCONVERSION_YUV2RGB_H diff --git a/libheif/context.cc b/libheif/context.cc index e096b9a60f..e732325a7a 100644 --- a/libheif/context.cc +++ b/libheif/context.cc @@ -1087,7 +1087,7 @@ Result> HeifContext::decode_image(heif_item_id I // TODO: check BPP changed if (different_chroma || different_colorspace) { - auto img_result = convert_colorspace(img, target_colorspace, target_chroma, nullptr, bpp, options.color_conversion_options); + auto img_result = convert_colorspace(img, target_colorspace, target_chroma, nullptr, bpp, options.color_conversion_options, get_security_limits()); if (img_result.error) { return img_result.error; } @@ -1103,7 +1103,8 @@ Result> HeifContext::decode_image(heif_item_id I static Result> -create_alpha_image_from_image_alpha_channel(const std::shared_ptr& image) +create_alpha_image_from_image_alpha_channel(const std::shared_ptr& image, + const heif_security_limits* limits) { // --- generate alpha image @@ -1112,10 +1113,10 @@ create_alpha_image_from_image_alpha_channel(const std::shared_ptrhas_channel(heif_channel_Alpha)) { - alpha_image->copy_new_plane_from(image, heif_channel_Alpha, heif_channel_Y); + alpha_image->copy_new_plane_from(image, heif_channel_Alpha, heif_channel_Y, limits); } else if (image->get_chroma_format() == heif_chroma_interleaved_RGBA) { - if (auto err = alpha_image->extract_alpha_from_RGBA2(image)) { + if (auto err = alpha_image->extract_alpha_from_RGBA(image, limits)) { return err; } } @@ -1200,7 +1201,7 @@ Result> HeifContext::encode_image(const std::shared_p // TODO: can we directly code a monochrome image instead of the dummy color channels? std::shared_ptr alpha_image; - auto alpha_image_result = create_alpha_image_from_image_alpha_channel(colorConvertedImage); + auto alpha_image_result = create_alpha_image_from_image_alpha_channel(colorConvertedImage, get_security_limits()); if (!alpha_image_result) { return alpha_image_result.error; } @@ -1300,7 +1301,7 @@ Result> HeifContext::encode_thumbnail(const std::shar std::shared_ptr thumbnail_image; - Error error = image->scale_nearest_neighbor(thumbnail_image, thumb_width, thumb_height); + Error error = image->scale_nearest_neighbor(thumbnail_image, thumb_width, thumb_height, get_security_limits()); if (error) { return error; } diff --git a/libheif/image-items/grid.cc b/libheif/image-items/grid.cc index cbc127540b..cc1002e346 100644 --- a/libheif/image-items/grid.cc +++ b/libheif/image-items/grid.cc @@ -453,7 +453,7 @@ Error ImageItem_Grid::decode_and_paste_tile_image(heif_item_id tileID, uint32_t if (!inout_image) { auto grid_image = std::make_shared(); - auto err = grid_image->create_clone_image_at_new_size2(tile_img, w, h); + auto err = grid_image->create_clone_image_at_new_size(tile_img, w, h, get_context()->get_security_limits()); if (err) { return err; } diff --git a/libheif/image-items/image_item.cc b/libheif/image-items/image_item.cc index 887a1be3bf..de3d4ce014 100644 --- a/libheif/image-items/image_item.cc +++ b/libheif/image-items/image_item.cc @@ -618,7 +618,8 @@ Result> ImageItem::convert_colorspace_for_encodi //target_nclx->set_from_heif_color_profile_nclx(target_heif_nclx); auto output_image_result = convert_colorspace(image, colorspace, chroma, target_nclx_profile, - output_bpp, options.color_conversion_options); + output_bpp, options.color_conversion_options, + get_context()->get_security_limits()); if (output_image_result.error) { return output_image_result.error; } @@ -799,7 +800,7 @@ Result> ImageItem::decode_image(const struct hei for (const auto& property : properties) { if (auto rot = std::dynamic_pointer_cast(property)) { - auto rotateResult = img->rotate_ccw(rot->get_rotation_ccw()); + auto rotateResult = img->rotate_ccw(rot->get_rotation_ccw(), m_heif_context->get_security_limits()); if (rotateResult.error) { return error; } @@ -809,7 +810,8 @@ Result> ImageItem::decode_image(const struct hei if (auto mirror = std::dynamic_pointer_cast(property)) { - auto mirrorResult = img->mirror_inplace(mirror->get_mirror_direction()); + auto mirrorResult = img->mirror_inplace(mirror->get_mirror_direction(), + get_context()->get_security_limits()); if (mirrorResult.error) { return error; } @@ -843,7 +845,7 @@ Result> ImageItem::decode_image(const struct hei heif_suberror_Invalid_clean_aperture); } - auto cropResult = img->crop(left, right, top, bottom); + auto cropResult = img->crop(left, right, top, bottom, m_heif_context->get_security_limits()); if (error) { return error; } @@ -898,7 +900,7 @@ Result> ImageItem::decode_image(const struct hei if ((alpha_image->get_width() != img->get_width()) || (alpha_image->get_height() != img->get_height())) { std::shared_ptr scaled_alpha; - Error err = alpha->scale_nearest_neighbor(scaled_alpha, img->get_width(), img->get_height()); + Error err = alpha->scale_nearest_neighbor(scaled_alpha, img->get_width(), img->get_height(), m_heif_context->get_security_limits()); if (err) { return err; } diff --git a/libheif/image-items/mask_image.cc b/libheif/image-items/mask_image.cc index cf7cc3c735..9c2c4e6a40 100644 --- a/libheif/image-items/mask_image.cc +++ b/libheif/image-items/mask_image.cc @@ -105,7 +105,8 @@ Error MaskImageCodec::decode_mask_image(const HeifContext* context, img = std::make_shared(); img->create(width, height, heif_colorspace_monochrome, heif_chroma_monochrome); - auto err = img->add_plane2(heif_channel_Y, width, height, mskC->get_bits_per_pixel()); + auto err = img->add_plane(heif_channel_Y, width, height, mskC->get_bits_per_pixel(), + context->get_security_limits()); if (err) { return err; } diff --git a/libheif/image-items/overlay.cc b/libheif/image-items/overlay.cc index 8f2078e3b2..c010fee8e5 100644 --- a/libheif/image-items/overlay.cc +++ b/libheif/image-items/overlay.cc @@ -297,13 +297,13 @@ Result> ImageItem_Overlay::decode_overlay_image( img->create(w, h, heif_colorspace_RGB, heif_chroma_444); - if (auto error = img->add_plane2(heif_channel_R, w, h, 8)) { // TODO: other bit depths + if (auto error = img->add_plane(heif_channel_R, w, h, 8, get_context()->get_security_limits())) { // TODO: other bit depths return error; } - if (auto error = img->add_plane2(heif_channel_G, w, h, 8)) { // TODO: other bit depths + if (auto error = img->add_plane(heif_channel_G, w, h, 8, get_context()->get_security_limits())) { // TODO: other bit depths return error; } - if (auto error = img->add_plane2(heif_channel_B, w, h, 8)) { // TODO: other bit depths + if (auto error = img->add_plane(heif_channel_B, w, h, 8, get_context()->get_security_limits())) { // TODO: other bit depths return error; } @@ -345,7 +345,8 @@ Result> ImageItem_Overlay::decode_overlay_image( if (overlay_img->get_colorspace() != heif_colorspace_RGB || overlay_img->get_chroma_format() != heif_chroma_444) { - auto overlay_img_result = convert_colorspace(overlay_img, heif_colorspace_RGB, heif_chroma_444, nullptr, 0, options.color_conversion_options); + auto overlay_img_result = convert_colorspace(overlay_img, heif_colorspace_RGB, heif_chroma_444, nullptr, 0, options.color_conversion_options, + get_context()->get_security_limits()); if (overlay_img_result.error) { return overlay_img_result.error; } diff --git a/libheif/pixelimage.cc b/libheif/pixelimage.cc index a5cd194dc3..eac54113d7 100644 --- a/libheif/pixelimage.cc +++ b/libheif/pixelimage.cc @@ -189,7 +189,8 @@ static uint32_t rounded_size(uint32_t s) return s; } -Error HeifPixelImage::add_plane2(heif_channel channel, uint32_t width, uint32_t height, int bit_depth) +Error HeifPixelImage::add_plane(heif_channel channel, uint32_t width, uint32_t height, int bit_depth, + const heif_security_limits* limits) { assert(!has_channel(channel)); @@ -206,7 +207,7 @@ Error HeifPixelImage::add_plane2(heif_channel channel, uint32_t width, uint32_t bit_depth = 8; } - if (auto err = plane.alloc2(width, height, heif_channel_datatype_unsigned_integer, bit_depth, num_interleaved_pixels)) { + if (auto err = plane.alloc(width, height, heif_channel_datatype_unsigned_integer, bit_depth, num_interleaved_pixels, limits)) { return err; } else { @@ -216,10 +217,11 @@ Error HeifPixelImage::add_plane2(heif_channel channel, uint32_t width, uint32_t } -bool HeifPixelImage::add_channel(heif_channel channel, uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth) +Error HeifPixelImage::add_channel(heif_channel channel, uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth, + const heif_security_limits* limits) { ImagePlane plane; - if (Error err = plane.alloc2(width, height, datatype, bit_depth, 1)) { + if (Error err = plane.alloc(width, height, datatype, bit_depth, 1, limits)) { return err; } else { @@ -229,8 +231,9 @@ bool HeifPixelImage::add_channel(heif_channel channel, uint32_t width, uint32_t } -Error HeifPixelImage::ImagePlane::alloc2(uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth, - int num_interleaved_components) // heif_chroma chroma) +Error HeifPixelImage::ImagePlane::alloc(uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth, + int num_interleaved_components, + const heif_security_limits* limits) { assert(bit_depth >= 1); assert(bit_depth <= 128); @@ -257,10 +260,15 @@ Error HeifPixelImage::ImagePlane::alloc2(uint32_t width, uint32_t height, heif_c stride = m_mem_width * bytes_per_pixel; stride = (stride + alignment - 1U) & ~(alignment - 1U); - if ((heif_get_global_security_limits()->max_memory_block_size - (alignment - 1)) / stride < m_mem_height) { + assert(alignment>=1); + + if (limits && + limits->max_memory_block_size && + (limits->max_memory_block_size < alignment - 1U || + (limits->max_memory_block_size - (alignment - 1U)) / stride < m_mem_height)) { std::stringstream sstr; sstr << "Allocating " << static_cast(m_mem_height) * stride + alignment - 1 << " exceeds the security limit of " - << heif_get_global_security_limits()->max_memory_block_size << " bytes"; + << limits->max_memory_block_size << " bytes"; return {heif_error_Memory_allocation_error, heif_suberror_Security_limit_exceeded, @@ -294,7 +302,8 @@ Error HeifPixelImage::ImagePlane::alloc2(uint32_t width, uint32_t height, heif_c } -Error HeifPixelImage::extend_padding_to_size2(uint32_t width, uint32_t height, bool adjust_size) +Error HeifPixelImage::extend_padding_to_size(uint32_t width, uint32_t height, bool adjust_size, + const heif_security_limits* limits) { for (auto& planeIter : m_planes) { auto* plane = &planeIter.second; @@ -312,7 +321,10 @@ Error HeifPixelImage::extend_padding_to_size2(uint32_t width, uint32_t height, b plane->m_mem_height < subsampled_height) { ImagePlane newPlane; - if (auto err = newPlane.alloc2(subsampled_width, subsampled_height, plane->m_datatype, plane->m_bit_depth, num_interleaved_pixels_per_plane(m_chroma))) { + if (auto err = newPlane.alloc(subsampled_width, subsampled_height, plane->m_datatype, plane->m_bit_depth, + num_interleaved_pixels_per_plane(m_chroma), + limits)) + { return err; } @@ -369,7 +381,7 @@ Error HeifPixelImage::extend_padding_to_size2(uint32_t width, uint32_t height, b } -Error HeifPixelImage::extend_to_size_with_zero2(uint32_t width, uint32_t height) +Error HeifPixelImage::extend_to_size_with_zero(uint32_t width, uint32_t height, const heif_security_limits* limits) { for (auto& planeIter : m_planes) { auto* plane = &planeIter.second; @@ -387,7 +399,7 @@ Error HeifPixelImage::extend_to_size_with_zero2(uint32_t width, uint32_t height) plane->m_mem_height < subsampled_height) { ImagePlane newPlane; - if (auto err = newPlane.alloc2(subsampled_width, subsampled_height, plane->m_datatype, plane->m_bit_depth, num_interleaved_pixels_per_plane(m_chroma))) { + if (auto err = newPlane.alloc(subsampled_width, subsampled_height, plane->m_datatype, plane->m_bit_depth, num_interleaved_pixels_per_plane(m_chroma), limits)) { return err; } @@ -546,9 +558,10 @@ int HeifPixelImage::get_number_of_interleaved_components(heif_channel channel) c } -void HeifPixelImage::copy_new_plane_from(const std::shared_ptr& src_image, - heif_channel src_channel, - heif_channel dst_channel) +Error HeifPixelImage::copy_new_plane_from(const std::shared_ptr& src_image, + heif_channel src_channel, + heif_channel dst_channel, + const heif_security_limits* limits) { assert(src_image->has_channel(src_channel)); assert(!has_channel(dst_channel)); @@ -560,9 +573,12 @@ void HeifPixelImage::copy_new_plane_from(const std::shared_ptrm_planes.end()); const auto& src_plane = src_plane_iter->second; - add_channel(dst_channel, width, height, - src_plane.m_datatype, - src_image->get_bits_per_pixel(src_channel)); + auto err = add_channel(dst_channel, width, height, + src_plane.m_datatype, + src_image->get_bits_per_pixel(src_channel), limits); + if (err) { + return err; + } uint8_t* dst; uint32_t dst_stride = 0; @@ -578,15 +594,18 @@ void HeifPixelImage::copy_new_plane_from(const std::shared_ptr& src_image) +Error HeifPixelImage::extract_alpha_from_RGBA(const std::shared_ptr& src_image, + const heif_security_limits* limits) { uint32_t width = src_image->get_width(); uint32_t height = src_image->get_height(); - if (Error err = add_plane2(heif_channel_Y, width, height, src_image->get_bits_per_pixel(heif_channel_interleaved))) { + if (Error err = add_plane(heif_channel_Y, width, height, src_image->get_bits_per_pixel(heif_channel_interleaved), limits)) { return err; } @@ -611,9 +630,10 @@ Error HeifPixelImage::extract_alpha_from_RGBA2(const std::shared_ptr& } -Result> HeifPixelImage::rotate_ccw(int angle_degrees) +Result> HeifPixelImage::rotate_ccw(int angle_degrees, const heif_security_limits* limits) { // --- for some subsampled chroma colorspaces, we have to transform to 4:4:4 before rotation @@ -774,12 +794,12 @@ Result> HeifPixelImage::rotate_ccw(int angle_deg heif_color_conversion_options options{}; heif_color_conversion_options_set_defaults(&options); - auto converted_image_result = convert_colorspace(shared_from_this(), heif_colorspace_YCbCr, heif_chroma_444, nullptr, get_bits_per_pixel(heif_channel_Y), options); + auto converted_image_result = convert_colorspace(shared_from_this(), heif_colorspace_YCbCr, heif_chroma_444, nullptr, get_bits_per_pixel(heif_channel_Y), options, limits); if (converted_image_result.error) { return converted_image_result.error; } - return (*converted_image_result)->rotate_ccw(angle_degrees); + return (*converted_image_result)->rotate_ccw(angle_degrees, limits); } @@ -821,7 +841,10 @@ Result> HeifPixelImage::rotate_ccw(int angle_deg std::swap(out_plane_width, out_plane_height); } - out_img->add_channel(channel, out_plane_width, out_plane_height, plane.m_datatype, plane.m_bit_depth); + Error err = out_img->add_channel(channel, out_plane_width, out_plane_height, plane.m_datatype, plane.m_bit_depth, limits); + if (err) { + return err; + } auto out_plane_iter = out_img->m_planes.find(channel); assert(out_plane_iter != out_img->m_planes.end()); @@ -905,7 +928,8 @@ void HeifPixelImage::ImagePlane::mirror_inplace(heif_transform_mirror_direction } -Result> HeifPixelImage::mirror_inplace(heif_transform_mirror_direction direction) +Result> HeifPixelImage::mirror_inplace(heif_transform_mirror_direction direction, + const heif_security_limits* limits) { // --- for some subsampled chroma colorspaces, we have to transform to 4:4:4 before rotation @@ -926,12 +950,12 @@ Result> HeifPixelImage::mirror_inplace(heif_tran heif_color_conversion_options options{}; heif_color_conversion_options_set_defaults(&options); - auto converted_image_result = convert_colorspace(shared_from_this(), heif_colorspace_YCbCr, heif_chroma_444, nullptr, get_bits_per_pixel(heif_channel_Y), options); + auto converted_image_result = convert_colorspace(shared_from_this(), heif_colorspace_YCbCr, heif_chroma_444, nullptr, get_bits_per_pixel(heif_channel_Y), options, limits); if (converted_image_result.error) { return converted_image_result.error; } - return (*converted_image_result)->mirror_inplace(direction); + return (*converted_image_result)->mirror_inplace(direction, limits); } @@ -987,7 +1011,8 @@ int HeifPixelImage::ImagePlane::get_bytes_per_pixel() const } -Result> HeifPixelImage::crop(uint32_t left, uint32_t right, uint32_t top, uint32_t bottom) const +Result> HeifPixelImage::crop(uint32_t left, uint32_t right, uint32_t top, uint32_t bottom, + const heif_security_limits* limits) const { // --- for some subsampled chroma colorspaces, we have to transform to 4:4:4 before rotation @@ -1005,12 +1030,12 @@ Result> HeifPixelImage::crop(uint32_t left, uint heif_color_conversion_options options{}; heif_color_conversion_options_set_defaults(&options); - auto converted_image_result = convert_colorspace(shared_from_this(), heif_colorspace_YCbCr, heif_chroma_444, nullptr, get_bits_per_pixel(heif_channel_Y), options); + auto converted_image_result = convert_colorspace(shared_from_this(), heif_colorspace_YCbCr, heif_chroma_444, nullptr, get_bits_per_pixel(heif_channel_Y), options, limits); if (converted_image_result.error) { return converted_image_result.error; } - return (*converted_image_result)->crop(left, right, top, bottom); + return (*converted_image_result)->crop(left, right, top, bottom, limits); } @@ -1033,11 +1058,15 @@ Result> HeifPixelImage::crop(uint32_t left, uint uint32_t plane_top = top * h / m_height; uint32_t plane_bottom = bottom * h / m_height; - out_img->add_channel(channel, - plane_right - plane_left + 1, - plane_bottom - plane_top + 1, - plane.m_datatype, - plane.m_bit_depth); + auto err = out_img->add_channel(channel, + plane_right - plane_left + 1, + plane_bottom - plane_top + 1, + plane.m_datatype, + plane.m_bit_depth, + limits); + if (err) { + return err; + } auto out_plane_iter = out_img->m_planes.find(channel); assert(out_plane_iter != out_img->m_planes.end()); @@ -1283,7 +1312,8 @@ Error HeifPixelImage::overlay(std::shared_ptr& overlay, int32_t Error HeifPixelImage::scale_nearest_neighbor(std::shared_ptr& out_img, - uint32_t width, uint32_t height) const + uint32_t width, uint32_t height, + const heif_security_limits* limits) const { out_img = std::make_shared(); out_img->create(width, height, m_colorspace, m_chroma); @@ -1292,7 +1322,7 @@ Error HeifPixelImage::scale_nearest_neighbor(std::shared_ptr& ou // --- create output image with scaled planes if (has_channel(heif_channel_interleaved)) { - if (auto err = out_img->add_plane2(heif_channel_interleaved, width, height, get_bits_per_pixel(heif_channel_interleaved))) { + if (auto err = out_img->add_plane(heif_channel_interleaved, width, height, get_bits_per_pixel(heif_channel_interleaved), limits)) { return err; } } @@ -1304,13 +1334,13 @@ Error HeifPixelImage::scale_nearest_neighbor(std::shared_ptr& ou return {heif_error_Invalid_input, heif_suberror_Unspecified, "RGB input without R,G,B, planes"}; } - if (auto err = out_img->add_plane2(heif_channel_R, width, height, get_bits_per_pixel(heif_channel_R))) { + if (auto err = out_img->add_plane(heif_channel_R, width, height, get_bits_per_pixel(heif_channel_R), limits)) { return err; } - if (auto err = out_img->add_plane2(heif_channel_G, width, height, get_bits_per_pixel(heif_channel_G))) { + if (auto err = out_img->add_plane(heif_channel_G, width, height, get_bits_per_pixel(heif_channel_G), limits)) { return err; } - if (auto err = out_img->add_plane2(heif_channel_B, width, height, get_bits_per_pixel(heif_channel_B))) { + if (auto err = out_img->add_plane(heif_channel_B, width, height, get_bits_per_pixel(heif_channel_B), limits)) { return err; } } @@ -1319,7 +1349,7 @@ Error HeifPixelImage::scale_nearest_neighbor(std::shared_ptr& ou return {heif_error_Invalid_input, heif_suberror_Unspecified, "monochrome input with no Y plane"}; } - if (auto err = out_img->add_plane2(heif_channel_Y, width, height, get_bits_per_pixel(heif_channel_Y))) { + if (auto err = out_img->add_plane(heif_channel_Y, width, height, get_bits_per_pixel(heif_channel_Y), limits)) { return err; } } @@ -1332,13 +1362,13 @@ Error HeifPixelImage::scale_nearest_neighbor(std::shared_ptr& ou uint32_t cw, ch; get_subsampled_size(width, height, heif_channel_Cb, get_chroma_format(), &cw, &ch); - if (auto err = out_img->add_plane2(heif_channel_Y, width, height, get_bits_per_pixel(heif_channel_Y))) { + if (auto err = out_img->add_plane(heif_channel_Y, width, height, get_bits_per_pixel(heif_channel_Y), limits)) { return err; } - if (auto err = out_img->add_plane2(heif_channel_Cb, cw, ch, get_bits_per_pixel(heif_channel_Cb))) { + if (auto err = out_img->add_plane(heif_channel_Cb, cw, ch, get_bits_per_pixel(heif_channel_Cb), limits)) { return err; } - if (auto err = out_img->add_plane2(heif_channel_Cr, cw, ch, get_bits_per_pixel(heif_channel_Cr))) { + if (auto err = out_img->add_plane(heif_channel_Cr, cw, ch, get_bits_per_pixel(heif_channel_Cr), limits)) { return err; } } @@ -1347,7 +1377,7 @@ Error HeifPixelImage::scale_nearest_neighbor(std::shared_ptr& ou } if (has_channel(heif_channel_Alpha)) { - if (auto err = out_img->add_plane2(heif_channel_Alpha, width, height, get_bits_per_pixel(heif_channel_Alpha))) { + if (auto err = out_img->add_plane(heif_channel_Alpha, width, height, get_bits_per_pixel(heif_channel_Alpha), limits)) { return err; } } @@ -1425,7 +1455,8 @@ void HeifPixelImage::debug_dump() const } } -Error HeifPixelImage::create_clone_image_at_new_size2(const std::shared_ptr& source, uint32_t w, uint32_t h) +Error HeifPixelImage::create_clone_image_at_new_size(const std::shared_ptr& source, uint32_t w, uint32_t h, + const heif_security_limits* limits) { heif_colorspace colorspace = source->get_colorspace(); heif_chroma chroma = source->get_chroma_format(); @@ -1434,29 +1465,29 @@ Error HeifPixelImage::create_clone_image_at_new_size2(const std::shared_ptrget_bits_per_pixel(heif_channel_Y))) { + if (auto err = add_plane(heif_channel_Y, w, h, source->get_bits_per_pixel(heif_channel_Y), limits)) { return err; } break; case heif_colorspace_YCbCr: - if (auto err = add_plane2(heif_channel_Y, w, h, source->get_bits_per_pixel(heif_channel_Y))) { + if (auto err = add_plane(heif_channel_Y, w, h, source->get_bits_per_pixel(heif_channel_Y), limits)) { return err; } - if (auto err = add_plane2(heif_channel_Cb, chroma_width(w, chroma), chroma_height(h, chroma), source->get_bits_per_pixel(heif_channel_Cb))) { + if (auto err = add_plane(heif_channel_Cb, chroma_width(w, chroma), chroma_height(h, chroma), source->get_bits_per_pixel(heif_channel_Cb), limits)) { return err; } - if (auto err = add_plane2(heif_channel_Cr, chroma_width(w, chroma), chroma_height(h, chroma), source->get_bits_per_pixel(heif_channel_Cr))) { + if (auto err = add_plane(heif_channel_Cr, chroma_width(w, chroma), chroma_height(h, chroma), source->get_bits_per_pixel(heif_channel_Cr), limits)) { return err; } break; case heif_colorspace_RGB: - if (auto err = add_plane2(heif_channel_R, w, h, source->get_bits_per_pixel(heif_channel_R))) { + if (auto err = add_plane(heif_channel_R, w, h, source->get_bits_per_pixel(heif_channel_R), limits)) { return err; } - if (auto err = add_plane2(heif_channel_G, w, h, source->get_bits_per_pixel(heif_channel_G))) { + if (auto err = add_plane(heif_channel_G, w, h, source->get_bits_per_pixel(heif_channel_G), limits)) { return err; } - if (auto err = add_plane2(heif_channel_B, w, h, source->get_bits_per_pixel(heif_channel_B))) { + if (auto err = add_plane(heif_channel_B, w, h, source->get_bits_per_pixel(heif_channel_B), limits)) { return err; } break; @@ -1466,7 +1497,7 @@ Error HeifPixelImage::create_clone_image_at_new_size2(const std::shared_ptrhas_alpha()) { - if (auto err = add_plane2(heif_channel_Alpha, w, h, source->get_bits_per_pixel(heif_channel_Alpha))) { + if (auto err = add_plane(heif_channel_Alpha, w, h, source->get_bits_per_pixel(heif_channel_Alpha), limits)) { return err; } } diff --git a/libheif/pixelimage.h b/libheif/pixelimage.h index fcbc3aba2a..f30729cb74 100644 --- a/libheif/pixelimage.h +++ b/libheif/pixelimage.h @@ -81,11 +81,13 @@ class HeifPixelImage : public std::enable_shared_from_this, void create(uint32_t width, uint32_t height, heif_colorspace colorspace, heif_chroma chroma); - Error create_clone_image_at_new_size2(const std::shared_ptr& source, uint32_t w, uint32_t h); + Error create_clone_image_at_new_size(const std::shared_ptr& source, uint32_t w, uint32_t h, + const heif_security_limits* limits); - Error add_plane2(heif_channel channel, uint32_t width, uint32_t height, int bit_depth); + Error add_plane(heif_channel channel, uint32_t width, uint32_t height, int bit_depth, const heif_security_limits* limits); - bool add_channel(heif_channel channel, uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth); + Error add_channel(heif_channel channel, uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth, + const heif_security_limits* limits); bool has_channel(heif_channel channel) const; @@ -152,15 +154,16 @@ class HeifPixelImage : public std::enable_shared_from_this, return const_cast(this)->get_channel(channel, out_stride); } - void copy_new_plane_from(const std::shared_ptr& src_image, - heif_channel src_channel, - heif_channel dst_channel); + Error copy_new_plane_from(const std::shared_ptr& src_image, + heif_channel src_channel, + heif_channel dst_channel, + const heif_security_limits* limits); - Error extract_alpha_from_RGBA2(const std::shared_ptr& srcimage); + Error extract_alpha_from_RGBA(const std::shared_ptr& srcimage, const heif_security_limits* limits); void fill_plane(heif_channel dst_channel, uint16_t value); - Error fill_new_plane2(heif_channel dst_channel, uint16_t value, int width, int height, int bpp); + Error fill_new_plane(heif_channel dst_channel, uint16_t value, int width, int height, int bpp, const heif_security_limits* limits); void transfer_plane_from_image_as(const std::shared_ptr& source, heif_channel src_channel, @@ -168,17 +171,19 @@ class HeifPixelImage : public std::enable_shared_from_this, Error copy_image_to(const std::shared_ptr& source, uint32_t x0, uint32_t y0); - Result> rotate_ccw(int angle_degrees); + Result> rotate_ccw(int angle_degrees, const heif_security_limits* limits); - Result> mirror_inplace(heif_transform_mirror_direction); + Result> mirror_inplace(heif_transform_mirror_direction, const heif_security_limits* limits); - Result> crop(uint32_t left, uint32_t right, uint32_t top, uint32_t bottom) const; + Result> crop(uint32_t left, uint32_t right, uint32_t top, uint32_t bottom, + const heif_security_limits* limits) const; Error fill_RGB_16bit(uint16_t r, uint16_t g, uint16_t b, uint16_t a); Error overlay(std::shared_ptr& overlay, int32_t dx, int32_t dy); - Error scale_nearest_neighbor(std::shared_ptr& output, uint32_t width, uint32_t height) const; + Error scale_nearest_neighbor(std::shared_ptr& output, uint32_t width, uint32_t height, + const heif_security_limits* limits) const; void set_color_profile_nclx(const std::shared_ptr& profile) { m_color_profile_nclx = profile; } @@ -190,9 +195,10 @@ class HeifPixelImage : public std::enable_shared_from_this, void debug_dump() const; - Error extend_padding_to_size2(uint32_t width, uint32_t height, bool adjust_size = false); + Error extend_padding_to_size(uint32_t width, uint32_t height, bool adjust_size, + const heif_security_limits* limits); - Error extend_to_size_with_zero2(uint32_t width, uint32_t height); + Error extend_to_size_with_zero(uint32_t width, uint32_t height, const heif_security_limits* limits); // --- pixel aspect ratio @@ -243,7 +249,9 @@ class HeifPixelImage : public std::enable_shared_from_this, private: struct ImagePlane { - Error alloc2(uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth, int num_interleaved_components); + // limits=nullptr disables the limits + Error alloc(uint32_t width, uint32_t height, heif_channel_datatype datatype, int bit_depth, int num_interleaved_components, + const heif_security_limits* limits); heif_channel_datatype m_datatype = heif_channel_datatype_unsigned_integer; uint8_t m_bit_depth = 0; diff --git a/tests/conversion.cc b/tests/conversion.cc index ea10f3e52b..abeedc0454 100644 --- a/tests/conversion.cc +++ b/tests/conversion.cc @@ -237,8 +237,8 @@ bool MakeTestImage(const ColorState& state, int width, int height, int half_max = (1 << (plane.bit_depth -1)); uint16_t value = SwapBytesIfNeeded( static_cast(half_max + i * 10 + i), state.chroma); - auto err = image->fill_new_plane2(plane.channel, value, plane.width, plane.height, - plane.bit_depth); + auto err = image->fill_new_plane(plane.channel, value, plane.width, plane.height, + plane.bit_depth, nullptr); if (err) { return false; } @@ -291,7 +291,7 @@ void TestConversion(const std::string& test_name, const ColorState& input_state, int height = 8; REQUIRE(MakeTestImage(input_state, width, height, in_image.get())); - auto out_image_result = pipeline.convert_image(in_image); + auto out_image_result = pipeline.convert_image(in_image, nullptr); REQUIRE(out_image_result); std::shared_ptr out_image = *out_image_result; @@ -319,7 +319,7 @@ void TestConversion(const std::string& test_name, const ColorState& input_state, ColorConversionPipeline reverse_pipeline; if (reverse_pipeline.construct_pipeline(target_state, input_state, options)) { INFO("reverse pipeline: " << reverse_pipeline.debug_dump_pipeline()); - auto recovered_image_result =reverse_pipeline.convert_image(out_image); + auto recovered_image_result =reverse_pipeline.convert_image(out_image, heif_get_disabled_security_limits()); REQUIRE(recovered_image_result); std::shared_ptr recovered_image = *recovered_image_result; // If the alpha plane was lost in the target state, it should come back @@ -609,7 +609,7 @@ TEST_CASE("Sharp yuv conversion", "[heif_image]") { static void fill_plane(std::shared_ptr& img, heif_channel channel, int w, int h, const std::vector& pixels) { - auto error = img->add_plane2(channel, w, h, 8); + auto error = img->add_plane(channel, w, h, 8, nullptr); REQUIRE(!error); uint32_t stride; @@ -651,7 +651,7 @@ TEST_CASE("Bilinear upsampling", "[heif_image]") std::shared_ptr img = std::make_shared(); img->create(4, 4, heif_colorspace_YCbCr, heif_chroma_420); - auto error = img->fill_new_plane2(heif_channel_Y, 128, 4,4, 8); + auto error = img->fill_new_plane(heif_channel_Y, 128, 4,4, 8, nullptr); REQUIRE(!error); fill_plane(img, heif_channel_Cb, 2,2, @@ -661,7 +661,7 @@ TEST_CASE("Bilinear upsampling", "[heif_image]") {255, 200, 50, 0}); - auto conversionResult = convert_colorspace(img, heif_colorspace_YCbCr, heif_chroma_444, nullptr, 8, options); + auto conversionResult = convert_colorspace(img, heif_colorspace_YCbCr, heif_chroma_444, nullptr, 8, options, heif_get_disabled_security_limits()); REQUIRE(conversionResult); std::shared_ptr out = *conversionResult; @@ -692,13 +692,13 @@ TEST_CASE("RGB 5-6-5 to RGB") const uint32_t height = 2; img->create(width, height, heif_colorspace_RGB, heif_chroma_444); Error err; - err = img->add_plane2(heif_channel_R, width, height, 5); + err = img->add_plane(heif_channel_R, width, height, 5, heif_get_disabled_security_limits()); REQUIRE(!err); REQUIRE(img->get_bits_per_pixel(heif_channel_R) == 5); - err = img->add_plane2(heif_channel_G, width, height, 6); + err = img->add_plane(heif_channel_G, width, height, 6, heif_get_disabled_security_limits()); REQUIRE(!err); REQUIRE(img->get_bits_per_pixel(heif_channel_G) == 6); - err = img->add_plane2(heif_channel_B, width, height, 5); + err = img->add_plane(heif_channel_B, width, height, 5, heif_get_disabled_security_limits()); REQUIRE(!err); REQUIRE(img->get_bits_per_pixel(heif_channel_B) == 5); @@ -714,7 +714,7 @@ TEST_CASE("RGB 5-6-5 to RGB") } } - auto conversionResult = convert_colorspace(img, heif_colorspace_RGB, heif_chroma_444, nullptr, 8, options); + auto conversionResult = convert_colorspace(img, heif_colorspace_RGB, heif_chroma_444, nullptr, 8, options, heif_get_disabled_security_limits()); REQUIRE(conversionResult); std::shared_ptr out = *conversionResult;