2008-05-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / Bitmap.cs
old mode 100755 (executable)
new mode 100644 (file)
index d90890f..799ad32
@@ -13,7 +13,7 @@
 //
 
 //
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 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.IO;
 using System.Drawing.Imaging;
 using System.Runtime.Serialization;
 using System.Runtime.InteropServices;
 using System.ComponentModel;
+using System.Security.Permissions;
 
 namespace System.Drawing
 {
@@ -51,6 +51,14 @@ namespace System.Drawing
        {
                #region constructors
                // constructors
+
+#if NET_2_0
+               // required for XmlSerializer (#323246)
+               private Bitmap ()
+               {
+               }
+#endif
+
                internal Bitmap (IntPtr ptr)
                {
                        nativeObject = ptr;
@@ -58,7 +66,6 @@ namespace System.Drawing
 
                public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
                {
-                       
                }
 
                public Bitmap (int width, int height, Graphics g)
@@ -66,7 +73,7 @@ namespace System.Drawing
                        IntPtr bmp;
                        Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
                        GDIPlus.CheckStatus (s);
-                       nativeObject = (IntPtr)bmp;                                             
+                       nativeObject = bmp;                                             
                }
 
                public Bitmap (int width, int height, PixelFormat format)
@@ -74,7 +81,7 @@ namespace System.Drawing
                        IntPtr bmp;
                        Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, 0, format, IntPtr.Zero, out bmp);
                        GDIPlus.CheckStatus (s);
-                       nativeObject = (IntPtr) bmp;
+                       nativeObject = bmp;
                        
                }
 
@@ -86,43 +93,38 @@ namespace System.Drawing
 
                public Bitmap (Image original, Size newSize)  : this(original, newSize.Width, newSize.Height) {}
                
-               internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
-               {                       
-                       nativeObject = (IntPtr)bmp;                                             
-               }
-               
-               internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
-               {                       
-                       nativeObject = (IntPtr)bmp;                     
-                       
-               }
-
-               void InitFromFile (string filename)
-               {               
-                       IntPtr imagePtr;
-                       Status st = GDIPlus.GdipLoadImageFromFile (filename, out imagePtr);
-                       GDIPlus.CheckStatus (st);
-                       nativeObject = imagePtr;                        
-               }
-
                public Bitmap (Stream stream, bool useIcm)
                {
-                       InitFromStream (stream);
+                       // false: stream is owned by user code
+                       nativeObject = InitFromStream (stream);
                }
 
                public Bitmap (string filename, bool useIcm)
                {
-                       InitFromFile (filename);
+                       IntPtr imagePtr;
+                       Status st;
+
+                       if (useIcm)
+                               st = GDIPlus.GdipCreateBitmapFromFileICM (filename, out imagePtr);
+                       else
+                               st = GDIPlus.GdipCreateBitmapFromFile (filename, out imagePtr);
+
+                       GDIPlus.CheckStatus (st);
+                       nativeObject = imagePtr;
                }
 
                public Bitmap (Type type, string resource)
                {
-                       using (Stream s = type.Assembly.GetManifestResourceStream (type, resource)){
-                               if (s == null)
-                                       throw new FileNotFoundException ("Resource name was not found: `" + resource + "'");
-
-                               InitFromStream (s);
+                       Stream s = type.Assembly.GetManifestResourceStream (type, resource);
+                       if (s == null) {
+                               string msg = Locale.GetText ("Resource '{0}' was not found.", resource);
+                               throw new FileNotFoundException (msg);
                        }
+
+                       nativeObject = InitFromStream (s);
+                       // under Win32 stream is owned by SD/GDI+ code
+                       if (GDIPlus.RunningOnWindows ())
+                               stream = s;
                }
 
                public Bitmap (Image original, int width, int height)  : this(width, height, PixelFormat.Format32bppArgb)
@@ -139,27 +141,13 @@ namespace System.Drawing
                                
                        Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
                        GDIPlus.CheckStatus (status);   
-                       nativeObject = (IntPtr) bmp;                                                                                                            
+                       nativeObject = bmp;                                                                                                             
                }
 
                private Bitmap (SerializationInfo info, StreamingContext context)
+                       : base (info, context)
                {
-                       foreach (SerializationEntry serEnum in info) {
-                               if (String.Compare(serEnum.Name, "Data", true) == 0) {
-                                       byte[] bytes = (byte[]) serEnum.Value;
-               
-                                       if (bytes != null) {
-                                               InitFromStream(new MemoryStream(bytes));
-                                       }
-                               }
-                       }
                }
-               //The below function is not required. Call should resolve to base
-               //Moreover there is a problem with the declaration. Base class function
-               //is not declared as protected to access in descendent class
-               /*private Bitmap (SerializationInfo info, StreamingContext context) : base(info, context)
-               {
-               }*/
 
                #endregion
                // methods
@@ -174,61 +162,66 @@ namespace System.Drawing
                }
 
                public void SetPixel (int x, int y, Color color)
-               {                                                                       
-                       Status s = GDIPlus.GdipBitmapSetPixel(nativeObject, x, y, color.ToArgb());
+               {
+                       Status s = GDIPlus.GdipBitmapSetPixel (nativeObject, x, y, color.ToArgb ());
+                       if (s == Status.InvalidParameter) {
+                               // check is done in case of an error only to avoid another
+                               // unmanaged call for normal (successful) calls
+                               if ((this.PixelFormat & PixelFormat.Indexed) != 0) {
+                                       string msg = Locale.GetText ("SetPixel cannot be called on indexed bitmaps.");
+#if NET_2_0
+                                       throw new InvalidOperationException (msg);
+#else
+                                       throw new Exception (msg);
+#endif
+                               }
+                       }
                        GDIPlus.CheckStatus (s);
                }
 
-               public Bitmap Clone (Rectangle rect,PixelFormat format)
+               public Bitmap Clone (Rectangle rect, PixelFormat format)
                {                               
                        IntPtr bmp;                     
-                       Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
-                               PixelFormat, nativeObject,  out bmp);
-                               
+                       Status status = GDIPlus.GdipCloneBitmapAreaI (rect.X, rect.Y, rect.Width, rect.Height,
+                               format, nativeObject, out bmp);
                        GDIPlus.CheckStatus (status);
-
-                       Bitmap bmpnew = new Bitmap (rect.Width, rect.Height,  PixelFormat, (IntPtr) bmp);
-                               return bmpnew;
+                       return new Bitmap (bmp);
                        }
                
                public Bitmap Clone (RectangleF rect, PixelFormat format)
                {
                        IntPtr bmp;                     
-                       Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Top, rect.Width, rect.Height,
-                               PixelFormat, nativeObject,  out bmp);
+                       Status status = GDIPlus.GdipCloneBitmapArea (rect.X, rect.Y, rect.Width, rect.Height,
+                               format, nativeObject, out bmp);
                        GDIPlus.CheckStatus (status);
-
-                       Bitmap bmpnew = new Bitmap (rect.Width, rect.Height,  PixelFormat, (IntPtr) bmp);
-                       return bmpnew;
+                       return new Bitmap (bmp);
                }
 
-               public static Bitmap FromHicon (IntPtr hicon)   //TODO: Untested
+               public static Bitmap FromHicon (IntPtr hicon)
                {       
                        IntPtr bitmap;  
-                               
                        Status status = GDIPlus.GdipCreateBitmapFromHICON (hicon, out bitmap);
                        GDIPlus.CheckStatus (status);
-
-                       return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap);   // FIXME
+                       return new Bitmap (bitmap);
                }
 
                public static Bitmap FromResource (IntPtr hinstance, string bitmapName) //TODO: Untested
                {
                        IntPtr bitmap;  
-                               
                        Status status = GDIPlus.GdipCreateBitmapFromResource (hinstance, bitmapName, out bitmap);
                        GDIPlus.CheckStatus (status);
-
-                       return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
+                       return new Bitmap (bitmap);
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+               [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public IntPtr GetHbitmap ()
                {
                        return GetHbitmap(Color.Gray);
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+               [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public IntPtr GetHbitmap (Color background)
                {
                        IntPtr HandleBmp;
@@ -240,6 +233,7 @@ namespace System.Drawing
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+               [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public IntPtr GetHicon ()
                {
                        IntPtr HandleIcon;
@@ -253,21 +247,19 @@ namespace System.Drawing
                public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
                {
                        BitmapData result = new BitmapData();
+                       return LockBits (rect, flags, format, result);
+               }
 
-                       if (nativeObject == (IntPtr) 0)
-                               throw new Exception ("nativeObject is null");                   
-                       
-                       IntPtr lfBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(result));
-                       Marshal.StructureToPtr(result, lfBuffer, false);                                                
-               
-                       Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format,  lfBuffer);
-                       
-                       result = (BitmapData) Marshal.PtrToStructure(lfBuffer,  typeof(BitmapData));                                                                                    
-                       Marshal.FreeHGlobal (lfBuffer);                 
+#if NET_2_0
+               public
+#endif
+               BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
+               {
+                       Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format, bitmapData);
                        //NOTE: scan0 points to piece of memory allocated in the unmanaged space
                        GDIPlus.CheckStatus (status);
 
-                       return  result;
+                       return bitmapData;
                }
 
                public void MakeTransparent ()