Skip to content

Commit

Permalink
Update GetImageBlob/GetImagesBlob to return error as second element (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfx authored Jun 2, 2024
1 parent 76b9205 commit 0c141b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
13 changes: 11 additions & 2 deletions imagick/magick_wand_exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ func (mw *MagickWand) clearException() bool {
return 1 == C.int(C.MagickClearException(mw.mw))
}

// Returns the kind, reason and description of any error that occurs when using other methods in this API
// GetLastError returns the kind, reason and description of any error that occurs when using other methods in this API.
// The exception is cleared after this call.
func (mw *MagickWand) GetLastError() error {
return mw.getLastError(true)
}

// Returns the kind, reason and description of any error that occurs when using other methods in this API.
// Clears the exception, if clear is true.
func (mw *MagickWand) getLastError(clear bool) error {
var et C.ExceptionType
csdescription := C.MagickGetException(mw.mw, &et)
defer relinquishMemory(unsafe.Pointer(csdescription))
if ExceptionType(et) != EXCEPTION_UNDEFINED {
mw.clearException()
if clear {
mw.clearException()
}
return &MagickWandException{ExceptionType(C.int(et)), C.GoString(csdescription)}
}
runtime.KeepAlive(mw)
Expand Down
13 changes: 7 additions & 6 deletions imagick/magick_wand_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,16 +937,16 @@ func (mw *MagickWand) GetImageBackgroundColor() (bgColor *PixelWand, err error)
// position in the image sequence. Use SetImageFormat() to set the format to
// write to the blob (GIF, JPEG, PNG, etc.). Utilize ResetIterator() to ensure
// the write is from the beginning of the image sequence.
func (mw *MagickWand) GetImageBlob() []byte {
func (mw *MagickWand) GetImageBlob() ([]byte, error) {
clen := C.size_t(0)
csblob := C.MagickGetImageBlob(mw.mw, &clen)
defer relinquishMemory(unsafe.Pointer(csblob))
if err := mw.GetLastError(); err != nil {
return nil
return nil, err
}
ret := C.GoBytes(unsafe.Pointer(csblob), C.int(clen))
runtime.KeepAlive(mw)
return ret
return ret, nil
}

// GetImagesBlob Implements direct to memory image formats. It returns the image sequence
Expand All @@ -955,15 +955,16 @@ func (mw *MagickWand) GetImageBlob() []byte {
// format, use SetImageFormat(). Note, some image formats do not permit
// multiple images to the same image stream (e.g. JPEG). in this instance,
// just the first image of the sequence is returned as a blob.
func (mw *MagickWand) GetImagesBlob() []byte {
func (mw *MagickWand) GetImagesBlob() ([]byte, error) {
clen := C.size_t(0)
csblob := C.MagickGetImagesBlob(mw.mw, &clen)
defer relinquishMemory(unsafe.Pointer(csblob))
if err := mw.GetLastError(); err != nil {
return nil
return nil, err
}
ret := C.GoBytes(unsafe.Pointer(csblob), C.int(clen))
runtime.KeepAlive(mw)
return C.GoBytes(unsafe.Pointer(csblob), C.int(clen))
return ret, nil
}

// GetImageBluePrimary Returns the chromaticy blue primary point for the image.
Expand Down
7 changes: 5 additions & 2 deletions imagick/magick_wand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestReadImageBlob(t *testing.T) {
mw := NewMagickWand()

// Try to read an empty wand
blob := mw.GetImageBlob()
blob, _ := mw.GetImageBlob()
if blob != nil {
t.Fatal("Expected nil blob on invalid wand")
}
Expand All @@ -246,7 +246,10 @@ func TestReadImageBlob(t *testing.T) {
}

mw.ReadImage(`logo:`)
blob = mw.GetImageBlob()
blob, err := mw.GetImageBlob()
if err != nil {
t.Fatal(err)
}

// Read a valid blob
if err := mw.ReadImageBlob(blob); err != nil {
Expand Down

0 comments on commit 0c141b9

Please sign in to comment.