|
@@ -25,7 +25,8 @@
|
|
|
#:use-module (srfi srfi-9)
|
|
|
#:use-module ((system foreign) #:prefix ffi:)
|
|
|
#:export (magick-alpha-channel-options
|
|
|
- magick-convert-image))
|
|
|
+ magick-convert-image
|
|
|
+ magick-create-image))
|
|
|
|
|
|
(define-foreign-library libMagickWand %libmagickwand)
|
|
|
|
|
@@ -129,70 +130,115 @@
|
|
|
(define-foreign-return-type wand-error int
|
|
|
(lambda (result wand . _)
|
|
|
(when (zero? result)
|
|
|
- (apply error (MagickGetException wand)))))
|
|
|
+ (apply error (MagickGetException* wand)))))
|
|
|
+
|
|
|
+(define-foreign-pointer-type pixel-wand-ptr <pixel-wand>
|
|
|
+ pixel-wand? pointer->pixel-wand pixel-wand->pointer)
|
|
|
+
|
|
|
+(define-foreign-return-type pixel-wand-error int
|
|
|
+ (lambda (result pixel-wand . _)
|
|
|
+ (when (zero? result)
|
|
|
+ (apply error (PixelGetException* pixel-wand)))))
|
|
|
|
|
|
(define-foreign-functions libMagickWand
|
|
|
(MagickWandGenesis -> void)
|
|
|
(MagickWandTerminus -> void)
|
|
|
(NewMagickWand -> wand-ptr)
|
|
|
(DestroyMagickWand wand-ptr -> wand-ptr)
|
|
|
- (MagickGetException wand-ptr pointer -> cstring)
|
|
|
+ (MagickRelinquishMemory pointer -> pointer)
|
|
|
+ (MagickGetException wand-ptr pointer -> pointer)
|
|
|
(MagickReadImage wand-ptr cstring -> wand-error)
|
|
|
(MagickReadImageBlob wand-ptr pointer size_t -> wand-error)
|
|
|
+ (MagickNewImage wand-ptr size_t size_t pixel-wand-ptr -> wand-error)
|
|
|
(MagickWriteImage wand-ptr cstring -> bool)
|
|
|
(MagickGetImageBlob wand-ptr pointer -> pointer)
|
|
|
(MagickGetImageFormat wand-ptr -> cstring)
|
|
|
(MagickSetImageFormat wand-ptr cstring -> wand-error)
|
|
|
(MagickGetImageAlphaChannel wand-ptr -> alpha-channel-enum)
|
|
|
- (MagickSetImageAlphaChannel wand-ptr alpha-channel-enum -> wand-error))
|
|
|
-
|
|
|
-(set! MagickGetException
|
|
|
- (let ((func MagickGetException))
|
|
|
- (lambda (wand)
|
|
|
- (let* ((type-ptr (ffi:make-c-struct (list ffi:int) (list 0)))
|
|
|
- (message (func wand type-ptr))
|
|
|
- (type-int (car (ffi:parse-c-struct type-ptr (list ffi:int))))
|
|
|
- (type (int->exception-type type-int)))
|
|
|
- (list type message)))))
|
|
|
-
|
|
|
-(set! MagickReadImageBlob
|
|
|
- (let ((func MagickReadImageBlob))
|
|
|
- (lambda (wand bv)
|
|
|
- (let ((ptr (ffi:bytevector->pointer bv))
|
|
|
- (len (bytevector-length bv)))
|
|
|
- (func wand ptr len)))))
|
|
|
-
|
|
|
-(set! MagickGetImageBlob
|
|
|
- (let ((func MagickGetImageBlob))
|
|
|
- (lambda (wand)
|
|
|
- (let* ((len-ptr (ffi:make-c-struct (list ffi:size_t) (list 0)))
|
|
|
- (data-ptr (func wand len-ptr))
|
|
|
- (data-len (car (ffi:parse-c-struct len-ptr (list ffi:size_t)))))
|
|
|
- (ffi:pointer->bytevector data-ptr data-len)))))
|
|
|
+ (MagickSetImageAlphaChannel wand-ptr alpha-channel-enum -> wand-error)
|
|
|
+ (NewPixelWand -> pixel-wand-ptr)
|
|
|
+ (DestroyPixelWand pixel-wand-ptr -> pixel-wand-ptr)
|
|
|
+ (PixelGetException pixel-wand-ptr pointer -> pointer)
|
|
|
+ (PixelSetColor pixel-wand-ptr cstring -> pixel-wand-error)
|
|
|
+ (PixelSetAlpha pixel-wand-ptr double -> void))
|
|
|
+
|
|
|
+(define (MagickGetException* wand)
|
|
|
+ (let* ((type-ptr (ffi:make-c-struct (list ffi:int) (list 0)))
|
|
|
+ (message-ptr (MagickGetException wand type-ptr))
|
|
|
+ (message (ffi:pointer->string message-ptr))
|
|
|
+ (type-int (car (ffi:parse-c-struct type-ptr (list ffi:int))))
|
|
|
+ (type (int->exception-type type-int)))
|
|
|
+ (MagickRelinquishMemory message-ptr)
|
|
|
+ (list type message)))
|
|
|
+
|
|
|
+(define (MagickReadImageBlob* wand bv)
|
|
|
+ (let ((ptr (ffi:bytevector->pointer bv))
|
|
|
+ (len (bytevector-length bv)))
|
|
|
+ (MagickReadImageBlob wand ptr len)))
|
|
|
+
|
|
|
+(define (MagickGetImageBlob* wand)
|
|
|
+ (let* ((len-ptr (ffi:make-c-struct (list ffi:size_t) (list 0)))
|
|
|
+ (data-ptr (MagickGetImageBlob wand len-ptr))
|
|
|
+ (data-len (car (ffi:parse-c-struct len-ptr (list ffi:size_t)))))
|
|
|
+ (ffi:pointer->bytevector data-ptr data-len)))
|
|
|
+
|
|
|
+(define (PixelGetException* pixel-wand)
|
|
|
+ (let* ((type-ptr (ffi:make-c-struct (list ffi:int) (list 0)))
|
|
|
+ (message-ptr (PixelGetException pixel-wand type-ptr))
|
|
|
+ (message (ffi:pointer->string message-ptr))
|
|
|
+ (type-int (car (ffi:parse-c-struct type-ptr (list ffi:int))))
|
|
|
+ (type (int->exception-type type-int)))
|
|
|
+ (MagickRelinquishMemory message-ptr)
|
|
|
+ (list type message)))
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
-(define (call-with-magick-wand proc)
|
|
|
- (let ((wand #f))
|
|
|
+(define (call-with-pointer alloc free proc)
|
|
|
+ (let ((ptr #f))
|
|
|
(dynamic-wind
|
|
|
(const #t)
|
|
|
(lambda ()
|
|
|
- (MagickWandGenesis)
|
|
|
- (set! wand (NewMagickWand))
|
|
|
- (proc wand))
|
|
|
+ (set! ptr (alloc))
|
|
|
+ (proc ptr))
|
|
|
(lambda ()
|
|
|
- (when wand
|
|
|
- (DestroyMagickWand wand))))))
|
|
|
+ (when ptr
|
|
|
+ (free ptr))))))
|
|
|
+
|
|
|
+(define (call-with-magick-wand proc)
|
|
|
+ (call-with-pointer NewMagickWand DestroyMagickWand proc))
|
|
|
+
|
|
|
+(define (call-with-pixel-wand proc)
|
|
|
+ (call-with-pointer NewPixelWand DestroyPixelWand proc))
|
|
|
|
|
|
(define (magick-alpha-channel-options)
|
|
|
(alpha-channel-list))
|
|
|
|
|
|
-(define* (magick-convert-image image-bytes #:key
|
|
|
- format
|
|
|
- alpha-channel)
|
|
|
+(define* (magick-convert-image image-bytes #:key format alpha-channel)
|
|
|
(call-with-magick-wand
|
|
|
(lambda (wand)
|
|
|
- (MagickReadImageBlob wand image-bytes)
|
|
|
- (MagickSetImageFormat wand format)
|
|
|
- (MagickSetImageAlphaChannel wand alpha-channel)
|
|
|
- (MagickGetImageBlob wand))))
|
|
|
+ (MagickReadImageBlob* wand image-bytes)
|
|
|
+ (when format
|
|
|
+ (MagickSetImageFormat wand format))
|
|
|
+ (when alpha-channel
|
|
|
+ (MagickSetImageAlphaChannel wand alpha-channel))
|
|
|
+ (MagickGetImageBlob* wand))))
|
|
|
+
|
|
|
+(define* (magick-create-image #:key width height
|
|
|
+ (format "PNG")
|
|
|
+ (background-color "white")
|
|
|
+ (background-alpha 0.0)
|
|
|
+ alpha-channel)
|
|
|
+ (call-with-magick-wand
|
|
|
+ (lambda (wand)
|
|
|
+ (call-with-pixel-wand
|
|
|
+ (lambda (background)
|
|
|
+ (PixelSetColor background background-color)
|
|
|
+ (PixelSetAlpha background background-alpha)
|
|
|
+ (MagickNewImage wand width height background)))
|
|
|
+ (when format
|
|
|
+ (MagickSetImageFormat wand format))
|
|
|
+ (when alpha-channel
|
|
|
+ (MagickSetImageAlphaChannel wand alpha-channel))
|
|
|
+ (MagickGetImageBlob* wand))))
|
|
|
+
|
|
|
+(MagickWandGenesis)
|