GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 30, 2014 9:16 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Block-transfering PixBufs
PostPosted: Sun Jan 06, 2013 11:42 pm 
Offline
Familiar Face

Joined: Tue Jan 01, 2013 4:57 pm
Posts: 6
Is there a way to blit pixbufs onto Images or DrawingAreas? I'm using GTK+3 with VisualRuby.


Top
 Profile  
 
 Post subject: Re: Block-transfering PixBufs
PostPosted: Tue Jan 08, 2013 4:06 pm 
Offline
Familiar Face

Joined: Thu Aug 27, 2009 9:44 pm
Posts: 21
My answer is limited for the case that you want to blit a pixbuf onto a DrawingArea.

In the case you want to blit a pixbuf onto a drawingarea you are mainly concerned with cairo as the main drawing backend of gtk3.

The gdk3 api provides a function

Code:
void                gdk_cairo_set_source_pixbuf         (cairo_t *cr,
                                                         const GdkPixbuf *pixbuf,
                                                         gdouble pixbuf_x,
                                                         gdouble pixbuf_y);


that makes a pixbuf the source for the next paint operation onto a cairo context. It seems that the ruby wrapper provides this function as a method of Cairo::Context [1]

Since you convert the pixbuf to the source of the following cairo-paint operation you can perform any operation that cairo provides onto the "pixbuf" before your call the above function (like scale, translate etc.)

[1] http://ruby-gnome2.sourceforge.jp/hiki. ... %3AContext


Top
 Profile  
 
 Post subject: Re: Block-transfering PixBufs
PostPosted: Tue Jan 15, 2013 10:02 pm 
Offline
Familiar Face

Joined: Tue Jan 01, 2013 4:57 pm
Posts: 6
Ok great, it seems to be working. The relevant code in Ruby comes up as this:

Code:
da = @builder["frame_view"]
    # Get the dimensions of the frame
    dims = da.allocation
    d_width, d_height = dims.width, dims.height
    # Create the context
    c = da.window.create_cairo_context
    # Clear the pixbuf to a neutral colour
    c.set_operator(Cairo::Operator::SOURCE)
    c.set_source_rgb( RGB_NEUTRAL )
    c.paint
    # Find the center point
    center_x = (d_width / 2).round
    center_y = (d_height / 2).round
    # Paint the cell
    c.set_operator(Cairo::Operator::OVER)
    c.set_source_pixbuf(@cell.cell, center_x, center_y)
    c.paint


where @cell.cell returns a rectangular partition of the source pixbuf, like this:

Code:
Gdk::Pixbuf.new(source, rect.x, rect.y, rect.w, rect.h)


My last concern is, is there a way to apply just a partition of the source pixbuf to the cairo context, without having to create a new Pixbuf object?


Top
 Profile  
 
 Post subject: Re: Block-transfering PixBufs
PostPosted: Wed Jan 16, 2013 8:25 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Cairo is actually very flexible. It maps arbitrary sources to destinations via masks. A destination can be recycled again as a source for further work (see the layering description at http://www.cairographics.org/tutorial/#L2nouns). E.g. you can load up your pixbuf as source, draw a path as a mask and then do a clip in order to use just a portion of your original. Incidentally, loading a pixbuf is as simple as e.g. calling cairo_image_surface_create_from_png(). No need to get your hands dirty with gdk (a lot of functions got depreciated with gtk2->3) and you might not even need pixbuf for that matter either. You can then render to a variety of forms (DrawingArea and other widgets; svg/png files, etc.)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group