Skip to content

Commit

Permalink
Merge pull request #2499 from traPtitech/fix/gif-resize
Browse files Browse the repository at this point in the history
fix: グローバルカラーテーブルを持たないgifイメージに対応
  • Loading branch information
pirosiki197 authored Aug 15, 2024
2 parents f265c0f + 639ebae commit daf8cb7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions service/imaging/processor_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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のサイズより小さいことがある
Expand Down Expand Up @@ -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なら、直前のフレームを復元
Expand Down
6 changes: 6 additions & 0 deletions service/imaging/processor_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions testdata/gif/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Binary file added testdata/gif/surprised.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/gif/surprised_resized.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit daf8cb7

Please sign in to comment.