mistelix r13 - in trunk: . Test Test/NUnit Test/dataset Test/dataset/images Test/perf src/core



Author: jmas
Date: Wed Apr  8 10:50:16 2009
New Revision: 13
URL: http://svn.gnome.org/viewvc/mistelix?rev=13&view=rev

Log:
2009-04-08 Jordi Mas <jmas softcatala org>

	* Test/: New test infrastructure (performance & unit tests)
	* src/core/SlideImage.cs: Better load LoadPixBuffer



Added:
   trunk/Test/
   trunk/Test/NUnit/
   trunk/Test/NUnit/Makefile
   trunk/Test/dataset/
   trunk/Test/dataset/images/
   trunk/Test/dataset/images/image1.jpg   (contents, props changed)
   trunk/Test/dataset/images/image2.jpg   (contents, props changed)
   trunk/Test/perf/
   trunk/Test/perf/Makefile
   trunk/Test/perf/SlideImagePerf.cs   (contents, props changed)
Modified:
   trunk/ChangeLog
   trunk/src/core/SlideImage.cs

Added: trunk/Test/NUnit/Makefile
==============================================================================
--- (empty file)
+++ trunk/Test/NUnit/Makefile	Wed Apr  8 10:50:16 2009
@@ -0,0 +1,26 @@
+all: test
+
+TESTASSEMBLY = mistelix.tests.dll
+
+SOURCES = 				\
+	SlideImageTest.cs
+PKGS = 					\
+	-pkg:mono-nunit			\
+	-pkg:mistelix			\
+	-pkg:gnome-sharp-2.0		\
+	-pkg:gtk-sharp-2.0
+
+REFS = -r:Mono.Cairo
+RESOURCES =
+
+
+MCSFLAGS = -t:library -d:ENABLE_NUNIT
+
+$(TESTASSEMBLY): $(SOURCES)
+	gmcs $(MCSFLAGS) $(PKGS) $(REFS) $(SOURCES) $(RESOURCES) -out:$(TESTASSEMBLY)
+
+test: $(TESTASSEMBLY)
+	export MONO_PATH=/usr/lib/mono/gtk-sharp-2.0/:/usr/local/lib/mistelix && nunit-console2 $(TESTASSEMBLY)
+
+clean:
+	rm -f *~ *.dll TestResult.xml *.db

Added: trunk/Test/dataset/images/image1.jpg
==============================================================================
Binary file. No diff available.

Added: trunk/Test/dataset/images/image2.jpg
==============================================================================
Binary file. No diff available.

Added: trunk/Test/perf/Makefile
==============================================================================
--- (empty file)
+++ trunk/Test/perf/Makefile	Wed Apr  8 10:50:16 2009
@@ -0,0 +1,19 @@
+all: outfile
+
+outfile = slideimage.exe
+
+SOURCES = SlideImagePerf.cs
+	  
+	
+PKGS = 					\
+	-pkg:gtk-sharp-2.0 -pkg:mono-addins
+
+REFS =					\
+	-r:../../src/mistelix.exe
+
+
+outfile: $(SOURCES) 
+	gmcs $(PKGS) $(REFS) $(SOURCES) $(RESOURCES) -out:$(outfile)	
+
+clean:
+	rm -f *~ *.dll TestResult.xml *.db

Added: trunk/Test/perf/SlideImagePerf.cs
==============================================================================
--- (empty file)
+++ trunk/Test/perf/SlideImagePerf.cs	Wed Apr  8 10:50:16 2009
@@ -0,0 +1,52 @@
+using System;
+using Gtk;
+using System.Diagnostics;
+
+using Mistelix.Core;
+using Mistelix.DataModel;
+using Mistelix.Effects;
+using Mono.Addins;
+
+namespace Mistelix.Performance
+{
+	// Performance testing of SlideImage operations
+	public class SlideImagePerf
+	{
+		static void Main ()
+		{
+			SlideImage img;
+			TimeSpan time;
+			byte [] pixels;
+			Project project = new Project ();
+			Stopwatch stopWatch = new Stopwatch ();
+			int times = 10;
+
+			Application.Init ();
+
+			stopWatch.Start ();
+			for (int i = 0; i < times; i++)
+			{
+				img = new SlideImage ("../dataset/images/image1.jpg");
+				img.Title = "Hello world";
+				img.Project = project;
+				pixels = img.Pixels;
+			}
+			stopWatch.Stop();
+			time = stopWatch.Elapsed;
+			Console.WriteLine ("Images with title. Time needed {0}", time);
+
+			stopWatch.Reset ();
+			stopWatch.Start ();
+			for (int i = 0; i < times; i++)
+			{
+				img = new SlideImage ("../dataset/images/image1.jpg");
+				img.Project = project;
+				pixels = img.Pixels;
+			}
+
+			time = stopWatch.Elapsed;
+			Console.WriteLine ("Images. Time needed {0}", time);
+			return;
+		}
+	}
+}

Modified: trunk/src/core/SlideImage.cs
==============================================================================
--- trunk/src/core/SlideImage.cs	(original)
+++ trunk/src/core/SlideImage.cs	Wed Apr  8 10:50:16 2009
@@ -42,11 +42,27 @@
 	//
 	public class SlideImage : SlideShowProjectElement.Image
     	{
-		[XmlIgnoreAttribute] int width, height, stride, channels = 3, requested_channels = 3;
+
+		//
+		// Pixel order
+		//	
+		//	mistelixvideosrc uses RGB pixel order
+		// 	Cairo uses ARGB (each pixel is a 32-bit quantity, ARGB. The 32-bit quantities are stored native-endian)
+		//	Pixbuf uses RGBA (big-endian order)
+		//
+		enum PixelFormat
+		{
+			CAIRO_ARGB,	//
+			PIXBUF_RGB,	// 3 channels
+			PIXBUF_ARGB	// 4 channels
+		};
+
+		[XmlIgnoreAttribute] int width, height, stride, channels = 3;
 		[XmlIgnoreAttribute] bool alpha = false;
 		[XmlIgnoreAttribute] byte[] pixels = null;
 		[XmlIgnoreAttribute] Project project;
 		[XmlIgnoreAttribute] int offset_x, offset_y, w, h;
+		[XmlIgnoreAttribute] PixelFormat pixel_format = PixelFormat.PIXBUF_RGB;
 
 		[XmlIgnoreAttribute]
 		public int Width {
@@ -111,7 +127,7 @@
 			alpha = true;
 			pixels = img.Data;
 		}
-
+	
 		[System.Xml.Serialization.XmlIgnoreAttribute]
 		public byte[] Pixels {
 			get {
@@ -159,7 +175,7 @@
 		{
 			SlideImage slide = new SlideImage ();
 			slide.CopyProperties (this);
-			slide.requested_channels = 4;
+			slide.pixel_format = PixelFormat.CAIRO_ARGB;
 			slide.LoadAndScaleImage (width, height);
 			//slide.ProcessImage ();
 			slide.ProcessEffects ();
@@ -169,17 +185,17 @@
 		}
 
 		// mistelixvideosrc expects images in 24 bits (3 channels)
-		void FromImage (DataImageSurface img)
+		void FromDataImageSurface (DataImageSurface img)
 		{
-			if (img.Format != Cairo.Format.Argb32 && img.Format != Cairo.Format.Rgb24)
+			if (img.Format != Cairo.Format.Argb32)
 				throw new InvalidOperationException (String.Format ("SlideImage.FromCairo: unsupported format {0}", img.Format));
 
 			width = img.Width;
 			height = img.Height;
-			stride = img.Width * 3;
+			pixels = img.Get24bitsPixBuf ();
 			channels = 3;
+			stride = img.Width * channels;
 			alpha = false;
-			pixels = img.Get24bitsPixBuf ();
 		}
 		
 		void DrawImageLegend (Cairo.Context cr, string title, int x, int y, int width, int height)
@@ -259,7 +275,15 @@
 			Gdk.Pixbuf raw_image, processed_image;
 
 			raw_image = new Gdk.Pixbuf (image);
-			processed_image = new Gdk.Pixbuf (Colorspace.Rgb, false, 8, max_w, max_h);
+
+			Logger.Debug ("SlideImage.LoadAndScaleImage. Load image w:{0} h:{1} channels:{2}", raw_image.Width, raw_image.Height, raw_image.NChannels);
+
+			if (raw_image.NChannels != 3 && raw_image.NChannels != 4) {
+				Logger.Error ("Image {0} with unsupported number of channels ({1})", image, raw_image.NChannels);
+				return;
+			}
+				
+			processed_image = new Gdk.Pixbuf (Colorspace.Rgb, raw_image.NChannels == 3 ? false : true, 8, max_w, max_h);
 			processed_image.Fill (0x00000000);
 
 			original_ratio = (double) raw_image.Width / (double) raw_image.Height;
@@ -342,7 +366,7 @@
 			DataImageSurface surface = new DataImageSurface (DataImageSurface.Allocate (pix), Cairo.Format.Argb32, width, height, stride);
 			Cairo.Context gr = new Cairo.Context (surface);
 			DrawImageLegend (gr, Title, offset_x, offset_y,  w, h);
-			FromImage (surface);
+			FromDataImageSurface (surface);
 			((IDisposable)gr).Dispose ();
 			((IDisposable)surface).Dispose ();
 		}
@@ -367,50 +391,65 @@
 			CopyProperties (processed);
 			Pixels = processed.Pixels;
 		}
-		
+
 		void LoadPixBuffer (Gdk.Pixbuf buf)
 		{
-			int len;
+			if ((pixel_format == PixelFormat.CAIRO_ARGB && pixel_format == PixelFormat.PIXBUF_RGB) ||
+					(buf.NChannels != 3 && buf.NChannels != 4)) {
+				throw new InvalidOperationException (
+						String.Format ("Could not process SlideImage.LoadPixBuffer requested format {0} image {1}", 
+						pixel_format, buf.NChannels));
+			}
 
-			width = buf.Width;
-			height = buf.Height;
-			stride = buf.Rowstride;
-			alpha = buf.HasAlpha;
-			
-			if (requested_channels == buf.NChannels) {
-				channels = buf.NChannels;
-				len = stride * height;
-				pixels = new byte [len];
-				Marshal.Copy (buf.Pixels, pixels, 0, len);
-				return;
+			int src, len, channels_src;
+			byte [] source;
+
+			switch (pixel_format) {
+			case PixelFormat.PIXBUF_RGB:
+				alpha = false;
+				channels = 3;
+				break;
+			case PixelFormat.PIXBUF_ARGB:
+			case PixelFormat.CAIRO_ARGB:
+				channels = 4;
+				alpha = true;
+				break;
+			default:
+				throw new InvalidOperationException ("Unsupported format");
 			}
 
-			if (requested_channels == 4 && buf.NChannels == 3) {
-				int src;
-				byte [] source;
+			// Move source data to an array
+			src = 0;
+			channels_src = buf.NChannels;
+			len = buf.Rowstride * buf.Height;
+			source = new byte [len];
+			Marshal.Copy (buf.Pixels, source, 0, len);
 
-				src = 0;
-				channels = buf.NChannels;
-				stride = 4 * width;
-				len = stride * height;
-				source = new byte [channels * height * width];
-				Marshal.Copy (buf.Pixels, source, 0, channels * height * width);
+			// Target data array
+			width = buf.Width;
+			height = buf.Height;
+			stride = channels * width;
+			len = stride * height;
+			pixels = new byte [len];
+						
+			for (int trg = 0; trg < len; trg = trg + channels) {
 
-				pixels = new byte [len];
-							
-				for (int trg = 0; trg < len; trg = trg + 4) {
+				if (pixel_format == PixelFormat.CAIRO_ARGB) {
 					pixels [trg]      = source [src + 2];
 					pixels [trg + 1]  = source [src + 1];
 					pixels [trg + 2]  = source [src + 0];
-					pixels [trg + 3]  = 0xff;
-					src += 3;
+					
+				} else {
+					pixels [trg]      = source [src];
+					pixels [trg + 1]  = source [src + 1];
+					pixels [trg + 2]  = source [src + 2];
 				}
-				return;
-			}
 
-			throw new InvalidOperationException (
-					String.Format ("Could not process SlideImage.LoadPixBuffer requested channels {0} image {1}", 
-					requested_channels, buf.NChannels));
+				if (channels == 4)
+					pixels [trg + 3]  = 0xff;
+
+				src += channels_src;
+			}
 		}
 	}
 }



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