Re: Accessing PixBuf pixels



It is almost correct except the fact that the rows of a pixbuf may be
padded. You are therefore supposed to use gdk_pixbuf_get_rowstride(pixbuf)
to get the distance between the rows.

Here is e.g. an example of vertically flip an image:

   guint8 *buf = gdk_pixbuf_get_pixels(img);
   gint w = gdk_pixbuf_get_width(img);
   gint h = gdk_pixbuf_get_height(img);
   gint rs = gdk_pixbuf_get_rowstride(img);
   gint row_idx, col_idx;

   for (row_idx=0; row_idx<h/2; row_idx++)
     {
       guint8 *ptr1 = buf+rs * row_idx;
       guint8 *ptr2 = buf+rs * (h-row_idx-1);

       for (col_idx=0; col_idx<w; col_idx++)
         {
           guint8 tmp_r = *ptr1;
           guint8 tmp_g = *(ptr1+1);
           guint8 tmp_b = *(ptr1+2);
           guint8 tmp_alpha = *(ptr1+3);
           *ptr1++ = *ptr2;
           *ptr1++ = *(ptr2+1);
           *ptr1++ = *(ptr2+2);
           *ptr1++ = *(ptr2+3);
           *ptr2++ = tmp_r;
           *ptr2++ = tmp_g;
           *ptr2++ = tmp_b;
           *ptr2++ = tmp_alpha;
         }
     }

Hope this helps.

Regads,

2008/12/18 Luka Napotnik <luka napotnik gmail com>

Hello. I have some difficulties with manipulation of pixels in the
GdkPixbuf data buffer. I use the following loop to iterate thought
each pixel:

-----------------------------------
n_channels = gdk_pixbuf_get_n_channels(pixbuf);
guchar *pixel;
guchar *data = gdk_pixbuf_get_pixels(pixbuf);

for (i = 0; i < width*height; i++) {
   pixel = buffer + i * n_channels;

   pixel[0] = 100; /* Red channel */
   pixel[1] = 100; /* Green channel */
   pixel[2] = 100; /* Blue channel */
   pixel[3] = 100; /* Alpha channel */
}
-----------------------------------

Is this the right way to handle a RGBA buffer?

Greets,
Luka
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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