Re: Crash when calling Gdk::Drawable::draw_pixbuf



Am Samstag, den 20.06.2009, 11:48 +0200 schrieb Falk Schilling:
[...]
> 	    {
> 	      m_refGlade->get_widget_derived(
> 					     glade_widget_names[m][n],
> 					     m_imgcol.image(m,n)
> 					     );
> 	      m_imgcol.image(m,n)->load(EMPTY_IMAGE.c_str());
> 	      m_imgcol.pixmap(m,n) =
> 		Gdk::Pixmap::create(m_imgcol.image(m,n)->get_window(),
> 				    m_dim.get_width(),
> 				    m_dim.get_height(),
> 				    m_imgcol.image(m,n)->get_window()
> 				    ->get_depth());
[...]
> The problem is, that now Gdk::Pixmap::create causes the segfault.

Most likely because the Gtk::Image widget isn't realized yet at this
point, which means its Gdk::Window doesn't exist yet.

> I
> know, that this is a very bad solution, because I use the complete
> (derived) Gtk:Image as Drawable,

I don't see where you do that. It wouldn't work anyway. (Gtk::Image is
*not* a Gdk::Drawable).

> although I only want to modify its
> Pixbuf, but that was just a try whether it works or not.

Gtk::Image is a widget that can display some image. It does not by
itself maintain the image data. Modifying its Gdk::Pixbuf means just
assigning a new one.

> > Well, that explains a few things, e.g. some missing constructor methods
> > for Gdk::Drawable (compared with other classes). Since this class method
> > returns a Glib::RefPtr<Gdk::Drawable> I assumed that this is the way of
> > creating objects. The documentation unfortunately lacks some information
> > at this point, what this is used for.

Well, I had a look and it's indeed there.  There cannot possibly be any
documentation because it is useless and doesn't work.  It seems to have
been added by accident.  Congratulations, you are the first person to
notice this, even though it must have been around for about a
decade. :-)

> >> Also, are you sure Gdk::Image is really what you want to use?  This
> >> stuff all maps directly to X11 primitives...
> > 
> > Well, I don’t want to use Cairo for just drawing simple pixels. I indeed
> > don’t need stuff like arcs, lines or whatever else; I want to read
> > pixels out of an image, process the values and write the result into
> > another image. Purely and simple pixel operations.

You've got it completely the wrong way around.  Cairo does simple
client-side drawing.  Using Gdk::Drawable means you are communicating
with the X server.  Gdk::Image is client-side, but still comes with all
the Xlib complexity like visuals and color maps and what not.

If you really only want to work with pixels, you can just access
Gdk::Pixbuf directly. From the top of my head:

    // Assume we have an RGB pixbuf with 8 bits per component
    g_assert(pixbuf->get_colorspace() == Gdk::COLORSPACE_RGB);
    g_assert(pixbuf->get_bits_per_sample() == 8);

    guint8 *const pixel = pixbuf->get_pixels()
                        + row * pixbuf->get_rowstride()
                        + col * pixbuf->get_n_channels();
    pixel[0] = red;
    pixel[1] = green;
    pixel[2] = blue;

I think there was a libart or something to encapsulate this, but it's
rarely used these days.  From my point of view, either you are drawing
at a high level and use Cairo, or you want to deal with raw pixel data
in which case setting single pixels in isolation would be unacceptably
slow, so you would work on the buffer as a whole.

> > Ok, you suggest to take Gdk::Pixmap for this stuff. Well, how do I
> > convert the Gdk::Pixbuf into a Gdk::Pixmap?

No, I didn't suggest to use Gdk::Pixmap.  Well, maybe I suggested it,
but I certainly don't recommend it. :-)  What I meant was that
Gdk::Pixmap would be a Gdk::Drawable which you can instantiate.  In
other words, it would work, but still be a bad idea for what you are
doing.

Generally, I think you need to step back for a moment and think things
over.  Your current attempt makes it look way more complicated than it
actually is.

--Daniel




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