2
0

ImageAttributes.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. try {
  206. for (int i=0; i < map.Length; i++) {
  207. gdiclr.from = map[i].OldColor.ToArgb();
  208. gdiclr.to = map[i].NewColor.ToArgb();
  209. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  210. lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);
  211. }
  212. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  213. ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
  214. GDIPlus.CheckStatus (status);
  215. }
  216. finally {
  217. Marshal.FreeHGlobal (clrmap);
  218. }
  219. }
  220. public void SetColorKey(Color colorLow, Color colorHigh, ColorAdjustType type)
  221. {
  222. Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr,
  223. type, true, colorLow.ToArgb (), colorHigh.ToArgb ());
  224. GDIPlus.CheckStatus (status);
  225. }
  226. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix)
  227. {
  228. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  229. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, ColorMatrixFlag.Default);
  230. GDIPlus.CheckStatus (status);
  231. }
  232. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags)
  233. {
  234. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  235. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, flags);
  236. GDIPlus.CheckStatus (status);
  237. }
  238. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag mode, ColorAdjustType type)
  239. {
  240. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  241. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, mode);
  242. GDIPlus.CheckStatus (status);
  243. }
  244. public void SetGamma(float gamma)
  245. {
  246. SetGamma (gamma, ColorAdjustType.Default);
  247. }
  248. public void SetGamma(float gamma, ColorAdjustType coloradjust)
  249. {
  250. Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr, coloradjust, true,
  251. 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,
  261. type, true);
  262. GDIPlus.CheckStatus (status);
  263. }
  264. public void SetOutputChannel(ColorChannelFlag flags)
  265. {
  266. SetOutputChannel (flags, ColorAdjustType.Default);
  267. }
  268. public void SetOutputChannel(ColorChannelFlag flags, ColorAdjustType type)
  269. {
  270. Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr,
  271. type, true, flags);
  272. GDIPlus.CheckStatus (status);
  273. }
  274. public void SetOutputChannelColorProfile(string colorProfileFilename)
  275. {
  276. SetOutputChannelColorProfile (colorProfileFilename, ColorAdjustType.Default);
  277. }
  278. public void SetOutputChannelColorProfile(string colorProfileFilename, ColorAdjustType type)
  279. {
  280. Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr,
  281. type, true, colorProfileFilename);
  282. GDIPlus.CheckStatus (status);
  283. }
  284. public void SetRemapTable(ColorMap[] map)
  285. {
  286. SetRemapTable (map, ColorAdjustType.Default);
  287. }
  288. public void SetRemapTable(ColorMap[] map, ColorAdjustType type)
  289. {
  290. GdiColorMap gdiclr = new GdiColorMap ();
  291. IntPtr clrmap, lpPointer;
  292. int mapsize = Marshal.SizeOf (gdiclr);
  293. int size = mapsize * map.Length;
  294. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  295. try {
  296. for (int i=0; i < map.Length; i++) {
  297. gdiclr.from = map[i].OldColor.ToArgb();
  298. gdiclr.to = map[i].NewColor.ToArgb();
  299. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  300. lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);
  301. }
  302. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  303. type, true, (uint) map.Length, clrmap);
  304. GDIPlus.CheckStatus (status);
  305. }
  306. finally {
  307. Marshal.FreeHGlobal (clrmap);
  308. }
  309. }
  310. public void SetThreshold(float threshold)
  311. {
  312. SetThreshold (threshold, ColorAdjustType.Default);
  313. }
  314. public void SetThreshold(float threshold, ColorAdjustType type)
  315. {
  316. Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr,
  317. type, true, 0);
  318. GDIPlus.CheckStatus (status);
  319. }
  320. public void SetWrapMode(WrapMode mode)
  321. {
  322. SetWrapMode (mode, Color.Black);
  323. }
  324. public void SetWrapMode(WrapMode mode, Color color)
  325. {
  326. SetWrapMode (mode, color, false);
  327. }
  328. public void SetWrapMode(WrapMode mode, Color color, bool clamp)
  329. {
  330. Status status = GDIPlus.GdipSetImageAttributesWrapMode (nativeImageAttr, mode,
  331. color.ToArgb(), clamp);
  332. GDIPlus.CheckStatus (status);
  333. }
  334. }
  335. }