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