IconCodecTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // ICO Codec class testing unit
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernàndez ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Drawing;
  31. using System.Drawing.Imaging;
  32. using System.IO;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Drawing.Imaging {
  37. [TestFixture]
  38. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  39. public class IconCodecTest {
  40. /* Get suffix to add to the filename */
  41. internal string getOutSufix ()
  42. {
  43. string s;
  44. int p = (int) Environment.OSVersion.Platform;
  45. if ((p == 4) || (p == 128))
  46. s = "-unix";
  47. else
  48. s = "-windows";
  49. if (Type.GetType ("Mono.Runtime", false) == null)
  50. s += "-msnet";
  51. else
  52. s += "-mono";
  53. return s;
  54. }
  55. /* Get the input directory depending on the runtime*/
  56. internal string getInFile (string file)
  57. {
  58. string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
  59. if (!File.Exists (sRslt))
  60. sRslt = "Test/System.Drawing/" + file;
  61. return sRslt;
  62. }
  63. /* Checks bitmap features on a know 1bbp bitmap */
  64. [Test]
  65. [Ignore ("ICON support is missing")]
  66. public void Bitmap16Features ()
  67. {
  68. string sInFile = getInFile ("bitmaps/smiley.ico");
  69. using (Bitmap bmp = new Bitmap (sInFile)) {
  70. GraphicsUnit unit = GraphicsUnit.World;
  71. RectangleF rect = bmp.GetBounds (ref unit);
  72. // ??? why is it a 4bbp ?
  73. Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
  74. Assert.AreEqual (16, bmp.Width, "bmp.Width");
  75. Assert.AreEqual (16, bmp.Height, "bmp.Height");
  76. Assert.AreEqual (0, rect.X, "rect.X");
  77. Assert.AreEqual (0, rect.Y, "rect.Y");
  78. Assert.AreEqual (16, rect.Width, "rect.Width");
  79. Assert.AreEqual (16, rect.Height, "rect.Height");
  80. Assert.AreEqual (16, bmp.Size.Width, "bmp.Size.Width");
  81. Assert.AreEqual (16, bmp.Size.Height, "bmp.Size.Height");
  82. }
  83. }
  84. [Test]
  85. [Ignore ("ICON support is missing")]
  86. public void Bitmap16Pixels ()
  87. {
  88. string sInFile = getInFile ("bitmaps/smiley.ico");
  89. using (Bitmap bmp = new Bitmap (sInFile)) {
  90. #if false
  91. for (int x = 0; x < bmp.Width; x += 4) {
  92. for (int y = 0; y < bmp.Height; y += 4)
  93. Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  94. }
  95. #else
  96. // sampling values from a well known bitmap
  97. Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
  98. Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
  99. Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
  100. Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
  101. Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
  102. Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
  103. Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
  104. Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
  105. Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
  106. Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
  107. Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
  108. Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
  109. Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
  110. Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
  111. Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
  112. Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
  113. #endif
  114. }
  115. }
  116. [Test]
  117. [Category ("NotWorking")]
  118. [Ignore ("ICON support is missing")]
  119. public void Bitmat16Data ()
  120. {
  121. string sInFile = getInFile ("bitmaps/smiley.ico");
  122. using (Bitmap bmp = new Bitmap (sInFile)) {
  123. BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  124. try {
  125. Assert.AreEqual (bmp.Height, data.Height, "Height");
  126. Assert.AreEqual (bmp.Width, data.Width, "Width");
  127. Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
  128. int size = data.Height * data.Stride;
  129. unsafe {
  130. byte* scan = (byte*) data.Scan0;
  131. #if false
  132. // 13 is prime (so we're not affected by a recurring pattern)
  133. for (int p = 0; p < size; p += 13) {
  134. Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
  135. }
  136. #else
  137. // sampling values from a well known bitmap
  138. Assert.AreEqual (0, *(scan + 0), "0");
  139. Assert.AreEqual (0, *(scan + 13), "13");
  140. Assert.AreEqual (0, *(scan + 26), "26");
  141. Assert.AreEqual (0, *(scan + 39), "39");
  142. Assert.AreEqual (0, *(scan + 52), "52");
  143. Assert.AreEqual (0, *(scan + 65), "65");
  144. Assert.AreEqual (0, *(scan + 78), "78");
  145. Assert.AreEqual (0, *(scan + 91), "91");
  146. Assert.AreEqual (0, *(scan + 104), "104");
  147. Assert.AreEqual (0, *(scan + 117), "117");
  148. Assert.AreEqual (0, *(scan + 130), "130");
  149. Assert.AreEqual (0, *(scan + 143), "143");
  150. Assert.AreEqual (0, *(scan + 156), "156");
  151. Assert.AreEqual (255, *(scan + 169), "169");
  152. Assert.AreEqual (0, *(scan + 182), "182");
  153. Assert.AreEqual (0, *(scan + 195), "195");
  154. Assert.AreEqual (255, *(scan + 208), "208");
  155. Assert.AreEqual (255, *(scan + 221), "221");
  156. Assert.AreEqual (0, *(scan + 234), "234");
  157. Assert.AreEqual (128, *(scan + 247), "247");
  158. Assert.AreEqual (0, *(scan + 260), "260");
  159. Assert.AreEqual (0, *(scan + 273), "273");
  160. Assert.AreEqual (0, *(scan + 286), "286");
  161. Assert.AreEqual (255, *(scan + 299), "299");
  162. Assert.AreEqual (0, *(scan + 312), "312");
  163. Assert.AreEqual (128, *(scan + 325), "325");
  164. Assert.AreEqual (0, *(scan + 338), "338");
  165. Assert.AreEqual (0, *(scan + 351), "351");
  166. Assert.AreEqual (255, *(scan + 364), "364");
  167. Assert.AreEqual (0, *(scan + 377), "377");
  168. Assert.AreEqual (0, *(scan + 390), "390");
  169. Assert.AreEqual (255, *(scan + 403), "403");
  170. Assert.AreEqual (255, *(scan + 416), "416");
  171. Assert.AreEqual (0, *(scan + 429), "429");
  172. Assert.AreEqual (255, *(scan + 442), "442");
  173. Assert.AreEqual (0, *(scan + 455), "455");
  174. Assert.AreEqual (0, *(scan + 468), "468");
  175. Assert.AreEqual (0, *(scan + 481), "481");
  176. Assert.AreEqual (255, *(scan + 494), "494");
  177. Assert.AreEqual (0, *(scan + 507), "507");
  178. Assert.AreEqual (0, *(scan + 520), "520");
  179. Assert.AreEqual (0, *(scan + 533), "533");
  180. Assert.AreEqual (0, *(scan + 546), "546");
  181. Assert.AreEqual (255, *(scan + 559), "559");
  182. Assert.AreEqual (0, *(scan + 572), "572");
  183. Assert.AreEqual (0, *(scan + 585), "585");
  184. Assert.AreEqual (255, *(scan + 598), "598");
  185. Assert.AreEqual (0, *(scan + 611), "611");
  186. Assert.AreEqual (0, *(scan + 624), "624");
  187. Assert.AreEqual (0, *(scan + 637), "637");
  188. Assert.AreEqual (128, *(scan + 650), "650");
  189. Assert.AreEqual (0, *(scan + 663), "663");
  190. Assert.AreEqual (0, *(scan + 676), "676");
  191. Assert.AreEqual (0, *(scan + 689), "689");
  192. Assert.AreEqual (0, *(scan + 702), "702");
  193. Assert.AreEqual (0, *(scan + 715), "715");
  194. Assert.AreEqual (0, *(scan + 728), "728");
  195. Assert.AreEqual (0, *(scan + 741), "741");
  196. Assert.AreEqual (0, *(scan + 754), "754");
  197. Assert.AreEqual (0, *(scan + 767), "767");
  198. #endif
  199. }
  200. }
  201. finally {
  202. bmp.UnlockBits (data);
  203. }
  204. }
  205. }
  206. /* Checks bitmap features on a know 1bbp bitmap */
  207. [Test]
  208. [Ignore ("ICON support is missing")]
  209. public void Bitmap32Features ()
  210. {
  211. string sInFile = getInFile ("bitmaps/VisualPng.ico");
  212. using (Bitmap bmp = new Bitmap (sInFile)) {
  213. GraphicsUnit unit = GraphicsUnit.World;
  214. RectangleF rect = bmp.GetBounds (ref unit);
  215. // ??? why is it a 4bbp ?
  216. Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
  217. Assert.AreEqual (32, bmp.Width, "bmp.Width");
  218. Assert.AreEqual (32, bmp.Height, "bmp.Height");
  219. Assert.AreEqual (0, rect.X, "rect.X");
  220. Assert.AreEqual (0, rect.Y, "rect.Y");
  221. Assert.AreEqual (32, rect.Width, "rect.Width");
  222. Assert.AreEqual (32, rect.Height, "rect.Height");
  223. Assert.AreEqual (32, bmp.Size.Width, "bmp.Size.Width");
  224. Assert.AreEqual (32, bmp.Size.Height, "bmp.Size.Height");
  225. }
  226. }
  227. [Test]
  228. [Ignore ("ICON support is missing")]
  229. public void Bitmap32Pixels ()
  230. {
  231. string sInFile = getInFile ("bitmaps/smiley.ico");
  232. using (Bitmap bmp = new Bitmap (sInFile)) {
  233. #if false
  234. for (int x = 0; x < bmp.Width; x += 4) {
  235. for (int y = 0; y < bmp.Height; y += 4)
  236. Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  237. }
  238. #else
  239. // sampling values from a well known bitmap
  240. Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
  241. Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
  242. Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
  243. Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
  244. Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
  245. Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
  246. Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
  247. Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
  248. Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
  249. Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
  250. Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
  251. Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
  252. Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
  253. Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
  254. Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
  255. Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
  256. #endif
  257. }
  258. }
  259. [Test]
  260. [Category ("NotWorking")]
  261. [Ignore ("ICON support is missing")]
  262. public void Bitmat32Data ()
  263. {
  264. string sInFile = getInFile ("bitmaps/smiley.ico");
  265. using (Bitmap bmp = new Bitmap (sInFile)) {
  266. BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  267. try {
  268. Assert.AreEqual (bmp.Height, data.Height, "Height");
  269. Assert.AreEqual (bmp.Width, data.Width, "Width");
  270. Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
  271. int size = data.Height * data.Stride;
  272. unsafe {
  273. byte* scan = (byte*) data.Scan0;
  274. #if false
  275. // 13 is prime (so we're not affected by a recurring pattern)
  276. for (int p = 0; p < size; p += 13) {
  277. Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
  278. }
  279. #else
  280. // sampling values from a well known bitmap
  281. Assert.AreEqual (0, *(scan + 0), "0");
  282. Assert.AreEqual (0, *(scan + 13), "13");
  283. Assert.AreEqual (0, *(scan + 26), "26");
  284. Assert.AreEqual (0, *(scan + 39), "39");
  285. Assert.AreEqual (0, *(scan + 52), "52");
  286. Assert.AreEqual (0, *(scan + 65), "65");
  287. Assert.AreEqual (0, *(scan + 78), "78");
  288. Assert.AreEqual (0, *(scan + 91), "91");
  289. Assert.AreEqual (0, *(scan + 104), "104");
  290. Assert.AreEqual (0, *(scan + 117), "117");
  291. Assert.AreEqual (0, *(scan + 130), "130");
  292. Assert.AreEqual (0, *(scan + 143), "143");
  293. Assert.AreEqual (0, *(scan + 156), "156");
  294. Assert.AreEqual (255, *(scan + 169), "169");
  295. Assert.AreEqual (0, *(scan + 182), "182");
  296. Assert.AreEqual (0, *(scan + 195), "195");
  297. Assert.AreEqual (255, *(scan + 208), "208");
  298. Assert.AreEqual (255, *(scan + 221), "221");
  299. Assert.AreEqual (0, *(scan + 234), "234");
  300. Assert.AreEqual (128, *(scan + 247), "247");
  301. Assert.AreEqual (0, *(scan + 260), "260");
  302. Assert.AreEqual (0, *(scan + 273), "273");
  303. Assert.AreEqual (0, *(scan + 286), "286");
  304. Assert.AreEqual (255, *(scan + 299), "299");
  305. Assert.AreEqual (0, *(scan + 312), "312");
  306. Assert.AreEqual (128, *(scan + 325), "325");
  307. Assert.AreEqual (0, *(scan + 338), "338");
  308. Assert.AreEqual (0, *(scan + 351), "351");
  309. Assert.AreEqual (255, *(scan + 364), "364");
  310. Assert.AreEqual (0, *(scan + 377), "377");
  311. Assert.AreEqual (0, *(scan + 390), "390");
  312. Assert.AreEqual (255, *(scan + 403), "403");
  313. Assert.AreEqual (255, *(scan + 416), "416");
  314. Assert.AreEqual (0, *(scan + 429), "429");
  315. Assert.AreEqual (255, *(scan + 442), "442");
  316. Assert.AreEqual (0, *(scan + 455), "455");
  317. Assert.AreEqual (0, *(scan + 468), "468");
  318. Assert.AreEqual (0, *(scan + 481), "481");
  319. Assert.AreEqual (255, *(scan + 494), "494");
  320. Assert.AreEqual (0, *(scan + 507), "507");
  321. Assert.AreEqual (0, *(scan + 520), "520");
  322. Assert.AreEqual (0, *(scan + 533), "533");
  323. Assert.AreEqual (0, *(scan + 546), "546");
  324. Assert.AreEqual (255, *(scan + 559), "559");
  325. Assert.AreEqual (0, *(scan + 572), "572");
  326. Assert.AreEqual (0, *(scan + 585), "585");
  327. Assert.AreEqual (255, *(scan + 598), "598");
  328. Assert.AreEqual (0, *(scan + 611), "611");
  329. Assert.AreEqual (0, *(scan + 624), "624");
  330. Assert.AreEqual (0, *(scan + 637), "637");
  331. Assert.AreEqual (128, *(scan + 650), "650");
  332. Assert.AreEqual (0, *(scan + 663), "663");
  333. Assert.AreEqual (0, *(scan + 676), "676");
  334. Assert.AreEqual (0, *(scan + 689), "689");
  335. Assert.AreEqual (0, *(scan + 702), "702");
  336. #endif
  337. }
  338. }
  339. finally {
  340. bmp.UnlockBits (data);
  341. }
  342. }
  343. }
  344. [Test]
  345. [Ignore ("ICON support is missing")]
  346. public void Save ()
  347. {
  348. string sOutFile = "linerect" + getOutSufix () + ".ico";
  349. // Save
  350. Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);
  351. Graphics gr = Graphics.FromImage (bmp);
  352. using (Pen p = new Pen (Color.Red, 2)) {
  353. gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
  354. gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
  355. }
  356. try {
  357. bmp.Save (sOutFile, ImageFormat.Icon);
  358. // Load
  359. using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
  360. Color color = bmpLoad.GetPixel (10, 10);
  361. Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color);
  362. }
  363. }
  364. finally {
  365. gr.Dispose ();
  366. bmp.Dispose ();
  367. try {
  368. File.Delete (sOutFile);
  369. }
  370. catch {
  371. }
  372. }
  373. }
  374. }
  375. }