|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Drawing; |
| 4 | +using AppKit; |
| 5 | +using CoreGraphics; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Drawing.Imaging; |
| 8 | + |
| 9 | +namespace System.Drawing |
| 10 | +{ |
| 11 | + public class Bitmap : IDisposable |
| 12 | + { |
| 13 | + public int Width { get; private set; } |
| 14 | + |
| 15 | + public int Height { get; private set; } |
| 16 | + |
| 17 | + public PixelFormat PixelFormat |
| 18 | + { |
| 19 | + get |
| 20 | + { |
| 21 | + return PixelFormat.Format32bppArgb; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private Color[] _colors; |
| 26 | + |
| 27 | + public Bitmap(int width, int height) |
| 28 | + { |
| 29 | + Width = width; |
| 30 | + Height = height; |
| 31 | + _colors = new Color[Width * Height]; |
| 32 | + } |
| 33 | + |
| 34 | + public Color GetPixel(int x, int y) |
| 35 | + { |
| 36 | + return _colors[Width * y + x]; |
| 37 | + } |
| 38 | + |
| 39 | + public void SetPixel(int x, int y, Color c) |
| 40 | + { |
| 41 | + _colors[Width * y + x] = c; |
| 42 | + } |
| 43 | + |
| 44 | + public NSImage ToNSImage() |
| 45 | + { |
| 46 | + return new NSImage(ToCGImage(), new CGSize(Width, Height)); |
| 47 | + } |
| 48 | + |
| 49 | + public Bitmap(NSImage sourceImage) |
| 50 | + { |
| 51 | + Width = (int)sourceImage.CGImage.Width; |
| 52 | + Height = (int)sourceImage.CGImage.Height; |
| 53 | + _colors = new Color[Width * Height]; |
| 54 | + |
| 55 | + var rawData = new byte[Width * Height * 4]; |
| 56 | + var bytesPerPixel = 4; |
| 57 | + var bytesPerRow = bytesPerPixel * Width; |
| 58 | + var bitsPerComponent = 8; |
| 59 | + |
| 60 | + using (var colorSpace = CGColorSpace.CreateDeviceRGB()) |
| 61 | + { |
| 62 | + using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast)) |
| 63 | + { |
| 64 | + context.DrawImage(new CGRect(0, 0, Width, Height), sourceImage.CGImage); |
| 65 | + |
| 66 | + for (int y = 0; y < Height; y++) |
| 67 | + { |
| 68 | + for (int x = 0; x < Width; x++) |
| 69 | + { |
| 70 | + var i = bytesPerRow * y + bytesPerPixel * x; |
| 71 | + byte red = rawData[i + 0]; |
| 72 | + byte green = rawData[i + 1]; |
| 73 | + byte blue = rawData[i + 2]; |
| 74 | + byte alpha = rawData[i + 3]; |
| 75 | + SetPixel(x, y, Color.FromArgb(alpha, red, green, blue)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public CGImage ToCGImage() |
| 83 | + { |
| 84 | + var rawData = new byte[Width * Height * 4]; |
| 85 | + var bytesPerPixel = 4; |
| 86 | + var bytesPerRow = bytesPerPixel * Width; |
| 87 | + var bitsPerComponent = 8; |
| 88 | + using (var colorSpace = CGColorSpace.CreateDeviceRGB()) |
| 89 | + { |
| 90 | + using (var context = new CGBitmapContext(rawData, Width, Height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.ByteOrder32Big | CGBitmapFlags.PremultipliedLast)) |
| 91 | + { |
| 92 | + for (int y = 0; y < Height; y++) |
| 93 | + { |
| 94 | + for (int x = 0; x < Width; x++) |
| 95 | + { |
| 96 | + Color c = GetPixel(x, y); |
| 97 | + var i = bytesPerRow * y + bytesPerPixel * x; |
| 98 | + rawData[i + 0] = c.R; |
| 99 | + rawData[i + 1] = c.G; |
| 100 | + rawData[i + 2] = c.B; |
| 101 | + rawData[i + 3] = c.A; |
| 102 | + } |
| 103 | + } |
| 104 | +// //context.Flush(); |
| 105 | +// |
| 106 | +// context.SetFillColor(new CGColor(1,0,0, 1)); |
| 107 | +// context.FillRect(new CGRect(0,0, 20, 20)); |
| 108 | +// |
| 109 | +// context.SetTextDrawingMode(CGTextDrawingMode.Clip); |
| 110 | +// context.SetFillColor(new CGColor(0,0,0, 1)); |
| 111 | +// context.ShowTextAtPoint(20, 20, "HEJ"); |
| 112 | +// context.SetTextDrawingMode(CGTextDrawingMode.Fill); |
| 113 | +// context.ShowTextAtPoint(30, 30, "Yo"); |
| 114 | + return context.ToImage(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + #region IDisposable implementation |
| 120 | + public void Dispose () |
| 121 | + { |
| 122 | + _colors = null; |
| 123 | + } |
| 124 | + #endregion |
| 125 | + |
| 126 | + public Bitmap Clone (Rectangle rectangle, System.Drawing.Imaging.PixelFormat pixelFormat) |
| 127 | + { |
| 128 | + return null; //TODO: Get section... |
| 129 | + } |
| 130 | + |
| 131 | + public Bitmap Clone () |
| 132 | + { |
| 133 | + return new Bitmap (ToNSImage ()); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | +} |
| 138 | + |
0 commit comments