Beispiel #1
0
func Resize(fpath, tpath string, typ, cols, rows int) error {
	var mw *C.MagickWand = C.NewMagickWand()
	var cfpath = C.CString(fpath)
	var ctpath = C.CString(tpath)
	var etype C.ExceptionType
	defer func() {
		C.free(unsafe.Pointer(cfpath))
		C.free(unsafe.Pointer(ctpath))
	}()
	defer func() {
		C.ClearMagickWand(mw)
		C.DestroyMagickWand(mw)
	}()
	if C.MagickReadImage(mw, cfpath) == C.MagickFalse {
		goto ERR
	}

	if C.scale(mw, C.int(typ), C.int(cols), C.int(rows)) == C.MagickFalse {
		goto ERR
	}
	if C.MagickWriteImages(mw, ctpath, C.MagickTrue) == C.MagickFalse {
		goto ERR
	}
	return nil
ERR:
	etype = C.MagickGetExceptionType(mw)
	return errors.New(C.GoString(C.MagickGetException(mw, &etype)))
}
Beispiel #2
0
/*
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
}
Beispiel #3
0
/*
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
}
Beispiel #4
0
/*
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
}
Beispiel #5
0
/* Returns the severity, reason, and description of any error that occurs when
   using other methods in this API.
*/
func (w *MagickWand) Exception() (string, int) {
	var severity C.ExceptionType
	errPtr := C.MagickGetException(w.wand, &severity)
	C.MagickClearException(w.wand)
	err := C.GoString(errPtr)
	C.MagickRelinquishMemory(unsafe.Pointer(errPtr))
	return err, int(severity)
}
Beispiel #6
0
// 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)
}
// Returns the kind, reason and description of any error that occurs when using other methods in this API
func (mw *MagickWand) GetLastError() error {
	var et C.ExceptionType
	csdescription := C.MagickGetException(mw.mw, &et)
	defer mw.relinquishMemory(unsafe.Pointer(csdescription))
	if ExceptionType(et) != EXCEPTION_UNDEFINED {
		mw.clearException()
		return &MagickWandException{ExceptionType(C.int(et)), C.GoString(csdescription)}
	}
	return nil
}
Beispiel #8
0
func (self *PixelIterator) Error() error {
	var exception C.ExceptionType

	ptr := C.MagickGetException(self.iterator, &exception)
	message := C.GoString(ptr)

	C.MagickClearException(self.iterator)
	C.MagickRelinquishMemory(unsafe.Pointer(ptr))

	return errors.New(message)
}
Beispiel #9
0
/*
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
}
Beispiel #10
0
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
}
Beispiel #11
0
/*
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
}
Beispiel #12
0
/*
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
}
Beispiel #13
0
/*
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
}
Beispiel #14
0
/*
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
}
Beispiel #15
0
/*
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
}
Beispiel #16
0
/*
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
}
Beispiel #17
0
/*
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
}
Beispiel #18
0
/*
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
}
Beispiel #19
0
/*
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
}
Beispiel #20
0
/*
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
}
Beispiel #21
0
// Returns the latest error reported by the MagickWand API.
func (self Canvas) Error() error {
	var t C.ExceptionType
	message := C.MagickGetException(self.wand, &t)
	C.MagickClearException(self.wand)
	return fmt.Errorf(C.GoString(message))
}