ChangeLog: Updated ChangeLog.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestBitmap.cs
1 //
2 // Bitmap class testing unit
3 //
4 // Author:
5 //
6 //       Jordi Mas i Hernàndez (jmas@softcatala.org>
7 //
8 // (C) 2004 Ximian, Inc.  http://www.ximian.com
9 //
10 using System;
11 using System.Drawing;
12 using System.Drawing.Imaging;
13 using NUnit.Framework;
14 using System.IO;
15 using System.Security.Cryptography;
16 using System.Text;
17 using System.Runtime.InteropServices;
18
19 namespace MonoTests.System.Drawing{
20
21         [TestFixture]   
22         public class TestBitmap {
23                 
24                 [TearDown]
25                 public void Clean() {}
26                 
27                 [SetUp]
28                 public void GetReady()          
29                 {
30                 
31                 }
32                         
33                 [Test]
34                 public void TestPixels() 
35                 {               
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);                                
40                                                 
41                         Assert.AreEqual (Color.FromArgb(255,128,128,128), color);
42                         
43                         bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));                                      
44                         Color color2 = bmp.GetPixel(99,99);                                                                             
45                         Assert.AreEqual (Color.FromArgb(255,255,0,155), color2);                        
46                 }
47                 
48                 /* Get the output directory depending on the runtime and location*/
49                 public static string getOutSubDir()
50                 {                               
51                         string sSub, sRslt;                     
52                         
53                         if (Environment.GetEnvironmentVariable("MSNet")==null)
54                                 sSub = "mono/";
55                         else
56                                 sSub = "MSNet/";                        
57                         
58                         sRslt = Path.GetFullPath (sSub);
59                                 
60                         if (Directory.Exists(sRslt) ==  false) 
61                                 sRslt = "Test/System.Drawing/" + sSub;                                                  
62                         
63                         if (sRslt.Length > 0)
64                                 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
65                                         sRslt += "/";                                   
66                         
67                         return sRslt;
68                 }
69                 
70                 /* Get the input directory depending on the runtime*/
71                 public static string getInFile(string file)
72                 {                               
73                         string sRslt;                                           
74                         
75                         sRslt = Path.GetFullPath (file);
76                                 
77                         if (File.Exists(file)==false) 
78                                 sRslt = "Test/System.Drawing/" + file;                                                  
79                         
80                         return sRslt;
81                 }
82                 
83                 //[Test]
84                 public void MakeTransparent() 
85                 {
86                         string sInFile =   getInFile("bitmaps/maketransparent.bmp");
87                         string sOutFile =  getOutSubDir() + "transparent.bmp";
88                                                 
89                         Bitmap  bmp = new Bitmap(sInFile);
90                         Console.WriteLine("Bitmap loaded OK", bmp != null);
91                                         
92                         bmp.MakeTransparent();
93                         bmp.Save(sOutFile);                                                     
94                         
95                         Color color = bmp.GetPixel(1,1);                                                        
96                         Assert.AreEqual (Color.Black.R, color.R);                                                                                       
97                         Assert.AreEqual (Color.Black.G, color.G);                                                                                       
98                         Assert.AreEqual (Color.Black.B, color.B);                                                                               
99                 }
100                 
101                 [Test]
102                 public void Clone()
103                 {
104                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
105                         string sOutFile =  getOutSubDir() + "clone24.bmp";                      
106                         
107                         Rectangle rect = new Rectangle(0,0,50,50);                                              
108                         Bitmap  bmp = new Bitmap(sInFile);                      
109                         
110                         Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);                                                                  
111                         
112                         Color colororg0 = bmp.GetPixel(0,0);            
113                         Color colororg50 = bmp.GetPixel(49,49);                                 
114                         Color colornew0 = bmpNew.GetPixel(0,0);         
115                         Color colornew50 = bmpNew.GetPixel(49,49);                              
116                         
117                         Assert.AreEqual (colororg0, colornew0);                                                                                 
118                         Assert.AreEqual (colororg50, colornew50);                               
119                 }       
120                 
121                 [Test]
122                 public void CloneImage()
123                 {
124                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");                     
125                         Bitmap  bmp = new Bitmap(sInFile);                      
126                         
127                         Bitmap bmpNew = (Bitmap) bmp.Clone ();                  
128                         
129                         Assert.AreEqual (bmp.Width, bmpNew.Width);
130                         Assert.AreEqual (bmp.Height, bmpNew.Height);            
131                         Assert.AreEqual (bmp.PixelFormat, bmpNew.PixelFormat);                  
132                         
133                 }       
134
135                 //[Test]
136                 public void Frames()
137                 {
138                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");                     
139                         Bitmap  bmp = new Bitmap(sInFile);                                              
140                         int cnt = bmp.GetFrameCount(FrameDimension.Page);                       
141                         int active = bmp.SelectActiveFrame (FrameDimension.Page, 0);
142                         
143                         Assert.AreEqual (1, cnt);                                                               
144                         Assert.AreEqual (0, active);                                                                                    
145                 }
146
147                 static string ByteArrayToString(byte[] arrInput)
148                 {
149                         int i;
150                         StringBuilder sOutput = new StringBuilder(arrInput.Length);
151                         for (i=0;i < arrInput.Length -1; i++) 
152                         {
153                                 sOutput.Append(arrInput[i].ToString("X2"));
154                         }
155                         return sOutput.ToString();
156                 }
157
158
159                 public string RotateBmp (Bitmap src, RotateFlipType rotate)
160                 {                       
161                         int witdh = 150, height = 150, index = 0;                       
162                         byte[] pixels = new byte [witdh * height * 3];
163                         Bitmap bmp_rotate;
164                         byte[] hash;
165                         Color clr;
166
167
168                         bmp_rotate = src.Clone (new RectangleF (0,0, witdh, height), PixelFormat.Format32bppArgb);      
169                         bmp_rotate.RotateFlip (rotate);                 
170
171                         for (int y = 0; y < height; y++) {
172                                 for (int x = 0; x < witdh; x++) {                               
173                                         clr = bmp_rotate.GetPixel (x,y);
174                                         pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++]  = clr.B;     
175                                 }                               
176                         }
177                 
178                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
179                         return ByteArrayToString (hash);
180                 }
181                 
182                 
183                 /*
184                         Rotate bitmap in diffent ways, and check the result
185                         pixels using MD5
186                 */
187                 [Test]
188                 public void Rotate()
189                 {
190                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");     
191                         Bitmap  bmp = new Bitmap(sInFile);              
192                         
193                         Assert.AreEqual ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));   
194                         Assert.AreEqual ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
195                         Assert.AreEqual ("15AD2ADBDC7090C0EC744D0F7ACE2F", RotateBmp (bmp, RotateFlipType.Rotate270FlipNone));
196                         Assert.AreEqual ("2E10FEC1F4FD64ECC51D7CE68AEB18", RotateBmp (bmp, RotateFlipType.RotateNoneFlipX));
197                         Assert.AreEqual ("E63204779B566ED01162B90B49BD9E", RotateBmp (bmp, RotateFlipType.Rotate90FlipX));
198                         Assert.AreEqual ("B1ECB17B5093E13D04FF55CFCF7763", RotateBmp (bmp, RotateFlipType.Rotate180FlipX));
199                         Assert.AreEqual ("71A173882C16755D86F4BC26532374", RotateBmp (bmp, RotateFlipType.Rotate270FlipX));
200
201                 }
202                 
203                 public void LockBmp (PixelFormat fmt, PixelFormat fmtlock, string output, 
204                         int lwidth , int lheight, ref string hash1, ref string hash2)
205                 {                       
206                         int width = 100, height = 100, bbps, cur, pos;
207                         Bitmap  bmp = new Bitmap (width, height, fmt);                                                                          
208                         Graphics gr = Graphics.FromImage (bmp);                 
209                         byte[] hash;
210                         Color clr;
211                         byte[] btv = new byte[1];                                               
212                         int y, x, len = width * height * 4, index = 0;                  
213                         byte[] pixels = new byte [len];
214                         hash1 = hash2 ="";
215                         
216                         bbps = Image.GetPixelFormatSize (fmt);                  
217                                  
218                         Pen p = new Pen (Color.FromArgb (255, 100, 200, 250), 2);                               
219                         gr.DrawRectangle(p, 1.0F, 1.0F, 80.0F, 80.0F);                          
220                         
221                         BitmapData bd = bmp.LockBits (new Rectangle (0, 0, lwidth, lheight), ImageLockMode.ReadOnly,  fmtlock);
222                         
223                         pos = bd.Scan0.ToInt32();                       
224                         for (y = 0; y < bd.Height; y++) {                       
225                                 for (x = 0; x < bd.Width; x++) {
226                                         
227                                         /* Read the pixels*/
228                                         for (int bt =0; bt < bbps/8; bt++, index++) {
229                                                 cur = pos;
230                                                 cur+= y * bd.Stride;
231                                                 cur+= x * bbps/8;                                       
232                                                 cur+= bt;                                       
233                                                 Marshal.Copy ((IntPtr)cur, btv, 0, 1);
234                                                 pixels[index] = btv[0];
235                                                 
236                                                 /* Make change of all the colours = 250 to 10*/                                         
237                                                 if (btv[0] == 250) {
238                                                         btv[0] = 10;
239                                                         Marshal.Copy (btv, 0, (IntPtr)cur, 1);
240                                                 }
241                                         }
242                                 }
243                         }                                       
244                         
245                         for (int i = index; i < len; i++)
246                                 pixels[index] = 0;
247                 
248                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);                     
249                         bmp.UnlockBits (bd);                                                    
250                                                 
251                         hash1 = ByteArrayToString (hash);
252                         
253                         /* MD5 of the changed bitmap*/
254                         for (y = 0, index = 0; y < height; y++) {
255                                 for (x = 0; x < width; x++) {                           
256                                         clr = bmp.GetPixel (x,y);
257                                         pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++]  = clr.B;     
258                                 }                               
259                         }
260                         
261                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);                                             
262                         hash2 = ByteArrayToString (hash);
263                         
264                         /*bmp.Save (output, ImageFormat.Bmp);*/
265                 }
266                 /*
267                         Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns
268                         firsts, changes them, and then using GetPixel does another check of the changes.
269                         The results match the .Net framework
270                 */
271                 [Test]
272                 public void LockBitmap ()
273                 {       
274                         string hash = "";               
275                         string hashchg = "";                            
276                                                         
277                         /* Locks the whole bitmap*/                     
278                         LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 100, 100, ref hash, ref hashchg);                             
279                         Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);                       
280                         Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);                    
281                         
282                         LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 100, 100, ref hash, ref hashchg);                  
283                         Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);                       
284                         Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);                    
285                         
286                         LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 100, 100, ref hash, ref hashchg);
287                         Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);                       
288                         Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);            
289                         
290                         LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 100, 100, ref hash, ref hashchg);
291                         Assert.AreEqual ("A8A071D0B3A3743905B4E193A62769", hash);                       
292                         Assert.AreEqual ("EEE846FA8F892339C64082DFF775CF", hashchg);                                    
293                         
294                         /* Locks a portion of the bitmap*/              
295                         LockBmp (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, "output32bppArgb.bmp", 50, 50, ref hash, ref hashchg);                               
296                         Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);                       
297                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                    
298                         
299                         LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 50, 50, ref hash, ref hashchg);                    
300                         Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);                       
301                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                    
302                 
303                         LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 50, 50, ref hash, ref hashchg);
304                         Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);                       
305                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                    
306                         
307                         LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 50, 50, ref hash, ref hashchg);
308                         Assert.AreEqual ("FFE86628478591D1A1EB30E894C34F", hash);                       
309                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                            
310                                                 
311                 }
312                 
313         }
314 }