Skip to content

imgcodecs: gif: support Disposal Method#26930

Merged
asmorkalov merged 11 commits intoopencv:4.xfrom
Kumataro:fix26924
Feb 26, 2025
Merged

imgcodecs: gif: support Disposal Method#26930
asmorkalov merged 11 commits intoopencv:4.xfrom
Kumataro:fix26924

Conversation

@Kumataro
Copy link
Copy Markdown
Contributor

@Kumataro Kumataro commented Feb 17, 2025

Close #26924

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • 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

@Kumataro
Copy link
Copy Markdown
Contributor Author

Now this pull request is draft because we have to add tests for Disposal Method = 3.

kmtr@kmtr-VMware-Virtual-Platform:~/work/opencv_extra4/testdata/highgui/gifsuite$ git show
commit 727770994fe49acca31b8d7ffe8653febed67cb5 (HEAD -> 4.x, origin/master, origin/HEAD, origin/4.x)
Merge: eddc7acc 8ff6cb8a
Author: Alexander Smorkalov <2536374+asmorkalov@users.noreply.github.com>
Date:   Fri Feb 14 19:40:11 2025 +0300

    Merge pull request #1236 from asmorkalov:as/openvx_hal_features2d

    Regression data for FAST OpenVX case.

kmtr@kmtr-VMware-Virtual-Platform:~/work/opencv_extra4/testdata/highgui/gifsuite$ gifbuild -d *.gif | grep disposal | sort | uniq -c
      7         disposal mode 0
     87         disposal mode 1
     10         disposal mode 2

@sturkmen72
Copy link
Copy Markdown
Contributor

i can confirm the PR seems fixed the bug.

@Kumataro Kumataro marked this pull request as ready for review February 19, 2025 10:55
@Kumataro
Copy link
Copy Markdown
Contributor Author

I feel more refactoring seems be needed from current GifEncoder/Decoder.
Here are some points I noticed. I think these should be fixed separately from this fix.

GifEncoder::GifEncoder() {
m_description = "Graphics Interchange Format 89a(*.gif)";
m_height = 0, m_width = 0;
width = 0, height = 0, top = 0, left = 0;
m_buf_supported = true;
opMode = GRFMT_GIF_Cover;
transparentColor = 0; // index of the transparent color, default 0. currently it is a constant number
transparentRGB = Vec3b(0, 0, 0); // the transparent color, default black
lzwMaxCodeSize = 12; // the maximum code size, default 12. currently it is a constant number
// default value of the params
fast = true;
loopCount = 0; // infinite loops by default
criticalTransparency = 1; // critical transparency, default 1, range from 0 to 255, 0 means no transparency
frameDelay = 5; // 20fps by default, 10ms per unit
bitDepth = 8; // the number of bits per pixel, default 8, currently it is a constant number
lzwMinCodeSize = 8; // the minimum code size, default 8, this changes as the color number changes
colorNum = 256; // the number of colors in the color table, default 256
dithering = 0; // the level dithering, default 0
globalColorTableSize = 256, localColorTableSize = 0;
}

  • Member variables in Encoder/Decoder should be started "m_" prefix. Now it is hard to know which is it function local variable or not.
  • readExtensions() should be rename readGraphiControlExtension(). Now it doesn't handle other extensions.
  • uchar should be uint8_t.
  • "flags" variable is used for "Packed Fields" for some Descriptor and Extensions. But it is hard to know what does means.
  • GifEncoder has both (A) m_height/m_width which means "Logical Screen Width/Height" in "Logical Screen Descriptor" , and (B) height/width which means "Image Width/Height" in "Image Descriptor". I feel it is natural to introduce innner strcutors to split information.
class GifEncoder {
 + struct ImageDescriptor {
    + imageWidth;
    + imageHeight; };
 + struct LogicalScreenDescriptor {
    + logicalScreenWidth;
    + logicalScreenHeight; };
}

@Kumataro
Copy link
Copy Markdown
Contributor Author

Sorry I test with ./bin/opencv_test_imgcodecs --gtest_filter="*Gif* command, so I miss this error.

     const uint8_t gcePackedFields = (GIF_DISPOSE_RESTORE_PREVIOUS << GIF_DISPOSE_METHOD_SHIFT) |
-                                    (criticalTransparency) ? GIF_TRANSPARENT_INDEX_GIVEN : GIF_TRANSPARENT_INDEX_NOT_GIVEN;
+                                    (criticalTransparency ? GIF_TRANSPARENT_INDEX_GIVEN : GIF_TRANSPARENT_INDEX_NOT_GIVEN);

It seems this code works following.

const uint8_t gcePackedFields =
  ( (GIF_DISPOSE_RESTORE_PREVIOUS << GIF_DISPOSE_METHOD_SHIFT) | criticalTransparency) )
  ? 
  GIF_TRANSPARENT_INDEX_GIVEN : 
  GIF_TRANSPARENT_INDEX_NOT_GIVEN;
- ```

@asmorkalov asmorkalov added this to the 4.12.0 milestone Feb 25, 2025
@asmorkalov asmorkalov self-assigned this Feb 25, 2025
Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Need to check possible out-of-bound access mentioned bellow.

TEST(Imgcodecs_Gif, decode_disposal_method)
{
const string root = cvtest::TS::ptr()->get_data_path();
const string filename = root + "gifsuite/disposalMethod.gif";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's a new file. Please create PR to opencv_extra repo with the same branch name.

Copy link
Copy Markdown
Contributor Author

@Kumataro Kumataro Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I created branch which has same name this patch so its test is passed, but I forget to create PR.

Please could you merge this pull request ? opencv/opencv_extra#1239

@asmorkalov asmorkalov merged commit a63ede6 into opencv:4.x Feb 26, 2025
28 checks passed
@asmorkalov asmorkalov mentioned this pull request Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The imreadanimation function fails to read some animated GIFs correctly.

3 participants