TestBmpCodec.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.Text;
  39. namespace MonoTests.System.Drawing
  40. {
  41. [TestFixture]
  42. public class TestBmpCodec
  43. {
  44. [TearDown]
  45. public void Clean() {}
  46. [SetUp]
  47. public void GetReady()
  48. {
  49. }
  50. /* Get the output directory depending on the runtime and location*/
  51. internal string getOutSubDir()
  52. {
  53. string sSub, sRslt;
  54. if (Environment.GetEnvironmentVariable("MSNet")==null)
  55. sSub = "mono/";
  56. else
  57. sSub = "MSNet/";
  58. sRslt = Path.GetFullPath ("../System.Drawing/" + sSub);
  59. if (Directory.Exists(sRslt) == false)
  60. sRslt = "Test/System.Drawing/" + sSub;
  61. if (sRslt.Length > 0)
  62. if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
  63. sRslt += "/";
  64. return sRslt;
  65. }
  66. /* Get the input directory depending on the runtime*/
  67. internal string getInFile(string file)
  68. {
  69. string sRslt, local;
  70. local = "../System.Drawing/" + file;
  71. sRslt = Path.GetFullPath (local);
  72. if (File.Exists(sRslt)==false)
  73. sRslt = "Test/System.Drawing/" + file;
  74. return sRslt;
  75. }
  76. /* Checks bitmap features on a know 24-bits bitmap */
  77. [Test]
  78. public void Bitmap24bitFeatures()
  79. {
  80. string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
  81. Bitmap bmp = new Bitmap(sInFile);
  82. RectangleF rect;
  83. GraphicsUnit unit = GraphicsUnit.World;
  84. rect = bmp.GetBounds(ref unit);
  85. Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat);
  86. Assert.AreEqual (173, bmp.Width);
  87. Assert.AreEqual (183, bmp.Height);
  88. Assert.AreEqual (0, rect.X);
  89. Assert.AreEqual (0, rect.Y);
  90. Assert.AreEqual (173, rect.Width);
  91. Assert.AreEqual (183, rect.Height);
  92. Assert.AreEqual (173, bmp.Size.Width);
  93. Assert.AreEqual (183, bmp.Size.Height);
  94. }
  95. /* Checks bitmap features on a know 32-bits bitmap (codec)*/
  96. [Test]
  97. public void Bitmap32bitFeatures()
  98. {
  99. string sInFile = getInFile ("bitmaps/almogaver32bits.bmp");
  100. Bitmap bmp = new Bitmap(sInFile);
  101. RectangleF rect;
  102. GraphicsUnit unit = GraphicsUnit.World;
  103. rect = bmp.GetBounds(ref unit);
  104. //Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
  105. Assert.AreEqual (173, bmp.Width);
  106. Assert.AreEqual (183, bmp.Height);
  107. Assert.AreEqual (0, rect.X);
  108. Assert.AreEqual (0, rect.Y);
  109. Assert.AreEqual (173, rect.Width);
  110. Assert.AreEqual (183, rect.Height);
  111. Assert.AreEqual (173, bmp.Size.Width);
  112. Assert.AreEqual (183, bmp.Size.Height);
  113. }
  114. [Test]
  115. public void Save()
  116. {
  117. string sOutFile = getOutSubDir() + "linerect.bmp";
  118. // Save
  119. Bitmap bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);
  120. Graphics gr = Graphics.FromImage(bmp);
  121. Pen p = new Pen(Color.Red, 2);
  122. gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
  123. gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
  124. p.Dispose();
  125. bmp.Save(sOutFile, ImageFormat.Bmp);
  126. gr.Dispose();
  127. bmp.Dispose();
  128. // Load
  129. Bitmap bmpLoad = new Bitmap(sOutFile);
  130. Color color = bmpLoad.GetPixel(10,10);
  131. Assert.AreEqual (Color.FromArgb(255,255,0,0), color);
  132. }
  133. }
  134. }