TestBmpCodec.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // BMPCodec class testing unit
  3. //
  4. // Author:
  5. //
  6. // Jordi Mas i Hernàndez ([email protected])
  7. //
  8. // (C) 2004 Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Drawing;
  34. using System.Drawing.Imaging;
  35. using NUnit.Framework;
  36. using System.IO;
  37. using System.Security.Cryptography;
  38. using System.Security.Permissions;
  39. using System.Text;
  40. namespace MonoTests.System.Drawing.Imaging
  41. {
  42. [TestFixture]
  43. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  44. public class TestBmpCodec
  45. {
  46. [TearDown]
  47. public void Clean() {}
  48. [SetUp]
  49. public void GetReady()
  50. {
  51. }
  52. /* Get suffix to add to the filename */
  53. internal string getOutSufix()
  54. {
  55. if (Environment.GetEnvironmentVariable("MSNet")==null)
  56. return "-mono";
  57. return "";
  58. }
  59. /* Get the input directory depending on the runtime*/
  60. internal string getInFile(string file)
  61. {
  62. string sRslt, local;
  63. local = "../System.Drawing/" + file;
  64. sRslt = Path.GetFullPath (local);
  65. if (File.Exists(sRslt)==false)
  66. sRslt = "Test/System.Drawing/" + file;
  67. return sRslt;
  68. }
  69. /* Checks bitmap features on a know 24-bits bitmap */
  70. #if TARGET_JVM
  71. [NUnit.Framework.Category ("NotWorking")]
  72. #endif
  73. [Test]
  74. public void Bitmap24bitFeatures()
  75. {
  76. string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
  77. Bitmap bmp = new Bitmap(sInFile);
  78. RectangleF rect;
  79. GraphicsUnit unit = GraphicsUnit.World;
  80. rect = bmp.GetBounds(ref unit);
  81. Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat);
  82. Assert.AreEqual (173, bmp.Width);
  83. Assert.AreEqual (183, bmp.Height);
  84. Assert.AreEqual (0, rect.X);
  85. Assert.AreEqual (0, rect.Y);
  86. Assert.AreEqual (173, rect.Width);
  87. Assert.AreEqual (183, rect.Height);
  88. Assert.AreEqual (173, bmp.Size.Width);
  89. Assert.AreEqual (183, bmp.Size.Height);
  90. // sampling values from a well known bitmap
  91. Assert.AreEqual (-1645353, bmp.GetPixel (0, 32).ToArgb (), "0,32");
  92. Assert.AreEqual (-461332, bmp.GetPixel (0, 64).ToArgb (), "0,64");
  93. Assert.AreEqual (-330005, bmp.GetPixel (0, 96).ToArgb (), "0,96");
  94. Assert.AreEqual (-2237489, bmp.GetPixel (0, 128).ToArgb (), "0,128");
  95. Assert.AreEqual (-1251105, bmp.GetPixel (0, 160).ToArgb (), "0,160");
  96. Assert.AreEqual (-3024947, bmp.GetPixel (32, 0).ToArgb (), "32,0");
  97. Assert.AreEqual (-2699070, bmp.GetPixel (32, 32).ToArgb (), "32,32");
  98. Assert.AreEqual (-2366734, bmp.GetPixel (32, 64).ToArgb (), "32,64");
  99. Assert.AreEqual (-4538413, bmp.GetPixel (32, 96).ToArgb (), "32,96");
  100. Assert.AreEqual (-6116681, bmp.GetPixel (32, 128).ToArgb (), "32,128");
  101. Assert.AreEqual (-7369076, bmp.GetPixel (32, 160).ToArgb (), "32,160");
  102. Assert.AreEqual (-13024729, bmp.GetPixel (64, 0).ToArgb (), "64,0");
  103. Assert.AreEqual (-7174020, bmp.GetPixel (64, 32).ToArgb (), "64,32");
  104. Assert.AreEqual (-51, bmp.GetPixel (64, 64).ToArgb (), "64,64");
  105. Assert.AreEqual (-16053503, bmp.GetPixel (64, 96).ToArgb (), "64,96");
  106. Assert.AreEqual (-8224431, bmp.GetPixel (64, 128).ToArgb (), "64,128");
  107. Assert.AreEqual (-16579326, bmp.GetPixel (64, 160).ToArgb (), "64,160");
  108. Assert.AreEqual (-2502457, bmp.GetPixel (96, 0).ToArgb (), "96,0");
  109. Assert.AreEqual (-9078395, bmp.GetPixel (96, 32).ToArgb (), "96,32");
  110. Assert.AreEqual (-12696508, bmp.GetPixel (96, 64).ToArgb (), "96,64");
  111. Assert.AreEqual (-70772, bmp.GetPixel (96, 96).ToArgb (), "96,96");
  112. Assert.AreEqual (-4346279, bmp.GetPixel (96, 128).ToArgb (), "96,128");
  113. Assert.AreEqual (-11583193, bmp.GetPixel (96, 160).ToArgb (), "96,160");
  114. Assert.AreEqual (-724763, bmp.GetPixel (128, 0).ToArgb (), "128,0");
  115. Assert.AreEqual (-7238268, bmp.GetPixel (128, 32).ToArgb (), "128,32");
  116. Assert.AreEqual (-2169612, bmp.GetPixel (128, 64).ToArgb (), "128,64");
  117. Assert.AreEqual (-3683883, bmp.GetPixel (128, 96).ToArgb (), "128,96");
  118. Assert.AreEqual (-12892867, bmp.GetPixel (128, 128).ToArgb (), "128,128");
  119. Assert.AreEqual (-3750464, bmp.GetPixel (128, 160).ToArgb (), "128,160");
  120. Assert.AreEqual (-3222844, bmp.GetPixel (160, 0).ToArgb (), "160,0");
  121. Assert.AreEqual (-65806, bmp.GetPixel (160, 32).ToArgb (), "160,32");
  122. Assert.AreEqual (-2961726, bmp.GetPixel (160, 64).ToArgb (), "160,64");
  123. Assert.AreEqual (-2435382, bmp.GetPixel (160, 96).ToArgb (), "160,96");
  124. Assert.AreEqual (-2501944, bmp.GetPixel (160, 128).ToArgb (), "160,128");
  125. Assert.AreEqual (-9211799, bmp.GetPixel (160, 160).ToArgb (), "160,160");
  126. /* for (int x = 0; x <bmp.Width; x += 32) {
  127. for (int y = 0; y < bmp.Height; y += 32)
  128. Console.WriteLine ("\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
  129. }*/
  130. }
  131. /* Checks bitmap features on a know 32-bits bitmap (codec)*/
  132. [Test]
  133. public void Bitmap32bitFeatures()
  134. {
  135. string sInFile = getInFile ("bitmaps/almogaver32bits.bmp");
  136. Bitmap bmp = new Bitmap(sInFile);
  137. RectangleF rect;
  138. GraphicsUnit unit = GraphicsUnit.World;
  139. rect = bmp.GetBounds(ref unit);
  140. Assert.AreEqual (173, bmp.Width);
  141. Assert.AreEqual (183, bmp.Height);
  142. Assert.AreEqual (0, rect.X);
  143. Assert.AreEqual (0, rect.Y);
  144. Assert.AreEqual (173, rect.Width);
  145. Assert.AreEqual (183, rect.Height);
  146. Assert.AreEqual (173, bmp.Size.Width);
  147. Assert.AreEqual (183, bmp.Size.Height);
  148. }
  149. [Test]
  150. public void Save()
  151. {
  152. string sOutFile = "linerect" + getOutSufix() + ".bmp";
  153. // Save
  154. Bitmap bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);
  155. Graphics gr = Graphics.FromImage(bmp);
  156. Pen p = new Pen(Color.Red, 2);
  157. gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
  158. gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
  159. p.Dispose();
  160. bmp.Save(sOutFile, ImageFormat.Bmp);
  161. gr.Dispose();
  162. bmp.Dispose();
  163. // Load
  164. Bitmap bmpLoad = new Bitmap(sOutFile);
  165. Color color = bmpLoad.GetPixel(10,10);
  166. //Assert.AreEqual (Color.FromArgb(255,255,0,0), color);
  167. }
  168. }
  169. }