Permalink
Browse files

Added WriteTo and ReadFrom convenience methods

  • Loading branch information...
1 parent fa0ec28 commit 37d5cb6b388ec2e9177da9baf0ff01c0c0035570 @tobi committed May 12, 2012
Showing with 39 additions and 10 deletions.
  1. BIN assets/image.jpg
  2. BIN example/example
  3. +26 −3 image.go
  4. +13 −7 image_test.go
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Binary file not shown.
View
@@ -2,17 +2,18 @@ package mogrify
/*
#cgo CFLAGS: -fopenmp -I/usr/local/include/ImageMagick -I/usr/include/ImageMagick
-#cgo LDFLAGS: -lMagickWand -lMagickCore
+#cgo LDFLAGS: -lMagickCore -lMagickWand
#include <wand/magick_wand.h>
*/
import "C"
import (
+ "bytes"
"errors"
"fmt"
"io"
- "unsafe"
"sync"
+ "unsafe"
)
var once sync.Once
@@ -52,7 +53,7 @@ func (img *Image) error() error {
return &ImageError{C.GoString(char_ptr), int(ex)}
}
-func NewImage() *Image {
+func NewImage() *Image {
image := new(Image)
image.wand = C.NewMagickWand()
@@ -68,12 +69,34 @@ func (img *Image) OpenFile(filename string) error {
defer C.free(unsafe.Pointer(cfilename))
status := C.MagickReadImage(img.wand, cfilename)
+
if status == C.MagickFalse {
return img.error()
}
return nil
}
+func (img *Image) ReadFrom(reader io.Reader) (n int64, err error) {
+ buffer := bytes.NewBuffer(nil)
+
+ n, err = buffer.ReadFrom(reader)
+
+ if err != nil {
+ return
+ }
+
+ return n, img.OpenBlob(buffer.Bytes())
+}
+
+func (img *Image) WriteTo(writer io.Writer) (n int64, err error) {
+ buffer, err := img.Blob()
+ if err != nil {
+ return 0, err
+ }
+
+ return bytes.NewBuffer(buffer).WriteTo(writer)
+}
+
func (img *Image) Width() int64 {
return int64(C.MagickGetImageWidth(img.wand))
}
View
@@ -1,14 +1,13 @@
package mogrify
import (
- "runtime"
"io/ioutil"
"log"
"os"
+ "runtime"
"testing"
)
-
func assertDimension(t *testing.T, img *Image, expected string) {
if actual := img.Dimensions(); actual != expected {
t.Fatalf("Got wrong dimensions expected:%s got %s", expected, actual)
@@ -36,7 +35,7 @@ func TestHeightWidth(t *testing.T) {
}
if img.Width() != 600 {
-
+
t.Fatalf("%d", img.Width())
}
@@ -189,9 +188,9 @@ func TestTransformation(t *testing.T) {
if err != nil {
t.FailNow()
- return
+ return
}
-
+
defer img2.Destroy()
assertDimension(t, img2, "75x50")
@@ -201,14 +200,21 @@ func TestTransformation(t *testing.T) {
if err != nil {
t.FailNow()
- return
+ return
}
assertDimension(t, img3, "100x50")
//img2.SaveFile("/tmp/img4.jpg")
}
+func TestReadFrom(t *testing.T) {
+ file, _ := os.Open("./assets/image.jpg")
+ image := NewImage()
+ image.ReadFrom(file)
+ assertDimension(t, image, "100x50")
+}
+
func BenchmarkAndMemoryTest(b *testing.B) {
var before runtime.MemStats
var after runtime.MemStats
@@ -226,5 +232,5 @@ func BenchmarkAndMemoryTest(b *testing.B) {
runtime.ReadMemStats(&after)
- log.Printf("sys memory before: %d after %d - diff: %d", before.HeapSys, after.HeapSys, after.HeapSys - before.HeapSys)
+ log.Printf("sys memory before: %d after %d - diff: %d", before.HeapSys, after.HeapSys, after.HeapSys-before.HeapSys)
}

0 comments on commit 37d5cb6

Please sign in to comment.