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