refactor gifresize to be a resuable package

- move out of main package
- export Process func
- Process now accepts an io.Reader and io.Writer as input rather than
  working directly on files
- Process accepts a transform function as input rather than always
  resizing to a fixed size
This commit is contained in:
Will Norris 2015-05-15 15:32:43 -07:00
parent ef63a74da1
commit 58bb2e4ce9

View file

@ -12,45 +12,33 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package main // Package gifresize resizes animated gifs.
// This demonstrates a solution to resizing animated gifs.
// //
// Frames in an animated gif aren't necessarily the same size, subsequent // Frames in an animated gif aren't necessarily the same size, subsequent
// frames are overlayed on previous frames. Therefore, resizing the frames // frames are overlayed on previous frames. Therefore, resizing the frames
// individually may cause problems due to aliasing of transparent pixels. This // individually may cause problems due to aliasing of transparent pixels. This
// example tries to avoid this by building frames from all previous frames and // package tries to avoid this by building frames from all previous frames and
// resizing the frames as RGB. // resizing the frames as RGB.
package gifresize
import ( import (
"image" "image"
"image/color/palette" "image/color/palette"
"image/draw" "image/draw"
"image/gif" "image/gif"
"log" "io"
"os"
"github.com/nfnt/resize"
) )
func main() { // TransformFunc is a function that transforms an image.
process("shapes") type TransformFunc func(image.Image) image.Image
process("blob")
}
func process(filename string) {
// Open image file.
f, err := os.Open(filename + ".gif")
if err != nil {
log.Fatal(err.Error())
}
defer f.Close()
// Process the GIF read from r, applying transform to each frame, and writing
// the result to w.
func Process(w io.Writer, r io.Reader, transform TransformFunc) error {
// Decode the original gif. // Decode the original gif.
im, err := gif.DecodeAll(f) im, err := gif.DecodeAll(r)
if err != nil { if err != nil {
log.Fatal(err.Error()) return err
} }
// Create a new RGBA image to hold the incremental frames. // Create a new RGBA image to hold the incremental frames.
@ -62,23 +50,14 @@ func process(filename string) {
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(ProcessImage(img)) im.Image[index] = imageToPaletted(transform(img))
} }
// Write resized gif. gif.EncodeAll(w, im)
out, err := os.Create(filename + ".out.fixed.gif") return nil
if err != nil {
log.Fatal(err.Error())
}
defer out.Close()
gif.EncodeAll(out, im)
} }
func ProcessImage(img image.Image) image.Image { func imageToPaletted(img image.Image) *image.Paletted {
return resize.Resize(250, 0, img, resize.NearestNeighbor)
}
func ImageToPaletted(img image.Image) *image.Paletted {
b := img.Bounds() b := img.Bounds()
pm := image.NewPaletted(b, palette.Plan9) pm := image.NewPaletted(b, palette.Plan9)
draw.FloydSteinberg.Draw(pm, b, img, image.ZP) draw.FloydSteinberg.Draw(pm, b, img, image.ZP)