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;
42 namespace MonoTests.System.Drawing{
45 public class TestBitmap {
48 public void Clean() {}
51 public void GetReady()
57 public void TestPixels()
59 // Tests GetSetPixel/SetPixel
60 Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
61 bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));
62 Color color = bmp.GetPixel(0,0);
64 Assert.AreEqual (Color.FromArgb(255,128,128,128), color);
66 bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));
67 Color color2 = bmp.GetPixel(99,99);
68 Assert.AreEqual (Color.FromArgb(255,255,0,155), color2);
71 /* Get the output directory depending on the runtime and location*/
72 public static string getOutSubDir()
76 if (Environment.GetEnvironmentVariable("MSNet")==null)
81 sRslt = Path.GetFullPath (sSub);
83 if (Directory.Exists(sRslt) == false)
84 sRslt = "Test/System.Drawing/" + sSub;
87 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
93 /* Get the input directory depending on the runtime*/
94 public static string getInFile(string file)
98 sRslt = Path.GetFullPath (file);
100 if (File.Exists(file)==false)
101 sRslt = "Test/System.Drawing/" + file;
107 public void MakeTransparent()
109 string sInFile = getInFile("bitmaps/maketransparent.bmp");
110 string sOutFile = getOutSubDir() + "transparent.bmp";
112 Bitmap bmp = new Bitmap(sInFile);
113 Console.WriteLine("Bitmap loaded OK", bmp != null);
115 bmp.MakeTransparent();
118 Color color = bmp.GetPixel(1,1);
119 Assert.AreEqual (Color.Black.R, color.R);
120 Assert.AreEqual (Color.Black.G, color.G);
121 Assert.AreEqual (Color.Black.B, color.B);
127 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
128 string sOutFile = getOutSubDir() + "clone24.bmp";
130 Rectangle rect = new Rectangle(0,0,50,50);
131 Bitmap bmp = new Bitmap(sInFile);
133 Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);
135 Color colororg0 = bmp.GetPixel(0,0);
136 Color colororg50 = bmp.GetPixel(49,49);
137 Color colornew0 = bmpNew.GetPixel(0,0);
138 Color colornew50 = bmpNew.GetPixel(49,49);
140 Assert.AreEqual (colororg0, colornew0);
141 Assert.AreEqual (colororg50, colornew50);
145 public void CloneImage()
147 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
148 Bitmap bmp = new Bitmap(sInFile);
150 Bitmap bmpNew = (Bitmap) bmp.Clone ();
152 Assert.AreEqual (bmp.Width, bmpNew.Width);
153 Assert.AreEqual (bmp.Height, bmpNew.Height);
154 Assert.AreEqual (bmp.PixelFormat, bmpNew.PixelFormat);
161 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
162 Bitmap bmp = new Bitmap(sInFile);
163 int cnt = bmp.GetFrameCount(FrameDimension.Page);
164 int active = bmp.SelectActiveFrame (FrameDimension.Page, 0);
166 Assert.AreEqual (1, cnt);
167 Assert.AreEqual (0, active);
170 static string ByteArrayToString(byte[] arrInput)
173 StringBuilder sOutput = new StringBuilder(arrInput.Length);
174 for (i=0;i < arrInput.Length -1; i++)
176 sOutput.Append(arrInput[i].ToString("X2"));
178 return sOutput.ToString();
182 public string RotateBmp (Bitmap src, RotateFlipType rotate)
184 int witdh = 150, height = 150, index = 0;
185 byte[] pixels = new byte [witdh * height * 3];
191 bmp_rotate = src.Clone (new RectangleF (0,0, witdh, height), PixelFormat.Format32bppArgb);
192 bmp_rotate.RotateFlip (rotate);
194 for (int y = 0; y < height; y++) {
195 for (int x = 0; x < witdh; x++) {
196 clr = bmp_rotate.GetPixel (x,y);
197 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
201 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
202 return ByteArrayToString (hash);
207 Rotate bitmap in diffent ways, and check the result
213 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
214 Bitmap bmp = new Bitmap(sInFile);
216 Assert.AreEqual ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));
217 Assert.AreEqual ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
218 Assert.AreEqual ("15AD2ADBDC7090C0EC744D0F7ACE2F", RotateBmp (bmp, RotateFlipType.Rotate270FlipNone));
219 Assert.AreEqual ("2E10FEC1F4FD64ECC51D7CE68AEB18", RotateBmp (bmp, RotateFlipType.RotateNoneFlipX));
220 Assert.AreEqual ("E63204779B566ED01162B90B49BD9E", RotateBmp (bmp, RotateFlipType.Rotate90FlipX));
221 Assert.AreEqual ("B1ECB17B5093E13D04FF55CFCF7763", RotateBmp (bmp, RotateFlipType.Rotate180FlipX));
222 Assert.AreEqual ("71A173882C16755D86F4BC26532374", RotateBmp (bmp, RotateFlipType.Rotate270FlipX));
226 public void LockBmp (PixelFormat fmt, PixelFormat fmtlock, string output,
227 int lwidth , int lheight, ref string hash1, ref string hash2)
229 int width = 100, height = 100, bbps, cur, pos;
230 Bitmap bmp = new Bitmap (width, height, fmt);
231 Graphics gr = Graphics.FromImage (bmp);
234 byte[] btv = new byte[1];
235 int y, x, len = width * height * 4, index = 0;
236 byte[] pixels = new byte [len];
239 bbps = Image.GetPixelFormatSize (fmt);
241 Pen p = new Pen (Color.FromArgb (255, 100, 200, 250), 2);
242 gr.DrawRectangle(p, 1.0F, 1.0F, 80.0F, 80.0F);
244 BitmapData bd = bmp.LockBits (new Rectangle (0, 0, lwidth, lheight), ImageLockMode.ReadOnly, fmtlock);
246 pos = bd.Scan0.ToInt32();
247 for (y = 0; y < bd.Height; y++) {
248 for (x = 0; x < bd.Width; x++) {
251 for (int bt =0; bt < bbps/8; bt++, index++) {
256 Marshal.Copy ((IntPtr)cur, btv, 0, 1);
257 pixels[index] = btv[0];
259 /* Make change of all the colours = 250 to 10*/
262 Marshal.Copy (btv, 0, (IntPtr)cur, 1);
268 for (int i = index; i < len; i++)
271 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
274 hash1 = ByteArrayToString (hash);
276 /* MD5 of the changed bitmap*/
277 for (y = 0, index = 0; y < height; y++) {
278 for (x = 0; x < width; x++) {
279 clr = bmp.GetPixel (x,y);
280 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
284 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
285 hash2 = ByteArrayToString (hash);
287 /*bmp.Save (output, ImageFormat.Bmp);*/
290 Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns
291 firsts, changes them, and then using GetPixel does another check of the changes.
292 The results match the .Net framework
295 public void LockBitmap ()
300 /* Locks the whole bitmap*/
301 LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 100, 100, ref hash, ref hashchg);
302 Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
303 Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);
305 LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 100, 100, ref hash, ref hashchg);
306 Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
307 Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);
309 LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 100, 100, ref hash, ref hashchg);
310 Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);
311 Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);
313 LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 100, 100, ref hash, ref hashchg);
314 Assert.AreEqual ("A8A071D0B3A3743905B4E193A62769", hash);
315 Assert.AreEqual ("EEE846FA8F892339C64082DFF775CF", hashchg);
317 /* Locks a portion of the bitmap*/
318 LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 50, 50, ref hash, ref hashchg);
319 Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);
320 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
322 LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 50, 50, ref hash, ref hashchg);
323 Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);
324 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
326 LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 50, 50, ref hash, ref hashchg);
327 Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);
328 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);
330 LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 50, 50, ref hash, ref hashchg);
331 Assert.AreEqual ("FFE86628478591D1A1EB30E894C34F", hash);
332 Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);