[f-spot: 13/40] Continued cleaning of PixbufUtils



commit be4234632854904b8ac83c9948dc2988ac5645e7
Author: Stephane Delcroix <stephane delcroix org>
Date:   Sun Jun 21 14:16:54 2009 +0200

    Continued cleaning of PixbufUtils
    
    * PixbufUtils.cs, Utils.PixbufUtils.cs: drop an unused overload of TransformOrientation, move the remaining one in Utils.PixbufUtils and rewrite it using pixbuf methods like Roatate and Flip instead of using the native f_pixbuf_copy_with_orientation.
    
    libfspot/f-pixbuf-utils.cs: drop f_pixbuf_copy_with_orientation and other unused methods

 libfspot/f-pixbuf-utils.c |  179 ---------------------------------------------
 src/Imaging/ImageFile.cs  |    2 +-
 src/Imaging/JpegFile.cs   |    2 +-
 src/Imaging/RafFile.cs    |    2 +-
 src/PixbufUtils.cs        |  155 +++++++++++++++++++--------------------
 src/RotateCommand.cs      |    2 +-
 src/Utils/PixbufUtils.cs  |   47 +++++++++++-
 7 files changed, 123 insertions(+), 266 deletions(-)
---
diff --git a/libfspot/f-pixbuf-utils.c b/libfspot/f-pixbuf-utils.c
index c8cf632..d87e01f 100644
--- a/libfspot/f-pixbuf-utils.c
+++ b/libfspot/f-pixbuf-utils.c
@@ -348,46 +348,6 @@ f_pixbuf_from_cairo_surface (cairo_surface_t *source)
   return pixbuf;
 }
 
-
-/**
- *  This alorithm is based on the redeye algorithm in flphoto 
- *  Copyright 2002-2003 by Michael Sweet
- *
- *  FIXME this is a very simplist algorithm, something more intelligent needs to be used.
- *
- *  Note: this is no longer used. A better implementation was written in C# in PixbufUtils.cs
- */
-
-//void
-//f_pixbuf_remove_redeye (GdkPixbuf *src)
-//{
-//	int width = gdk_pixbuf_get_width (src);
-//	int height = gdk_pixbuf_get_height (src);
-//	int i, j;
-//
-//	int r, g, b;
-//	int channels = gdk_pixbuf_get_n_channels (src);
-//
-//	guchar *row = gdk_pixbuf_get_pixels (src);
-//
-//	for (i = 0; i < height; i++) {
-//		guchar *col = row;
-//
-//		for (j = 0; j < width; j++) {
-//			r = *col;
-//			g = *(col + 1);
-//			b = *(col + 2);
-//			
-//			if ((r > (3 * g / 2) && r > (3 * b / 2)) || (g > r && b > r)) {
-//				memset(col, (r * 31 + g * 61 + b * 8) / 100, 3);
-//			}
-//			
-//			col += channels;
-//		}
-//		row += gdk_pixbuf_get_rowstride (src);
-//	}
-//}
-
 gboolean
 f_pixbuf_save_jpeg_atomic  (GdkPixbuf   *pixbuf,
 			    const char  *file_name,
@@ -420,142 +380,3 @@ f_pixbuf_save_jpeg_atomic  (GdkPixbuf   *pixbuf,
 	g_free (tmp_file_name);
 	return TRUE;
 }
-
-static void 
-rotate_line (guchar *sbuf, guchar *dstart, int length, int stride, int channels, gboolean mirror)
-{
-	guchar *dbuf = dstart;
-	int doffset = stride - channels;
-	int soffset = 0;
-
-	if (mirror) {
-		sbuf += (length - 1) * channels;
-		doffset = stride - channels;
-		soffset = - (2 * channels);
-	}   
-
-	if (channels == 3)
-		while (length--) {
-			*(dbuf++) = *(sbuf++);
-			*(dbuf++) = *(sbuf++);
-			*(dbuf++) = *(sbuf++);
-			dbuf += doffset;
-			sbuf += soffset;
-		}
-	else
-		while (length--) {
-			*(dbuf++) = *(sbuf++);
-			*(dbuf++) = *(sbuf++);
-			*(dbuf++) = *(sbuf++);
-			*(dbuf++) = *(sbuf++);
-			dbuf += doffset;
-			sbuf += soffset;
-		}
-}
-
-static void
-copy_line (guchar *sbuf, guchar *dbuf, int length, int channels, gboolean mirror)
-{
-	if (!mirror) {
-		memcpy (dbuf, sbuf, length * channels);
-	} else {
-		dbuf += (length - 1) * channels;
-		if (channels == 3)
-			while (length --) {
-				*(dbuf++) = *(sbuf++);
-				*(dbuf++) = *(sbuf++);
-				*(dbuf++) = *(sbuf++);	
-				dbuf -= 6;
-			}
-		else 
-			while (length --) {
-				*(dbuf++) = *(sbuf++);
-				*(dbuf++) = *(sbuf++);
-				*(dbuf++) = *(sbuf++);	
-				*(dbuf++) = *(sbuf++);	
-				dbuf -= 8;
-			}				
-	}	
-}
-
-void
-f_pixbuf_copy_with_orientation (GdkPixbuf *src, GdkPixbuf *dest, int orientation)
-{
-	gboolean rotate = FALSE;
-	gboolean flip = FALSE;
-	gboolean mirror = FALSE;
-	
-	int sw = gdk_pixbuf_get_width (src);
-	int sh = gdk_pixbuf_get_height (src);
-	int dw = gdk_pixbuf_get_width (dest);
-	int dh = gdk_pixbuf_get_height (dest);
-	
-
-	int channels = gdk_pixbuf_get_n_channels (src);
-	
-	int dstride = gdk_pixbuf_get_rowstride (dest);
-	int sstride = gdk_pixbuf_get_rowstride (src);
-	
-	guchar *sp = gdk_pixbuf_get_pixels (src);
-	guchar *dp = gdk_pixbuf_get_pixels (dest);		
-	int offset = sstride;
-
-	if (channels != gdk_pixbuf_get_n_channels (dest)) {
-		g_warning ("source and dest channels do no match");
-		return;
-	}
-	
-	switch (orientation) {
-	case 1: // TopLeft
-		break;
-	case 2: // TopRight
-		flip = TRUE;
-		break;
-	case 3: // BottomRight
-		mirror = TRUE;
-		flip = TRUE;
-		break;
-	case 4: // BottomLeft
-		mirror = TRUE;
-		break;
-	case 5: // LeftTop
-		rotate = TRUE;
-		break;
-	case 6: // RightTop
-		mirror = TRUE;
-		rotate = TRUE;
-		break;
-	case 7: // RightBottom
-		flip = TRUE;
-		mirror = TRUE;
-		rotate = TRUE;
-		break;
-	case 8: // LeftBottom
-		flip = TRUE;
-		rotate = TRUE;
-		break;
-	}
-
-	if (rotate && (dh != sw || dw != sh)) {
-		g_warning ("source and destination sizes do not match orientation");
-		return;
-	}
-	
-	//g_warning ("rotate = %d, flip = %d, mirror = %d", rotate, flip, mirror);
-	if (mirror) {
-		offset = -sstride;
-		sp = sp + (sh - 1) * sstride;
-	}
-	
-	while (sh --) {
-		if (rotate) {
-			rotate_line (sp, dp, sw, dstride, channels, flip);
-			dp += channels;
-		} else {
-				
-			copy_line (sp, dp, sw, channels, flip);
-			dp += dstride;
-		}
-		sp += offset;
-	}
-}
diff --git a/src/Imaging/ImageFile.cs b/src/Imaging/ImageFile.cs
index d9095ef..abd22f6 100644
--- a/src/Imaging/ImageFile.cs
+++ b/src/Imaging/ImageFile.cs
@@ -96,7 +96,7 @@ namespace FSpot {
 			if (orig == null)
 				return null;
 
-			Gdk.Pixbuf rotated = PixbufUtils.TransformOrientation (orig, this.Orientation, true);
+			Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (orig, this.Orientation);
 			//ValidateThumbnail (photo, rotated);
 			if (rotated != orig)
 				orig.Dispose ();
diff --git a/src/Imaging/JpegFile.cs b/src/Imaging/JpegFile.cs
index c513d38..a29c720 100644
--- a/src/Imaging/JpegFile.cs
+++ b/src/Imaging/JpegFile.cs
@@ -233,7 +233,7 @@ namespace FSpot {
 			if (this.ExifData.Data.Length > 0) {
 				MemoryStream mem = new MemoryStream (this.ExifData.Data);
 				Gdk.Pixbuf thumb = new Gdk.Pixbuf (mem);
-				Gdk.Pixbuf rotated = PixbufUtils.TransformOrientation (thumb, this.Orientation);
+				Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (thumb, this.Orientation);
 				
 				if (rotated != thumb)
 					thumb.Dispose ();
diff --git a/src/Imaging/RafFile.cs b/src/Imaging/RafFile.cs
index f873d27..5bcb3dd 100644
--- a/src/Imaging/RafFile.cs
+++ b/src/Imaging/RafFile.cs
@@ -79,7 +79,7 @@ namespace FSpot.Raf {
 		public override Gdk.Pixbuf Load (int width, int height)
 		{
 			Gdk.Pixbuf full = this.Load ();
-			Gdk.Pixbuf rotated = PixbufUtils.TransformOrientation (full, this.GetOrientation(), true);
+			Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (full, this.GetOrientation());
 			Gdk.Pixbuf scaled  = PixbufUtils.ScaleToMaxSize (rotated, width, height);
 			full.Dispose ();
 			return scaled;
diff --git a/src/PixbufUtils.cs b/src/PixbufUtils.cs
index 66e7423..afda49a 100644
--- a/src/PixbufUtils.cs
+++ b/src/PixbufUtils.cs
@@ -118,7 +118,7 @@ public class PixbufUtils {
 			
 			loader.Close ();
 			Pixbuf orig = loader.Pixbuf;
-			Gdk.Pixbuf rotated = TransformOrientation (orig, orientation, true);
+			Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (orig, orientation);
 			
 			if (orig != rotated) {
 				CopyThumbnailOptions (orig, rotated);
@@ -709,7 +709,7 @@ public class PixbufUtils {
 			using (MemoryStream mem = new MemoryStream (thumb_data)) {
 				Gdk.Pixbuf thumb = new Gdk.Pixbuf (mem);
 
-				Gdk.Pixbuf rotated = PixbufUtils.TransformOrientation (thumb, orientation);
+				Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (thumb, orientation);
 				
 				if (rotated != thumb)
 					thumb.Dispose ();
@@ -762,77 +762,72 @@ public class PixbufUtils {
 		return ret;
 	}
 
-	public static Gdk.Pixbuf TransformOrientation (Gdk.Pixbuf src, PixbufOrientation orientation, bool copy_data)
-	{
-		Gdk.Pixbuf pixbuf;
-		if (src == null)
-			return null;
-		
-		switch (orientation) {
-		case PixbufOrientation.LeftTop:
-		case PixbufOrientation.LeftBottom:
-		case PixbufOrientation.RightTop:
-		case PixbufOrientation.RightBottom:	
-			pixbuf = new Gdk.Pixbuf (src.Colorspace, src.HasAlpha, 
-						 src.BitsPerSample,
-						 src.Height, src.Width);
-			break;
-		case PixbufOrientation.TopRight:
-		case PixbufOrientation.BottomRight:
-		case PixbufOrientation.BottomLeft:
-			pixbuf = new Gdk.Pixbuf (src.Colorspace, src.HasAlpha, 
-						 src.BitsPerSample,
-						 src.Width, src.Height);
-			break;
-		default:
-			pixbuf = src;
-			break;
-		}
-
-		if (copy_data && src != pixbuf) 
-			TransformAndCopy (src, pixbuf, orientation, new Gdk.Rectangle (0, 0, src.Width, src.Height));
-
-		return pixbuf;
-	}
-
-	public static Gdk.Pixbuf TransformOrientation (Gdk.Pixbuf src, PixbufOrientation orientation)
-	{
-		return TransformOrientation (src, orientation, true);
-	}
-
-	public static Gdk.Rectangle TransformAndCopy (Gdk.Pixbuf src, Gdk.Pixbuf dest, PixbufOrientation orientation, Gdk.Rectangle args)
-	{
-		Gdk.Rectangle area = FSpot.Utils.PixbufUtils.TransformOrientation (src, args, orientation);
-
-		int step = 256;
-
-		Gdk.Rectangle rect = new Gdk.Rectangle (args.X, args.Y, 
-							Math.Min (step, args.Width),
-							Math.Min (step, args.Height));
-
-		Gdk.Rectangle trect = FSpot.Utils.PixbufUtils.TransformOrientation (src, rect, orientation);
-		Gdk.Pixbuf tmp = new Gdk.Pixbuf (src.Colorspace, src.HasAlpha, 
-						 src.BitsPerSample,
-						 trect.Width, trect.Height);
-
-		Gdk.Rectangle subarea;
-		BlockProcessor proc = new BlockProcessor (args, 256);
-		while (proc.Step (out subarea)) {
-			Gdk.Rectangle trans = FSpot.Utils.PixbufUtils.TransformOrientation (src, subarea, orientation);
-			Gdk.Pixbuf ssub = new Gdk.Pixbuf (src, subarea.X, subarea.Y,
-							  subarea.Width, subarea.Height);
-
-			Gdk.Pixbuf tsub = new Gdk.Pixbuf (tmp, 0, 0, trans.Width, trans.Height);
-			CopyWithOrientation (ssub, tsub, orientation);
-
-			tsub.CopyArea (0, 0, trans.Width, trans.Height, dest, trans.X, trans.Y);
-			ssub.Dispose ();
-			tsub.Dispose ();
-		}
-
-		tmp.Dispose ();
-		return area;
-	}
+//	public static Gdk.Pixbuf TransformOrientation (Gdk.Pixbuf src, PixbufOrientation orientation)
+//	{
+//		Gdk.Pixbuf pixbuf;
+//		if (src == null)
+//			return null;
+//		
+//		switch (orientation) {
+//		case PixbufOrientation.LeftTop:
+//		case PixbufOrientation.LeftBottom:
+//		case PixbufOrientation.RightTop:
+//		case PixbufOrientation.RightBottom:	
+//			pixbuf = new Gdk.Pixbuf (src.Colorspace, src.HasAlpha, 
+//						 src.BitsPerSample,
+//						 src.Height, src.Width);
+//			break;
+//		case PixbufOrientation.TopRight:
+//		case PixbufOrientation.BottomRight:
+//		case PixbufOrientation.BottomLeft:
+//			pixbuf = new Gdk.Pixbuf (src.Colorspace, src.HasAlpha, 
+//						 src.BitsPerSample,
+//						 src.Width, src.Height);
+//			break;
+//		default:
+//			pixbuf = src;
+//			break;
+//		}
+//
+//		if (src != pixbuf) 
+//			TransformAndCopy (src, pixbuf, orientation, new Gdk.Rectangle (0, 0, src.Width, src.Height));
+//
+//		return pixbuf;
+//	}
+//
+//	static Gdk.Rectangle TransformAndCopy (Gdk.Pixbuf src, Gdk.Pixbuf dest, PixbufOrientation orientation, Gdk.Rectangle args)
+//	{
+//		Gdk.Rectangle area = FSpot.Utils.PixbufUtils.TransformOrientation (src, args, orientation);
+//
+//		int step = 256;
+//
+//		Gdk.Rectangle rect = new Gdk.Rectangle (args.X, args.Y, 
+//							Math.Min (step, args.Width),
+//							Math.Min (step, args.Height));
+//
+//		Gdk.Rectangle trect = FSpot.Utils.PixbufUtils.TransformOrientation (src, rect, orientation);
+//		Gdk.Pixbuf tmp = new Gdk.Pixbuf (src.Colorspace, src.HasAlpha, 
+//						 src.BitsPerSample,
+//						 trect.Width, trect.Height);
+//
+//		Gdk.Rectangle subarea;
+//		BlockProcessor proc = new BlockProcessor (args, 256);
+//		while (proc.Step (out subarea)) {
+//			Gdk.Rectangle trans = FSpot.Utils.PixbufUtils.TransformOrientation (src, subarea, orientation);
+//			Gdk.Pixbuf ssub = new Gdk.Pixbuf (src, subarea.X, subarea.Y,
+//							  subarea.Width, subarea.Height);
+//
+//			Gdk.Pixbuf tsub = new Gdk.Pixbuf (tmp, 0, 0, trans.Width, trans.Height);
+//			CopyWithOrientation (ssub, tsub, orientation);
+//
+//			tsub.CopyArea (0, 0, trans.Width, trans.Height, dest, trans.X, trans.Y);
+//			ssub.Dispose ();
+//			tsub.Dispose ();
+//		}
+//
+//		tmp.Dispose ();
+//		return area;
+//	}
 	// Bindings from libf.
 
 	[DllImport ("libfspot")]
@@ -855,13 +850,13 @@ public class PixbufUtils {
 		}
 	}
 
-	[DllImport ("libfspot")]
-	static extern void f_pixbuf_copy_with_orientation (IntPtr src, IntPtr dest, int orientation);
-
-	public static void CopyWithOrientation (Gdk.Pixbuf src, Gdk.Pixbuf dest, PixbufOrientation orientation)
-	{
-		f_pixbuf_copy_with_orientation (src.Handle, dest.Handle, (int)orientation);
-	}
+//	[DllImport ("libfspot")]
+//	static extern void f_pixbuf_copy_with_orientation (IntPtr src, IntPtr dest, int orientation);
+//
+//	public static void CopyWithOrientation (Gdk.Pixbuf src, Gdk.Pixbuf dest, PixbufOrientation orientation)
+//	{
+//		f_pixbuf_copy_with_orientation (src.Handle, dest.Handle, (int)orientation);
+//	}
 
 #if false
 	[DllImport("glibsharpglue")]
diff --git a/src/RotateCommand.cs b/src/RotateCommand.cs
index b3b46b3..8f6f7c5 100644
--- a/src/RotateCommand.cs
+++ b/src/RotateCommand.cs
@@ -100,7 +100,7 @@ namespace FSpot {
 					using (Stream stream = File.Open (backup, FileMode.Truncate, FileAccess.Write)) {
 						using (Pixbuf pixbuf = img.Load ()) {
 							PixbufOrientation fake = (direction == RotateDirection.Clockwise) ? PixbufOrientation.RightTop : PixbufOrientation.LeftBottom;
-							using (Pixbuf rotated = PixbufUtils.TransformOrientation (pixbuf, fake)) {
+							using (Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (pixbuf, fake)) {
 								img.Save (rotated, stream);
 							}
 						}
diff --git a/src/Utils/PixbufUtils.cs b/src/Utils/PixbufUtils.cs
index 4b3f429..a92dd96 100644
--- a/src/Utils/PixbufUtils.cs
+++ b/src/Utils/PixbufUtils.cs
@@ -9,6 +9,8 @@
 // This is free softwae. See cOPYING for details
 //
 
+using Gdk;
+
 namespace FSpot.Utils
 {
 	public static class PixbufUtils
@@ -38,14 +40,14 @@ namespace FSpot.Utils
 			return orientation;
 		}
 
-		public static Gdk.Rectangle TransformOrientation (Gdk.Pixbuf src, Gdk.Rectangle args, PixbufOrientation orientation)
+		public static Rectangle TransformOrientation (Pixbuf src, Rectangle args, PixbufOrientation orientation)
 		{
 			return TransformOrientation (src.Width, src.Height, args, orientation);
 		}
 		
-		public static Gdk.Rectangle TransformOrientation (int total_width, int total_height, Gdk.Rectangle args, PixbufOrientation orientation)
+		public static Rectangle TransformOrientation (int total_width, int total_height, Rectangle args, PixbufOrientation orientation)
 		{
-			Gdk.Rectangle area = args;
+			Rectangle area = args;
 			
 			switch (orientation) {
 			case PixbufOrientation.BottomRight:
@@ -88,5 +90,44 @@ namespace FSpot.Utils
 			
 			return area;
 		}
+
+		public static Pixbuf TransformOrientation (Pixbuf src, PixbufOrientation orientation)
+		{
+			Pixbuf dest;
+
+			switch (orientation) {
+			default:
+			case PixbufOrientation.TopLeft:
+				dest = src;
+				break;
+			case PixbufOrientation.TopRight:
+				dest = src.Flip (false);
+				break;
+			case PixbufOrientation.BottomRight:
+				dest = src.RotateSimple (PixbufRotation.Upsidedown);
+				break;
+			case PixbufOrientation.BottomLeft:
+				dest = src.Flip (true);
+				break;
+			case PixbufOrientation.LeftTop:
+				using (var rotated = src.RotateSimple (PixbufRotation.Clockwise)) {
+					dest = rotated.Flip (false);
+				}
+				break;
+			case PixbufOrientation.RightTop:
+				dest = src.RotateSimple (PixbufRotation.Clockwise);
+				break;
+			case PixbufOrientation.RightBottom:
+				using (var rotated = src.RotateSimple (PixbufRotation.Counterclockwise)) {
+					dest = rotated.Flip (false);
+				}
+				break;
+			case PixbufOrientation.LeftBottom:
+				dest = src.RotateSimple (PixbufRotation.Counterclockwise);
+				break;
+			}
+			
+			return dest;
+		}
 	}
 }



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