2 // Bitmap class testing unit
5 // Jordi Mas i Hernàndez (jmas@softcatala.org>
6 // Jonathan Gilbert <logic@deltaq.org>
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // (C) 2004 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Drawing.Imaging;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Runtime.Serialization.Formatters.Binary;
39 using System.Runtime.Serialization.Formatters.Soap;
40 using System.Security.Cryptography;
41 using System.Security.Permissions;
43 using System.Xml.Serialization;
44 using NUnit.Framework;
46 namespace MonoTests.System.Drawing {
49 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
51 [Category ("NotWorking")]
53 public class TestBitmap {
56 public void TestPixels()
58 // Tests GetSetPixel/SetPixel
59 Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
60 bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));
61 Color color = bmp.GetPixel(0,0);
63 Assert.AreEqual (Color.FromArgb(255,128,128,128), color);
65 bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));
66 Color color2 = bmp.GetPixel(99,99);
67 Assert.AreEqual (Color.FromArgb(255,255,0,155), color2);
71 public void LockBits_32_32_NonIndexedWrite ()
73 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
74 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
75 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
76 Assert.AreEqual (100, data.Height, "Height");
77 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
78 Assert.AreEqual (400, data.Stride, "Stride");
79 Assert.AreEqual (100, data.Width, "Width");
80 bmp.UnlockBits (data);
85 public void LockBits_32_24_NonIndexedWrite ()
87 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
88 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
89 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
90 Assert.AreEqual (100, data.Height, "Height");
91 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
92 Assert.AreEqual (300, data.Stride, "Stride");
93 Assert.AreEqual (100, data.Width, "Width");
94 bmp.UnlockBits (data);
99 public void LockBits_24_24_NonIndexedWrite ()
101 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
102 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
103 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
104 Assert.AreEqual (100, data.Height, "Height");
105 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
106 Assert.AreEqual (300, data.Stride, "Stride");
107 Assert.AreEqual (100, data.Width, "Width");
108 bmp.UnlockBits (data);
113 public void LockBits_24_32_NonIndexedWrite ()
115 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
116 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
117 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
118 Assert.AreEqual (100, data.Height, "Height");
119 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
120 Assert.AreEqual (400, data.Stride, "Stride");
121 Assert.AreEqual (100, data.Width, "Width");
122 bmp.UnlockBits (data);
127 [ExpectedException (typeof (ArgumentException))]
128 public void LockBits_IndexedWrite_NonIndexed ()
130 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format8bppIndexed)) {
131 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
132 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
137 [ExpectedException (typeof (ArgumentException))]
138 public void LockBits_NonIndexedWrite_ToIndexed ()
140 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
141 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
143 BitmapData bd = new BitmapData ();
145 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed, bd);
147 catch (ArgumentException) {
148 // test to see if there's a leak or not in this case
149 Assert.AreEqual (IntPtr.Zero, bd.Scan0, "Scan0");
153 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
159 public void LockBits_IndexedWrite_SameIndexedFormat ()
161 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format8bppIndexed)) {
162 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
163 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
164 Assert.AreEqual (100, data.Height, "Height");
165 Assert.AreEqual (PixelFormat.Format8bppIndexed, data.PixelFormat, "PixelFormat");
166 Assert.AreEqual (100, data.Stride, "Stride");
167 Assert.AreEqual (100, data.Width, "Width");
168 bmp.UnlockBits (data);
173 public void LockBits_ImageLockMode_Invalid ()
175 using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format24bppRgb)) {
176 Rectangle r = new Rectangle (4, 4, 4, 4);
177 BitmapData data = bmp.LockBits (r, (ImageLockMode)0, PixelFormat.Format24bppRgb);
179 Assert.AreEqual (4, data.Height, "Height");
180 Assert.AreEqual (4, data.Width, "Width");
181 Assert.IsTrue (data.Stride >= 12, "Stride");
182 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
183 Assert.IsFalse (IntPtr.Zero.Equals (data.Scan0), "Scan0");
186 bmp.UnlockBits (data);
192 [ExpectedException (typeof (InvalidOperationException))]
193 public void LockBits_Double ()
195 using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format24bppRgb)) {
196 Rectangle r = new Rectangle (4, 4, 4, 4);
197 BitmapData data = bmp.LockBits (r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
199 bmp.LockBits (r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
202 bmp.UnlockBits (data);
208 [ExpectedException (typeof (ArgumentException))]
209 public void LockBits_Disposed ()
211 Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);
212 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
214 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
218 [ExpectedException (typeof (ArgumentException))]
219 [Category ("Valgrind")] // this test is known to leak memory (API design limitation)
220 public void UnlockBits_Disposed ()
222 Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);
223 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
224 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
226 bmp.UnlockBits (data);
227 // and that results in something like this when executed under Valgrind
228 // "40,000 bytes in 1 blocks are possibly lost in loss record 88 of 92"
232 [ExpectedException (typeof (ArgumentException))]
233 public void UnlockBits_Null ()
235 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
236 bmp.UnlockBits (null);
241 [ExpectedException (typeof (ArgumentException))]
243 [Ignore ("Bitmap.LockBits is not implemented")]
245 public void LockBits_BitmapData_Null ()
248 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
249 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
250 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb, null);
257 [Ignore ("Bitmap.LockBits is not implemented")]
259 public void LockBits_32_32_BitmapData ()
262 BitmapData data = new BitmapData ();
263 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
264 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
265 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb, data);
266 Assert.AreEqual (100, data.Height, "Height");
267 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
268 Assert.AreEqual (400, data.Stride, "Stride");
269 Assert.AreEqual (100, data.Width, "Width");
270 bmp.UnlockBits (data);
277 [Ignore ("Bitmap.LockBits is not implemented")]
279 public void LockBits_32_24_BitmapData ()
282 BitmapData data = new BitmapData ();
283 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
284 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
285 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb, data);
286 Assert.AreEqual (100, data.Height, "Height");
287 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
288 Assert.AreEqual (300, data.Stride, "Stride");
289 Assert.AreEqual (100, data.Width, "Width");
290 bmp.UnlockBits (data);
297 [Ignore ("Bitmap.LockBits is not implemented")]
299 public void LockBits_24_24_BitmapData ()
302 BitmapData data = new BitmapData ();
303 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
304 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
305 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb, data);
306 Assert.AreEqual (100, data.Height, "Height");
307 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
308 Assert.AreEqual (300, data.Stride, "Stride");
309 Assert.AreEqual (100, data.Width, "Width");
310 bmp.UnlockBits (data);
317 [Ignore ("Bitmap.LockBits is not implemented")]
319 public void LockBits_24_32_BitmapData ()
322 BitmapData data = new BitmapData ();
323 using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
324 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
325 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb, data);
326 Assert.AreEqual (100, data.Height, "Height");
327 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
328 Assert.AreEqual (400, data.Stride, "Stride");
329 Assert.AreEqual (100, data.Width, "Width");
330 bmp.UnlockBits (data);
336 private void FormatTest (PixelFormat format)
338 bool alpha = Image.IsAlphaPixelFormat (format);
339 int size = Image.GetPixelFormatSize (format) / 8 * 2;
340 using (Bitmap bmp = new Bitmap (2, 1, format)) {
341 Color a = Color.FromArgb (128, 64, 32, 16);
342 Color b = Color.FromArgb (192, 96, 48, 24);
343 bmp.SetPixel (0, 0, a);
344 bmp.SetPixel (1, 0, b);
345 Color c = bmp.GetPixel (0, 0);
346 Color d = bmp.GetPixel (1, 0);
348 if (format == PixelFormat.Format32bppPArgb) {
349 Assert.AreEqual (a.A, c.A, "0,0-alpha-A");
350 // note sure why the -1
351 Assert.AreEqual (a.R - 1, c.R, "0,0-alpha-premultiplied-R");
352 Assert.AreEqual (a.G - 1, c.G, "0,0-alpha-premultiplied-G");
353 Assert.AreEqual (a.B - 1, c.B, "0,0-alpha-premultiplied-B");
355 Assert.AreEqual (b.A, d.A, "1,0-alpha-A");
356 // note sure why the -1
357 Assert.AreEqual (b.R - 1, d.R, "1,0-alpha-premultiplied-R");
358 Assert.AreEqual (b.G - 1, d.G, "1,0-alpha-premultiplied-G");
359 Assert.AreEqual (b.B - 1, d.B, "1,0-alpha-premultiplied-B");
361 Assert.AreEqual (a, c, "0,0-alpha");
362 Assert.AreEqual (b, d, "1,0-alpha");
365 Assert.AreEqual (Color.FromArgb (255, 64, 32, 16), c, "0,0-non-alpha");
366 Assert.AreEqual (Color.FromArgb (255, 96, 48, 24), d, "1,0-non-alpha");
368 BitmapData bd = bmp.LockBits (new Rectangle (0, 0, 2, 1), ImageLockMode.ReadOnly, format);
370 byte[] data = new byte[size];
371 Marshal.Copy (bd.Scan0, data, 0, size);
372 if (format == PixelFormat.Format32bppPArgb) {
373 Assert.AreEqual (Math.Ceiling ((float)c.B * c.A / 255), data[0], "0.alpha-premultiplied-B");
374 Assert.AreEqual (Math.Ceiling ((float)c.G * c.A / 255), data[1], "0.alpha-premultiplied-R");
375 Assert.AreEqual (Math.Ceiling ((float)c.R * c.A / 255), data[2], "0.alpha-premultiplied-G");
376 Assert.AreEqual (c.A, data[3], "0.alpha-A");
377 Assert.AreEqual (Math.Ceiling ((float)d.B * d.A / 255), data[4], "1.alpha-premultiplied-B");
378 Assert.AreEqual (Math.Ceiling ((float)d.G * d.A / 255), data[5], "1.alpha-premultiplied-R");
379 Assert.AreEqual (Math.Ceiling ((float)d.R * d.A / 255), data[6], "1.alpha-premultiplied-G");
380 Assert.AreEqual (d.A, data[7], "1.alpha-A");
383 Assert.AreEqual (c.B, data[n++], "0.B");
384 Assert.AreEqual (c.G, data[n++], "0.R");
385 Assert.AreEqual (c.R, data[n++], "0.G");
387 Assert.AreEqual (c.A, data[n++], "0.A");
388 Assert.AreEqual (d.B, data[n++], "1.B");
389 Assert.AreEqual (d.G, data[n++], "1.R");
390 Assert.AreEqual (d.R, data[n++], "1.G");
392 Assert.AreEqual (d.A, data[n++], "1.A");
402 public void Format32bppArgb ()
404 FormatTest (PixelFormat.Format32bppArgb);
408 [Category ("NotWorking")] // I'm not sure we're handling this format anywhere (Cairo itself use it)
409 public void Format32bppPArgb ()
411 FormatTest (PixelFormat.Format32bppPArgb);
415 public void Format32bppRgb ()
417 FormatTest (PixelFormat.Format32bppRgb);
421 public void Format24bppRgb ()
423 FormatTest (PixelFormat.Format24bppRgb);
426 /* Get the output directory depending on the runtime and location*/
427 public static string getOutSubDir()
431 if (Environment.GetEnvironmentVariable("MSNet")==null)
436 sRslt = Path.GetFullPath (sSub);
438 if (Directory.Exists(sRslt) == false)
439 sRslt = "Test/System.Drawing/" + sSub;
441 if (sRslt.Length > 0)
442 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
448 /* Get the input directory depending on the runtime*/
449 public static string getInFile(string file)
451 string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
452 if (!File.Exists (sRslt))
453 sRslt = "Test/System.Drawing/" + file;
457 // note: this test fails when saving (for the same reason) on Mono and MS.NET
459 public void MakeTransparent()
461 string sInFile = getInFile("bitmaps/maketransparent.bmp");
462 string sOutFile = getOutSubDir() + "transparent.bmp";
464 Bitmap bmp = new Bitmap(sInFile);
466 bmp.MakeTransparent();
469 Color color = bmp.GetPixel(1,1);
470 Assert.AreEqual (Color.Black.R, color.R);
471 Assert.AreEqual (Color.Black.G, color.G);
472 Assert.AreEqual (Color.Black.B, color.B);
478 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
479 Rectangle rect = new Rectangle(0,0,50,50);
480 Bitmap bmp = new Bitmap(sInFile);
482 Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);
483 Color colororg0 = bmp.GetPixel(0,0);
484 Color colororg50 = bmp.GetPixel(49,49);
485 Color colornew0 = bmpNew.GetPixel(0,0);
486 Color colornew50 = bmpNew.GetPixel(49,49);
488 Assert.AreEqual (colororg0, colornew0);
489 Assert.AreEqual (colororg50, colornew50);
493 public void CloneImage()
495 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
496 Bitmap bmp = new Bitmap(sInFile);
498 Bitmap bmpNew = (Bitmap) bmp.Clone ();
500 Assert.AreEqual (bmp.Width, bmpNew.Width);
501 Assert.AreEqual (bmp.Height, bmpNew.Height);
502 Assert.AreEqual (bmp.PixelFormat, bmpNew.PixelFormat);
509 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
510 Bitmap bmp = new Bitmap(sInFile);
511 int cnt = bmp.GetFrameCount(FrameDimension.Page);
512 int active = bmp.SelectActiveFrame (FrameDimension.Page, 0);
514 Assert.AreEqual (1, cnt);
515 Assert.AreEqual (0, active);
519 [ExpectedException (typeof (ArgumentException))]
521 [Category ("NotWorking")]
523 public void FileDoesNotExists ()
525 Bitmap bmp = new Bitmap ("FileDoesNotExists.jpg");
528 static string ByteArrayToString(byte[] arrInput)
531 StringBuilder sOutput = new StringBuilder(arrInput.Length);
532 for (i=0;i < arrInput.Length -1; i++)
534 sOutput.Append(arrInput[i].ToString("X2"));
536 return sOutput.ToString();
540 public string RotateBmp (Bitmap src, RotateFlipType rotate)
542 int width = 150, height = 150, index = 0;
543 byte[] pixels = new byte [width * height * 3];
548 bmp_rotate = src.Clone (new RectangleF (0,0, width, height), PixelFormat.Format32bppArgb);
549 bmp_rotate.RotateFlip (rotate);
551 for (int y = 0; y < height; y++) {
552 for (int x = 0; x < width; x++) {
553 clr = bmp_rotate.GetPixel (x,y);
554 pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++] = clr.B;
558 hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
559 return ByteArrayToString (hash);
562 public string RotateIndexedBmp (Bitmap src, RotateFlipType type)
566 switch (src.PixelFormat)
568 case PixelFormat.Format1bppIndexed: pixels_per_byte = 8; break;
569 case PixelFormat.Format4bppIndexed: pixels_per_byte = 2; break;
570 case PixelFormat.Format8bppIndexed: pixels_per_byte = 1; break;
572 default: throw new Exception("Cannot pass a bitmap of format " + src.PixelFormat + " to RotateIndexedBmp");
575 Bitmap test = src.Clone () as Bitmap;
577 test.RotateFlip (type);
579 BitmapData data = null;
584 data = test.LockBits (new Rectangle (0, 0, test.Width, test.Height), ImageLockMode.ReadOnly, test.PixelFormat);
586 int scan_size = (data.Width + pixels_per_byte - 1) / pixels_per_byte;
587 pixel_data = new byte[data.Height * scan_size];
589 for (int y=0; y < data.Height; y++) {
590 IntPtr src_ptr = (IntPtr)(y * data.Stride + data.Scan0.ToInt64 ());
591 int dest_offset = y * scan_size;
592 for (int x=0; x < scan_size; x++)
593 pixel_data[dest_offset + x] = Marshal.ReadByte (src_ptr, x);
600 try { test.UnlockBits(data); } catch {}
602 try { test.Dispose(); } catch {}
606 if (pixel_data == null)
609 byte[] hash = new MD5CryptoServiceProvider().ComputeHash (pixel_data);
610 return ByteArrayToString (hash);
616 Rotate bitmap in diffent ways, and check the result
622 string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
623 Bitmap bmp = new Bitmap(sInFile);
625 Assert.AreEqual ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));
626 Assert.AreEqual ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
627 Assert.AreEqual ("15AD2ADBDC7090C0EC744D0F7ACE2F", RotateBmp (bmp, RotateFlipType.Rotate270FlipNone));
628 Assert.AreEqual ("2E10FEC1F4FD64ECC51D7CE68AEB18", RotateBmp (bmp, RotateFlipType.RotateNoneFlipX));
629 Assert.AreEqual ("E63204779B566ED01162B90B49BD9E", RotateBmp (bmp, RotateFlipType.Rotate90FlipX));
630 Assert.AreEqual ("B1ECB17B5093E13D04FF55CFCF7763", RotateBmp (bmp, RotateFlipType.Rotate180FlipX));
631 Assert.AreEqual ("71A173882C16755D86F4BC26532374", RotateBmp (bmp, RotateFlipType.Rotate270FlipX));
637 Rotate 1- and 4-bit bitmaps in different ways and check the
638 resulting pixels using MD5
641 public void Rotate1bit4bit()
643 if ((Environment.OSVersion.Platform != (PlatformID)4)
644 && (Environment.OSVersion.Platform != (PlatformID)128))
645 Assert.Ignore("This does not work with Microsoft's GDIPLUS.DLL due to off-by-1 errors in their GdipBitmapRotateFlip function.");
648 getInFile ("bitmaps/1bit.png"),
649 getInFile ("bitmaps/4bit.png")
652 StringBuilder md5s = new StringBuilder();
654 foreach (string file in files)
655 using (Bitmap bmp = new Bitmap(file))
656 foreach (RotateFlipType type in Enum.GetValues (typeof(RotateFlipType)))
657 md5s.Append (RotateIndexedBmp (bmp, type));
659 using (StreamWriter writer = new StreamWriter("/tmp/md5s.txt"))
660 writer.WriteLine(md5s);
663 "A4DAF507C92BDE10626BC7B34FEFE5" + // 1-bit RotateNoneFlipNone
664 "A4DAF507C92BDE10626BC7B34FEFE5" + // 1-bit Rotate180FlipXY
665 "C0975EAFD2FC1CC9CC7AF20B92FC9F" + // 1-bit Rotate90FlipNone
666 "C0975EAFD2FC1CC9CC7AF20B92FC9F" + // 1-bit Rotate270FlipXY
667 "64AE60858A02228F7B1B18C7812FB6" + // 1-bit Rotate180FlipNone
668 "64AE60858A02228F7B1B18C7812FB6" + // 1-bit RotateNoneFlipXY
669 "E96D3390938350F9DE2608C4364424" + // 1-bit Rotate270FlipNone
670 "E96D3390938350F9DE2608C4364424" + // 1-bit Rotate90FlipXY
671 "23947CE822C1DDE6BEA69C01F8D0D9" + // 1-bit RotateNoneFlipX
672 "23947CE822C1DDE6BEA69C01F8D0D9" + // 1-bit Rotate180FlipY
673 "BE45F685BDEBD7079AA1B2CBA46723" + // 1-bit Rotate90FlipX
674 "BE45F685BDEBD7079AA1B2CBA46723" + // 1-bit Rotate270FlipY
675 "353E937CFF31B1BF6C3DD0A031ACB5" + // 1-bit Rotate180FlipX
676 "353E937CFF31B1BF6C3DD0A031ACB5" + // 1-bit RotateNoneFlipY
677 "AEA18A770A845E25B6A8CE28DD6DCB" + // 1-bit Rotate270FlipX
678 "AEA18A770A845E25B6A8CE28DD6DCB" + // 1-bit Rotate90FlipY
679 "3CC874B571902366AACED5D619E87D" + // 4-bit RotateNoneFlipNone
680 "3CC874B571902366AACED5D619E87D" + // 4-bit Rotate180FlipXY
681 "8DE25C7E1BE4A3B535DB5D83198D83" + // 4-bit Rotate90FlipNone
682 "8DE25C7E1BE4A3B535DB5D83198D83" + // 4-bit Rotate270FlipXY
683 "27CF5E9CE70BE9EBC47FB996721B95" + // 4-bit Rotate180FlipNone
684 "27CF5E9CE70BE9EBC47FB996721B95" + // 4-bit RotateNoneFlipXY
685 "A919CCB8F97CAD7DC1F01026D11A5D" + // 4-bit Rotate270FlipNone
686 "A919CCB8F97CAD7DC1F01026D11A5D" + // 4-bit Rotate90FlipXY
687 "545876C99ACF833E69FBFFBF436034" + // 4-bit RotateNoneFlipX
688 "545876C99ACF833E69FBFFBF436034" + // 4-bit Rotate180FlipY
689 "5DB56687757CDEFC52D89C77CA9223" + // 4-bit Rotate90FlipX
690 "5DB56687757CDEFC52D89C77CA9223" + // 4-bit Rotate270FlipY
691 "05A77EDDCDF20D5B0AC0169E95D7D7" + // 4-bit Rotate180FlipX
692 "05A77EDDCDF20D5B0AC0169E95D7D7" + // 4-bit RotateNoneFlipY
693 "B6B6245796C836923ABAABDF368B29" + // 4-bit Rotate270FlipX
694 "B6B6245796C836923ABAABDF368B29", // 4-bit Rotate90FlipY
698 private Bitmap CreateBitmap (int width, int height, PixelFormat fmt)
700 Bitmap bmp = new Bitmap (width, height, fmt);
701 using (Graphics gr = Graphics.FromImage (bmp)) {
702 Color c = Color.FromArgb (255, 100, 200, 250);
703 for (int x = 1; x < 80; x++) {
704 bmp.SetPixel (x, 1, c);
705 bmp.SetPixel (x, 2, c);
706 bmp.SetPixel (x, 78, c);
707 bmp.SetPixel (x, 79, c);
709 for (int y = 3; y < 78; y++) {
710 bmp.SetPixel (1, y, c);
711 bmp.SetPixel (2, y, c);
712 bmp.SetPixel (78, y, c);
713 bmp.SetPixel (79, y, c);
719 private byte[] HashPixels (Bitmap bmp)
721 int len = bmp.Width * bmp.Height * 4;
723 byte[] pixels = new byte [len];
725 for (int y = 0; y < bmp.Height; y++) {
726 for (int x = 0; x < bmp.Width; x++) {
727 Color clr = bmp.GetPixel (x, y);
728 pixels[index++] = clr.R;
729 pixels[index++] = clr.G;
730 pixels[index++] = clr.B;
733 return MD5.Create ().ComputeHash (pixels);
736 private byte[] HashLock (Bitmap bmp, int width, int height, PixelFormat fmt, ImageLockMode mode)
738 int len = bmp.Width * bmp.Height * 4;
739 byte[] pixels = new byte[len];
740 BitmapData bd = bmp.LockBits (new Rectangle (0, 0, width, height), mode, fmt);
743 int bbps = Image.GetPixelFormatSize (fmt);
744 int pos = bd.Scan0.ToInt32 ();
745 byte[] btv = new byte[1];
746 for (int y = 0; y < bd.Height; y++) {
747 for (int x = 0; x < bd.Width; x++) {
750 for (int bt = 0; bt < bbps / 8; bt++, index++) {
752 cur += y * bd.Stride;
755 Marshal.Copy ((IntPtr) cur, btv, 0, 1);
756 pixels[index] = btv[0];
758 /* Make change of all the colours = 250 to 10*/
761 Marshal.Copy (btv, 0, (IntPtr) cur, 1);
767 for (int i = index; i < len; i++)
773 return MD5.Create ().ComputeHash (pixels);
777 Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns
778 firsts, changes them, and then using GetPixel does another check of the changes.
779 The results match the .Net framework
781 private static byte[] DefaultBitmapHash = new byte[] { 0xD8, 0xD3, 0x68, 0x9C, 0x86, 0x7F, 0xB6, 0xA0, 0x76, 0xD6, 0x00, 0xEF, 0xFF, 0xE5, 0x8E, 0x1B };
782 private static byte[] FinalWholeBitmapHash = new byte[] { 0x5F, 0x52, 0x98, 0x37, 0xE3, 0x94, 0xE1, 0xA6, 0x06, 0x6C, 0x5B, 0xF1, 0xA9, 0xC2, 0xA9, 0x43 };
785 public void LockBitmap_Format32bppArgb_Format32bppArgb_ReadWrite_Whole ()
787 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
788 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
789 byte[] expected = { 0x89, 0x6A, 0x6B, 0x35, 0x5C, 0x89, 0xD9, 0xE9, 0xF4, 0x51, 0xD5, 0x89, 0xED, 0x28, 0x68, 0x5C };
790 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format32bppArgb, ImageLockMode.ReadWrite);
791 Assert.AreEqual (expected, actual, "Full-Format32bppArgb");
792 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
797 public void LockBitmap_Format32bppArgb_Format32bppPArgb_ReadWrite_Whole ()
799 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
800 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
801 byte[] expected = { 0x89, 0x6A, 0x6B, 0x35, 0x5C, 0x89, 0xD9, 0xE9, 0xF4, 0x51, 0xD5, 0x89, 0xED, 0x28, 0x68, 0x5C };
802 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format32bppPArgb, ImageLockMode.ReadWrite);
803 Assert.AreEqual (expected, actual, "Full-Format32bppPArgb");
804 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
809 public void LockBitmap_Format32bppArgb_Format32bppRgb_ReadWrite_Whole ()
811 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
812 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
813 byte[] expected = { 0xC0, 0x28, 0xB5, 0x2E, 0x86, 0x90, 0x6F, 0x37, 0x09, 0x5F, 0x49, 0xA4, 0x91, 0xDA, 0xEE, 0xB9 };
814 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format32bppRgb, ImageLockMode.ReadWrite);
815 Assert.AreEqual (expected, actual, "Full-Format32bppRgb");
816 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
821 public void LockBitmap_Format32bppArgb_Format24bppRgb_ReadWrite_Whole ()
823 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
824 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
825 byte[] expected = { 0xA7, 0xB2, 0x50, 0x04, 0x11, 0x12, 0x64, 0x68, 0x6B, 0x7D, 0x2F, 0x6E, 0x69, 0x24, 0xCB, 0x14 };
826 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format24bppRgb, ImageLockMode.ReadWrite);
827 Assert.AreEqual (expected, actual, "Full-Format24bppRgb");
828 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
832 private static byte[] FinalPartialBitmapHash = new byte[] { 0xED, 0xD8, 0xDC, 0x9B, 0x44, 0x00, 0x22, 0x9B, 0x07, 0x06, 0x4A, 0x21, 0x70, 0xA7, 0x31, 0x1D };
835 public void LockBitmap_Format32bppArgb_Format32bppArgb_ReadWrite_Partial ()
837 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
838 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
839 byte[] expected = { 0x5D, 0xFF, 0x02, 0x34, 0xEB, 0x7C, 0xF7, 0x42, 0xD4, 0xB7, 0x70, 0x49, 0xB4, 0x06, 0x79, 0xBC };
840 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format32bppArgb, ImageLockMode.ReadWrite);
841 Assert.AreEqual (expected, actual, "Partial-Format32bppArgb");
842 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
847 public void LockBitmap_Format32bppArgb_Format32bppPArgb_ReadWrite_Partial ()
849 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
850 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
851 byte[] expected = { 0x5D, 0xFF, 0x02, 0x34, 0xEB, 0x7C, 0xF7, 0x42, 0xD4, 0xB7, 0x70, 0x49, 0xB4, 0x06, 0x79, 0xBC };
852 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format32bppPArgb, ImageLockMode.ReadWrite);
853 Assert.AreEqual (expected, actual, "Partial-Format32bppPArgb");
854 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
859 public void LockBitmap_Format32bppArgb_Format32bppRgb_ReadWrite_Partial ()
861 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
862 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
863 byte[] expected = { 0x72, 0x33, 0x09, 0x67, 0x53, 0x65, 0x38, 0xF9, 0xE4, 0x58, 0xE1, 0x0A, 0xAA, 0x6A, 0xCC, 0xB8 };
864 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format32bppRgb, ImageLockMode.ReadWrite);
865 Assert.AreEqual (expected, actual, "Partial-Format32bppRgb");
866 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
871 public void LockBitmap_Format32bppArgb_Format24bppRgb_ReadWrite_Partial ()
873 using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
874 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
875 byte[] expected = { 0x4D, 0x39, 0x21, 0x88, 0xC2, 0x17, 0x14, 0x5F, 0x89, 0x9E, 0x02, 0x75, 0xF3, 0x64, 0xD8, 0xF0 };
876 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format24bppRgb, ImageLockMode.ReadWrite);
877 Assert.AreEqual (expected, actual, "Partial-Format24bppRgb");
878 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
883 Tests the LockBitmap and UnlockBitmap functions, specifically the copying
884 of bitmap data in the directions indicated by the ImageLockMode.
887 public void LockUnlockBitmap()
893 Color red = Color.FromArgb (Color.Red.A, Color.Red.R, Color.Red.G, Color.Red.B);
894 Color blue = Color.FromArgb (Color.Blue.A, Color.Blue.R, Color.Blue.G, Color.Blue.B);
896 using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format32bppRgb))
898 bmp.SetPixel (0, 0, red);
899 pixel_colour = bmp.GetPixel (0, 0);
900 Assert.AreEqual (red, pixel_colour, "Set/Get-Red");
902 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
904 pixel_value = Marshal.ReadByte (data.Scan0, 0);
905 pixel_value |= Marshal.ReadByte (data.Scan0, 1) << 8;
906 pixel_value |= Marshal.ReadByte (data.Scan0, 2) << 16;
907 pixel_value |= Marshal.ReadByte (data.Scan0, 3) << 24;
909 pixel_colour = Color.FromArgb (pixel_value);
911 // Disregard alpha information in the test
912 pixel_colour = Color.FromArgb(red.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
914 Assert.AreEqual (red, pixel_colour, "Red-FromLockedBitmap");
916 Marshal.WriteInt32 (data.Scan0, blue.ToArgb ());
918 bmp.UnlockBits (data);
920 pixel_colour = bmp.GetPixel (0, 0);
922 // Disregard alpha information in the test
923 pixel_colour = Color.FromArgb(red.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
925 Assert.AreEqual (red, pixel_colour);
927 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
929 Marshal.WriteInt32 (data.Scan0, blue.ToArgb ());
931 bmp.UnlockBits (data);
933 pixel_colour = bmp.GetPixel (0, 0);
935 // Disregard alpha information in the test
936 pixel_colour = Color.FromArgb(blue.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
938 Assert.AreEqual (blue, pixel_colour);
941 using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
943 bmp.SetPixel (0, 0, red);
945 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
949 b = Marshal.ReadByte (data.Scan0, 0);
950 g = Marshal.ReadByte (data.Scan0, 1);
951 r = Marshal.ReadByte (data.Scan0, 2);
952 pixel_colour = Color.FromArgb (red.A, r, g, b);
954 Assert.AreEqual (red, pixel_colour);
956 Marshal.WriteByte (data.Scan0, 0, blue.B);
957 Marshal.WriteByte (data.Scan0, 1, blue.G);
958 Marshal.WriteByte (data.Scan0, 2, blue.R);
960 bmp.UnlockBits (data);
962 pixel_colour = bmp.GetPixel (0, 0);
964 // Disregard alpha information in the test
965 pixel_colour = Color.FromArgb(red.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
967 Assert.AreEqual (red, bmp.GetPixel (0, 0));
969 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
971 Marshal.WriteByte (data.Scan0, 0, blue.B);
972 Marshal.WriteByte (data.Scan0, 1, blue.G);
973 Marshal.WriteByte (data.Scan0, 2, blue.R);
975 bmp.UnlockBits(data);
977 pixel_colour = bmp.GetPixel (0, 0);
979 // Disregard alpha information in the test
980 pixel_colour = Color.FromArgb(blue.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
982 Assert.AreEqual (blue, bmp.GetPixel (0, 0));
987 public void DefaultFormat1 ()
989 using (Bitmap bmp = new Bitmap (20, 20)) {
990 Assert.AreEqual (ImageFormat.MemoryBmp, bmp.RawFormat);
995 public void DefaultFormat2 ()
997 string filename = Path.GetTempFileName ();
998 using (Bitmap bmp = new Bitmap (20, 20)) {
1002 using (Bitmap other = new Bitmap (filename)) {
1003 Assert.AreEqual (ImageFormat.Png, other.RawFormat);
1005 File.Delete (filename);
1009 public void BmpDataStride1 ()
1011 Bitmap bmp = new Bitmap (184, 184, PixelFormat.Format1bppIndexed);
1012 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
1014 Assert.AreEqual (24, data.Stride);
1016 bmp.UnlockBits (data);
1021 private Stream Serialize (object o)
1023 MemoryStream ms = new MemoryStream ();
1024 IFormatter formatter = new BinaryFormatter ();
1025 formatter.Serialize (ms, o);
1030 private object Deserialize (Stream s)
1032 return new BinaryFormatter ().Deserialize (s);
1036 public void Serialize_Icon ()
1038 // this cause a problem with resgen, see http://bugzilla.ximian.com/show_bug.cgi?id=80565
1039 string filename = getInFile ("bitmaps/16x16x16.ico");
1040 using (Bitmap icon = new Bitmap (filename)) {
1041 using (Stream s = Serialize (icon)) {
1042 using (Bitmap copy = (Bitmap)Deserialize (s)) {
1043 Assert.AreEqual (icon.Height, copy.Height, "Height");
1044 Assert.AreEqual (icon.Width, copy.Width, "Width");
1045 Assert.AreEqual (icon.PixelFormat, copy.PixelFormat, "PixelFormat");
1046 Assert.IsTrue (icon.RawFormat.Equals (ImageFormat.Icon), "Icon");
1047 Assert.IsTrue (copy.RawFormat.Equals (ImageFormat.Png), "Png");
1053 private Stream SoapSerialize (object o)
1055 MemoryStream ms = new MemoryStream ();
1056 IFormatter formatter = new SoapFormatter ();
1057 formatter.Serialize (ms, o);
1062 private object SoapDeserialize (Stream s)
1064 return new SoapFormatter ().Deserialize (s);
1068 public void SoapSerialize_Icon ()
1070 string filename = getInFile ("bitmaps/16x16x16.ico");
1071 using (Bitmap icon = new Bitmap (filename)) {
1072 using (Stream s = SoapSerialize (icon)) {
1073 using (Bitmap copy = (Bitmap) SoapDeserialize (s)) {
1074 Assert.AreEqual (icon.Height, copy.Height, "Height");
1075 Assert.AreEqual (icon.Width, copy.Width, "Width");
1076 Assert.AreEqual (icon.PixelFormat, copy.PixelFormat, "PixelFormat");
1077 Assert.AreEqual (16, icon.Palette.Entries.Length, "icon Palette");
1078 Assert.IsTrue (icon.RawFormat.Equals (ImageFormat.Icon), "Icon");
1079 Assert.AreEqual (0, copy.Palette.Entries.Length, "copy Palette");
1080 Assert.IsTrue (copy.RawFormat.Equals (ImageFormat.Png), "Png");
1087 public void SoapSerialize_Bitmap8 ()
1089 string filename = getInFile ("bitmaps/almogaver8bits.bmp");
1090 using (Bitmap bmp = new Bitmap (filename)) {
1091 using (Stream s = SoapSerialize (bmp)) {
1092 using (Bitmap copy = (Bitmap) SoapDeserialize (s)) {
1093 Assert.AreEqual (bmp.Height, copy.Height, "Height");
1094 Assert.AreEqual (bmp.Width, copy.Width, "Width");
1095 Assert.AreEqual (bmp.PixelFormat, copy.PixelFormat, "PixelFormat");
1096 Assert.AreEqual (256, copy.Palette.Entries.Length, "Palette");
1097 Assert.AreEqual (bmp.RawFormat, copy.RawFormat, "RawFormat");
1104 public void SoapSerialize_Bitmap24 ()
1106 string filename = getInFile ("bitmaps/almogaver24bits.bmp");
1107 using (Bitmap bmp = new Bitmap (filename)) {
1108 using (Stream s = SoapSerialize (bmp)) {
1109 using (Bitmap copy = (Bitmap) SoapDeserialize (s)) {
1110 Assert.AreEqual (bmp.Height, copy.Height, "Height");
1111 Assert.AreEqual (bmp.Width, copy.Width, "Width");
1112 Assert.AreEqual (bmp.PixelFormat, copy.PixelFormat, "PixelFormat");
1113 Assert.AreEqual (bmp.Palette.Entries.Length, copy.Palette.Entries.Length, "Palette");
1114 Assert.AreEqual (bmp.RawFormat, copy.RawFormat, "RawFormat");
1122 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
1124 [ExpectedException (typeof (InvalidOperationException))]
1126 public void XmlSerialize ()
1128 new XmlSerializer (typeof (Bitmap));
1131 static int[] palette1 = {
1137 public void Format1bppIndexed_Palette ()
1139 using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format1bppIndexed)) {
1140 ColorPalette pal = bmp.Palette;
1141 Assert.AreEqual (2, pal.Entries.Length, "Length");
1142 for (int i = 0; i < pal.Entries.Length; i++) {
1143 Assert.AreEqual (palette1[i], pal.Entries[i].ToArgb (), i.ToString ());
1145 Assert.AreEqual (2, pal.Flags, "Flags");
1149 static int[] palette16 = {
1169 public void Format4bppIndexed_Palette ()
1171 using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format4bppIndexed)) {
1172 ColorPalette pal = bmp.Palette;
1173 Assert.AreEqual (16, pal.Entries.Length, "Length");
1174 for (int i = 0; i < pal.Entries.Length; i++) {
1175 Assert.AreEqual (palette16 [i], pal.Entries[i].ToArgb (), i.ToString ());
1177 Assert.AreEqual (0, pal.Flags, "Flags");
1181 static int[] palette256 = {
1441 public void Format8bppIndexed_Palette ()
1443 using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format8bppIndexed)) {
1444 ColorPalette pal = bmp.Palette;
1445 Assert.AreEqual (256, pal.Entries.Length, "Length");
1446 for (int i = 0; i < pal.Entries.Length; i++) {
1447 Assert.AreEqual (palette256[i], pal.Entries[i].ToArgb (), i.ToString ());
1449 Assert.AreEqual (4, pal.Flags, "Flags");
1456 [Ignore ("Unsafe code is not supported")]
1458 public class BitmapFullTrustTest {
1460 // BitmapFromHicon## is *almost* the same as IconTest.Icon##ToBitmap except
1461 // for the Flags property
1463 private void HiconTest (string msg, Bitmap b, int size)
1465 Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, msg + ".PixelFormat");
1466 // unlike the GDI+ icon decoder the palette isn't kept
1467 Assert.AreEqual (0, b.Palette.Entries.Length, msg + ".Palette");
1468 Assert.AreEqual (size, b.Height, msg + ".Height");
1469 Assert.AreEqual (size, b.Width, msg + ".Width");
1470 Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), msg + ".RawFormat");
1471 Assert.AreEqual (335888, b.Flags, msg + ".Flags");
1475 public void Hicon16 ()
1479 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
1481 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1482 HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1483 hicon = bitmap.GetHicon ();
1486 using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1487 // hicon survives bitmap and icon disposal
1488 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1493 public void Hicon32 ()
1497 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/32x32x16.ico"))) {
1499 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1500 HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1501 hicon = bitmap.GetHicon ();
1504 using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1505 // hicon survives bitmap and icon disposal
1506 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1511 [ExpectedException (typeof (ArgumentException))]
1512 [Category ("NotWorking")] // libgdiplus has lost track of the original 1bpp state
1513 public void Hicon48 ()
1515 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/48x48x1.ico"))) {
1516 // looks like 1bbp icons aren't welcome as bitmaps ;-)
1517 Bitmap.FromHicon (icon.Handle);
1522 public void Hicon64 ()
1526 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/64x64x256.ico"))) {
1528 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1529 HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1530 hicon = bitmap.GetHicon ();
1533 using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1534 // hicon survives bitmap and icon disposal
1535 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1540 public void Hicon96 ()
1544 using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/96x96x256.ico"))) {
1546 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1547 HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1548 hicon = bitmap.GetHicon ();
1551 using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1552 // hicon survives bitmap and icon disposal
1553 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1558 public void HBitmap ()
1561 string sInFile = TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp");
1562 using (Bitmap bitmap = new Bitmap (sInFile)) {
1563 Assert.AreEqual (PixelFormat.Format24bppRgb, bitmap.PixelFormat, "Original.PixelFormat");
1564 Assert.AreEqual (0, bitmap.Palette.Entries.Length, "Original.Palette");
1565 Assert.AreEqual (183, bitmap.Height, "Original.Height");
1566 Assert.AreEqual (173, bitmap.Width, "Original.Width");
1567 Assert.AreEqual (73744, bitmap.Flags, "Original.Flags");
1568 Assert.IsTrue (bitmap.RawFormat.Equals (ImageFormat.Bmp), "Original.RawFormat");
1569 hbitmap = bitmap.GetHbitmap ();
1571 // hbitmap survives original bitmap disposal
1572 using (Image image = Image.FromHbitmap (hbitmap)) {
1573 //Assert.AreEqual (PixelFormat.Format32bppRgb, image.PixelFormat, "FromHbitmap.PixelFormat");
1574 Assert.AreEqual (0, image.Palette.Entries.Length, "FromHbitmap.Palette");
1575 Assert.AreEqual (183, image.Height, "FromHbitmap.Height");
1576 Assert.AreEqual (173, image.Width, "FromHbitmap.Width");
1577 Assert.AreEqual (335888, image.Flags, "FromHbitmap.Flags");
1578 Assert.IsTrue (image.RawFormat.Equals (ImageFormat.MemoryBmp), "FromHbitmap.RawFormat");
1583 public void CreateMultipleBitmapFromSameHBITMAP ()
1586 string sInFile = TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp");
1587 using (Bitmap bitmap = new Bitmap (sInFile)) {
1588 Assert.AreEqual (PixelFormat.Format24bppRgb, bitmap.PixelFormat, "Original.PixelFormat");
1589 Assert.AreEqual (0, bitmap.Palette.Entries.Length, "Original.Palette");
1590 Assert.AreEqual (183, bitmap.Height, "Original.Height");
1591 Assert.AreEqual (173, bitmap.Width, "Original.Width");
1592 Assert.AreEqual (73744, bitmap.Flags, "Original.Flags");
1593 Assert.IsTrue (bitmap.RawFormat.Equals (ImageFormat.Bmp), "Original.RawFormat");
1594 hbitmap = bitmap.GetHbitmap ();
1596 // hbitmap survives original bitmap disposal
1597 using (Image image = Image.FromHbitmap (hbitmap)) {
1598 //Assert.AreEqual (PixelFormat.Format32bppRgb, image.PixelFormat, "1.PixelFormat");
1599 Assert.AreEqual (0, image.Palette.Entries.Length, "1.Palette");
1600 Assert.AreEqual (183, image.Height, "1.Height");
1601 Assert.AreEqual (173, image.Width, "1.Width");
1602 Assert.AreEqual (335888, image.Flags, "1.Flags");
1603 Assert.IsTrue (image.RawFormat.Equals (ImageFormat.MemoryBmp), "1.RawFormat");
1605 using (Image image2 = Image.FromHbitmap (hbitmap)) {
1606 //Assert.AreEqual (PixelFormat.Format32bppRgb, image2.PixelFormat, "2.PixelFormat");
1607 Assert.AreEqual (0, image2.Palette.Entries.Length, "2.Palette");
1608 Assert.AreEqual (183, image2.Height, "2.Height");
1609 Assert.AreEqual (173, image2.Width, "2.Width");
1610 Assert.AreEqual (335888, image2.Flags, "2.Flags");
1611 Assert.IsTrue (image2.RawFormat.Equals (ImageFormat.MemoryBmp), "2.RawFormat");