Permalink
Browse files

Rewrote jpg code in terms of type composition

  • Loading branch information...
1 parent cc7e13b commit c5366e5960aa6c32a014a6c90717ed3a29ec6b54 @tobi committed May 29, 2012
Showing with 95 additions and 56 deletions.
  1. +1 −1 example/resize.go
  2. +57 −0 gd_image.go
  3. +11 −1 image.go
  4. +7 −4 image_test.go
  5. +19 −50 jpg.go
View
@@ -16,7 +16,7 @@ func main() {
return
}
- jpg := mogrify.NewJpg(file)
+ jpg := mogrify.DecodeJpg(file)
if jpg == nil {
fmt.Println("could not load image")
View
@@ -0,0 +1,57 @@
+package mogrify
+
+import (
+ "io"
+)
+
+type GdImage struct {
+ gd *gdImage
+}
+
+func NewImage(width, height int) Image {
+ var image GdImage
+ image.gd = gdCreate(width, height)
+ if image.gd == nil {
+ return nil
+ }
+
+ return &image
+}
+
+func (img *GdImage) NewResized(width, height int) (*GdImage, error) {
+ resized := img.image().gdCopyResized(0, 0, 0, 0, width, height, img.image().width(), img.image().height())
+ if resized == nil {
+ return nil, resampleError
+ }
+
+ return &GdImage{resized}, nil
+}
+
+func (img *GdImage) NewResampled(width, height int) (*GdImage, error) {
+ resized := img.image().gdCopyResampled(0, 0, 0, 0, width, height, img.image().width(), img.image().height())
+ if resized == nil {
+ return nil, resampleError
+ }
+
+ return &GdImage{resized}, nil
+}
+
+func (img *GdImage) Width() int {
+ return img.image().width()
+}
+
+func (img *GdImage) Height() int {
+ return img.image().height()
+}
+
+func (img *GdImage) image() *gdImage {
+ return img.gd
+}
+
+func (img *GdImage) Destroy() {
+ img.image().gdDestroy()
+}
+
+func (img *GdImage) Encode(w io.Writer) (int64, error) {
+ panic("No idea how")
+}
View
@@ -3,6 +3,7 @@ package mogrify
import (
"fmt"
"errors"
+ "io"
)
var (
@@ -14,8 +15,17 @@ var (
type Image interface {
Width() int
Height() int
+ Destroy()
+
+ NewResampled(width, height int) (*GdImage, error)
+ NewResized(width, height int) (*GdImage, error)
+
+ Encode(w io.Writer) (int64, error)
+
+ image() *gdImage
}
func Dimensions(img Image) string {
return fmt.Sprintf("%dx%d", img.Width(), img.Height())
-}
+}
+
View
@@ -11,11 +11,11 @@ func assertDimension(t *testing.T, img Image, expected string) {
}
}
-func asset(asset string) *Jpg {
+func asset(asset string) Image {
file, _ := os.Open("./assets/image.jpg")
defer file.Close()
- image := NewJpg(file)
+ image := DecodeJpg(file)
if image == nil {
panic("Image didnt load")
@@ -71,7 +71,7 @@ func TestResampleSuccess(t *testing.T) {
}
func TestCrateFailure(t *testing.T) {
- image := NewBlankJpg(-1, -1)
+ image := NewImage(-1, -1)
if image != nil {
t.Fatalf("This should have failed...")
}
@@ -95,7 +95,10 @@ func TestSave(t *testing.T) {
dest, _ := os.Create("/tmp/dest.jpg")
defer dest.Close()
- resized.WriteTo(dest)
+
+
+
+ resized.Encode(dest)
}
View
@@ -10,84 +10,53 @@ var (
BlobEmpty = errors.New("blob was empty")
)
-type Jpg struct {
- gd *gdImage
-}
-func NewJpg(reader io.Reader) *Jpg {
- var image Jpg
- if _, err := image.ReadFrom(reader); err != nil {
- return nil
- }
- return &image
+type Jpg struct {
+ GdImage
}
-func NewBlankJpg(width, height int) *Jpg {
+func DecodeJpg(reader io.Reader) Image {
var image Jpg
- image.gd = gdCreate(width, height)
- if image.gd == nil {
+ if _, err := readFromJpg(reader); err != nil {
return nil
}
-
return &image
}
-func (img *Jpg) ReadFrom(reader io.Reader) (n int64, err error) {
+// Take an io.Reader and read a gdImage from it
+func readFromJpg(reader io.Reader) (gd *gdImage, err error) {
var buffer bytes.Buffer
- n, err = buffer.ReadFrom(reader)
+ _, err = buffer.ReadFrom(reader)
if err != nil {
return
}
- gd := gdCreateFromJpeg(buffer.Bytes())
+ gd = gdCreateFromJpeg(buffer.Bytes())
if gd == nil {
- return n, loadError
+ return nil, loadError
}
- img.Destroy()
- img.gd = gd
- return
+ return gd, nil
}
-func (img *Jpg) WriteTo(writer io.Writer) (n int64, err error) {
- slice, err := img.gd.gdImageJpeg()
+func writeAsJpg(img Image, writer io.Writer) (n int64, err error) {
+ slice, err := img.image().gdImageJpeg()
if err != nil {
return 0, err
}
-
_, err = writer.Write(slice)
-
+ // todo: return actual len of write
return 0, err
}
-func (img *Jpg) Width() int {
- return img.gd.width()
-}
-
-func (img *Jpg) Height() int {
- return img.gd.height()
-}
-
-func (img *Jpg) NewResized(width, height int) (*Jpg, error) {
- resized := img.gd.gdCopyResized(0, 0, 0, 0, width, height, img.gd.width(), img.gd.height())
- if resized == nil {
- return nil, resampleError
- }
-
- return &Jpg{resized}, nil
-}
+func (img *Jpg) Encode(w io.Writer) (int64, error) {
-func (img *Jpg) NewResampled(width, height int) (*Jpg, error) {
- resized := img.gd.gdCopyResampled(0, 0, 0, 0, width, height, img.gd.width(), img.gd.height())
- if resized == nil {
- return nil, resampleError
+ slice, err := img.image().gdImageJpeg()
+ if err != nil {
+ return 0, err
}
- return &Jpg{resized}, nil
-}
-
-func (img *Jpg) Destroy() {
- img.gd.gdDestroy()
-}
+ return bytes.NewBuffer(slice).WriteTo(w)
+}

0 comments on commit c5366e5

Please sign in to comment.