2 // Bitmap class testing unit
6 // Jordi Mas i Hernàndez (jmas@softcatala.org>
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Drawing.Imaging;
36 using NUnit.Framework;
38 using System.Security.Cryptography;
40 using System.Runtime.InteropServices;
41 using System.Security.Permissions;
43 namespace MonoTests.System.Drawing{
46 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
47 public class TestBitmap {
50 public void Clean() {}
53 public void GetReady()
59 public void TestPixels()
61 // Tests GetSetPixel/SetPixel
62 Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
63 bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));
64 Color color = bmp.GetPixel(0,0);
66 Assert.AreEqual (Color.FromArgb(255,128,128,128), color);
68 bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));
69 Color color2 = bmp.GetPixel(99,99);
70 Assert.AreEqual (Color.FromArgb(255,255,0,155), color2);
73 /* Get the output directory depending on the runtime and location*/
74 public static string getOutSubDir()
78 if (Environment.GetEnvironmentVariable("MSNet")==null)
83 sRslt = Path.GetFullPath (sSub);
85 if (Directory.Exists(sRslt) == false)
86 sRslt = "Test/System.Drawing/" + sSub;
89 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
95 /* Get the input directory depending on the runtime*/
96 public static string getInFile(string file)
100 sRslt = Path.GetFullPath (file);
102 if (File.Exists(file)==false)
103 sRslt = "Test/System.Drawing/" + file;
109 public void MakeTransparent()
111 string sInFile = getInFile("bitmaps/maketransparent.bmp");
112 string sOutFile = getOutSubDir() + "transparent.bmp";
114 Bitmap bmp = new Bitmap(sInFile);
116 bmp.MakeTransparent();
119 Color color = bmp.GetPixel(1,1);
120 Assert.AreEqual (Color.Black.R, color.R);
121 Assert.AreEqual (Color.Black.G, color.G);
122 Assert.AreEqual (Color.Black.B, color.B);
128 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
129 string sOutFile = getOutSubDir() + "clone24.bmp";
131 Rectangle rect = new Rectangle(0,0,50,50);
132 Bitmap bmp = new Bitmap(sInFile);
134 Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);
136 Color colororg0 = bmp.GetPixel(0,0);
137 Color colororg50 = bmp.GetPixel(49,49);
138 Color colornew0 = bmpNew.GetPixel(0,0);
139 Color colornew50 = bmpNew.GetPixel(49,49);
141 Assert.AreEqual (colororg0, colornew0);
142 Assert.AreEqual (colororg50, colornew50);
146 public void CloneImage()
148 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
149 Bitmap bmp = new Bitmap(sInFile);
151 Bitmap bmpNew = (Bitmap) bmp.Clone ();
153 Assert.AreEqual (bmp.Width, bmpNew.Width);
154 Assert.AreEqual (bmp.Height, bmpNew.Height);
155 Assert.AreEqual (bmp.PixelFormat, bmpNew.PixelFormat);
162 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
163 Bitmap bmp = new Bitmap(sInFile);
164 int cnt = bmp.GetFrameCount(FrameDimension.Page);
165 int active = bmp.SelectActiveFrame (FrameDimension.Page, 0);
167 Assert.AreEqual (1, cnt);
168 Assert.AreEqual (0, active);
172 [ExpectedException (typeof (ArgumentException))]
173 public void FileDoesNotExists ()
175 Bitmap bmp = new Bitmap ("FileDoesNotExists.jpg");
178 static string ByteArrayToString(byte[] arrInput)
181 StringBuilder sOutput = new StringBuilder(arrInput.Length);
182 for (i=0;i < arrInput.Length -1; i++)
184 sOutput.Append(arrInput[i].ToString("X2"));
186 return sOutput.ToString();
190 public string RotateBmp (Bitmap src, RotateFlipType rotate)
192 int witdh = 150, height = 150, index = 0;
193 byte[] pixels = new byte [witdh * height * 3];
199 bmp_rotate = src.Clone (new RectangleF (0,0, witdh, height), PixelFormat.Format32bppArgb);
200 bmp_rotate.RotateFlip (rotate);
202 for (int y = 0; y < height; y++) {
203 for (int x = 0; x < witdh; x++) {
204 clr = bmp_rotate.GetPixel (x,y);
205 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
209 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
210 return ByteArrayToString (hash);
215 Rotate bitmap in diffent ways, and check the result
221 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
222 Bitmap bmp = new Bitmap(sInFile);
224 Assert.AreEqual ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));
225 Assert.AreEqual ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
226 Assert.AreEqual ("15AD2ADBDC7090C0EC744D0F7ACE2F", RotateBmp (bmp, RotateFlipType.Rotate270FlipNone));
227 Assert.AreEqual ("2E10FEC1F4FD64ECC51D7CE68AEB18", RotateBmp (bmp, RotateFlipType.RotateNoneFlipX));
228 Assert.AreEqual ("E63204779B566ED01162B90B49BD9E", RotateBmp (bmp, RotateFlipType.Rotate90FlipX));
229 Assert.AreEqual ("B1ECB17B5093E13D04FF55CFCF7763", RotateBmp (bmp, RotateFlipType.Rotate180FlipX));
230 Assert.AreEqual ("71A173882C16755D86F4BC26532374", RotateBmp (bmp, RotateFlipType.Rotate270FlipX));
234 public void LockBmp (PixelFormat fmt, PixelFormat fmtlock, string output,
235 int lwidth , int lheight, ref string hash1, ref string hash2)
237 int width = 100, height = 100, bbps, cur, pos;
238 Bitmap bmp = new Bitmap (width, height, fmt);
239 Graphics gr = Graphics.FromImage (bmp);
242 byte[] btv = new byte[1];
243 int y, x, len = width * height * 4, index = 0;
244 byte[] pixels = new byte [len];
247 bbps = Image.GetPixelFormatSize (fmt);
249 Pen p = new Pen (Color.FromArgb (255, 100, 200, 250), 2);
250 gr.DrawRectangle(p, 1.0F, 1.0F, 80.0F, 80.0F);
252 BitmapData bd = bmp.LockBits (new Rectangle (0, 0, lwidth, lheight), ImageLockMode.ReadOnly, fmtlock);
254 pos = bd.Scan0.ToInt32();
255 for (y = 0; y < bd.Height; y++) {
256 for (x = 0; x < bd.Width; x++) {
259 for (int bt =0; bt < bbps/8; bt++, index++) {
264 Marshal.Copy ((IntPtr)cur, btv, 0, 1);
265 pixels[index] = btv[0];
267 /* Make change of all the colours = 250 to 10*/
270 Marshal.Copy (btv, 0, (IntPtr)cur, 1);
276 for (int i = index; i < len; i++)
279 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
282 hash1 = ByteArrayToString (hash);
284 /* MD5 of the changed bitmap*/
285 for (y = 0, index = 0; y < height; y++) {
286 for (x = 0; x < width; x++) {
287 clr = bmp.GetPixel (x,y);
288 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
292 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
293 hash2 = ByteArrayToString (hash);
295 /*bmp.Save (output, ImageFormat.Bmp);*/
298 Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns
299 firsts, changes them, and then using GetPixel does another check of the changes.
300 The results match the .Net framework
303 public void LockBitmap ()
308 /* Locks the whole bitmap*/
309 LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 100, 100, ref hash, ref hashchg);
310 Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
311 Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);
313 LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 100, 100, ref hash, ref hashchg);
314 Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
315 Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);
317 LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 100, 100, ref hash, ref hashchg);
318 Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
319 Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);
321 LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 100, 100, ref hash, ref hashchg);
322 Assert.AreEqual ("A8A071D0B3A3743905B4E193A62769", hash);
323 Assert.AreEqual ("EEE846FA8F892339C64082DFF775CF", hashchg);
325 /* Locks a portion of the bitmap*/
326 LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 50, 50, ref hash, ref hashchg);
327 Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);
328 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
330 LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 50, 50, ref hash, ref hashchg);
331 Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);
332 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
334 LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 50, 50, ref hash, ref hashchg);
335 Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);
336 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
338 LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 50, 50, ref hash, ref hashchg);
339 Assert.AreEqual ("FFE86628478591D1A1EB30E894C34F", hash);
340 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);