Cairo.jl: is it possible to draw off screen and access the raw pixel array?

247 views
Skip to first unread message

Pierre-Yves Gérardy

unread,
Jan 3, 2014, 9:19:42 AM1/3/14
to juli...@googlegroups.com
Hello,

With Cairo.jl, is it possible to 1) draw off screen and 2) read the raw pixel buffer?

—Pierre-Yves

Andreas Lobinger

unread,
Jan 3, 2014, 10:08:13 AM1/3/14
to juli...@googlegroups.com
Hello colleague,

i have no julia example available, but in one of my python projects i use something like this:

            stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_ARGB32, 1200)
            map_data = numpy.zeros((1200,1200),numpy.int32)
            self.c_surface = cairo.ImageSurface.create_for_data(map_data,cairo.FORMAT_ARGB32,1200,1200,stride)
            self.c_map = map_data

with that i draw to the surface, then access the map as 2D (numpy) array.

Wishing a happy day,
    Andreas

Andreas Lobinger

unread,
Jan 3, 2014, 10:13:03 AM1/3/14
to juli...@googlegroups.com
... and it looks like this is already provided with (from Cairo/src/cairo.jl):

function CairoImageSurface(img::Array{Uint32,2}, format::Integer; flipxy::Bool = true)
    if flipxy
        img = img'
    end
    w,h = size(img)
    stride = format_stride_for_width(format, w)
    @assert stride == 4w
    ptr = ccall((:cairo_image_surface_create_for_data,_jl_libcairo),
                Ptr{Void}, (Ptr{Void},Int32,Int32,Int32,Int32),
                img, format, w, h, stride)
    CairoSurface(ptr, w, h, img)
end

so you can construct and Image surface by provding your 2D array (Uint32).

Pierre-Yves Gérardy

unread,
Jan 6, 2014, 7:16:12 PM1/6/14
to juli...@googlegroups.com
Thank you! 

CairoImageSurface(ary, fmt) copies the input array, but you can access the raw pixels (R/W) by using the .data attribute of the surface.

—Pierre-Yves
Reply all
Reply to author
Forward
0 new messages