From 606445f790e290fae775a00c4f5ccb257a713a0b Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 29 Mar 2008 15:31:31 -0500 Subject: [PATCH] improve png --- extra/cairo/ffi/ffi.factor | 3 +++ extra/cairo/png/png.factor | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/extra/cairo/ffi/ffi.factor b/extra/cairo/ffi/ffi.factor index 76ce27975b..c319ade93b 100644 --- a/extra/cairo/ffi/ffi.factor +++ b/extra/cairo/ffi/ffi.factor @@ -203,6 +203,9 @@ C-ENUM: CAIRO_HINT_METRICS_ON ; +FUNCTION: char* cairo_status_to_string ( cairo_status_t status ) ; +FUNCTION: cairo_status_t cairo_status ( cairo_t* cr ) ; + : cairo_create ( cairo_surface_t -- cairo_t ) "cairo_t*" "cairo" "cairo_create" [ "void*" ] alien-invoke ; diff --git a/extra/cairo/png/png.factor b/extra/cairo/png/png.factor index eaab28e659..774a1afe8e 100755 --- a/extra/cairo/png/png.factor +++ b/extra/cairo/png/png.factor @@ -1,16 +1,34 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: arrays kernel accessors math ui.gadgets ui.render -opengl.gl byte-arrays namespaces opengl cairo.ffi cairo.lib ; +opengl.gl byte-arrays namespaces opengl cairo.ffi cairo.lib +inspector sequences combinators io.backend ; IN: cairo.png TUPLE: png surface width height cairo-t array ; TUPLE: png-gadget png ; +ERROR: cairo-error string ; + +: check-zero + dup zero? [ + "PNG dimension is 0" cairo-error + ] when ; + +: cairo-png-error ( n -- ) + { + { [ dup CAIRO_STATUS_NO_MEMORY = ] [ "Cairo: no memory" cairo-error ] } + { [ dup CAIRO_STATUS_FILE_NOT_FOUND = ] [ "Cairo: file not found" cairo-error ] } + { [ dup CAIRO_STATUS_READ_ERROR = ] [ "Cairo: read error" cairo-error ] } + { [ t ] [ drop ] } + } cond ; + : ( path -- png ) + normalize-pathname cairo_image_surface_create_from_png - dup [ cairo_image_surface_get_width ] - [ cairo_image_surface_get_height ] [ ] tri + dup cairo_surface_status cairo-png-error + dup [ cairo_image_surface_get_width check-zero ] + [ cairo_image_surface_get_height check-zero ] [ ] tri cairo-surface>array png construct-boa ; : write-png ( png path -- ) @@ -33,6 +51,7 @@ M: png-gadget draw-gadget* ( gadget -- ) png>> [ width>> ] [ height>> GL_RGBA GL_UNSIGNED_BYTE ] + ! [ height>> GL_BGRA GL_UNSIGNED_BYTE ] [ array>> ] tri glDrawPixels ] with-translation ; @@ -42,3 +61,5 @@ M: png-gadget graft* ( gadget -- ) M: png-gadget ungraft* ( gadget -- ) png>> surface>> cairo_destroy ; + +! "resource:misc/icons/Factor_1x16.png" USE: cairo.png gadget.