From 34004794e3427a033f636b88891df1615d51a384 Mon Sep 17 00:00:00 2001 From: Justin Israel Date: Sun, 2 Jun 2024 19:24:23 +1200 Subject: [PATCH] Update GetImageBlob/GetImagesBlob to return error as second element --- imagick/magick_wand_exception.go | 13 +++++++++++-- imagick/magick_wand_image.go | 13 +++++++------ imagick/magick_wand_test.go | 7 +++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/imagick/magick_wand_exception.go b/imagick/magick_wand_exception.go index e9961b4..5820e28 100644 --- a/imagick/magick_wand_exception.go +++ b/imagick/magick_wand_exception.go @@ -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) diff --git a/imagick/magick_wand_image.go b/imagick/magick_wand_image.go index ced987e..963b7b4 100644 --- a/imagick/magick_wand_image.go +++ b/imagick/magick_wand_image.go @@ -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 @@ -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. diff --git a/imagick/magick_wand_test.go b/imagick/magick_wand_test.go index 5779dcd..082836b 100644 --- a/imagick/magick_wand_test.go +++ b/imagick/magick_wand_test.go @@ -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") } @@ -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 {