Index: src/ImageFile.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/ImageFile.cs,v
retrieving revision 1.32
diff -u -p -r1.32 ImageFile.cs
--- src/ImageFile.cs	22 Mar 2006 21:24:02 -0000	1.32
+++ src/ImageFile.cs	12 Jun 2006 11:23:45 -0000
@@ -11,7 +11,10 @@ namespace FSpot {
 	public class ImageFile {
 		protected Uri uri;
 
+		const int READ_BYTES = 4;
 		static System.Collections.Hashtable name_table;
+		static System.Collections.Hashtable magic_table;
+		static System.Collections.Hashtable type_table;
 
 		public ImageFile (string path) 
 		{
@@ -42,26 +45,36 @@ namespace FSpot {
 
 		static ImageFile ()
 		{
-			name_table = new System.Collections.Hashtable ();
-			name_table [".svg"] = typeof (FSpot.Svg.SvgFile);
-			name_table [".gif"] = typeof (ImageFile);
-			name_table [".jpeg"] = typeof (JpegFile);
-			name_table [".jpg"] = typeof (JpegFile);
-			name_table [".png"] = typeof (FSpot.Png.PngFile);
-			name_table [".cr2"] = typeof (FSpot.Tiff.Cr2File);
-			name_table [".nef"] = typeof (FSpot.Tiff.NefFile);
-			name_table [".pef"] = typeof (FSpot.Tiff.NefFile);
-			name_table [".raw"] = typeof (FSpot.Tiff.NefFile);
-			name_table [".tiff"] = typeof (FSpot.Tiff.TiffFile);
-			name_table [".tif"] = typeof (FSpot.Tiff.TiffFile);
-			name_table [".orf"] =  typeof (FSpot.Tiff.NefFile);
-			name_table [".srf"] = typeof (FSpot.Tiff.NefFile);
-			name_table [".dng"] = typeof (FSpot.Tiff.DngFile);
-			name_table [".crw"] = typeof (FSpot.Ciff.CiffFile);
-			name_table [".ppm"] = typeof (FSpot.Pnm.PnmFile);
-			name_table [".mrw"] = typeof (FSpot.Mrw.MrwFile);
-			name_table [".raf"] = typeof (FSpot.Raf.RafFile);
-			name_table [".x3f"] = typeof (FSpot.X3f.X3fFile);
+//			name_table = new System.Collections.Hashtable ();
+//			name_table [".svg"] = typeof (FSpot.Svg.SvgFile);
+//			name_table [".gif"] = typeof (ImageFile);
+//			name_table [".jpeg"] = typeof (JpegFile);
+//			name_table [".jpg"] = typeof (JpegFile);
+//			name_table [".png"] = typeof (FSpot.Png.PngFile);
+//			name_table [".cr2"] = typeof (FSpot.Tiff.Cr2File);
+//			name_table [".nef"] = typeof (FSpot.Tiff.NefFile);
+//			name_table [".pef"] = typeof (FSpot.Tiff.NefFile);
+//			name_table [".raw"] = typeof (FSpot.Tiff.NefFile);
+//			name_table [".tiff"] = typeof (FSpot.Tiff.TiffFile);
+//			name_table [".tif"] = typeof (FSpot.Tiff.TiffFile);
+//			name_table [".orf"] =  typeof (FSpot.Tiff.NefFile);
+//			name_table [".srf"] = typeof (FSpot.Tiff.NefFile);
+//			name_table [".dng"] = typeof (FSpot.Tiff.DngFile);
+//			name_table [".crw"] = typeof (FSpot.Ciff.CiffFile);
+//			name_table [".ppm"] = typeof (FSpot.Pnm.PnmFile);
+//			name_table [".mrw"] = typeof (FSpot.Mrw.MrwFile);
+//			name_table [".raf"] = typeof (FSpot.Raf.RafFile);
+//			name_table [".x3f"] = typeof (FSpot.X3f.X3fFile);
+			
+			magic_table = new System.Collections.Hashtable ();
+			magic_table ["jpeg"] = new byte [] {0xff, 0xd8};
+			magic_table ["png"]  = new byte [] {0x89, 0x50, 0x4e, 0x47};
+			magic_table ["gif"]  = new byte [] {0x47, 0x49, 0x46, 0x38};
+
+			type_table = new System.Collections.Hashtable();
+			type_table ["gif"] = typeof (ImageFile);
+			type_table ["jpeg"] = typeof (JpegFile);
+			type_table ["png"] = typeof (FSpot.Png.PngFile);
 		}
 
 		public Uri Uri {
@@ -147,6 +160,30 @@ namespace FSpot {
 			}
 		}
 
+		private static System.Type TypeOf (Uri uri)
+		{
+			System.IO.FileStream f = System.IO.File.OpenRead(uri.AbsolutePath);
+			byte [] b = new byte [READ_BYTES];
+			f.Read (b, 0, READ_BYTES);
+			bool found = false;
+			foreach (System.Collections.DictionaryEntry entry in magic_table) {
+				byte [] val = (byte [])(entry.Value);
+				for (int i=0; i < val.Length; i++) {
+					if (val[i] != b[i]){
+						found = false;
+						break;
+					}
+					found = true;
+				}
+				if (found){
+					f.Close();
+					return (System.Type) type_table [entry.Key];
+				}
+			}
+			f.Close();
+			return null;
+		}
+
 		public static bool HasLoader (string path)
 		{
 			return HasLoader (UriList.PathToFileUri (path));
@@ -154,9 +191,7 @@ namespace FSpot {
 		
 		public static bool HasLoader (Uri uri)
 		{
-			string path = uri.AbsolutePath;
-			string extension = System.IO.Path.GetExtension (path).ToLower ();
-			System.Type t = (System.Type) name_table [extension];
+			System.Type t = TypeOf (uri);
 			
 			return (t != null);
 		}
@@ -168,9 +203,7 @@ namespace FSpot {
 
 		public static ImageFile Create (Uri uri)
 		{
-			string path = uri.AbsolutePath;
-			string extension = System.IO.Path.GetExtension (path).ToLower ();
-			System.Type t = (System.Type) name_table [extension];
+			System.Type t = TypeOf (uri);
 			ImageFile img;
 
 			if (t != null)
