This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33 using System;
34 using System.Drawing;
35 using System.Drawing.Imaging;
36 using NUnit.Framework;
37 using System.IO;
38 using System.Security.Cryptography;
39 using System.Text;
40 using System.Runtime.InteropServices;
41
42 namespace MonoTests.System.Drawing{
43
44         [TestFixture]   
45         public class TestBitmap {
46                 
47                 [TearDown]
48                 public void Clean() {}
49                 
50                 [SetUp]
51                 public void GetReady()          
52                 {
53                 
54                 }
55                         
56                 [Test]
57                 public void TestPixels() 
58                 {               
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);                                
63                                                 
64                         Assert.AreEqual (Color.FromArgb(255,128,128,128), color);
65                         
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);                        
69                 }
70                 
71                 /* Get the output directory depending on the runtime and location*/
72                 public static string getOutSubDir()
73                 {                               
74                         string sSub, sRslt;                     
75                         
76                         if (Environment.GetEnvironmentVariable("MSNet")==null)
77                                 sSub = "mono/";
78                         else
79                                 sSub = "MSNet/";                        
80                         
81                         sRslt = Path.GetFullPath (sSub);
82                                 
83                         if (Directory.Exists(sRslt) ==  false) 
84                                 sRslt = "Test/System.Drawing/" + sSub;                                                  
85                         
86                         if (sRslt.Length > 0)
87                                 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
88                                         sRslt += "/";                                   
89                         
90                         return sRslt;
91                 }
92                 
93                 /* Get the input directory depending on the runtime*/
94                 public static string getInFile(string file)
95                 {                               
96                         string sRslt;                                           
97                         
98                         sRslt = Path.GetFullPath (file);
99                                 
100                         if (File.Exists(file)==false) 
101                                 sRslt = "Test/System.Drawing/" + file;                                                  
102                         
103                         return sRslt;
104                 }
105                 
106                 //[Test]
107                 public void MakeTransparent() 
108                 {
109                         string sInFile =   getInFile("bitmaps/maketransparent.bmp");
110                         string sOutFile =  getOutSubDir() + "transparent.bmp";
111                                                 
112                         Bitmap  bmp = new Bitmap(sInFile);
113                         Console.WriteLine("Bitmap loaded OK", bmp != null);
114                                         
115                         bmp.MakeTransparent();
116                         bmp.Save(sOutFile);                                                     
117                         
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);                                                                               
122                 }
123                 
124                 [Test]
125                 public void Clone()
126                 {
127                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
128                         string sOutFile =  getOutSubDir() + "clone24.bmp";                      
129                         
130                         Rectangle rect = new Rectangle(0,0,50,50);                                              
131                         Bitmap  bmp = new Bitmap(sInFile);                      
132                         
133                         Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);                                                                  
134                         
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);                              
139                         
140                         Assert.AreEqual (colororg0, colornew0);                                                                                 
141                         Assert.AreEqual (colororg50, colornew50);                               
142                 }       
143                 
144                 [Test]
145                 public void CloneImage()
146                 {
147                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");                     
148                         Bitmap  bmp = new Bitmap(sInFile);                      
149                         
150                         Bitmap bmpNew = (Bitmap) bmp.Clone ();                  
151                         
152                         Assert.AreEqual (bmp.Width, bmpNew.Width);
153                         Assert.AreEqual (bmp.Height, bmpNew.Height);            
154                         Assert.AreEqual (bmp.PixelFormat, bmpNew.PixelFormat);                  
155                         
156                 }       
157
158                 //[Test]
159                 public void Frames()
160                 {
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);
165                         
166                         Assert.AreEqual (1, cnt);                                                               
167                         Assert.AreEqual (0, active);                                                                                    
168                 }
169
170                 static string ByteArrayToString(byte[] arrInput)
171                 {
172                         int i;
173                         StringBuilder sOutput = new StringBuilder(arrInput.Length);
174                         for (i=0;i < arrInput.Length -1; i++) 
175                         {
176                                 sOutput.Append(arrInput[i].ToString("X2"));
177                         }
178                         return sOutput.ToString();
179                 }
180
181
182                 public string RotateBmp (Bitmap src, RotateFlipType rotate)
183                 {                       
184                         int witdh = 150, height = 150, index = 0;                       
185                         byte[] pixels = new byte [witdh * height * 3];
186                         Bitmap bmp_rotate;
187                         byte[] hash;
188                         Color clr;
189
190
191                         bmp_rotate = src.Clone (new RectangleF (0,0, witdh, height), PixelFormat.Format32bppArgb);      
192                         bmp_rotate.RotateFlip (rotate);                 
193
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;     
198                                 }                               
199                         }
200                 
201                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
202                         return ByteArrayToString (hash);
203                 }
204                 
205                 
206                 /*
207                         Rotate bitmap in diffent ways, and check the result
208                         pixels using MD5
209                 */
210                 [Test]
211                 public void Rotate()
212                 {
213                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");     
214                         Bitmap  bmp = new Bitmap(sInFile);              
215                         
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));
223
224                 }
225                 
226                 public void LockBmp (PixelFormat fmt, PixelFormat fmtlock, string output, 
227                         int lwidth , int lheight, ref string hash1, ref string hash2)
228                 {                       
229                         int width = 100, height = 100, bbps, cur, pos;
230                         Bitmap  bmp = new Bitmap (width, height, fmt);                                                                          
231                         Graphics gr = Graphics.FromImage (bmp);                 
232                         byte[] hash;
233                         Color clr;
234                         byte[] btv = new byte[1];                                               
235                         int y, x, len = width * height * 4, index = 0;                  
236                         byte[] pixels = new byte [len];
237                         hash1 = hash2 ="";
238                         
239                         bbps = Image.GetPixelFormatSize (fmt);                  
240                                  
241                         Pen p = new Pen (Color.FromArgb (255, 100, 200, 250), 2);                               
242                         gr.DrawRectangle(p, 1.0F, 1.0F, 80.0F, 80.0F);                          
243                         
244                         BitmapData bd = bmp.LockBits (new Rectangle (0, 0, lwidth, lheight), ImageLockMode.ReadOnly,  fmtlock);
245                         
246                         pos = bd.Scan0.ToInt32();                       
247                         for (y = 0; y < bd.Height; y++) {                       
248                                 for (x = 0; x < bd.Width; x++) {
249                                         
250                                         /* Read the pixels*/
251                                         for (int bt =0; bt < bbps/8; bt++, index++) {
252                                                 cur = pos;
253                                                 cur+= y * bd.Stride;
254                                                 cur+= x * bbps/8;                                       
255                                                 cur+= bt;                                       
256                                                 Marshal.Copy ((IntPtr)cur, btv, 0, 1);
257                                                 pixels[index] = btv[0];
258                                                 
259                                                 /* Make change of all the colours = 250 to 10*/                                         
260                                                 if (btv[0] == 250) {
261                                                         btv[0] = 10;
262                                                         Marshal.Copy (btv, 0, (IntPtr)cur, 1);
263                                                 }
264                                         }
265                                 }
266                         }                                       
267                         
268                         for (int i = index; i < len; i++)
269                                 pixels[index] = 0;
270                 
271                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);                     
272                         bmp.UnlockBits (bd);                                                    
273                                                 
274                         hash1 = ByteArrayToString (hash);
275                         
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;     
281                                 }                               
282                         }
283                         
284                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);                                             
285                         hash2 = ByteArrayToString (hash);
286                         
287                         /*bmp.Save (output, ImageFormat.Bmp);*/
288                 }
289                 /*
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
293                 */
294                 [Test]
295                 public void LockBitmap ()
296                 {       
297                         string hash = "";               
298                         string hashchg = "";                            
299                                                         
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);                    
304                         
305                         LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 100, 100, ref hash, ref hashchg);                  
306                         Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);                       
307                         Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);                    
308                         
309                         LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 100, 100, ref hash, ref hashchg);
310                         Assert.AreEqual ("AF5BFD4E98D6708FF4C9982CC9C68F", hash);                       
311                         Assert.AreEqual ("BBEE27DC85563CB58EE11E8951230F", hashchg);            
312                         
313                         LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 100, 100, ref hash, ref hashchg);
314                         Assert.AreEqual ("A8A071D0B3A3743905B4E193A62769", hash);                       
315                         Assert.AreEqual ("EEE846FA8F892339C64082DFF775CF", hashchg);                                    
316                         
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);                    
321                         
322                         LockBmp (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, "output32bppPArgb.bmp", 50, 50, ref hash, ref hashchg);                    
323                         Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);                       
324                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                    
325                 
326                         LockBmp (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, "output32bppRgb.bmp", 50, 50, ref hash, ref hashchg);
327                         Assert.AreEqual ("C361FBFD82A4F3C278605AE9EC5385", hash);                       
328                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                    
329                         
330                         LockBmp (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, "output24bppRgb.bmp", 50, 50, ref hash, ref hashchg);
331                         Assert.AreEqual ("FFE86628478591D1A1EB30E894C34F", hash);                       
332                         Assert.AreEqual ("8C2C04B361E1D5875EE8ACF5073F4E", hashchg);                            
333                                                 
334                 }
335                 
336         }
337 }