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
Hi Johann,
Thanks, I applied your patch. It is now committed to the repository.
I think that the change to keywords you suggested is very sensible, and I will do it as soon as I have the time. I will be travelling until Tuesday though, so that is the earliest I can do it. In the meantime, if you feel you would like to do it and submit a patch, that would be very welcome.
Best,
Tamas
On Wed, May 28, 2008 at 01:58:55AM +0200, Johann Korndoerfer wrote:
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))
cairo-enum))(error "Could not find ~a in ~a." enum table))
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
cl-cairo2-devel mailing list cl-cairo2-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/cl-cairo2-devel
hi
Tamas K Papp wrote:
I think that the change to keywords you suggested is very sensible [...] if you feel you would like to do it and submit a patch
After poking around i found the table mechanism quite unnecessary because the cenums could just be renamed to something prettier. I did that (completely emptying the tables.lisp) and also changed the naming scheme of the cfuns from cairo_foo_bar to cairo-foo-bar to make them more pleasing to the eye (mine at least), so now the only file containing underscores is cl-cairo2-swig.lisp. changing this file means that it will no longer be possible to generate cl-cairo2-swig.lisp with swig, leading to the obvious disadvantage that catching cairo api changes will not be as easy as before. i still think it is worth it since the code has become much more simple and readable in my opinion and we can always easily extend the file when cairo's api is extended, which should not be that often. this is of course very debatable :)
This patch also constitutes an api change: instead of
(cl-cairo2:create-image-surface 'cl-cairo2:format-argb32 23 42))
the user will have to write
(cairo:create-image-surface :argb32 23 42))
and similarly for font weight etc. The examples and the tutorial have been updated to reflect this change. I also added :cairo as a nickname for the package and fixed some minor bugs i stumbled upon.
Since i will be needing patterns, i have started a pattern.lisp which i will submit as soon as it is complete.
greetings johann
cl-cairo2-devel@common-lisp.net