Permalink
Browse files

split it up into multiple structs for formats

  • Loading branch information...
1 parent 5cdac9f commit 55b95af36373ac3d8a4be62f3fbc2d55ee6a059f @tobi committed May 14, 2012
Showing with 223 additions and 183 deletions.
  1. BIN example/example
  2. +40 −0 example/resize.go
  3. +0 −30 example/test.go
  4. +0 −14 example/test2.go
  5. +26 −7 gd.go
  6. +4 −84 image.go
  7. +54 −48 image_test.go
  8. +99 −0 jpg.go
View
Binary file not shown.
View
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "os"
+ "fmt"
+ "github.com/tobi/mogrify-go"
+)
+
+func main() {
+
+ file, err := os.Open("../assets/image.jpg")
+
+ if err != nil {
+ fmt.Errorf("Error loading file: %s ", err)
+ os.Exit(1)
+ return
+ }
+
+ jpg := mogrify.NewJpg(file)
+
+ if jpg == nil {
+ fmt.Println("could not load image")
+ os.Exit(1)
+ return
+ }
+
+ defer jpg.Destroy()
+
+ resized, err := jpg.NewResized(50, 50)
+
+ if err != nil {
+ fmt.Errorf("failed to resize: %s", err)
+ os.Exit(1)
+ return
+ }
+
+ defer resized.Destroy()
+
+ fmt.Printf("Resized image from %s => %s \n", mogrify.Dimensions(jpg), mogrify.Dimensions(resized))
+}
View
@@ -1,30 +0,0 @@
-package main
-
-import (
- "fmt"
- "github.com/tobi/mogrify-go"
-)
-
-func main() {
-
- img := mogrify.NewImage()
- defer img.Destroy()
-
- if err := img.OpenFile("../assets/example.com.png"); err != nil {
- panic(err.Error())
- }
-
- fmt.Printf("Image dimensions: %s\n", img.Dimensions())
-
- resized, err := img.NewTransformation("", "100x75>")
-
- if err != nil {
- panic(err.Error())
- }
- defer resized.Destroy()
-
- fmt.Printf("Resized image dimensions: %s\n", resized.Dimensions())
-
- resized.SaveFile("/tmp/image.png")
-
-}
View
@@ -1,14 +0,0 @@
-package main
-
-import (
- "github.com/tobi/mogrify-go"
-)
-
-func main() {
- img := mogrify.NewImage()
- defer img.Destroy()
-
- if err := img.OpenFile("../assets/example.com.png"); err != nil {
- panic(err.Error())
- }
-}
View
33 gd.go
@@ -6,25 +6,31 @@ import "C"
import (
// "bytes"
+ // "fmt"
+ "errors"
"unsafe"
)
+var (
+ imageError = errors.New("image is nil")
+)
+
type gdImage struct {
img *C.gdImage
}
func img(img *C.gdImage) *gdImage {
+ if img == nil {
+ return nil
+ }
+
image := &gdImage{img}
- if image.invalid() {
+ if isInvalid(image) {
return nil
}
return image
}
-func assertGd(img *C.gdImage) bool {
- return (*img).pixels != nil
-}
-
func gdCreate(sx, sy int) *gdImage {
return img(C.gdImageCreate(C.int(sx), C.int(sy)))
}
@@ -47,24 +53,37 @@ func (p *gdImage) gdDestroy() {
}
}
-func (p *gdImage) invalid() bool {
- return p == nil || (*p.img).pixels == nil
+func isInvalid(p *gdImage) bool {
+ return p == nil
}
func (p *gdImage) width() int {
+ if p == nil {
+ panic(imageError)
+ }
return int((*p.img).sx)
}
func (p *gdImage) height() int {
+ if p == nil {
+ panic(imageError)
+ }
return int((*p.img).sy)
}
func (p *gdImage) gdCopyResampled(dst *gdImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH int) {
+ if p == nil || p.img == nil {
+ panic(imageError)
+ }
+
C.gdImageCopyResampled(dst.img, p.img, C.int(dstX), C.int(dstY), C.int(srcX), C.int(srcY),
C.int(dstW), C.int(dstH), C.int(srcW), C.int(srcH))
}
func (p *gdImage) gdCopyResized(dst *gdImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH int) {
+ if p == nil {
+ panic(imageError)
+ }
C.gdImageCopyResized(dst.img, p.img, C.int(dstX), C.int(dstY), C.int(srcX), C.int(srcY),
C.int(dstW), C.int(dstH), C.int(srcW), C.int(srcH))
}
View
@@ -1,94 +1,14 @@
package mogrify
import (
- "bytes"
- "errors"
- "fmt"
- "io"
-)
-
-var (
- BlobEmpty = errors.New("blob was empty")
-)
-
-type Format int
-
-var (
- JpgLoadError = errors.New("Jpeg cannot be loaded")
- resampleError = errors.New("Resampling failed")
+ "fmt"
)
type Image interface {
- Width() int
- Height() int
+ Width() int
+ Height() int
}
func Dimensions(img Image) string {
- return fmt.Sprintf("%dx%d", img.Width(), img.Height())
-}
-
-
-type Jpg struct {
- gd *gdImage
-}
-
-func NewJpg(reader io.Reader) *Jpg {
- var image Jpg
- image.ReadFrom(reader)
- return &image
-}
-
-func (img *Jpg) ReadFrom(reader io.Reader) (n int64, err error) {
- var buffer bytes.Buffer
- n, err = buffer.ReadFrom(reader)
-
- if err != nil {
- return
- }
-
- gd := gdCreateFromJpeg(buffer.Bytes())
- if gd == nil {
- return n, JpgLoadError
- }
-
- if img.gd != nil {
- img.Destroy()
- }
-
- img.gd = gd
- return
-}
-
-func (img *Jpg) WriteTo(writer io.Writer) (n int64, err error) {
- return 0, nil
-}
-
-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) {
- dst := gdCreate(width, height)
- img.gd.gdCopyResized(dst, 0, 0, 0, 0, width, height, img.gd.width(), img.gd.height())
-
- return &Jpg{dst}, nil
-}
-
-func (img *Jpg) NewResampled(width, height int) (*Jpg, error) {
- dst := gdCreate(width, height)
- img.gd.gdCopyResampled(dst, 0, 0, 0, 0, width, height, img.gd.width(), img.gd.height())
-
- if dst.invalid() {
- return nil, resampleError
- }
-
- return &Jpg{dst}, nil
-}
-
-func (img *Jpg) Destroy() {
- img.gd.gdDestroy()
+ return fmt.Sprintf("%dx%d", img.Width(), img.Height())
}
Oops, something went wrong.

0 comments on commit 55b95af

Please sign in to comment.