[GSoC] Add GIF decode and encode for imgcodecs#25691
Conversation
|
|
i've already done this, then how to rerun the build bot? or do i need to start a new pull request? |
|
just add a new commit. for example you can write some information here |
|
You need to update the CMake code to take GIF into account. The best is probably to have a look at how it's done for AVIF. |
|
Thx for the code. I'd recommend to wait for the animation API that @sturkmen72 will during the GSoC. |
Co-authored-by: TrendMYX <trendmyx@126.com>
vrabaud
left a comment
There was a problem hiding this comment.
The biggest issue I see is with transparency: it seems not be handled properly. There should be an option to apply the background color or not (which would therefore return cv::Mat4b)
…e, imcount and use_rgb
Co-authored-by: TrendMYX <trendmyx@126.com>
Co-authored-by: TrendMYX <trendmyx@126.com>
Co-authored-by: TrendMYX <trendmyx@126.com>
|
@vrabaud What is the current PR status? Is it ready for integration? Please list issues, if not. |
|
@redhecker could you check this file https://upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif |
|
take a look at the source of file only the file above is problematic |
ok, i will try to fix this as soon as possible |
|
in m_frame_count ++; reaches 9 imcount("d:/github/Newtons_cradle_animation_book_2.gif") returns 9 |
wait, when i run the following codes, i got 36 as output? i don't think i fully understand what you mean, do you mean you get 9 as output with the folling codes? but this cannot work for imreadmulti and imreadanimation. i think it's somewhere else that are wrong |
sorry my bad.. you are right. i accidentally overwrite the image only readed frames when testing. |
|
hasRead = lzwDecode(); // hasRead is false at 10th frame |
|
@sturkmen72 pls have a look at the newest pr, are there any other errors? |
|
@redhecker well done! thank you. |
Fix bugs in GIF decoding #26738 ### Pull Request Readiness Checklist this is related to #25691 i solved two bugs here: 1. the decoding setting: according to [https://www.w3.org/Graphics/GIF/spec-gif89a.txt](https://www.w3.org/Graphics/GIF/spec-gif89a.txt) ``` DEFERRED CLEAR CODE IN LZW COMPRESSION There has been confusion about where clear codes can be found in the data stream. As the specification says, they may appear at anytime. There is not a requirement to send a clear code when the string table is full. It is the encoder's decision as to when the table should be cleared. When the table is full, the encoder can chose to use the table as is, making no changes to it until the encoder chooses to clear it. The encoder during this time sends out codes that are of the maximum Code Size. As we can see from the above, when the decoder's table is full, it must not change the table until a clear code is received. The Code Size is that of the maximum Code Size. Processing other than this is done normally. Because of a large base of decoders that do not handle the decompression in this manner, we ask developers of GIF encoding software to NOT implement this feature until at least January 1991 and later if they see that their particular market is not ready for it. This will give developers of GIF decoding software time to implement this feature and to get it into the hands of their clients before the decoders start "breaking" on the new GIF's. It is not required that encoders change their software to take advantage of the deferred clear code, but it is for decoders. ``` at first i didn't consider this case, thus leads to a bug discussed in #25691. the changes made in function lzwDecode() is aiming at solving this. 2. the fetch method of loopCount: in the codes at https://github.com/opencv/opencv/blob/4.x/modules/imgcodecs/src/grfmt_gif.cpp#L410, if the branch is taken, 3 more bytes will be taken, leading to unpredictable behavior. 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. - [ ] The feature is well documented and sample code can be built with the project CMake
Fix bugs in GIF decoding opencv#26738 ### Pull Request Readiness Checklist this is related to opencv#25691 i solved two bugs here: 1. the decoding setting: according to [https://www.w3.org/Graphics/GIF/spec-gif89a.txt](https://www.w3.org/Graphics/GIF/spec-gif89a.txt) ``` DEFERRED CLEAR CODE IN LZW COMPRESSION There has been confusion about where clear codes can be found in the data stream. As the specification says, they may appear at anytime. There is not a requirement to send a clear code when the string table is full. It is the encoder's decision as to when the table should be cleared. When the table is full, the encoder can chose to use the table as is, making no changes to it until the encoder chooses to clear it. The encoder during this time sends out codes that are of the maximum Code Size. As we can see from the above, when the decoder's table is full, it must not change the table until a clear code is received. The Code Size is that of the maximum Code Size. Processing other than this is done normally. Because of a large base of decoders that do not handle the decompression in this manner, we ask developers of GIF encoding software to NOT implement this feature until at least January 1991 and later if they see that their particular market is not ready for it. This will give developers of GIF decoding software time to implement this feature and to get it into the hands of their clients before the decoders start "breaking" on the new GIF's. It is not required that encoders change their software to take advantage of the deferred clear code, but it is for decoders. ``` at first i didn't consider this case, thus leads to a bug discussed in opencv#25691. the changes made in function lzwDecode() is aiming at solving this. 2. the fetch method of loopCount: in the codes at https://github.com/opencv/opencv/blob/4.x/modules/imgcodecs/src/grfmt_gif.cpp#L410, if the branch is taken, 3 more bytes will be taken, leading to unpredictable behavior. 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. - [ ] The feature is well documented and sample code can be built with the project CMake
Fix bugs in GIF decoding opencv#26738 ### Pull Request Readiness Checklist this is related to opencv#25691 i solved two bugs here: 1. the decoding setting: according to [https://www.w3.org/Graphics/GIF/spec-gif89a.txt](https://www.w3.org/Graphics/GIF/spec-gif89a.txt) ``` DEFERRED CLEAR CODE IN LZW COMPRESSION There has been confusion about where clear codes can be found in the data stream. As the specification says, they may appear at anytime. There is not a requirement to send a clear code when the string table is full. It is the encoder's decision as to when the table should be cleared. When the table is full, the encoder can chose to use the table as is, making no changes to it until the encoder chooses to clear it. The encoder during this time sends out codes that are of the maximum Code Size. As we can see from the above, when the decoder's table is full, it must not change the table until a clear code is received. The Code Size is that of the maximum Code Size. Processing other than this is done normally. Because of a large base of decoders that do not handle the decompression in this manner, we ask developers of GIF encoding software to NOT implement this feature until at least January 1991 and later if they see that their particular market is not ready for it. This will give developers of GIF decoding software time to implement this feature and to get it into the hands of their clients before the decoders start "breaking" on the new GIF's. It is not required that encoders change their software to take advantage of the deferred clear code, but it is for decoders. ``` at first i didn't consider this case, thus leads to a bug discussed in opencv#25691. the changes made in function lzwDecode() is aiming at solving this. 2. the fetch method of loopCount: in the codes at https://github.com/opencv/opencv/blob/4.x/modules/imgcodecs/src/grfmt_gif.cpp#L410, if the branch is taken, 3 more bytes will be taken, leading to unpredictable behavior. 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. - [ ] The feature is well documented and sample code can be built with the project CMake
Fix bugs in GIF decoding opencv#26738 ### Pull Request Readiness Checklist this is related to opencv#25691 i solved two bugs here: 1. the decoding setting: according to [https://www.w3.org/Graphics/GIF/spec-gif89a.txt](https://www.w3.org/Graphics/GIF/spec-gif89a.txt) ``` DEFERRED CLEAR CODE IN LZW COMPRESSION There has been confusion about where clear codes can be found in the data stream. As the specification says, they may appear at anytime. There is not a requirement to send a clear code when the string table is full. It is the encoder's decision as to when the table should be cleared. When the table is full, the encoder can chose to use the table as is, making no changes to it until the encoder chooses to clear it. The encoder during this time sends out codes that are of the maximum Code Size. As we can see from the above, when the decoder's table is full, it must not change the table until a clear code is received. The Code Size is that of the maximum Code Size. Processing other than this is done normally. Because of a large base of decoders that do not handle the decompression in this manner, we ask developers of GIF encoding software to NOT implement this feature until at least January 1991 and later if they see that their particular market is not ready for it. This will give developers of GIF decoding software time to implement this feature and to get it into the hands of their clients before the decoders start "breaking" on the new GIF's. It is not required that encoders change their software to take advantage of the deferred clear code, but it is for decoders. ``` at first i didn't consider this case, thus leads to a bug discussed in opencv#25691. the changes made in function lzwDecode() is aiming at solving this. 2. the fetch method of loopCount: in the codes at https://github.com/opencv/opencv/blob/4.x/modules/imgcodecs/src/grfmt_gif.cpp#L410, if the branch is taken, 3 more bytes will be taken, leading to unpredictable behavior. 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. - [ ] The feature is well documented and sample code can be built with the project CMake
[GSoC] Add GIF decode and encode for imgcodecs opencv#25691 this is related to opencv#24855 we add gif support for `imread`, `imreadmulti`, `imwrite` and `imwritemulti` opencv_extra: opencv/opencv_extra#1203 ### 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. - [ ] The feature is well documented and sample code can be built with the project CMake
Fix bugs in GIF decoding opencv#26738 ### Pull Request Readiness Checklist this is related to opencv#25691 i solved two bugs here: 1. the decoding setting: according to [https://www.w3.org/Graphics/GIF/spec-gif89a.txt](https://www.w3.org/Graphics/GIF/spec-gif89a.txt) ``` DEFERRED CLEAR CODE IN LZW COMPRESSION There has been confusion about where clear codes can be found in the data stream. As the specification says, they may appear at anytime. There is not a requirement to send a clear code when the string table is full. It is the encoder's decision as to when the table should be cleared. When the table is full, the encoder can chose to use the table as is, making no changes to it until the encoder chooses to clear it. The encoder during this time sends out codes that are of the maximum Code Size. As we can see from the above, when the decoder's table is full, it must not change the table until a clear code is received. The Code Size is that of the maximum Code Size. Processing other than this is done normally. Because of a large base of decoders that do not handle the decompression in this manner, we ask developers of GIF encoding software to NOT implement this feature until at least January 1991 and later if they see that their particular market is not ready for it. This will give developers of GIF decoding software time to implement this feature and to get it into the hands of their clients before the decoders start "breaking" on the new GIF's. It is not required that encoders change their software to take advantage of the deferred clear code, but it is for decoders. ``` at first i didn't consider this case, thus leads to a bug discussed in opencv#25691. the changes made in function lzwDecode() is aiming at solving this. 2. the fetch method of loopCount: in the codes at https://github.com/opencv/opencv/blob/4.x/modules/imgcodecs/src/grfmt_gif.cpp#L410, if the branch is taken, 3 more bytes will be taken, leading to unpredictable behavior. 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. - [ ] The feature is well documented and sample code can be built with the project CMake
| param.push_back(7); | ||
| param.push_back(IMWRITE_GIF_DITHER); | ||
| param.push_back(2); | ||
| EXPECT_NO_THROW(imencode(".png", img_gt, buff, param)); |
There was a problem hiding this comment.
@redhecker is there something like forgotten issue here ?
EXPECT_NO_THROW(imencode(".gif", img_gt, buff, param)); fails
There was a problem hiding this comment.
I think that there's something wrong here, where ".png" used with gif params. However, the code was written by @TrendMYX , i think he will fix it in a few days.
There was a problem hiding this comment.
@redhecker @TrendMYX fyi
[ERROR:0@0.088] global loadsave.cpp:1687 cv::imencodeWithMetadata imencode(): can't encode data: OpenCV(4.13.0-dev) C:\build\precommit_windows64\4.x\opencv\modules\imgcodecs\src\grfmt_gif.cpp:1022: error: (-215:Assertion failed) false in function 'cv::GifEncoder::ditheringKernel'
Exception message: OpenCV(4.13.0-dev) C:\build\precommit_windows64\4.x\opencv\modules\imgcodecs\src\loadsave.cpp:1274: error: (-215:Assertion failed) !buf.empty() in function 'cv::imdecode_'
There was a problem hiding this comment.
@sturkmen72 @redhecker Sorry for all the mistakes. Here is the modified version which goes through successfully on my laptop.
TEST(Imgcodecs_Gif,write_gif_flags){
//rows611 x cols293, three‑channel, constant value 1
const int expected_rows = 611;
const int expected_cols = 293;
cv::Mat img_gt(expected_rows, expected_cols, CV_8UC3, cv::Scalar::all(1));
//the encoding parameters
std::vector<unsigned char> buff;
std::vector<int> param;
param.push_back(cv::IMWRITE_GIF_QUALITY);
param.push_back(7);
param.push_back(cv::IMWRITE_GIF_DITHER);
param.push_back(2);
//Encode part
ASSERT_NO_THROW(cv::imencode(".gif", img_gt, buff, param));
//Decode part
cv::Mat img;
ASSERT_NO_THROW(img = cv::imdecode(buff, cv::IMREAD_ANYCOLOR));
ASSERT_FALSE(img.empty());
EXPECT_EQ(img.cols, expected_cols);
EXPECT_EQ(img.rows, expected_rows);
EXPECT_EQ(img.type(), CV_8UC3);
// Compare, maxdiff=16
EXPECT_PRED_FORMAT2(cvtest::MatComparator(16, 0), img, img_gt);
}There was a problem hiding this comment.
@TrendMYX thank you. feel free to create a PR or i will do later.
this is related to #24855
we add gif support for
imread,imreadmulti,imwriteandimwritemultiopencv_extra: opencv/opencv_extra#1203
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.