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