ImageAttributes.cs 13 KB

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