ImageAttributes.cs 13 KB

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