Skip to content

Commit

Permalink
using imaging package
Browse files Browse the repository at this point in the history
  • Loading branch information
logica0419 committed Dec 9, 2023
1 parent fd2f92f commit 250cf6e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
47 changes: 18 additions & 29 deletions service/imaging/mks2013_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,23 @@ import (
"math"

"github.com/disintegration/imaging"
"golang.org/x/image/draw"
)

var (
// Magic Kernel Sharp 2013
// http://johncostella.com/magic/
mks2013Filter = imaging.ResampleFilter{
Support: 2.5,
Kernel: func(x float64) float64 {
x = math.Abs(x)
if x >= 2.5 {
return 0.0
}
if x >= 1.5 {
return -0.125 * (x - 2.5) * (x - 2.5)
}
if x >= 0.5 {
return 0.25 * (4*x*x - 11*x + 7)
}
return 1.0625 - 1.75*x*x
},
}

// mks2013Filterのdraw.Kernelへの型キャスト
// Magic Kernel Sharp 2013
// http://johncostella.com/magic/
mks2013FilterKernel = draw.Kernel{
Support: mks2013Filter.Support,
At: mks2013Filter.Kernel,
}
)
// Magic Kernel Sharp 2013
// http://johncostella.com/magic/
var mks2013Filter = imaging.ResampleFilter{
Support: 2.5,
Kernel: func(x float64) float64 {
x = math.Abs(x)
if x >= 2.5 {
return 0.0
}
if x >= 1.5 {
return -0.125 * (x - 2.5) * (x - 2.5)
}
if x >= 0.5 {
return 0.25 * (4*x*x - 11*x + 7)
}
return 1.0625 - 1.75*x*x
},
}
15 changes: 8 additions & 7 deletions service/imaging/processor_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (p *defaultProcessor) FitAnimationGIF(src io.Reader, width, height int) (*b
width = int(math.Round(floatSrcWidth * ratio))
}

dstImage := &gif.GIF{
destImage := &gif.GIF{
Delay: srcImage.Delay,
LoopCount: srcImage.LoopCount,
Disposal: srcImage.Disposal,
Expand All @@ -113,20 +113,21 @@ func (p *defaultProcessor) FitAnimationGIF(src io.Reader, width, height int) (*b
BackgroundIndex: srcImage.BackgroundIndex,
}

for _, frame := range srcImage.Image {
srcBounds := frame.Bounds()
for _, srcFrame := range srcImage.Image {
srcBounds := srcFrame.Bounds()
destBounds := image.Rect(
int(float64(srcBounds.Min.X)*ratio),
int(float64(srcBounds.Min.Y)*ratio),
int(float64(srcBounds.Max.X)*ratio),
int(float64(srcBounds.Max.Y)*ratio),
)
destFrame := image.NewPaletted(destBounds, frame.Palette)
mks2013FilterKernel.Scale(destFrame, destBounds, frame.SubImage(srcBounds), srcBounds, draw.Over, nil)
dstImage.Image = append(dstImage.Image, destFrame)
fittedImage := imaging.Resize(srcFrame.SubImage(srcBounds), destBounds.Dx(), destBounds.Dy(), mks2013Filter)
destFrame := image.NewPaletted(destBounds, srcFrame.Palette)
draw.Draw(destFrame, destBounds, fittedImage, image.Point{}, draw.Over)
destImage.Image = append(destImage.Image, destFrame)
}

return imaging2.GifToBytesReader(dstImage)
return imaging2.GifToBytesReader(destImage)
}

func (p *defaultProcessor) WaveformMp3(src io.ReadSeeker, width, height int) (r io.Reader, err error) {
Expand Down

0 comments on commit 250cf6e

Please sign in to comment.