2006-11-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing.Imaging / ImageAttributes.cs
index 6af0cf013912b116dd8e9fe04cc5f467f4047280..7af13924408e8bde4f6d61b3e09761c6aa0b1705 100644 (file)
@@ -1,16 +1,14 @@
 //
 // System.Drawing.Imaging.ImageAttributes.cs
 //
-// Author:
-//   Dennis Hayes (dennish@raytek.com) (stubbed out)
-//      Jordi Mas i Hernàndez (jmas@softcatala.org)
-//      Sanjay Gupta (gsanjay@novell.com)
+// Authors:
+//     Dennis Hayes (dennish@raytek.com) (stubbed out)
+//     Jordi Mas i Hernàndez (jmas@softcatala.org)
+//     Sanjay Gupta (gsanjay@novell.com)
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002-4 Ximian, Inc.  http://www.ximian.com
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
-using System.Drawing;
 using System.Drawing.Drawing2D;
 using System.Runtime.InteropServices;
 
-namespace System.Drawing.Imaging
-{
-       /// <summary>
-       /// Summary description for ImageAttributes.
-       /// </summary>
+namespace System.Drawing.Imaging {
+
+       [StructLayout(LayoutKind.Sequential)]
        public sealed class ImageAttributes : ICloneable, IDisposable {
                
                private IntPtr nativeImageAttr = IntPtr.Zero;
@@ -82,7 +76,7 @@ namespace System.Drawing.Imaging
                        Status status = GDIPlus.GdipSetImageAttributesColorKeys (nativeImageAttr, 
                                type, false, 0, 0);
                        
-                       GDIPlus.CheckStatus (status);                   
+                       GDIPlus.CheckStatus (status);
                }
 
                public void ClearColorMatrix()
@@ -90,12 +84,11 @@ namespace System.Drawing.Imaging
                        ClearColorMatrix (ColorAdjustType.Default);
                }
                
-               public void ClearColorMatrix(ColorAdjustType type)
+               public void ClearColorMatrix (ColorAdjustType type)
                {
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
-                               type, false, null, null, ColorMatrixFlag.Default);
-                       
-                       GDIPlus.CheckStatus (status);                   
+                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, type, false, 
+                               IntPtr.Zero, IntPtr.Zero, ColorMatrixFlag.Default);
+                       GDIPlus.CheckStatus (status);
                }
                
                public void ClearGamma()
@@ -177,41 +170,48 @@ namespace System.Drawing.Imaging
                //Sets the color keys for all GDI+ objects
                public void SetColorKey(Color colorLow, Color colorHigh)
                {
-                                               
-                       Status status = GDIPlus.GdipSetImageAttributesColorKeys(nativeImageAttr,
-                                       ColorAdjustType.Default, true,  colorLow.ToArgb(), colorHigh.ToArgb());
-
-                       if (status != Status.Ok)
-                               throw new Exception ("Error calling GDIPlus.GdipSetImageAttributesColorKeys:" +status);
+                       SetColorKey (colorLow, colorHigh, ColorAdjustType.Default);
                }
 
-               public void SetColorMatrix(ColorMatrix colorMatrix) 
+               public void SetColorMatrix (ColorMatrix colorMatrix) 
                {
-                       
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr, ColorAdjustType.Default,
-                                            true, colorMatrix, (ColorMatrix)null, ColorMatrixFlag.Default);
-                                            
-                       if (status != Status.Ok)
-                               throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);                                           
+                       IntPtr cm = ColorMatrix.Alloc (colorMatrix);
+                       try {
+                               Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
+                                       ColorAdjustType.Default, true, cm, IntPtr.Zero, ColorMatrixFlag.Default);
+                               GDIPlus.CheckStatus (status);
+                       }
+                       finally {
+                               ColorMatrix.Free (cm);
+                       }
                }
 
-               public void SetColorMatrix(ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag) 
-               {
-                       
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr, ColorAdjustType.Default,
-                                            true, colorMatrix, (ColorMatrix)null, colorMatrixFlag);
-                       if (status != Status.Ok)
-                               throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);                                                                                       
-                                            
+               [MonoTODO ("colorMatrixFlag parameter is ignored in libgdiplus")]
+               public void SetColorMatrix (ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag) 
+               {
+                       IntPtr cm = ColorMatrix.Alloc (colorMatrix);
+                       try {
+                               Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr,
+                                       ColorAdjustType.Default, true, cm, IntPtr.Zero, colorMatrixFlag);
+                               GDIPlus.CheckStatus (status);
+                       }
+                       finally {
+                               ColorMatrix.Free (cm);
+                       }
                }
 
-               public void SetColorMatrix(ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag, ColorAdjustType colorAdjustType) {
-                       
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr,colorAdjustType,
-                                            true,  colorMatrix,  (ColorMatrix)null,  colorMatrixFlag);
-                                            
-                       if (status != Status.Ok)
-                               throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);                                                                                       
+               [MonoTODO ("colorMatrixFlag parameter is ignored in libgdiplus")]
+               public void SetColorMatrix (ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag, ColorAdjustType colorAdjustType)
+               {
+                       IntPtr cm = ColorMatrix.Alloc (colorMatrix);
+                       try {
+                               Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
+                                       colorAdjustType, true, cm, IntPtr.Zero, colorMatrixFlag);
+                               GDIPlus.CheckStatus (status);
+                       }
+                       finally {
+                               ColorMatrix.Free (cm);
+                       }
                }
                
                public void Dispose() 
@@ -259,22 +259,22 @@ namespace System.Drawing.Imaging
                        int mapsize = Marshal.SizeOf (gdiclr); 
                        int size =  mapsize * map.Length;                       
                        clrmap = lpPointer =  Marshal.AllocHGlobal (size);      
-                       
-                       for (int i=0; i < map.Length; i++)
-                       {
-                               gdiclr.from = map[i].OldColor.ToArgb();
-                               gdiclr.to = map[i].NewColor.ToArgb();
+                       try {
+                               for (int i=0; i < map.Length; i++) {
+                                       gdiclr.from = map[i].OldColor.ToArgb();
+                                       gdiclr.to = map[i].NewColor.ToArgb();
                                
-                               Marshal.StructureToPtr (gdiclr, lpPointer, false);                              
-                               lpPointer = (IntPtr) (lpPointer.ToInt32() + mapsize);                                                           
-                       }
+                                       Marshal.StructureToPtr (gdiclr, lpPointer, false);
+                                       lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);                                           
+                               }
                        
-                       Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
-                               ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
-                               
-                       Marshal.FreeHGlobal (clrmap);   
-                       
-                       GDIPlus.CheckStatus (status);                                                                                                           
+                               Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
+                                       ColorAdjustType.Brush, true, (uint) map.Length, clrmap);
+                               GDIPlus.CheckStatus (status);
+                       }
+                       finally {               
+                               Marshal.FreeHGlobal (clrmap);   
+                       }
                }
                
                
@@ -287,28 +287,64 @@ namespace System.Drawing.Imaging
                }
                
                
-               public void SetColorMatrices(ColorMatrix newColorMatrix,  ColorMatrix grayMatrix)
-               {
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
-                               ColorAdjustType.Default, true, newColorMatrix, grayMatrix, ColorMatrixFlag.Default);
-                       
-                       GDIPlus.CheckStatus (status);                   
+               [MonoTODO ("grayMatrix parameter is ignored in libgdiplus")]
+               public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix)
+               {
+                       IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
+                       try {
+                               IntPtr gm = ColorMatrix.Alloc (grayMatrix);
+                               try {
+                                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
+                                               ColorAdjustType.Default, true, cm, gm, ColorMatrixFlag.Default);
+                                       GDIPlus.CheckStatus (status);
+                               }
+                               finally {
+                                       ColorMatrix.Free (gm);
+                               }
+                       }
+                       finally {
+                               ColorMatrix.Free (cm);
+                       }
                }               
                
-               public void SetColorMatrices(ColorMatrix newColorMatrix,  ColorMatrix grayMatrix, ColorMatrixFlag flags)
-               {
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
-                               ColorAdjustType.Default, true, newColorMatrix, grayMatrix, flags);
-                       
-                       GDIPlus.CheckStatus (status);                                           
-               }               
+               [MonoTODO ("grayMatrix and colorMatrixFlag parameters are ignored in libgdiplus")]
+               public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag flags)
+               {
+                       IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
+                       try {
+                               IntPtr gm = ColorMatrix.Alloc (grayMatrix);
+                               try {
+                                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
+                                               ColorAdjustType.Default, true, cm, gm, flags);
+                                       GDIPlus.CheckStatus (status);
+                               }
+                               finally {
+                                       ColorMatrix.Free (gm);
+                               }
+                       }
+                       finally {
+                               ColorMatrix.Free (cm);
+                       }
+               }
                
-               public void SetColorMatrices(ColorMatrix newColorMatrix,  ColorMatrix grayMatrix,  ColorMatrixFlag mode,   ColorAdjustType type)
-               {
-                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
-                               ColorAdjustType.Default, true, newColorMatrix, grayMatrix, mode);
-                       
-                       GDIPlus.CheckStatus (status);                   
+               [MonoTODO ("grayMatrix and colorMatrixFlag parameters are ignored in libgdiplus")]
+               public void SetColorMatrices (ColorMatrix newColorMatrix, ColorMatrix grayMatrix, ColorMatrixFlag mode, ColorAdjustType type)
+               {
+                       IntPtr cm = ColorMatrix.Alloc (newColorMatrix);
+                       try {
+                               IntPtr gm = ColorMatrix.Alloc (grayMatrix);
+                               try {
+                                       Status status = GDIPlus.GdipSetImageAttributesColorMatrix (nativeImageAttr, 
+                                               ColorAdjustType.Default, true, cm, gm, mode);
+                                       GDIPlus.CheckStatus (status);
+                               }
+                               finally {
+                                       ColorMatrix.Free (gm);
+                               }
+                       }
+                       finally {
+                               ColorMatrix.Free (cm);
+                       }
                }               
 
                public void SetGamma(float gamma)
@@ -376,23 +412,22 @@ namespace System.Drawing.Imaging
                        int mapsize = Marshal.SizeOf (gdiclr); 
                        int size =  mapsize * map.Length;                       
                        clrmap = lpPointer =  Marshal.AllocHGlobal (size);      
-                       
-                       for (int i=0; i < map.Length; i++)
-                       {
-                               gdiclr.from = map[i].OldColor.ToArgb();
-                               gdiclr.to = map[i].NewColor.ToArgb();
+                       try {
+                               for (int i=0; i < map.Length; i++) {
+                                       gdiclr.from = map[i].OldColor.ToArgb();
+                                       gdiclr.to = map[i].NewColor.ToArgb();
                                
-                               Marshal.StructureToPtr (gdiclr, lpPointer, false);                              
-                               lpPointer = (IntPtr) (lpPointer.ToInt32() + mapsize);                                                           
-                       }
-                       
-                       Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
-                               type, true, (uint) map.Length, clrmap);
-                               
-                       Marshal.FreeHGlobal (clrmap);   
-                       
-                       GDIPlus.CheckStatus (status);                                                                                                           
+                                       Marshal.StructureToPtr (gdiclr, lpPointer, false);
+                                       lpPointer = (IntPtr) (lpPointer.ToInt64() + mapsize);                                           
+                               }
                        
+                               Status status = GDIPlus.GdipSetImageAttributesRemapTable (nativeImageAttr, 
+                                       type, true, (uint) map.Length, clrmap);
+                               GDIPlus.CheckStatus (status);
+                       }
+                       finally {
+                               Marshal.FreeHGlobal (clrmap);
+                       }
                }               
                
                public void SetThreshold(float threshold)
@@ -427,6 +462,5 @@ namespace System.Drawing.Imaging
                        
                        GDIPlus.CheckStatus (status);                   
                }               
-               
        }
 }