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 override object CreateImageBuilder (int width, int height, ImageFormat format) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = width * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = width * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException ("ImageFormat: " + format.ToString ()); } var bmp = new CGBitmapContext (IntPtr.Zero, width, height, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM (0, height); bmp.ScaleCTM (1, -1); return new CGContextBackend { Context = bmp, Size = new CGSize (width, height), InverseViewTransform = bmp.GetCTM ().Invert () }; }
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); }
private void InitWithCGImage(CGImage image, All filter) { int width, height, i; CGContext context = null; IntPtr data; CGColorSpace colorSpace; IntPtr tempData; bool hasAlpha; CGImageAlphaInfo info; CGAffineTransform transform; Size imageSize; SurfaceFormat pixelFormat; //bool sizeToFit = false; if (image == null) { throw new ArgumentException (" NSImage is invalid! " ); } info = image.AlphaInfo; hasAlpha = ((info == CGImageAlphaInfo.PremultipliedLast) || (info == CGImageAlphaInfo.PremultipliedFirst) || (info == CGImageAlphaInfo.Last) || (info == CGImageAlphaInfo.First) ? true : false); if (image.ColorSpace != null) { if (hasAlpha) { pixelFormat = SurfaceFormat.Color; } else { pixelFormat = SurfaceFormat.Color; } } else { pixelFormat = SurfaceFormat.Alpha8; } imageSize = new Size (image.Width,image.Height); transform = CGAffineTransform.MakeIdentity (); width = imageSize.Width; // Take out the width and height adjustments for power of 2 // If not then GetData and SetData is messed up. // The Mac opengl version supports non power of 2 textures // so we do not have to make them so // if ((width != 1) && ((width & (width - 1)) != 0)) { // i = 1; // while ((sizeToFit ? 2 * i : i) < width) // i *= 2; // width = i; // } height = imageSize.Height; // The Mac opengl version supports non power of 2 textures // so we do not have to make them so // if ((height != 1) && ((height & (height - 1)) != 0)) { // i = 1; // while ((sizeToFit ? 2 * i : i) < height) // i *= 2; // height = i; // } // TODO: kMaxTextureSize = 1024 // while ((width > 1024) || (height > 1024)) { // width /= 2; // height /= 2; // transform = CGAffineTransform.MakeScale (0.5f, 0.5f); // imageSize.Width /= 2; // imageSize.Height /= 2; // } float size = Math.Max(width,height); if(size > 1024) { float ratio = 1024 / size; width = (int)(width * ratio); height = (int)(height * ratio); transform = CGAffineTransform.MakeScale(ratio, ratio); imageSize.Width = (int)(imageSize.Width * ratio); imageSize.Height = (int)(imageSize.Height * ratio);; } switch (pixelFormat) { case SurfaceFormat.Color: colorSpace = CGColorSpace.CreateDeviceRGB (); data = Marshal.AllocHGlobal (height * width * 4); context = new CGBitmapContext (data, width, height, 8, 4 * width, colorSpace,CGImageAlphaInfo.PremultipliedLast); colorSpace.Dispose (); break; case SurfaceFormat.Alpha8: data = Marshal.AllocHGlobal (height * width); context = new CGBitmapContext (data, width, height, 8, width, null, CGImageAlphaInfo.Only); break; default: throw new NotSupportedException ("Invalid pixel format"); } context.ClearRect (new RectangleF (0,0,width,height)); context.TranslateCTM (0, height - imageSize.Height); if (!transform.IsIdentity) { context.ConcatCTM (transform); } context.DrawImage (new RectangleF (0, 0, image.Width, image.Height), image); //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB" /* if(pixelFormat == SurfaceFormat.Rgb32) { tempData = Marshal.AllocHGlobal(height * width * 2); int d32; short d16; int inPixel32Count=0,outPixel16Count=0; for(i = 0; i < width * height; ++i, inPixel32Count+=sizeof(int)) { d32 = Marshal.ReadInt32(data,inPixel32Count); short R = (short)((((d32 >> 0) & 0xFF) >> 3) << 11); short G = (short)((((d32 >> 8) & 0xFF) >> 2) << 5); short B = (short)((((d32 >> 16) & 0xFF) >> 3) << 0); d16 = (short) (R | G | B); Marshal.WriteInt16(tempData,outPixel16Count,d16); outPixel16Count += sizeof(short); } Marshal.FreeHGlobal(data); data = tempData; } */ InitWithData (data, pixelFormat, width, height, imageSize, filter); context.Dispose (); Marshal.FreeHGlobal (data); }
public override object ConvertToBitmap(object handle, double width, double height, double scaleFactor, ImageFormat format) { int pixelWidth = (int)(width * scaleFactor); int pixelHeight = (int)(height * scaleFactor); if (handle is CustomImage) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = pixelWidth * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = pixelWidth * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException ("ImageFormat: " + format.ToString ()); } var bmp = new CGBitmapContext (IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM (0, pixelHeight); bmp.ScaleCTM ((float)scaleFactor, (float)-scaleFactor); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF ((float)width, (float)height), InverseViewTransform = bmp.GetCTM ().Invert (), ScaleFactor = scaleFactor }; var ci = (CustomImage)handle; ci.DrawInContext (ctx); var img = new NSImage (((CGBitmapContext)bmp).ToImage (), new SizeF (pixelWidth, pixelHeight)); var imageData = img.AsTiff (); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData (imageData); var im = new NSImage (); im.AddRepresentation (imageRep); im.Size = new SizeF ((float)width, (float)height); bmp.Dispose (); return im; } else { NSImage img = (NSImage)handle; NSBitmapImageRep bitmap = img.Representations ().OfType<NSBitmapImageRep> ().FirstOrDefault (); if (bitmap == null) { var imageData = img.AsTiff (); var imageRep = (NSBitmapImageRep)NSBitmapImageRep.ImageRepFromData (imageData); var im = new NSImage (); im.AddRepresentation (imageRep); im.Size = new SizeF ((float)width, (float)height); return im; } return handle; } }
private void InitWithCGImage (CGImage image, All filter) { int width, height, i; CGContext context = null; IntPtr data; CGColorSpace colorSpace; IntPtr tempData; bool hasAlpha; CGImageAlphaInfo info; CGAffineTransform transform; Size imageSize; SurfaceFormat pixelFormat; bool sizeToFit = false; if (image == null) { throw new ArgumentException (" NSImage is invalid! " ); } info = image.AlphaInfo; hasAlpha = ((info == CGImageAlphaInfo.PremultipliedLast) || (info == CGImageAlphaInfo.PremultipliedFirst) || (info == CGImageAlphaInfo.Last) || (info == CGImageAlphaInfo.First) ? true : false); if (image.ColorSpace != null) { if (hasAlpha) { pixelFormat = SurfaceFormat.Rgba32; } else { pixelFormat = SurfaceFormat.Rgb32; } } else { pixelFormat = SurfaceFormat.Alpha8; } imageSize = new Size (image.Width,image.Height); transform = CGAffineTransform.MakeIdentity (); width = imageSize.Width; if ((width != 1) && ((width & (width - 1)) != 0)) { i = 1; while ((sizeToFit ? 2 * i : i) < width) i *= 2; width = i; } height = imageSize.Height; if ((height != 1) && ((height & (height - 1)) != 0)) { i = 1; while ((sizeToFit ? 2 * i : i) < height) i *= 2; height = i; } // TODO: kMaxTextureSize = 1024 while ((width > 1024) || (height > 1024)) { width /= 2; height /= 2; transform = CGAffineTransform.MakeScale (0.5f, 0.5f); imageSize.Width /= 2; imageSize.Height /= 2; } switch (pixelFormat) { case SurfaceFormat.Rgba32: colorSpace = CGColorSpace.CreateDeviceRGB (); data = Marshal.AllocHGlobal (height * width * 4); context = new CGBitmapContext (data, width, height, 8, 4 * width, colorSpace,CGImageAlphaInfo.PremultipliedLast); colorSpace.Dispose (); break; case SurfaceFormat.Rgb32: colorSpace = CGColorSpace.CreateDeviceRGB (); data = Marshal.AllocHGlobal (height * width * 4); context = new CGBitmapContext (data, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.NoneSkipLast); colorSpace.Dispose (); break; case SurfaceFormat.Alpha8: data = Marshal.AllocHGlobal (height * width); context = new CGBitmapContext (data, width, height, 8, width, null, CGImageAlphaInfo.Only); break; default: throw new NotSupportedException ("Invalid pixel format"); } context.ClearRect (new RectangleF (0,0,width,height)); context.TranslateCTM (0, height - imageSize.Height); if (!transform.IsIdentity) { context.ConcatCTM (transform); } context.DrawImage (new RectangleF (0, 0, image.Width, image.Height), image); //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB" if (pixelFormat == SurfaceFormat.Rgb32) { tempData = Marshal.AllocHGlobal (height * width * 2); int d32; short d16; int inPixel32Count = 0, outPixel16Count=0; for (i = 0; i < width * height; ++i, inPixel32Count+=sizeof(int)) { d32 = Marshal.ReadInt32 (data, inPixel32Count); short R = (short)((((d32 >> 0) & 0xFF) >> 3) << 11); short G = (short)((((d32 >> 8) & 0xFF) >> 2) << 5); short B = (short)((((d32 >> 16) & 0xFF) >> 3) << 0); d16 = (short)(R | G | B); Marshal.WriteInt16 (tempData, outPixel16Count, d16); outPixel16Count += sizeof(short); } Marshal.FreeHGlobal (data); data = tempData; } InitWithData (data, pixelFormat, width, height, imageSize, filter); context.Dispose (); Marshal.FreeHGlobal (data); }
public override object ConvertToBitmap(object handle, int pixelWidth, int pixelHeight, ImageFormat format) { if (handle is CustomImage) { var flags = CGBitmapFlags.ByteOrderDefault; int bytesPerRow; switch (format) { case ImageFormat.ARGB32: bytesPerRow = pixelWidth * 4; flags |= CGBitmapFlags.PremultipliedFirst; break; case ImageFormat.RGB24: bytesPerRow = pixelWidth * 3; flags |= CGBitmapFlags.None; break; default: throw new NotImplementedException ("ImageFormat: " + format.ToString ()); } var bmp = new CGBitmapContext (IntPtr.Zero, pixelWidth, pixelHeight, 8, bytesPerRow, Util.DeviceRGBColorSpace, flags); bmp.TranslateCTM (0, pixelHeight); bmp.ScaleCTM (1, -1); var ctx = new CGContextBackend { Context = bmp, Size = new SizeF (pixelWidth, pixelHeight), InverseViewTransform = bmp.GetCTM ().Invert () }; var ci = (CustomImage)handle; ci.DrawInContext (ctx); var img = new NSImage (((CGBitmapContext)bmp).ToImage (), new SizeF (pixelWidth, pixelHeight)); var imageData = img.AsTiff (); var imageRep = (NSBitmapImageRep) NSBitmapImageRep.ImageRepFromData (imageData); var im = new NSImage (); im.AddRepresentation (imageRep); return im; } else return handle; }