2f196e0d35cecb1980b9565ee931bfaddc21891b
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / GifCodecTest.cs
1 //
2 // PNG Codec class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernàndez (jordi@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Drawing;
32 using System.Drawing.Imaging;
33 using System.IO;
34 using System.Security.Permissions;
35 using System.Text;
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Drawing.Imaging {
39
40         [TestFixture]
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class GifCodecTest {
43
44                 /* Get suffix to add to the filename */
45                 internal string getOutSufix ()
46                 {
47                         string s;
48
49                         int p = (int) Environment.OSVersion.Platform;
50                         if ((p == 4) || (p == 128))
51                                 s = "-unix";
52                         else
53                                 s = "-windows";
54
55                         if (Type.GetType ("Mono.Runtime", false) == null)
56                                 s += "-msnet";
57                         else
58                                 s += "-mono";
59
60                         return s;
61                 }
62
63                 /* Get the input directory depending on the runtime*/
64                 internal string getInFile (string file)
65                 {
66                         string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
67
68                         if (!File.Exists (sRslt))
69                                 sRslt = "Test/System.Drawing/" + file;
70
71                         return sRslt;
72                 }
73
74                 /* Checks bitmap features on a know 1bbp bitmap */
75                 [Test]
76                 public void Bitmap8bitsFeatures ()
77                 {
78                         string sInFile = getInFile ("bitmaps/nature24bits.gif");
79                         using (Bitmap bmp = new Bitmap (sInFile)) {
80                                 GraphicsUnit unit = GraphicsUnit.World;
81                                 RectangleF rect = bmp.GetBounds (ref unit);
82
83                                 Assert.AreEqual (PixelFormat.Format8bppIndexed, bmp.PixelFormat);
84                                 Assert.AreEqual (110, bmp.Width, "bmp.Width");
85                                 Assert.AreEqual (100, bmp.Height, "bmp.Height");
86
87                                 Assert.AreEqual (0, rect.X, "rect.X");
88                                 Assert.AreEqual (0, rect.Y, "rect.Y");
89                                 Assert.AreEqual (110, rect.Width, "rect.Width");
90                                 Assert.AreEqual (100, rect.Height, "rect.Height");
91
92                                 Assert.AreEqual (110, bmp.Size.Width, "bmp.Size.Width");
93                                 Assert.AreEqual (100, bmp.Size.Height, "bmp.Size.Height");
94                         }
95                 }
96
97                 [Test]
98                 public void Bitmap8bitsPixels ()
99                 {
100                         string sInFile = getInFile ("bitmaps/nature24bits.gif");
101                         using (Bitmap bmp = new Bitmap (sInFile)) {
102 #if false
103                                 for (int x = 0; x < bmp.Width; x += 32) {
104                                         for (int y = 0; y < bmp.Height; y += 32)
105                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
106                                 }
107 #else
108                                 // sampling values from a well known bitmap
109                                 Assert.AreEqual (-10644802, bmp.GetPixel (0, 0).ToArgb (), "0,0");
110                                 Assert.AreEqual (-12630705, bmp.GetPixel (0, 32).ToArgb (), "0,32");
111                                 Assert.AreEqual (-14537409, bmp.GetPixel (0, 64).ToArgb (), "0,64");
112                                 Assert.AreEqual (-14672099, bmp.GetPixel (0, 96).ToArgb (), "0,96");
113                                 Assert.AreEqual (-526863, bmp.GetPixel (32, 0).ToArgb (), "32,0");
114                                 Assert.AreEqual (-10263970, bmp.GetPixel (32, 32).ToArgb (), "32,32");
115                                 Assert.AreEqual (-10461317, bmp.GetPixel (32, 64).ToArgb (), "32,64");
116                                 Assert.AreEqual (-9722415, bmp.GetPixel (32, 96).ToArgb (), "32,96");
117                                 Assert.AreEqual (-131076, bmp.GetPixel (64, 0).ToArgb (), "64,0");
118                                 Assert.AreEqual (-2702435, bmp.GetPixel (64, 32).ToArgb (), "64,32");
119                                 Assert.AreEqual (-6325922, bmp.GetPixel (64, 64).ToArgb (), "64,64");
120                                 Assert.AreEqual (-12411924, bmp.GetPixel (64, 96).ToArgb (), "64,96");
121                                 Assert.AreEqual (-131076, bmp.GetPixel (96, 0).ToArgb (), "96,0");
122                                 Assert.AreEqual (-7766649, bmp.GetPixel (96, 32).ToArgb (), "96,32");
123                                 Assert.AreEqual (-11512986, bmp.GetPixel (96, 64).ToArgb (), "96,64");
124                                 Assert.AreEqual (-12616230, bmp.GetPixel (96, 96).ToArgb (), "96,96");
125 #endif
126                         }
127                 }
128
129                 [Test]
130                 [Category ("NotWorking")]
131                 public void Bitmat8bitsData ()
132                 {
133                         string sInFile = getInFile ("bitmaps/nature24bits.gif");
134                         using (Bitmap bmp = new Bitmap (sInFile)) {
135                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
136                                 try {
137                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
138                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
139                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
140                                         int size = data.Height * data.Stride;
141                                         unsafe {
142                                                 byte* scan = (byte*) data.Scan0;
143 #if false
144                                                 // 1009 is the first prime after 1000 (so we're not affected by a recurring pattern)
145                                                 for (int p = 0; p < size; p += 1009) {
146                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
147                                                 }
148 #else
149                                                 // sampling values from a well known bitmap
150                                                 Assert.AreEqual (190, *(scan + 0), "0");
151                                                 Assert.AreEqual (217, *(scan + 1009), "1009");
152                                                 Assert.AreEqual (120, *(scan + 2018), "2018");
153                                                 Assert.AreEqual (253, *(scan + 3027), "3027");
154                                                 Assert.AreEqual (233, *(scan + 4036), "4036");
155                                                 Assert.AreEqual (176, *(scan + 5045), "5045");
156                                                 Assert.AreEqual (151, *(scan + 6054), "6054");
157                                                 Assert.AreEqual (220, *(scan + 7063), "7063");
158                                                 Assert.AreEqual (139, *(scan + 8072), "8072");
159                                                 Assert.AreEqual (121, *(scan + 9081), "9081");
160                                                 Assert.AreEqual (160, *(scan + 10090), "10090");
161                                                 Assert.AreEqual (92, *(scan + 11099), "11099");
162                                                 Assert.AreEqual (96, *(scan + 12108), "12108");
163                                                 Assert.AreEqual (64, *(scan + 13117), "13117");
164                                                 Assert.AreEqual (156, *(scan + 14126), "14126");
165                                                 Assert.AreEqual (68, *(scan + 15135), "15135");
166                                                 Assert.AreEqual (156, *(scan + 16144), "16144");
167                                                 Assert.AreEqual (84, *(scan + 17153), "17153");
168                                                 Assert.AreEqual (55, *(scan + 18162), "18162");
169                                                 Assert.AreEqual (68, *(scan + 19171), "19171");
170                                                 Assert.AreEqual (116, *(scan + 20180), "20180");
171                                                 Assert.AreEqual (61, *(scan + 21189), "21189");
172                                                 Assert.AreEqual (69, *(scan + 22198), "22198");
173                                                 Assert.AreEqual (75, *(scan + 23207), "23207");
174                                                 Assert.AreEqual (61, *(scan + 24216), "24216");
175                                                 Assert.AreEqual (66, *(scan + 25225), "25225");
176                                                 Assert.AreEqual (40, *(scan + 26234), "26234");
177                                                 Assert.AreEqual (55, *(scan + 27243), "27243");
178                                                 Assert.AreEqual (53, *(scan + 28252), "28252");
179                                                 Assert.AreEqual (215, *(scan + 29261), "29261");
180                                                 Assert.AreEqual (99, *(scan + 30270), "30270");
181                                                 Assert.AreEqual (67, *(scan + 31279), "31279");
182                                                 Assert.AreEqual (142, *(scan + 32288), "32288");
183 #endif
184                                         }
185                                 }
186                                 finally {
187                                         bmp.UnlockBits (data);
188                                 }
189                         }
190                 }
191
192                 private void Save (PixelFormat original, PixelFormat expected, bool colorCheck)
193                 {
194                         string sOutFile = String.Format ("linerect{0}-{1}.gif", getOutSufix (), expected.ToString ());
195
196                         // Save         
197                         Bitmap bmp = new Bitmap (100, 100, original);
198                         Graphics gr = Graphics.FromImage (bmp);
199
200                         using (Pen p = new Pen (Color.Red, 2)) {
201                                 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
202                                 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
203                         }
204
205                         try {
206 // FIXME: we don't save a pure red (F8 instead of FF) into the file so the color-check assert will fail
207                                 bmp.Save (sOutFile, ImageFormat.Gif);
208
209                                 // Load
210                                 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
211                                         Assert.AreEqual (expected, bmp.PixelFormat, "PixelFormat");
212                                         if (colorCheck) {
213                                                 Color color = bmpLoad.GetPixel (10, 10);
214                                                 Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color, "Red");
215                                         }
216                                 }
217                         }
218                         finally {
219                                 gr.Dispose ();
220                                 bmp.Dispose ();
221                                 try {
222                                         File.Delete (sOutFile);
223                                 }
224                                 catch {
225                                 }
226                         }
227                 }
228
229                 [Test]
230                 [Category ("NotWorking")] // palette mismatch
231                 public void Save_24bppRgb ()
232                 {
233                         Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, true);
234                 }
235
236                 [Test]
237                 [Category ("NotWorking")] // palette mismatch
238                 public void Save_32bppRgb ()
239                 {
240                         Save (PixelFormat.Format32bppRgb, PixelFormat.Format32bppRgb, true);
241                 }
242
243                 [Test]
244                 [Category ("NotWorking")] // palette mismatch
245                 public void Save_32bppArgb ()
246                 {
247                         Save (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, true);
248                 }
249
250                 [Test]
251                 [Category ("NotWorking")] // palette mismatch
252                 public void Save_32bppPArgb ()
253                 {
254                         Save (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppPArgb, true);
255                 }
256
257                 [Test]
258                 [Category ("NotWorking")]
259                 public void Save_48bppRgb ()
260                 {
261                         Save (PixelFormat.Format48bppRgb, PixelFormat.Format48bppRgb, false);
262                 }
263
264                 [Test]
265                 [Category ("NotWorking")]
266                 public void Save_64bppArgb ()
267                 {
268                         Save (PixelFormat.Format64bppArgb, PixelFormat.Format64bppArgb, false);
269                 }
270
271                 [Test]
272                 [Category ("NotWorking")]
273                 public void Save_64bppPArgb ()
274                 {
275                         Save (PixelFormat.Format64bppPArgb, PixelFormat.Format64bppPArgb, false);
276                 }
277         }
278 }