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. public sealed class ImageAttributes : ICloneable, IDisposable {
  43. private IntPtr nativeImageAttr = IntPtr.Zero;
  44. internal IntPtr NativeObject{
  45. get{
  46. return nativeImageAttr;
  47. }
  48. }
  49. internal ImageAttributes(IntPtr native)
  50. {
  51. nativeImageAttr = native;
  52. }
  53. public ImageAttributes() {
  54. Status status = GDIPlus.GdipCreateImageAttributes(out nativeImageAttr);
  55. if (status != Status.Ok)
  56. throw new Exception ("Error calling GDIPlus.GdipCreateImageAttributes:" +status);
  57. }
  58. public void ClearBrushRemapTable()
  59. {
  60. ClearRemapTable (ColorAdjustType.Brush);
  61. }
  62. //Clears the color keys for all GDI+ objects
  63. public void ClearColorKey()
  64. {
  65. ClearColorKey (ColorAdjustType.Default);
  66. }
  67. public void ClearColorKey(ColorAdjustType type)
  68. {
  69. Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr,
  70. type, false, 0, 0);
  71. GDIPlus.CheckStatus (status);
  72. }
  73. public void ClearColorMatrix()
  74. {
  75. ClearColorMatrix (ColorAdjustType.Default);
  76. }
  77. public void ClearColorMatrix(ColorAdjustType type)
  78. {
  79. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  80. type, false, null, null, ColorMatrixFlag.Default);
  81. GDIPlus.CheckStatus (status);
  82. }
  83. public void ClearGamma()
  84. {
  85. ClearGamma (ColorAdjustType.Default);
  86. }
  87. public void ClearGamma(ColorAdjustType type)
  88. {
  89. Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr, type, false, 0);
  90. GDIPlus.CheckStatus (status);
  91. }
  92. public void ClearNoOp()
  93. {
  94. ClearNoOp (ColorAdjustType.Default);
  95. }
  96. public void ClearNoOp(ColorAdjustType type)
  97. {
  98. Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr, type, false);
  99. GDIPlus.CheckStatus (status);
  100. }
  101. public void ClearOutputChannel()
  102. {
  103. ClearOutputChannel (ColorAdjustType.Default);
  104. }
  105. public void ClearOutputChannel(ColorAdjustType type)
  106. {
  107. Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr,
  108. type, false, ColorChannelFlag.ColorChannelLast);
  109. GDIPlus.CheckStatus (status);
  110. }
  111. public void ClearOutputChannelColorProfile()
  112. {
  113. ClearOutputChannelColorProfile (ColorAdjustType.Default);
  114. }
  115. public void ClearOutputChannelColorProfile(ColorAdjustType type)
  116. {
  117. Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr,
  118. type, false, null);
  119. GDIPlus.CheckStatus (status);
  120. }
  121. public void ClearRemapTable()
  122. {
  123. ClearRemapTable (ColorAdjustType.Default);
  124. }
  125. public void ClearRemapTable(ColorAdjustType type)
  126. {
  127. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  128. type, false, 0, IntPtr.Zero);
  129. GDIPlus.CheckStatus (status);
  130. }
  131. public void ClearThreshold()
  132. {
  133. ClearThreshold (ColorAdjustType.Default);
  134. }
  135. public void ClearThreshold(ColorAdjustType type)
  136. {
  137. Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr,
  138. type, false, 0);
  139. GDIPlus.CheckStatus (status);
  140. }
  141. //Sets the color keys for all GDI+ objects
  142. public void SetColorKey(Color colorLow, Color colorHigh)
  143. {
  144. Status status = GDIPlus.GdipSetImageAttributesColorKeys(nativeImageAttr,
  145. ColorAdjustType.Default, true, colorLow.ToArgb(), colorHigh.ToArgb());
  146. if (status != Status.Ok)
  147. throw new Exception ("Error calling GDIPlus.GdipSetImageAttributesColorKeys:" +status);
  148. }
  149. public void SetColorMatrix(ColorMatrix colorMatrix)
  150. {
  151. Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr, ColorAdjustType.Default,
  152. true, colorMatrix, (ColorMatrix)null, ColorMatrixFlag.Default);
  153. if (status != Status.Ok)
  154. throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);
  155. }
  156. public void SetColorMatrix(ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag)
  157. {
  158. Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr, ColorAdjustType.Default,
  159. true, colorMatrix, (ColorMatrix)null, colorMatrixFlag);
  160. if (status != Status.Ok)
  161. throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);
  162. }
  163. public void SetColorMatrix(ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag, ColorAdjustType colorAdjustType) {
  164. Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr,colorAdjustType,
  165. true, colorMatrix, (ColorMatrix)null, colorMatrixFlag);
  166. if (status != Status.Ok)
  167. throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);
  168. }
  169. public void Dispose()
  170. {
  171. if (nativeImageAttr != IntPtr.Zero) {
  172. Status status = GDIPlus.GdipDisposeImageAttributes(nativeImageAttr);
  173. GDIPlus.CheckStatus (status);
  174. nativeImageAttr = IntPtr.Zero;
  175. }
  176. System.GC.SuppressFinalize (this);
  177. }
  178. ~ImageAttributes()
  179. {
  180. Dispose ();
  181. }
  182. public object Clone()
  183. {
  184. IntPtr imgclone;
  185. Status status = GDIPlus.GdipCloneImageAttributes (nativeImageAttr, out imgclone);
  186. GDIPlus.CheckStatus (status);
  187. return new ImageAttributes (imgclone);
  188. }
  189. public void GetAdjustedPalette(ColorPalette palette, ColorAdjustType type)
  190. {
  191. IntPtr colorPalette;
  192. Status status = GDIPlus.GdipGetImageAttributesAdjustedPalette (nativeImageAttr,
  193. out colorPalette, type);
  194. palette.setFromGDIPalette (colorPalette);
  195. GDIPlus.CheckStatus (status);
  196. }
  197. public void SetBrushRemapTable(ColorMap[] map)
  198. {
  199. GdiColorMap gdiclr = new GdiColorMap ();
  200. IntPtr clrmap, lpPointer;
  201. int mapsize = Marshal.SizeOf (gdiclr);
  202. int size = mapsize * map.Length;
  203. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  204. for (int i=0; i < map.Length; i++)
  205. {
  206. gdiclr.from = map[i].OldColor.ToArgb();
  207. gdiclr.to = map[i].NewColor.ToArgb();
  208. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  209. lpPointer = (IntPtr) (lpPointer.ToInt32() + mapsize);
  210. }
  211. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  212. ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
  213. Marshal.FreeHGlobal (clrmap);
  214. GDIPlus.CheckStatus (status);
  215. }
  216. public void SetColorKey(Color colorLow, Color colorHigh, ColorAdjustType type)
  217. {
  218. Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr,
  219. type, true, colorLow.ToArgb (), colorHigh.ToArgb ());
  220. GDIPlus.CheckStatus (status);
  221. }
  222. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix)
  223. {
  224. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  225. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, ColorMatrixFlag.Default);
  226. GDIPlus.CheckStatus (status);
  227. }
  228. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags)
  229. {
  230. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  231. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, flags);
  232. GDIPlus.CheckStatus (status);
  233. }
  234. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag mode, ColorAdjustType type)
  235. {
  236. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  237. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, mode);
  238. GDIPlus.CheckStatus (status);
  239. }
  240. public void SetGamma(float gamma)
  241. {
  242. SetGamma (gamma, ColorAdjustType.Default);
  243. }
  244. public void SetGamma(float gamma, ColorAdjustType coloradjust)
  245. {
  246. Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr, coloradjust, true,
  247. gamma);
  248. GDIPlus.CheckStatus (status);
  249. }
  250. public void SetNoOp()
  251. {
  252. SetNoOp (ColorAdjustType.Default);
  253. }
  254. public void SetNoOp(ColorAdjustType type)
  255. {
  256. Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr,
  257. type, true);
  258. GDIPlus.CheckStatus (status);
  259. }
  260. public void SetOutputChannel(ColorChannelFlag flags)
  261. {
  262. SetOutputChannel (flags, ColorAdjustType.Default);
  263. }
  264. public void SetOutputChannel(ColorChannelFlag flags, ColorAdjustType type)
  265. {
  266. Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr,
  267. type, true, flags);
  268. GDIPlus.CheckStatus (status);
  269. }
  270. public void SetOutputChannelColorProfile(string colorProfileFilename)
  271. {
  272. SetOutputChannelColorProfile (colorProfileFilename, ColorAdjustType.Default);
  273. }
  274. public void SetOutputChannelColorProfile(string colorProfileFilename, ColorAdjustType type)
  275. {
  276. Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr,
  277. type, true, colorProfileFilename);
  278. GDIPlus.CheckStatus (status);
  279. }
  280. public void SetRemapTable(ColorMap[] map)
  281. {
  282. SetRemapTable (map, ColorAdjustType.Default);
  283. }
  284. public void SetRemapTable(ColorMap[] map, ColorAdjustType type)
  285. {
  286. GdiColorMap gdiclr = new GdiColorMap ();
  287. IntPtr clrmap, lpPointer;
  288. int mapsize = Marshal.SizeOf (gdiclr);
  289. int size = mapsize * map.Length;
  290. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  291. for (int i=0; i < map.Length; i++)
  292. {
  293. gdiclr.from = map[i].OldColor.ToArgb();
  294. gdiclr.to = map[i].NewColor.ToArgb();
  295. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  296. lpPointer = (IntPtr) (lpPointer.ToInt32() + mapsize);
  297. }
  298. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  299. type, true, (uint) map.Length, clrmap);
  300. Marshal.FreeHGlobal (clrmap);
  301. GDIPlus.CheckStatus (status);
  302. }
  303. public void SetThreshold(float threshold)
  304. {
  305. SetThreshold (threshold, ColorAdjustType.Default);
  306. }
  307. public void SetThreshold(float threshold, ColorAdjustType type)
  308. {
  309. Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr,
  310. type, true, 0);
  311. GDIPlus.CheckStatus (status);
  312. }
  313. public void SetWrapMode(WrapMode mode)
  314. {
  315. SetWrapMode (mode, Color.Black);
  316. }
  317. public void SetWrapMode(WrapMode mode, Color color)
  318. {
  319. SetWrapMode (mode, color, false);
  320. }
  321. public void SetWrapMode(WrapMode mode, Color color, bool clamp)
  322. {
  323. Status status = GDIPlus.GdipSetImageAttributesWrapMode (nativeImageAttr, mode,
  324. color.ToArgb(), clamp);
  325. GDIPlus.CheckStatus (status);
  326. }
  327. }
  328. }