示例#1
0
文件: magick.go 项目: mat/magick
// NewFromFile loads a file at filename into a MagickImage.
// Exceptions are returned as MagickErrors.
func NewFromFile(filename string) (im *MagickImage, err error) {
	exception := C.AcquireExceptionInfo()
	defer C.DestroyExceptionInfo(exception)
	info := C.AcquireImageInfo()
	c_filename := C.CString(filename)
	defer C.free(unsafe.Pointer(c_filename))
	C.SetImageInfoFilename(info, c_filename)
	image := C.ReadImage(info, exception)
	if failed := C.CheckException(exception); failed == C.MagickTrue {
		C.DestroyImageInfo(info)
		return nil, ErrorFromExceptionInfo(exception)
	}
	return &MagickImage{Image: image, ImageInfo: info}, nil
}
示例#2
0
文件: magick.go 项目: mat/magick
// ToFile writes the (transformed) MagickImage to the regular file at filename. Magick determines
// the encoding of the output file by the extension given to the filename (e.g. "image.jpg", "image.png")
func (im *MagickImage) ToFile(filename string) (err error) {
	exception := C.AcquireExceptionInfo()
	defer C.DestroyExceptionInfo(exception)
	c_outpath := C.CString(filename)
	defer C.free(unsafe.Pointer(c_outpath))
	C.SetImageInfoFilename(im.ImageInfo, c_outpath)
	success := C.WriteImages(im.ImageInfo, im.Image, c_outpath, exception)
	if failed := C.CheckException(exception); failed == C.MagickTrue {
		return ErrorFromExceptionInfo(exception)
	}
	if success != C.MagickTrue {
		return &MagickError{"fatal", "", "could not write to " + filename + " for unknown reason"}
	}
	return nil
}
示例#3
0
文件: magick.go 项目: mat/magick
// ToBlob takes a (transformed) MagickImage and returns a byte slice in the format you specify with extension.
// Magick uses the extension to transform the image in to the proper encoding (e.g. "jpg", "png")
func (im *MagickImage) ToBlob(extension string) (blob []byte, err error) {
	exception := C.AcquireExceptionInfo()
	defer C.DestroyExceptionInfo(exception)
	c_outpath := C.CString("image." + extension)
	defer C.free(unsafe.Pointer(c_outpath))
	C.SetImageInfoFilename(im.ImageInfo, c_outpath)
	var outlength (C.size_t)
	outblob := C.ImageToBlob(im.ImageInfo, im.Image, &outlength, exception)
	if failed := C.CheckException(exception); failed == C.MagickTrue {
		return nil, ErrorFromExceptionInfo(exception)
	}
	char_pointer := unsafe.Pointer(outblob)
	defer C.free(char_pointer)
	return C.GoBytes(char_pointer, (C.int)(outlength)), nil
}
示例#4
0
文件: magick.go 项目: mat/magick
// NewFromBlob takes a byte slice of image data and an extension that defines the
// image type (e.g. "png", "jpg", etc). It loads the image data and returns a MagickImage.
// The extension is required so that Magick knows what processor to use.
func NewFromBlob(blob []byte, extension string) (im *MagickImage, err error) {
	if len(blob) < 1 {
		return nil, &MagickError{"fatal", "", "zero length blob passed to NewFromBlob"}
	}
	if len(extension) < 1 {
		return nil, &MagickError{"fatal", "", "zero length extension passed to NewFromBlob"}
	}
	exception := C.AcquireExceptionInfo()
	defer C.DestroyExceptionInfo(exception)
	info := C.AcquireImageInfo()
	defer C.DestroyImageInfo(info)
	c_filename := C.CString("image." + extension)
	defer C.free(unsafe.Pointer(c_filename))
	C.SetImageInfoFilename(info, c_filename)
	var success (C.MagickBooleanType)
	success = C.SetImageInfo(info, 1, exception)
	if success != C.MagickTrue {
		return nil, ErrorFromExceptionInfo(exception)
	}
	cloned_info := C.CloneImageInfo(info)
	success = C.GetBlobSupport(info)
	if success != C.MagickTrue {
		// No blob support, lets try reading from a file
		file, err := ioutil.TempFile("", "image."+extension)
		if _, err = file.Write(blob); err != nil {
			return nil, &MagickError{"fatal", "", "image format " + extension + " does not support blobs and could not write temp file"}
		}
		file.Close()
		return NewFromFile(file.Name())
	}
	length := (C.size_t)(len(blob))
	if length == 0 {
		return nil, &MagickError{"fatal", "", "empty blob"}
	}
	blob_start := unsafe.Pointer(&blob[0])
	image := C.ReadImageFromBlob(info, blob_start, length)

	if image == nil {
		return nil, &MagickError{"fatal", "", "corrupt image, not a " + extension}
	}

	if failed := C.CheckException(exception); failed == C.MagickTrue {
		return nil, ErrorFromExceptionInfo(exception)
	}

	return &MagickImage{Image: image, ImageInfo: cloned_info}, nil
}