[Gtk-sharp-list] Flipping and inverting a Pixbuf - My Solution

Anset Anset@anset.org
Wed, 19 Jan 2005 17:48:12 +0100


This is a multi-part message in MIME format.
--------------090209070206080706050200
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Well,

Even though I cannot say that this list has been very helpful for me, I 
thought I would post my solution anyway.

The code I found on that website (cybernetics.freewebspace.com/gtk) 
turned out to have a couple of bugs.

The attached document contains the code that works.
Also, using Pixdata, I was able to do it all in safe code.

Anset




--------------090209070206080706050200
Content-Type: text/plain;
 name="pixbuf_transforms.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="pixbuf_transforms.txt"

public class Gfx_Util
{

	static private void swap (ref byte one, ref byte two) {
		byte tmp;
		
		tmp = one;
		one = two;
		two = tmp;
	}

	static public Pixbuf Rotate180(Pixbuf image) {
			image = Flip(image);
		image = Invert(image);
		
		return image;			
	}

	static public Pixbuf  Flip(Pixbuf image) {
	
		// Pixdata struct has a 24 byte header before the actual pixel data
		//
		// http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3APixdata#HEADER_LENGTH
		//
		// HEADER_LENGTH = (4 + 4 + 4 + 4 + 4 + 4). 
		// The length of a Gdk::Pixdata structure without the pixel_data pointer.
		
		int pixdata_header = 24;
		
		int height, width, channels;
		int i=0, j=0, n=0;
		byte[] pixel;
		Pixdata pd = new Pixdata();
		
		height   = image.Height;
		width    = image.Width;
		channels = image.NChannels;
		pd.FromPixbuf(image, false);	
		pixel    = pd.Serialize();		
			for (i=0;i<height;i++) {
			for (j=0;j<(width/2)*channels;j+=channels) {
				for (n=0;n<channels;n++) {
					swap(ref pixel[pixdata_header+(i*width*channels+j+n)], 
    						 ref pixel[pixdata_header+(i*width*channels+((width-1)*channels-j)+n)]);
				}					
			}
		}
		
		pd.Deserialize((uint)pixel.Length, pixel);
		return Pixbuf.FromPixdata(pd, true);
	}
	
	static public Pixbuf Invert(Pixbuf image) {
	
		// Pixdata struct has a 24 byte header before the actual pixel data
		//
		// http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gdk%3A%3APixdata#HEADER_LENGTH
		//
		// HEADER_LENGTH = (4 + 4 + 4 + 4 + 4 + 4). 
		// The length of a Gdk::Pixdata structure without the pixel_data pointer.
		
		int pixdata_header = 24;
		
		int height, width, channels, rowstride;
		int i=0, j=0, n=0;
		byte[] pixel;
		Pixdata pd = new Pixdata();
		
		height   = image.Height;
		width    = image.Width;
		rowstride= image.Rowstride;
		channels = image.NChannels;
		pd.FromPixbuf(image, false);	
		pixel    = pd.Serialize();		
			for (i=0;i<height/2;i++) {
			for (j=0;j<rowstride;j+=channels) {
				for (n=0;n<channels;n++) {
					swap(ref pixel[pixdata_header+(i*rowstride+j+n)],
					     ref pixel[pixdata_header+(((height-1)-i)*rowstride+j+n)]);
				}					
			}
		}		
		
		pd.Deserialize((uint)pixel.Length, pixel);
		return Pixbuf.FromPixdata(pd, true);
	}

--------------090209070206080706050200--