diff --git a/service/imaging/processor_default.go b/service/imaging/processor_default.go index f93a4f039..0a37fe91e 100644 --- a/service/imaging/processor_default.go +++ b/service/imaging/processor_default.go @@ -128,7 +128,7 @@ func (p *defaultProcessor) FitAnimationGIF(src io.Reader, width, height int) (*b // ため、キャンバスでフレームを重ねてから縮小する tempCanvas = image.NewNRGBA(gifBound) // DisposalBackgroundに対応するための、背景色の画像 - bgColorUniform = image.NewUniform(srcImage.Config.ColorModel.(color.Palette)[srcImage.BackgroundIndex]) + bgColorUniform *image.Uniform // DisposalPreviousに対応するため、直前のフレームを保持するためのキャンバス backupCanvas = image.NewNRGBA(gifBound) @@ -137,6 +137,12 @@ func (p *defaultProcessor) FitAnimationGIF(src io.Reader, width, height int) (*b eg, _ = errgroup.WithContext(context.Background()) ) + // グローバルカラーテーブルがあれば、背景色を取得 + palette, ok := srcImage.Config.ColorModel.(color.Palette) + if ok && len(palette) > 0 { + bgColorUniform = image.NewUniform(palette[srcImage.BackgroundIndex]) + } + for i, srcFrame := range srcImage.Image { // 元のフレームのサイズと位置 // 差分最適化されたGIFでは、これが元GIFのサイズより小さいことがある @@ -182,7 +188,7 @@ func (p *defaultProcessor) FitAnimationGIF(src io.Reader, width, height int) (*b r, g, b, a := srcFrame.Palette[srcFrame.Palette.Index(color.Transparent)].RGBA() if r == 0 && g == 0 && b == 0 && a == 0 { draw.Draw(tempCanvas, srcBounds, image.Transparent, image.Point{}, draw.Src) - } else { + } else if bgColorUniform != nil { draw.Draw(tempCanvas, srcBounds, bgColorUniform, image.Point{}, draw.Src) } case gif.DisposalPrevious: // DisposalがPreviousなら、直前のフレームを復元 diff --git a/service/imaging/processor_default_test.go b/service/imaging/processor_default_test.go index 94552c965..dba6db097 100644 --- a/service/imaging/processor_default_test.go +++ b/service/imaging/processor_default_test.go @@ -121,6 +121,12 @@ func TestProcessorDefault_FitAnimationGIF(t *testing.T) { want: lo.Must(io.ReadAll(testutils.MustOpenGif("frog_resized.gif"))), err: nil, }, + { + name: "success (surprised 正方形、グローバルカラーテーブルなし)", + file: "surprised.gif", + want: lo.Must(io.ReadAll(testutils.MustOpenGif("surprised_resized.gif"))), + err: nil, + }, } for _, tt := range test { diff --git a/testdata/gif/embed.go b/testdata/gif/embed.go index c3c37f02d..006c79ae1 100644 --- a/testdata/gif/embed.go +++ b/testdata/gif/embed.go @@ -20,3 +20,5 @@ var FS embed.FS // new_year.gif https://freesozaixtrain.web.fc2.com/freesozai-nenga-train2.html // tooth.gif https://patirabi.com/2021/10/10/061gif/ + +// surprised.gif https://patirabi.com/2021/10/08/058gif/ diff --git a/testdata/gif/surprised.gif b/testdata/gif/surprised.gif new file mode 100644 index 000000000..b3f57b9ce Binary files /dev/null and b/testdata/gif/surprised.gif differ diff --git a/testdata/gif/surprised_resized.gif b/testdata/gif/surprised_resized.gif new file mode 100644 index 000000000..025ddc9aa Binary files /dev/null and b/testdata/gif/surprised_resized.gif differ