New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing.Imaging / ImageAttributes.cs
1 //
2 // System.Drawing.Imaging.ImageAttributes.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@raytek.com) (stubbed out)
6 //      Jordi Mas i Hernàndez (jmas@softcatala.org)
7 //      Sanjay Gupta (gsanjay@novell.com)
8 //      Sebastien Pouliot  <sebastien@ximian.com>
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
33 using System.Drawing.Drawing2D;
34 using System.Runtime.InteropServices;
35
36 namespace System.Drawing.Imaging {
37
38         [StructLayout(LayoutKind.Sequential)]
39         public sealed class ImageAttributes : ICloneable, IDisposable {
40                 
41                 private IntPtr nativeImageAttr = IntPtr.Zero;
42                 
43                 internal IntPtr NativeObject{
44                         get{
45                                 return nativeImageAttr;
46                         }
47                 }
48                 
49                 internal  ImageAttributes(IntPtr native)
50                 {
51                         nativeImageAttr = native;
52                 }
53                 
54                 public ImageAttributes() {                              
55                         
56                         Status status = GDIPlus.GdipCreateImageAttributes(out nativeImageAttr);
57                                                 
58                         if (status != Status.Ok)
59                                 throw new Exception ("Error calling GDIPlus.GdipCreateImageAttributes:" +status);
60                         
61                 }
62
63                 public void ClearBrushRemapTable()
64                 {
65                         ClearRemapTable (ColorAdjustType.Brush);                        
66                 }
67
68                 //Clears the color keys for all GDI+ objects
69                 public void ClearColorKey() 
70                 {                                        
71                         ClearColorKey (ColorAdjustType.Default);
72                 }
73
74                 public void ClearColorKey(ColorAdjustType type)
75                 {
76                         Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr, 
77                                 type, false, 0, 0);
78                         
79                         GDIPlus.CheckStatus (status);
80                 }
81
82                 public void ClearColorMatrix()
83                 {
84                         ClearColorMatrix (ColorAdjustType.Default);
85                 }
86                 
87                 public void ClearColorMatrix (ColorAdjustType type)
88                 {
89                         Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, type, false, 
90                                 IntPtr.Zero, IntPtr.Zero, ColorMatrixFlag.Default);
91                         GDIPlus.CheckStatus (status);
92                 }
93                 
94                 public void ClearGamma()
95                 {
96                         ClearGamma (ColorAdjustType.Default);
97                 }
98                 
99                 public void ClearGamma(ColorAdjustType type)
100                 {
101                         Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr,   type, false, 0);
102                         
103                         GDIPlus.CheckStatus (status);
104                 }
105                 
106                 public void ClearNoOp()
107                 {
108                         ClearNoOp (ColorAdjustType.Default);
109                 }
110                 
111                 public void ClearNoOp(ColorAdjustType type)
112                 {
113                         Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr, type, false);
114                         
115                         GDIPlus.CheckStatus (status);
116                 }
117
118                 public void ClearOutputChannel()
119                 {
120                         ClearOutputChannel (ColorAdjustType.Default);
121                 }
122                 
123                 public void ClearOutputChannel(ColorAdjustType type)
124                 {
125                         Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr,   
126                                 type, false, ColorChannelFlag.ColorChannelLast);
127                         
128                         GDIPlus.CheckStatus (status);
129                 }
130
131                 public void ClearOutputChannelColorProfile()
132                 {
133                         ClearOutputChannelColorProfile (ColorAdjustType.Default);
134                 }
135                 
136                 public void ClearOutputChannelColorProfile(ColorAdjustType type)
137                 {
138                         Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr, 
139                                 type, false, null);
140                         
141                         GDIPlus.CheckStatus (status);
142                 }
143
144                 public void ClearRemapTable()
145                 {
146                         ClearRemapTable (ColorAdjustType.Default);
147                 }
148                 
149                 public void ClearRemapTable(ColorAdjustType type)
150                 {
151                         Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
152                                 type, false, 0, IntPtr.Zero);
153                         
154                         GDIPlus.CheckStatus (status);
155                 }
156
157                 public void ClearThreshold()
158                 {
159                         ClearThreshold (ColorAdjustType.Default);
160                 }
161                 
162                 public void ClearThreshold(ColorAdjustType type)
163                 {
164                         Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr,
165                                 type, false, 0);
166                         
167                         GDIPlus.CheckStatus (status);
168                 }
169
170                 //Sets the color keys for all GDI+ objects
171                 public void SetColorKey(Color colorLow, Color colorHigh)
172                 {
173                         SetColorKey (colorLow, colorHigh, ColorAdjustType.Default);
174                 }
175
176                 public void SetColorMatrix (ColorMatrix colorMatrix) 
177                 {
178                         IntPtr cm = ColorMatrix.Alloc (colorMatrix);
179                         try {
180                                 Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
181                                         ColorAdjustType.Default, true, cm, IntPtr.Zero, ColorMatrixFlag.Default);
182                                 GDIPlus.CheckStatus (status);
183                         }
184                         finally {
185                                 ColorMatrix.Free (cm);
186                         }
187                 }
188
189                 [MonoTODO ("colorMatrixFlag is ignored in libgdiplus")]
190                 public void SetColorMatrix (ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag) 
191                 {
192                         IntPtr cm = ColorMatrix.Alloc (colorMatrix);
193                         try {
194                                 Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
195                                         ColorAdjustType.Default, true, cm, IntPtr.Zero, colorMatrixFlag);
196                                 GDIPlus.CheckStatus (status);
197                         }
198                         finally {
199                                 ColorMatrix.Free (cm);
200                         }
201                 }
202
203                 [MonoTODO ("colorMatrixFlag is ignored in libgdiplus")]
204                 public void SetColorMatrix (ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag, ColorAdjustType colorAdjustType)
205                 {
206                         IntPtr cm = ColorMatrix.Alloc (colorMatrix);
207                         try {
208                                 Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
209                                         colorAdjustType, true, cm, IntPtr.Zero, colorMatrixFlag);
210                                 GDIPlus.CheckStatus (status);
211                         }
212                         finally {
213                                 ColorMatrix.Free (cm);
214                         }
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                         try {
263                                 for (int i=0; i < map.Length; i++) {
264                                         gdiclr.from = map[i].OldColor.ToArgb();
265                                         gdiclr.to = map[i].NewColor.ToArgb();
266                                 
267                                         Marshal.StructureToPtr (gdiclr, lpPointer, false);
268                                         lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);                                           
269                                 }
270                         
271                                 Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
272                                         ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
273                                 GDIPlus.CheckStatus (status);
274                         }
275                         finally {               
276                                 Marshal.FreeHGlobal (clrmap);   
277                         }
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                 [MonoTODO ("grayMatrix is ignored in libgdiplus")]
291                 public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix)
292                 {
293                         IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
294                         try {
295                                 IntPtr gm = ColorMatrix.Alloc (grayMatrix);
296                                 try {
297                                         Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
298                                                 ColorAdjustType.Default, true, cm, gm, ColorMatrixFlag.Default);
299                                         GDIPlus.CheckStatus (status);
300                                 }
301                                 finally {
302                                         ColorMatrix.Free (gm);
303                                 }
304                         }
305                         finally {
306                                 ColorMatrix.Free (cm);
307                         }
308                 }               
309                 
310                 [MonoTODO ("grayMatrix and colorMatrixFlag are ignored in libgdiplus")]
311                 public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags)
312                 {
313                         IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
314                         try {
315                                 IntPtr gm = ColorMatrix.Alloc (grayMatrix);
316                                 try {
317                                         Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
318                                                 ColorAdjustType.Default, true, cm, gm, flags);
319                                         GDIPlus.CheckStatus (status);
320                                 }
321                                 finally {
322                                         ColorMatrix.Free (gm);
323                                 }
324                         }
325                         finally {
326                                 ColorMatrix.Free (cm);
327                         }
328                 }
329                 
330                 [MonoTODO ("grayMatrix and colorMatrixFlag are ignored in libgdiplus")]
331                 public void SetColorMatrices(ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag mode, ColorAdjustType type)
332                 {
333                         IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
334                         try {
335                                 IntPtr gm = ColorMatrix.Alloc (grayMatrix);
336                                 try {
337                                         Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
338                                                 ColorAdjustType.Default, true, cm, gm, mode);
339                                         GDIPlus.CheckStatus (status);
340                                 }
341                                 finally {
342                                         ColorMatrix.Free (gm);
343                                 }
344                         }
345                         finally {
346                                 ColorMatrix.Free (cm);
347                         }
348                 }               
349
350                 public void SetGamma(float gamma)
351                 {
352                         SetGamma (gamma, ColorAdjustType.Default);                      
353                 }               
354                 
355                 public void SetGamma(float gamma, ColorAdjustType coloradjust)
356                 {
357                         Status status = GDIPlus.GdipSetImageAttributesGamma (nativeImageAttr, coloradjust, true, 
358                                 gamma);
359                         
360                         GDIPlus.CheckStatus (status);                                           
361                 }       
362                 
363                 public void SetNoOp()
364                 {
365                         SetNoOp (ColorAdjustType.Default);                                              
366                 }                       
367                 
368                 public void SetNoOp(ColorAdjustType type)
369                 {
370                         Status status = GDIPlus.GdipSetImageAttributesNoOp (nativeImageAttr, 
371                                 type, true);
372                         
373                         GDIPlus.CheckStatus (status);                   
374                 }                                       
375                 
376                 public void SetOutputChannel(ColorChannelFlag flags)
377                 {
378                         SetOutputChannel (flags, ColorAdjustType.Default);              
379                 }
380                 
381                 public void SetOutputChannel(ColorChannelFlag flags,  ColorAdjustType type)
382                 {
383                         Status status = GDIPlus.GdipSetImageAttributesOutputChannel (nativeImageAttr,   
384                                 type, true, flags);
385                                 
386                         GDIPlus.CheckStatus (status);                   
387                 }               
388                 
389                 public void SetOutputChannelColorProfile(string colorProfileFilename)
390                 {
391                         SetOutputChannelColorProfile (colorProfileFilename, ColorAdjustType.Default);                   
392                 }               
393                 
394                 public void SetOutputChannelColorProfile(string colorProfileFilename,  ColorAdjustType type)
395                 {
396                         Status status = GDIPlus.GdipSetImageAttributesOutputChannelColorProfile (nativeImageAttr, 
397                                 type, true, colorProfileFilename);
398                                 
399                         GDIPlus.CheckStatus (status);                                                                                           
400                 }               
401                 
402                 public void SetRemapTable(ColorMap[] map)
403                 {                       
404                         SetRemapTable (map, ColorAdjustType.Default);
405                 }
406
407                 
408                 public void SetRemapTable(ColorMap[] map, ColorAdjustType type)
409                 {
410                         GdiColorMap gdiclr = new GdiColorMap ();                                                
411                         IntPtr clrmap, lpPointer;
412                         int mapsize = Marshal.SizeOf (gdiclr); 
413                         int size =  mapsize * map.Length;                       
414                         clrmap = lpPointer =  Marshal.AllocHGlobal (size);      
415                         try {
416                                 for (int i=0; i < map.Length; i++) {
417                                         gdiclr.from = map[i].OldColor.ToArgb();
418                                         gdiclr.to = map[i].NewColor.ToArgb();
419                                 
420                                         Marshal.StructureToPtr (gdiclr, lpPointer, false);
421                                         lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);                                           
422                                 }
423                         
424                                 Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
425                                         type, true, (uint) map.Length, clrmap);
426                                 GDIPlus.CheckStatus (status);
427                         }
428                         finally {
429                                 Marshal.FreeHGlobal (clrmap);
430                         }
431                 }               
432                 
433                 public void SetThreshold(float threshold)
434                 {
435                         SetThreshold (threshold, ColorAdjustType.Default);
436                 }               
437                 
438                 public void SetThreshold(float threshold, ColorAdjustType type)
439                 {
440                         Status status = GDIPlus.GdipSetImageAttributesThreshold (nativeImageAttr,
441                                 type, true, 0);
442                         
443                         GDIPlus.CheckStatus (status);                   
444                 }               
445
446                 public void SetWrapMode(WrapMode mode)
447                 {
448                         SetWrapMode (mode, Color.Black);
449                 }
450                 
451                 
452                 public void SetWrapMode(WrapMode mode,  Color color)
453                 {
454                         SetWrapMode (mode,  color, false);
455                 }
456                 
457                 
458                 public void SetWrapMode(WrapMode mode,  Color color, bool clamp)
459                 {
460                         Status status = GDIPlus.GdipSetImageAttributesWrapMode (nativeImageAttr,  mode,
461                                 color.ToArgb(), clamp); 
462                         
463                         GDIPlus.CheckStatus (status);                   
464                 }               
465         }
466 }