7b61bc33c720dd009ee33b8d58d64d15a9b0f66b
[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                 /* 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                 [Ignore ("ICON support is missing")]
77                 public void Bitmap16Features ()
78                 {
79                         string sInFile = getInFile ("bitmaps/smiley.ico");
80                         using (Bitmap bmp = new Bitmap (sInFile)) {
81                                 GraphicsUnit unit = GraphicsUnit.World;
82                                 RectangleF rect = bmp.GetBounds (ref unit);
83
84                                 // ??? why is it a 4bbp ?
85                                 Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
86                                 Assert.AreEqual (16, bmp.Width, "bmp.Width");
87                                 Assert.AreEqual (16, bmp.Height, "bmp.Height");
88
89                                 Assert.AreEqual (0, rect.X, "rect.X");
90                                 Assert.AreEqual (0, rect.Y, "rect.Y");
91                                 Assert.AreEqual (16, rect.Width, "rect.Width");
92                                 Assert.AreEqual (16, rect.Height, "rect.Height");
93
94                                 Assert.AreEqual (16, bmp.Size.Width, "bmp.Size.Width");
95                                 Assert.AreEqual (16, bmp.Size.Height, "bmp.Size.Height");
96                         }
97                 }
98
99                 [Test]
100                 [Ignore ("ICON support is missing")]
101                 public void Bitmap16Pixels ()
102                 {
103                         string sInFile = getInFile ("bitmaps/smiley.ico");
104                         using (Bitmap bmp = new Bitmap (sInFile)) {
105 #if false
106                                 for (int x = 0; x < bmp.Width; x += 4) {
107                                         for (int y = 0; y < bmp.Height; y += 4)
108                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
109                                 }
110 #else
111                                 // sampling values from a well known bitmap
112                                 Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
113                                 Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
114                                 Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
115                                 Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
116                                 Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
117                                 Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
118                                 Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
119                                 Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
120                                 Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
121                                 Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
122                                 Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
123                                 Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
124                                 Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
125                                 Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
126                                 Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
127                                 Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
128 #endif
129                         }
130                 }
131
132                 [Test]
133                 [Category ("NotWorking")]
134                 [Ignore ("ICON support is missing")]
135                 public void Bitmat16Data ()
136                 {
137                         string sInFile = getInFile ("bitmaps/smiley.ico");
138                         using (Bitmap bmp = new Bitmap (sInFile)) {
139                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
140                                 try {
141                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
142                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
143                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
144                                         int size = data.Height * data.Stride;
145                                         unsafe {
146                                                 byte* scan = (byte*) data.Scan0;
147 #if false
148                                                 // 13 is prime (so we're not affected by a recurring pattern)
149                                                 for (int p = 0; p < size; p += 13) {
150                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
151                                                 }
152 #else
153                                                 // sampling values from a well known bitmap
154                                                 Assert.AreEqual (0, *(scan + 0), "0");
155                                                 Assert.AreEqual (0, *(scan + 13), "13");
156                                                 Assert.AreEqual (0, *(scan + 26), "26");
157                                                 Assert.AreEqual (0, *(scan + 39), "39");
158                                                 Assert.AreEqual (0, *(scan + 52), "52");
159                                                 Assert.AreEqual (0, *(scan + 65), "65");
160                                                 Assert.AreEqual (0, *(scan + 78), "78");
161                                                 Assert.AreEqual (0, *(scan + 91), "91");
162                                                 Assert.AreEqual (0, *(scan + 104), "104");
163                                                 Assert.AreEqual (0, *(scan + 117), "117");
164                                                 Assert.AreEqual (0, *(scan + 130), "130");
165                                                 Assert.AreEqual (0, *(scan + 143), "143");
166                                                 Assert.AreEqual (0, *(scan + 156), "156");
167                                                 Assert.AreEqual (255, *(scan + 169), "169");
168                                                 Assert.AreEqual (0, *(scan + 182), "182");
169                                                 Assert.AreEqual (0, *(scan + 195), "195");
170                                                 Assert.AreEqual (255, *(scan + 208), "208");
171                                                 Assert.AreEqual (255, *(scan + 221), "221");
172                                                 Assert.AreEqual (0, *(scan + 234), "234");
173                                                 Assert.AreEqual (128, *(scan + 247), "247");
174                                                 Assert.AreEqual (0, *(scan + 260), "260");
175                                                 Assert.AreEqual (0, *(scan + 273), "273");
176                                                 Assert.AreEqual (0, *(scan + 286), "286");
177                                                 Assert.AreEqual (255, *(scan + 299), "299");
178                                                 Assert.AreEqual (0, *(scan + 312), "312");
179                                                 Assert.AreEqual (128, *(scan + 325), "325");
180                                                 Assert.AreEqual (0, *(scan + 338), "338");
181                                                 Assert.AreEqual (0, *(scan + 351), "351");
182                                                 Assert.AreEqual (255, *(scan + 364), "364");
183                                                 Assert.AreEqual (0, *(scan + 377), "377");
184                                                 Assert.AreEqual (0, *(scan + 390), "390");
185                                                 Assert.AreEqual (255, *(scan + 403), "403");
186                                                 Assert.AreEqual (255, *(scan + 416), "416");
187                                                 Assert.AreEqual (0, *(scan + 429), "429");
188                                                 Assert.AreEqual (255, *(scan + 442), "442");
189                                                 Assert.AreEqual (0, *(scan + 455), "455");
190                                                 Assert.AreEqual (0, *(scan + 468), "468");
191                                                 Assert.AreEqual (0, *(scan + 481), "481");
192                                                 Assert.AreEqual (255, *(scan + 494), "494");
193                                                 Assert.AreEqual (0, *(scan + 507), "507");
194                                                 Assert.AreEqual (0, *(scan + 520), "520");
195                                                 Assert.AreEqual (0, *(scan + 533), "533");
196                                                 Assert.AreEqual (0, *(scan + 546), "546");
197                                                 Assert.AreEqual (255, *(scan + 559), "559");
198                                                 Assert.AreEqual (0, *(scan + 572), "572");
199                                                 Assert.AreEqual (0, *(scan + 585), "585");
200                                                 Assert.AreEqual (255, *(scan + 598), "598");
201                                                 Assert.AreEqual (0, *(scan + 611), "611");
202                                                 Assert.AreEqual (0, *(scan + 624), "624");
203                                                 Assert.AreEqual (0, *(scan + 637), "637");
204                                                 Assert.AreEqual (128, *(scan + 650), "650");
205                                                 Assert.AreEqual (0, *(scan + 663), "663");
206                                                 Assert.AreEqual (0, *(scan + 676), "676");
207                                                 Assert.AreEqual (0, *(scan + 689), "689");
208                                                 Assert.AreEqual (0, *(scan + 702), "702");
209                                                 Assert.AreEqual (0, *(scan + 715), "715");
210                                                 Assert.AreEqual (0, *(scan + 728), "728");
211                                                 Assert.AreEqual (0, *(scan + 741), "741");
212                                                 Assert.AreEqual (0, *(scan + 754), "754");
213                                                 Assert.AreEqual (0, *(scan + 767), "767");
214 #endif
215                                         }
216                                 }
217                                 finally {
218                                         bmp.UnlockBits (data);
219                                 }
220                         }
221                 }
222
223                 /* Checks bitmap features on a know 1bbp bitmap */
224                 [Test]
225                 [Ignore ("ICON support is missing")]
226                 public void Bitmap32Features ()
227                 {
228                         string sInFile = getInFile ("bitmaps/VisualPng.ico");
229                         using (Bitmap bmp = new Bitmap (sInFile)) {
230                                 GraphicsUnit unit = GraphicsUnit.World;
231                                 RectangleF rect = bmp.GetBounds (ref unit);
232
233                                 // ??? why is it a 4bbp ?
234                                 Assert.AreEqual (PixelFormat.Format32bppArgb, bmp.PixelFormat);
235                                 Assert.AreEqual (32, bmp.Width, "bmp.Width");
236                                 Assert.AreEqual (32, bmp.Height, "bmp.Height");
237
238                                 Assert.AreEqual (0, rect.X, "rect.X");
239                                 Assert.AreEqual (0, rect.Y, "rect.Y");
240                                 Assert.AreEqual (32, rect.Width, "rect.Width");
241                                 Assert.AreEqual (32, rect.Height, "rect.Height");
242
243                                 Assert.AreEqual (32, bmp.Size.Width, "bmp.Size.Width");
244                                 Assert.AreEqual (32, bmp.Size.Height, "bmp.Size.Height");
245                         }
246                 }
247
248                 [Test]
249                 [Ignore ("ICON support is missing")]
250                 public void Bitmap32Pixels ()
251                 {
252                         string sInFile = getInFile ("bitmaps/smiley.ico");
253                         using (Bitmap bmp = new Bitmap (sInFile)) {
254 #if false
255                                 for (int x = 0; x < bmp.Width; x += 4) {
256                                         for (int y = 0; y < bmp.Height; y += 4)
257                                                 Console.WriteLine ("\t\t\t\tAssert.AreEqual ({0}, bmp.GetPixel ({1}, {2}).ToArgb (), \"{1},{2}\");", bmp.GetPixel (x, y).ToArgb (), x, y);
258                                 }
259 #else
260                                 // sampling values from a well known bitmap
261                                 Assert.AreEqual (0, bmp.GetPixel (0, 0).ToArgb (), "0,0");
262                                 Assert.AreEqual (0, bmp.GetPixel (0, 4).ToArgb (), "0,4");
263                                 Assert.AreEqual (0, bmp.GetPixel (0, 8).ToArgb (), "0,8");
264                                 Assert.AreEqual (0, bmp.GetPixel (0, 12).ToArgb (), "0,12");
265                                 Assert.AreEqual (0, bmp.GetPixel (4, 0).ToArgb (), "4,0");
266                                 Assert.AreEqual (-256, bmp.GetPixel (4, 4).ToArgb (), "4,4");
267                                 Assert.AreEqual (-256, bmp.GetPixel (4, 8).ToArgb (), "4,8");
268                                 Assert.AreEqual (-8355840, bmp.GetPixel (4, 12).ToArgb (), "4,12");
269                                 Assert.AreEqual (0, bmp.GetPixel (8, 0).ToArgb (), "8,0");
270                                 Assert.AreEqual (-256, bmp.GetPixel (8, 4).ToArgb (), "8,4");
271                                 Assert.AreEqual (-256, bmp.GetPixel (8, 8).ToArgb (), "8,8");
272                                 Assert.AreEqual (-256, bmp.GetPixel (8, 12).ToArgb (), "8,12");
273                                 Assert.AreEqual (0, bmp.GetPixel (12, 0).ToArgb (), "12,0");
274                                 Assert.AreEqual (0, bmp.GetPixel (12, 4).ToArgb (), "12,4");
275                                 Assert.AreEqual (-8355840, bmp.GetPixel (12, 8).ToArgb (), "12,8");
276                                 Assert.AreEqual (0, bmp.GetPixel (12, 12).ToArgb (), "12,12");
277 #endif
278                         }
279                 }
280
281                 [Test]
282                 [Category ("NotWorking")]
283                 [Ignore ("ICON support is missing")]
284                 public void Bitmat32Data ()
285                 {
286                         string sInFile = getInFile ("bitmaps/smiley.ico");
287                         using (Bitmap bmp = new Bitmap (sInFile)) {
288                                 BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
289                                 try {
290                                         Assert.AreEqual (bmp.Height, data.Height, "Height");
291                                         Assert.AreEqual (bmp.Width, data.Width, "Width");
292                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
293                                         int size = data.Height * data.Stride;
294                                         unsafe {
295                                                 byte* scan = (byte*) data.Scan0;
296 #if false
297                                                 // 13 is prime (so we're not affected by a recurring pattern)
298                                                 for (int p = 0; p < size; p += 13) {
299                                                         Console.WriteLine ("\t\t\t\t\t\tAssert.AreEqual ({0}, *(scan + {1}), \"{1}\");", *(scan + p), p);
300                                                 }
301 #else
302                                                 // sampling values from a well known bitmap
303                                                 Assert.AreEqual (0, *(scan + 0), "0");
304                                                 Assert.AreEqual (0, *(scan + 13), "13");
305                                                 Assert.AreEqual (0, *(scan + 26), "26");
306                                                 Assert.AreEqual (0, *(scan + 39), "39");
307                                                 Assert.AreEqual (0, *(scan + 52), "52");
308                                                 Assert.AreEqual (0, *(scan + 65), "65");
309                                                 Assert.AreEqual (0, *(scan + 78), "78");
310                                                 Assert.AreEqual (0, *(scan + 91), "91");
311                                                 Assert.AreEqual (0, *(scan + 104), "104");
312                                                 Assert.AreEqual (0, *(scan + 117), "117");
313                                                 Assert.AreEqual (0, *(scan + 130), "130");
314                                                 Assert.AreEqual (0, *(scan + 143), "143");
315                                                 Assert.AreEqual (0, *(scan + 156), "156");
316                                                 Assert.AreEqual (255, *(scan + 169), "169");
317                                                 Assert.AreEqual (0, *(scan + 182), "182");
318                                                 Assert.AreEqual (0, *(scan + 195), "195");
319                                                 Assert.AreEqual (255, *(scan + 208), "208");
320                                                 Assert.AreEqual (255, *(scan + 221), "221");
321                                                 Assert.AreEqual (0, *(scan + 234), "234");
322                                                 Assert.AreEqual (128, *(scan + 247), "247");
323                                                 Assert.AreEqual (0, *(scan + 260), "260");
324                                                 Assert.AreEqual (0, *(scan + 273), "273");
325                                                 Assert.AreEqual (0, *(scan + 286), "286");
326                                                 Assert.AreEqual (255, *(scan + 299), "299");
327                                                 Assert.AreEqual (0, *(scan + 312), "312");
328                                                 Assert.AreEqual (128, *(scan + 325), "325");
329                                                 Assert.AreEqual (0, *(scan + 338), "338");
330                                                 Assert.AreEqual (0, *(scan + 351), "351");
331                                                 Assert.AreEqual (255, *(scan + 364), "364");
332                                                 Assert.AreEqual (0, *(scan + 377), "377");
333                                                 Assert.AreEqual (0, *(scan + 390), "390");
334                                                 Assert.AreEqual (255, *(scan + 403), "403");
335                                                 Assert.AreEqual (255, *(scan + 416), "416");
336                                                 Assert.AreEqual (0, *(scan + 429), "429");
337                                                 Assert.AreEqual (255, *(scan + 442), "442");
338                                                 Assert.AreEqual (0, *(scan + 455), "455");
339                                                 Assert.AreEqual (0, *(scan + 468), "468");
340                                                 Assert.AreEqual (0, *(scan + 481), "481");
341                                                 Assert.AreEqual (255, *(scan + 494), "494");
342                                                 Assert.AreEqual (0, *(scan + 507), "507");
343                                                 Assert.AreEqual (0, *(scan + 520), "520");
344                                                 Assert.AreEqual (0, *(scan + 533), "533");
345                                                 Assert.AreEqual (0, *(scan + 546), "546");
346                                                 Assert.AreEqual (255, *(scan + 559), "559");
347                                                 Assert.AreEqual (0, *(scan + 572), "572");
348                                                 Assert.AreEqual (0, *(scan + 585), "585");
349                                                 Assert.AreEqual (255, *(scan + 598), "598");
350                                                 Assert.AreEqual (0, *(scan + 611), "611");
351                                                 Assert.AreEqual (0, *(scan + 624), "624");
352                                                 Assert.AreEqual (0, *(scan + 637), "637");
353                                                 Assert.AreEqual (128, *(scan + 650), "650");
354                                                 Assert.AreEqual (0, *(scan + 663), "663");
355                                                 Assert.AreEqual (0, *(scan + 676), "676");
356                                                 Assert.AreEqual (0, *(scan + 689), "689");
357                                                 Assert.AreEqual (0, *(scan + 702), "702");
358 #endif
359                                         }
360                                 }
361                                 finally {
362                                         bmp.UnlockBits (data);
363                                 }
364                         }
365                 }
366
367                 [Test]
368                 [Ignore ("ICON support is missing")]
369                 public void Save ()
370                 {
371                         string sOutFile = "linerect" + getOutSufix () + ".ico";
372
373                         // Save         
374                         Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);
375                         Graphics gr = Graphics.FromImage (bmp);
376
377                         using (Pen p = new Pen (Color.Red, 2)) {
378                                 gr.DrawLine (p, 10.0F, 10.0F, 90.0F, 90.0F);
379                                 gr.DrawRectangle (p, 10.0F, 10.0F, 80.0F, 80.0F);
380                         }
381
382                         try {
383                                 bmp.Save (sOutFile, ImageFormat.Icon);
384
385                                 // Load
386                                 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
387                                         Color color = bmpLoad.GetPixel (10, 10);
388                                         Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color);
389                                 }
390                         }
391                         finally {
392                                 gr.Dispose ();
393                                 bmp.Dispose ();
394                                 try {
395                                         File.Delete (sOutFile);
396                                 }
397                                 catch {
398                                 }
399                         }
400                 }
401         }
402 }