ImageAttributes.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. void Dispose (bool disposing)
  170. {
  171. if (!disposing) return;
  172. Status status = GDIPlus.GdipDisposeImageAttributes(nativeImageAttr);
  173. if (status != Status.Ok)
  174. throw new Exception ("Error calling GDIPlus.GdipDisposeImageAttributes:" +status);
  175. else
  176. nativeImageAttr = IntPtr.Zero;
  177. }
  178. public void Dispose()
  179. {
  180. Dispose (true);
  181. System.GC.SuppressFinalize (this);
  182. }
  183. ~ImageAttributes()
  184. {
  185. Dispose (false);
  186. }
  187. public object Clone()
  188. {
  189. IntPtr imgclone;
  190. Status status = GDIPlus.GdipCloneImageAttributes (nativeImageAttr, out imgclone);
  191. GDIPlus.CheckStatus (status);
  192. return new ImageAttributes (imgclone);
  193. }
  194. public void GetAdjustedPalette(ColorPalette palette, ColorAdjustType type)
  195. {
  196. IntPtr colorPalette;
  197. Status status = GDIPlus.GdipGetImageAttributesAdjustedPalette (nativeImageAttr,
  198. out colorPalette, type);
  199. palette.setFromGDIPalette (colorPalette);
  200. GDIPlus.CheckStatus (status);
  201. }
  202. public void SetBrushRemapTable(ColorMap[] map)
  203. {
  204. GdiColorMap gdiclr = new GdiColorMap ();
  205. IntPtr clrmap, lpPointer;
  206. int mapsize = Marshal.SizeOf (gdiclr);
  207. int size = mapsize * map.Length;
  208. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  209. for (int i=0; i < map.Length; i++)
  210. {
  211. gdiclr.from = map[i].OldColor.ToArgb();
  212. gdiclr.to = map[i].NewColor.ToArgb();
  213. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  214. lpPointer = (IntPtr) (lpPointer.ToInt32() + mapsize);
  215. }
  216. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  217. ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
  218. Marshal.FreeHGlobal (clrmap);
  219. GDIPlus.CheckStatus (status);
  220. }
  221. public void SetColorKey(Color colorLow, Color colorHigh, ColorAdjustType type)
  222. {
  223. Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr,
  224. type, true, colorLow.ToArgb (), colorHigh.ToArgb ());
  225. GDIPlus.CheckStatus (status);
  226. }
  227. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix)
  228. {
  229. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  230. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, ColorMatrixFlag.Default);
  231. GDIPlus.CheckStatus (status);
  232. }
  233. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags)
  234. {
  235. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  236. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, flags);
  237. GDIPlus.CheckStatus (status);
  238. }
  239. public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag mode, ColorAdjustType type)
  240. {
  241. Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
  242. ColorAdjustType.Default, true, newColorMatrix, grayMatrix, mode);
  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,
  252. gamma);
  253. GDIPlus.CheckStatus (status);
  254. }
  255. public void SetNoOp()
  256. {
  257. SetNoOp (ColorAdjustType.Default);
  258. }
  259. public void SetNoOp(ColorAdjustType type)
  260. {
  261. Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr,
  262. type, true);
  263. GDIPlus.CheckStatus (status);
  264. }
  265. public void SetOutputChannel(ColorChannelFlag flags)
  266. {
  267. SetOutputChannel (flags, ColorAdjustType.Default);
  268. }
  269. public void SetOutputChannel(ColorChannelFlag flags, ColorAdjustType type)
  270. {
  271. Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr,
  272. type, true, flags);
  273. GDIPlus.CheckStatus (status);
  274. }
  275. public void SetOutputChannelColorProfile(string colorProfileFilename)
  276. {
  277. SetOutputChannelColorProfile (colorProfileFilename, ColorAdjustType.Default);
  278. }
  279. public void SetOutputChannelColorProfile(string colorProfileFilename, ColorAdjustType type)
  280. {
  281. Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr,
  282. type, true, colorProfileFilename);
  283. GDIPlus.CheckStatus (status);
  284. }
  285. public void SetRemapTable(ColorMap[] map)
  286. {
  287. SetRemapTable (map, ColorAdjustType.Default);
  288. }
  289. public void SetRemapTable(ColorMap[] map, ColorAdjustType type)
  290. {
  291. GdiColorMap gdiclr = new GdiColorMap ();
  292. IntPtr clrmap, lpPointer;
  293. int mapsize = Marshal.SizeOf (gdiclr);
  294. int size = mapsize * map.Length;
  295. clrmap = lpPointer = Marshal.AllocHGlobal (size);
  296. for (int i=0; i < map.Length; i++)
  297. {
  298. gdiclr.from = map[i].OldColor.ToArgb();
  299. gdiclr.to = map[i].NewColor.ToArgb();
  300. Marshal.StructureToPtr (gdiclr, lpPointer, false);
  301. lpPointer = (IntPtr) (lpPointer.ToInt32() + mapsize);
  302. }
  303. Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr,
  304. type, true, (uint) map.Length, clrmap);
  305. Marshal.FreeHGlobal (clrmap);
  306. GDIPlus.CheckStatus (status);
  307. }
  308. public void SetThreshold(float threshold)
  309. {
  310. SetThreshold (threshold, ColorAdjustType.Default);
  311. }
  312. public void SetThreshold(float threshold, ColorAdjustType type)
  313. {
  314. Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr,
  315. type, true, 0);
  316. GDIPlus.CheckStatus (status);
  317. }
  318. public void SetWrapMode(WrapMode mode)
  319. {
  320. SetWrapMode (mode, Color.Black);
  321. }
  322. public void SetWrapMode(WrapMode mode, Color color)
  323. {
  324. SetWrapMode (mode, color, false);
  325. }
  326. public void SetWrapMode(WrapMode mode, Color color, bool clamp)
  327. {
  328. Status status = GDIPlus.GdipSetImageAttributesWrapMode (nativeImageAttr, mode,
  329. color.ToArgb(), clamp);
  330. GDIPlus.CheckStatus (status);
  331. }
  332. }
  333. }