IconCodecTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. [TestFixtureSetUp]
  41. public void FixtureSetUp ()
  42. {
  43. if (Type.GetType ("Mono.Runtime", false) != null)
  44. Assert.Ignore ("ICON support is missing");
  45. }
  46. /* Get suffix to add to the filename */
  47. internal string getOutSufix ()
  48. {
  49. string s;
  50. int p = (int) Environment.OSVersion.Platform;
  51. if ((p == 4) || (p == 128))
  52. s = "-unix";
  53. else
  54. s = "-windows";
  55. if (Type.GetType ("Mono.Runtime", false) == null)
  56. s += "-msnet";
  57. else
  58. s += "-mono";
  59. return s;
  60. }
  61. /* Get the input directory depending on the runtime*/
  62. internal string getInFile (string file)
  63. {
  64. string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
  65. if (!File.Exists (sRslt))
  66. sRslt = "Test/System.Drawing/" + file;
  67. return sRslt;
  68. }
  69. /* Checks bitmap features on a know 1bbp bitmap */
  70. [Test]
  71. public void Bitmap16Features ()
  72. {
  73. string sInFile = getInFile ("bitmaps/smiley.ico");
  74. using (Bitmap bmp = new Bitmap (sInFile)) {
  75. GraphicsUnit unit = GraphicsUnit.World;
  76. RectangleF rect = bmp.GetBounds (ref unit);
  77. // ??? why is it a 4bbp ?
  78. Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
  79. Assert.AreEqual (16, bmp.Width, "bmp.Width");
  80. Assert.AreEqual (16, bmp.Height, "bmp.Height");
  81. Assert.AreEqual (0, rect.X, "rect.X");
  82. Assert.AreEqual (0, rect.Y, "rect.Y");
  83. Assert.AreEqual (16, rect.Width, "rect.Width");
  84. Assert.AreEqual (16, rect.Height, "rect.Height");
  85. Assert.AreEqual (16, bmp.Size.Width, "bmp.Size.Width");
  86. Assert.AreEqual (16, bmp.Size.Height, "bmp.Size.Height");
  87. }
  88. }
  89. [Test]
  90. public void Bitmap16Pixels ()
  91. {
  92. string sInFile = getInFile ("bitmaps/smiley.ico");
  93. using (Bitmap bmp = new Bitmap (sInFile)) {
  94. #if false
  95. for (int x = 0; x < bmp.Width; x += 4) {
  96. for (int y = 0; y < bmp.Height; y += 4)
  97. Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  98. }
  99. #else
  100. // sampling values from a well known bitmap
  101. Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
  102. Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
  103. Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
  104. Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
  105. Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
  106. Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
  107. Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
  108. Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
  109. Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
  110. Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
  111. Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
  112. Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
  113. Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
  114. Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
  115. Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
  116. Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
  117. #endif
  118. }
  119. }
  120. [Test]
  121. [Category ("NotWorking")]
  122. public void Bitmat16Data ()
  123. {
  124. string sInFile = getInFile ("bitmaps/smiley.ico");
  125. using (Bitmap bmp = new Bitmap (sInFile)) {
  126. BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  127. try {
  128. Assert.AreEqual (bmp.Height, data.Height, "Height");
  129. Assert.AreEqual (bmp.Width, data.Width, "Width");
  130. Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
  131. int size = data.Height * data.Stride;
  132. unsafe {
  133. byte* scan = (byte*) data.Scan0;
  134. #if false
  135. // 13 is prime (so we're not affected by a recurring pattern)
  136. for (int p = 0; p < size; p += 13) {
  137. Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
  138. }
  139. #else
  140. // sampling values from a well known bitmap
  141. Assert.AreEqual (0, *(scan + 0), "0");
  142. Assert.AreEqual (0, *(scan + 13), "13");
  143. Assert.AreEqual (0, *(scan + 26), "26");
  144. Assert.AreEqual (0, *(scan + 39), "39");
  145. Assert.AreEqual (0, *(scan + 52), "52");
  146. Assert.AreEqual (0, *(scan + 65), "65");
  147. Assert.AreEqual (0, *(scan + 78), "78");
  148. Assert.AreEqual (0, *(scan + 91), "91");
  149. Assert.AreEqual (0, *(scan + 104), "104");
  150. Assert.AreEqual (0, *(scan + 117), "117");
  151. Assert.AreEqual (0, *(scan + 130), "130");
  152. Assert.AreEqual (0, *(scan + 143), "143");
  153. Assert.AreEqual (0, *(scan + 156), "156");
  154. Assert.AreEqual (255, *(scan + 169), "169");
  155. Assert.AreEqual (0, *(scan + 182), "182");
  156. Assert.AreEqual (0, *(scan + 195), "195");
  157. Assert.AreEqual (255, *(scan + 208), "208");
  158. Assert.AreEqual (255, *(scan + 221), "221");
  159. Assert.AreEqual (0, *(scan + 234), "234");
  160. Assert.AreEqual (128, *(scan + 247), "247");
  161. Assert.AreEqual (0, *(scan + 260), "260");
  162. Assert.AreEqual (0, *(scan + 273), "273");
  163. Assert.AreEqual (0, *(scan + 286), "286");
  164. Assert.AreEqual (255, *(scan + 299), "299");
  165. Assert.AreEqual (0, *(scan + 312), "312");
  166. Assert.AreEqual (128, *(scan + 325), "325");
  167. Assert.AreEqual (0, *(scan + 338), "338");
  168. Assert.AreEqual (0, *(scan + 351), "351");
  169. Assert.AreEqual (255, *(scan + 364), "364");
  170. Assert.AreEqual (0, *(scan + 377), "377");
  171. Assert.AreEqual (0, *(scan + 390), "390");
  172. Assert.AreEqual (255, *(scan + 403), "403");
  173. Assert.AreEqual (255, *(scan + 416), "416");
  174. Assert.AreEqual (0, *(scan + 429), "429");
  175. Assert.AreEqual (255, *(scan + 442), "442");
  176. Assert.AreEqual (0, *(scan + 455), "455");
  177. Assert.AreEqual (0, *(scan + 468), "468");
  178. Assert.AreEqual (0, *(scan + 481), "481");
  179. Assert.AreEqual (255, *(scan + 494), "494");
  180. Assert.AreEqual (0, *(scan + 507), "507");
  181. Assert.AreEqual (0, *(scan + 520), "520");
  182. Assert.AreEqual (0, *(scan + 533), "533");
  183. Assert.AreEqual (0, *(scan + 546), "546");
  184. Assert.AreEqual (255, *(scan + 559), "559");
  185. Assert.AreEqual (0, *(scan + 572), "572");
  186. Assert.AreEqual (0, *(scan + 585), "585");
  187. Assert.AreEqual (255, *(scan + 598), "598");
  188. Assert.AreEqual (0, *(scan + 611), "611");
  189. Assert.AreEqual (0, *(scan + 624), "624");
  190. Assert.AreEqual (0, *(scan + 637), "637");
  191. Assert.AreEqual (128, *(scan + 650), "650");
  192. Assert.AreEqual (0, *(scan + 663), "663");
  193. Assert.AreEqual (0, *(scan + 676), "676");
  194. Assert.AreEqual (0, *(scan + 689), "689");
  195. Assert.AreEqual (0, *(scan + 702), "702");
  196. Assert.AreEqual (0, *(scan + 715), "715");
  197. Assert.AreEqual (0, *(scan + 728), "728");
  198. Assert.AreEqual (0, *(scan + 741), "741");
  199. Assert.AreEqual (0, *(scan + 754), "754");
  200. Assert.AreEqual (0, *(scan + 767), "767");
  201. #endif
  202. }
  203. }
  204. finally {
  205. bmp.UnlockBits (data);
  206. }
  207. }
  208. }
  209. /* Checks bitmap features on a know 1bbp bitmap */
  210. [Test]
  211. public void Bitmap32Features ()
  212. {
  213. string sInFile = getInFile ("bitmaps/VisualPng.ico");
  214. using (Bitmap bmp = new Bitmap (sInFile)) {
  215. GraphicsUnit unit = GraphicsUnit.World;
  216. RectangleF rect = bmp.GetBounds (ref unit);
  217. // ??? why is it a 4bbp ?
  218. Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
  219. Assert.AreEqual (32, bmp.Width, "bmp.Width");
  220. Assert.AreEqual (32, bmp.Height, "bmp.Height");
  221. Assert.AreEqual (0, rect.X, "rect.X");
  222. Assert.AreEqual (0, rect.Y, "rect.Y");
  223. Assert.AreEqual (32, rect.Width, "rect.Width");
  224. Assert.AreEqual (32, rect.Height, "rect.Height");
  225. Assert.AreEqual (32, bmp.Size.Width, "bmp.Size.Width");
  226. Assert.AreEqual (32, bmp.Size.Height, "bmp.Size.Height");
  227. }
  228. }
  229. [Test]
  230. public void Bitmap32Pixels ()
  231. {
  232. string sInFile = getInFile ("bitmaps/smiley.ico");
  233. using (Bitmap bmp = new Bitmap (sInFile)) {
  234. #if false
  235. for (int x = 0; x < bmp.Width; x += 4) {
  236. for (int y = 0; y < bmp.Height; y += 4)
  237. Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  238. }
  239. #else
  240. // sampling values from a well known bitmap
  241. Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
  242. Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
  243. Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
  244. Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
  245. Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
  246. Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
  247. Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
  248. Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
  249. Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
  250. Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
  251. Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
  252. Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
  253. Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
  254. Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
  255. Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
  256. Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
  257. #endif
  258. }
  259. }
  260. [Test]
  261. [Category ("NotWorking")]
  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. private void Save (PixelFormat original, PixelFormat expected, bool colorCheck)
  345. {
  346. string sOutFile = String.Format ("linerect{0}-{1}.ico", getOutSufix (), expected.ToString ());
  347. // Save
  348. Bitmap bmp = new Bitmap (100, 100, original);
  349. Graphics gr = Graphics.FromImage (bmp);
  350. using (Pen p = new Pen (Color.Red, 2)) {
  351. gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
  352. gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
  353. }
  354. try {
  355. bmp.Save (sOutFile, ImageFormat.Icon);
  356. // Load
  357. using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
  358. Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
  359. if (colorCheck) {
  360. Color color = bmpLoad.GetPixel (10, 10);
  361. Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color, "Red");
  362. }
  363. }
  364. }
  365. finally {
  366. gr.Dispose ();
  367. bmp.Dispose ();
  368. try {
  369. File.Delete (sOutFile);
  370. }
  371. catch {
  372. }
  373. }
  374. }
  375. [Test]
  376. public void Save_24bppRgb ()
  377. {
  378. Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, true);
  379. }
  380. [Test]
  381. public void Save_32bppRgb ()
  382. {
  383. Save (PixelFormat.Format32bppRgb, PixelFormat.Format32bppArgb, true);
  384. }
  385. [Test]
  386. public void Save_32bppArgb ()
  387. {
  388. Save (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, true);
  389. }
  390. [Test]
  391. public void Save_32bppPArgb ()
  392. {
  393. Save (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppArgb, true);
  394. }
  395. [Test]
  396. [Category ("NotWorking")]
  397. public void Save_48bppRgb ()
  398. {
  399. Save (PixelFormat.Format48bppRgb, PixelFormat.Format48bppRgb, false);
  400. }
  401. [Test]
  402. [Category ("NotWorking")]
  403. public void Save_64bppArgb ()
  404. {
  405. Save (PixelFormat.Format64bppArgb, PixelFormat.Format64bppArgb, false);
  406. }
  407. [Test]
  408. [Category ("NotWorking")]
  409. public void Save_64bppPArgb ()
  410. {
  411. Save (PixelFormat.Format64bppPArgb, PixelFormat.Format64bppArgb, false);
  412. }
  413. }
  414. }