void GetImagaDataFromPath(string path) { int width, height; NSImage src; CGImage image; CGContext context = null; RectangleF rect = RectangleF.Empty; data = new byte[TEXTURE_WIDTH * TEXTURE_HEIGHT * 4]; src = new NSImage(path); image = src.AsCGImage(ref rect, null, null); width = image.Width; height = image.Height; CGImageAlphaInfo ai = CGImageAlphaInfo.PremultipliedLast; context = new CGBitmapContext(data, width, height, 8, 4 * width, image.ColorSpace, ai); // Core Graphics referential is upside-down compared to OpenGL referential // Flip the Core Graphics context here // An alternative is to use flipped OpenGL texture coordinates when drawing textures context.TranslateCTM(0, height); context.ScaleCTM(1, -1); // Set the blend mode to copy before drawing since the previous contents of memory aren't used. // This avoids unnecessary blending. context.SetBlendMode(CGBlendMode.Copy); context.DrawImage(new RectangleF(0, 0, width, height), image); }
public CGImage(int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo alphaInfo, CGDataProvider provider, nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent) { if (width < 0) { throw new ArgumentException("width"); } if (height < 0) { throw new ArgumentException("height"); } if (bitsPerPixel < 0) { throw new ArgumentException("bitsPerPixel"); } if (bitsPerComponent < 0) { throw new ArgumentException("bitsPerComponent"); } if (bytesPerRow < 0) { throw new ArgumentException("bytesPerRow"); } handle = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace == null ? IntPtr.Zero : colorSpace.Handle, (CGBitmapFlags)alphaInfo, provider == null ? IntPtr.Zero : provider.Handle, decode, shouldInterpolate, intent); }
private CGImage CreateRGBImageFromBufferData(int mByteWidth, int mWidth, int mHeight) { CGColorSpace cgColorSpace = CGColorSpace.CreateDeviceRGB(); CGImageAlphaInfo alphaInfo = (CGImageAlphaInfo)((int)CGImageAlphaInfo.PremultipliedLast | (int)CGBitmapFlags.ByteOrderDefault); CGBitmapContext bitmap; byte[] mData = GetImageData(0); try { unsafe { fixed(byte *ptr = mData) { bitmap = new CGBitmapContext((IntPtr)ptr, mWidth, mHeight, 8, mByteWidth, cgColorSpace, alphaInfo); } } } catch { } CGImage image = bitmap.ToImage(); return(image); }
public static UIImage Scale(UIImage image, int maxSize) { UIImage res = null; using (CGImage imageRef = image.CGImage) { CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo; CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB(); if (alphaInfo == CGImageAlphaInfo.None) { alphaInfo = CGImageAlphaInfo.NoneSkipLast; } nfloat width = imageRef.Width; nfloat height = imageRef.Height; if (height >= width) { width = (nfloat)Math.Floor((double)width * ((double)maxSize / (double)height)); height = maxSize; } else { height = (nfloat)Math.Floor((double)height * ((double)maxSize / (double)width)); width = maxSize; } CGBitmapContext bitmap = new CGBitmapContext(IntPtr.Zero, (nint)width, (nint)height, imageRef.BitsPerComponent, 0, colorSpaceInfo, alphaInfo); bitmap.DrawImage(new CGRect(0, 0, width, height), imageRef); res = UIImage.FromImage(bitmap.ToImage()); bitmap = null; } return(res); }
//======================================================================== #endregion //======================================================================== public override void ViewDidLoad() { base.ViewDidLoad(); // no data IntPtr data = IntPtr.Zero; // size SizeF bitmapSize = new SizeF(200, 300); //this.View.Frame.Size; // 32bit RGB (8bits * 4components (aRGB) = 32bit) int bitsPerComponent = 8; // 4bytes for each pixel (32 bits = 4bytes) int bytesPerRow = (int)(4 * bitmapSize.Width); // no special color space CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); // aRGB CGImageAlphaInfo alphaType = CGImageAlphaInfo.PremultipliedFirst; using (CGBitmapContext context = new CGBitmapContext(data , (int)bitmapSize.Width, (int)bitmapSize.Height, bitsPerComponent , bytesPerRow, colorSpace, alphaType)) { //---- draw } }
void GetImagaDataFromPath(string path) { NSImage src; CGImage image; CGContext context = null; src = new NSImage(path); image = src.AsCGImage(RectangleF.Empty, null, null); width = image.Width; height = image.Height; int bytesPerRow = image.BytesPerRow; int bitsPerPixel = image.BitsPerPixel; int bitsPerComponent = image.BitsPerComponent; data = new byte[bytesPerRow * height * 4]; CGImageAlphaInfo ai = CGImageAlphaInfo.PremultipliedLast; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); context = new CGBitmapContext(data, width, height, 8, 4 * width, colorSpace, ai); // Core Graphics referential is upside-down compared to OpenGL referential // Flip the Core Graphics context here // An alternative is to use flipped OpenGL texture coordinates when drawing textures context.TranslateCTM(0, height); context.ScaleCTM(1, -1); // Set the blend mode to copy before drawing since the previous contents of memory aren't used. // This avoids unnecessary blending. context.SetBlendMode(CGBlendMode.Copy); context.DrawImage(new RectangleF(0, 0, width, height), image); }
int[] GetBitmapPixels(NSData data, CGImageAlphaInfo alphaInfo) { var adjustOrder = 2; if (alphaInfo == CGImageAlphaInfo.First || alphaInfo == CGImageAlphaInfo.PremultipliedFirst) { adjustOrder = 0; } var bytesPerPixel = _image.CGImage.BitsPerPixel / 8; // Order by ARGB var pixels = new int[Width * Height]; var idx = 0; for (var i = 0; i < Height; i++) { for (var j = 0; j < Width; j++) { var addr = (i * Width + j) * bytesPerPixel; pixels[idx++] = (data[addr + 3] << 24) | (data[addr + 2 - adjustOrder] << 16) | (data[addr + 1] << 8) | (data[addr + adjustOrder]); } } return(pixels); }
public static CGBitmapContext CreateContext(CGSize size, out byte[] data, CGImageAlphaInfo bitmapInfo) { var width = (int)size.Width; var height = (int)size.Height; var bytesPerRow = width * 4; var byteCount = bytesPerRow * height; var colorSpace = CGColorSpace.CreateDeviceRGB(); data = new byte[byteCount]; return(new CGBitmapContext(data, width, height, 8, bytesPerRow, colorSpace, bitmapInfo)); }
public static UIImage Rotate(UIImage image) { UIImage res = null; using (CGImage imageRef = image.CGImage) { CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo; CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB(); if (alphaInfo == CGImageAlphaInfo.None) { alphaInfo = CGImageAlphaInfo.NoneSkipLast; } CGBitmapContext bitmap = new CGBitmapContext(IntPtr.Zero, imageRef.Height, imageRef.Width, imageRef.BitsPerComponent, 0, colorSpaceInfo, alphaInfo); bitmap.RotateCTM((nfloat)Math.PI / 2); bitmap.TranslateCTM(0, -imageRef.Height); bitmap.DrawImage(new CGRect(0, 0, imageRef.Width, imageRef.Height), imageRef); res = UIImage.FromImage(bitmap.ToImage()); bitmap = null; } return(res); }
private CGImage createRGBImageFromBufferData(int mByteWidth, int mWidth, int mHeight) { CGColorSpace cSpace = CGColorSpace.CreateDeviceRGB(); CGImageAlphaInfo ai = (CGImageAlphaInfo)((int)CGImageAlphaInfo.NoneSkipFirst | (int)CGBitmapFlags.ByteOrder32Little); CGBitmapContext bitmap; byte[] mData = GetImageData(0); try { unsafe { fixed(byte *ptr = mData) { bitmap = new CGBitmapContext((IntPtr)ptr, mWidth, mHeight, 8, mByteWidth, cSpace, ai); } } } catch { } CGImage image = bitmap.ToImage(); return(image); }
public CGBitmapContext(IntPtr data, int width, int height, int bitsPerComponent, int bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) : base(CGBitmapContextCreate (data, (UIntPtr) width, (UIntPtr) height, (UIntPtr) bitsPerComponent, (UIntPtr) bytesPerRow, colorSpace.handle, (uint) bitmapInfo), true) { }
public static UIImage ScaleImage(UIImage image, int maxSize) { UIImage result; nint width, height; using (CGImage imageRef = image.CGImage) { CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo; CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB(); if (alphaInfo == CGImageAlphaInfo.None) { alphaInfo = CGImageAlphaInfo.NoneSkipLast; } width = imageRef.Width; height = imageRef.Height; if (height >= width) { width = (int)Math.Floor((double)width * ((double)maxSize / (double)height)); height = maxSize; } else { height = (int)Math.Floor((double)height * ((double)maxSize / (double)width)); width = maxSize; } CGBitmapContext bitmap; if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down) { bitmap = new CGBitmapContext(IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo); } else { bitmap = new CGBitmapContext(IntPtr.Zero, height, width, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo); } switch (image.Orientation) { case UIImageOrientation.Left: bitmap.RotateCTM((float)Math.PI / 2); bitmap.TranslateCTM(0, -height); break; case UIImageOrientation.Right: bitmap.RotateCTM(-((float)Math.PI / 2)); bitmap.TranslateCTM(-width, 0); break; case UIImageOrientation.Up: break; case UIImageOrientation.Down: bitmap.TranslateCTM(width, height); bitmap.RotateCTM(-(float)Math.PI); break; } bitmap.DrawImage(new CGRect(0, 0, width, height), imageRef); result = UIImage.FromImage(bitmap.ToImage()); bitmap = null; } return(result); }
private static UIImage ScaleImage(UIImage image, int maxSize) { UIImage res = image; CGImage imageRef = image.CGImage; CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo; CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB(); if (alphaInfo == CGImageAlphaInfo.None) { alphaInfo = CGImageAlphaInfo.NoneSkipLast; } int width = imageRef.Width; int height = imageRef.Height; if (maxSize > 0 && maxSize < Math.Max(width, height)) { try { if (height >= width) { width = (int)Math.Floor(width * (maxSize / (double)height)); height = maxSize; } else { height = (int)Math.Floor(height * (maxSize / (double)width)); width = maxSize; } int bytesPerRow = (int)image.Size.Width * 4; var buffer = new byte[(int)(bytesPerRow * image.Size.Height)]; CGBitmapContext bitmap; if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down) { bitmap = new CGBitmapContext(buffer, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo); } else { bitmap = new CGBitmapContext(buffer, height, width, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo); } switch (image.Orientation) { case UIImageOrientation.Left: bitmap.RotateCTM((float)Math.PI / 2); bitmap.TranslateCTM(0, -height); break; case UIImageOrientation.Right: bitmap.RotateCTM(-((float)Math.PI / 2)); bitmap.TranslateCTM(-width, 0); break; case UIImageOrientation.Up: break; case UIImageOrientation.Down: bitmap.TranslateCTM(width, height); bitmap.RotateCTM(-(float)Math.PI); break; } bitmap.DrawImage(new RectangleF(0, 0, width, height), imageRef); res = UIImage.FromImage(bitmap.ToImage()); } finally { image.Dispose(); } } return(res); }
public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) { if (data != null) buffer = GCHandle.Alloc (data, GCHandleType.Pinned); Handle = CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, GetHandle (colorSpace), (uint) bitmapInfo); }
public CGBitmapContext(byte []?data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGImageAlphaInfo bitmapInfo) : base(Create(data, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo, out var buffer), true) { this.buffer = buffer; }
static IntPtr Create(byte []?data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGImageAlphaInfo bitmapInfo, out GCHandle buffer) { buffer = default(GCHandle); if (data != null) { buffer = GCHandle.Alloc(data, GCHandleType.Pinned); // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns. } return(CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle(), (uint)bitmapInfo)); }
public CGBitmapContext(IntPtr data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace?colorSpace, CGImageAlphaInfo bitmapInfo) : base(CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorSpace.GetHandle(), (uint)bitmapInfo), true) { }
public CGBitmapContext(IntPtr data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) : base(CGBitmapContextCreate (data, width, height, bitsPerComponent, bytesPerRow, GetHandle (colorSpace), (uint) bitmapInfo), true) { }
public CGBitmapContext(byte [] data, int width, int height, int bitsPerComponent, int bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) : base(CGBitmapContextCreate(data, (UIntPtr)width, (UIntPtr)height, (UIntPtr)bitsPerComponent, (UIntPtr)bytesPerRow, colorSpace.handle, (uint)bitmapInfo), true) { }
public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) { if (data != null) { buffer = GCHandle.Alloc(data, GCHandleType.Pinned); // This requires a pinned GCHandle, because unsafe code is scoped to the current block, and the address of the byte array will be used after this function returns. } Handle = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, GetHandle(colorSpace), (uint)bitmapInfo); }
public CGImage (int width, int height, int bitsPerComponent, int bitsPerPixel, int bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo alphaInfo, CGDataProvider provider, nfloat [] decode, bool shouldInterpolate, CGColorRenderingIntent intent) { if (width < 0) throw new ArgumentException ("width"); if (height < 0) throw new ArgumentException ("height"); if (bitsPerPixel < 0) throw new ArgumentException ("bitsPerPixel"); if (bitsPerComponent < 0) throw new ArgumentException ("bitsPerComponent"); if (bytesPerRow < 0) throw new ArgumentException ("bytesPerRow"); handle = CGImageCreate (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace == null ? IntPtr.Zero : colorSpace.Handle, (CGBitmapFlags) alphaInfo, provider == null ? IntPtr.Zero : provider.Handle, decode, shouldInterpolate, intent); }
private UIImage ScaleAndRotateImage(UIImage imageIn, UIImageOrientation orIn) { UIImage res; using (CGImage imageRef = imageIn.CGImage) { CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo; CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB(); if (alphaInfo == CGImageAlphaInfo.None) { alphaInfo = CGImageAlphaInfo.NoneSkipLast; } int width, height; double maxSize = (double)imageIn.Size.Width; width = (int)imageRef.Width; height = (int)imageRef.Height; if (height >= width) { if (orIn == UIImageOrientation.Up || orIn == UIImageOrientation.Down) { maxSize = (double)imageIn.Size.Height; } else { maxSize = (double)imageIn.Size.Height; alphaInfo = CGImageAlphaInfo.PremultipliedLast; } width = (int)Math.Floor((double)width * ((double)maxSize / (double)height)); height = (int)maxSize; } else { height = (int)Math.Floor((double)height * ((double)maxSize / (double)width)); width = (int)maxSize; } CGBitmapContext bitmap; if (orIn == UIImageOrientation.Up || orIn == UIImageOrientation.Down) { bitmap = new CGBitmapContext(IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo); } else { bitmap = new CGBitmapContext(IntPtr.Zero, height, width, imageRef.BitsPerComponent, width * imageRef.BitsPerPixel, colorSpaceInfo, alphaInfo); } switch (orIn) { case UIImageOrientation.Left: bitmap.RotateCTM((float)Math.PI / 2); bitmap.TranslateCTM(0, -height); break; case UIImageOrientation.Right: bitmap.RotateCTM(-((float)Math.PI / 2)); bitmap.TranslateCTM(-width, 0); break; case UIImageOrientation.Up: break; case UIImageOrientation.Down: bitmap.TranslateCTM(width, height); bitmap.RotateCTM(-(float)Math.PI); break; } bitmap.DrawImage(new Rectangle(0, 0, width, height), imageRef); res = UIImage.FromImage(bitmap.ToImage()); bitmap = null; return(res); } }
public CGBitmapContext(byte [] data, nint width, nint height, nint bitsPerComponent, nint bytesPerRow, CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) { if (data != null) { buffer = GCHandle.Alloc(data, GCHandleType.Pinned); } Handle = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, GetHandle(colorSpace), (uint)bitmapInfo); }