Skip to content

Commit 1e0099a

Browse files
committed
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
1 parent f5c6c2e commit 1e0099a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

torchvision/csrc/io/image/cpu/decode_gif.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,15 @@ torch::Tensor decode_gif(const torch::Tensor& encoded_data) {
122122
// - the current image is smaller than the canvas, hence exposing its pixels
123123
// The "background" disposal method means that the current canvas should be
124124
// set to the background color.
125-
// We only support these 2 modes and default to "background" when the
126-
// disposal method is unspecified, or when it's set to "DISPOSE_PREVIOUS"
125+
// We only support these 2 modes and default to DISPOSE_DO_NOT when the
126+
// disposal method is unspecified, or when it's set to DISPOSE_PREVIOUS
127127
// which according to GIFLIB is not widely supported.
128128
// (https://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html).
129-
if (i > 0 && gcb.DisposalMode == DISPOSE_DO_NOT) {
129+
// This is consistent with default behaviour in the majority of web browsers
130+
// and image libraries like Pillow.
131+
if (i > 0 && (gcb.DisposalMode == DISPOSAL_UNSPECIFIED ||
132+
gcb.DisposalMode == DISPOSE_DO_NOT ||
133+
gcb.DisposalMode == DISPOSE_PREVIOUS)) {
130134
out[i] = out[i - 1];
131135
} else {
132136
// Background. If bg wasn't defined, it will be (0, 0, 0)

0 commit comments

Comments
 (0)