TestImageAttributes.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. [ExpectedException (typeof (ArgumentException))]
  162. public void SetColorMatrix_Null ()
  163. {
  164. using (ImageAttributes ia = new ImageAttributes ()) {
  165. ia.SetColorMatrix (null);
  166. }
  167. }
  168. [Test]
  169. public void SetColorMatrix_Default ()
  170. {
  171. using (ImageAttributes ia = new ImageAttributes ()) {
  172. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default);
  173. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  174. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Brush);
  175. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  176. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Pen);
  177. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Text);
  178. }
  179. }
  180. [Test]
  181. [ExpectedException (typeof (ArgumentException))]
  182. public void SetColorMatrix_Default_Any ()
  183. {
  184. using (ImageAttributes ia = new ImageAttributes ()) {
  185. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Any);
  186. }
  187. }
  188. [Test]
  189. [ExpectedException (typeof (ArgumentException))]
  190. public void SetColorMatrix_Default_Count ()
  191. {
  192. using (ImageAttributes ia = new ImageAttributes ()) {
  193. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Count);
  194. }
  195. }
  196. [Test]
  197. [ExpectedException (typeof (ArgumentException))]
  198. public void SetColorMatrix_AltGrays ()
  199. {
  200. using (ImageAttributes ia = new ImageAttributes ()) {
  201. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays);
  202. }
  203. }
  204. [Test]
  205. [ExpectedException (typeof (ArgumentException))]
  206. public void SetColorMatrix_AltGrays_Any ()
  207. {
  208. using (ImageAttributes ia = new ImageAttributes ()) {
  209. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Any);
  210. }
  211. }
  212. [Test]
  213. [ExpectedException (typeof (ArgumentException))]
  214. public void SetColorMatrix_AltGrays_Bitmap ()
  215. {
  216. using (ImageAttributes ia = new ImageAttributes ()) {
  217. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Bitmap);
  218. }
  219. }
  220. [Test]
  221. [ExpectedException (typeof (ArgumentException))]
  222. public void SetColorMatrix_AltGrays_Brush ()
  223. {
  224. using (ImageAttributes ia = new ImageAttributes ()) {
  225. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Brush);
  226. }
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentException))]
  230. public void SetColorMatrix_AltGrays_Count ()
  231. {
  232. using (ImageAttributes ia = new ImageAttributes ()) {
  233. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Count);
  234. }
  235. }
  236. [Test]
  237. [ExpectedException (typeof (ArgumentException))]
  238. public void SetColorMatrix_AltGrays_Default ()
  239. {
  240. using (ImageAttributes ia = new ImageAttributes ()) {
  241. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
  242. }
  243. }
  244. [Test]
  245. [ExpectedException (typeof (ArgumentException))]
  246. public void SetColorMatrix_AltGrays_Pen ()
  247. {
  248. using (ImageAttributes ia = new ImageAttributes ()) {
  249. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Pen);
  250. }
  251. }
  252. [Test]
  253. [ExpectedException (typeof (ArgumentException))]
  254. public void SetColorMatrix_AltGrays_Text ()
  255. {
  256. using (ImageAttributes ia = new ImageAttributes ()) {
  257. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Text);
  258. }
  259. }
  260. [Test]
  261. public void SetColorMatrix_SkipGrays ()
  262. {
  263. using (ImageAttributes ia = new ImageAttributes ()) {
  264. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays);
  265. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Bitmap);
  266. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Brush);
  267. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
  268. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Pen);
  269. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Text);
  270. }
  271. }
  272. [Test]
  273. [ExpectedException (typeof (ArgumentException))]
  274. public void SetColorMatrix_SkipGrays_Any ()
  275. {
  276. using (ImageAttributes ia = new ImageAttributes ()) {
  277. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Any);
  278. }
  279. }
  280. [Test]
  281. [ExpectedException (typeof (ArgumentException))]
  282. public void SetColorMatrix_SkipGrays_Count ()
  283. {
  284. using (ImageAttributes ia = new ImageAttributes ()) {
  285. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Count);
  286. }
  287. }
  288. [Test]
  289. [ExpectedException (typeof (ArgumentException))]
  290. public void SetColorMatrix_InvalidFlag ()
  291. {
  292. using (ImageAttributes ia = new ImageAttributes ()) {
  293. ia.SetColorMatrix (global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
  294. }
  295. }
  296. [Test]
  297. [ExpectedException (typeof (ArgumentException))]
  298. public void SetColorMatrix_InvalidType()
  299. {
  300. using (ImageAttributes ia = new ImageAttributes ()) {
  301. ia.SetColorMatrix (global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType)Int32.MinValue);
  302. }
  303. }
  304. [Test]
  305. [ExpectedException (typeof (ArgumentException))]
  306. public void SetColorMatrices_Null_ColorMatrix ()
  307. {
  308. using (ImageAttributes ia = new ImageAttributes ()) {
  309. ia.SetColorMatrices (null, global_color_matrix);
  310. }
  311. }
  312. [Test]
  313. public void SetColorMatrices_ColorMatrix_Null ()
  314. {
  315. using (ImageAttributes ia = new ImageAttributes ()) {
  316. ia.SetColorMatrices (global_color_matrix, null);
  317. ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.Default);
  318. ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.SkipGrays);
  319. }
  320. }
  321. [Test]
  322. [ExpectedException (typeof (ArgumentException))]
  323. public void SetColorMatrices_ColorMatrix_Null_AltGrays ()
  324. {
  325. using (ImageAttributes ia = new ImageAttributes ()) {
  326. ia.SetColorMatrices (global_color_matrix, null, ColorMatrixFlag.AltGrays);
  327. }
  328. }
  329. [Test]
  330. public void SetColorMatrices_ColorMatrix_ColorMatrix ()
  331. {
  332. using (ImageAttributes ia = new ImageAttributes ()) {
  333. ia.SetColorMatrices (global_color_matrix, global_color_matrix);
  334. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default);
  335. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.SkipGrays);
  336. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.AltGrays);
  337. }
  338. }
  339. [Test]
  340. public void SetColorMatrices_Gray ()
  341. {
  342. Color c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  343. Assert.AreEqual (0xFFFF8080, (uint)c.ToArgb (), "Gray|Default|Default");
  344. c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
  345. Assert.AreEqual (0xFF808080, (uint) c.ToArgb (), "Gray|SkipGrays|Default");
  346. c = ProcessColorMatrices (Color.Gray, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
  347. Assert.AreEqual (0xFFFFFFFF, (uint) c.ToArgb (), "Gray|AltGrays|Default");
  348. }
  349. [Test]
  350. public void SetColorMatrices_Color ()
  351. {
  352. Color c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  353. Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|Default|Default");
  354. c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.SkipGrays, ColorAdjustType.Default);
  355. Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|SkipGrays|Default");
  356. c = ProcessColorMatrices (Color.MidnightBlue, global_color_matrix, global_gray_matrix, ColorMatrixFlag.AltGrays, ColorAdjustType.Default);
  357. Assert.AreEqual (0xFF651970, (uint) c.ToArgb (), "Color|AltGrays|Default");
  358. }
  359. [Test]
  360. [ExpectedException (typeof (ArgumentException))]
  361. public void SetColorMatrices_InvalidFlags ()
  362. {
  363. using (ImageAttributes ia = new ImageAttributes ()) {
  364. ia.SetColorMatrices (global_color_matrix, global_color_matrix, (ColorMatrixFlag) Int32.MinValue);
  365. }
  366. }
  367. [Test]
  368. [ExpectedException (typeof (ArgumentException))]
  369. public void SetColorMatrices_InvalidType ()
  370. {
  371. using (ImageAttributes ia = new ImageAttributes ()) {
  372. ia.SetColorMatrices (global_color_matrix, global_color_matrix, ColorMatrixFlag.Default, (ColorAdjustType) Int32.MinValue);
  373. }
  374. }
  375. [Test]
  376. public void BigAlpha ()
  377. {
  378. ColorMatrix cm = new ColorMatrix (new float[][] {
  379. new float[] {1, 0, 0, 0, 0}, //R
  380. new float[] {0, 1, 0, 0, 0}, //G
  381. new float[] {0, 0, 1, 0, 0}, //B
  382. new float[] {0, 0, 0, 253, 0}, //A
  383. new float[] {0, 0, 0, 0, 1}, //Translation
  384. });
  385. using (Bitmap bmp = new Bitmap (1, 1)) {
  386. bmp.SetPixel (0, 0, Color.White);
  387. using (Bitmap b = new Bitmap (1, 1)) {
  388. using (Graphics g = Graphics.FromImage (b)) {
  389. ImageAttributes ia = new ImageAttributes ();
  390. ia.SetColorMatrix (cm);
  391. g.DrawImage (bmp, new Rectangle (0, 0, 1, 1), 0, 0, 1, 1, GraphicsUnit.Pixel, ia);
  392. Assert.AreEqual (Color.FromArgb (3, 255, 255, 255), b.GetPixel (0,0), "0,0");
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }