Make System.Drawing unit tests use Assert.Throws instead of [ExpectedException] ...
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TestImageAttributes.cs
1 //
2 // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 //
24 // Authors:
25 //      Jordi Mas i Hernandez (jordi@ximian.com)
26 //      Sebastien Pouliot  <sebastien@ximian.com>
27 //
28
29 using System;
30 using System.IO;
31 using System.Drawing;
32 using System.Drawing.Imaging;
33 using System.Security.Permissions;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Drawing.Imaging {
37
38         [TestFixture]
39         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40         public class ImageAttributesTest {
41
42                 static ColorMatrix global_color_matrix = new ColorMatrix (new float[][] {
43                         new float[]     {2,     0,      0,      0,      0}, //R
44                         new float[]     {0,     1,      0,      0,      0}, //G
45                         new float[]     {0,     0,      1,      0,      0}, //B
46                         new float[]     {0,     0,      0,      1,      0}, //A
47                         new float[]     {0.2f,  0,      0,      0,      0}, //Translation
48                 });
49
50                 static ColorMatrix global_gray_matrix = new ColorMatrix (new float[][] {
51                         new float[]     {1,     0,      0,      0,      0}, //R
52                         new float[]     {0,     2,      0,      0,      0}, //G
53                         new float[]     {0,     0,      3,      0,      0}, //B
54                         new float[]     {0,     0,      0,      1,      0}, //A
55                         new float[]     {0.5f,  0,      0,      0,      0}, //Translation
56                 });
57
58                 private static Color ProcessColorMatrix (Color color, ColorMatrix colorMatrix)
59                 {
60                         using (Bitmap bmp = new Bitmap (64, 64)) {
61                                 using (Graphics gr = Graphics.FromImage (bmp)) {
62                                         ImageAttributes imageAttr = new ImageAttributes ();
63                                         bmp.SetPixel (0,0, color);
64                                         imageAttr.SetColorMatrix (colorMatrix);
65                                         gr.DrawImage (bmp, new Rectangle (0, 0, 64, 64), 0, 0, 64, 64, GraphicsUnit.Pixel, imageAttr);          
66                                         return bmp.GetPixel (0,0);
67                                 }
68                         }
69                 }
70
71
72                 // Text Color Matrix processing
73                 [Test]
74                 public void ColorMatrix1 ()
75                 {                       
76                         Color clr_src, clr_rslt;
77                         
78                         ColorMatrix cm = new ColorMatrix (new float[][] {
79                                 new float[]     {2,     0,      0,      0,      0}, //R
80                                 new float[]     {0,     1,      0,      0,      0}, //G
81                                 new float[]     {0,     0,      1,      0,      0}, //B
82                                 new float[]     {0,     0,      0,      1,      0}, //A
83                                 new float[]     {0.2f,  0,      0,      0,      0}, //Translation
84                           });
85
86                         clr_src = Color.FromArgb (255, 100, 20, 50);
87                         clr_rslt = ProcessColorMatrix (clr_src, cm);
88
89                         Assert.AreEqual (Color.FromArgb (255, 251, 20, 50), clr_rslt, "Color");
90                 }
91
92                 [Test]
93                 public void ColorMatrix2 ()
94                 {
95                         Color clr_src, clr_rslt;
96
97                         ColorMatrix cm = new ColorMatrix (new float[][] {
98                                 new float[]     {1,     0,      0,      0,      0}, //R
99                                 new float[]     {0,     1,      0,      0,      0}, //G
100                                 new float[]     {0,     0,      1.5f,   0,      0}, //B
101                                 new float[]     {0,     0,      0.5f,   1,      0}, //A
102                                 new float[]     {0,     0,      0,      0,      0}, //Translation
103                           });
104
105                         clr_src = Color.FromArgb (255, 100, 40, 25);
106                         clr_rslt = ProcessColorMatrix (clr_src, cm);
107                         Assert.AreEqual (Color.FromArgb (255, 100, 40, 165), clr_rslt, "Color");
108                 }
109
110                 private void Bug80323 (Color c)
111                 {
112                         string fileName = String.Format ("80323-{0}.png", c.ToArgb ().ToString ("X"));
113
114                         // test case from bug #80323
115                         ColorMatrix cm = new ColorMatrix (new float[][] {
116                                 new float[]     {1,     0,      0,      0,      0}, //R
117                                 new float[]     {0,     1,      0,      0,      0}, //G
118                                 new float[]     {0,     0,      1,      0,      0}, //B
119                                 new float[]     {0,     0,      0,      0.5f,   0}, //A
120                                 new float[]     {0,     0,      0,      0,      1}, //Translation
121                           });
122
123                         using (SolidBrush sb = new SolidBrush (c)) {
124                                 using (Bitmap bmp = new Bitmap (100, 100)) {
125                                         using (Graphics gr = Graphics.FromImage (bmp)) {
126                                                 gr.FillRectangle (Brushes.White, 0, 0, 100, 100);
127                                                 gr.FillEllipse (sb, 0, 0, 100, 100);
128                                         }
129                                         using (Bitmap b = new Bitmap (200, 100)) {
130                                                 using (Graphics g = Graphics.FromImage (b)) {
131                                                         g.FillRectangle (Brushes.White, 0, 0, 200, 100);
132
133                                                         ImageAttributes ia = new ImageAttributes ();
134                                                         ia.SetColorMatrix (cm);
135                                                         g.DrawImage (bmp, new Rectangle (0, 0, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel, null);
136                                                         g.DrawImage (bmp, new Rectangle (100, 0, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel, ia);
137                                                 }
138                                                 b.Save (fileName);
139                                                 Assert.AreEqual (Color.FromArgb (255, 255, 155, 155), b.GetPixel (50, 50), "50,50");
140                                                 Assert.AreEqual (Color.FromArgb (255, 255, 205, 205), b.GetPixel (150, 50), "150,50");
141                                         }
142                                 }
143                         }
144
145                         File.Delete (fileName);
146                 }
147         
148                 [Test]
149                 public void ColorMatrix_80323_UsingAlpha ()
150                 {
151                         Bug80323 (Color.FromArgb (100, 255, 0, 0));
152                 }
153
154                 [Test]
155                 public void ColorMatrix_80323_WithoutAlpha ()
156                 {
157                         // this color is identical, once drawn over the bitmap, to Color.FromArgb (100, 255, 0, 0)
158                         Bug80323 (Color.FromArgb (255, 255, 155, 155));
159                 }
160
161
162
163                 private static Color ProcessColorMatrices (Color color, ColorMatrix colorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags, ColorAdjustType type)
164                 {
165                         using (Bitmap bmp = new Bitmap (64, 64)) {
166                                 using (Graphics gr = Graphics.FromImage (bmp)) {
167                                         ImageAttributes imageAttr = new ImageAttributes ();
168                                         bmp.SetPixel (0, 0, color);
169                                         imageAttr.SetColorMatrices (colorMatrix, grayMatrix, flags, type);
170                                         gr.DrawImage (bmp, new Rectangle (0, 0, 64, 64), 0, 0, 64, 64, GraphicsUnit.Pixel, imageAttr);
171                                         return bmp.GetPixel (0, 0);
172                                 }
173                         }
174                 }
175
176                 [Test]
177                 public void SetColorMatrix_Null ()
178                 {
179                         using (ImageAttributes ia = new ImageAttributes ()) {
180                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (null));
181                         }
182                 }
183
184                 [Test]
185                 public void SetColorMatrix_Default ()
186                 {
187                         using (ImageAttributes ia = new ImageAttributes ()) {
188                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default);
189                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
190                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Brush);
191                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
192                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Pen);
193                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Text);
194                         }
195                 }
196
197                 [Test]
198                 public void SetColorMatrix_Default_Any ()
199                 {
200                         using (ImageAttributes ia = new ImageAttributes ()) {
201                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Any));
202                         }
203                 }
204
205                 [Test]
206                 public void SetColorMatrix_Default_Count ()
207                 {
208                         using (ImageAttributes ia = new ImageAttributes ()) {
209                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Count));
210                         }
211                 }
212
213                 [Test]
214                 public void SetColorMatrix_AltGrays ()
215                 {
216                         using (ImageAttributes ia = new ImageAttributes ()) {
217                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays));
218                         }
219                 }
220
221                 [Test]
222                 public void SetColorMatrix_AltGrays_Any ()
223                 {
224                         using (ImageAttributes ia = new ImageAttributes ()) {
225                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Any));
226                         }
227                 }
228
229                 [Test]
230                 public void SetColorMatrix_AltGrays_Bitmap ()
231                 {
232                         using (ImageAttributes ia = new ImageAttributes ()) {
233                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Bitmap));
234                         }
235                 }
236
237                 [Test]
238                 public void SetColorMatrix_AltGrays_Brush ()
239                 {
240                         using (ImageAttributes ia = new ImageAttributes ()) {
241                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Brush));
242                         }
243                 }
244
245                 [Test]
246                 public void SetColorMatrix_AltGrays_Count ()
247                 {
248                         using (ImageAttributes ia = new ImageAttributes ()) {
249                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Count));
250                         }
251                 }
252
253                 [Test]
254                 public void SetColorMatrix_AltGrays_Default ()
255                 {
256                         using (ImageAttributes ia = new ImageAttributes ()) {
257                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default));
258                         }
259                 }
260
261                 [Test]
262                 public void SetColorMatrix_AltGrays_Pen ()
263                 {
264                         using (ImageAttributes ia = new ImageAttributes ()) {
265                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Pen));
266                         }
267                 }
268
269                 [Test]
270                 public void SetColorMatrix_AltGrays_Text ()
271                 {
272                         using (ImageAttributes ia = new ImageAttributes ()) {
273                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Text));
274                         }
275                 }
276
277                 [Test]
278                 public void SetColorMatrix_SkipGrays ()
279                 {
280                         using (ImageAttributes ia = new ImageAttributes ()) {
281                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays);
282                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Bitmap);
283                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Brush);
284                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
285                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Pen);
286                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Text);
287                         }
288                 }
289
290                 [Test]
291                 public void SetColorMatrix_SkipGrays_Any ()
292                 {
293                         using (ImageAttributes ia = new ImageAttributes ()) {
294                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Any));
295                         }
296                 }
297
298                 [Test]
299                 public void SetColorMatrix_SkipGrays_Count ()
300                 {
301                         using (ImageAttributes ia = new ImageAttributes ()) {
302                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Count));
303                         }
304                 }
305
306                 [Test]
307                 public void SetColorMatrix_InvalidFlag ()
308                 {
309                         using (ImageAttributes ia = new ImageAttributes ()) {
310                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, (ColorMatrixFlag) Int32.MinValue));
311                         }
312                 }
313
314                 [Test]
315                 public void SetColorMatrix_InvalidType()
316                 {
317                         using (ImageAttributes ia = new ImageAttributes ()) {
318                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType)Int32.MinValue));
319                         }
320                 }
321
322                 [Test]
323                 public void SetColorMatrices_Null_ColorMatrix ()
324                 {
325                         using (ImageAttributes ia = new ImageAttributes ()) {
326                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrices (null, global_color_matrix));
327                         }
328                 }
329
330                 [Test]
331                 public void SetColorMatrices_ColorMatrix_Null ()
332                 {
333                         using (ImageAttributes ia = new ImageAttributes ()) {
334                                 ia.SetColorMatrices (global_color_matrix, null);
335                                 ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.Default);
336                                 ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.SkipGrays);
337                         }
338                 }
339
340                 [Test]
341                 public void SetColorMatrices_ColorMatrix_Null_AltGrays ()
342                 {
343                         using (ImageAttributes ia = new ImageAttributes ()) {
344                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.AltGrays));
345                         }
346                 }
347
348                 [Test]
349                 public void SetColorMatrices_ColorMatrix_ColorMatrix ()
350                 {
351                         using (ImageAttributes ia = new ImageAttributes ()) {
352                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix);
353                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default);
354                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.SkipGrays);
355                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.AltGrays);
356                         }
357                 }
358
359                 [Test]
360                 public void SetColorMatrices_Gray ()
361                 {
362                         Color c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
363                         Assert.AreEqual (0xFFFF8080, (uint)c.ToArgb (), "Gray|Default|Default");
364
365                         c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
366                         Assert.AreEqual (0xFF808080, (uint) c.ToArgb (), "Gray|SkipGrays|Default");
367
368                         c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
369                         Assert.AreEqual (0xFFFFFFFF, (uint) c.ToArgb (), "Gray|AltGrays|Default");
370                 }
371
372                 [Test]
373                 public void SetColorMatrices_Color ()
374                 {
375                         Color c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
376                         Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|Default|Default");
377
378                         c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
379                         Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|SkipGrays|Default");
380
381                         c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
382                         Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|AltGrays|Default");
383                 }
384
385                 [Test]
386                 public void SetColorMatrices_InvalidFlags ()
387                 {
388                         using (ImageAttributes ia = new ImageAttributes ()) {
389                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrices (global_color_matrix, global_color_matrix, (ColorMatrixFlag) Int32.MinValue));
390                         }
391                 }
392
393                 [Test]
394                 public void SetColorMatrices_InvalidType ()
395                 {
396                         using (ImageAttributes ia = new ImageAttributes ()) {
397                                 Assert.Throws<ArgumentException> (() => ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType) Int32.MinValue));
398                         }
399                 }
400
401                 private void Alpha (string prefix, int n, float a)
402                 {
403                         ColorMatrix cm = new ColorMatrix (new float[][] {
404                                 new float[]     {1,     0,      0,      0,      0}, //R
405                                 new float[]     {0,     1,      0,      0,      0}, //G
406                                 new float[]     {0,     0,      1,      0,      0}, //B
407                                 new float[]     {0,     0,      0,      a,      0}, //A
408                                 new float[]     {0,     0,      0,      0,      1}, //Translation
409                           });
410
411                         using (Bitmap bmp = new Bitmap (1, 4)) {
412                                 bmp.SetPixel (0, 0, Color.White);
413                                 bmp.SetPixel (0, 1, Color.Red);
414                                 bmp.SetPixel (0, 2, Color.Lime);
415                                 bmp.SetPixel (0, 3, Color.Blue);
416                                 using (Bitmap b = new Bitmap (1, 4)) {
417                                         using (Graphics g = Graphics.FromImage (b)) {
418                                                 ImageAttributes ia = new ImageAttributes ();
419                                                 ia.SetColorMatrix (cm);
420                                                 g.FillRectangle (Brushes.White, new Rectangle (0, 0, 1, 4));
421                                                 g.DrawImage (bmp, new Rectangle (0, 0, 1, 4), 0, 0, 1, 4, GraphicsUnit.Pixel, ia);
422                                                 Assert.AreEqual (Color.FromArgb (255, 255, 255, 255), b.GetPixel (0,0), prefix + "-0,0");
423                                                 int val = 255 - n;
424                                                 Assert.AreEqual (Color.FromArgb (255, 255, val, val), b.GetPixel (0, 1), prefix + "-0,1");
425                                                 Assert.AreEqual (Color.FromArgb (255, val, 255, val), b.GetPixel (0, 2), prefix + "-0,2");
426                                                 Assert.AreEqual (Color.FromArgb (255, val, val, 255), b.GetPixel (0, 3), prefix + "-0,3");
427                                         }
428                                 }
429                         }
430                 }
431
432                 [Test]
433                 public void ColorMatrixAlpha ()
434                 {
435                         for (int i = 0; i < 256; i++) {
436                                 Alpha (i.ToString (), i, (float) i / 255);
437                                 // generally color matrix are specified with values between [0..1]
438                                 Alpha ("small-" + i.ToString (), i, (float) i / 255);
439                                 // but GDI+ also accept value > 1
440                                 Alpha ("big-" + i.ToString (), i, 256 - i);
441                         }
442                 }
443         }
444 }