Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement blob reading and writing

  • Loading branch information...
commit a7dc1e2b84669004f362786d31805c044cfa73e5 1 parent 970e095
@tobi authored
Showing with 94 additions and 12 deletions.
  1. +43 −12 gm.go
  2. +51 −0 gm_test.go
View
55 gm.go
@@ -8,12 +8,12 @@ import "C"
import (
"errors"
"fmt"
+ "io"
"unsafe"
)
var (
- CannotOpen = errors.New("Cannot open file")
- ResizeFailed = errors.New("Resize operation failed")
+ BlobEmpty = errors.New("blob was empty")
)
type Image struct {
@@ -26,12 +26,21 @@ type ImageError struct {
severity int
}
+func init() {
+ C.InitializeMagick(nil)
+}
+
func (e *ImageError) Error() string {
return fmt.Sprintf("GraphicsMagick: %s severity: %d", e.message, e.severity)
}
-func init() {
- C.InitializeMagick(nil)
+func Open(filename string) *Image {
+ image := NewImage()
+
+ if image.OpenFile(filename) == nil {
+ return image
+ }
+ return nil
}
func (img *Image) exception() error {
@@ -49,24 +58,46 @@ func NewImage() *Image {
return image
}
-func Open(filename string) *Image {
- image := NewImage()
+func (img *Image) OpenFile(filename string) error {
+ cfilename := C.CString(filename)
+ defer C.free(unsafe.Pointer(cfilename))
- if image.OpenFile(filename) == nil {
- return image
+ status := C.MagickReadImage(img.wand, cfilename)
+ if status == C.MagickFalse {
+ return img.exception()
}
return nil
}
-func (img *Image) OpenFile(filename string) error {
- status := C.MagickReadImage(img.wand, C.CString(filename))
+func (img *Image) OpenBlob(bytes []byte) error {
+ if len(bytes) < 1 {
+ return BlobEmpty
+ }
+
+ status := C.MagickReadImageBlob(img.wand, (*C.uchar)(&bytes[0]), C.size_t(len(bytes)))
+
if status == C.MagickFalse {
- return CannotOpen
+ return img.exception()
}
return nil
}
-func (img *Image) Resize(width, height uint64) error {
+func (img *Image) Write(writer io.Writer) (int, error) {
+ var len C.size_t
+ char_ptr := C.MagickWriteImageBlob(img.wand, &len)
+
+ if char_ptr == nil {
+ return 0, img.exception()
+ }
+
+ defer C.free(unsafe.Pointer(char_ptr))
+
+ bytes := C.GoBytes(unsafe.Pointer(char_ptr), C.int(len))
+
+ return writer.Write(bytes)
+}
+
+func (img *Image) Resize(width, height uint) error {
res := C.MagickResizeImage(img.wand, C.ulong(width), C.ulong(height), C.GaussianFilter, 1)
if res == C.MagickFalse {
View
51 gm_test.go
@@ -1,7 +1,9 @@
package mogrify
import (
+ "io/ioutil"
"log"
+ "os"
"testing"
)
@@ -83,3 +85,52 @@ func TestSaveToFailure(t *testing.T) {
t.Fail()
}
}
+
+func TestOpenBlopSuccess(t *testing.T) {
+ bytes, _ := ioutil.ReadFile("./assets/image.jpg")
+
+ img := NewImage()
+ res := img.OpenBlob(bytes)
+
+ if res != nil {
+ t.Fail()
+ }
+
+ img.Destroy()
+}
+
+func TestOpenBlopFailure(t *testing.T) {
+
+ img := NewImage()
+ res := img.OpenBlob([]byte{'a'})
+
+ if res == nil {
+ t.Fail()
+ }
+
+ res = img.OpenBlob([]byte{})
+
+ if res == nil {
+ t.Fail()
+ }
+}
+
+func TestSaveToBlob(t *testing.T) {
+ img := Open("./assets/image.jpg")
+
+ fp, err := os.Create("/tmp/img3.jpg")
+ if err != nil {
+ t.Fail()
+ }
+
+ defer fp.Close()
+
+ n, err := img.Write(fp)
+
+ if err != nil {
+ t.Fail()
+ }
+
+ log.Printf("%d", n)
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.