2 // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
25 // Jordi Mas i Hernandez (jordi@ximian.com)
26 // Sebastien Pouliot <sebastien@ximian.com>
32 using System.Drawing.Imaging;
33 using System.Security.Permissions;
34 using NUnit.Framework;
36 namespace MonoTests.System.Drawing.Imaging {
39 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40 public class ImageAttributesTest {
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
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
58 private static Color ProcessColorMatrix (Color color, ColorMatrix colorMatrix)
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);
72 // Text Color Matrix processing
74 public void ColorMatrix1 ()
76 Color clr_src, clr_rslt;
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
86 clr_src = Color.FromArgb (255, 100, 20, 50);
87 clr_rslt = ProcessColorMatrix (clr_src, cm);
89 Assert.AreEqual (Color.FromArgb (255, 251, 20, 50), clr_rslt, "Color");
93 public void ColorMatrix2 ()
95 Color clr_src, clr_rslt;
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
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");
110 private void Bug80323 (Color c)
112 string fileName = String.Format ("80323-{0}.png", c.ToArgb ().ToString ("X"));
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
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);
129 using (Bitmap b = new Bitmap (200, 100)) {
130 using (Graphics g = Graphics.FromImage (b)) {
131 g.FillRectangle (Brushes.White, 0, 0, 200, 100);
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);
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");
145 File.Delete (fileName);
149 public void ColorMatrix_80323_UsingAlpha ()
151 Bug80323 (Color.FromArgb (100, 255, 0, 0));
155 public void ColorMatrix_80323_WithoutAlpha ()
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));
163 private static Color ProcessColorMatrices (Color color, ColorMatrix colorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags, ColorAdjustType type)
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);
177 [ExpectedException (typeof (ArgumentException))]
178 public void SetColorMatrix_Null ()
180 using (ImageAttributes ia = new ImageAttributes ()) {
181 ia.SetColorMatrix (null);
186 public void SetColorMatrix_Default ()
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);
199 [ExpectedException (typeof (ArgumentException))]
200 public void SetColorMatrix_Default_Any ()
202 using (ImageAttributes ia = new ImageAttributes ()) {
203 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Any);
208 [ExpectedException (typeof (ArgumentException))]
209 public void SetColorMatrix_Default_Count ()
211 using (ImageAttributes ia = new ImageAttributes ()) {
212 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Count);
217 [ExpectedException (typeof (ArgumentException))]
218 public void SetColorMatrix_AltGrays ()
220 using (ImageAttributes ia = new ImageAttributes ()) {
221 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays);
226 [ExpectedException (typeof (ArgumentException))]
227 public void SetColorMatrix_AltGrays_Any ()
229 using (ImageAttributes ia = new ImageAttributes ()) {
230 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Any);
235 [ExpectedException (typeof (ArgumentException))]
236 public void SetColorMatrix_AltGrays_Bitmap ()
238 using (ImageAttributes ia = new ImageAttributes ()) {
239 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Bitmap);
244 [ExpectedException (typeof (ArgumentException))]
245 public void SetColorMatrix_AltGrays_Brush ()
247 using (ImageAttributes ia = new ImageAttributes ()) {
248 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Brush);
253 [ExpectedException (typeof (ArgumentException))]
254 public void SetColorMatrix_AltGrays_Count ()
256 using (ImageAttributes ia = new ImageAttributes ()) {
257 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Count);
262 [ExpectedException (typeof (ArgumentException))]
263 public void SetColorMatrix_AltGrays_Default ()
265 using (ImageAttributes ia = new ImageAttributes ()) {
266 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
271 [ExpectedException (typeof (ArgumentException))]
272 public void SetColorMatrix_AltGrays_Pen ()
274 using (ImageAttributes ia = new ImageAttributes ()) {
275 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Pen);
280 [ExpectedException (typeof (ArgumentException))]
281 public void SetColorMatrix_AltGrays_Text ()
283 using (ImageAttributes ia = new ImageAttributes ()) {
284 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Text);
289 public void SetColorMatrix_SkipGrays ()
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);
302 [ExpectedException (typeof (ArgumentException))]
303 public void SetColorMatrix_SkipGrays_Any ()
305 using (ImageAttributes ia = new ImageAttributes ()) {
306 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Any);
311 [ExpectedException (typeof (ArgumentException))]
312 public void SetColorMatrix_SkipGrays_Count ()
314 using (ImageAttributes ia = new ImageAttributes ()) {
315 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Count);
320 [ExpectedException (typeof (ArgumentException))]
321 public void SetColorMatrix_InvalidFlag ()
323 using (ImageAttributes ia = new ImageAttributes ()) {
324 ia.SetColorMatrix (global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
329 [ExpectedException (typeof (ArgumentException))]
330 public void SetColorMatrix_InvalidType()
332 using (ImageAttributes ia = new ImageAttributes ()) {
333 ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType)Int32.MinValue);
338 [ExpectedException (typeof (ArgumentException))]
339 public void SetColorMatrices_Null_ColorMatrix ()
341 using (ImageAttributes ia = new ImageAttributes ()) {
342 ia.SetColorMatrices (null, global_color_matrix);
347 public void SetColorMatrices_ColorMatrix_Null ()
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);
357 [ExpectedException (typeof (ArgumentException))]
358 public void SetColorMatrices_ColorMatrix_Null_AltGrays ()
360 using (ImageAttributes ia = new ImageAttributes ()) {
361 ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.AltGrays);
366 public void SetColorMatrices_ColorMatrix_ColorMatrix ()
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);
377 public void SetColorMatrices_Gray ()
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");
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");
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");
390 public void SetColorMatrices_Color ()
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");
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");
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");
403 [ExpectedException (typeof (ArgumentException))]
404 public void SetColorMatrices_InvalidFlags ()
406 using (ImageAttributes ia = new ImageAttributes ()) {
407 ia.SetColorMatrices (global_color_matrix, global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
412 [ExpectedException (typeof (ArgumentException))]
413 public void SetColorMatrices_InvalidType ()
415 using (ImageAttributes ia = new ImageAttributes ()) {
416 ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType) Int32.MinValue);
420 private void Alpha (string prefix, int n, float a)
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
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");
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");
452 public void ColorMatrixAlpha ()
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);