mirror of
https://github.com/opencv/opencv.git
synced 2026-11-16 06:17:53 -06:00
imgcodecs(avif): add support for alpha channel quality control #30499 This pull request adds support for independent alpha channel quality control when encoding AVIF images, resolving #30451. ### What was happening OpenCV previously only controlled color channel compression via `IMWRITE_AVIF_QUALITY`. Alpha channels remained unhandled or were tied to color quality, making it impossible to adjust color and alpha compression independently (for example, keeping crisp lossless alpha edges while compressing background RGB content lossily to save file size). ### What changed 1. In `modules/imgcodecs/include/opencv2/imgcodecs.hpp`, added `IMWRITE_AVIF_ALPHA_QUALITY = 515` (range 0 to 100, default 95), and updated documentation for `IMWRITE_AVIF_QUALITY` to clarify that it controls color planes. 2. In `modules/imgcodecs/src/grfmt_avif.cpp`: - Registered `IMWRITE_AVIF_ALPHA_QUALITY` in `m_supported_encode_key`. - Handled `IMWRITE_AVIF_ALPHA_QUALITY` to set `encoder_->qualityAlpha` (and quantizers for fallback paths). - Removed the automatic fallback that synced alpha quality to color quality, keeping the two parameters decoupled and order-independent. 3. Added dedicated unit test `Imgcodecs_Avif.alpha_quality_control` in `modules/imgcodecs/test/test_avif.cpp` verifying independent lossy/lossless combinations, order independence, and bounds clamping. ### Testing - Verified with standalone reproducer (`reproduce_opencv_30451.cpp`): - Quality=100, AlphaQuality=10: 0 color difference (lossless color) with lossy alpha. - Quality=10, AlphaQuality=100: 0 alpha difference (lossless alpha) with lossy color. - Parameter order independence confirmed. - Native test suite: All 413 AVIF tests passed in `opencv_test_imgcodecs`. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
491 lines
17 KiB
C++
491 lines
17 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level
|
|
// directory of this distribution and at http://opencv.org/license.html
|
|
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
|
|
#include "test_precomp.hpp"
|
|
|
|
#ifdef HAVE_AVIF
|
|
|
|
namespace opencv_test {
|
|
namespace {
|
|
|
|
class Imgcodecs_Avif_RoundTripSuite
|
|
: public testing::TestWithParam<std::tuple<int, int, int, ImreadModes>> {
|
|
protected:
|
|
static cv::Mat modifyImage(const cv::Mat& img_original, int channels,
|
|
int bit_depth) {
|
|
cv::Mat img;
|
|
if (channels == 1) {
|
|
cv::cvtColor(img_original, img, cv::COLOR_BGR2GRAY);
|
|
} else if (channels == 4) {
|
|
std::vector<cv::Mat> imgs;
|
|
cv::split(img_original, imgs);
|
|
cv::Mat alpha(imgs[0].size, CV_8UC1, cv::Scalar(128)); // Do not use opaque alpha plane. it will be dropped by libavif to optimize.
|
|
imgs.push_back(alpha);
|
|
cv::merge(imgs, img);
|
|
} else {
|
|
img = img_original.clone();
|
|
}
|
|
|
|
cv::Mat img_final;
|
|
// Convert image to CV_16U for some bit depths.
|
|
if (bit_depth == 10) {
|
|
img.convertTo(img_final, CV_16U, 1023.0 / 255.0);
|
|
} else if (bit_depth == 12) {
|
|
img.convertTo(img_final, CV_16U, 4095.0 / 255.0);
|
|
} else if (bit_depth == 16) {
|
|
img.convertTo(img_final, CV_16U, 65535.0 / 255.0);
|
|
} else {
|
|
img_final = img;
|
|
}
|
|
|
|
return img_final;
|
|
}
|
|
|
|
void SetUp() {
|
|
bit_depth_ = std::get<0>(GetParam());
|
|
channels_ = std::get<1>(GetParam());
|
|
quality_ = std::get<2>(GetParam());
|
|
imread_mode_ = std::get<3>(GetParam());
|
|
encoding_params_ = {cv::IMWRITE_AVIF_QUALITY, quality_,
|
|
cv::IMWRITE_AVIF_DEPTH, bit_depth_};
|
|
}
|
|
|
|
// Memo: this is used only for animation test. AVIF animation does not support 16 bit_depth.
|
|
bool IsBitDepthValid() const {
|
|
return (bit_depth_ == 8 || bit_depth_ == 10 || bit_depth_ == 12);
|
|
}
|
|
|
|
// Makes sure images are close enough after encode/decode roundtrip.
|
|
void ValidateRead(const cv::Mat& img_original, const cv::Mat& img) const {
|
|
EXPECT_EQ(img_original.size(), img.size());
|
|
if (imread_mode_ == IMREAD_UNCHANGED) {
|
|
ASSERT_EQ(img_original.type(), img.type());
|
|
// Lossless.
|
|
if (quality_ == 100) {
|
|
EXPECT_EQ(0, cvtest::norm(img, img_original, NORM_INF));
|
|
} else {
|
|
const float num_samples = static_cast<float>(img.channels() * img.cols * img.rows);
|
|
const float norm_ = cvtest::norm(img, img_original, NORM_L2) / std::sqrt(num_samples);
|
|
const float norm = (bit_depth_ == 16)? norm_ / 65535.0f * 255.0f :
|
|
(bit_depth_ == 12)? norm_ / 4095.0f * 255.0f :
|
|
(bit_depth_ == 10)? norm_ / 1023.0f * 255.0f :
|
|
norm_; // scale to 8 bit
|
|
|
|
if (quality_ == 50) {
|
|
EXPECT_LE(norm, 20); // Maximum = 18.15 -> ceil(18.15 * 1.1) = 20
|
|
} else if (quality_ == 0) {
|
|
EXPECT_LE(norm, 60); // Maximum = 54.0416 -> ceil(54.0416 * 1.1) = 60
|
|
} else {
|
|
EXPECT_FALSE(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public:
|
|
int bit_depth_;
|
|
int channels_;
|
|
int quality_;
|
|
int imread_mode_;
|
|
std::vector<int> encoding_params_;
|
|
};
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class Imgcodecs_Avif_Image_RoundTripSuite
|
|
: public Imgcodecs_Avif_RoundTripSuite {
|
|
public:
|
|
const cv::Mat& get_img_original() {
|
|
const Key key = {channels_, (bit_depth_ < 8) ? 8 : bit_depth_};
|
|
return imgs_[key];
|
|
}
|
|
|
|
// Prepare the original image modified for different number of channels and
|
|
// bit depth.
|
|
static void SetUpTestCase() {
|
|
const string root = cvtest::TS::ptr()->get_data_path();
|
|
const string filename = root + "../cv/shared/lena.png";
|
|
const cv::Mat img_original = cv::imread(filename);
|
|
cv::Mat img_resized;
|
|
cv::resize(img_original, img_resized, cv::Size(kWidth, kHeight), 0, 0);
|
|
for (int channels : {1, 3, 4}) {
|
|
for (int bit_depth : {8, 10, 12, 16}) {
|
|
const Key key{channels, bit_depth};
|
|
imgs_[key] = modifyImage(img_resized, channels, bit_depth);
|
|
}
|
|
}
|
|
}
|
|
|
|
static const int kWidth;
|
|
static const int kHeight;
|
|
|
|
private:
|
|
typedef std::tuple<int, int> Key;
|
|
static std::map<Key, cv::Mat> imgs_;
|
|
};
|
|
std::map<std::tuple<int, int>, cv::Mat>
|
|
Imgcodecs_Avif_Image_RoundTripSuite::imgs_;
|
|
const int Imgcodecs_Avif_Image_RoundTripSuite::kWidth = 51;
|
|
const int Imgcodecs_Avif_Image_RoundTripSuite::kHeight = 31;
|
|
|
|
class Imgcodecs_Avif_Image_WriteReadSuite
|
|
: public Imgcodecs_Avif_Image_RoundTripSuite {};
|
|
|
|
TEST_P(Imgcodecs_Avif_Image_WriteReadSuite, imwrite_imread) {
|
|
#ifndef OPENCV_IMGCODECS_AVIF_SUPPORT_16BIT
|
|
if(bit_depth_ == 16) {
|
|
throw SkipTestException("Test is skipped(libavif 1.4.0+ is required to test 16 bit supporting)");
|
|
}
|
|
#endif
|
|
|
|
const cv::Mat& img_original = get_img_original();
|
|
ASSERT_FALSE(img_original.empty());
|
|
|
|
// Encode.
|
|
const string output = cv::tempfile(".avif");
|
|
EXPECT_NO_THROW(cv::imwrite(output, img_original, encoding_params_));
|
|
|
|
// Read from file.
|
|
const cv::Mat img = cv::imread(output, imread_mode_);
|
|
|
|
ValidateRead(img_original, img);
|
|
|
|
EXPECT_EQ(0, remove(output.c_str()));
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
Imgcodecs_AVIF, Imgcodecs_Avif_Image_WriteReadSuite,
|
|
::testing::Combine(::testing::ValuesIn({6, 8, 10, 12, 16}),
|
|
::testing::ValuesIn({1, 3, 4}),
|
|
::testing::ValuesIn({0, 50, 100}),
|
|
::testing::ValuesIn({IMREAD_UNCHANGED, IMREAD_GRAYSCALE,
|
|
IMREAD_COLOR, IMREAD_COLOR_RGB})));
|
|
|
|
class Imgcodecs_Avif_Image_EncodeDecodeSuite
|
|
: public Imgcodecs_Avif_Image_RoundTripSuite {};
|
|
|
|
TEST_P(Imgcodecs_Avif_Image_EncodeDecodeSuite, imencode_imdecode) {
|
|
#ifndef OPENCV_IMGCODECS_AVIF_SUPPORT_16BIT
|
|
if(bit_depth_ == 16) {
|
|
throw SkipTestException("Test is skipped(libavif 1.4.0+ is required to test 16 bit supporting)");
|
|
}
|
|
#endif
|
|
|
|
const cv::Mat& img_original = get_img_original();
|
|
ASSERT_FALSE(img_original.empty());
|
|
|
|
// Encode.
|
|
std::vector<unsigned char> buf;
|
|
bool result = true;
|
|
EXPECT_NO_THROW(
|
|
result = cv::imencode(".avif", img_original, buf, encoding_params_););
|
|
EXPECT_TRUE(result);
|
|
|
|
// Read back.
|
|
const cv::Mat img = cv::imdecode(buf, imread_mode_);
|
|
|
|
ValidateRead(img_original, img);
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
Imgcodecs_AVIF, Imgcodecs_Avif_Image_EncodeDecodeSuite,
|
|
::testing::Combine(::testing::ValuesIn({6, 8, 10, 12, 16}),
|
|
::testing::ValuesIn({1, 3, 4}),
|
|
::testing::ValuesIn({0, 50, 100}),
|
|
::testing::ValuesIn({IMREAD_UNCHANGED, IMREAD_GRAYSCALE,
|
|
IMREAD_COLOR, IMREAD_COLOR_RGB})));
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
class Imgcodecs_Avif_Animation_RoundTripSuite
|
|
: public Imgcodecs_Avif_RoundTripSuite {
|
|
public:
|
|
const std::vector<cv::Mat>& get_anim_original() {
|
|
const Key key = {channels_, bit_depth_};
|
|
return anims_[key];
|
|
}
|
|
|
|
// Prepare the original image modified for different number of channels and
|
|
// bit depth.
|
|
static void SetUpTestCase() {
|
|
const string root = cvtest::TS::ptr()->get_data_path();
|
|
const string filename = root + "../cv/shared/lena.png";
|
|
const cv::Mat img_original = cv::imread(filename);
|
|
cv::Mat img_resized;
|
|
cv::resize(img_original, img_resized, cv::Size(kWidth, kHeight), 0, 0);
|
|
for (int channels : {1, 3, 4}) {
|
|
for (int bit_depth : {8, 10, 12}) {
|
|
const Key key{channels, bit_depth};
|
|
const cv::Mat img = modifyImage(img_resized, channels, bit_depth);
|
|
cv::Mat img2, img3;
|
|
cv::flip(img, img2, 0);
|
|
cv::flip(img, img3, -1);
|
|
anims_[key] = {img, img2, img3};
|
|
}
|
|
}
|
|
}
|
|
|
|
void ValidateRead(const std::vector<cv::Mat>& anim_original,
|
|
const std::vector<cv::Mat>& anim) const {
|
|
ASSERT_EQ(anim_original.size(), anim.size());
|
|
for (size_t i = 0; i < anim.size(); ++i) {
|
|
Imgcodecs_Avif_RoundTripSuite::ValidateRead(anim_original[i], anim[i]);
|
|
}
|
|
}
|
|
|
|
static const int kWidth;
|
|
static const int kHeight;
|
|
|
|
private:
|
|
typedef std::tuple<int, int> Key;
|
|
static std::map<Key, std::vector<cv::Mat>> anims_;
|
|
};
|
|
std::map<std::tuple<int, int>, std::vector<cv::Mat>>
|
|
Imgcodecs_Avif_Animation_RoundTripSuite::anims_;
|
|
const int Imgcodecs_Avif_Animation_RoundTripSuite::kWidth = 5;
|
|
const int Imgcodecs_Avif_Animation_RoundTripSuite::kHeight = 5;
|
|
|
|
class Imgcodecs_Avif_Animation_WriteReadSuite
|
|
: public Imgcodecs_Avif_Animation_RoundTripSuite {};
|
|
|
|
TEST_P(Imgcodecs_Avif_Animation_WriteReadSuite, encode_decode) {
|
|
const std::vector<cv::Mat>& anim_original = get_anim_original();
|
|
ASSERT_FALSE(anim_original.empty());
|
|
|
|
// Encode.
|
|
const string output = cv::tempfile(".avif");
|
|
if (!IsBitDepthValid()) {
|
|
EXPECT_THROW(cv::imwritemulti(output, anim_original, encoding_params_),
|
|
cv::Exception);
|
|
EXPECT_NE(0, remove(output.c_str()));
|
|
return;
|
|
}
|
|
EXPECT_NO_THROW(cv::imwritemulti(output, anim_original, encoding_params_));
|
|
EXPECT_EQ(anim_original.size(), imcount(output));
|
|
|
|
// Read from file.
|
|
std::vector<cv::Mat> anim;
|
|
ASSERT_TRUE(cv::imreadmulti(output, anim, imread_mode_));
|
|
|
|
ValidateRead(anim_original, anim);
|
|
|
|
EXPECT_EQ(0, remove(output.c_str()));
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
Imgcodecs_AVIF, Imgcodecs_Avif_Animation_WriteReadSuite,
|
|
::testing::Combine(::testing::ValuesIn({8, 10, 12}),
|
|
::testing::ValuesIn({1, 3}), ::testing::ValuesIn({50}),
|
|
::testing::ValuesIn({IMREAD_UNCHANGED, IMREAD_GRAYSCALE,
|
|
IMREAD_COLOR, IMREAD_COLOR_RGB})));
|
|
class Imgcodecs_Avif_Animation_WriteDecodeSuite
|
|
: public Imgcodecs_Avif_Animation_RoundTripSuite {};
|
|
|
|
TEST_P(Imgcodecs_Avif_Animation_WriteDecodeSuite, encode_decode) {
|
|
const std::vector<cv::Mat>& anim_original = get_anim_original();
|
|
ASSERT_FALSE(anim_original.empty());
|
|
|
|
// Encode.
|
|
const string output = cv::tempfile(".avif");
|
|
if (!IsBitDepthValid()) {
|
|
EXPECT_THROW(cv::imwritemulti(output, anim_original, encoding_params_),
|
|
cv::Exception);
|
|
EXPECT_NE(0, remove(output.c_str()));
|
|
return;
|
|
}
|
|
EXPECT_NO_THROW(cv::imwritemulti(output, anim_original, encoding_params_));
|
|
|
|
// Put file into buffer and read from buffer.
|
|
std::ifstream file(output, std::ios::binary | std::ios::ate);
|
|
std::streamsize size = file.tellg();
|
|
file.seekg(0, std::ios::beg);
|
|
std::vector<unsigned char> buf(size);
|
|
EXPECT_TRUE(file.read(reinterpret_cast<char*>(buf.data()), size));
|
|
file.close();
|
|
std::vector<cv::Mat> anim;
|
|
ASSERT_TRUE(cv::imdecodemulti(buf, imread_mode_, anim));
|
|
|
|
ValidateRead(anim_original, anim);
|
|
|
|
if (imread_mode_ == IMREAD_UNCHANGED) {
|
|
ImageCollection collection(output, IMREAD_UNCHANGED);
|
|
anim.clear();
|
|
for (auto&& i : collection)
|
|
anim.push_back(i);
|
|
ValidateRead(anim_original, anim);
|
|
}
|
|
|
|
EXPECT_EQ(0, remove(output.c_str()));
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
Imgcodecs_AVIF, Imgcodecs_Avif_Animation_WriteDecodeSuite,
|
|
::testing::Combine(::testing::ValuesIn({8, 10, 12}),
|
|
::testing::ValuesIn({1, 3}), ::testing::ValuesIn({50}),
|
|
::testing::ValuesIn({IMREAD_UNCHANGED, IMREAD_GRAYSCALE,
|
|
IMREAD_COLOR, IMREAD_COLOR_RGB})));
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifdef OPENCV_IMGCODECS_AVIF_SUPPORT_16BIT
|
|
TEST(Imgcodecs_Avif, unsupported_animation_with_16bit)
|
|
#else
|
|
TEST(Imgcodecs_Avif, DISABLED_unsupported_animation_with_16bit)
|
|
#endif
|
|
{
|
|
cv::Mat frame(cv::Size(160, 120), CV_16UC3, cv::Scalar::all(32));
|
|
std::vector<cv::Mat> frames = {frame, frame};
|
|
std::vector<uchar> buf;
|
|
// Encoding 16-bit AVIF animations should fail due to a libavif limitation.
|
|
EXPECT_NO_THROW(EXPECT_FALSE(cv::imencodemulti(".avif", frames, buf, {IMWRITE_AVIF_DEPTH, 16})));
|
|
}
|
|
|
|
class Imgcodecs_Avif_AlphaQuality : public testing::Test
|
|
{
|
|
protected:
|
|
void SetUp() override
|
|
{
|
|
const int width = 64;
|
|
const int height = 64;
|
|
img = cv::Mat(height, width, CV_8UC4);
|
|
for (int y = 0; y < height; ++y) {
|
|
for (int x = 0; x < width; ++x) {
|
|
img.at<cv::Vec4b>(y, x) = cv::Vec4b(
|
|
static_cast<uchar>((x * 3 + y * 7) % 256),
|
|
static_cast<uchar>((x * 11 + y * 5) % 256),
|
|
static_cast<uchar>((x * 13 + y * 17) % 256),
|
|
static_cast<uchar>((x * 5 + y * 3) % 256)
|
|
);
|
|
}
|
|
}
|
|
cv::split(img, planes);
|
|
}
|
|
|
|
cv::Mat img;
|
|
std::vector<cv::Mat> planes;
|
|
};
|
|
|
|
// 1. Lossless color (quality=100) + Lossy alpha (alpha_quality=10)
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, lossless_color_lossy_alpha)
|
|
{
|
|
std::vector<uchar> buf;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf,
|
|
{IMWRITE_AVIF_QUALITY, 100, IMWRITE_AVIF_ALPHA_QUALITY, 10}));
|
|
cv::Mat decoded = cv::imdecode(buf, IMREAD_UNCHANGED);
|
|
ASSERT_EQ(decoded.type(), CV_8UC4);
|
|
|
|
std::vector<cv::Mat> dec_planes;
|
|
cv::split(decoded, dec_planes);
|
|
EXPECT_EQ(0, cvtest::norm(planes[0], dec_planes[0], NORM_INF));
|
|
EXPECT_EQ(0, cvtest::norm(planes[1], dec_planes[1], NORM_INF));
|
|
EXPECT_EQ(0, cvtest::norm(planes[2], dec_planes[2], NORM_INF));
|
|
EXPECT_GT(cvtest::norm(planes[3], dec_planes[3], NORM_INF), 0);
|
|
}
|
|
|
|
// 2. Lossy color (quality=10) + Lossless alpha (alpha_quality=100)
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, lossy_color_lossless_alpha)
|
|
{
|
|
std::vector<uchar> buf;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf,
|
|
{IMWRITE_AVIF_QUALITY, 10, IMWRITE_AVIF_ALPHA_QUALITY, 100}));
|
|
cv::Mat decoded = cv::imdecode(buf, IMREAD_UNCHANGED);
|
|
ASSERT_EQ(decoded.type(), CV_8UC4);
|
|
|
|
std::vector<cv::Mat> dec_planes;
|
|
cv::split(decoded, dec_planes);
|
|
EXPECT_GT(cvtest::norm(planes[0], dec_planes[0], NORM_INF), 0);
|
|
EXPECT_EQ(0, cvtest::norm(planes[3], dec_planes[3], NORM_INF));
|
|
}
|
|
|
|
// 3. Both lossless (quality=100, alpha_quality=100)
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, both_lossless)
|
|
{
|
|
std::vector<uchar> buf;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf,
|
|
{IMWRITE_AVIF_QUALITY, 100, IMWRITE_AVIF_ALPHA_QUALITY, 100}));
|
|
cv::Mat decoded = cv::imdecode(buf, IMREAD_UNCHANGED);
|
|
ASSERT_EQ(decoded.type(), CV_8UC4);
|
|
EXPECT_EQ(0, cvtest::norm(img, decoded, NORM_INF));
|
|
}
|
|
|
|
// 4. Default sync: IMWRITE_AVIF_QUALITY=100 without alpha quality syncs alpha to 100 (both lossless)
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, default_sync_color_to_alpha)
|
|
{
|
|
std::vector<uchar> buf;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf,
|
|
{IMWRITE_AVIF_QUALITY, 100}));
|
|
cv::Mat decoded = cv::imdecode(buf, IMREAD_UNCHANGED);
|
|
ASSERT_EQ(decoded.type(), CV_8UC4);
|
|
EXPECT_EQ(0, cvtest::norm(img, decoded, NORM_INF));
|
|
}
|
|
|
|
// 5. Only IMWRITE_AVIF_ALPHA_QUALITY=100 specified (color defaults to 95, alpha lossless)
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, only_alpha_quality_specified)
|
|
{
|
|
std::vector<uchar> buf;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf,
|
|
{IMWRITE_AVIF_ALPHA_QUALITY, 100}));
|
|
cv::Mat decoded = cv::imdecode(buf, IMREAD_UNCHANGED);
|
|
ASSERT_EQ(decoded.type(), CV_8UC4);
|
|
|
|
std::vector<cv::Mat> dec_planes;
|
|
cv::split(decoded, dec_planes);
|
|
EXPECT_EQ(0, cvtest::norm(planes[3], dec_planes[3], NORM_INF));
|
|
}
|
|
|
|
// 6. Parameter order independence
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, parameter_order_independence)
|
|
{
|
|
std::vector<uchar> buf1, buf2;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf1,
|
|
{IMWRITE_AVIF_QUALITY, 80, IMWRITE_AVIF_ALPHA_QUALITY, 20}));
|
|
EXPECT_TRUE(cv::imencode(".avif", img, buf2,
|
|
{IMWRITE_AVIF_ALPHA_QUALITY, 20, IMWRITE_AVIF_QUALITY, 80}));
|
|
cv::Mat dec1 = cv::imdecode(buf1, IMREAD_UNCHANGED);
|
|
cv::Mat dec2 = cv::imdecode(buf2, IMREAD_UNCHANGED);
|
|
EXPECT_EQ(0, cvtest::norm(dec1, dec2, NORM_INF));
|
|
}
|
|
|
|
// 7. Out-of-bounds parameter fallback
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, out_of_bounds_fallback)
|
|
{
|
|
std::vector<uchar> buf;
|
|
EXPECT_NO_THROW(cv::imencode(".avif", img, buf,
|
|
{IMWRITE_AVIF_QUALITY, 150, IMWRITE_AVIF_ALPHA_QUALITY, -50}));
|
|
EXPECT_FALSE(buf.empty());
|
|
}
|
|
|
|
// 8. Omitted key fallback tests: default parameters match explicit 95/95
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, omitted_params_default_95)
|
|
{
|
|
std::vector<uchar> bufDefault, bufExplicit;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, bufDefault));
|
|
EXPECT_TRUE(cv::imencode(".avif", img, bufExplicit,
|
|
{IMWRITE_AVIF_QUALITY, 95, IMWRITE_AVIF_ALPHA_QUALITY, 95}));
|
|
cv::Mat decDefault = cv::imdecode(bufDefault, IMREAD_UNCHANGED);
|
|
cv::Mat decExplicit = cv::imdecode(bufExplicit, IMREAD_UNCHANGED);
|
|
EXPECT_EQ(0, cvtest::norm(decDefault, decExplicit, NORM_INF));
|
|
}
|
|
|
|
// 9. Omitted alpha quality matches explicitly synced color quality
|
|
TEST_F(Imgcodecs_Avif_AlphaQuality, omitted_alpha_syncs_with_color)
|
|
{
|
|
std::vector<uchar> bufSynced, bufExplicit;
|
|
EXPECT_TRUE(cv::imencode(".avif", img, bufSynced,
|
|
{IMWRITE_AVIF_QUALITY, 80}));
|
|
EXPECT_TRUE(cv::imencode(".avif", img, bufExplicit,
|
|
{IMWRITE_AVIF_QUALITY, 80, IMWRITE_AVIF_ALPHA_QUALITY, 80}));
|
|
cv::Mat decSynced = cv::imdecode(bufSynced, IMREAD_UNCHANGED);
|
|
cv::Mat decExplicit = cv::imdecode(bufExplicit, IMREAD_UNCHANGED);
|
|
EXPECT_EQ(0, cvtest::norm(decSynced, decExplicit, NORM_INF));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace opencv_test
|
|
|
|
#endif // HAVE_AVIF
|