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

Merge pull request #27621 from Kumataro:trial27557

Add strict validation for encoding parameters #27621

Close https://github.com/opencv/opencv/issues/27557

### 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
This commit is contained in:
Kumataro
2025-08-12 22:04:45 +09:00
committed by GitHub
parent d33999cb6e
commit c5bb6a014a
22 changed files with 448 additions and 84 deletions
+20 -5
View File
@@ -554,6 +554,8 @@ GifEncoder::GifEncoder() {
colorNum = 256; // the number of colors in the color table, default 256
dithering = 0; // the level dithering, default 0
globalColorTableSize = 256, localColorTableSize = 0;
m_supported_encode_key = {IMWRITE_GIF_QUALITY, IMWRITE_GIF_DITHER, IMWRITE_GIF_TRANSPARENCY, IMWRITE_GIF_COLORTABLE};
}
GifEncoder::~GifEncoder() {
@@ -576,6 +578,7 @@ bool GifEncoder::writeanimation(const Animation& animation, const std::vector<in
// confirm the params
for (size_t i = 0; i < params.size(); i += 2) {
const int value = params[i+1];
switch (params[i]) {
case IMWRITE_GIF_LOOP:
CV_LOG_WARNING(NULL, "IMWRITE_GIF_LOOP is not functional since 4.12.0. Replaced by cv::Animation::loop_count.");
@@ -584,17 +587,26 @@ bool GifEncoder::writeanimation(const Animation& animation, const std::vector<in
CV_LOG_WARNING(NULL, "IMWRITE_GIF_SPEED is not functional since 4.12.0. Replaced by cv::Animation::durations.");
break;
case IMWRITE_GIF_DITHER:
dithering = std::min(std::max(params[i + 1], -1), 3);
dithering = std::min(std::max(value, -1), 3);
if(value != dithering) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_GIF_DITHER must be between -1 to 3. It is fallbacked to %d", value, dithering));
}
fast = false;
break;
case IMWRITE_GIF_TRANSPARENCY:
criticalTransparency = (uchar)std::min(std::max(params[i + 1], 0), 255);
criticalTransparency = (uchar)std::min(std::max(value, 0), 255);
if(value != criticalTransparency) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_GIF_TRANSPARENCY must be between 0 to 255. It is fallbacked to %d", value, criticalTransparency));
}
break;
case IMWRITE_GIF_COLORTABLE:
localColorTableSize = std::min(std::max(params[i + 1], 0), 1);
localColorTableSize = std::min(std::max(value, 0), 1);
if(value != localColorTableSize) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_GIF_COLORTABLE must be 0 or 1. It is fallbacked to %d", value, localColorTableSize));
}
break;
case IMWRITE_GIF_QUALITY:
switch (params[i + 1]) {
switch (value) {
case IMWRITE_GIF_FAST_FLOYD_DITHER:
fast = true;
dithering = GRFMT_GIF_FloydSteinberg;
@@ -604,10 +616,13 @@ bool GifEncoder::writeanimation(const Animation& animation, const std::vector<in
dithering = GRFMT_GIF_None;
break;
default:
lzwMinCodeSize = std::min(std::max(params[i + 1], 3), 8);
lzwMinCodeSize = std::min(std::max(value, 3), 8);
colorNum = 1 << lzwMinCodeSize;
globalColorTableSize = colorNum;
fast = false;
if(value != lzwMinCodeSize) {
CV_LOG_WARNING(nullptr, cv::format("The value(%d) for IMWRITE_GIF_QUALITY must be one of ImwriteGIFCompressionFlags. It is fallbacked to %d", value, lzwMinCodeSize));
}
break;
}
break; // case IMWRITE_GIF_QUALITY