Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

register image formats centrally

  • Loading branch information...
commit 8924dc7ac353c424324a21a705f2f923fc65d22a 1 parent 0e69bd5
@tobi authored
Showing with 109 additions and 63 deletions.
  1. +21 −21 gd_image.go
  2. +15 −15 gif.go
  3. +11 −12 image.go
  4. +15 −15 png.go
  5. +47 −0 registry.go
View
42 gd_image.go
@@ -3,49 +3,49 @@ package mogrify
import ()
type GdImage struct {
- gd *gdImage
+ gd *gdImage
}
func NewImage(width, height int) Image {
- var image GdImage
- image.gd = gdCreate(width, height)
- if image.gd == nil {
- return nil
- }
+ var image GdImage
+ image.gd = gdCreate(width, height)
+ if image.gd == nil {
+ return nil
+ }
- return &image
+ return &image
}
func (img *GdImage) NewResized(bounds Bounds) (*GdImage, error) {
- resized := img.image().gdCopyResized(0, 0, 0, 0, bounds.Width, bounds.Height, img.image().width(), img.image().height())
- if resized == nil {
- return nil, resampleError
- }
+ resized := img.image().gdCopyResized(0, 0, 0, 0, bounds.Width, bounds.Height, img.image().width(), img.image().height())
+ if resized == nil {
+ return nil, resampleError
+ }
- return &GdImage{resized}, nil
+ return &GdImage{resized}, nil
}
func (img *GdImage) NewResampled(bounds Bounds) (*GdImage, error) {
- resized := img.image().gdCopyResampled(0, 0, 0, 0, bounds.Width, bounds.Height, img.image().width(), img.image().height())
- if resized == nil {
- return nil, resampleError
- }
+ resized := img.image().gdCopyResampled(0, 0, 0, 0, bounds.Width, bounds.Height, img.image().width(), img.image().height())
+ if resized == nil {
+ return nil, resampleError
+ }
- return &GdImage{resized}, nil
+ return &GdImage{resized}, nil
}
func (img *GdImage) Bounds() Bounds {
- return Bounds{img.image().width(), img.image().height()}
+ return Bounds{img.image().width(), img.image().height()}
}
func (img *GdImage) Height() int {
- return img.image().height()
+ return img.image().height()
}
func (img *GdImage) image() *gdImage {
- return img.gd
+ return img.gd
}
func (img *GdImage) Destroy() {
- img.image().gdDestroy()
+ img.image().gdDestroy()
}
View
30 gif.go
@@ -1,31 +1,31 @@
package mogrify
import (
- "bytes"
- "io"
+ "bytes"
+ "io"
)
type Gif struct {
- // Import GdImage and all it's methods
- GdImage
+ // Import GdImage and all it's methods
+ GdImage
}
func DecodeGif(reader io.Reader) Image {
- var image Gif
+ var image Gif
- image.gd = gdCreateFromGif(drain(reader))
- if image.gd == nil {
- return nil
- }
+ image.gd = gdCreateFromGif(drain(reader))
+ if image.gd == nil {
+ return nil
+ }
- return &image
+ return &image
}
func EncodeGif(w io.Writer, img Image) (int64, error) {
- slice, err := img.image().gdImageGif()
- if err != nil {
- return 0, err
- }
+ slice, err := img.image().gdImageGif()
+ if err != nil {
+ return 0, err
+ }
- return bytes.NewBuffer(slice).WriteTo(w)
+ return bytes.NewBuffer(slice).WriteTo(w)
}
View
23 image.go
@@ -1,27 +1,26 @@
package mogrify
import (
- "errors"
+ "errors"
)
var (
- resampleError = errors.New("Resampling failed")
- resizeError = errors.New("Resampling failed")
- loadError = errors.New("Image cannot be loaded")
+ resampleError = errors.New("Resampling failed")
+ resizeError = errors.New("Resampling failed")
+ loadError = errors.New("Image cannot be loaded")
)
-type Image interface {
- Bounds() Bounds
+type Image interface {
+ Bounds() Bounds
- Destroy()
+ Destroy()
- NewResampled(bounds Bounds) (*GdImage, error)
- NewResized(bounds Bounds) (*GdImage, error)
+ NewResampled(bounds Bounds) (*GdImage, error)
+ NewResized(bounds Bounds) (*GdImage, error)
- image() *gdImage
+ image() *gdImage
}
-
func Dimensions(img Image) string {
- return img.Bounds().String()
+ return img.Bounds().String()
}
View
30 png.go
@@ -1,31 +1,31 @@
package mogrify
import (
- "bytes"
- "io"
+ "bytes"
+ "io"
)
type Png struct {
- // Import GdImage and all it's methods
- GdImage
+ // Import GdImage and all it's methods
+ GdImage
}
func DecodePng(reader io.Reader) Image {
- var image Png
+ var image Png
- image.gd = gdCreateFromPng(drain(reader))
- if image.gd == nil {
- return nil
- }
+ image.gd = gdCreateFromPng(drain(reader))
+ if image.gd == nil {
+ return nil
+ }
- return &image
+ return &image
}
func EncodePng(w io.Writer, img Image) (int64, error) {
- slice, err := img.image().gdImagePng()
- if err != nil {
- return 0, err
- }
+ slice, err := img.image().gdImagePng()
+ if err != nil {
+ return 0, err
+ }
- return bytes.NewBuffer(slice).WriteTo(w)
+ return bytes.NewBuffer(slice).WriteTo(w)
}
View
47 registry.go
@@ -0,0 +1,47 @@
+package mogrify
+
+import (
+ "io"
+ "errors"
+)
+
+type decodeFunc (func(io.Reader) Image)
+type encodeFunc (func(io.Writer, Image) (int64, error))
+
+var (
+ noEncoder = errors.New("no encoder for mime type")
+ encoders map[string]encodeFunc = make(map[string]encodeFunc)
+ decoders map[string]decodeFunc = make(map[string]decodeFunc)
+)
+
+func registerFormat(mime string, e encodeFunc, d decodeFunc) {
+ encoders[mime] = e
+ decoders[mime] = d
+}
+
+func init() {
+ registerFormat("image/png", EncodePng, DecodePng)
+ registerFormat("image/jpeg", EncodeJpeg, DecodeJpeg)
+ registerFormat("image/jpg", EncodeJpeg, DecodeJpeg)
+ registerFormat("image/gif", EncodeGif, DecodeGif)
+}
+
+func Encode(mime string, w io.Writer, i Image) (int64, error) {
+ encoder := encoders[mime]
+
+ if encoder == nil {
+ return 0, noEncoder
+ }
+
+ return encoder(w, i)
+}
+
+func Decode(mime string, r io.Reader) Image {
+ decoder := decoders[mime]
+
+ if decoder == nil {
+ return nil
+ }
+
+ return decoder(r)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.