github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/gif/gif.go (about) 1 // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package gif implements a GIF image decoder and encoder. 6 // 7 // The GIF specification is at http://www.w3.org/Graphics/GIF/spec-gif89a.txt. 8 package gif 9 10 import ( 11 "image" 12 "image/color" 13 "image/gif" 14 "io" 15 16 image_ext "github.com/chai2010/gopkg/image" 17 "github.com/chai2010/gopkg/image/convert" 18 ) 19 20 // Options are the encoding and decoding parameters. 21 type Options struct { 22 *gif.Options 23 ColorModel color.Model 24 } 25 26 // DecodeConfig returns the global color model and dimensions of a GIF image 27 // without decoding the entire image. 28 func DecodeConfig(r io.Reader) (config image.Config, err error) { 29 return gif.DecodeConfig(r) 30 } 31 32 // Decode reads a GIF image from r and returns the first embedded 33 // image as an image.Image. 34 func Decode(r io.Reader, opt *Options) (m image.Image, err error) { 35 if m, err = gif.Decode(r); err != nil { 36 return 37 } 38 if opt != nil && opt.ColorModel != nil { 39 m = convert.ColorModel(m, opt.ColorModel) 40 } 41 return 42 } 43 44 // DecodeAll reads a GIF image from r and returns the sequential frames 45 // and timing information. 46 func DecodeAll(r io.Reader) (*gif.GIF, error) { 47 return gif.DecodeAll(r) 48 } 49 50 // EncodeAll writes the images in g to w in GIF format with the 51 // given loop count and delay between frames. 52 func EncodeAll(w io.Writer, g *gif.GIF) error { 53 return gif.EncodeAll(w, g) 54 } 55 56 // Encode writes the Image m to w in GIF format. 57 func Encode(w io.Writer, m image.Image, opt *Options) error { 58 if opt != nil && opt.ColorModel != nil { 59 m = convert.ColorModel(m, opt.ColorModel) 60 } 61 if opt != nil && opt.Options != nil { 62 return gif.Encode(w, m, opt.Options) 63 } else { 64 return gif.Encode(w, m, nil) 65 } 66 } 67 68 func imageExtDecode(r io.Reader, opt interface{}) (image.Image, error) { 69 if opt, ok := opt.(*Options); ok { 70 return Decode(r, opt) 71 } else { 72 return Decode(r, nil) 73 } 74 } 75 76 func imageExtEncode(w io.Writer, m image.Image, opt interface{}) error { 77 if opt, ok := opt.(*Options); ok { 78 return Encode(w, m, opt) 79 } else { 80 return Encode(w, m, nil) 81 } 82 } 83 84 func init() { 85 image_ext.RegisterFormat(image_ext.Format{ 86 Name: "gif", 87 Extensions: []string{".gif"}, 88 Magics: []string{"GIF8?a"}, 89 DecodeConfig: DecodeConfig, 90 Decode: imageExtDecode, 91 Encode: imageExtEncode, 92 }) 93 }