2 // Bitmap class testing unit
6 // Jordi Mas i Hernàndez (jmas@softcatala.org>
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
12 using System.Drawing.Imaging;
13 using NUnit.Framework;
15 using System.Security.Cryptography;
17 using System.Runtime.InteropServices;
19 namespace MonoTests.System.Drawing{
22 public class TestBitmap : Assertion {
25 public void Clean() {}
28 public void GetReady()
34 public void TestPixels()
36 // Tests GetSetPixel/SetPixel
37 Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
38 bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));
39 Color color = bmp.GetPixel(0,0);
41 AssertEquals (Color.FromArgb(255,128,128,128), color);
43 bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));
44 Color color2 = bmp.GetPixel(99,99);
45 AssertEquals (Color.FromArgb(255,255,0,155), color2);
48 /* Get the output directory depending on the runtime and location*/
49 internal string getOutSubDir()
53 if (Environment.GetEnvironmentVariable("MSNet")==null)
58 sRslt = Path.GetFullPath (sSub);
60 if (Directory.Exists(sRslt) == false)
61 sRslt = "Test/System.Drawing/" + sSub;
64 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
70 /* Get the input directory depending on the runtime*/
71 internal string getInFile(string file)
75 sRslt = Path.GetFullPath (file);
77 if (File.Exists(file)==false)
78 sRslt = "Test/System.Drawing/" + file;
86 public void MakeTransparent()
88 string sInFile = getInFile("bitmaps/maketransparent.bmp");
89 string sOutFile = getOutSubDir() + "transparent.bmp";
91 Bitmap bmp = new Bitmap(sInFile);
92 Console.WriteLine("Bitmap loaded OK", bmp != null);
94 bmp.MakeTransparent();
97 Color color = bmp.GetPixel(1,1);
98 AssertEquals (Color.Black.R, color.R);
99 AssertEquals (Color.Black.G, color.G);
100 AssertEquals (Color.Black.B, color.B);
106 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
107 string sOutFile = getOutSubDir() + "clone24.bmp";
109 Rectangle rect = new Rectangle(0,0,50,50);
110 Bitmap bmp = new Bitmap(sInFile);
112 Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);
113 bmpNew.Save(sOutFile);
115 Color colororg0 = bmp.GetPixel(0,0);
116 Color colororg50 = bmp.GetPixel(49,49);
117 Color colornew0 = bmpNew.GetPixel(0,0);
118 Color colornew50 = bmpNew.GetPixel(49,49);
120 AssertEquals (colororg0, colornew0);
121 AssertEquals (colororg50, colornew50);
125 public void CloneImage()
127 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
128 Bitmap bmp = new Bitmap(sInFile);
130 Bitmap bmpNew = (Bitmap) bmp.Clone ();
132 AssertEquals (bmp.Width, bmpNew.Width);
133 AssertEquals (bmp.Height, bmpNew.Height);
134 AssertEquals (bmp.PixelFormat, bmpNew.PixelFormat);
141 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
142 Bitmap bmp = new Bitmap(sInFile);
143 int cnt = bmp.GetFrameCount(FrameDimension.Page);
144 int active = bmp.SelectActiveFrame (FrameDimension.Page, 0);
146 AssertEquals (1, cnt);
147 AssertEquals (0, active);
150 static string ByteArrayToString(byte[] arrInput)
153 StringBuilder sOutput = new StringBuilder(arrInput.Length);
154 for (i=0;i < arrInput.Length -1; i++)
156 sOutput.Append(arrInput[i].ToString("X2"));
158 return sOutput.ToString();
162 public string RotateBmp (Bitmap src, RotateFlipType rotate)
164 int witdh = 150, height = 150, index = 0;
165 byte[] pixels = new byte [witdh * height * 3];
171 bmp_rotate = src.Clone (new RectangleF (0,0, witdh, height), PixelFormat.Format32bppArgb);
172 bmp_rotate.RotateFlip (rotate);
174 for (int y = 0; y < height; y++) {
175 for (int x = 0; x < witdh; x++) {
176 clr = bmp_rotate.GetPixel (x,y);
177 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
181 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
182 return ByteArrayToString (hash);
187 Rotate bitmap in diffent ways, and check the result
193 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
194 Bitmap bmp = new Bitmap(sInFile);
196 AssertEquals ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));
197 AssertEquals ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
198 AssertEquals ("15AD2ADBDC7090C0EC744D0F7ACE2F", RotateBmp (bmp, RotateFlipType.Rotate270FlipNone));
199 AssertEquals ("2E10FEC1F4FD64ECC51D7CE68AEB18", RotateBmp (bmp, RotateFlipType.RotateNoneFlipX));
200 AssertEquals ("E63204779B566ED01162B90B49BD9E", RotateBmp (bmp, RotateFlipType.Rotate90FlipX));
201 AssertEquals ("B1ECB17B5093E13D04FF55CFCF7763", RotateBmp (bmp, RotateFlipType.Rotate180FlipX));
202 AssertEquals ("71A173882C16755D86F4BC26532374", RotateBmp (bmp, RotateFlipType.Rotate270FlipX));
206 public void LockBmp (PixelFormat fmt, PixelFormat fmtlock, string output,
207 int lwidth , int lheight, ref string hash1, ref string hash2)
209 int width = 100, height = 100, bbps, cur, pos;
210 Bitmap bmp = new Bitmap (width, height, fmt);
211 Graphics gr = Graphics.FromImage (bmp);
214 byte[] btv = new byte[1];
215 int y, x, len = width * height * 4, index = 0;
216 byte[] pixels = new byte [len];
219 bbps = Image.GetPixelFormatSize (fmt);
221 Pen p = new Pen (Color.FromArgb (255, 100, 200, 250), 2);
222 gr.DrawRectangle(p, 1.0F, 1.0F, 80.0F, 80.0F);
224 BitmapData bd = bmp.LockBits (new Rectangle (0, 0, lwidth, lheight), ImageLockMode.ReadOnly, fmtlock);
226 pos = bd.Scan0.ToInt32();
227 for (y = 0; y < bd.Height; y++) {
228 for (x = 0; x < bd.Width; x++) {
231 for (int bt =0; bt < bbps/8; bt++, index++) {
236 Marshal.Copy ((IntPtr)cur, btv, 0, 1);
237 pixels[index] = btv[0];
239 /* Make change of all the colours = 250 to 10*/
242 Marshal.Copy (btv, 0, (IntPtr)cur, 1);
248 for (int i = index; i < len; i++)
251 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
254 hash1 = ByteArrayToString (hash);
256 /* MD5 of the changed bitmap*/
257 for (y = 0, index = 0; y < height; y++) {
258 for (x = 0; x < width; x++) {
259 clr = bmp.GetPixel (x,y);
260 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
264 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
265 hash2 = ByteArrayToString (hash);
267 /*bmp.Save (output, ImageFormat.Bmp);*/
270 Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns
271 firsts, changes them, and then using GetPixel does another check of the changes.
272 The results match the .Net framework
275 public void LockBitmap ()
280 /* Locks the whole bitmap*/
281 LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 100, 100, ref hash, ref hashchg);
282 AssertEquals ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
283 AssertEquals ("BBEE27DC85563CB58EE11E8951230F", hashchg);
285 LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 100, 100, ref hash, ref hashchg);
286 AssertEquals ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
287 AssertEquals ("BBEE27DC85563CB58EE11E8951230F", hashchg);
289 LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 100, 100, ref hash, ref hashchg);
290 AssertEquals ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
291 AssertEquals ("BBEE27DC85563CB58EE11E8951230F", hashchg);
293 LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 100, 100, ref hash, ref hashchg);
294 AssertEquals ("A8A071D0B3A3743905B4E193A62769", hash);
295 AssertEquals ("EEE846FA8F892339C64082DFF775CF", hashchg);
297 /* Locks a portion of the bitmap*/
298 LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 50, 50, ref hash, ref hashchg);
299 AssertEquals ("C361FBFD82A4F3C278605AE9EC5385", hash);
300 AssertEquals ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
302 LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 50, 50, ref hash, ref hashchg);
303 AssertEquals ("C361FBFD82A4F3C278605AE9EC5385", hash);
304 AssertEquals ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
306 LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 50, 50, ref hash, ref hashchg);
307 AssertEquals ("C361FBFD82A4F3C278605AE9EC5385", hash);
308 AssertEquals ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
310 LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 50, 50, ref hash, ref hashchg);
311 AssertEquals ("FFE86628478591D1A1EB30E894C34F", hash);
312 AssertEquals ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);