b4724040e1e4b1908ca15fe12af82095708422a4
[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                 [ExpectedException (typeof (ArgumentException))]
178                 public void SetColorMatrix_Null ()
179                 {
180                         using (ImageAttributes ia = new ImageAttributes ()) {
181                                 ia.SetColorMatrix (null);
182                         }
183                 }
184
185                 [Test]
186                 public void SetColorMatrix_Default ()
187                 {
188                         using (ImageAttributes ia = new ImageAttributes ()) {
189                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default);
190                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
191                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Brush);
192                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
193                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Pen);
194                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Text);
195                         }
196                 }
197
198                 [Test]
199                 [ExpectedException (typeof (ArgumentException))]
200                 public void SetColorMatrix_Default_Any ()
201                 {
202                         using (ImageAttributes ia = new ImageAttributes ()) {
203                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Any);
204                         }
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (ArgumentException))]
209                 public void SetColorMatrix_Default_Count ()
210                 {
211                         using (ImageAttributes ia = new ImageAttributes ()) {
212                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Count);
213                         }
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (ArgumentException))]
218                 public void SetColorMatrix_AltGrays ()
219                 {
220                         using (ImageAttributes ia = new ImageAttributes ()) {
221                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays);
222                         }
223                 }
224
225                 [Test]
226                 [ExpectedException (typeof (ArgumentException))]
227                 public void SetColorMatrix_AltGrays_Any ()
228                 {
229                         using (ImageAttributes ia = new ImageAttributes ()) {
230                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Any);
231                         }
232                 }
233
234                 [Test]
235                 [ExpectedException (typeof (ArgumentException))]
236                 public void SetColorMatrix_AltGrays_Bitmap ()
237                 {
238                         using (ImageAttributes ia = new ImageAttributes ()) {
239                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Bitmap);
240                         }
241                 }
242
243                 [Test]
244                 [ExpectedException (typeof (ArgumentException))]
245                 public void SetColorMatrix_AltGrays_Brush ()
246                 {
247                         using (ImageAttributes ia = new ImageAttributes ()) {
248                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Brush);
249                         }
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (ArgumentException))]
254                 public void SetColorMatrix_AltGrays_Count ()
255                 {
256                         using (ImageAttributes ia = new ImageAttributes ()) {
257                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Count);
258                         }
259                 }
260
261                 [Test]
262                 [ExpectedException (typeof (ArgumentException))]
263                 public void SetColorMatrix_AltGrays_Default ()
264                 {
265                         using (ImageAttributes ia = new ImageAttributes ()) {
266                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
267                         }
268                 }
269
270                 [Test]
271                 [ExpectedException (typeof (ArgumentException))]
272                 public void SetColorMatrix_AltGrays_Pen ()
273                 {
274                         using (ImageAttributes ia = new ImageAttributes ()) {
275                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Pen);
276                         }
277                 }
278
279                 [Test]
280                 [ExpectedException (typeof (ArgumentException))]
281                 public void SetColorMatrix_AltGrays_Text ()
282                 {
283                         using (ImageAttributes ia = new ImageAttributes ()) {
284                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Text);
285                         }
286                 }
287
288                 [Test]
289                 public void SetColorMatrix_SkipGrays ()
290                 {
291                         using (ImageAttributes ia = new ImageAttributes ()) {
292                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays);
293                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Bitmap);
294                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Brush);
295                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
296                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Pen);
297                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Text);
298                         }
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (ArgumentException))]
303                 public void SetColorMatrix_SkipGrays_Any ()
304                 {
305                         using (ImageAttributes ia = new ImageAttributes ()) {
306                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Any);
307                         }
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ArgumentException))]
312                 public void SetColorMatrix_SkipGrays_Count ()
313                 {
314                         using (ImageAttributes ia = new ImageAttributes ()) {
315                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Count);
316                         }
317                 }
318
319                 [Test]
320                 [ExpectedException (typeof (ArgumentException))]
321                 public void SetColorMatrix_InvalidFlag ()
322                 {
323                         using (ImageAttributes ia = new ImageAttributes ()) {
324                                 ia.SetColorMatrix (global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
325                         }
326                 }
327
328                 [Test]
329                 [ExpectedException (typeof (ArgumentException))]
330                 public void SetColorMatrix_InvalidType()
331                 {
332                         using (ImageAttributes ia = new ImageAttributes ()) {
333                                 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType)Int32.MinValue);
334                         }
335                 }
336
337                 [Test]
338                 [ExpectedException (typeof (ArgumentException))]
339                 public void SetColorMatrices_Null_ColorMatrix ()
340                 {
341                         using (ImageAttributes ia = new ImageAttributes ()) {
342                                 ia.SetColorMatrices (null, global_color_matrix);
343                         }
344                 }
345
346                 [Test]
347                 public void SetColorMatrices_ColorMatrix_Null ()
348                 {
349                         using (ImageAttributes ia = new ImageAttributes ()) {
350                                 ia.SetColorMatrices (global_color_matrix, null);
351                                 ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.Default);
352                                 ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.SkipGrays);
353                         }
354                 }
355
356                 [Test]
357                 [ExpectedException (typeof (ArgumentException))]
358                 public void SetColorMatrices_ColorMatrix_Null_AltGrays ()
359                 {
360                         using (ImageAttributes ia = new ImageAttributes ()) {
361                                 ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.AltGrays);
362                         }
363                 }
364
365                 [Test]
366                 public void SetColorMatrices_ColorMatrix_ColorMatrix ()
367                 {
368                         using (ImageAttributes ia = new ImageAttributes ()) {
369                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix);
370                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default);
371                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.SkipGrays);
372                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.AltGrays);
373                         }
374                 }
375
376                 [Test]
377                 public void SetColorMatrices_Gray ()
378                 {
379                         Color c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
380                         Assert.AreEqual (0xFFFF8080, (uint)c.ToArgb (), "Gray|Default|Default");
381
382                         c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
383                         Assert.AreEqual (0xFF808080, (uint) c.ToArgb (), "Gray|SkipGrays|Default");
384
385                         c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
386                         Assert.AreEqual (0xFFFFFFFF, (uint) c.ToArgb (), "Gray|AltGrays|Default");
387                 }
388
389                 [Test]
390                 public void SetColorMatrices_Color ()
391                 {
392                         Color c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
393                         Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|Default|Default");
394
395                         c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
396                         Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|SkipGrays|Default");
397
398                         c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
399                         Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|AltGrays|Default");
400                 }
401
402                 [Test]
403                 [ExpectedException (typeof (ArgumentException))]
404                 public void SetColorMatrices_InvalidFlags ()
405                 {
406                         using (ImageAttributes ia = new ImageAttributes ()) {
407                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
408                         }
409                 }
410
411                 [Test]
412                 [ExpectedException (typeof (ArgumentException))]
413                 public void SetColorMatrices_InvalidType ()
414                 {
415                         using (ImageAttributes ia = new ImageAttributes ()) {
416                                 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType) Int32.MinValue);
417                         }
418                 }
419
420                 private void Alpha (string prefix, int n, float a)
421                 {
422                         ColorMatrix cm = new ColorMatrix (new float[][] {
423                                 new float[]     {1,     0,      0,      0,      0}, //R
424                                 new float[]     {0,     1,      0,      0,      0}, //G
425                                 new float[]     {0,     0,      1,      0,      0}, //B
426                                 new float[]     {0,     0,      0,      a,      0}, //A
427                                 new float[]     {0,     0,      0,      0,      1}, //Translation
428                           });
429
430                         using (Bitmap bmp = new Bitmap (1, 4)) {
431                                 bmp.SetPixel (0, 0, Color.White);
432                                 bmp.SetPixel (0, 1, Color.Red);
433                                 bmp.SetPixel (0, 2, Color.Lime);
434                                 bmp.SetPixel (0, 3, Color.Blue);
435                                 using (Bitmap b = new Bitmap (1, 4)) {
436                                         using (Graphics g = Graphics.FromImage (b)) {
437                                                 ImageAttributes ia = new ImageAttributes ();
438                                                 ia.SetColorMatrix (cm);
439                                                 g.FillRectangle (Brushes.White, new Rectangle (0, 0, 1, 4));
440                                                 g.DrawImage (bmp, new Rectangle (0, 0, 1, 4), 0, 0, 1, 4, GraphicsUnit.Pixel, ia);
441                                                 Assert.AreEqual (Color.FromArgb (255, 255, 255, 255), b.GetPixel (0,0), prefix + "-0,0");
442                                                 int val = 255 - n;
443                                                 Assert.AreEqual (Color.FromArgb (255, 255, val, val), b.GetPixel (0, 1), prefix + "-0,1");
444                                                 Assert.AreEqual (Color.FromArgb (255, val, 255, val), b.GetPixel (0, 2), prefix + "-0,2");
445                                                 Assert.AreEqual (Color.FromArgb (255, val, val, 255), b.GetPixel (0, 3), prefix + "-0,3");
446                                         }
447                                 }
448                         }
449                 }
450
451                 [Test]
452                 public void ColorMatrixAlpha ()
453                 {
454                         for (int i = 0; i < 256; i++) {
455                                 Alpha (i.ToString (), i, (float) i / 255);
456                                 // generally color matrix are specified with values between [0..1]
457                                 Alpha ("small-" + i.ToString (), i, (float) i / 255);
458                                 // but GDI+ also accept value > 1
459                                 Alpha ("big-" + i.ToString (), i, 256 - i);
460                         }
461                 }
462         }
463 }