Re: [gtkmm] pixmap to pixbuf conversion

On Fri, Jan 17, 2003 at 07:53:29PM +0000, Vishal Modak wrote:

> i can load an image using pixbuf..on the drawing area..
> how can i get image drawn in the drawing area in to pixbuf..

better to work right with the pixbuf. what I do is load the image
from disk in a pixbuf:

  Glib::RefPtr<Gdk::Pixbuf> buf_current  =

Then display it using an appropriate widget, Gtk::Image in my case:

Gtk::Image img_current.set(buf_current);

Now, if I want to edit the buffer, e.g. apply some filters, I read
the buffer in an array using

guint8* get_pixels () const

to get a pointer to the actual pixel data. This data is in the
format 'first pixel r value, first pixel g value, first pixel
b value, second pixel r value, second pixel g value and so on'.
So you need an array width*height*3 to hold all the rgb values
or three width*height arrays to hold the three color 'planes'.
If you have an alpha channel, the format is rgbargba and the
array size is width*height*4.

Then fiddle with the array, and finally write it back to the buffer
with Gdk::Pixbuf::create_from_data(...).

The last part i didn't figure out completely. It works here like

newpixbuf = Gdk::Pixbuf::create_from_data(array,
            Gdk::COLORSPACE_RGB, false, 8, width, height, width*3);

with array being an array like above, false meaning there is no
alpha channel, and the last argument, rowstride, being width*3
or width*4 with alpha channel.

However, I am not sure how to manage memory. Does the array need
to stay around as long as the buffer has and do I delete it then?
That's what I am doing now.

Another possibility is to use the other create_from_data method,
which adds a const SlotDestroyData& destroy_slot at the and of
the argument list, which should handle the memory management for

Can someone give a small, working example on how to use this

Thanks, t.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]