[f-spot] Split the ImageFile Interface out into its own file



commit f096f17f7a49a9e2f998afbd8cabaa64a34566f8
Author: Stephen Shaw <sshaw decriptor com>
Date:   Wed Nov 30 00:40:09 2011 -0700

    Split the ImageFile Interface out into its own file

 src/Clients/MainApp/FSpot.Imaging/IImageFile.cs |  139 ++++++++++
 src/Clients/MainApp/FSpot.Imaging/ImageFile.cs  |  311 ++++++++--------------
 src/Clients/MainApp/MainApp.csproj              |    1 +
 src/Clients/MainApp/Makefile.am                 |    1 +
 4 files changed, 254 insertions(+), 198 deletions(-)
---
diff --git a/src/Clients/MainApp/FSpot.Imaging/IImageFile.cs b/src/Clients/MainApp/FSpot.Imaging/IImageFile.cs
new file mode 100644
index 0000000..5d3ee55
--- /dev/null
+++ b/src/Clients/MainApp/FSpot.Imaging/IImageFile.cs
@@ -0,0 +1,139 @@
+//
+// ImageFile.cs
+//
+// Author:
+//   Stephane Delcroix <stephane delcroix org>
+//   Ruben Vermeersch <ruben savanne be>
+//   Stephen Shaw <sshaw decriptor com>
+//
+// Copyright (C) 2007-2010 Novell, Inc.
+// Copyright (C) 2007-2009 Stephane Delcroix
+// Copyright (C) 2010 Ruben Vermeersch
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using Hyena;
+
+using System;
+using System.IO;
+using System.Collections.Generic;
+
+using FSpot.Utils;
+using Mono.Unix;
+using Mono.Unix.Native;
+using Gdk;
+
+using GLib;
+using TagLib.Image;
+
+using GFileInfo = GLib.FileInfo;
+
+
+namespace FSpot.Imaging
+{
+	public interface IImageFile : IDisposable
+	{
+		SafeUri Uri { get; }
+		ImageOrientation Orientation { get; }
+
+		Gdk.Pixbuf Load ();
+		Cms.Profile GetProfile ();
+		Gdk.Pixbuf Load (int max_width, int max_height);
+		Stream PixbufStream ();
+	}
+
+	public class BaseImageFile : IImageFile
+	{
+		public SafeUri Uri { get; private set; }
+
+		public ImageOrientation Orientation { get; private set; }
+
+		public BaseImageFile (SafeUri uri)
+		{
+			Uri = uri;
+			Orientation = ImageOrientation.TopLeft;
+
+			using (var metadata_file = Metadata.Parse (uri)) {
+				ExtractMetadata (metadata_file);
+			}
+		}
+
+		protected virtual void ExtractMetadata (TagLib.Image.File metadata)
+		{
+			if (metadata != null)
+				Orientation = metadata.ImageTag.Orientation;
+		}
+
+		~BaseImageFile ()
+		{
+			Dispose ();
+		}
+
+		public virtual Stream PixbufStream ()
+		{
+			Hyena.Log.DebugFormat ("open uri = {0}", Uri.ToString ());
+			return new GLib.GioStream (GLib.FileFactory.NewForUri (Uri).Read (null));
+		}
+
+		protected Gdk.Pixbuf TransformAndDispose (Gdk.Pixbuf orig)
+		{
+			if (orig == null)
+				return null;
+
+			Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (orig, this.Orientation);
+
+			orig.Dispose ();
+
+			return rotated;
+		}
+
+		public Gdk.Pixbuf Load ()
+		{
+			using (Stream stream = PixbufStream ()) {
+				Gdk.Pixbuf orig = new Gdk.Pixbuf (stream);
+				return TransformAndDispose (orig);
+			}
+		}
+
+		public Gdk.Pixbuf Load (int max_width, int max_height)
+		{
+			Gdk.Pixbuf full = this.Load ();
+			Gdk.Pixbuf scaled = PixbufUtils.ScaleToMaxSize (full, max_width, max_height);
+			full.Dispose ();
+			return scaled;
+		}
+
+		// FIXME this need to have an intent just like the loading stuff.
+		public virtual Cms.Profile GetProfile ()
+		{
+			return null;
+		}
+
+		public void Dispose ()
+		{
+			Close ();
+			System.GC.SuppressFinalize (this);
+		}
+
+		protected virtual void Close ()
+		{
+		}
+	}
+}
diff --git a/src/Clients/MainApp/FSpot.Imaging/ImageFile.cs b/src/Clients/MainApp/FSpot.Imaging/ImageFile.cs
index b5ea776..78a9358 100644
--- a/src/Clients/MainApp/FSpot.Imaging/ImageFile.cs
+++ b/src/Clients/MainApp/FSpot.Imaging/ImageFile.cs
@@ -4,6 +4,7 @@
 // Author:
 //   Stephane Delcroix <stephane delcroix org>
 //   Ruben Vermeersch <ruben savanne be>
+//   Stephen Shaw <sshaw decriptor com>
 //
 // Copyright (C) 2007-2010 Novell, Inc.
 // Copyright (C) 2007-2009 Stephane Delcroix
@@ -45,130 +46,128 @@ using TagLib.Image;
 
 using GFileInfo = GLib.FileInfo;
 
-namespace FSpot.Imaging {
-	public class ImageFormatException : ApplicationException {
+namespace FSpot.Imaging
+{
+	public class ImageFormatException : ApplicationException
+	{
 		public ImageFormatException (string msg) : base (msg)
 		{
 		}
 	}
 
-	public static class ImageFile {
-
-#region Factory functionality
-
-		static Dictionary<string, Type> name_table;
-		internal static Dictionary<string, Type> NameTable { get { return name_table; } }
-
-        static ImageFile ()
-        {
-            var base_type = typeof (BaseImageFile);
-            var raw_type = typeof (DCRawFile);
-            var nef_type = typeof (NefFile);
-            var cr2_type = typeof (Cr2File);
-            var dng_type = typeof (DngFile);
-            var ciff_type = typeof (Ciff.CiffFile);
-            var raf_type = typeof (RafFile);
-
-            name_table = new Dictionary<string, Type> ();
-
-            // Plain image files
-            name_table ["image/gif"] = name_table [".gif"] = base_type;
-            name_table ["image/x-pcx"] = name_table [".pcx"] = base_type;
-            name_table ["image/x-portable-anymap"] = name_table [".pnm"] = base_type;
-            name_table ["image/x-portable-bitmap"] = name_table [".pbm"] = base_type;
-            name_table ["image/x-portable-graymap"] = name_table [".pgm"] = base_type;
-            name_table ["image/x-portable-pixmap"] = name_table [".ppm"] = base_type;
-            name_table ["image/x-bmp"] = name_table ["image/x-MS-bmp"] = name_table [".bmp"] = base_type;
-            name_table ["image/jpeg"] = name_table [".jfi"] = name_table [".jfif"] = name_table [".jif"] = name_table [".jpe"] = name_table [".jpeg"] = name_table [".jpg"] = base_type;
-            name_table ["image/png"] = name_table [".png"] = base_type;
-            name_table ["image/tiff"] = name_table [".tif"] = name_table [".tiff"] = base_type;
-            name_table ["image/svg+xml"] = name_table [".svg"] = name_table [".svgz"] = base_type;
-
-            // RAW files
-            name_table ["image/arw"] = name_table ["image/x-sony-arw"] = name_table [".arw"] = nef_type;
-            name_table ["image/cr2"] = name_table ["image/x-canon-cr2"] = name_table [".cr2"] = cr2_type;
-            name_table ["image/dng"] = name_table ["image/x-adobe-dng"] = name_table [".dng"] = dng_type;
-            name_table ["image/nef"] = name_table ["image/x-nikon-nef"] = name_table [".nef"] = nef_type;
-            name_table ["image/rw2"] = name_table ["image/x-raw"] = name_table [".rw2"] = raw_type;
-            name_table ["image/pef"] = name_table ["image/x-pentax-pef"] = name_table [".pef"] = nef_type;
-            name_table ["image/raw"] = name_table ["image/x-panasonic-raw"] = name_table [".raw"] = nef_type;
-
-            // Other types (FIXME: Currently unsupported by Taglib#, this list should shrink).
-
-            name_table ["image/x-ciff"] = name_table [".crw"] = ciff_type;
-            name_table ["image/x-mrw"]  = name_table [".mrw"] = raw_type;
-            name_table ["image/x-x3f"]  = name_table [".x3f"] = raw_type;
-            name_table ["image/x-orf"]  = name_table [".orf"] = nef_type;
-            name_table ["image/x-raf"]  = name_table [".raf"] = raf_type;
-            name_table [".kdc"] = nef_type;
-            name_table [".rw2"] = raw_type;
-            name_table [".srf"] = nef_type;
+	public static class ImageFile
+	{
+		#region Factory functionality
+		internal static Dictionary<string, Type> NameTable { get; private set; }
 
+		static ImageFile ()
+		{
+			var base_type = typeof(BaseImageFile);
+			var raw_type = typeof(DCRawFile);
+			var nef_type = typeof(NefFile);
+			var cr2_type = typeof(Cr2File);
+			var dng_type = typeof(DngFile);
+			var ciff_type = typeof(Ciff.CiffFile);
+			var raf_type = typeof(RafFile);
+
+			NameTable = new Dictionary<string, Type> ();
+
+			// Plain image files
+			NameTable ["image/gif"] = NameTable [".gif"] = base_type;
+			NameTable ["image/x-pcx"] = NameTable [".pcx"] = base_type;
+			NameTable ["image/x-portable-anymap"] = NameTable [".pnm"] = base_type;
+			NameTable ["image/x-portable-bitmap"] = NameTable [".pbm"] = base_type;
+			NameTable ["image/x-portable-graymap"] = NameTable [".pgm"] = base_type;
+			NameTable ["image/x-portable-pixmap"] = NameTable [".ppm"] = base_type;
+			NameTable ["image/x-bmp"] = NameTable ["image/x-MS-bmp"] = NameTable [".bmp"] = base_type;
+			NameTable ["image/jpeg"] = NameTable [".jfi"] = NameTable [".jfif"] = NameTable [".jif"] = NameTable [".jpe"] = NameTable [".jpeg"] = NameTable [".jpg"] = base_type;
+			NameTable ["image/png"] = NameTable [".png"] = base_type;
+			NameTable ["image/tiff"] = NameTable [".tif"] = NameTable [".tiff"] = base_type;
+			NameTable ["image/svg+xml"] = NameTable [".svg"] = NameTable [".svgz"] = base_type;
+
+			// RAW files
+			NameTable ["image/arw"] = NameTable ["image/x-sony-arw"] = NameTable [".arw"] = nef_type;
+			NameTable ["image/cr2"] = NameTable ["image/x-canon-cr2"] = NameTable [".cr2"] = cr2_type;
+			NameTable ["image/dng"] = NameTable ["image/x-adobe-dng"] = NameTable [".dng"] = dng_type;
+			NameTable ["image/nef"] = NameTable ["image/x-nikon-nef"] = NameTable [".nef"] = nef_type;
+			NameTable ["image/rw2"] = NameTable ["image/x-raw"] = NameTable [".rw2"] = raw_type;
+			NameTable ["image/pef"] = NameTable ["image/x-pentax-pef"] = NameTable [".pef"] = nef_type;
+			NameTable ["image/raw"] = NameTable ["image/x-panasonic-raw"] = NameTable [".raw"] = nef_type;
+
+			// Other types (FIXME: Currently unsupported by Taglib#, this list should shrink).
+
+			NameTable ["image/x-ciff"] = NameTable [".crw"] = ciff_type;
+			NameTable ["image/x-mrw"] = NameTable [".mrw"] = raw_type;
+			NameTable ["image/x-x3f"] = NameTable [".x3f"] = raw_type;
+			NameTable ["image/x-orf"] = NameTable [".orf"] = nef_type;
+			NameTable ["image/x-raf"] = NameTable [".raf"] = raf_type;
+			NameTable [".kdc"] = nef_type;
+			NameTable [".rw2"] = raw_type;
+			NameTable [".srf"] = nef_type;
+
+
+			// as xcf pixbufloader is not part of gdk-pixbuf, check if it's there,
+			// and enable it if needed.
+			foreach (Gdk.PixbufFormat format in Gdk.Pixbuf.Formats) {
+				if (format.Name == "xcf") {
+					if (format.IsDisabled)
+						format.SetDisabled (false);
+					NameTable [".xcf"] = base_type;
+				}
+			}
+		}
 
-            // as xcf pixbufloader is not part of gdk-pixbuf, check if it's there,
-            // and enable it if needed.
-            foreach (Gdk.PixbufFormat format in Gdk.Pixbuf.Formats) {
-                if (format.Name == "xcf") {
-                    if (format.IsDisabled)
-                        format.SetDisabled (false);
-                    name_table [".xcf"] = base_type;
-                }
-            }
-        }
+		public static bool HasLoader (SafeUri uri)
+		{
+			return GetLoaderType (uri) != null;
+		}
 
-        public static bool HasLoader (SafeUri uri)
-        {
-            return GetLoaderType (uri) != null;
-        }
+		static Type GetLoaderType (SafeUri uri)
+		{
+			// check if GIO can find the file, which is not the case
+			// with filenames with invalid encoding
+			var file = GLib.FileFactory.NewForUri (uri);
+			if (!file.Exists)
+             		   return null;
 
-        static Type GetLoaderType (SafeUri uri)
-        {
-            // check if GIO can find the file, which is not the case
-            // with filenames with invalid encoding
-            var file = GLib.FileFactory.NewForUri (uri);
-            if (!file.Exists) {
-                return null;
-            }
+			string extension = uri.GetExtension ().ToLower ();
 
-            var extension = uri.GetExtension ().ToLower ();
-            if (extension == ".thm") {
-                // Ignore video thumbnails.
-                return null;
-            }
+			// Ignore video thumbnails
+			if (extension == ".thm")
+		                return null;
 
-            // Detect mime-type
-            var info = file.QueryInfo ("standard::content-type,standard::size", FileQueryInfoFlags.None, null);
-            var mime = info.ContentType;
-            var size = info.Size;
+			// Detect mime-type
+			var info = file.QueryInfo ("standard::content-type,standard::size", FileQueryInfoFlags.None, null);
+			string mime = info.ContentType;
+			long size = info.Size;
 
-            if (size == 0) {
-                // Empty file
-                return null;
-            }
+			// Empty file
+			if (size == 0)
+		                return null;
 
-            Type t = null;
+			Type t = null;
 
-            if (name_table.TryGetValue (mime, out t)) {
-                return t;
-            } else if (name_table.TryGetValue (extension, out t)) {
-                return t;
-            }
-            return null;
-        }
+			if (NameTable.TryGetValue (mime, out t))
+				return t;
+			else if (NameTable.TryGetValue (extension, out t))
+				return t;
 
-        public static IImageFile Create (SafeUri uri)
-        {
-            var t = GetLoaderType (uri);
-            if (t == null)
-                throw new Exception (String.Format ("Unsupported image: {0}", uri));
+			return null;
+		}
 
-            try {
-                return (IImageFile) System.Activator.CreateInstance (t, new object[] { uri });
-            } catch (Exception e) {
-                Hyena.Log.DebugException (e);
-                throw e;
-            }
-        }
+		public static IImageFile Create (SafeUri uri)
+		{
+			var t = GetLoaderType (uri);
+			if (t == null)
+				throw new Exception (String.Format ("Unsupported image: {0}", uri));
+
+			try {
+				return (IImageFile)System.Activator.CreateInstance (t, new object[] { uri });
+			} catch (Exception e) {
+				Hyena.Log.DebugException (e);
+				throw e;
+			}
+		}
 
 		public static bool IsRaw (SafeUri uri)
 		{
@@ -186,107 +185,23 @@ namespace FSpot.Imaging {
 				".rw2",
 			};
 			var extension = uri.GetExtension ().ToLower ();
-			foreach (string ext in raw_extensions)
+			foreach (string ext in raw_extensions) {
 				if (ext == extension)
 					return true;
+			}
 			return false;
 		}
 
 		public static bool IsJpeg (SafeUri uri)
 		{
-            string [] jpg_extensions = {".jpg", ".jpeg", ".jpe", ".jfi", ".jfif", ".jif"};
+			string [] jpg_extensions = {".jpg", ".jpeg", ".jpe", ".jfi", ".jfif", ".jif"};
 			var extension = uri.GetExtension ().ToLower ();
-			foreach (string ext in jpg_extensions)
+			foreach (string ext in jpg_extensions) {
 				if (ext == extension)
 					return true;
+			}
 			return false;
 		}
-
-#endregion
+		#endregion
 	}
-
-    public interface IImageFile : IDisposable {
-        SafeUri Uri { get; }
-        Gdk.Pixbuf Load ();
-        Cms.Profile GetProfile ();
-        Gdk.Pixbuf Load (int max_width, int max_height);
-        Stream PixbufStream ();
-        ImageOrientation Orientation { get; }
-    }
-
-    public class BaseImageFile : IImageFile {
-        public SafeUri Uri { get; private set; }
-        public ImageOrientation Orientation { get; private set; }
-
-        public BaseImageFile (SafeUri uri)
-        {
-            Uri = uri;
-            Orientation = ImageOrientation.TopLeft;
-
-            using (var metadata_file = Metadata.Parse (uri)) {
-                ExtractMetadata (metadata_file);
-            }
-        }
-
-        protected virtual void ExtractMetadata (TagLib.Image.File metadata) {
-            if (metadata != null) {
-                Orientation = metadata.ImageTag.Orientation;
-            }
-        }
-
-		~BaseImageFile ()
-		{
-			Dispose ();
-		}
-
-		public virtual Stream PixbufStream ()
-		{
-			Hyena.Log.DebugFormat ("open uri = {0}", Uri.ToString ());
-			return new GLib.GioStream (GLib.FileFactory.NewForUri (Uri).Read (null));
-		}
-
-		protected Gdk.Pixbuf TransformAndDispose (Gdk.Pixbuf orig)
-		{
-			if (orig == null)
-				return null;
-
-			Gdk.Pixbuf rotated = FSpot.Utils.PixbufUtils.TransformOrientation (orig, this.Orientation);
-
-			orig.Dispose ();
-
-			return rotated;
-		}
-
-		public Gdk.Pixbuf Load ()
-		{
-			using (Stream stream = PixbufStream ()) {
-				Gdk.Pixbuf orig = new Gdk.Pixbuf (stream);
-				return TransformAndDispose (orig);
-			}
-		}
-
-		public Gdk.Pixbuf Load (int max_width, int max_height)
-		{
-			Gdk.Pixbuf full = this.Load ();
-			Gdk.Pixbuf scaled  = PixbufUtils.ScaleToMaxSize (full, max_width, max_height);
-			full.Dispose ();
-			return scaled;
-		}
-
-		// FIXME this need to have an intent just like the loading stuff.
-		public virtual Cms.Profile GetProfile ()
-		{
-			return null;
-		}
-
-		public void Dispose ()
-		{
-			Close ();
-			System.GC.SuppressFinalize (this);
-		}
-
-		protected virtual void Close ()
-		{
-		}
-    }
 }
diff --git a/src/Clients/MainApp/MainApp.csproj b/src/Clients/MainApp/MainApp.csproj
index d04e018..fe296fd 100644
--- a/src/Clients/MainApp/MainApp.csproj
+++ b/src/Clients/MainApp/MainApp.csproj
@@ -208,6 +208,7 @@
     <Compile Include="Pinta\CairoExtensions.cs" />
     <Compile Include="Pinta\GaussianBlurEffect.cs" />
     <Compile Include="FSpot\Literal.cs" />
+    <Compile Include="FSpot.Imaging\IImageFile.cs" />
   </ItemGroup>
   <ItemGroup>
     <EmbeddedResource Include="..\..\..\COPYING">
diff --git a/src/Clients/MainApp/Makefile.am b/src/Clients/MainApp/Makefile.am
index 84f31e9..50bed0a 100644
--- a/src/Clients/MainApp/Makefile.am
+++ b/src/Clients/MainApp/Makefile.am
@@ -51,6 +51,7 @@ SOURCES =  \
 	FSpot.Filters/WhiteListFilter.cs \
 	FSpot.Imaging/Ciff.cs \
 	FSpot.Imaging/DCRawFile.cs \
+	FSpot.Imaging/IImageFile.cs \
 	FSpot.Imaging/ImageFile.cs \
 	FSpot.Imaging/InternalProcess.cs \
 	FSpot.Imaging/IOChannel.cs \



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