* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestBitmap.cs
index 92d0434f1552094e2b6c662801de639d1cd132a7..121dfec02d8939e3e61e7842555745f9f74abc20 100644 (file)
@@ -7,10 +7,7 @@
 //      Jonathan Gilbert <logic@deltaq.org>
 //
 // (C) 2004 Ximian, Inc.  http://www.ximian.com
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -47,20 +44,11 @@ namespace MonoTests.System.Drawing{
        [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
        public class TestBitmap {
                
-               [TearDown]
-               public void Clean() {}
-               
-               [SetUp]
-               public void GetReady()          
-               {
-               
-               }
-                       
                [Test]
                public void TestPixels() 
                {               
                        // Tests GetSetPixel/SetPixel                   
-                       Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);                                                                                    
+                       Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
                        bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));                                      
                        Color color = bmp.GetPixel(0,0);                                
                                                
@@ -70,7 +58,26 @@ namespace MonoTests.System.Drawing{
                        Color color2 = bmp.GetPixel(99,99);                                                                             
                        Assert.AreEqual (Color.FromArgb(255,255,0,155), color2);                        
                }
-               
+
+               [Test]
+               public void LockBits_NonIndexedWrite ()
+               {
+                       Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
+                       Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
+                        BitmapData mybitmapdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
+                       bmp.UnlockBits (mybitmapdata);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void LockBits_IndexedWrite ()
+               {
+                       Bitmap bmp= new Bitmap(100,100, PixelFormat.Format8bppIndexed);
+                       Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
+                        BitmapData mybitmapdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
+                       bmp.UnlockBits (mybitmapdata);
+               }
+
                /* Get the output directory depending on the runtime and location*/
                public static string getOutSubDir()
                {                               
@@ -105,7 +112,8 @@ namespace MonoTests.System.Drawing{
                        
                        return sRslt;
                }
-               
+
+               // note: this test fails when saving (for the same reason) on Mono and MS.NET
                //[Test]
                public void MakeTransparent() 
                {
@@ -127,13 +135,10 @@ namespace MonoTests.System.Drawing{
                public void Clone()
                {
                        string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
-                       string sOutFile =  getOutSubDir() + "clone24.bmp";                      
-                       
                        Rectangle rect = new Rectangle(0,0,50,50);                                              
                        Bitmap  bmp = new Bitmap(sInFile);                      
                        
                        Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);                                                                  
-                       
                        Color colororg0 = bmp.GetPixel(0,0);            
                        Color colororg50 = bmp.GetPixel(49,49);                                 
                        Color colornew0 = bmpNew.GetPixel(0,0);         
@@ -193,18 +198,17 @@ namespace MonoTests.System.Drawing{
 
                public string RotateBmp (Bitmap src, RotateFlipType rotate)
                {                       
-                       int witdh = 150, height = 150, index = 0;                       
-                       byte[] pixels = new byte [witdh * height * 3];
+                       int width = 150, height = 150, index = 0;                       
+                       byte[] pixels = new byte [width * height * 3];
                        Bitmap bmp_rotate;
                        byte[] hash;
                        Color clr;
 
-
-                       bmp_rotate = src.Clone (new RectangleF (0,0, witdh, height), PixelFormat.Format32bppArgb);      
+                       bmp_rotate = src.Clone (new RectangleF (0,0, width, height), PixelFormat.Format32bppArgb);      
                        bmp_rotate.RotateFlip (rotate);                 
 
                        for (int y = 0; y < height; y++) {
-                               for (int x = 0; x < witdh; x++) {                               
+                               for (int x = 0; x < width; x++) {
                                        clr = bmp_rotate.GetPixel (x,y);
                                        pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++]  = clr.B;     
                                }                               
@@ -275,7 +279,7 @@ namespace MonoTests.System.Drawing{
                public void Rotate()
                {
                        string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");     
-                       Bitmap  bmp = new Bitmap(sInFile);              
+                       Bitmap  bmp = new Bitmap(sInFile);
                        
                        Assert.AreEqual ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));   
                        Assert.AreEqual ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
@@ -293,6 +297,7 @@ namespace MonoTests.System.Drawing{
                        resulting pixels using MD5
                */
                [Test]
+               [Category ("NotDotNet")] // that doesn't work under MS.NET and Windows (even without the StreamWriter)
                public void Rotate1bit4bit()
                {
                        string[] files = {
@@ -414,7 +419,8 @@ namespace MonoTests.System.Drawing{
                        firsts, changes them, and then using GetPixel does another check of the changes.
                        The results match the .Net framework
                */
-               //[Test]
+               [Test]
+               [Category ("NotWorking")]
                public void LockBitmap ()
                {       
                        string hash = "";               
@@ -553,5 +559,40 @@ namespace MonoTests.System.Drawing{
                        }
                }
 #endif         
+               [Test]
+               public void DefaultFormat1 ()
+               {
+                       using (Bitmap bmp = new Bitmap (20, 20)) {
+                               Assert.AreEqual (ImageFormat.MemoryBmp, bmp.RawFormat);
+                       }
+               }
+
+               [Test]
+               public void DefaultFormat2 ()
+               {
+                       string filename =  Path.GetTempFileName ();
+                       using (Bitmap bmp = new Bitmap (20, 20)) {
+                               bmp.Save (filename);
+                       }
+
+                       using (Bitmap other = new Bitmap (filename)) {
+                               Assert.AreEqual (ImageFormat.Png, other.RawFormat);
+                       }
+                       File.Delete (filename);
+               }
+
+               [Test]
+               public void BmpDataStride1 ()
+               {
+                       Bitmap bmp = new Bitmap (184, 184, PixelFormat.Format1bppIndexed);
+                       BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
+                       try {
+                               Assert.AreEqual (24, data.Stride);
+                       } finally {
+                               bmp.UnlockBits (data);
+                               bmp.Dispose ();
+                       }
+               }
        }
 }
+