hello
included is a diff for extending cl-cairo2 to support extracting pixel
data (as a 1d array) from image surfaces via image-surface-get-data. it
also fixes a minor glitch concerning error messages thrown when a symbol
was not found in a table.
i also noticed something like
(cl-cairo2:create-image-surface 'format-argb32 23 42))
will not work (complaining about not finding the symbol) since the
symbol cl-user:format-argb32 obviously is not the same as
cl:cairo2:format-argb32.
i therefore suggest using keyword symbols for the right halves of the
tables as well if there is no reason to do otherwise.
johann
Index: tables.lisp
===================================================================
--- tables.lisp (revision 19)
+++ tables.lisp (working copy)
@@ -112,5 +112,5 @@
(defun lookup-enum (enum table)
(let ((cairo-enum (car (rassoc enum table))))
(unless cairo-enum
- (error "Could not find ~a in ~a." cairo-enum table))
+ (error "Could not find ~a in ~a." enum table))
cairo-enum))
Index: surface.lisp
===================================================================
--- surface.lisp (revision 19)
+++ surface.lisp (working copy)
@@ -113,6 +113,25 @@
width height)
width height t))
+(defun get-bytes-per-pixel (format)
+ (case format
+ (format-argb32 4)
+ (format-rgb24 3)
+ (format-a8 1)
+ (otherwise (error (format nil "unknown format: ~a" format))))) ;todo: how does format-a1 fit in here?
+
+(defun image-surface-get-data (surface)
+ (with-surface (surface pointer)
+ (let* ((width (image-surface-get-width surface))
+ (height (image-surface-get-height surface))
+ (bytes-per-pixel (get-bytes-per-pixel (image-surface-get-format surface)))
+ (buffer (make-array (* width height bytes-per-pixel) :element-type '(unsigned-byte 8) :fill-pointer 0))
+ (data (cairo_image_surface_get_data pointer)))
+ (loop for i from 0 below (* width height bytes-per-pixel) do
+ (vector-push-extend (cffi:mem-ref data :uint8 i) buffer))
+ buffer)))
+
+
(defun image-surface-get-format (surface)
(with-surface (surface pointer)
(lookup-cairo-enum (cairo_image_surface_get_format pointer) table-format)))
Index: package.lisp
===================================================================
--- package.lisp (revision 19)
+++ package.lisp (working copy)
@@ -14,6 +14,7 @@
destroy create-ps-surface create-pdf-surface create-svg-surface
create-image-surface image-surface-get-format
image-surface-get-width image-surface-get-height
+ image-surface-get-data
image-surface-create-from-png surface-write-to-png
;; context