2010-05-27 William Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing.Imaging / ImageCodec.jvm.cs
index dae47121a932d5e57367ab1c457e69d6be73e676..8ded371ba7523563123e6639120dba51be139e9b 100644 (file)
@@ -93,7 +93,7 @@ namespace Mainsoft.Drawing.Imaging {
                public static ImageCodec CreateReader(Guid clsid) {\r
                        ImageCodec codec = null;\r
                        try {\r
-                               ImageCodecInfo codecInfo = (ImageCodecInfo) Decoders[clsid];\r
+                               ImageCodecInfo codecInfo = FindDecoder(clsid);\r
                                java.util.Iterator iter = imageio.ImageIO.getImageReadersByMIMEType( codecInfo.MimeType );\r
                                codec = CreateReader(iter);\r
                        }\r
@@ -124,7 +124,7 @@ namespace Mainsoft.Drawing.Imaging {
                public static ImageCodec CreateWriter(Guid clsid) {\r
                        ImageCodec codec = null;\r
                        try {\r
-                               ImageCodecInfo codecInfo = (ImageCodecInfo) Encoders[clsid];\r
+                               ImageCodecInfo codecInfo = FindEncoder(clsid);\r
                                java.util.Iterator iter = imageio.ImageIO.getImageWritersByMIMEType( codecInfo.MimeType );\r
                                codec = CreateWriter(iter);\r
                        }\r
@@ -177,11 +177,22 @@ namespace Mainsoft.Drawing.Imaging {
                }
 
                internal static ImageCodecInfo FindEncoder (Guid clsid) {
-                       return (ImageCodecInfo) Encoders[clsid];
+                       ImageCodecInfo codec = (ImageCodecInfo) Encoders[clsid];
+                       if (codec == null) {
+                               // .net saves in png if cannot find requested encoder. atc id 316563
+                               codec = (ImageCodecInfo) Encoders[ ImageCodec.PngClsid ];
+                       }
+                       return codec;
                }
 
                internal static ImageCodecInfo FindDecoder (Guid clsid) {
-                       return (ImageCodecInfo) Decoders[clsid];
+                       ImageCodecInfo codec = (ImageCodecInfo) Decoders[clsid];
+                       if (codec == null) {\r
+                               ImageFormat format = ClsidToImageFormat(clsid);\r
+                               string name = (format != null) ? format.ToString() : clsid.ToString();\r
+                               throw new NotSupportedException(String.Format("The '{0}' format decoder is not installed.", name));\r
+                       }\r
+                       return codec;\r
                }
 \r
                #endregion\r
@@ -198,35 +209,39 @@ namespace Mainsoft.Drawing.Imaging {
                                ici.Clsid = clsid;
                                ici.FormatID = formatID;
                                ici.MimeType = mimeType;
-                               java.util.Iterator iter = GetIterator (mimeType);
+                               java.util.Iterator iter = null;
+                               try {
+                                       iter = GetIterator (mimeType);
+                               }
+                               catch(Exception) {
+                                       return null;
+                               }
                                while (iter.hasNext ()) {
                                        spi.ImageReaderWriterSpi rw = GetNext (iter);
+
+                                       ici.CodecName = rw.getDescription (java.util.Locale.getDefault ());
+                                       //ici.DllName = null;
+                                       foreach (string suffix in rw.getFileSuffixes ()) {
+                                               if (ici.FilenameExtension != null)
+                                                       ici.FilenameExtension += ";";
+                                               ici.FilenameExtension += "*."+suffix;
+                                       }
+                                       ici.Flags = ImageCodecFlags.Builtin|ImageCodecFlags.SupportBitmap;
+                                       if (rw is spi.ImageReaderSpi)
+                                               ici.Flags |= ImageCodecFlags.Decoder;
+
+                                       if (rw is spi.ImageWriterSpi)
+                                               ici.Flags |= ImageCodecFlags.Encoder;
+
+                                       ici.FormatDescription = string.Join(";",
+                                               rw.getFormatNames());
                                        try {
-                                               ici.CodecName = rw.getDescription (java.util.Locale.getDefault ());
-                                               ici.DllName = null;
-                                               foreach (string suffix in rw.getFileSuffixes ()) {
-                                                       if (ici.FilenameExtension != null)
-                                                               ici.FilenameExtension += ";";
-                                                       ici.FilenameExtension += "*."+suffix;
-                                               }
-                                               ici.Flags = ImageCodecFlags.Builtin|ImageCodecFlags.SupportBitmap;
-                                               if (rw is spi.ImageReaderSpi) {
-                                                       ici.Flags |= ImageCodecFlags.Decoder;
-                                                       if ((rw as spi.ImageReaderSpi).getImageWriterSpiNames().Length != 0)
-                                                               ici.Flags |= ImageCodecFlags.Encoder;
-                                               }
-                                               if (rw is spi.ImageWriterSpi) {
-                                                       ici.Flags |= ImageCodecFlags.Encoder;
-                                                       if ((rw as spi.ImageWriterSpi).getImageReaderSpiNames().Length != 0)
-                                                               ici.Flags |= ImageCodecFlags.Decoder;
-                                               }
-                                               ici.FormatDescription = string.Join(";",
-                                                       rw.getFormatNames());
                                                ici.Version = (int)Convert.ToDouble(rw.getVersion ());
-                                               break;
                                        }
-                                       catch {
+                                       catch (Exception) {
+                                               ici.Version = 1;
                                        }
+                                       break;
                                }
                                return ici;
                        }
@@ -234,15 +249,15 @@ namespace Mainsoft.Drawing.Imaging {
 
                        internal Hashtable Iterate () {
                                // TBD: Insert Exception handling here
-                               NameValueCollection nvc = (NameValueCollection) System.Configuration.ConfigurationSettings
-                                       .GetConfig ("system.drawing/codecs");
+                               NameValueCollection nvc = (NameValueCollection) System.Configuration.ConfigurationSettings\r
+                                       .GetConfig ("mainsoft.drawing/codecs");
                                Hashtable codecs = new Hashtable (10);
                        
                                for (int i=0; i<nvc.Count; i++) {
                                        Guid clsid = new Guid (nvc.GetKey (i));
                                        ImageFormat format = ClsidToImageFormat (clsid);
                                        ImageCodecInfo codec = ProcessOneCodec (clsid, format.Guid, nvc[i]);
-                                       if (codec.FilenameExtension != null)
+                                       if ((codec != null) && (codec.FilenameExtension != null))
                                                codecs [clsid] = codec;
                                }
                                return codecs;
@@ -282,9 +297,9 @@ namespace Mainsoft.Drawing.Imaging {
 
                private static ImageFormat MimeTypesToImageFormat (string [] mimeTypes) {
                        foreach (ImageCodecInfo codec in Decoders.Values)
-                               for (int i=0; i<mimeTypes.Length; i++)
-                                       if (codec.MimeType == mimeTypes [i])
-                                               return new ImageFormat (codec.FormatID);
+                               for (int i=0; i<mimeTypes.Length; i++)\r
+                                       if (codec.MimeType == mimeTypes [i])\r
+                                               return ClsidToImageFormat (codec.Clsid);\r
                        return null;
                }
 
@@ -363,7 +378,7 @@ namespace Mainsoft.Drawing.Imaging {
                \r
                #region Image read/write methods\r
 \r
-               public PlainImage ReadPlainImage() {\r
+               internal PlainImage ReadPlainImage() {\r
                        awt.Image img = ReadImage( _currentFrame );\r
                        if (img == null)\r
                                return null;\r
@@ -392,7 +407,7 @@ namespace Mainsoft.Drawing.Imaging {
                        return pi;\r
                }\r
 \r
-               public PlainImage ReadNextPlainImage() {\r
+               internal PlainImage ReadNextPlainImage() {\r
                        _currentFrame++;\r
                        return ReadPlainImage();\r
                }\r
@@ -435,7 +450,7 @@ namespace Mainsoft.Drawing.Imaging {
                        }
                }\r
 #endif\r
-               public void WritePlainImage(PlainImageCollection pic) {\r
+               internal void WritePlainImage(PlainImageCollection pic) {\r
                        if ((pic == null) || (pic.Count == 0))\r
                                return;\r
 \r
@@ -461,7 +476,7 @@ namespace Mainsoft.Drawing.Imaging {
                        }
                }\r
 \r
-               public void WritePlainImage(PlainImage pi) {\r
+               internal void WritePlainImage(PlainImage pi) {\r
                        try {\r
                                imageio.IIOImage iio = GetIIOImageContainer( pi );\r
                                WriteImage( iio );\r
@@ -476,7 +491,6 @@ namespace Mainsoft.Drawing.Imaging {
                                throw new Exception("Output stream not specified");\r
 \r
                        NativeWriter.write( iio );\r
-                       NativeStream.flush();\r
                }\r
                \r
                private imageio.IIOImage GetIIOImageContainer(PlainImage pi) {\r
@@ -531,8 +545,8 @@ namespace Mainsoft.Drawing.Imaging {
                                return new float[]{0, 0};\r
 \r
                        ResolutionConfigurationCollection rcc = 
-                               (ResolutionConfigurationCollection)
-                               ConfigurationSettings.GetConfig("system.drawing/codecsmetadata");
+                               (ResolutionConfigurationCollection)\r
+                               ConfigurationSettings.GetConfig ("mainsoft.drawing/codecsmetadata");
 
                        if (rcc == null)
                                throw new ConfigurationException("Configuration section codecsmetadata not found");