Added license and copyright
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TestBmpCodec.cs
1 //
2 // BMPCodec class testing unit
3 //
4 // Author:
5 //
6 //       Jordi Mas i Hernàndez (jordi@ximian.com)
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
41 namespace MonoTests.System.Drawing
42 {
43
44         [TestFixture]   
45         public class TestBmpCodec
46         {
47                 
48                 [TearDown]
49                 public void Clean() {}
50                 
51                 [SetUp]
52                 public void GetReady()          
53                 {
54                 
55                 }
56                 
57                 /* Get the output directory depending on the runtime and location*/
58                 internal string getOutSubDir()
59                 {                               
60                         string sSub, sRslt;                     
61                         
62                         if (Environment.GetEnvironmentVariable("MSNet")==null)
63                                 sSub = "mono/";
64                         else
65                                 sSub = "MSNet/";                        
66                         
67                         sRslt = Path.GetFullPath ("../System.Drawing/" +  sSub);
68                                 
69                         if (Directory.Exists(sRslt) ==  false) 
70                                 sRslt = "Test/System.Drawing/" + sSub;                                                  
71                         
72                         if (sRslt.Length > 0)
73                                 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
74                                         sRslt += "/";                                   
75                         return sRslt;
76                 }
77
78                 /* Get the input directory depending on the runtime*/
79                 internal string getInFile(string file)
80                 {                               
81                         string sRslt, local;                                            
82
83                         local = "../System.Drawing/" + file;
84                         
85                         sRslt = Path.GetFullPath (local);
86                                 
87                         if (File.Exists(sRslt)==false) 
88                                 sRslt = "Test/System.Drawing/" + file;                                                  
89                         
90                         return sRslt;
91                 }
92                 
93                 /* Checks bitmap features on a know 24-bits bitmap */
94                 [Test]
95                 public void Bitmap24bitFeatures()
96                 {
97                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
98                         Bitmap  bmp = new Bitmap(sInFile);                                              
99                         RectangleF rect;
100                         GraphicsUnit unit = GraphicsUnit.World;
101                         
102                         rect = bmp.GetBounds(ref unit);
103                         
104                         Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat);
105                         Assert.AreEqual (173, bmp.Width);
106                         Assert.AreEqual (183, bmp.Height);              
107                         
108                         Assert.AreEqual (0, rect.X);
109                         Assert.AreEqual (0, rect.Y);            
110                         Assert.AreEqual (173, rect.Width);
111                         Assert.AreEqual (183, rect.Height);                                     
112                         
113                         Assert.AreEqual (173, bmp.Size.Width);
114                         Assert.AreEqual (183, bmp.Size.Height);                                 
115                 }
116
117                 
118
119                 /* Checks bitmap features on a know 32-bits bitmap (codec)*/
120                 [Test]
121                 public void Bitmap32bitFeatures()
122                 {
123                         string sInFile = getInFile ("bitmaps/almogaver32bits.bmp");
124                         Bitmap  bmp = new Bitmap(sInFile);                                              
125                         RectangleF rect;
126                         GraphicsUnit unit = GraphicsUnit.World;
127                         
128                         rect = bmp.GetBounds(ref unit);
129                         
130                         //Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
131                         Assert.AreEqual (173, bmp.Width);
132                         Assert.AreEqual (183, bmp.Height);              
133                         
134                         Assert.AreEqual (0, rect.X);
135                         Assert.AreEqual (0, rect.Y);            
136                         Assert.AreEqual (173, rect.Width);
137                         Assert.AreEqual (183, rect.Height);                                     
138                         
139                         Assert.AreEqual (173, bmp.Size.Width);
140                         Assert.AreEqual (183, bmp.Size.Height);                                 
141                 }
142
143                 [Test]
144                 public void Save() 
145                 {                               
146                         string sOutFile =  getOutSubDir() + "linerect.bmp";
147                                                 
148                         // Save         
149                         Bitmap  bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);                                          
150                         Graphics gr = Graphics.FromImage(bmp);          
151                         
152                         Pen p = new Pen(Color.Red, 2);
153                         gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
154                         gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
155                         p.Dispose();                                    
156                         bmp.Save(sOutFile, ImageFormat.Bmp);
157                         gr.Dispose();
158                         bmp.Dispose();                                                  
159                         
160                         // Load                 
161                         Bitmap  bmpLoad = new Bitmap(sOutFile);                                 
162                         
163                         Color color = bmpLoad.GetPixel(10,10);                                  
164                         
165                         Assert.AreEqual (Color.FromArgb(255,255,0,0), color);                                                                                   
166                 }
167
168                 
169         }
170 }