Procházet zdrojové kódy

serveral methods added, serveral fixes, and test case

svn path=/trunk/mcs/; revision=22390
Jordi Mas i Hernandez před 22 roky
rodič
revize
fe395ca826

+ 20 - 0
mcs/class/System.Drawing/System.Drawing.Imaging/BitmapData.cs

@@ -9,6 +9,7 @@
 
 using System;
 using System.Runtime.InteropServices;
+using System.IO;
 
 namespace System.Drawing.Imaging
 {
@@ -18,6 +19,17 @@ namespace System.Drawing.Imaging
 		internal PixelFormat pixel_format;
 		internal IntPtr address;
 		internal int reserved;
+		private	 bool bAllocated = false;
+		
+		~BitmapData()
+		{			
+			if (bAllocated)
+				Marshal.FreeHGlobal(address);						
+		}
+		
+		internal bool Allocated {
+			set {bAllocated = value;}
+		}
 		
 		public int Height {
 			get {
@@ -41,6 +53,7 @@ namespace System.Drawing.Imaging
 
 		public PixelFormat PixelFormat {
 			get {
+				
 				return pixel_format;
 			}
 
@@ -65,6 +78,13 @@ namespace System.Drawing.Imaging
 			}
 
 			set {
+				
+				if (bAllocated)
+				{
+					Marshal.FreeHGlobal(address);				
+					bAllocated = false;
+				}
+				
 				address = value;
 			}
 		}

+ 40 - 8
mcs/class/System.Drawing/System.Drawing.Imaging/BmpCodec.cs

@@ -3,6 +3,7 @@
 //
 // Author: 
 //    Alexandre Pigolkine ([email protected])
+//	  Jordi Mas i Hernàndez ([email protected]>, 2004
 //    BITMAPINFOHEADER,Decode functions implemented using code/ideas from
 //    CxImage (c)  07/Aug/2001 <[email protected]>
 //
@@ -200,9 +201,7 @@ namespace System.Drawing.Imaging {
 		}
 		
 		internal bool Decode (Image image, Stream stream, BitmapData info)
-		{
-								
-#if false
+		{							
 			if (stream.Length < 14 + BITMAPINFOHEADER_SIZE/* sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)*/)
 				return false;
 			long startPosition = stream.Position;
@@ -231,13 +230,30 @@ namespace System.Drawing.Imaging {
 
 			switch (bmih.biBitCount) {
 			case 24:
+				Console.WriteLine ("BmpCodec: 24 bits bitmap", bmih.biSizeImage);
 				info.PixelFormat = PixelFormat.Format24bppRgb;
 				if (bmfh.bfOffBits != 0L)
 					stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
 
 				if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
-					info.RawImageBytes = new byte[bmih.biSizeImage];
-					stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
+					
+					IntPtr lfBuffer = Marshal.AllocHGlobal(bmih.biSizeImage);	
+					byte[] bt = new byte[info.Stride];					
+					int offset = (info.Height-1) * info.Stride;						
+					int baseadr = lfBuffer.ToInt32();
+										
+					//	DIB are stored upside down. That means that the uppest row which 
+					//	appears on the screen actually is the lowest row stored in the bitmap 					
+					while(offset>=0){									
+						stream.Read(bt, 0, info.Stride); 
+						Marshal.Copy (bt, 0, (IntPtr)( baseadr + offset), info.Stride);						
+						offset -= info.Stride;				
+					}										
+					
+					Console.WriteLine ("BmpCodec: 24 bits bitmap", bmih.biSizeImage);
+					info.Scan0 = lfBuffer;
+					info.Allocated=true;		
+			
 				} else {
 					//
 					// FIXME
@@ -247,11 +263,27 @@ namespace System.Drawing.Imaging {
 				break;
 			case 32:
 				info.PixelFormat = PixelFormat.Format32bppArgb;
+				Console.WriteLine ("BmpCodec: 32 bits bitmap", bmih.biSizeImage);
 				if (bmfh.bfOffBits != 0L)
 					stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
 				if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
-					info.RawImageBytes = new byte[bmih.biSizeImage];
-					stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
+					
+					IntPtr lfBuffer = Marshal.AllocHGlobal(bmih.biSizeImage);	
+					byte[] bt = new byte[info.Stride];					
+					int offset = (info.Height-1) * info.Stride;						
+					int baseadr = lfBuffer.ToInt32();
+										
+					//	DIB are stored upside down. That means that the uppest row which 
+					//	appears on the screen actually is the lowest row stored in the bitmap 					
+					while(offset>=0){									
+						stream.Read(bt, 0, info.Stride); 
+						Marshal.Copy (bt, 0, (IntPtr)( baseadr + offset), info.Stride);						
+						offset -= info.Stride;				
+					}													
+					
+					info.Scan0 = lfBuffer;
+					info.Allocated=true;						
+					
 				} else {
 					//
 					// FIXME
@@ -262,7 +294,7 @@ namespace System.Drawing.Imaging {
 			default:
 				throw new NotImplementedException(String.Format("This format is not yet supported : {0} bpp", bmih.biBitCount));
 			}
-#endif
+
 			return true;
 		}
 

+ 4 - 0
mcs/class/System.Drawing/System.Drawing.Imaging/ChangeLog

@@ -1,3 +1,7 @@
+2004-01-21 Jordi Mas i Hernàdez <[email protected]>
+	* BmpBitmap.cs: fixed encoding and decoding problems
+	* ImageAttributes.cs: implemented
+
 2004-01-19  Ravindra <[email protected]>
 
 	* Encoder.cs: Implemented.

+ 67 - 7
mcs/class/System.Drawing/System.Drawing.Imaging/ImageAttributes.cs

@@ -2,8 +2,9 @@
 // System.Drawing.Imaging.ImageAttributes.cs
 //
 // Author:
-//   Dennis Hayes ([email protected])
-// (C) 2002 Ximian, Inc.  http://www.ximian.com
+//   Dennis Hayes ([email protected]) (stubbed out)
+//	 Jordi Mas i Hernàndez ([email protected])
+// (C) 2002-4 Ximian, Inc.  http://www.ximian.com
 //
 
 using System;
@@ -15,34 +16,93 @@ namespace System.Drawing.Imaging
 	/// Summary description for ImageAttributes.
 	/// </summary>
 	public sealed class ImageAttributes : ICloneable, IDisposable {
-		public ImageAttributes() {
+		
+		private IntPtr nativeImageAttr = IntPtr.Zero;
+		
+		internal IntPtr NativeObject{
+			get{
+				return nativeImageAttr;
+			}
+		}
+		
+		public ImageAttributes() {	
+			
+			
+			Status status = GDIPlus.GdipCreateImageAttributes(out nativeImageAttr);
+						
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.GdipCreateImageAttributes:" +status);
+				
+			Console.WriteLine("ImageAttributes().ImageAttributes()" + nativeImageAttr);
 		}
 
 		//Clears the color keys for all GDI+ objects
-		public void ClearColorKey(){
+		public void ClearColorKey(){			                    
+			
+			Status status = GDIPlus.GdipSetImageAttributesColorKeys(nativeImageAttr,  ColorAdjustType.Default,  false, 0,0);
+			
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.ClearColorKey:" +status);
 		}
 
 		//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);
 		}
 
 		public void SetColorMatrix(ColorMatrix colorMatrix) {
-			throw new NotImplementedException();
+			
+			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);                                           
 		}
 
 		public void SetColorMatrix(ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag) {
-			throw new NotImplementedException();
+			
+			Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr, ColorAdjustType.Default,
+                                            true, colorMatrix, (ColorMatrix)null, colorMatrixFlag);
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);                                                                                       
+                                            
 		}
 
 		public void SetColorMatrix(ColorMatrix colorMatrix, ColorMatrixFlag colorMatrixFlag, ColorAdjustType colorAdjustType) {
-			throw new NotImplementedException();
+			
+			Status status = GDIPlus.GdipSetImageAttributesColorMatrix(nativeImageAttr,colorAdjustType,
+                                            true,  colorMatrix,  (ColorMatrix)null,  colorMatrixFlag);
+                                            
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.SetColorMatrix:" +status);                                                                                       
+		}
+		
+		void Dispose (bool disposing)
+		{
+			if (!disposing) return;
+			
+			Status status = GDIPlus.GdipDisposeImageAttributes(nativeImageAttr);
+			
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.GdipDisposeImageAttributes:" +status);
+			else
+				nativeImageAttr = IntPtr.Zero;
 		}
 
+
 		public void Dispose() {
+			
+			Dispose (true);
+			System.GC.SuppressFinalize (this);
 		}
 
 		~ImageAttributes() {
+			
+			Dispose (false);
 		}
 
 		[MonoTODO]

+ 141 - 30
mcs/class/System.Drawing/System.Drawing/Bitmap.cs

@@ -7,6 +7,7 @@
 //   Alexandre Pigolkine ([email protected])
 //   Christian Meyer ([email protected])
 //   Miguel de Icaza ([email protected])
+//	 Jordi Mas i Hernàdez ([email protected])
 //
 using System;
 using System.IO;
@@ -26,12 +27,14 @@ namespace System.Drawing {
 		// constructors
 		public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
 		{
+			raw_format = ImageFormat.Bmp;
 		}
 
 		public Bitmap (int width, int height, Graphics g)
 		{
+			raw_format = ImageFormat.Bmp;
 			image_size = new Size(width, height);
-			int bmp = 0;
+			IntPtr bmp;
 			Status s = GDIPlus.GdipCreateBitmapFromGraphics (width, height, g.nativeObject, out bmp);
 			if (s != Status.Ok)
 				throw new Exception ("Could not create Bitmap from Graphics: " + s);
@@ -41,6 +44,7 @@ namespace System.Drawing {
 
 		public Bitmap (int width, int height, PixelFormat format)
 		{
+			raw_format = ImageFormat.Bmp;
 			image_size = new Size(width, height);
 			pixel_format = format;
 			int bpp = 32;
@@ -48,7 +52,7 @@ namespace System.Drawing {
 			stride = (stride + 3) & ~3;
 			int bmp_size = stride * height;			
 			
-			int bmp = 0;
+			IntPtr bmp;
 			Status s = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, PixelFormat.Format32bppArgb, IntPtr.Zero, 
 				out bmp);
 				
@@ -66,13 +70,38 @@ namespace System.Drawing {
 
 		public Bitmap (string filename) : this (filename, false) {}
 
-		public Bitmap (Image original, Size newSize)
+		public Bitmap (Image original, Size newSize) 
 		{
+			raw_format = ImageFormat.Bmp;
+			BitmapFromImage(original, newSize);
+		}
+		
+		internal Bitmap (int width, int height, PixelFormat pixel, IntPtr bmp)
+		{
+			image_size = new Size(width, height);			
+			nativeObject = (IntPtr)bmp;
+			pixel_format = pixel;
+			raw_format = ImageFormat.Bmp;
+		}
+		
+		internal Bitmap (float width, float height, PixelFormat pixel, IntPtr bmp)
+		{
+			image_size = new Size((int)width, (int)height);			
+			nativeObject = (IntPtr)bmp;
+			pixel_format = pixel;
+			raw_format = ImageFormat.Bmp;
+		}
+		
+		internal void BitmapFromImage(Image original, Size newSize){
+			
 			if (original is Bitmap) {
+				
+				if (nativeObject!=IntPtr.Zero) Dispose();
+				
 				Bitmap bmpOriginal = (Bitmap) original;
 				image_size = bmpOriginal.Size;
 				pixel_format = bmpOriginal.pixel_format;
-				int bmp = 0;
+				IntPtr bmp;
 				Status s = GDIPlus.GdipCloneBitmapAreaI (0, 0, newSize.Width, newSize.Height, bmpOriginal.PixelFormat, bmpOriginal.nativeObject, out bmp);
 				if (s != Status.Ok)
 					throw new ArgumentException ("Could not allocate the GdiPlus image: " + s);
@@ -85,6 +114,8 @@ namespace System.Drawing {
 
 		void InitFromStream (Stream stream)
 		{
+			Console.WriteLine("Bitmap.InitFromStream");
+			
 			BitmapData bd = Decode (stream);
 			if (bd == null)
 				throw new ArgumentException ("Stream could not be decoded");
@@ -92,8 +123,7 @@ namespace System.Drawing {
 			image_size = new Size (bd.Width, bd.Height);
 			pixel_format = bd.PixelFormat;
 
-			int bmp = 0;
-			//buffer = bd.Scan0;
+			IntPtr bmp;			
 			Console.WriteLine ("Stride: {0} ", bd.Stride);
 			Console.WriteLine ("Scan0: {0:x}" , (long) bd.Scan0);
 			Status s = GDIPlus.GdipCreateBitmapFromScan0 (bd.Width, bd.Height, bd.Stride, bd.PixelFormat, bd.Scan0, out bmp);
@@ -120,14 +150,28 @@ namespace System.Drawing {
 			throw new NotImplementedException ();
 		}
 
-		public Bitmap (Image original, int width, int heigth)
+		public Bitmap (Image original, int width, int heigth) 
 		{
-			throw new NotImplementedException ();
+			Size newSize = new Size();
+			newSize.Height=heigth;
+			newSize.Width=width;
+			
+			BitmapFromImage(original,newSize);
 		}
 
 		public Bitmap (int width, int height, int stride, PixelFormat format, IntPtr scan0)
-		{
-			throw new NotImplementedException ();
+		{						
+			IntPtr bmp;
+			
+			Status status = GDIPlus.GdipCreateBitmapFromScan0 (width, height, stride, format, scan0, out bmp);
+			
+			if (status != Status.Ok)
+				throw new ArgumentException ("Could not allocate the GdiPlus image: " + status);
+				
+			nativeObject = (IntPtr)bmp;			
+			pixel_format = format;
+			raw_format = ImageFormat.Bmp;
+			image_size = new Size(width, height);
 		}
 
 		private Bitmap (SerializationInfo info, StreamingContext context)
@@ -157,38 +201,83 @@ namespace System.Drawing {
 		}
 
 		public Bitmap Clone (Rectangle rect,PixelFormat format)
-		{
-			throw new NotImplementedException ();
-		}
+		{				
+			IntPtr bmp;			
+   			Status status = GDIPlus.GdipCloneBitmapAreaI(rect.X, rect.Top, rect.Width, rect.Height,
+                               PixelFormat, nativeObject,  out bmp);
+                               
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GdipBitmapUnlockBits " +status);		
+
+			Bitmap bmpnew = new Bitmap (rect.Width, rect.Height,  PixelFormat, (IntPtr) bmp);
+       		return bmpnew;
+       	}
 		
 		public Bitmap Clone (RectangleF rect, PixelFormat format)
 		{
-			throw new NotImplementedException ();
+			IntPtr bmp;			
+   			Status status = GDIPlus.GdipCloneBitmapArea(rect.X, rect.Top, rect.Width, rect.Height,
+                               PixelFormat, nativeObject,  out bmp);
+                               
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GdipBitmapUnlockBits " +status);		
+
+			Bitmap bmpnew = new Bitmap (rect.Width, rect.Height,  PixelFormat, (IntPtr) bmp);
+       		return bmpnew;
 		}
 
-		public static Bitmap FromHicon (IntPtr hicon)
-		{
-			throw new NotImplementedException ();
+		public static Bitmap FromHicon (IntPtr hicon)	//TODO: Untested
+		{	
+			IntPtr bitmap;	
+				
+			Status status = GDIPlus.GdipCreateBitmapFromHICON(hicon, out bitmap);
+			    
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GdipCreateBitmapFromHICON " +status);		
+				
+			
+			return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap);	// FIXME
 		}
 
-		public static Bitmap FromResource (IntPtr hinstance, string bitmapName)
+		public static Bitmap FromResource (IntPtr hinstance, string bitmapName)	//TODO: Untested
 		{
-			throw new NotImplementedException ();
+			IntPtr bitmap;	
+				
+			Status status = GDIPlus.GdipCreateBitmapFromResource(hinstance, bitmapName, out bitmap);
+			    
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GdipCreateBitmapFromResource " +status);		
+			
+			return new Bitmap (0,0, PixelFormat.Format32bppArgb, bitmap); // FIXME
 		}
 
 		public IntPtr GetHbitmap ()
 		{
-			throw new NotImplementedException ();
+			return GetHbitmap(Color.Gray);
 		}
 
 		public IntPtr GetHbitmap (Color background)
 		{
-			throw new NotImplementedException ();
+			IntPtr HandleBmp;
+			
+			Status status = GDIPlus.GdipCreateHBITMAPFromBitmap(nativeObject, out HandleBmp, background.ToArgb());
+                               
+			if (status != Status.Ok)
+				throw new Exception ("GdipCreateHBITMAPFromBitmap " +status);				
+				
+			return  HandleBmp;
 		}
 
 		public IntPtr GetHicon ()
 		{
-			throw new NotImplementedException ();
+			IntPtr HandleIcon;
+			
+			Status status = GDIPlus.GdipCreateHICONFromBitmap(nativeObject, out HandleIcon);
+                               
+			if (status != Status.Ok)
+				throw new Exception ("GdipCreateHICONFromBitmap " +status);				
+				
+			return  HandleIcon;			
 		}
 
 		public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format)
@@ -206,7 +295,8 @@ namespace System.Drawing {
 			Status status = GDIPlus.GdipBitmapLockBits (nativeObject, ref rect, flags, format,  lfBuffer);
 			
 			result = (BitmapData) Marshal.PtrToStructure(lfBuffer,  typeof(BitmapData));											
-			Marshal.FreeHGlobal (lfBuffer);
+			Marshal.FreeHGlobal (lfBuffer);			
+			//NOTE: scan0 points to piece of memory allocated in the unmanaged space
 			
 			if (status != Status.Ok)
 				throw new Exception ("Could not lock bits: " + status);
@@ -218,25 +308,46 @@ namespace System.Drawing {
 
 		public void MakeTransparent ()
 		{
-			throw new NotImplementedException ();
+			Color clr = GetPixel(0,0);			
+			MakeTransparent (clr);
 		}
 
 		public void MakeTransparent (Color transparentColor)
-		{
-			throw new NotImplementedException ();
+		{				
+			Bitmap	bmp = new Bitmap(Width, Height, PixelFormat);		
+			Graphics gr = Graphics.FromImage(bmp);
+			Rectangle destRect = new Rectangle(0,0, Width, Height);
+			ImageAttributes imageAttr = new ImageAttributes();			
+						
+			gr.Clear(Color.Transparent);					
+			
+			imageAttr.SetColorKey(transparentColor,	transparentColor);
+
+			gr.DrawImage (this, destRect, 0, 0, Width, Height, 	GraphicsUnit.Pixel, imageAttr);					
+			
+			Size newSize = new Size();
+			newSize.Height=Height;
+			newSize.Width=Width;			
+			BitmapFromImage(bmp,newSize);			
+			
+			gr.Dispose();
+			bmp.Dispose();
 		}
 
 		public void SetResolution (float xDpi, float yDpi)
 		{
-			throw new NotImplementedException ();
+			Status status = GDIPlus.GdipBitmapSetResolution(nativeObject, xDpi, yDpi);
+			
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GdipBitmapSetResolution " +status);		
 		}
 
 		public void UnlockBits (BitmapData bitmap_data)
 		{
-			Status s = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
+			Status status = GDIPlus.GdipBitmapUnlockBits (nativeObject, bitmap_data);
 
-			if (s != Status.Ok)
-				throw new Exception ("Could not unlock bits: " + s);
+			if (status != Status.Ok)
+				throw new Exception ("Error calling GdipBitmapUnlockBits " +status);		
 		}
 
 		// properties

+ 4 - 0
mcs/class/System.Drawing/System.Drawing/ChangeLog

@@ -1,3 +1,6 @@
+2004-01-21  Jordi Mas i Hernàndez
+	* Bitmap.cs: Several new methods added.
+		
 2004-01-21  Duncan Mak  <[email protected]>
 
 	* gdipFunctions.cs (GdipGetPathTypes, GdipGetPathPoints): Add
@@ -24,6 +27,7 @@
 
 	* gdipFunctions.cs: Import functions for GraphicsPath.
 
+>>>>>>> 1.89
 2004-01-14  Ravindra <[email protected]>
                                                                                 
         * SolidBrush.cs: Made SolidBrush to initialize its color

+ 23 - 12
mcs/class/System.Drawing/System.Drawing/Graphics.cs

@@ -18,7 +18,7 @@ namespace System.Drawing
 	[ComVisible(false)]
 	public sealed class Graphics : MarshalByRefObject, IDisposable
 	{
-		internal IntPtr nativeObject;
+		internal IntPtr nativeObject = IntPtr.Zero;
 		
 		public delegate bool EnumerateMetafileProc (EmfPlusRecordType recordType,
 							    int flags,
@@ -57,10 +57,13 @@ namespace System.Drawing
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
+		
 		public void Clear (Color color)
-		{
-			throw new NotImplementedException ();
+		{			
+ 			Status status = GDIPlus.GdipGraphicsClear(nativeObject, color.ToArgb());
+ 			
+ 			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.GdipGraphicsClear:" +status);
 		}
 
 		[MonoTODO]
@@ -260,10 +263,13 @@ namespace System.Drawing
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
+		
 		public void DrawImage (Image image, PointF point)
 		{
-			throw new NotImplementedException ();
+			Status status = GDIPlus.GdipDrawImage(nativeObject, image.NativeObject, point.X, point.Y);
+ 			
+ 			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.DrawImage (Image image, PointF point):" +status);					
 		}
 
 		[MonoTODO]
@@ -403,11 +409,16 @@ namespace System.Drawing
 		{
 			throw new NotImplementedException ();
 		}
-
-		[MonoTODO]
+		
 		public void DrawImage (Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)
-		{
-			throw new NotImplementedException ();
+		{			
+			Status status =  GDIPlus.GdipDrawImageRectRectI(nativeObject, image.NativeObject, //aki
+								destRect.X, destRect.Y, destRect.Width, destRect.Height,
+								srcX, srcY, srcWidth, srcHeight,
+								srcUnit, imageAttr.NativeObject, IntPtr.Zero, 0);
+								 			
+ 			if (status != Status.Ok)
+				throw new Exception ("Error calling GDIPlus.DrawImage(Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)" +status);		
 		}
 
 		[MonoTODO]
@@ -601,8 +612,8 @@ namespace System.Drawing
 			Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, IntPtr.Zero, 
 				ref rc, IntPtr.Zero, brush.nativeObject);
 			
-			if (status != Status.Ok)
-				throw new Exception ("Cannot DrawString (string s, Font font, Brush brush, float x, float y):");
+			if (status != Status.Ok)				
+				throw new Exception ("Error calling GDIPlus.GdipDrawString(string s, Font font, Brush brush, float x, float y):" +s);
 		}
 
 		[MonoTODO]

+ 12 - 1
mcs/class/System.Drawing/System.Drawing/Image.cs

@@ -32,7 +32,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
 	protected PixelFormat pixel_format;
 	protected ColorPalette colorPalette;
 
-	ImageFormat raw_format;
+	protected ImageFormat raw_format;
 	
 	// constructor
 	public Image ()
@@ -44,6 +44,8 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
 	private Image (SerializationInfo info, StreamingContext context)
 	{
 	}
+	
+	
 
 	void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
 	{
@@ -336,6 +338,15 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
 		}
 	}
 	
+	internal IntPtr NativeObject{
+		get{
+			return nativeObject;
+		}
+		set	{
+			nativeObject = value;
+		}
+	}
+	
 	public void Dispose ()
 	{
 		Dispose (true);

+ 56 - 6
mcs/class/System.Drawing/System.Drawing/gdipFunctions.cs

@@ -3,6 +3,7 @@
 //
 // Author: 
 // Alexandre Pigolkine ([email protected])
+// Jordi Mas i Hernàndez ([email protected])
 //
 
 using System;
@@ -129,7 +130,9 @@ namespace System.Drawing {
 		[DllImport ("gdiplus.dll")]
 		static internal extern Status GdipSetRenderingOrigin (IntPtr graphics, int x, int y);
  		[DllImport("gdiplus.dll")]
- 		internal static extern Status GdipCloneBitmapAreaI (int x, int y, int width, int height, PixelFormat format, IntPtr original, out int bitmap);
+ 		internal static extern Status GdipCloneBitmapArea (float x, float y, float width, float height, PixelFormat format, IntPtr original, out IntPtr bitmap);
+ 		[DllImport("gdiplus.dll")]
+ 		internal static extern Status GdipCloneBitmapAreaI (int x, int y, int width, int height, PixelFormat format, IntPtr original, out IntPtr bitmap);
  		[DllImport("gdiplus.dll")]
  		internal static extern Status GdipResetWorldTransform (IntPtr graphics);
  		[DllImport("gdiplus.dll")]
@@ -138,6 +141,9 @@ namespace System.Drawing {
  		internal static extern Status GdipGetWorldTransform (IntPtr graphics, IntPtr matrix);
  		[DllImport("gdiplus.dll")]
  		internal static extern Status GdipScaleWorldTransform (IntPtr graphics, float sx, float sy, MatrixOrder order);
+ 		
+ 		[DllImport("gdiplus.dll")]
+ 		internal static extern Status GdipGraphicsClear(IntPtr graphics, int argb);
 		
 		// Pen functions
 		[DllImport("gdiplus.dll")]
@@ -228,14 +234,18 @@ namespace System.Drawing {
 		
 		// Bitmap functions
 		[DllImport("gdiplus.dll")]
-		internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out int bitmap);
+		internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out IntPtr bmp);
 
 		[DllImport("gdiplus.dll")]
-		internal static extern Status GdipCreateBitmapFromGraphics (int width, int height, IntPtr target, out int bitmap);
+		internal static extern Status GdipCreateBitmapFromGraphics (int width, int height, IntPtr target, out IntPtr bitmap);
 
 		[DllImport("gdiplus.dll")]
 		internal static extern Status GdipBitmapLockBits (IntPtr bmp, ref Rectangle rc, ImageLockMode flags, PixelFormat format, [In, Out] IntPtr bmpData);
 		
+		[DllImport("gdiplus.dll")]
+		internal static extern Status GdipBitmapSetResolution(IntPtr bmp, float xdpi, float ydpi);
+
+		
 		// This an internal GDIPlus Cairo function, not part GDIPlus interface
 		//[DllImport("gdiplus.dll")]
 		//(internal static extern Status ____BitmapLockBits (IntPtr bmp, ref GpRect  rc, ImageLockMode flags, PixelFormat format, ref int width, ref int height, ref int stride, ref int format2, ref int reserved, ref IntPtr scan0);
@@ -255,7 +265,30 @@ namespace System.Drawing {
 		[DllImport("gdiplus.dll")]
 		internal static extern Status GdipDrawImageI( IntPtr graphics, IntPtr image, int x, int y);
 		[DllImport("gdiplus.dll")]
-		internal static extern Status GdipGetImageGraphicsContext( IntPtr image, out int graphics);
+		internal static extern Status GdipGetImageGraphicsContext( IntPtr image, out int graphics);		
+		[DllImport("gdiplus.dll")]
+		internal static extern Status GdipDrawImage(IntPtr graphics, IntPtr image, float x, float y);
+		
+		[DllImport("gdiplus.dll")]
+		internal static extern Status GdipDrawImageRectRectI(IntPtr graphics, IntPtr image,
+							int dstx, int dsty, int dstwidth, int dstheight,
+                       		int srcx, int srcy, int srcwidth, int srcheight,
+                       		GraphicsUnit srcUnit, IntPtr imageattr, IntPtr callback, int callbackData);                      		
+		
+		[DllImport("gdiplus.dll")]		
+		internal static extern Status GdipCreateHBITMAPFromBitmap(IntPtr bmp, out IntPtr HandleBmp, int clrbackground);
+		
+		[DllImport("gdiplus.dll")]
+		internal static extern Status GdipCreateHICONFromBitmap(IntPtr bmp, out IntPtr HandleIcon);
+		
+		[DllImport("gdiplus.dll")]
+		internal static extern Status GdipCreateBitmapFromHICON(IntPtr  hicon,  out IntPtr bitmap);
+		
+		[DllImport("gdiplus.dll")]
+		internal static extern Status GdipCreateBitmapFromResource(IntPtr hInstance,
+                             string lpBitmapName, out IntPtr bitmap);
+
+
 
                 // Matrix functions
                 [DllImport ("gdiplus.dll")]
@@ -393,8 +426,25 @@ namespace System.Drawing {
                 [DllImport ("gdiplus.dll")]                
                 internal static extern Status GdipIsOutlineVisiblePathPoint (IntPtr path, float x, float y, IntPtr graphics, out bool result);
                 [DllImport ("gdiplus.dll")]                
-                internal static extern Status GdipIsOutlineVisiblePathPointI (IntPtr path, int x, int y, IntPtr graphics, out bool result);
-                
+                internal static extern Status GdipIsOutlineVisiblePathPointI (IntPtr path, int x, int y, IntPtr graphics, out bool result); 
+                                             
+				// ImageAttributes
+				[DllImport ("gdiplus.dll")]     
+				internal static extern Status GdipCreateImageAttributes(out IntPtr imageattr);
+				
+				[DllImport ("gdiplus.dll")]     
+				internal static extern Status GdipSetImageAttributesColorKeys(IntPtr imageattr,
+                                ColorAdjustType type, bool enableFlag, int colorLow, int colorHigh);
+                                
+                [DllImport ("gdiplus.dll")]     
+				internal static extern Status GdipDisposeImageAttributes(IntPtr imageattr);
+				
+				[DllImport ("gdiplus.dll")]     
+				internal static extern Status GdipSetImageAttributesColorMatrix(IntPtr imageattr,
+                             ColorAdjustType type, bool enableFlag, ColorMatrix colorMatrix,
+                               ColorMatrix grayMatrix,  ColorMatrixFlag flags);                                
+				
+
 #endregion      
 	}               
 }               

+ 3 - 3
mcs/class/System.Drawing/Test/System.Drawing/BmpPaint.cs

@@ -59,8 +59,8 @@ public class BmpPaint {
 		CreateBitmap ("file.bmp", ImageFormat.Bmp);
 		PaintOnBitmap ("file.bmp", "file-painted.bmp");
 		CreateBitmap ("file.jpg", ImageFormat.Jpeg);
-		PaintOnBitmap ("file.jpg", "file-painted.jpg");
-		CreateBitmap ("file.png", ImageFormat.Png);
-		PaintOnBitmap ("file.png", "file-painted.png");
+		//PaintOnBitmap ("file.jpg", "file-painted.jpg");
+		//CreateBitmap ("file.png", ImageFormat.Png);
+		//PaintOnBitmap ("file.png", "file-painted.png");
 	}
 };

+ 132 - 0
mcs/class/System.Drawing/Test/System.Drawing/TestBitmap.cs

@@ -0,0 +1,132 @@
+//
+// Bitmap class testing unit
+//
+// Author:
+//
+// 	 Jordi Mas i Hernàndez ([email protected]>
+//
+// (C) 2004 Ximian, Inc.  http://www.ximian.com
+//
+using System;
+using System.Drawing;
+using System.Drawing.Imaging;
+using NUnit.Framework;
+
+namespace MonoTests.System.Drawing{
+
+	[TestFixture]	
+	public class BitMapTest : Assertion {
+		
+		[TearDown]
+		public void Clean() {}
+		
+		[SetUp]
+		public void GetReady()		
+		{
+		
+		}
+			
+		[Test]
+		public void TestPixels() 
+		{		
+			// Tests GetSetPixel/SetPixel			
+			Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);											
+			bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));					
+			Color color = bmp.GetPixel(0,0);				
+						
+			AssertEquals (Color.FromArgb(255,128,128,128), color);
+			
+			bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));					
+			Color color2 = bmp.GetPixel(99,99);										
+			AssertEquals (Color.FromArgb(255,255,0,155), color2);			
+		}
+		
+		[Test]
+		public void BitmapLoadAndSave() 
+		{							
+			string sOutFile = "output/linerect.bmp";
+						
+			// Save		
+			Bitmap	bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);						
+			Graphics gr = Graphics.FromImage(bmp);		
+			
+			Pen p = new Pen(Color.Red, 2);
+			gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
+			gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
+			p.Dispose();					
+			bmp.Save(sOutFile, ImageFormat.Bmp);
+			gr.Dispose();
+			bmp.Dispose();							
+			
+			// Load			
+			Bitmap	bmpLoad = new Bitmap(sOutFile);
+			if( bmpLoad == null) 
+				Console.WriteLine("Unable to load "+ sOutFile);						
+			
+			Color color = bmpLoad.GetPixel(10,10);		
+			
+			Console.WriteLine("Color "+ color);			
+			AssertEquals (Color.FromArgb(255,255,0,0), color);											
+		}
+
+		[Test]
+		public void MakeTransparent() 
+		{
+			string sInFile = "bitmaps/maketransparent.bmp";
+			string sOutFile = "output/transparent.bmp";
+						
+			Bitmap	bmp = new Bitmap(sInFile);
+			Console.WriteLine("Bitmap loaded OK", bmp != null);
+					
+			bmp.MakeTransparent();
+			bmp.Save(sOutFile);							
+			
+			Color color = bmp.GetPixel(1,1);							
+			AssertEquals (Color.Black.R, color.R);											
+			AssertEquals (Color.Black.G, color.G);											
+			AssertEquals (Color.Black.B, color.B);										
+		}
+		
+		[Test]
+		public void Clone ()
+		{
+			string sInFile = "bitmaps/almogaver24bits.bmp";
+			string sOutFile = "output/clone24.bmp";			
+			
+			Rectangle rect = new Rectangle(0,0,50,50);						
+			Bitmap	bmp = new Bitmap(sInFile);
+			Console.WriteLine("Bitmap loaded OK", bmp != null);
+			
+			Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);			
+			bmpNew.Save(sOutFile);							
+			
+			Color colororg0 = bmp.GetPixel(0,0);		
+			Color colororg50 = bmp.GetPixel(49,49);					
+			Color colornew0 = bmpNew.GetPixel(0,0);		
+			Color colornew50 = bmpNew.GetPixel(49,49);				
+			
+			AssertEquals (colororg0, colornew0);											
+			AssertEquals (colororg50, colornew50);				
+		}
+		
+		[Test]
+		public void Handles()
+		{
+			// This is a simple test. We should test really if there are valid
+			// handles for Win32/Wine			
+			
+			string sInFile = "bitmaps/almogaver24bits.bmp";
+							
+			Bitmap	bmp = new Bitmap(sInFile);
+			IntPtr HandleBmp = bmp.GetHbitmap();
+			Console.WriteLine("Handles.GetHbitmap()-> " + HandleBmp);
+			Assert(HandleBmp!=IntPtr.Zero);				
+			
+			IntPtr HandleIcon = bmp.GetHicon();
+			Console.WriteLine("Handles.GetHicon()-> " + HandleIcon);
+			Assert(HandleIcon!=IntPtr.Zero);							
+		}
+		
+			
+	}
+}

+ 144 - 145
mcs/class/System.Drawing/Test/System.Drawing/TestPoint.cs

@@ -1,159 +1,158 @@
 // Tests for System.Drawing.Point.cs
 //
 // Author: Mike Kestner ([email protected])
-//
+// 		   Improvements by Jordi Mas i Hernŕndez <[email protected]>
 // Copyright (c) 2001 Ximian, Inc.
 
 using NUnit.Framework;
 using System;
 using System.Drawing;
 
-public class PointTest : TestCase {
-	Point pt1_1;
-	Point pt1_0;
-	Point pt0_1;
-
-	protected override void SetUp ()
-	{
-		pt1_1 = new Point (1, 1);
-		pt1_0 = new Point (1, 0);
-		pt0_1 = new Point (0, 1);
-	}
-
-	public PointTest(String name) : base (name) {}
-
-	public static ITest Suite {
-		get {
-			TestSuite suite = new TestSuite ();
-			suite.AddTest (new PointTest ("EqualsTest"));
-			suite.AddTest (new PointTest ("EqualityOpTest"));
-			suite.AddTest (new PointTest ("InequalityOpTest"));
-			suite.AddTest (new PointTest ("CeilingTest"));
-			suite.AddTest (new PointTest ("RoundTest"));
-			suite.AddTest (new PointTest ("TruncateTest"));
-			suite.AddTest (new PointTest ("NullTest"));
-			suite.AddTest (new PointTest ("AdditionTest"));
-			suite.AddTest (new PointTest ("SubtractionTest"));
-			suite.AddTest (new PointTest ("Point2SizeTest"));
-			suite.AddTest (new PointTest ("Point2PointFTest"));
-			suite.AddTest (new PointTest ("ConstructorTest"));
-			suite.AddTest (new PointTest ("PropertyTest"));
-			suite.AddTest (new PointTest ("OffsetTest"));
-			return suite;
+namespace MonoTests.System.Drawing{
+
+	[TestFixture]	
+	public class PointTest : Assertion {
+		Point pt1_1;
+		Point pt1_0;
+		Point pt0_1;
+	
+		[TearDown]
+		public void Clean() {}
+		
+		[SetUp]
+		public void GetReady()		
+		{
+			pt1_1 = new Point (1, 1);
+			pt1_0 = new Point (1, 0);
+			pt0_1 = new Point (0, 1);
+		}
+				
+	
+		[Test]
+		public void EqualsTest () 
+		{
+			AssertEquals (pt1_1, pt1_1);
+			AssertEquals (pt1_1, new Point (1, 1));
+			Assert (!pt1_1.Equals (pt1_0));
+			Assert (!pt1_1.Equals (pt0_1));
+			Assert (!pt1_0.Equals (pt0_1));
+		}
+		
+		[Test]
+		public void EqualityOpTest () 
+		{
+			Assert (pt1_1 == pt1_1);
+			Assert (pt1_1 == new Point (1, 1));
+			Assert (!(pt1_1 == pt1_0));
+			Assert (!(pt1_1 == pt0_1));
+			Assert (!(pt1_0 == pt0_1));
 		}
-	}
-
-	public void EqualsTest () 
-	{
-		AssertEquals (pt1_1, pt1_1);
-		AssertEquals (pt1_1, new Point (1, 1));
-		Assert (!pt1_1.Equals (pt1_0));
-		Assert (!pt1_1.Equals (pt0_1));
-		Assert (!pt1_0.Equals (pt0_1));
-	}
-
-	public void EqualityOpTest () 
-	{
-		Assert (pt1_1 == pt1_1);
-		Assert (pt1_1 == new Point (1, 1));
-		Assert (!(pt1_1 == pt1_0));
-		Assert (!(pt1_1 == pt0_1));
-		Assert (!(pt1_0 == pt0_1));
-	}
-
-	public void InequalityOpTest () 
-	{
-		Assert (!(pt1_1 != pt1_1));
-		Assert (!(pt1_1 != new Point (1, 1)));
-		Assert (pt1_1 != pt1_0);
-		Assert (pt1_1 != pt0_1);
-		Assert (pt1_0 != pt0_1);
-	}
-
-	public void CeilingTest () 
-	{
-		PointF ptf = new PointF (0.8f, 0.3f);
-		AssertEquals (pt1_1, Point.Ceiling (ptf));
-	}
-
-	public void RoundTest () 
-	{
-		PointF ptf = new PointF (0.8f, 1.3f);
-		AssertEquals (pt1_1, Point.Round (ptf));
-	}
-
-	public void TruncateTest () 
-	{
-		PointF ptf = new PointF (0.8f, 1.3f);
-		AssertEquals (pt0_1, Point.Truncate (ptf));
-	}
-
-	public void NullTest () 
-	{
-		Point pt = new Point (0, 0);
-		AssertEquals (pt, Point.Empty);
-	}
-
-	public void AdditionTest () 
-	{
-		AssertEquals (pt1_1, pt1_0 + new Size (0, 1));
-		AssertEquals (pt1_1, pt0_1 + new Size (1, 0));
-	}
-
-	public void SubtractionTest () 
-	{
-		AssertEquals (pt1_0, pt1_1 - new Size (0, 1));
-		AssertEquals (pt0_1, pt1_1 - new Size (1, 0));
-	}
-
-	public void Point2SizeTest () 
-	{
-		Size sz1 = new Size (1, 1);
-		Size sz2 = (Size) pt1_1;
-
-		AssertEquals (sz1, sz2);
-	}
-
-	public void Point2PointFTest () 
-	{
-		PointF ptf1 = new PointF (1, 1);
-		PointF ptf2 = pt1_1;
-
-		AssertEquals (ptf1, ptf2);
-	}
-
-	public void ConstructorTest () 
-	{
-		int i = (1 << 16) + 1;
-		Size sz = new Size (1, 1);
-		Point pt_i = new Point (i);
-		Point pt_sz = new Point (sz);
-
-		AssertEquals (pt_i, pt_sz);
-		AssertEquals (pt_i, pt1_1);
-		AssertEquals (pt_sz, pt1_1);
-	}
-
-	public void PropertyTest () 
-	{
-		Point pt = new Point (0, 0);
-
-		Assert (pt.IsEmpty);
-		Assert (!pt1_1.IsEmpty);
-		AssertEquals (1, pt1_0.X);
-		AssertEquals (1, pt0_1.Y);
-	}
 
-	public void OffsetTest () 
-	{
-		Point pt = new Point (0, 0);
-		pt.Offset (0, 1);
-		AssertEquals (pt, pt0_1);
-		pt.Offset (1, 0);
-		AssertEquals (pt, pt1_1);
-		pt.Offset (0, -1);
-		AssertEquals (pt, pt1_0);
+		[Test]
+		public void InequalityOpTest () 
+		{
+			Assert (!(pt1_1 != pt1_1));
+			Assert (!(pt1_1 != new Point (1, 1)));
+			Assert (pt1_1 != pt1_0);
+			Assert (pt1_1 != pt0_1);
+			Assert (pt1_0 != pt0_1);
+		}
+	
+		[Test]
+		public void CeilingTest () 
+		{
+			PointF ptf = new PointF (0.8f, 0.3f);
+			AssertEquals (pt1_1, Point.Ceiling (ptf));
+		}
+	
+		[Test]
+		public void RoundTest () 
+		{
+			PointF ptf = new PointF (0.8f, 1.3f);
+			AssertEquals (pt1_1, Point.Round (ptf));
+		}
+	
+		[Test]
+		public void TruncateTest () 
+		{
+			PointF ptf = new PointF (0.8f, 1.3f);
+			AssertEquals (pt0_1, Point.Truncate (ptf));
+		}
+	
+		[Test]
+		public void NullTest () 
+		{
+			Point pt = new Point (0, 0);
+			AssertEquals (pt, Point.Empty);
+		}
+	
+		[Test]
+		public void AdditionTest () 
+		{
+			AssertEquals (pt1_1, pt1_0 + new Size (0, 1));
+			AssertEquals (pt1_1, pt0_1 + new Size (1, 0));
+		}
+	
+		[Test]
+		public void SubtractionTest () 
+		{
+			AssertEquals (pt1_0, pt1_1 - new Size (0, 1));
+			AssertEquals (pt0_1, pt1_1 - new Size (1, 0));
+		}
+	
+		[Test]
+		public void Point2SizeTest () 
+		{
+			Size sz1 = new Size (1, 1);
+			Size sz2 = (Size) pt1_1;
+	
+			AssertEquals (sz1, sz2);
+		}
+	
+		[Test]
+		public void Point2PointFTest () 
+		{
+			PointF ptf1 = new PointF (1, 1);
+			PointF ptf2 = pt1_1;
+	
+			AssertEquals (ptf1, ptf2);
+		}
+	
+		[Test]
+		public void ConstructorTest () 
+		{
+			int i = (1 << 16) + 1;
+			Size sz = new Size (1, 1);
+			Point pt_i = new Point (i);
+			Point pt_sz = new Point (sz);
+	
+			AssertEquals (pt_i, pt_sz);
+			AssertEquals (pt_i, pt1_1);
+			AssertEquals (pt_sz, pt1_1);
+		}
+		
+		[Test]
+		public void PropertyTest () 
+		{
+			Point pt = new Point (0, 0);
+	
+			Assert (pt.IsEmpty);
+			Assert (!pt1_1.IsEmpty);
+			AssertEquals (1, pt1_0.X);
+			AssertEquals (1, pt0_1.Y);
+		}
+		
+		[Test]
+		public void OffsetTest () 
+		{
+			Point pt = new Point (0, 0);
+			pt.Offset (0, 1);
+			AssertEquals (pt, pt0_1);
+			pt.Offset (1, 0);
+			AssertEquals (pt, pt1_1);
+			pt.Offset (0, -1);
+			AssertEquals (pt, pt1_0);
+		}
 	}
 }
 
-

binární
mcs/class/System.Drawing/Test/System.Drawing/bitmaps/almogaver24bits.bmp


binární
mcs/class/System.Drawing/Test/System.Drawing/bitmaps/maketransparent.bmp