GdkPixbuf Alpha Channel and Double Buffering

From: Date: Thu, 05 Apr 2007 16:20:52 +0000
Subject: GdkPixbuf Alpha Channel and Double Buffering
Groups: php.gtk.general 
Request: Send a blank email to php-gtk-general+get-7779@lists.php.net to get a copy of this message
Hi, The signature for GdkPixbuf::add_alpha is wrong, or more likely the implementation is wrong leading to the wrong signature. The manual doc shows the signature: GdkPixbuf <http://gtk.php.net/manual/en/gdk.gdkpixbuf.php> add_alpha(bool /substitute_color/, char /r/, char /g/, char /b/); This is consistent with the Gdk API. However, when I use this method it complains that it requires exactly three arguments. I was still able to add alpha with it but I think the substitute_color bool is missing from the php-gtk hook for this method. Second, is there a way to force double-buffering for a GtkDrawingArea? I want to do very basic image manipulation within the context of an app I'm writing including cropping. To do this I track clicking and dragging and draw a transparent rectangle to indicate the selection. This works quickly enough but it constantly flickers as redraws happen. My interface is created with Glade. I have several images loaded in memory as GdkPixbuf objects and my display area is a GtkDrawingArea. When an image is selected I do the following: list($pixmap,$mask) = $image->render_pixmap_and_mask(); $photo_area = $this->Glade->get_widget("drwSelectedPhoto"); $photo_area->window->set_back_pixmap($pixmap,false); $photo_area->window->clear(); My GtkDrawingArea is the size of the image (they're all resized to a standard size) so there is no tiling and I enable events and connect signal handlers. I'm not including that code here but just thought I'd mention it so there is no ambiguity. I handle signals for button-press-event, button-release-event and motion-notify -event when track the dimensions of the drag rectangle then call a function to draw it. Once coordinates are calculated the rectangle is drawn like this: //create the transparent rectangle as a pixel buffer $blue_rectangle = new GdkPixbuf(Gdk::COLORSPACE_RGB,true,8,$width,$height); $blue_rectangle->fill(0,0,255,40); //now draw it onto our image $drawing_area = $this->Glade->get_widget("drwSelectedPhoto"); $drawing_area->window->clear(); $drawing_area->window->begin_paint_rect(new
GdkRectangle($LeftX,$TopY,$width,$height));        $drawing_area->window->draw_pixbuf($drawing_area->style->fg_gc[0],$blue_rectangle,0,0,$LeftX,$TopY);
$drawing_area->window->end_paint(); The speed of this method is fine in that it can keep a "resizing" rectangle moving along with the mouse without dragging but it flickers during the motion-notify-event triggered draws. The way I read the API, using begin_paint_rect and end_paint should cause my draw operations to be double-buffered, but the flicker I see is classic single-buffer behavior. The php-gtk manual hasn't documented these two methods yet so I supplemented from the PyGTK documentation. Here's what it says about the begin_paint_rect method: "The |begin_paint_rect|() method indicates that you are beginning the process of redrawing the area specified by /|rectangle|/. A backing store (off-screen buffer) large enough to contain /|rectangle|/ will be created. The backing store will be initialized with the background color or background pixmap for window. Then, all drawing operations performed on the window will be diverted to the backing store. When you call the |end_paint|() <http://www.pygtk.org/docs/pygtk/class-gdkwindow.html#method-gdkwindow--end-paint> method, the backing store will be copied to the window, making it visible on-screen. Only the part of window contained in region will be modified; that is, drawing operations are clipped to /|rectangle|/. The net result of all this is to remove flicker, because the user sees the finished product appear all at once when you call the |end_paint|() <http://www.pygtk.org/docs/pygtk/class-gdkwindow.html#method-gdkwindow--end-paint> method. If you draw to window directly without calling the |begin_paint_rect|() method, the user may see flicker as individual drawing operations are performed in sequence. The clipping and background initializing features of the |begin_paint_rect|() are conveniences for the programmer, so you can avoid doing that work yourself." Finally (thanks for sticking with me if you're reading this part), is it even necessary for me to handle my own drag-selection-rectangle in this manner? I notice some widgets have the capability I want already, such as the GtkIconView when in multiple selection mode. Is there some easier way of getting an image you can drag-select an area from by say extending an existing class or implementing an interface? I'm a new comer to the list so I apologize if the format of my post or the number of items I crammed into it doesn't fit the modus operandi. To the developers, keep up the great work. This project opens up a lot of exciting possibilities for PHP applications. -Jake

« previous php.gtk.general (#7779) next »