Re: Problem writing pixels to GdkPixbuf data buffer



2008/7/15 Luka Napotnik <luka napotnik gmail com>:
Hello.

I encounter a strange problem when I'm writing image data to the
GdkPixbuf data buffer.

I create an empty pixbuf and get the data buffer. Then I use:

pixel = pixbuf_data + i * n_channels

, where pixel is a pointer to the pixel, pixbuf_data is the pixbuf data
buffer, i is a counter in a for loop with a condition:

for (i = 0; i < width*height; i++)

This for loop should iterate throught all pixel in the image. The
n_channels variable is 4 because I use a RGBA pixbuf.
When I have this pixel pointer, I use pixel[0], pixel[1], pixel[2] and
pixel[3] to modify the RGBA values.

I then handle the "expose-event", create a cairo surface using the
GdkPixbuf and display the surface onto the drawing area. The problem is
when I display the pixbuf the image is shifted to the right for exacly
two pixels. Meaning the first two pixels in the upper left are two
places to the right, making the last two pixels of the row appear in the
second row. I hope you understand.

Am I making something wrong when writing data to the pixbuf? Or is there
any other explanation. Please help.

Greets,
Luka


Luka,
You should change this to something like this:
pixel = gdk_pixbuf_get_pixels(pixbuf);
rowstride = gdk_pixbuf_get_rowstride(pixbuf);
for(y=0;y<height;y++) {
  for(x=0;x<width;x++) {
    /* do something with 'pixel' */
    pixel += n_channels;
  }
  pixel += (rowstride - n_channels*width);
}

Rowstride can be something other than n_channels*width, which could
cause problems similar to what you're describing.
-Jim



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