[Gtk-sharp-list] Adding Widgets

Anset Anset@anset.org
Tue, 18 Jan 2005 21:23:03 +0100


Hi,

Since I'm still not able to flip a simple pixbuf, I'm turning again to 
this list in the hopes that someone would be kind enough to point me in 
the right direction.

I want to rotate a pixbu 180 degrees. I found a pdf about GTK image 
manipulation at http://cybernetics.freewebspace.com/gtk and I am trying 
to convert that c code to C#.

As a first step I try to flip the pixbuf. And it works. However, after 
my program flips a variable number of pixbufs, sometimes after two 
flips, sometimes it takes 10, I get unhandled exceptions (usually 
nullreferences). These occur in a different place every time, somewhere 
in the gtk code.

Since this is the only code I have added, It must be from here.
I am pretty conviced that the"unsafe" code I had to add is the cause of 
it, but I cannot find a way to get to the pixbuf's pixel data in a 
"safe" way. (If anyone could tell me about a safe way to do this?)

Anyway, below is my code.

Any and all hints are welcome.
(I can't believe it is so hard to do this...)

Anset

using System;
using Gdk;

public class Gfx_Util
{
	static private void swap (ref byte one, ref byte two) {
         	byte tmp;

                 tmp = one;
                 one = two;
                 two = tmp;
	}

	unsafe static public void  Flip(ref Pixbuf image) {

		int height, width, channels;
		int i=0, j=0;
		byte *pixel;

		height   = image.Height;
		width    = image.Width;
		channels = image.NChannels;
		pixel    = (byte *)image.Pixels;
	
		for (i=0;i<height;i++) {
			for (j=0;j<(width/2)*channels;j+=channels) {
				swap(ref pixel[i*width*channels+j+0], ref 
pixel[i*width*channels+(width*channels-j)+0]);
                                 swap(ref pixel[i*width*channels+j+1], 
ref pixel[i*width*channels+(width*channels-j)+1]);
                                 swap(ref pixel[i*width*channels+j+2], 
ref pixel[i*width*channels+(width*channels-j)+2]);

                         }
                 }
         }
}