Skip to content
Browse files

We can now start some tests, next step: generate the master reference…

… files to compare against
  • Loading branch information...
1 parent 878bda3 commit e9d2ad5bb95f47860986e803dd4bea5a06dd9f54 @migueldeicaza migueldeicaza committed Apr 22, 2012
Showing with 51 additions and 17 deletions.
  1. +32 −13 System.Drawing/Bitmap.cs
  2. +13 −1 System.Drawing/Graphics.cs
  3. +5 −2 System.Drawing/Image.cs
  4. +1 −1 sysdrawing-coregraphics.sln
View
45 System.Drawing/Bitmap.cs
@@ -38,13 +38,29 @@
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.ComponentModel;
+
using MonoTouch.CoreGraphics;
+using MonoTouch.UIKit;
namespace System.Drawing {
[Serializable]
public sealed class Bitmap : Image {
- CGImage image;
+ // if null, we created the bitmap in memory, otherwise, the backing file.
+ UIImage uiimage;
+ internal CGImage NativeCGImage;
+ internal IntPtr bitmapBlock;
+
+ public Bitmap (string filename)
+ {
+ //
+ // This is a quick hack: we should use ImageIO to load the data into
+ // memory, so we always have the bitmapBlock availabe
+ //
+ var uiimage = UIImage.FromFileUncached (filename);
+ NativeCGImage = uiimage.CGImage;
+ }
+
public Bitmap (Stream stream, bool useIcm)
{
// false: stream is owned by user code
@@ -67,48 +83,51 @@ public Bitmap (Stream stream, bool useIcm)
public Bitmap (int width, int height, PixelFormat format)
{
+ int bitsPerComponent, bytesPerRow;
CGColorSpace colorSpace;
+ CGBitmapFlags bitmapInfo;
bool premultiplied = false;
- int bitsPerComponent = 0;
int bitsPerPixel = 0;
- CGBitmapFlags info;
switch (format){
case PixelFormat.Format32bppPArgb:
premultiplied = true;
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
- info = CGBitmapFlags.PremultipliedFirst;
+ bitmapInfo = CGBitmapFlags.PremultipliedFirst;
break;
case PixelFormat.Format32bppArgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
- info = CGBitmapFlags.First;
+ bitmapInfo = CGBitmapFlags.PremultipliedFirst;
break;
case PixelFormat.Format32bppRgb:
colorSpace = CGColorSpace.CreateDeviceRGB ();
bitsPerComponent = 8;
bitsPerPixel = 32;
- info = CGBitmapFlags.None;
+ bitmapInfo = CGBitmapFlags.None;
break;
default:
throw new Exception ("Format not supported: " + format);
}
- int size = width * bitsPerPixel/bitsPerComponent * height;
- IntPtr block = Marshal.AllocHGlobal (size);
- var provider = new CGDataProvider (block, size, true);
- image = new CGImage (width, height, bitsPerComponent, bitsPerPixel, width * bitsPerPixel/bitsPerComponent, colorSpace, info, provider, null, false, CGColorRenderingIntent.Default);
+ bytesPerRow = width * bitsPerPixel/bitsPerComponent;
+ int size = bytesPerRow * height;
+ bitmapBlock = Marshal.AllocHGlobal (size);
+
+ var provider = new CGDataProvider (bitmapBlock, size, true);
+ NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, CGColorRenderingIntent.Default);
}
protected override void Dispose (bool disposing)
{
if (disposing){
- if (image != null){
- image.Dispose ();
- image = null;
+ if (NativeCGImage != null){
+ NativeCGImage.Dispose ();
+ NativeCGImage = null;
}
+ bitmapBlock = IntPtr.Zero;
}
base.Dispose (disposing);
}
View
14 System.Drawing/Graphics.cs
@@ -19,6 +19,7 @@
using MonoMac.CoreGraphics;
#else
using MonoTouch.CoreGraphics;
+using MonoTouch.UIKit;
#endif
namespace System.Drawing {
@@ -645,7 +646,18 @@ public static Graphics FromImage (Image image)
if ((image.PixelFormat & PixelFormat.Indexed) != 0)
throw new Exception ("Cannot create Graphics from an indexed bitmap.");
- return null; // TODO
+ Bitmap b = image as Bitmap;
+ if (b == null)
+ throw new Exception ("Can not create Graphics contexts from " + image.GetType () + " Images, only Bitmaps are supported");
+ var cgimage = b.NativeCGImage;
+
+ if (b.bitmapBlock == IntPtr.Zero){
+ throw new Exception ("Missing functionality: currently we can not create graphics contexts from bitmaps loaded from disk, need to do some extra work");
+ }
+
+ // Creates a context using the parameters that were used initially for the bitmap,
+ // reusing the memory address space on it as well.
+ return new Graphics (new CGBitmapContext (b.bitmapBlock, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo));
}
public void SetClip (RectangleF rect)
View
7 System.Drawing/Image.cs
@@ -57,7 +57,8 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public int Height {
get {
- // TODO
+ var b = this as Bitmap;
+ return b == null ? 0 : b.NativeCGImage.Height;
return 0;
}
}
@@ -81,7 +82,9 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public int Width {
get {
- // TODO
+ var b = this as Bitmap;
+ return b == null ? 0 : b.NativeCGImage.Width;
+
return 0;
}
}
View
2 sysdrawing-coregraphics.sln
@@ -41,6 +41,6 @@ Global
{F6AEFFC5-4807-49A3-83C5-661E655CB59A}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
- StartupItem = sample\sample.csproj
+ StartupItem = Tests\Tests.csproj
EndGlobalSection
EndGlobal

0 comments on commit e9d2ad5

Please sign in to comment.
Something went wrong with that request. Please try again.