TestImageCodecInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // ImageCodecInfo class testing unit
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernàndez ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2004 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
  10. //
  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:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  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.
  29. //
  30. using System;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using NUnit.Framework;
  34. using System.Collections;
  35. using System.Security.Permissions;
  36. using System.Text.RegularExpressions;
  37. namespace MonoTests.System.Drawing.Imaging {
  38. [TestFixture]
  39. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  40. public class ImageCodecInfoTest {
  41. Hashtable decoders;
  42. Hashtable encoders;
  43. ImageCodecInfo GetEncoder (Guid clsid)
  44. {
  45. return (ImageCodecInfo) encoders [clsid];
  46. }
  47. ImageCodecInfo GetDecoder (Guid clsid) {
  48. return (ImageCodecInfo) decoders [clsid];
  49. }
  50. [TestFixtureSetUp]
  51. public void FixtureGetReady()
  52. {
  53. decoders = new Hashtable ();
  54. encoders = new Hashtable ();
  55. foreach (ImageCodecInfo decoder in ImageCodecInfo.GetImageDecoders ())
  56. decoders[decoder.Clsid] = decoder;
  57. foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders ())
  58. encoders[encoder.Clsid] = encoder;
  59. }
  60. static void Check (ImageCodecInfo e, ImageCodecInfo d, Guid FormatID, string CodecName, string DllName,
  61. string FilenameExtension, ImageCodecFlags Flags, string FormatDescription,
  62. string MimeType, int Version, int signatureLength, string mask, string pattern, string pattern2)
  63. {
  64. Regex extRegex = new Regex (@"^(\*\.\w+(;(\*\.\w+))*;)?"+
  65. Regex.Escape (FilenameExtension)+@"(;\*\.\w+(;(\*\.\w+))*)?$",
  66. RegexOptions.IgnoreCase | RegexOptions.Singleline);
  67. if (e != null) {
  68. Assert.AreEqual (FormatID, e.FormatID, "Encoder.FormatID");
  69. Assert.IsTrue (e.CodecName.IndexOf (CodecName)>=0,
  70. "Encoder.CodecName contains "+CodecName);
  71. Assert.AreEqual (DllName, e.DllName, "Encoder.DllName");
  72. Assert.IsTrue (extRegex.IsMatch (e.FilenameExtension),
  73. "Encoder.FilenameExtension is a right list with "+FilenameExtension);
  74. Assert.AreEqual (Flags, e.Flags, "Encoder.Flags");
  75. Assert.IsTrue (e.FormatDescription.IndexOf (FormatDescription)>=0,
  76. "Encoder.FormatDescription contains "+FormatDescription);
  77. Assert.IsTrue (e.MimeType.IndexOf (MimeType)>=0,
  78. "Encoder.MimeType contains "+MimeType);
  79. Assert.AreEqual (signatureLength, e.SignatureMasks.Length, "Encoder.SignatureMasks.Length");
  80. for (int i = 0; i < signatureLength; i++) {
  81. Assert.AreEqual (mask, BitConverter.ToString (e.SignatureMasks[i]), String.Format ("Encoder.SignatureMasks[{0}]", i));
  82. }
  83. Assert.AreEqual (signatureLength, e.SignaturePatterns.Length, "Encoder.SignaturePatterns.Length");
  84. Assert.AreEqual (pattern, BitConverter.ToString (e.SignaturePatterns[0]), "Encoder.SignaturePatterns[0]");
  85. if (pattern2 != null)
  86. Assert.AreEqual (pattern2, BitConverter.ToString (e.SignaturePatterns[1]), "Encoder.SignaturePatterns[1]");
  87. }
  88. if (d != null) {
  89. Assert.AreEqual (FormatID, d.FormatID, "Decoder.FormatID");
  90. Assert.IsTrue (d.CodecName.IndexOf (CodecName)>=0,
  91. "Decoder.CodecName contains "+CodecName);
  92. Assert.AreEqual (DllName, d.DllName, "Decoder.DllName");
  93. Assert.IsTrue (extRegex.IsMatch (d.FilenameExtension),
  94. "Decoder.FilenameExtension is a right list with "+FilenameExtension);
  95. Assert.AreEqual (Flags, d.Flags, "Decoder.Flags");
  96. Assert.IsTrue (d.FormatDescription.IndexOf (FormatDescription)>=0,
  97. "Decoder.FormatDescription contains "+FormatDescription);
  98. Assert.IsTrue (d.MimeType.IndexOf (MimeType)>=0,
  99. "Decoder.MimeType contains "+MimeType);
  100. Assert.AreEqual (signatureLength, d.SignatureMasks.Length, "Decoder.SignatureMasks.Length");
  101. for (int i = 0; i < signatureLength; i++) {
  102. Assert.AreEqual (mask, BitConverter.ToString (d.SignatureMasks[i]), String.Format ("Decoder.SignatureMasks[{0}]", i));
  103. }
  104. Assert.AreEqual (signatureLength, d.SignaturePatterns.Length, "Decoder.SignaturePatterns.Length");
  105. Assert.AreEqual (pattern, BitConverter.ToString (d.SignaturePatterns[0]), "Decoder.SignaturePatterns[0]");
  106. if (pattern2 != null)
  107. Assert.AreEqual (pattern2, BitConverter.ToString (d.SignaturePatterns[1]), "Decoder.SignaturePatterns[1]");
  108. }
  109. }
  110. [Test]
  111. #if TARGET_JVM
  112. [Category ("NotWorking")]
  113. #endif
  114. public void Decoders ()
  115. {
  116. Assert.AreEqual (8, decoders.Count, "Count");
  117. foreach (DictionaryEntry de in decoders) {
  118. string guid = de.Key.ToString ();
  119. switch (guid) {
  120. case "557cf402-1a04-11d3-9a73-0000f81ef32e": // GIF
  121. case "557cf403-1a04-11d3-9a73-0000f81ef32e": // EMF
  122. case "557cf400-1a04-11d3-9a73-0000f81ef32e": // BMP/DIB/RLE
  123. case "557cf401-1a04-11d3-9a73-0000f81ef32e": // JPG,JPEG,JPE,JFIF
  124. case "557cf406-1a04-11d3-9a73-0000f81ef32e": // PNG
  125. case "557cf407-1a04-11d3-9a73-0000f81ef32e": // ICO
  126. case "557cf404-1a04-11d3-9a73-0000f81ef32e": // WMF
  127. case "557cf405-1a04-11d3-9a73-0000f81ef32e": // TIF,TIFF
  128. break;
  129. default:
  130. Assert.Ignore ("Unknown decoder " + guid);
  131. break;
  132. }
  133. }
  134. }
  135. [Test]
  136. #if TARGET_JVM
  137. [Category ("NotWorking")]
  138. #endif
  139. public void Encoders ()
  140. {
  141. Assert.AreEqual (5, encoders.Count, "Count");
  142. foreach (DictionaryEntry de in encoders) {
  143. string guid = de.Key.ToString ();
  144. switch (guid) {
  145. case "557cf402-1a04-11d3-9a73-0000f81ef32e": // GIF
  146. case "557cf400-1a04-11d3-9a73-0000f81ef32e": // BMP/DIB/RLE
  147. case "557cf401-1a04-11d3-9a73-0000f81ef32e": // JPG,JPEG,JPE,JFIF
  148. case "557cf406-1a04-11d3-9a73-0000f81ef32e": // PNG
  149. case "557cf405-1a04-11d3-9a73-0000f81ef32e": // TIF,TIFF
  150. break;
  151. default:
  152. Assert.Ignore ("Unknown encoder " + guid);
  153. break;
  154. }
  155. }
  156. }
  157. [Test]
  158. #if TARGET_JVM
  159. [Category ("NotWorking")]
  160. #endif
  161. public void BMPCodec()
  162. {
  163. Guid g = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
  164. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Bmp.Guid,
  165. "BMP", null, "*.BMP",
  166. ImageCodecFlags.Builtin | ImageCodecFlags.Encoder | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
  167. "BMP", "image/bmp", 1, 1, "FF-FF", "42-4D", null);
  168. }
  169. [Test]
  170. #if TARGET_JVM
  171. [Category ("NotWorking")]
  172. #endif
  173. public void GifCodec()
  174. {
  175. Guid g = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
  176. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Gif.Guid,
  177. "GIF", null, "*.GIF",
  178. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  179. "GIF", "image/gif", 1, 2, "FF-FF-FF-FF-FF-FF", "47-49-46-38-39-61", "47-49-46-38-37-61");
  180. }
  181. [Test]
  182. #if TARGET_JVM
  183. [Category ("NotWorking")]
  184. #endif
  185. public void JpegCodec()
  186. {
  187. Guid g = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
  188. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Jpeg.Guid,
  189. "JPEG", null, "*.JPG",
  190. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  191. "JPEG", "image/jpeg", 1, 1, "FF-FF", "FF-D8", null);
  192. }
  193. [Test]
  194. #if TARGET_JVM
  195. [Category ("NotWorking")]
  196. #endif
  197. public void PngCodec()
  198. {
  199. Guid g = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
  200. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Png.Guid,
  201. "PNG", null, "*.PNG",
  202. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  203. "PNG", "image/png", 1, 1, "FF-FF-FF-FF-FF-FF-FF-FF", "89-50-4E-47-0D-0A-1A-0A", null);
  204. }
  205. [Test]
  206. #if TARGET_JVM
  207. [Category ("NotWorking")]
  208. #endif
  209. public void IconCodec_Encoder ()
  210. {
  211. Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  212. Assert.IsNull (GetEncoder (g), "Encoder");
  213. }
  214. [Test]
  215. #if TARGET_JVM
  216. [Category ("NotWorking")]
  217. #endif
  218. public void IconCodec_Decoder ()
  219. {
  220. Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  221. Check (null, GetDecoder (g), ImageFormat.Icon.Guid,
  222. "ICO", null, "*.ICO",
  223. ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
  224. "ICO", "image/x-icon", 1, 1, "FF-FF-FF-FF", "00-00-01-00", null);
  225. }
  226. [Test]
  227. #if TARGET_JVM
  228. [Category ("NotWorking")]
  229. #endif
  230. public void EmfCodec_Encoder ()
  231. {
  232. Guid g = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
  233. Assert.IsNull (GetEncoder (g), "Encoder");
  234. }
  235. [Test]
  236. #if TARGET_JVM
  237. [Category ("NotWorking")]
  238. #endif
  239. public void EmfCodec_Decoder ()
  240. {
  241. Guid g = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
  242. Check (null, GetDecoder (g), ImageFormat.Emf.Guid,
  243. "EMF", null, "*.EMF",
  244. ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
  245. "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",
  246. "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);
  247. }
  248. [Test]
  249. #if TARGET_JVM
  250. [Category ("NotWorking")]
  251. #endif
  252. public void WmfCodec_Encoder ()
  253. {
  254. Guid g = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
  255. Assert.IsNull (GetEncoder (g), "Encoder");
  256. }
  257. [Test]
  258. #if TARGET_JVM
  259. [Category ("NotWorking")]
  260. #endif
  261. public void WmfCodec_Decoder ()
  262. {
  263. Guid g = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
  264. Check (null, GetDecoder (g), ImageFormat.Wmf.Guid,
  265. "WMF", null, "*.WMF",
  266. ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
  267. "WMF", "image/x-wmf", 1, 1, "FF-FF-FF-FF", "D7-CD-C6-9A", null);
  268. }
  269. }
  270. }