Permalink
Browse files

Init has to be called by the first thread otherwise evil things happen

  • Loading branch information...
1 parent a60090d commit bd585755b283d6182e3d35bd859ba23e25defb8f @tobi committed May 3, 2012
Showing with 104 additions and 24 deletions.
  1. BIN assets/example.com.png
  2. BIN example/image.png
  3. +36 −0 example/test.go
  4. +18 −6 image.go
  5. +50 −18 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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,36 @@
+package main
+
+import (
+ "io/ioutil"
+ "github.com/tobi/mogrify-go"
+)
+
+// func Server(w http.ResponseWriter, r *http.Request) {
+// }
+
+func init() {
+ mogrify.Init()
+}
+
+func main() {
+
+
+ img := mogrify.NewImage()
+ defer img.Destroy()
+ bytes, _ := ioutil.ReadFile("image.png")
+
+ defer print("\n")
+
+ err := img.OpenBlob(bytes)
+
+ if err != nil {
+ print("error ")
+ print(err.Error())
+ return
+ }
+
+ print(img.Dimensions())
+
+
+
+}
View
@@ -1,7 +1,7 @@
package mogrify
// #cgo CFLAGS: -I/usr/local/include/GraphicsMagick
-// #cgo LDFLAGS: -lGraphicsMagickWand -lGraphicsMagick
+// #cgo LDFLAGS: -fopenmp -lGraphicsMagickWand -lGraphicsMagick
// #include <wand/magick_wand.h>
import "C"
@@ -10,8 +10,11 @@ import (
"fmt"
"io"
"unsafe"
+ "sync"
)
+var once sync.Once
+
var (
BlobEmpty = errors.New("blob was empty")
)
@@ -25,7 +28,7 @@ type ImageError struct {
severity int
}
-func init() {
+func Init() {
C.InitializeMagick(nil)
}
@@ -51,9 +54,14 @@ 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()
+
+ if image.wand == nil {
+ panic(image.error())
+ }
+
return image
}
@@ -85,7 +93,7 @@ func (img *Image) OpenBlob(bytes []byte) error {
return BlobEmpty
}
- status := C.MagickReadImageBlob(img.wand, (*C.uchar)(&bytes[0]), C.size_t(len(bytes)))
+ status := C.MagickReadImageBlob(img.wand, (*C.uchar)(unsafe.Pointer(&bytes[0])), C.size_t(len(bytes)))
if status == C.MagickFalse {
return img.error()
@@ -125,7 +133,7 @@ func (img *Image) Resize(width, height uint) error {
return nil
}
-func (img *Image) NewTransformation(crop, geometry string) *Image {
+func (img *Image) NewTransformation(crop, geometry string) (*Image, error) {
ccrop := C.CString(crop)
defer C.free(unsafe.Pointer(ccrop))
@@ -134,7 +142,11 @@ func (img *Image) NewTransformation(crop, geometry string) *Image {
wand := C.MagickTransformImage(img.wand, ccrop, cgeometry)
- return &Image{(*C.MagickWand)(wand)}
+ if wand == nil {
+ return nil, img.error()
+ }
+
+ return &Image{(*C.MagickWand)(wand)}, nil
}
func (img *Image) SaveFile(filename string) error {
View
@@ -7,6 +7,17 @@ import (
"testing"
)
+func init() {
+ Init()
+}
+
+
+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)
+ }
+}
+
func TestOpenExisting(t *testing.T) {
img := Open("./assets/image.jpg")
if img == nil {
@@ -26,16 +37,15 @@ func TestHeightWidth(t *testing.T) {
if img == nil {
t.Fail()
}
+
if img.Width() != 600 {
- log.Printf("%d", img.Width())
- t.Fail()
+
+ t.Fatalf("%d", img.Width())
}
if img.Height() != 399 {
- log.Printf("%d", img.Height())
- t.Fail()
+ t.Fatalf("%d", img.Height())
}
-
}
func TestResizeSuccess(t *testing.T) {
@@ -113,12 +123,29 @@ func TestOpenBlopSuccess(t *testing.T) {
img := NewImage()
res := img.OpenBlob(bytes)
+ defer img.Destroy()
if res != nil {
- t.Fail()
+ t.FailNow()
}
- img.Destroy()
+ assertDimension(t, img, "600x399")
+}
+
+func TestOpenBlopSuccessPng(t *testing.T) {
+ bytes, _ := ioutil.ReadFile("./assets/example.com.png")
+
+ img := NewImage()
+ res := img.OpenBlob(bytes)
+ defer img.Destroy()
+
+ if res != nil {
+ t.FailNow()
+ }
+
+ if dim := img.Dimensions(); dim != "1280x500" {
+ t.Fatalf("Got wrong dimensions expected:1280x500 got %s", dim)
+ }
}
func TestOpenBlopFailure(t *testing.T) {
@@ -159,23 +186,28 @@ func TestSaveToBlob(t *testing.T) {
func TestTransformation(t *testing.T) {
img := Open("./assets/image.jpg")
- img2 := img.NewTransformation("", "100x50>")
-
defer img.Destroy()
- defer img2.Destroy()
- if img2.Dimensions() != "75x50" {
- log.Print(img2.Dimensions())
- t.Fail()
+ img2, err := img.NewTransformation("", "100x50>")
+
+ if err != nil {
+ t.FailNow()
+ return
}
+
+ defer img2.Destroy()
+
+ assertDimension(t, img2, "75x50")
- img3 := img.NewTransformation("", "100x50!")
+ img3, err := img.NewTransformation("", "100x50!")
defer img3.Destroy()
- if img3.Dimensions() != "100x50" {
- log.Print(img3.Dimensions())
- t.Fail()
+ if err != nil {
+ t.FailNow()
+ return
}
+ assertDimension(t, img3, "100x50")
+
//img2.SaveFile("/tmp/img4.jpg")
-}
+}

0 comments on commit bd58575

Please sign in to comment.