From 1e0099a810a9ca9304a23c1857c36103fe74a059 Mon Sep 17 00:00:00 2001 From: sg3-141-592 <5122866+sg3-141-592@users.noreply.github.com> Date: Sat, 11 Oct 2025 09:14:44 +0100 Subject: [PATCH 1/2] Updating decode_gif to default to DISPOSE_DO_NOT when a frame has disposal mode DISPOSAL_UNSPECIFIED or DISPOSAL_PREVIOUS. This fixes loading animated gifs with these modes set --- torchvision/csrc/io/image/cpu/decode_gif.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/torchvision/csrc/io/image/cpu/decode_gif.cpp b/torchvision/csrc/io/image/cpu/decode_gif.cpp index f26d37950e3..5773256cb5b 100644 --- a/torchvision/csrc/io/image/cpu/decode_gif.cpp +++ b/torchvision/csrc/io/image/cpu/decode_gif.cpp @@ -122,11 +122,15 @@ torch::Tensor decode_gif(const torch::Tensor& encoded_data) { // - the current image is smaller than the canvas, hence exposing its pixels // The "background" disposal method means that the current canvas should be // set to the background color. - // We only support these 2 modes and default to "background" when the - // disposal method is unspecified, or when it's set to "DISPOSE_PREVIOUS" + // We only support these 2 modes and default to DISPOSE_DO_NOT when the + // disposal method is unspecified, or when it's set to DISPOSE_PREVIOUS // which according to GIFLIB is not widely supported. // (https://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html). - if (i > 0 && gcb.DisposalMode == DISPOSE_DO_NOT) { + // This is consistent with default behaviour in the majority of web browsers + // and image libraries like Pillow. + if (i > 0 && (gcb.DisposalMode == DISPOSAL_UNSPECIFIED || + gcb.DisposalMode == DISPOSE_DO_NOT || + gcb.DisposalMode == DISPOSE_PREVIOUS)) { out[i] = out[i - 1]; } else { // Background. If bg wasn't defined, it will be (0, 0, 0) From a3054dc0f39268818d987917eaded07c884cd1dd Mon Sep 17 00:00:00 2001 From: Antoine Simoulin Date: Mon, 13 Oct 2025 12:52:13 -0700 Subject: [PATCH 2/2] lint --- torchvision/csrc/io/image/cpu/decode_gif.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/torchvision/csrc/io/image/cpu/decode_gif.cpp b/torchvision/csrc/io/image/cpu/decode_gif.cpp index 5773256cb5b..93b0861c5da 100644 --- a/torchvision/csrc/io/image/cpu/decode_gif.cpp +++ b/torchvision/csrc/io/image/cpu/decode_gif.cpp @@ -128,9 +128,10 @@ torch::Tensor decode_gif(const torch::Tensor& encoded_data) { // (https://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html). // This is consistent with default behaviour in the majority of web browsers // and image libraries like Pillow. - if (i > 0 && (gcb.DisposalMode == DISPOSAL_UNSPECIFIED || - gcb.DisposalMode == DISPOSE_DO_NOT || - gcb.DisposalMode == DISPOSE_PREVIOUS)) { + if (i > 0 && + (gcb.DisposalMode == DISPOSAL_UNSPECIFIED || + gcb.DisposalMode == DISPOSE_DO_NOT || + gcb.DisposalMode == DISPOSE_PREVIOUS)) { out[i] = out[i - 1]; } else { // Background. If bg wasn't defined, it will be (0, 0, 0)