Permalink
Browse files

Complete encoding/decoding for the three major formats

  • Loading branch information...
1 parent 5e1f3ca commit 8c4321f4d7bc6da0a9c1e04a5e9367ff85cba632 @tobi committed May 30, 2012
Showing with 142 additions and 95 deletions.
  1. +0 −33 encoding.go
  2. +31 −0 gif.go
  3. +32 −3 image_test.go
  4. +31 −0 jpeg.go
  5. +0 −59 jpg.go
  6. +31 −0 png.go
  7. +17 −0 util.go
View
@@ -1,33 +0,0 @@
-package mogrify
-
-import (
- "bytes"
- "io"
-)
-
-func EncodeJpg(w io.Writer, img Image) (int64, error) {
- slice, err := img.image().gdImageJpeg()
- if err != nil {
- return 0, err
- }
-
- return bytes.NewBuffer(slice).WriteTo(w)
-}
-
-func EncodePng(w io.Writer, img Image) (int64, error) {
- slice, err := img.image().gdImagePng()
- if err != nil {
- return 0, err
- }
-
- return bytes.NewBuffer(slice).WriteTo(w)
-}
-
-func EncodeGif(w io.Writer, img Image) (int64, error) {
- slice, err := img.image().gdImageGif()
- if err != nil {
- return 0, err
- }
-
- return bytes.NewBuffer(slice).WriteTo(w)
-}
View
31 gif.go
@@ -0,0 +1,31 @@
+package mogrify
+
+import (
+ "bytes"
+ "io"
+)
+
+type Gif struct {
+ // Import GdImage and all it's methods
+ GdImage
+}
+
+func DecodeGif(reader io.Reader) Image {
+ var image Gif
+
+ image.gd = gdCreateFromGif(drain(reader))
+ if image.gd == nil {
+ return nil
+ }
+
+ return &image
+}
+
+func EncodeGif(w io.Writer, img Image) (int64, error) {
+ slice, err := img.image().gdImageGif()
+ if err != nil {
+ return 0, err
+ }
+
+ return bytes.NewBuffer(slice).WriteTo(w)
+}
View
@@ -16,7 +16,7 @@ func asset(asset string) Image {
file, _ := os.Open("./assets/image.jpg")
defer file.Close()
- image := DecodeJpg(file)
+ image := DecodeJpeg(file)
if image == nil {
panic("Image didnt load")
@@ -106,15 +106,44 @@ func TestDecodeEncode(t *testing.T) {
var buffer bytes.Buffer
- _, err = EncodeJpg(&buffer, resized)
+ _, err = EncodeJpeg(&buffer, resized)
if err != nil {
t.Error(err)
return
}
- roundtrip := DecodeJpg(&buffer)
+ roundtrip := DecodeJpeg(&buffer)
assertDimension(t, roundtrip, "100x100")
+}
+
+func TestDecodePng(t *testing.T) {
+ img := asset("./assets/image.jpg")
+ defer img.Destroy()
+
+ dest, _ := os.Create("/tmp/dest.png")
+ defer dest.Close()
+ _, err := EncodePng(dest, img)
+
+ if err != nil {
+ t.Error(err)
+ return
+ }
+}
+
+func TestDecodeGif(t *testing.T) {
+ img := asset("./assets/image.jpg")
+ defer img.Destroy()
+
+ dest, _ := os.Create("/tmp/dest.gif")
+ defer dest.Close()
+
+ _, err := EncodeGif(dest, img)
+
+ if err != nil {
+ t.Error(err)
+ return
+ }
}
View
31 jpeg.go
@@ -0,0 +1,31 @@
+package mogrify
+
+import (
+ "bytes"
+ "io"
+)
+
+type Jpeg struct {
+ // Import GdImage and all it's methods
+ GdImage
+}
+
+func DecodeJpeg(reader io.Reader) Image {
+ var image Jpeg
+
+ image.gd = gdCreateFromJpeg(drain(reader))
+ if image.gd == nil {
+ return nil
+ }
+
+ return &image
+}
+
+func EncodeJpeg(w io.Writer, img Image) (int64, error) {
+ slice, err := img.image().gdImageJpeg()
+ if err != nil {
+ return 0, err
+ }
+
+ return bytes.NewBuffer(slice).WriteTo(w)
+}
View
59 jpg.go
@@ -1,59 +0,0 @@
-package mogrify
-
-import (
- "bytes"
- "errors"
- "io"
-)
-
-var (
- BlobEmpty = errors.New("blob was empty")
-)
-
-type Jpg struct {
- // Import GdImage and all it's methods
- GdImage
-}
-
-func DecodeJpg(reader io.Reader) Image {
- var image Jpg
-
- gd, err := readFromJpg(reader)
-
- if err != nil {
- return nil
- }
-
- image.gd = gd
-
- return &image
-}
-
-// Take an io.Reader and read a gdImage from it
-func readFromJpg(reader io.Reader) (gd *gdImage, err error) {
- var buffer bytes.Buffer
-
- _, err = buffer.ReadFrom(reader)
- if err != nil {
- return
- }
-
- gd = gdCreateFromJpeg(buffer.Bytes())
- if gd == nil {
- return nil, loadError
- }
-
- return gd, nil
-}
-
-func writeAsJpg(img Image, writer io.Writer) (n int64, err error) {
- slice, err := img.image().gdImageJpeg()
- if err != nil {
- return 0, err
- }
-
- written, err := writer.Write(slice)
-
- // todo: return actual len of write
- return (int64)(written), err
-}
View
31 png.go
@@ -0,0 +1,31 @@
+package mogrify
+
+import (
+ "bytes"
+ "io"
+)
+
+type Png struct {
+ // Import GdImage and all it's methods
+ GdImage
+}
+
+func DecodePng(reader io.Reader) Image {
+ var image Png
+
+ image.gd = gdCreateFromPng(drain(reader))
+ if image.gd == nil {
+ return nil
+ }
+
+ return &image
+}
+
+func EncodePng(w io.Writer, img Image) (int64, error) {
+ slice, err := img.image().gdImagePng()
+ if err != nil {
+ return 0, err
+ }
+
+ return bytes.NewBuffer(slice).WriteTo(w)
+}
View
17 util.go
@@ -0,0 +1,17 @@
+package mogrify
+
+import (
+ "bytes"
+ "io"
+)
+
+func drain(reader io.Reader) []byte {
+ var buffer bytes.Buffer
+
+ _, err := buffer.ReadFrom(reader)
+ if err != nil {
+ return []byte{}
+ }
+
+ return buffer.Bytes()
+}

0 comments on commit 8c4321f

Please sign in to comment.