0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05:00

retain original color palette for resized gifs

This commit is contained in:
Will Norris 2015-05-18 15:37:28 -07:00
parent 66fcba95a0
commit 5e9ceeaaf6

View file

@ -23,7 +23,7 @@ package gifresize
import ( import (
"image" "image"
"image/color/palette" "image/color"
"image/draw" "image/draw"
"image/gif" "image/gif"
"io" "io"
@ -50,15 +50,15 @@ func Process(w io.Writer, r io.Reader, transform TransformFunc) error {
for index, frame := range im.Image { for index, frame := range im.Image {
bounds := frame.Bounds() bounds := frame.Bounds()
draw.Draw(img, bounds, frame, bounds.Min, draw.Over) draw.Draw(img, bounds, frame, bounds.Min, draw.Over)
im.Image[index] = imageToPaletted(transform(img)) im.Image[index] = imageToPaletted(transform(img), frame.Palette)
} }
return gif.EncodeAll(w, im) return gif.EncodeAll(w, im)
} }
func imageToPaletted(img image.Image) *image.Paletted { func imageToPaletted(img image.Image, p color.Palette) *image.Paletted {
b := img.Bounds() b := img.Bounds()
pm := image.NewPaletted(b, palette.Plan9) pm := image.NewPaletted(b, p)
draw.FloydSteinberg.Draw(pm, b, img, image.ZP) draw.FloydSteinberg.Draw(pm, b, img, image.ZP)
return pm return pm
} }