Copied remotely
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TestJpegCodec.cs
1 //
2 // JpegCodec 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
39 namespace MonoTests.System.Drawing
40 {
41
42         [TestFixture]   
43         public class TestJpegCodec 
44         {
45                 
46                 [TearDown]
47                 public void Clean() {}
48                 
49                 [SetUp]
50                 public void GetReady()          
51                 {
52                 
53                 }
54                 
55                 /* Get the output directory depending on the runtime and location*/
56                 internal string getOutSubDir()
57                 {                               
58                         string sSub, sRslt;                     
59                         
60                         if (Environment.GetEnvironmentVariable("MSNet")==null)
61                                 sSub = "mono/";
62                         else
63                                 sSub = "MSNet/";                        
64                         
65                         sRslt = Path.GetFullPath ("../System.Drawing/" +  sSub);
66                                 
67                         if (Directory.Exists(sRslt) ==  false) 
68                                 sRslt = "Test/System.Drawing/" + sSub;                                                  
69                         
70                         if (sRslt.Length > 0)
71                                 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
72                                         sRslt += "/";                                   
73                         return sRslt;
74                 }
75
76                 /* Get the input directory depending on the runtime*/
77                 internal string getInFile(string file)
78                 {                               
79                         string sRslt, local;                                            
80
81                         local = "../System.Drawing/" + file;
82                         
83                         sRslt = Path.GetFullPath (local);
84                                 
85                         if (File.Exists(sRslt)==false) 
86                                 sRslt = "Test/System.Drawing/" + file;                                                  
87                         
88                         return sRslt;
89                 }
90                 
91                 /* Checks bitmap features on a know 24-bits bitmap */
92                 [Test]
93                 public void Bitmap24bitFeatures()
94                 {
95                         string sInFile = getInFile ("bitmaps/nature24bits.jpg");
96                         Bitmap  bmp = new Bitmap(sInFile);                                              
97                         RectangleF rect;
98                         GraphicsUnit unit = GraphicsUnit.World;
99                         
100                         rect = bmp.GetBounds(ref unit);
101
102                         Assert.AreEqual (PixelFormat.Format24bppRgb, bmp.PixelFormat);
103                         Assert.AreEqual (110, bmp.Width);
104                         Assert.AreEqual (100, bmp.Height);              
105                         
106                         Assert.AreEqual (0, rect.X);
107                         Assert.AreEqual (0, rect.Y);            
108                         Assert.AreEqual (110, rect.Width);
109                         Assert.AreEqual (100, rect.Height);                                     
110                         
111                         Assert.AreEqual (110, bmp.Size.Width);
112                         Assert.AreEqual (100, bmp.Size.Height);
113                         
114                 }
115
116                 [Test]
117                 public void Save() 
118                 {                               
119                         string sOutFile =  getOutSubDir() + "linerect.jpeg";
120                                                 
121                         // Save         
122                         Bitmap  bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);                                          
123                         Graphics gr = Graphics.FromImage(bmp);          
124                         
125                         Pen p = new Pen(Color.Red, 2);
126                         gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
127                         gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
128                         p.Dispose();                                    
129                         bmp.Save(sOutFile, ImageFormat.Bmp);
130                         gr.Dispose();
131                         bmp.Dispose();                                                  
132                         
133                         // Load                 
134                         Bitmap  bmpLoad = new Bitmap(sOutFile);                                 
135                         
136                         Color color = bmpLoad.GetPixel(10,10);                                  
137                         
138                         Assert.AreEqual (Color.FromArgb(255,255,0,0), color);                                                                                   
139                 }
140
141                 
142         }
143 }