2 // ICO Codec class testing unit
5 // Jordi Mas i Hernàndez (jordi@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Drawing.Imaging;
34 using System.Security.Permissions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing.Imaging {
41 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class IconCodecTest {
45 public void FixtureSetUp ()
47 if (Type.GetType ("Mono.Runtime", false) != null)
48 Assert.Ignore ("ICON support is missing");
51 /* Get suffix to add to the filename */
52 internal string getOutSufix ()
56 int p = (int) Environment.OSVersion.Platform;
57 if ((p == 4) || (p == 128))
62 if (Type.GetType ("Mono.Runtime", false) == null)
70 /* Get the input directory depending on the runtime*/
71 internal string getInFile (string file)
73 string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
75 if (!File.Exists (sRslt))
76 sRslt = "Test/System.Drawing/" + file;
81 /* Checks bitmap features on a know 1bbp bitmap */
83 public void Bitmap16Features ()
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);
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");
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");
100 Assert.AreEqual (16, bmp.Size.Width, "bmp.Size.Width");
101 Assert.AreEqual (16, bmp.Size.Height, "bmp.Size.Height");
106 public void Bitmap16Pixels ()
108 string sInFile = getInFile ("bitmaps/smiley.ico");
109 using (Bitmap bmp = new Bitmap (sInFile)) {
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);
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");
138 [Category ("NotWorking")]
139 public void Bitmat16Data ()
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);
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;
150 byte* scan = (byte*) data.Scan0;
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);
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");
222 bmp.UnlockBits (data);
227 /* Checks bitmap features on a know 1bbp bitmap */
229 public void Bitmap32Features ()
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);
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");
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");
246 Assert.AreEqual (32, bmp.Size.Width, "bmp.Size.Width");
247 Assert.AreEqual (32, bmp.Size.Height, "bmp.Size.Height");
252 public void Bitmap32Pixels ()
254 string sInFile = getInFile ("bitmaps/smiley.ico");
255 using (Bitmap bmp = new Bitmap (sInFile)) {
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);
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");
284 [Category ("NotWorking")]
285 public void Bitmat32Data ()
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);
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;
296 byte* scan = (byte*) data.Scan0;
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);
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");
363 bmp.UnlockBits (data);
368 private void Save (PixelFormat original, PixelFormat expected, bool colorCheck)
370 string sOutFile = String.Format ("linerect{0}-{1}.ico", getOutSufix (), expected.ToString ());
373 Bitmap bmp = new Bitmap (100, 100, original);
374 Graphics gr = Graphics.FromImage (bmp);
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);
382 bmp.Save (sOutFile, ImageFormat.Icon);
385 using (Bitmap bmpLoad = new Bitmap (sOutFile)) {
386 Assert.AreEqual (expected, bmpLoad.PixelFormat, "PixelFormat");
388 Color color = bmpLoad.GetPixel (10, 10);
389 Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), color, "Red");
397 File.Delete (sOutFile);
405 public void Save_24bppRgb ()
407 Save (PixelFormat.Format24bppRgb, PixelFormat.Format24bppRgb, true);
411 public void Save_32bppRgb ()
413 Save (PixelFormat.Format32bppRgb, PixelFormat.Format32bppArgb, true);
417 public void Save_32bppArgb ()
419 Save (PixelFormat.Format32bppArgb, PixelFormat.Format32bppArgb, true);
423 public void Save_32bppPArgb ()
425 Save (PixelFormat.Format32bppPArgb, PixelFormat.Format32bppArgb, true);
429 [Category ("NotWorking")]
430 public void Save_48bppRgb ()
432 Save (PixelFormat.Format48bppRgb, PixelFormat.Format48bppRgb, false);
436 [Category ("NotWorking")]
437 public void Save_64bppArgb ()
439 Save (PixelFormat.Format64bppArgb, PixelFormat.Format64bppArgb, false);
443 [Category ("NotWorking")]
444 public void Save_64bppPArgb ()
446 Save (PixelFormat.Format64bppPArgb, PixelFormat.Format64bppArgb, false);