ImageAttributes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // System.Drawing.Imaging.ImageAttributes.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected]) (stubbed out)
  6. // Jordi Mas i Hernàndez ([email protected])
  7. // Sanjay Gupta ([email protected])
  8. // Sebastien Pouliot <[email protected]>
  9. //
  10. // (C) 2002-4 Ximian, Inc. http://www.ximian.com
  11. // Copyright (C) 2004, 2006-2007 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Drawing.Drawing2D;
  33. using System.Runtime.InteropServices;
  34. namespace System.Drawing.Imaging {
  35. [StructLayout(LayoutKind.Sequential)]
  36. public sealed class ImageAttributes : ICloneable, IDisposable {
  37. private IntPtr nativeImageAttr;
  38. internal IntPtr NativeObject {
  39. get {
  40. return nativeImageAttr;
  41. }
  42. }
  43. internal ImageAttributes (IntPtr native)
  44. {
  45. nativeImageAttr = native;
  46. }
  47. public ImageAttributes ()
  48. {
  49. Status status = GDIPlus.GdipCreateImageAttributes (out nativeImageAttr);
  50. GDIPlus.CheckStatus (status);
  51. }
  52. public void ClearBrushRemapTable ()
  53. {
  54. ClearRemapTable (ColorAdjustType.Brush);
  55. }
  56. //Clears the color keys for all GDI+ objects
  57. public void ClearColorKey ()
  58. {
  59. ClearColorKey (ColorAdjustType.Default);
  60. }
  61. public void ClearColorKey (ColorAdjustType type)
  62. {
  63. Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr, type, false, 0, 0);
  64. GDIPlus.CheckStatus (status);
  65. }
  66. public void ClearColorMatrix ()
  67. {
  68. ClearColorMatrix (ColorAdjustType.Default);
  69. }
  70. public void ClearColorMatrix (ColorAdjustType type)
  71. {
  72. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, type, false,
  73. IntPtr.Zero, IntPtr.Zero, ColorMatrixFlag.Default);
  74. GDIPlus.CheckStatus (status);
  75. }
  76. public void ClearGamma ()
  77. {
  78. ClearGamma (ColorAdjustType.Default);
  79. }
  80. public void ClearGamma (ColorAdjustType type)
  81. {
  82. Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr, type, false, 0);
  83. GDIPlus.CheckStatus (status);
  84. }
  85. public void ClearNoOp ()
  86. {
  87. ClearNoOp (ColorAdjustType.Default);
  88. }
  89. public void ClearNoOp (ColorAdjustType type)
  90. {
  91. Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr, type, false);
  92. GDIPlus.CheckStatus (status);
  93. }
  94. public void ClearOutputChannel ()
  95. {
  96. ClearOutputChannel (ColorAdjustType.Default);
  97. }
  98. public void ClearOutputChannel (ColorAdjustType type)
  99. {
  100. Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr, type, false,
  101. ColorChannelFlag.ColorChannelLast);
  102. GDIPlus.CheckStatus (status);
  103. }
  104. public void ClearOutputChannelColorProfile ()
  105. {
  106. ClearOutputChannelColorProfile (ColorAdjustType.Default);
  107. }
  108. public void ClearOutputChannelColorProfile (ColorAdjustType type)
  109. {
  110. Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr, type, false,
  111. null);
  112. GDIPlus.CheckStatus (status);
  113. }
  114. public void ClearRemapTable ()
  115. {
  116. ClearRemapTable (ColorAdjustType.Default);
  117. }
  118. public void ClearRemapTable (ColorAdjustType type)
  119. {
  120. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, type, false, 0, IntPtr.Zero);
  121. GDIPlus.CheckStatus (status);
  122. }
  123. public void ClearThreshold ()
  124. {
  125. ClearThreshold (ColorAdjustType.Default);
  126. }
  127. public void ClearThreshold (ColorAdjustType type)
  128. {
  129. Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr, type, false, 0);
  130. GDIPlus.CheckStatus (status);
  131. }
  132. //Sets the color keys for all GDI+ objects
  133. public void SetColorKey (Color colorLow, Color colorHigh)
  134. {
  135. SetColorKey (colorLow, colorHigh, ColorAdjustType.Default);
  136. }
  137. public void SetColorMatrix (ColorMatrix colorMatrix)
  138. {
  139. SetColorMatrix (colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  140. }
  141. public void SetColorMatrix (ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag)
  142. {
  143. SetColorMatrix (colorMatrix, colorMatrixFlag, ColorAdjustType.Default);
  144. }
  145. public void SetColorMatrix (ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag, ColorAdjustType colorAdjustType)
  146. {
  147. IntPtr cm = ColorMatrix.Alloc (colorMatrix);
  148. try {
  149. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  150. colorAdjustType, true, cm, IntPtr.Zero, colorMatrixFlag);
  151. GDIPlus.CheckStatus (status);
  152. }
  153. finally {
  154. ColorMatrix.Free (cm);
  155. }
  156. }
  157. public void Dispose ()
  158. {
  159. if (nativeImageAttr != IntPtr.Zero) {
  160. Status status = GDIPlus.GdipDisposeImageAttributes (nativeImageAttr);
  161. nativeImageAttr = IntPtr.Zero;
  162. GDIPlus.CheckStatus (status);
  163. }
  164. System.GC.SuppressFinalize (this);
  165. }
  166. ~ImageAttributes ()
  167. {
  168. Dispose ();
  169. }
  170. public object Clone ()
  171. {
  172. IntPtr imgclone;
  173. Status status = GDIPlus.GdipCloneImageAttributes (nativeImageAttr, out imgclone);
  174. GDIPlus.CheckStatus (status);
  175. return new ImageAttributes (imgclone);
  176. }
  177. [MonoTODO ("Not supported by libgdiplus")]
  178. public void GetAdjustedPalette (ColorPalette palette, ColorAdjustType type)
  179. {
  180. IntPtr colorPalette;
  181. Status status = GDIPlus.GdipGetImageAttributesAdjustedPalette (nativeImageAttr, out colorPalette, type);
  182. palette.setFromGDIPalette (colorPalette);
  183. GDIPlus.CheckStatus (status);
  184. }
  185. public void SetBrushRemapTable (ColorMap[] map)
  186. {
  187. GdiColorMap gdiclr = new GdiColorMap ();
  188. IntPtr clrmap, lpPointer;
  189. int mapsize = Marshal.SizeOf (gdiclr);
  190. int size = mapsize * map.Length;
  191. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  192. try {
  193. for (int i=0; i < map.Length; i++) {
  194. gdiclr.from = map[i].OldColor.ToArgb();
  195. gdiclr.to = map[i].NewColor.ToArgb();
  196. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  197. lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);
  198. }
  199. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  200. ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
  201. GDIPlus.CheckStatus (status);
  202. }
  203. finally {
  204. Marshal.FreeHGlobal (clrmap);
  205. }
  206. }
  207. public void SetColorKey (Color colorLow, Color colorHigh, ColorAdjustType type)
  208. {
  209. Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr, type, true,
  210. colorLow.ToArgb (), colorHigh.ToArgb ());
  211. GDIPlus.CheckStatus (status);
  212. }
  213. public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix)
  214. {
  215. SetColorMatrices (newColorMatrix, grayMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
  216. }
  217. public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags)
  218. {
  219. SetColorMatrices (newColorMatrix, grayMatrix, flags, ColorAdjustType.Default);
  220. }
  221. public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag mode, ColorAdjustType type)
  222. {
  223. Status status;
  224. IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
  225. try {
  226. if (grayMatrix == null) {
  227. status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, type, true, cm,
  228. IntPtr.Zero, mode);
  229. } else {
  230. IntPtr gm = ColorMatrix.Alloc (grayMatrix);
  231. try {
  232. status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, type, true,
  233. cm, gm, mode);
  234. }
  235. finally {
  236. ColorMatrix.Free (gm);
  237. }
  238. }
  239. }
  240. finally {
  241. ColorMatrix.Free (cm);
  242. }
  243. GDIPlus.CheckStatus (status);
  244. }
  245. public void SetGamma (float gamma)
  246. {
  247. SetGamma (gamma, ColorAdjustType.Default);
  248. }
  249. public void SetGamma (float gamma, ColorAdjustType coloradjust)
  250. {
  251. Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr, coloradjust, true, gamma);
  252. GDIPlus.CheckStatus (status);
  253. }
  254. public void SetNoOp ()
  255. {
  256. SetNoOp (ColorAdjustType.Default);
  257. }
  258. public void SetNoOp (ColorAdjustType type)
  259. {
  260. Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr, type, true);
  261. GDIPlus.CheckStatus (status);
  262. }
  263. [MonoTODO ("Not supported by libgdiplus")]
  264. public void SetOutputChannel (ColorChannelFlag flags)
  265. {
  266. SetOutputChannel (flags, ColorAdjustType.Default);
  267. }
  268. [MonoTODO ("Not supported by libgdiplus")]
  269. public void SetOutputChannel (ColorChannelFlag flags, ColorAdjustType type)
  270. {
  271. Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr, type, true, flags);
  272. GDIPlus.CheckStatus (status);
  273. }
  274. [MonoTODO ("Not supported by libgdiplus")]
  275. public void SetOutputChannelColorProfile (string colorProfileFilename)
  276. {
  277. SetOutputChannelColorProfile (colorProfileFilename, ColorAdjustType.Default);
  278. }
  279. [MonoTODO ("Not supported by libgdiplus")]
  280. public void SetOutputChannelColorProfile (string colorProfileFilename, ColorAdjustType type)
  281. {
  282. Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr,
  283. type, true, colorProfileFilename);
  284. GDIPlus.CheckStatus (status);
  285. }
  286. public void SetRemapTable (ColorMap[] map)
  287. {
  288. SetRemapTable (map, ColorAdjustType.Default);
  289. }
  290. public void SetRemapTable (ColorMap[] map, ColorAdjustType type)
  291. {
  292. GdiColorMap gdiclr = new GdiColorMap ();
  293. IntPtr clrmap, lpPointer;
  294. int mapsize = Marshal.SizeOf (gdiclr);
  295. int size = mapsize * map.Length;
  296. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  297. try {
  298. for (int i=0; i < map.Length; i++) {
  299. gdiclr.from = map[i].OldColor.ToArgb();
  300. gdiclr.to = map[i].NewColor.ToArgb();
  301. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  302. lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);
  303. }
  304. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  305. type, true, (uint) map.Length, clrmap);
  306. GDIPlus.CheckStatus (status);
  307. }
  308. finally {
  309. Marshal.FreeHGlobal (clrmap);
  310. }
  311. }
  312. [MonoTODO ("Not supported by libgdiplus")]
  313. public void SetThreshold (float threshold)
  314. {
  315. SetThreshold (threshold, ColorAdjustType.Default);
  316. }
  317. [MonoTODO ("Not supported by libgdiplus")]
  318. public void SetThreshold (float threshold, ColorAdjustType type)
  319. {
  320. Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr, type, true, 0);
  321. GDIPlus.CheckStatus (status);
  322. }
  323. public void SetWrapMode (WrapMode mode)
  324. {
  325. SetWrapMode (mode, Color.Black);
  326. }
  327. public void SetWrapMode (WrapMode mode, Color color)
  328. {
  329. SetWrapMode (mode, color, false);
  330. }
  331. public void SetWrapMode (WrapMode mode, Color color, bool clamp)
  332. {
  333. Status status = GDIPlus.GdipSetImageAttributesWrapMode (nativeImageAttr, mode, color.ToArgb (), clamp);
  334. GDIPlus.CheckStatus (status);
  335. }
  336. }
  337. }