How to display an image from data in gtkmm book.



Hi,
I have been teaching myself gtkmm for displaying medical images. 

I would like to see the Gtkmm Book contain information on drawing to a drawable from an RGB data buffer.

For instance the pygtk documentation has a faq on this, 
(called: How do I to display an image from data (using a drawable or Image widget)?)
and an older version of the gtkmm book  I found on the web
talked about how to use the C api functions gdk_draw_rgb_image(), to do this
and mentions having to use gdk_rgb_init().
Also an old mailing list discussion by Murray Cumming mentioned having to use the C API 

" You need to use gobj() to get the C instance. For instance,

gdk_draw_rgb_image(get_window()->gobj(), 

But of course you should check get_window() for null and you probably need
to cast the GdkWindow* to a GdkDrawable*.

Murray Cumming"

However, when I look now at the API documentation we seem to currently have the 
member functions 
draw_rgb_image and draw_rgb_32_image 
actually in the class Gdk::Drawable, and no need to go to the C API.

It would be very nice to have this in the book...


Thus can we simply add back in  the following text (modified shamelessly from the older version:


You can also draw directly from a rgb buffer, ie a buffer which is an array of characters. Every three elements in the array define a color, the first element being the red component, the second green and the last blue. 3*width*height defines the number of elements needed for an rgb buffer of a given size. The rgb buffer is useful where pixel manipulation is needed.

First onstruct your array of data. To draw it into a drawing area use draw_rgb_image(), a method of Gdk::Drawable. 
The first argument is the GdkGC. A suitable GdkGC can be obtained from a call to get_style()->get_fg_gc(GTK_STATE_NORMAL). The next two arguments are the x and y points within the drawing area at which to draw the rgb buffer, and then come the width and height of the area to draw.

The next argument is a GdkRgbDither specifying the amount of dither to use. Dithering is an effect which causes colors close to each other to be blended by intermixing pixels from each color along the border of the two colors. If you've already calculated dither into your rgb buffer, then you will specify GDK_RGB_DITHER_NONE. Otherwise you can specify GDK_RGB_DITHER_NORMAL for dithering an image at 8 bits per pixel or lower, or GDK_RGB_DITHER_MAX for dithering an image at 16 bits per pixel or lower.

The next argument is the character array holding the rgb color information. The final argument is called rowstride. It is the number of elements in the array that compose one horizontal line of the array. From this number it is possible to draw a subset of the rgb buffer into the drawing area, knowing how wide the buffer is.

Here is a small bit of code to tie it all together: 


 class rgb_draw_area: public Gtk::DrawingArea
 {
      guchar *rgbbuff;
      public:
          rgb_draw_area(int x_size = 0, int y_size = 0);
          virtual bool on_expose_event(GdkEventExpose *event);
          guchar *get_buffer() const;
 };
 
 virtual bool rgb_draw_area::on_expose_event(GdkEventExpose *event)
 {
Glib::RefPtr<Gdk::Drawable> drawable=get_window();
      drawable->draw_rgb_image(drawable->get_style()->get_fg_gc(GTK_STATE_NORMAL), 
			0,0,
                         400,
                         200,
                         GDK_RGB_DITHER_MAX,
                         rgbbuff,
                         (this->get_width())*3);
 }
 

Thanks

Mitchell Laks



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