TestImageAttributes.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 ([email protected])
  26. // Sebastien Pouliot <[email protected]>
  27. //
  28. using System;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.Security.Permissions;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Drawing.Imaging {
  34. [TestFixture]
  35. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  36. public class ImageAttributesTest {
  37. static ColorMatrix global_color_matrix = new ColorMatrix (new float[][] {
  38. new float[] {2, 0, 0, 0, 0}, //R
  39. new float[] {0, 1, 0, 0, 0}, //G
  40. new float[] {0, 0, 1, 0, 0}, //B
  41. new float[] {0, 0, 0, 1, 0}, //A
  42. new float[] {0.2f, 0, 0, 0, 0}, //Translation
  43. });
  44. static ColorMatrix global_gray_matrix = new ColorMatrix (new float[][] {
  45. new float[] {1, 0, 0, 0, 0}, //R
  46. new float[] {0, 2, 0, 0, 0}, //G
  47. new float[] {0, 0, 3, 0, 0}, //B
  48. new float[] {0, 0, 0, 1, 0}, //A
  49. new float[] {0.5f, 0, 0, 0, 0}, //Translation
  50. });
  51. private static Color ProcessColorMatrix (Color color, ColorMatrix colorMatrix)
  52. {
  53. using (Bitmap bmp = new Bitmap (64, 64)) {
  54. using (Graphics gr = Graphics.FromImage (bmp)) {
  55. ImageAttributes imageAttr = new ImageAttributes ();
  56. bmp.SetPixel (0,0, color);
  57. imageAttr.SetColorMatrix (colorMatrix);
  58. gr.DrawImage (bmp, new Rectangle (0, 0, 64, 64), 0, 0, 64, 64, GraphicsUnit.Pixel, imageAttr);
  59. return bmp.GetPixel (0,0);
  60. }
  61. }
  62. }
  63. // Text Color Matrix processing
  64. [Test]
  65. #if TARGET_JVM
  66. [Category ("NotWorking")]
  67. #endif
  68. public void ColorMatrix1 ()
  69. {
  70. Color clr_src, clr_rslt;
  71. ColorMatrix cm = new ColorMatrix (new float[][] {
  72. new float[] {2, 0, 0, 0, 0}, //R
  73. new float[] {0, 1, 0, 0, 0}, //G
  74. new float[] {0, 0, 1, 0, 0}, //B
  75. new float[] {0, 0, 0, 1, 0}, //A
  76. new float[] {0.2f, 0, 0, 0, 0}, //Translation
  77. });
  78. clr_src = Color.FromArgb (255, 100, 20, 50);
  79. clr_rslt = ProcessColorMatrix (clr_src, cm);
  80. Assert.AreEqual (Color.FromArgb (255, 251, 20, 50), clr_rslt, "Color");
  81. }
  82. [Test]
  83. #if TARGET_JVM
  84. [Category ("NotWorking")]
  85. #endif
  86. public void ColorMatrix2 ()
  87. {
  88. Color clr_src, clr_rslt;
  89. ColorMatrix cm = new ColorMatrix (new float[][] {
  90. new float[] {1, 0, 0, 0, 0}, //R
  91. new float[] {0, 1, 0, 0, 0}, //G
  92. new float[] {0, 0, 1.5f, 0, 0}, //B
  93. new float[] {0, 0, 0.5f, 1, 0}, //A
  94. new float[] {0, 0, 0, 0, 0}, //Translation
  95. });
  96. clr_src = Color.FromArgb (255, 100, 40, 25);
  97. clr_rslt = ProcessColorMatrix (clr_src, cm);
  98. Assert.AreEqual (Color.FromArgb (255, 100, 40, 165), clr_rslt, "Color");
  99. }
  100. private void Bug80323 (Color c)
  101. {
  102. // test case from bug #80323
  103. ColorMatrix cm = new ColorMatrix (new float[][] {
  104. new float[] {1, 0, 0, 0, 0}, //R
  105. new float[] {0, 1, 0, 0, 0}, //G
  106. new float[] {0, 0, 1, 0, 0}, //B
  107. new float[] {0, 0, 0, 0.5f, 0}, //A
  108. new float[] {0, 0, 0, 0, 1}, //Translation
  109. });
  110. using (SolidBrush sb = new SolidBrush (c)) {
  111. using (Bitmap bmp = new Bitmap (100, 100)) {
  112. using (Graphics gr = Graphics.FromImage (bmp)) {
  113. gr.FillRectangle (Brushes.White, 0, 0, 100, 100);
  114. gr.FillEllipse (sb, 0, 0, 100, 100);
  115. }
  116. using (Bitmap b = new Bitmap (200, 100)) {
  117. using (Graphics g = Graphics.FromImage (b)) {
  118. g.FillRectangle (Brushes.White, 0, 0, 200, 100);
  119. ImageAttributes ia = new ImageAttributes ();
  120. ia.SetColorMatrix (cm);
  121. g.DrawImage (bmp, new Rectangle (0, 0, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel, null);
  122. g.DrawImage (bmp, new Rectangle (100, 0, 100, 100), 0, 0, 100, 100, GraphicsUnit.Pixel, ia);
  123. }
  124. b.Save (String.Format ("80323-{0}.png", c.ToArgb ().ToString ("X")));
  125. Assert.AreEqual (Color.FromArgb (255, 255, 155, 155), b.GetPixel (50, 50), "50,50");
  126. Assert.AreEqual (Color.FromArgb (255, 255, 205, 205), b.GetPixel (150, 50), "150,50");
  127. }
  128. }
  129. }
  130. }
  131. [Test]
  132. #if TARGET_JVM
  133. [Category ("NotWorking")]
  134. #endif
  135. public void ColorMatrix_80323_UsingAlpha ()
  136. {
  137. Bug80323 (Color.FromArgb (100, 255, 0, 0));
  138. }
  139. [Test]
  140. #if TARGET_JVM
  141. [Category ("NotWorking")]
  142. #endif
  143. public void ColorMatrix_80323_WithoutAlpha ()
  144. {
  145. // this color is identical, once drawn over the bitmap, to Color.FromArgb (100, 255, 0, 0)
  146. Bug80323 (Color.FromArgb (255, 255, 155, 155));
  147. }
  148. private static Color ProcessColorMatrices (Color color, ColorMatrix colorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags, ColorAdjustType type)
  149. {
  150. using (Bitmap bmp = new Bitmap (64, 64)) {
  151. using (Graphics gr = Graphics.FromImage (bmp)) {
  152. ImageAttributes imageAttr = new ImageAttributes ();
  153. bmp.SetPixel (0, 0, color);
  154. imageAttr.SetColorMatrices (colorMatrix, grayMatrix, flags, type);
  155. gr.DrawImage (bmp, new Rectangle (0, 0, 64, 64), 0, 0, 64, 64, GraphicsUnit.Pixel, imageAttr);
  156. return bmp.GetPixel (0, 0);
  157. }
  158. }
  159. }
  160. [Test]
  161. #if TARGET_JVM
  162. [Ignore ("ImageAttributes is not supported in GH")]
  163. #endif
  164. [ExpectedException (typeof (ArgumentException))]
  165. public void SetColorMatrix_Null ()
  166. {
  167. using (ImageAttributes ia = new ImageAttributes ()) {
  168. ia.SetColorMatrix (null);
  169. }
  170. }
  171. [Test]
  172. public void SetColorMatrix_Default ()
  173. {
  174. using (ImageAttributes ia = new ImageAttributes ()) {
  175. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default);
  176. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  177. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Brush);
  178. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  179. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Pen);
  180. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Text);
  181. }
  182. }
  183. [Test]
  184. #if TARGET_JVM
  185. [Ignore ("ImageAttributes is not supported in GH")]
  186. #endif
  187. [ExpectedException (typeof (ArgumentException))]
  188. public void SetColorMatrix_Default_Any ()
  189. {
  190. using (ImageAttributes ia = new ImageAttributes ()) {
  191. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Any);
  192. }
  193. }
  194. [Test]
  195. #if TARGET_JVM
  196. [Ignore ("ImageAttributes is not supported in GH")]
  197. #endif
  198. [ExpectedException (typeof (ArgumentException))]
  199. public void SetColorMatrix_Default_Count ()
  200. {
  201. using (ImageAttributes ia = new ImageAttributes ()) {
  202. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Count);
  203. }
  204. }
  205. [Test]
  206. #if TARGET_JVM
  207. [Ignore ("ImageAttributes is not supported in GH")]
  208. #endif
  209. [ExpectedException (typeof (ArgumentException))]
  210. public void SetColorMatrix_AltGrays ()
  211. {
  212. using (ImageAttributes ia = new ImageAttributes ()) {
  213. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays);
  214. }
  215. }
  216. [Test]
  217. #if TARGET_JVM
  218. [Ignore ("ImageAttributes is not supported in GH")]
  219. #endif
  220. [ExpectedException (typeof (ArgumentException))]
  221. public void SetColorMatrix_AltGrays_Any ()
  222. {
  223. using (ImageAttributes ia = new ImageAttributes ()) {
  224. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Any);
  225. }
  226. }
  227. [Test]
  228. #if TARGET_JVM
  229. [Ignore ("ImageAttributes is not supported in GH")]
  230. #endif
  231. [ExpectedException (typeof (ArgumentException))]
  232. public void SetColorMatrix_AltGrays_Bitmap ()
  233. {
  234. using (ImageAttributes ia = new ImageAttributes ()) {
  235. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Bitmap);
  236. }
  237. }
  238. [Test]
  239. #if TARGET_JVM
  240. [Ignore ("ImageAttributes is not supported in GH")]
  241. #endif
  242. [ExpectedException (typeof (ArgumentException))]
  243. public void SetColorMatrix_AltGrays_Brush ()
  244. {
  245. using (ImageAttributes ia = new ImageAttributes ()) {
  246. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Brush);
  247. }
  248. }
  249. [Test]
  250. #if TARGET_JVM
  251. [Ignore ("ImageAttributes is not supported in GH")]
  252. #endif
  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. [Test]
  261. #if TARGET_JVM
  262. [Ignore ("ImageAttributes is not supported in GH")]
  263. #endif
  264. [ExpectedException (typeof (ArgumentException))]
  265. public void SetColorMatrix_AltGrays_Default ()
  266. {
  267. using (ImageAttributes ia = new ImageAttributes ()) {
  268. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
  269. }
  270. }
  271. [Test]
  272. #if TARGET_JVM
  273. [Ignore ("ImageAttributes is not supported in GH")]
  274. #endif
  275. [ExpectedException (typeof (ArgumentException))]
  276. public void SetColorMatrix_AltGrays_Pen ()
  277. {
  278. using (ImageAttributes ia = new ImageAttributes ()) {
  279. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Pen);
  280. }
  281. }
  282. [Test]
  283. #if TARGET_JVM
  284. [Ignore ("ImageAttributes is not supported in GH")]
  285. #endif
  286. [ExpectedException (typeof (ArgumentException))]
  287. public void SetColorMatrix_AltGrays_Text ()
  288. {
  289. using (ImageAttributes ia = new ImageAttributes ()) {
  290. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Text);
  291. }
  292. }
  293. [Test]
  294. public void SetColorMatrix_SkipGrays ()
  295. {
  296. using (ImageAttributes ia = new ImageAttributes ()) {
  297. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays);
  298. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Bitmap);
  299. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Brush);
  300. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
  301. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Pen);
  302. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Text);
  303. }
  304. }
  305. [Test]
  306. #if TARGET_JVM
  307. [Ignore ("ImageAttributes is not supported in GH")]
  308. #endif
  309. [ExpectedException (typeof (ArgumentException))]
  310. public void SetColorMatrix_SkipGrays_Any ()
  311. {
  312. using (ImageAttributes ia = new ImageAttributes ()) {
  313. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Any);
  314. }
  315. }
  316. [Test]
  317. #if TARGET_JVM
  318. [Ignore ("ImageAttributes is not supported in GH")]
  319. #endif
  320. [ExpectedException (typeof (ArgumentException))]
  321. public void SetColorMatrix_SkipGrays_Count ()
  322. {
  323. using (ImageAttributes ia = new ImageAttributes ()) {
  324. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Count);
  325. }
  326. }
  327. [Test]
  328. #if TARGET_JVM
  329. [Ignore ("ImageAttributes is not supported in GH")]
  330. #endif
  331. [ExpectedException (typeof (ArgumentException))]
  332. public void SetColorMatrix_InvalidFlag ()
  333. {
  334. using (ImageAttributes ia = new ImageAttributes ()) {
  335. ia.SetColorMatrix (global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
  336. }
  337. }
  338. [Test]
  339. #if TARGET_JVM
  340. [Ignore ("ImageAttributes is not supported in GH")]
  341. #endif
  342. [ExpectedException (typeof (ArgumentException))]
  343. public void SetColorMatrix_InvalidType()
  344. {
  345. using (ImageAttributes ia = new ImageAttributes ()) {
  346. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType)Int32.MinValue);
  347. }
  348. }
  349. [Test]
  350. #if TARGET_JVM
  351. [Ignore ("ImageAttributes is not supported in GH")]
  352. #endif
  353. [ExpectedException (typeof (ArgumentException))]
  354. public void SetColorMatrices_Null_ColorMatrix ()
  355. {
  356. using (ImageAttributes ia = new ImageAttributes ()) {
  357. ia.SetColorMatrices (null, global_color_matrix);
  358. }
  359. }
  360. [Test]
  361. public void SetColorMatrices_ColorMatrix_Null ()
  362. {
  363. using (ImageAttributes ia = new ImageAttributes ()) {
  364. ia.SetColorMatrices (global_color_matrix, null);
  365. ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.Default);
  366. ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.SkipGrays);
  367. }
  368. }
  369. [Test]
  370. #if TARGET_JVM
  371. [Ignore ("ImageAttributes is not supported in GH")]
  372. #endif
  373. [ExpectedException (typeof (ArgumentException))]
  374. public void SetColorMatrices_ColorMatrix_Null_AltGrays ()
  375. {
  376. using (ImageAttributes ia = new ImageAttributes ()) {
  377. ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.AltGrays);
  378. }
  379. }
  380. [Test]
  381. public void SetColorMatrices_ColorMatrix_ColorMatrix ()
  382. {
  383. using (ImageAttributes ia = new ImageAttributes ()) {
  384. ia.SetColorMatrices (global_color_matrix, global_color_matrix);
  385. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default);
  386. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.SkipGrays);
  387. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.AltGrays);
  388. }
  389. }
  390. [Test]
  391. #if TARGET_JVM
  392. [Ignore ("ImageAttributes is not supported in GH")]
  393. #endif
  394. public void SetColorMatrices_Gray ()
  395. {
  396. Color c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  397. Assert.AreEqual (0xFFFF8080, (uint)c.ToArgb (), "Gray|Default|Default");
  398. c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
  399. Assert.AreEqual (0xFF808080, (uint) c.ToArgb (), "Gray|SkipGrays|Default");
  400. c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
  401. Assert.AreEqual (0xFFFFFFFF, (uint) c.ToArgb (), "Gray|AltGrays|Default");
  402. }
  403. [Test]
  404. #if TARGET_JVM
  405. [Ignore ("ImageAttributes is not supported in GH")]
  406. #endif
  407. public void SetColorMatrices_Color ()
  408. {
  409. Color c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  410. Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|Default|Default");
  411. c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
  412. Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|SkipGrays|Default");
  413. c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
  414. Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|AltGrays|Default");
  415. }
  416. [Test]
  417. #if TARGET_JVM
  418. [Ignore ("ImageAttributes is not supported in GH")]
  419. #endif
  420. [ExpectedException (typeof (ArgumentException))]
  421. public void SetColorMatrices_InvalidFlags ()
  422. {
  423. using (ImageAttributes ia = new ImageAttributes ()) {
  424. ia.SetColorMatrices (global_color_matrix, global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
  425. }
  426. }
  427. [Test]
  428. #if TARGET_JVM
  429. [Ignore ("ImageAttributes is not supported in GH")]
  430. #endif
  431. [ExpectedException (typeof (ArgumentException))]
  432. public void SetColorMatrices_InvalidType ()
  433. {
  434. using (ImageAttributes ia = new ImageAttributes ()) {
  435. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType) Int32.MinValue);
  436. }
  437. }
  438. private void Alpha (string prefix, int n, float a)
  439. {
  440. ColorMatrix cm = new ColorMatrix (new float[][] {
  441. new float[] {1, 0, 0, 0, 0}, //R
  442. new float[] {0, 1, 0, 0, 0}, //G
  443. new float[] {0, 0, 1, 0, 0}, //B
  444. new float[] {0, 0, 0, a, 0}, //A
  445. new float[] {0, 0, 0, 0, 1}, //Translation
  446. });
  447. using (Bitmap bmp = new Bitmap (1, 4)) {
  448. bmp.SetPixel (0, 0, Color.White);
  449. bmp.SetPixel (0, 1, Color.Red);
  450. bmp.SetPixel (0, 2, Color.Lime);
  451. bmp.SetPixel (0, 3, Color.Blue);
  452. using (Bitmap b = new Bitmap (1, 4)) {
  453. using (Graphics g = Graphics.FromImage (b)) {
  454. ImageAttributes ia = new ImageAttributes ();
  455. ia.SetColorMatrix (cm);
  456. g.FillRectangle (Brushes.White, new Rectangle (0, 0, 1, 4));
  457. g.DrawImage (bmp, new Rectangle (0, 0, 1, 4), 0, 0, 1, 4, GraphicsUnit.Pixel, ia);
  458. Assert.AreEqual (Color.FromArgb (255, 255, 255, 255), b.GetPixel (0,0), prefix + "-0,0");
  459. int val = 255 - n;
  460. Assert.AreEqual (Color.FromArgb (255, 255, val, val), b.GetPixel (0, 1), prefix + "-0,1");
  461. Assert.AreEqual (Color.FromArgb (255, val, 255, val), b.GetPixel (0, 2), prefix + "-0,2");
  462. Assert.AreEqual (Color.FromArgb (255, val, val, 255), b.GetPixel (0, 3), prefix + "-0,3");
  463. }
  464. }
  465. }
  466. }
  467. [Test]
  468. #if TARGET_JVM
  469. [Ignore ("ImageAttributes is not supported in GH")]
  470. #endif
  471. public void ColorMatrixAlpha ()
  472. {
  473. for (int i = 0; i < 256; i++) {
  474. Alpha (i.ToString (), i, (float) i / 255);
  475. // generally color matrix are specified with values between [0..1]
  476. Alpha ("small-" + i.ToString (), i, (float) i / 255);
  477. // but GDI+ also accept value > 1
  478. Alpha ("big-" + i.ToString (), i, 256 - i);
  479. }
  480. }
  481. }
  482. }