2 // ImageCodecInfo class testing unit
5 // Jordi Mas i Hernàndez (jordi@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Drawing.Imaging;
34 using NUnit.Framework;
35 using System.Collections;
36 using System.Security.Permissions;
37 using System.Text.RegularExpressions;
39 namespace MonoTests.System.Drawing.Imaging {
42 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43 public class ImageCodecInfoTest {
48 ImageCodecInfo GetEncoder (Guid clsid)
50 return (ImageCodecInfo) encoders [clsid];
53 ImageCodecInfo GetDecoder (Guid clsid) {
54 return (ImageCodecInfo) decoders [clsid];
58 public void FixtureGetReady()
60 decoders = new Hashtable ();
61 encoders = new Hashtable ();
63 foreach (ImageCodecInfo decoder in ImageCodecInfo.GetImageDecoders ())
64 decoders[decoder.Clsid] = decoder;
66 foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders ())
67 encoders[encoder.Clsid] = encoder;
70 static void Check (ImageCodecInfo e, ImageCodecInfo d, Guid FormatID, string CodecName, string DllName,
71 string FilenameExtension, ImageCodecFlags Flags, string FormatDescription,
72 string MimeType, int Version, int signatureLength, string mask, string pattern, string pattern2)
74 Regex extRegex = new Regex (@"^(\*\.\w+(;(\*\.\w+))*;)?"+
75 Regex.Escape (FilenameExtension)+@"(;\*\.\w+(;(\*\.\w+))*)?$",
76 RegexOptions.IgnoreCase | RegexOptions.Singleline);
79 Assert.AreEqual (FormatID, e.FormatID, "Encoder.FormatID");
80 Assert.IsTrue (e.CodecName.IndexOf (CodecName)>=0,
81 "Encoder.CodecName contains "+CodecName);
82 Assert.AreEqual (DllName, e.DllName, "Encoder.DllName");
83 Assert.IsTrue (extRegex.IsMatch (e.FilenameExtension),
84 "Encoder.FilenameExtension is a right list with "+FilenameExtension);
85 Assert.AreEqual (Flags, e.Flags, "Encoder.Flags");
86 Assert.IsTrue (e.FormatDescription.IndexOf (FormatDescription)>=0,
87 "Encoder.FormatDescription contains "+FormatDescription);
88 Assert.IsTrue (e.MimeType.IndexOf (MimeType)>=0,
89 "Encoder.MimeType contains "+MimeType);
91 Assert.AreEqual (signatureLength, e.SignatureMasks.Length, "Encoder.SignatureMasks.Length");
92 for (int i = 0; i < signatureLength; i++) {
93 Assert.AreEqual (mask, BitConverter.ToString (e.SignatureMasks[i]), String.Format ("Encoder.SignatureMasks[{0}]", i));
95 Assert.AreEqual (signatureLength, e.SignaturePatterns.Length, "Encoder.SignaturePatterns.Length");
96 Assert.AreEqual (pattern, BitConverter.ToString (e.SignaturePatterns[0]), "Encoder.SignaturePatterns[0]");
98 Assert.AreEqual (pattern2, BitConverter.ToString (e.SignaturePatterns[1]), "Encoder.SignaturePatterns[1]");
101 Assert.AreEqual (FormatID, d.FormatID, "Decoder.FormatID");
102 Assert.IsTrue (d.CodecName.IndexOf (CodecName)>=0,
103 "Decoder.CodecName contains "+CodecName);
104 Assert.AreEqual (DllName, d.DllName, "Decoder.DllName");
105 Assert.IsTrue (extRegex.IsMatch (d.FilenameExtension),
106 "Decoder.FilenameExtension is a right list with "+FilenameExtension);
107 Assert.AreEqual (Flags, d.Flags, "Decoder.Flags");
108 Assert.IsTrue (d.FormatDescription.IndexOf (FormatDescription)>=0,
109 "Decoder.FormatDescription contains "+FormatDescription);
110 Assert.IsTrue (d.MimeType.IndexOf (MimeType)>=0,
111 "Decoder.MimeType contains "+MimeType);
113 Assert.AreEqual (signatureLength, d.SignatureMasks.Length, "Decoder.SignatureMasks.Length");
114 for (int i = 0; i < signatureLength; i++) {
115 Assert.AreEqual (mask, BitConverter.ToString (d.SignatureMasks[i]), String.Format ("Decoder.SignatureMasks[{0}]", i));
117 Assert.AreEqual (signatureLength, d.SignaturePatterns.Length, "Decoder.SignaturePatterns.Length");
118 Assert.AreEqual (pattern, BitConverter.ToString (d.SignaturePatterns[0]), "Decoder.SignaturePatterns[0]");
119 if (pattern2 != null)
120 Assert.AreEqual (pattern2, BitConverter.ToString (d.SignaturePatterns[1]), "Decoder.SignaturePatterns[1]");
125 public void Decoders ()
127 Assert.AreEqual (8, decoders.Count, "Count");
128 foreach (DictionaryEntry de in decoders) {
129 string guid = de.Key.ToString ();
131 case "557cf402-1a04-11d3-9a73-0000f81ef32e": // GIF
132 case "557cf403-1a04-11d3-9a73-0000f81ef32e": // EMF
133 case "557cf400-1a04-11d3-9a73-0000f81ef32e": // BMP/DIB/RLE
134 case "557cf401-1a04-11d3-9a73-0000f81ef32e": // JPG,JPEG,JPE,JFIF
135 case "557cf406-1a04-11d3-9a73-0000f81ef32e": // PNG
136 case "557cf407-1a04-11d3-9a73-0000f81ef32e": // ICO
137 case "557cf404-1a04-11d3-9a73-0000f81ef32e": // WMF
138 case "557cf405-1a04-11d3-9a73-0000f81ef32e": // TIF,TIFF
141 Assert.Ignore ("Unknown decoder " + guid);
148 public void Encoders ()
150 Assert.AreEqual (5, encoders.Count, "Count");
151 foreach (DictionaryEntry de in encoders) {
152 string guid = de.Key.ToString ();
154 case "557cf402-1a04-11d3-9a73-0000f81ef32e": // GIF
155 case "557cf400-1a04-11d3-9a73-0000f81ef32e": // BMP/DIB/RLE
156 case "557cf401-1a04-11d3-9a73-0000f81ef32e": // JPG,JPEG,JPE,JFIF
157 case "557cf406-1a04-11d3-9a73-0000f81ef32e": // PNG
158 case "557cf405-1a04-11d3-9a73-0000f81ef32e": // TIF,TIFF
161 Assert.Ignore ("Unknown encoder " + guid);
168 public void BMPCodec()
170 Guid g = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
171 Check (GetEncoder (g), GetDecoder (g), ImageFormat.Bmp.Guid,
172 "BMP", null, "*.BMP",
173 ImageCodecFlags.Builtin | ImageCodecFlags.Encoder | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
174 "BMP", "image/bmp", 1, 1, "FF-FF", "42-4D", null);
178 public void GifCodec()
180 Guid g = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
181 Check (GetEncoder (g), GetDecoder (g), ImageFormat.Gif.Guid,
182 "GIF", null, "*.GIF",
183 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
184 "GIF", "image/gif", 1, 2, "FF-FF-FF-FF-FF-FF", "47-49-46-38-39-61", "47-49-46-38-37-61");
188 public void JpegCodec()
190 Guid g = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
191 Check (GetEncoder (g), GetDecoder (g), ImageFormat.Jpeg.Guid,
192 "JPEG", null, "*.JPG",
193 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
194 "JPEG", "image/jpeg", 1, 1, "FF-FF", "FF-D8", null);
198 public void PngCodec()
200 Guid g = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
201 Check (GetEncoder (g), GetDecoder (g), ImageFormat.Png.Guid,
202 "PNG", null, "*.PNG",
203 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
204 "PNG", "image/png", 1, 1, "FF-FF-FF-FF-FF-FF-FF-FF", "89-50-4E-47-0D-0A-1A-0A", null);
208 public void TiffCodec ()
210 Guid g = new Guid ("557cf405-1a04-11d3-9a73-0000f81ef32e");
211 Check (GetEncoder (g), GetDecoder (g), ImageFormat.Tiff.Guid,
212 "TIFF", null, "*.TIF;*.TIFF",
213 ImageCodecFlags.Builtin | ImageCodecFlags.Encoder | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
214 "TIFF", "image/tiff", 1, 2, "FF-FF", "49-49", "4D-4D");
218 public void IconCodec_Encoder ()
220 Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
221 Assert.IsNull (GetEncoder (g), "Encoder");
225 public void IconCodec_Decoder ()
227 Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
228 Check (null, GetDecoder (g), ImageFormat.Icon.Guid,
229 "ICO", null, "*.ICO",
230 ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
231 "ICO", "image/x-icon", 1, 1, "FF-FF-FF-FF", "00-00-01-00", null);
235 public void EmfCodec_Encoder ()
237 Guid g = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
238 Assert.IsNull (GetEncoder (g), "Encoder");
242 public void EmfCodec_Decoder ()
244 Guid g = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
245 Check (null, GetDecoder (g), ImageFormat.Emf.Guid,
246 "EMF", null, "*.EMF",
247 ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
248 "EMF", "image/x-emf", 1, 1, "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-FF-FF-FF-FF",
249 "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-20-45-4D-46", null);
253 public void WmfCodec_Encoder ()
255 Guid g = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
256 Assert.IsNull (GetEncoder (g), "Encoder");
260 public void WmfCodec_Decoder ()
262 Guid g = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
263 Check (null, GetDecoder (g), ImageFormat.Wmf.Guid,
264 "WMF", null, "*.WMF",
265 ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
266 "WMF", "image/x-wmf", 1, 1, "FF-FF-FF-FF", "D7-CD-C6-9A", null);