// Returns all metadata keys from the currently loaded image. func (self *Canvas) Metadata() map[string]string { var n C.size_t var i C.size_t var value *C.char var key *C.char data := make(map[string]string) cplist := C.CString("*") properties := C.MagickGetImageProperties(self.wand, cplist, &n) C.free(unsafe.Pointer(cplist)) for i = 0; i < n; i++ { key = C.MagickGetPropertyName(properties, C.size_t(i)) value = C.MagickGetImageProperty(self.wand, key) data[strings.Trim(C.GoString(key), " ")] = strings.Trim(C.GoString(value), " ") C.MagickRelinquishMemory(unsafe.Pointer(value)) C.MagickRelinquishMemory(unsafe.Pointer(key)) } return data }
/* Implements direct to memory image formats. */ func (w *MagickWand) ImageBlob(length *uint) []byte { blobPtr := unsafe.Pointer(C.MagickGetImageBlob(w.wand, (*C.size_t)(unsafe.Pointer(length)))) blob := C.GoBytes(blobPtr, C.int(int(*length))) // need free blob memory C.MagickRelinquishMemory(blobPtr) return blob }
func (img *Image) GetBytes() []byte { size := 0 buf := C.MagickGetImageBlob(img.wand, (*C.size_t)(unsafe.Pointer(&size))) defer C.MagickRelinquishMemory(unsafe.Pointer(buf)) return C.GoBytes(unsafe.Pointer(buf), C.int(size)) }
/* WriteImageBlob() writes this image wand to Blob */ func (this *Image) WriteImageBlob() error { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "WriteImageBlob") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand == nil { err = errors.New("error write image to blob:magickwand is nil") return err } var sizep int = 0 blob := C.MagickWriteImageBlob(this.magickWand, (*C.size_t)(unsafe.Pointer(&sizep))) if blob != nil { defer C.free(unsafe.Pointer(blob)) } else { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error write image to blob: %s (ExceptionType=%d)", C.GoString(descr), etype)) return err } this.Blob = C.GoBytes(unsafe.Pointer(blob), C.int(sizep)) return nil }
/* Sets the file or blob format (e.g. "BMP") to be used when a file or blob is read. Usually this is not necessary because GraphicsMagick is able to auto-detect the format based on the file header (or the file extension), but some formats do not use a unique header or the selection may be ambigious. Use MagickSetImageFormat() to set the format to be used when a file or blob is to be written. format: The file or blob format */ func (this *Image) SetFormat(format string) error { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "SetFormat") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand == nil { err = errors.New("error set image format:magickwand is nil") return err } var cs *C.char = C.CString(format) defer C.free(unsafe.Pointer(cs)) status := C.MagickSetImageFormat(this.magickWand, cs) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error set image format: %s (ExceptionType = %d)", C.GoString(descr), etype)) return err } return nil }
/* Composite() composite one image onto another at the specified offset. compositeImg: The composite image x: The column offset of the composited image. y: The row offset of the composited image. */ func (this *Image) Composite(compositeImg *Image, x int64, y int64) error { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "Composite") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand == nil { err = errors.New("error composite image:magickwand is nil") return err } if compositeImg.magickWand == nil { err = errors.New("error composite image:composite image wand is nil") return err } status := C.MagickCompositeImage(this.magickWand, compositeImg.magickWand, C.OverCompositeOp, C.long(x), C.long(y)) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error composite image: %s (ExceptionType = %d)", C.GoString(descr), etype)) return err } return nil }
// Returns the severity, reason, and description of any error that occurs // when using other methods in this API. func (p *PixelWand) Exception() (string, int) { var severity C.ExceptionType errPtr := C.PixelGetException(p.wand, &severity) C.PixelClearException(p.wand) err := C.GoString(errPtr) C.MagickRelinquishMemory(unsafe.Pointer(errPtr)) return err, int(severity) }
// Returns the latest error reported by the MagickWand API. func (self *Canvas) Error() error { var t C.ExceptionType ptr := C.MagickGetException(self.wand, &t) message := C.GoString(ptr) C.MagickClearException(self.wand) C.MagickRelinquishMemory(unsafe.Pointer(ptr)) return errors.New(message) }
// Writes canvas to a file, returns true on success. func (cv Canvas) GetImageBlob() []byte { cv.Update() var length C.size_t data := C.MagickGetImageBlob(cv.wand, &length) bin := C.GoBytes(unsafe.Pointer(data), C.int(length)) C.MagickRelinquishMemory(unsafe.Pointer(data)) return bin }
// Public: export the current image into a blob. Also destroy the current wand // // Examples: // im = NewMage() // original, err := ioutil.ReadFile("test.jpg") // success := im.ReadBlob(original) // imageBytes := im.ExportBlob() func (m *Mage) ExportBlob() []byte { defer m.Destroy() newSize := C.size_t(0) C.MagickResetIterator(m.wand) image := C.MagickGetImageBlob(m.wand, &newSize) imagePointer := unsafe.Pointer(image) defer C.MagickRelinquishMemory(imagePointer) return C.GoBytes(imagePointer, C.int(newSize)) }
func (self *PixelIterator) Error() error { var exception C.ExceptionType ptr := C.PixelGetIteratorException(self.iterator, &exception) message := C.GoString(ptr) C.PixelClearIteratorException(self.iterator) C.MagickRelinquishMemory(unsafe.Pointer(ptr)) return errors.New(message) }
// Get image data as a byte array. func (self *Canvas) GetImageBlob() ([]byte, error) { var size C.size_t = 0 p := unsafe.Pointer(C.MagickGetImageBlob(self.wand, &size)) if size == 0 { return nil, errors.New("Could not get image blob.") } blob := C.GoBytes(p, C.int(size)) C.MagickRelinquishMemory(p) return blob, nil }
//get image as byte array func (self *Canvas) GetImageBlob() ([]byte, error) { var imageSize C.size_t = 0 imageBlobPointer := unsafe.Pointer(C.MagickGetImageBlob(self.wand, &imageSize)) if imageSize == 0 { return nil, fmt.Errorf("Could not get image blob \n") } goBlob := C.GoBytes(imageBlobPointer, C.int(imageSize)) C.MagickRelinquishMemory(imageBlobPointer) return goBlob, nil }
/* Scale() scales the size of this image to the given dimensions. columns: The number of columns in the scaled image. rows: The number of rows in the scaled image */ func (this *Image) Scale(columns int64, rows int64) error { if this.magickWand == nil { return errors.New("error scale image:magickwand is nil") } status := C.MagickScaleImage(this.magickWand, C.ulong(columns), C.ulong(rows)) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) return errors.New(fmt.Sprintf("error scale image: %s (ExceptionType = %d)", C.GoString(descr), etype)) } return nil }
func (this *Image) Resize(width int64, height int64) error { if this.magickWand == nil { return errors.New("error resizing image:magickwand is nil") } status := C.MagickResizeImage(this.magickWand, C.ulong(width), C.ulong(height), C.CubicFilter, 0.5) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) return errors.New(fmt.Sprintf("error resizing image: %s (ExceptionType = %d)", C.GoString(descr), etype)) } return nil }
/* Strip() removes all profiles and text attributes from this image. */ func (this *Image) Strip() error { if this.magickWand == nil { return errors.New("error strip image:magickwand is nil") } status := C.MagickStripImage(this.magickWand) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) return errors.New(fmt.Sprintf("error strip image: %s (ExceptionType = %d)", C.GoString(descr), etype)) } return nil }
/* Sets the image quality factor, which determines compression options when saving the file quality: The image quality */ func (this *Image) SetCompressionQuality(quality int) error { if this.magickWand == nil { return errors.New("error set image compression quality:magickwand is nil") } status := C.MagickSetCompressionQuality(this.magickWand, C.ulong(quality)) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) return errors.New(fmt.Sprintf("error set image compression quality: %s (ExceptionType = %d)", C.GoString(descr), etype)) } return nil }
/* CreateWand() creates a new wand for this Image by using Blob data */ func (this *Image) CreateWand() error { if this.magickWand != nil { this.DestoryWand() } status := C.createWand(&this.magickWand, (*C.uchar)(unsafe.Pointer(&this.Blob[0])), C.size_t(len(this.Blob))) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err := errors.New(fmt.Sprintf("error create magick wand: %s (ExceptionType = %d)", C.GoString(descr), etype)) this.DestoryWand() return err } return nil }
/* Sets the file or blob format (e.g. "BMP") to be used when a file or blob is read. Usually this is not necessary because GraphicsMagick is able to auto-detect the format based on the file header (or the file extension), but some formats do not use a unique header or the selection may be ambigious. Use MagickSetImageFormat() to set the format to be used when a file or blob is to be written. format: The file or blob format */ func (this *Image) SetFormat(format string) error { if this.magickWand == nil { return errors.New("error set image format:magickwand is nil") } var cs *C.char = C.CString(format) defer C.free(unsafe.Pointer(cs)) status := C.MagickSetFormat(this.magickWand, cs) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) return errors.New(fmt.Sprintf("error set image format: %s (ExceptionType = %d)", C.GoString(descr), etype)) } return nil }
/* WriteImageBlob() writes this image wand to Blob */ func (this *Image) WriteImageBlob() error { if this.magickWand == nil { return errors.New("error write image to blob:magickwand is nil") } var sizep int = 0 blob := C.MagickWriteImageBlob(this.magickWand, (*C.size_t)(unsafe.Pointer(&sizep))) if blob != nil { defer C.free(unsafe.Pointer(blob)) } else { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err := errors.New(fmt.Sprintf("error write image to blob: %s (ExceptionType=%d)", C.GoString(descr), etype)) return err } this.Blob = C.GoBytes(unsafe.Pointer(blob), C.int(sizep)) return nil }
/* Composite() composite one image onto another at the specified offset. compositeImg: The composite image x: The column offset of the composited image. y: The row offset of the composited image. */ func (this *Image) Composite(compositeImg *Image, x int64, y int64) error { if this.magickWand == nil { return errors.New("error composite image:magickwand is nil") } if compositeImg.magickWand == nil { return errors.New("error composite image:composite image wand is nil") } compositeImg.Dissolve(100) status := C.MagickCompositeImage(this.magickWand, compositeImg.magickWand, C.OverCompositeOp, C.long(x), C.long(y)) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) return errors.New(fmt.Sprintf("error composite image: %s (ExceptionType = %d)", C.GoString(descr), etype)) } return nil }
/* CreateWand() creates a new wand for this Image by using Blob data */ func (this *Image) CreateWand() error { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "CreateWand") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand != nil { this.DestoryWand() } status := C.createWand(&this.magickWand, (*C.uchar)(unsafe.Pointer(&this.Blob[0])), C.size_t(len(this.Blob))) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error create magick wand: %s (ExceptionType = %d)", C.GoString(descr), etype)) this.DestoryWand() return err } return nil }
/* GetFormat() return format of this image */ func (this *Image) GetFormat() (string, error) { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "GetFormat") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand == nil { err = errors.New("error get image format:magickwand is nil") return "", err } format := C.MagickGetImageFormat(this.magickWand) if format == nil { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error get image format: %s (ExceptionType = %d)", C.GoString(descr), etype)) return "", err } return C.GoString(format), nil }
/* Strip() removes all profiles and text attributes from this image. */ func (this *Image) Strip() error { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "Strip") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand == nil { err = errors.New("error strip image:magickwand is nil") return err } status := C.MagickStripImage(this.magickWand) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error strip image: %s (ExceptionType = %d)", C.GoString(descr), etype)) return err } return nil }
/* Resize() resizes the size of this image to the given dimensions. width: width of the resized image height: height of the resized image */ func (this *Image) Resize(width int64, height int64) error { var err error = nil tran := this.Cat.NewTransaction("GraphicsMagickCmd", "Resize") defer func() { tran.SetStatus(err) tran.Complete() }() if this.magickWand == nil { err = errors.New("error resizing image:magickwand is nil") return err } status := C.MagickResizeImage(this.magickWand, C.ulong(width), C.ulong(height), C.CubicFilter, 0.5) if status == 0 { var etype int descr := C.MagickGetException(this.magickWand, (*C.ExceptionType)(unsafe.Pointer(&etype))) defer C.MagickRelinquishMemory(unsafe.Pointer(descr)) err = errors.New(fmt.Sprintf("error resizing image: %s (ExceptionType = %d)", C.GoString(descr), etype)) return err } return nil }
// Relinquishes memory resources returned by such methods as MagickIdentifyImage(), MagickGetException(), etc func (mw *MagickWand) relinquishMemory(ptr unsafe.Pointer) { C.MagickRelinquishMemory(ptr) }
// Implements direct to memory image formats. It returns the image as a blob func (self Canvas) Blob(length *uint) []byte { ptr := unsafe.Pointer(C.MagickGetImageBlob(self.wand, (*C.size_t)(unsafe.Pointer(length)))) data := C.GoBytes(ptr, C.int(*length)) C.MagickRelinquishMemory(ptr) return data }
// relinquishes memory resources returned by such methods as MagickIdentifyImage(), MagickGetException(), etc func relinquishMemory(ptr unsafe.Pointer) { if ptr != nil { C.MagickRelinquishMemory(ptr) } }