1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge pull request #27769 from Kumataro:refix27557

Add strict validation for encoding parameters for APNG, Animation WebP #27769

Extra fix for https://github.com/opencv/opencv/issues/27557

- Fix for build errors with libspng library.
- Add strict validation for APNG.
- Add strict validation for Animation WebP.

### 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
- [ ] 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
This commit is contained in:
Kumataro
2025-09-11 22:50:58 +09:00
committed by GitHub
parent 16c8308674
commit dac243bd26
4 changed files with 60 additions and 25 deletions
+44 -12
View File
@@ -1630,20 +1630,52 @@ bool PngEncoder::writeanimation(const Animation& animation, const std::vector<in
for (size_t i = 0; i < params.size(); i += 2)
{
if (params[i] == IMWRITE_PNG_COMPRESSION)
const int value = params[i+1];
switch (params[i])
{
case IMWRITE_PNG_COMPRESSION:
m_compression_strategy = IMWRITE_PNG_STRATEGY_DEFAULT; // Default strategy
m_compression_level = params[i + 1];
m_compression_level = MIN(MAX(m_compression_level, 0), Z_BEST_COMPRESSION);
}
if (params[i] == IMWRITE_PNG_STRATEGY)
{
m_compression_strategy = params[i + 1];
m_compression_strategy = MIN(MAX(m_compression_strategy, 0), Z_FIXED);
}
if (params[i] == IMWRITE_PNG_BILEVEL)
{
m_isBilevel = params[i + 1] != 0;
m_compression_level = MIN(MAX(value, 0), Z_BEST_COMPRESSION);
if(value != m_compression_level) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_PNG_COMPRESSION must be between 0 to 9. It is fallbacked to %d", value, m_compression_level));
}
break;
case IMWRITE_PNG_STRATEGY:
{
switch(value) {
case IMWRITE_PNG_STRATEGY_DEFAULT:
case IMWRITE_PNG_STRATEGY_FILTERED:
case IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY:
case IMWRITE_PNG_STRATEGY_RLE:
case IMWRITE_PNG_STRATEGY_FIXED:
m_compression_strategy = value;
break;
default:
m_compression_strategy = IMWRITE_PNG_STRATEGY_RLE;
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_PNG_STRATEGY must be one of ImwritePNGFlags. It is fallbacked to IMWRITE_PNG_STRATEGY_RLE", value));
break;
}
}
break;
case IMWRITE_PNG_BILEVEL:
m_isBilevel = value != 0;
if((value != 0) && (value != 1)) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_PNG_BILEVEL must be 0 or 1. It is fallbacked to 1", value ));
}
break;
case IMWRITE_PNG_FILTER:
CV_LOG_WARNING(nullptr, "IMWRITE_PNG_BILEVEL parameter is not supported for APNG");
break;
case IMWRITE_PNG_ZLIBBUFFER_SIZE:
CV_LOG_WARNING(nullptr, "IMWRITE_PNG_ZLIBBUFFER_SIZE parameter is not supported for APNG");
break;
default:
break;
}
}
+3 -3
View File
@@ -540,8 +540,8 @@ bool SPngEncoder::write(const Mat &img, const std::vector<int> &params)
{
compression_strategy = IMWRITE_PNG_STRATEGY_DEFAULT; // Default strategy
compression_level = MIN(MAX(value, 0), Z_BEST_COMPRESSION);
if(value != m_compression_level) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_PNG_COMPRESSION must be between 0 to 9. It is fallbacked to %d", value, m_compression_level));
if(value != compression_level) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_PNG_COMPRESSION must be between 0 to 9. It is fallbacked to %d", value, compression_level));
}
set_compression_level = true;
}
@@ -556,7 +556,7 @@ bool SPngEncoder::write(const Mat &img, const std::vector<int> &params)
compression_strategy = value;
break;
default:
compression_strategy = IMWRITE_PNG_STRATEGY_RLE:
compression_strategy = IMWRITE_PNG_STRATEGY_RLE;
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_PNG_STRATEGY must be one of ImwritePNGFlags. It is fallbacked to IMWRITE_PNG_STRATEGY_RLE", value));
break;
}
+10 -7
View File
@@ -511,23 +511,26 @@ bool WebPEncoder::writeanimation(const Animation& animation, const std::vector<i
anim_config.anim_params.bgcolor = bgvalue;
anim_config.anim_params.loop_count = animation.loop_count;
anim_config.minimize_size = 0;
if (params.size() > 1)
for(size_t i = 0; i < params.size(); i += 2)
{
if (params[0] == IMWRITE_WEBP_QUALITY)
const int value = params[i+1];
if (params[i] == IMWRITE_WEBP_QUALITY)
{
config.lossless = 0;
config.quality = static_cast<float>(params[1]);
config.lossless = 0; // false
config.quality = static_cast<float>(value);
if (config.quality < 1.0f)
{
config.quality = 1.0f;
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_WEBP_QUALITY must be between 1 to 100(lossy) or more(lossless). It is fallbacked to 1", value));
}
if (config.quality >= 100.0f)
if (config.quality > 100.0f)
{
config.lossless = 1;
config.quality = 100.0f;
config.lossless = 1; // true
}
}
anim_config.minimize_size = 0;
}
std::unique_ptr<WebPAnimEncoder, void (*)(WebPAnimEncoder*)> anim_encoder(