New test.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / IconCodecTest.cs
1 //
2 // ICO 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 IconCodecTest {
43
44                 [TestFixtureSetUp]
45                 public void FixtureSetUp ()
46                 {
47                         if (Type.GetType ("Mono.Runtime", false) != null)
48                                 Assert.Ignore ("ICON support is missing");
49                 }
50
51                 /* Get suffix to add to the filename */
52                 internal string getOutSufix ()
53                 {
54                         string s;
55
56                         int p = (int) Environment.OSVersion.Platform;
57                         if ((p == 4) || (p == 128))
58                                 s = "-unix";
59                         else
60                                 s = "-windows";
61
62                         if (Type.GetType ("Mono.Runtime", false) == null)
63                                 s += "-msnet";
64                         else
65                                 s += "-mono";
66
67                         return s;
68                 }
69
70                 /* Get the input directory depending on the runtime*/
71                 internal string getInFile (string file)
72                 {
73                         string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
74
75                         if (!File.Exists (sRslt))
76                                 sRslt = "Test/System.Drawing/" + file;
77
78                         return sRslt;
79                 }
80
81                 /* Checks bitmap features on a know 1bbp bitmap */
82                 [Test]
83                 public void Bitmap16Features ()
84                 {
85                         string sInFile = getInFile ("bitmaps/smiley.ico");
86                         using (Bitmap bmp = new Bitmap (sInFile)) {
87                                 GraphicsUnit unit = GraphicsUnit.World;
88                                 RectangleF rect = bmp.GetBounds (ref unit);
89
90                                 // ??? why is it a 4bbp ?
91                                 Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
92                                 Assert.AreEqual (16, bmp.Width, "bmp.Width");
93                                 Assert.AreEqual (16, bmp.Height, "bmp.Height");
94
95                                 Assert.AreEqual (0, rect.X, "rect.X");
96                                 Assert.AreEqual (0, rect.Y, "rect.Y");
97                                 Assert.AreEqual (16, rect.Width, "rect.Width");
98                                 Assert.AreEqual (16, rect.Height, "rect.Height");
99
100                                 Assert.AreEqual (16, bmp.Size.Width, "bmp.Size.Width");
101                                 Assert.AreEqual (16, bmp.Size.Height, "bmp.Size.Height");
102                         }
103                 }
104
105                 [Test]
106                 public void Bitmap16Pixels ()
107                 {
108                         string sInFile = getInFile ("bitmaps/smiley.ico");
109                         using (Bitmap bmp = new Bitmap (sInFile)) {
110 #if false
111                                 for (int x = 0; x < bmp.Width; x += 4) {
112                                         for (int y = 0; y < bmp.Height; y += 4)
113                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
114                                 }
115 #else
116                                 // sampling values from a well known bitmap
117                                 Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
118                                 Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
119                                 Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
120                                 Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
121                                 Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
122                                 Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
123                                 Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
124                                 Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
125                                 Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
126                                 Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
127                                 Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
128                                 Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
129                                 Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
130                                 Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
131                                 Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
132                                 Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
133 #endif
134                         }
135                 }
136
137                 [Test]
138                 [Category ("NotWorking")]
139                 public void Bitmat16Data ()
140                 {
141                         string sInFile = getInFile ("bitmaps/smiley.ico");
142                         using (Bitmap bmp = new Bitmap (sInFile)) {
143                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
144                                 try {
145                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
146                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
147                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
148                                         int size = data.Height * data.Stride;
149                                         unsafe {
150                                                 byte* scan = (byte*) data.Scan0;
151 #if false
152                                                 // 13 is prime (so we're not affected by a recurring pattern)
153                                                 for (int p = 0; p < size; p += 13) {
154                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
155                                                 }
156 #else
157                                                 // sampling values from a well known bitmap
158                                                 Assert.AreEqual (0, *(scan + 0), "0");
159                                                 Assert.AreEqual (0, *(scan + 13), "13");
160                                                 Assert.AreEqual (0, *(scan + 26), "26");
161                                                 Assert.AreEqual (0, *(scan + 39), "39");
162                                                 Assert.AreEqual (0, *(scan + 52), "52");
163                                                 Assert.AreEqual (0, *(scan + 65), "65");
164                                                 Assert.AreEqual (0, *(scan + 78), "78");
165                                                 Assert.AreEqual (0, *(scan + 91), "91");
166                                                 Assert.AreEqual (0, *(scan + 104), "104");
167                                                 Assert.AreEqual (0, *(scan + 117), "117");
168                                                 Assert.AreEqual (0, *(scan + 130), "130");
169                                                 Assert.AreEqual (0, *(scan + 143), "143");
170                                                 Assert.AreEqual (0, *(scan + 156), "156");
171                                                 Assert.AreEqual (255, *(scan + 169), "169");
172                                                 Assert.AreEqual (0, *(scan + 182), "182");
173                                                 Assert.AreEqual (0, *(scan + 195), "195");
174                                                 Assert.AreEqual (255, *(scan + 208), "208");
175                                                 Assert.AreEqual (255, *(scan + 221), "221");
176                                                 Assert.AreEqual (0, *(scan + 234), "234");
177                                                 Assert.AreEqual (128, *(scan + 247), "247");
178                                                 Assert.AreEqual (0, *(scan + 260), "260");
179                                                 Assert.AreEqual (0, *(scan + 273), "273");
180                                                 Assert.AreEqual (0, *(scan + 286), "286");
181                                                 Assert.AreEqual (255, *(scan + 299), "299");
182                                                 Assert.AreEqual (0, *(scan + 312), "312");
183                                                 Assert.AreEqual (128, *(scan + 325), "325");
184                                                 Assert.AreEqual (0, *(scan + 338), "338");
185                                                 Assert.AreEqual (0, *(scan + 351), "351");
186                                                 Assert.AreEqual (255, *(scan + 364), "364");
187                                                 Assert.AreEqual (0, *(scan + 377), "377");
188                                                 Assert.AreEqual (0, *(scan + 390), "390");
189                                                 Assert.AreEqual (255, *(scan + 403), "403");
190                                                 Assert.AreEqual (255, *(scan + 416), "416");
191                                                 Assert.AreEqual (0, *(scan + 429), "429");
192                                                 Assert.AreEqual (255, *(scan + 442), "442");
193                                                 Assert.AreEqual (0, *(scan + 455), "455");
194                                                 Assert.AreEqual (0, *(scan + 468), "468");
195                                                 Assert.AreEqual (0, *(scan + 481), "481");
196                                                 Assert.AreEqual (255, *(scan + 494), "494");
197                                                 Assert.AreEqual (0, *(scan + 507), "507");
198                                                 Assert.AreEqual (0, *(scan + 520), "520");
199                                                 Assert.AreEqual (0, *(scan + 533), "533");
200                                                 Assert.AreEqual (0, *(scan + 546), "546");
201                                                 Assert.AreEqual (255, *(scan + 559), "559");
202                                                 Assert.AreEqual (0, *(scan + 572), "572");
203                                                 Assert.AreEqual (0, *(scan + 585), "585");
204                                                 Assert.AreEqual (255, *(scan + 598), "598");
205                                                 Assert.AreEqual (0, *(scan + 611), "611");
206                                                 Assert.AreEqual (0, *(scan + 624), "624");
207                                                 Assert.AreEqual (0, *(scan + 637), "637");
208                                                 Assert.AreEqual (128, *(scan + 650), "650");
209                                                 Assert.AreEqual (0, *(scan + 663), "663");
210                                                 Assert.AreEqual (0, *(scan + 676), "676");
211                                                 Assert.AreEqual (0, *(scan + 689), "689");
212                                                 Assert.AreEqual (0, *(scan + 702), "702");
213                                                 Assert.AreEqual (0, *(scan + 715), "715");
214                                                 Assert.AreEqual (0, *(scan + 728), "728");
215                                                 Assert.AreEqual (0, *(scan + 741), "741");
216                                                 Assert.AreEqual (0, *(scan + 754), "754");
217                                                 Assert.AreEqual (0, *(scan + 767), "767");
218 #endif
219                                         }
220                                 }
221                                 finally {
222                                         bmp.UnlockBits (data);
223                                 }
224                         }
225                 }
226
227                 /* Checks bitmap features on a know 1bbp bitmap */
228                 [Test]
229                 public void Bitmap32Features ()
230                 {
231                         string sInFile = getInFile ("bitmaps/VisualPng.ico");
232                         using (Bitmap bmp = new Bitmap (sInFile)) {
233                                 GraphicsUnit unit = GraphicsUnit.World;
234                                 RectangleF rect = bmp.GetBounds (ref unit);
235
236                                 // ??? why is it a 4bbp ?
237                                 Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
238                                 Assert.AreEqual (32, bmp.Width, "bmp.Width");
239                                 Assert.AreEqual (32, bmp.Height, "bmp.Height");
240
241                                 Assert.AreEqual (0, rect.X, "rect.X");
242                                 Assert.AreEqual (0, rect.Y, "rect.Y");
243                                 Assert.AreEqual (32, rect.Width, "rect.Width");
244                                 Assert.AreEqual (32, rect.Height, "rect.Height");
245
246                                 Assert.AreEqual (32, bmp.Size.Width, "bmp.Size.Width");
247                                 Assert.AreEqual (32, bmp.Size.Height, "bmp.Size.Height");
248                         }
249                 }
250
251                 [Test]
252                 public void Bitmap32Pixels ()
253                 {
254                         string sInFile = getInFile ("bitmaps/smiley.ico");
255                         using (Bitmap bmp = new Bitmap (sInFile)) {
256 #if false
257                                 for (int x = 0; x < bmp.Width; x += 4) {
258                                         for (int y = 0; y < bmp.Height; y += 4)
259                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
260                                 }
261 #else
262                                 // sampling values from a well known bitmap
263                                 Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
264                                 Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
265                                 Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
266                                 Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
267                                 Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
268                                 Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
269                                 Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
270                                 Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
271                                 Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
272                                 Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
273                                 Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
274                                 Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
275                                 Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
276                                 Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
277                                 Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
278                                 Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
279 #endif
280                         }
281                 }
282
283                 [Test]
284                 [Category ("NotWorking")]
285                 public void Bitmat32Data ()
286                 {
287                         string sInFile = getInFile ("bitmaps/smiley.ico");
288                         using (Bitmap bmp = new Bitmap (sInFile)) {
289                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
290                                 try {
291                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
292                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
293                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
294                                         int size = data.Height * data.Stride;
295                                         unsafe {
296                                                 byte* scan = (byte*) data.Scan0;
297 #if false
298                                                 // 13 is prime (so we're not affected by a recurring pattern)
299                                                 for (int p = 0; p < size; p += 13) {
300                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
301                                                 }
302 #else
303                                                 // sampling values from a well known bitmap
304                                                 Assert.AreEqual (0, *(scan + 0), "0");
305                                                 Assert.AreEqual (0, *(scan + 13), "13");
306                                                 Assert.AreEqual (0, *(scan + 26), "26");
307                                                 Assert.AreEqual (0, *(scan + 39), "39");
308                                                 Assert.AreEqual (0, *(scan + 52), "52");
309                                                 Assert.AreEqual (0, *(scan + 65), "65");
310                                                 Assert.AreEqual (0, *(scan + 78), "78");
311                                                 Assert.AreEqual (0, *(scan + 91), "91");
312                                                 Assert.AreEqual (0, *(scan + 104), "104");
313                                                 Assert.AreEqual (0, *(scan + 117), "117");
314                                                 Assert.AreEqual (0, *(scan + 130), "130");
315                                                 Assert.AreEqual (0, *(scan + 143), "143");
316                                                 Assert.AreEqual (0, *(scan + 156), "156");
317                                                 Assert.AreEqual (255, *(scan + 169), "169");
318                                                 Assert.AreEqual (0, *(scan + 182), "182");
319                                                 Assert.AreEqual (0, *(scan + 195), "195");
320                                                 Assert.AreEqual (255, *(scan + 208), "208");
321                                                 Assert.AreEqual (255, *(scan + 221), "221");
322                                                 Assert.AreEqual (0, *(scan + 234), "234");
323                                                 Assert.AreEqual (128, *(scan + 247), "247");
324                                                 Assert.AreEqual (0, *(scan + 260), "260");
325                                                 Assert.AreEqual (0, *(scan + 273), "273");
326                                                 Assert.AreEqual (0, *(scan + 286), "286");
327                                                 Assert.AreEqual (255, *(scan + 299), "299");
328                                                 Assert.AreEqual (0, *(scan + 312), "312");
329                                                 Assert.AreEqual (128, *(scan + 325), "325");
330                                                 Assert.AreEqual (0, *(scan + 338), "338");
331                                                 Assert.AreEqual (0, *(scan + 351), "351");
332                                                 Assert.AreEqual (255, *(scan + 364), "364");
333                                                 Assert.AreEqual (0, *(scan + 377), "377");
334                                                 Assert.AreEqual (0, *(scan + 390), "390");
335                                                 Assert.AreEqual (255, *(scan + 403), "403");
336                                                 Assert.AreEqual (255, *(scan + 416), "416");
337                                                 Assert.AreEqual (0, *(scan + 429), "429");
338                                                 Assert.AreEqual (255, *(scan + 442), "442");
339                                                 Assert.AreEqual (0, *(scan + 455), "455");
340                                                 Assert.AreEqual (0, *(scan + 468), "468");
341                                                 Assert.AreEqual (0, *(scan + 481), "481");
342                                                 Assert.AreEqual (255, *(scan + 494), "494");
343                                                 Assert.AreEqual (0, *(scan + 507), "507");
344                                                 Assert.AreEqual (0, *(scan + 520), "520");
345                                                 Assert.AreEqual (0, *(scan + 533), "533");
346                                                 Assert.AreEqual (0, *(scan + 546), "546");
347                                                 Assert.AreEqual (255, *(scan + 559), "559");
348                                                 Assert.AreEqual (0, *(scan + 572), "572");
349                                                 Assert.AreEqual (0, *(scan + 585), "585");
350                                                 Assert.AreEqual (255, *(scan + 598), "598");
351                                                 Assert.AreEqual (0, *(scan + 611), "611");
352                                                 Assert.AreEqual (0, *(scan + 624), "624");
353                                                 Assert.AreEqual (0, *(scan + 637), "637");
354                                                 Assert.AreEqual (128, *(scan + 650), "650");
355                                                 Assert.AreEqual (0, *(scan + 663), "663");
356                                                 Assert.AreEqual (0, *(scan + 676), "676");
357                                                 Assert.AreEqual (0, *(scan + 689), "689");
358                                                 Assert.AreEqual (0, *(scan + 702), "702");
359 #endif
360                                         }
361                                 }
362                                 finally {
363                                         bmp.UnlockBits (data);
364                                 }
365                         }
366                 }
367
368                 private void Save (PixelFormat original, PixelFormat expected, bool colorCheck)
369                 {
370                         string sOutFile = String.Format ("linerect{0}-{1}.ico", getOutSufix (), expected.ToString ());
371
372                         // Save         
373                         Bitmap bmp = new Bitmap (100, 100, original);
374                         Graphics gr = Graphics.FromImage (bmp);
375
376                         using (Pen p = new Pen (Color.Red, 2)) {
377                                 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
378                                 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
379                         }
380
381                         try {
382                                 bmp.Save (sOutFile, ImageFormat.Icon);
383
384                                 // Load
385                                 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
386                                         Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
387                                         if (colorCheck) {
388                                                 Color color = bmpLoad.GetPixel (10, 10);
389                                                 Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color, "Red");
390                                         }
391                                 }
392                         }
393                         finally {
394                                 gr.Dispose ();
395                                 bmp.Dispose ();
396                                 try {
397                                         File.Delete (sOutFile);
398                                 }
399                                 catch {
400                                 }
401                         }
402                 }
403
404                 [Test]
405                 public void Save_24bppRgb ()
406                 {
407                         Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, true);
408                 }
409
410                 [Test]
411                 public void Save_32bppRgb ()
412                 {
413                         Save (PixelFormat.Format32bppRgb, PixelFormat.Format32bppArgb, true);
414                 }
415
416                 [Test]
417                 public void Save_32bppArgb ()
418                 {
419                         Save (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, true);
420                 }
421
422                 [Test]
423                 public void Save_32bppPArgb ()
424                 {
425                         Save (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppArgb, true);
426                 }
427
428                 [Test]
429                 [Category ("NotWorking")]
430                 public void Save_48bppRgb ()
431                 {
432                         Save (PixelFormat.Format48bppRgb, PixelFormat.Format48bppRgb, false);
433                 }
434
435                 [Test]
436                 [Category ("NotWorking")]
437                 public void Save_64bppArgb ()
438                 {
439                         Save (PixelFormat.Format64bppArgb, PixelFormat.Format64bppArgb, false);
440                 }
441
442                 [Test]
443                 [Category ("NotWorking")]
444                 public void Save_64bppPArgb ()
445                 {
446                         Save (PixelFormat.Format64bppPArgb, PixelFormat.Format64bppArgb, false);
447                 }
448         }
449 }