protected override void init(Stream stream, bool flip, Loader.LoadedCallbackMethod loadedCallback) { try { using (var image = NSImage.FromStream(stream)) { var rep = image.Representations()[0]; int width = rep.PixelsWide; int height = rep.PixelsHigh; Mipmaps = new Mipmap[1]; Size = new Size2(width, height); var data = new byte[width * height * 4]; var emptyRect = RectangleF.Empty; using (CGContext imageContext = new CGBitmapContext(data, width, height, 8, width*4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast)) using (var cgImage = image.AsCGImage(ref emptyRect, null, null)) { imageContext.DrawImage(new RectangleF(0, 0, width, height), cgImage); Mipmaps[0] = new Mipmap(data, width, height, 1, 4); if (flip) Mipmaps[0].FlipVertical(); } } } catch (Exception e) { FailedToLoad = true; Loader.AddLoadableException(e); if (loadedCallback != null) loadedCallback(this, false); return; } Loaded = true; if (loadedCallback != null) loadedCallback(this, true); }
void GetImagaDataFromPath (string path) { NSImage src; CGImage image; CGContext context = null; src = new NSImage (path); var rect = RectangleF.Empty; image = src.AsCGImage (ref rect, null, null); width = image.Width; height = image.Height; data = new byte[width * height * 4]; 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); }
// Used for debuggin purposes internal static void SaveToFile (string fileName, CGBitmapContext bitmap) { if (bitmap == null) throw new ObjectDisposedException ("cgimage"); // With MonoTouch we can use UTType from CoreMobileServices but since // MonoMac does not have that yet (or at least can not find it) I will // use the string version of those for now. I did not want to add another // #if #else in here. // for now we will just default this to png var typeIdentifier = "public.png"; // * NOTE * we only support one image for right now. //NSMutableData imgData = new NSMutableData(); NSUrl url = NSUrl.FromFilename (fileName); // Create an image destination that saves into the imgData CGImageDestination dest = CGImageDestination.Create (url, typeIdentifier, 1); // Add an image to the destination dest.AddImage(bitmap.GetImage(), (NSDictionary)null); // Finish the export bool success = dest.Close (); // if (success == false) // Console.WriteLine("did not work"); // else // Console.WriteLine("did work: " + path); dest.Dispose(); dest = null; }
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 () }; }
private void CreateBitmap(int width, int height) { // if (_bitmap == null || (_bitmap.Width < width || _bitmap.Height < height)) // { _bitmap = CCLabelUtilities.CreateBitmap (width, height); //} //if (_brush == null) //{ _brush = new CCColor4B(Microsoft.Xna.Framework.Color.White); //} }
private void CreateBitmap(int width, int height) { // if (_bitmap == null || (_bitmap.Width < width || _bitmap.Height < height)) // { _bitmap = CCLabelUtilities.CreateBitmap (width, height); //} //if (_brush == null) //{ _brush = CCColor4B.White; //} }
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; } }
internal static CGBitmapContext CreateBitmap (int width, int height) //, PixelFormat format) { int bitsPerComponent, bytesPerRow; CGColorSpace colorSpace; CGBitmapFlags bitmapInfo; // bool premultiplied = false; int bitsPerPixel = 0; // Don't forget to set the Image width and height for size. imageSize.Width = width; imageSize.Height = height; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.PremultipliedLast; bytesPerRow = width * bitsPerPixel/bitsPerComponent; int size = bytesPerRow * height; bitmapBlock = Marshal.AllocHGlobal (size); var bitmapContext = new CGBitmapContext (bitmapBlock, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); // This works for now but we need to look into initializing the memory area itself bitmapContext.ClearRect (new CGRect (0,0,width,height)); return bitmapContext; }
internal static void NativeDrawString (CGBitmapContext bitmapContext, string s, CTFont font, CCColor4B brush, CGRect layoutRectangle) { if (font == null) throw new ArgumentNullException ("font"); if (s == null || s.Length == 0) return; bitmapContext.ConcatCTM (bitmapContext.GetCTM().Invert()); // This is not needed here since the color is set in the attributed string. //bitmapContext.SetFillColor(brush.R/255f, brush.G/255f, brush.B/255f, brush.A/255f); // I think we only Fill the text with no Stroke surrounding //bitmapContext.SetTextDrawingMode(CGTextDrawingMode.Fill); var attributedString = buildAttributedString(s, font, brush); // Work out the geometry CGRect insetBounds = layoutRectangle; CGPoint textPosition = new CGPoint(insetBounds.X, insetBounds.Y); float boundsWidth = (float)insetBounds.Width; // Calculate the lines nint start = 0; nint length = attributedString.Length; var typesetter = new CTTypesetter(attributedString); float baselineOffset = 0; // First we need to calculate the offset for Vertical Alignment if we // are using anything but Top if (vertical != CCVerticalTextAlignment.Top) { while (start < length) { nint count = typesetter.SuggestLineBreak((int)start, (double)boundsWidth); var line = typesetter.GetLine (new NSRange(start, count)); // Create and initialize some values from the bounds. nfloat ascent; nfloat descent; nfloat leading; line.GetTypographicBounds(out ascent, out descent, out leading); baselineOffset += (float)Math.Ceiling ((float)(ascent + descent + leading + 1)); // +1 matches best to CTFramesetter's behavior line.Dispose (); start += count; } } start = 0; while (start < length && textPosition.Y < insetBounds.Bottom) { // Now we ask the typesetter to break off a line for us. // This also will take into account line feeds embedded in the text. // Example: "This is text \n with a line feed embedded inside it" nint count = typesetter.SuggestLineBreak((int)start, (double)boundsWidth); var line = typesetter.GetLine(new NSRange(start, count)); // Create and initialize some values from the bounds. nfloat ascent; nfloat descent; nfloat leading; line.GetTypographicBounds(out ascent, out descent, out leading); // Calculate the string format if need be var penFlushness = 0.0f; if (horizontal == CCTextAlignment.Right) penFlushness = (float)line.GetPenOffsetForFlush(1.0f, boundsWidth); else if (horizontal == CCTextAlignment.Center) penFlushness = (float)line.GetPenOffsetForFlush(0.5f, boundsWidth); // initialize our Text Matrix or we could get trash in here var textMatrix = CGAffineTransform.MakeIdentity(); if (vertical == CCVerticalTextAlignment.Top) textMatrix.Translate(penFlushness, insetBounds.Height - textPosition.Y -(float)Math.Floor(ascent - 1)); if (vertical == CCVerticalTextAlignment.Center) textMatrix.Translate(penFlushness, ((insetBounds.Height / 2) + (baselineOffset / 2)) - textPosition.Y -(float)Math.Floor(ascent - 1)); if (vertical == CCVerticalTextAlignment.Bottom) textMatrix.Translate(penFlushness, baselineOffset - textPosition.Y -(float)Math.Floor(ascent - 1)); // Set our matrix bitmapContext.TextMatrix = textMatrix; // and draw the line line.Draw(bitmapContext); // Move the index beyond the line break. start += count; textPosition.Y += (float)Math.Ceiling(ascent + descent + leading + 1); // +1 matches best to CTFramesetter's behavior line.Dispose(); } }
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; }
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 Bitmap(int width, int height, PixelFormat format) { imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height); int bitsPerComponent, bytesPerRow; CGColorSpace colorSpace; CGBitmapFlags bitmapInfo; bool premultiplied = false; int bitsPerPixel = 0; pixelFormat = format; // Don't forget to set the Image width and height for size. imageSize.Width = width; imageSize.Height = height; switch (format){ case PixelFormat.Format32bppPArgb: case PixelFormat.DontCare: premultiplied = true; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.PremultipliedFirst; break; case PixelFormat.Format32bppArgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.PremultipliedFirst; break; case PixelFormat.Format32bppRgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.NoneSkipLast; break; case PixelFormat.Format24bppRgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.NoneSkipLast; break; default: throw new Exception ("Format not supported: " + format); } bytesPerRow = width * bitsPerPixel/bitsPerComponent; int size = bytesPerRow * height; bitmapBlock = Marshal.AllocHGlobal (size); var bitmap = new CGBitmapContext (bitmapBlock, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); // This works for now but we need to look into initializing the memory area itself // TODO: Look at what we should do if the image does not have alpha channel bitmap.ClearRect (new RectangleF (0,0,width,height)); var provider = new CGDataProvider (bitmapBlock, size, true); NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, CGColorRenderingIntent.Default); dpiWidth = dpiHeight = ConversionHelpers.MS_DPI; physicalDimension.Width = width; physicalDimension.Height = height; // The physical size may be off on certain implementations. For instance the dpiWidth and dpiHeight // are read using integers in core graphics but in windows it is a float. // For example: // coregraphics dpiWidth = 24 as integer // windows dpiWidth = 24.999935 as float // this gives a few pixels difference when calculating the physical size. // 256 * 96 / 24 = 1024 // 256 * 96 / 24.999935 = 983.04 // // https://bugzilla.xamarin.com/show_bug.cgi?id=14365 // PR: https://github.com/mono/maccore/pull/57 // physicalSize = new SizeF (physicalDimension.Width, physicalDimension.Height); physicalSize.Width *= ConversionHelpers.MS_DPI / dpiWidth; physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight; rawFormat = ImageFormat.MemoryBmp; pixelFormat = format; }
private void MakeSureWeHaveAnAlphaChannel() { // Initialize our prmultiplied tables. if (!ConversionHelpers.sTablesInitialized) ConversionHelpers.CalculateTables (); var alphaInfo = NativeCGImage.AlphaInfo; var hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast) || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst) || (alphaInfo == CGImageAlphaInfo.Last) || (alphaInfo == CGImageAlphaInfo.First) ? true : false); if (cachedContext != null && cachedContext.Handle != IntPtr.Zero) { return; } // set our pixel format pixelFormat = PixelFormat.Format32bppArgb; // and mark the rawformat as from memory rawFormat = ImageFormat.MemoryBmp; //format = GetBestSupportedFormat (pixelFormat); cachedContext = CreateCompatibleBitmapContext (NativeCGImage.Width, NativeCGImage.Height, pixelFormat); // Fill our pixel data with the actual image information cachedContext.DrawImage (new RectangleF (0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage); // Dispose of the prevous image that is allocated. NativeCGImage.Dispose (); // Get a reference to the pixel data bitmapBlock = cachedContext.Data; int size = cachedContext.BytesPerRow * cachedContext.Height; var provider = new CGDataProvider (cachedContext.Data, size, true); // Get the image from the bitmap context. //NativeCGImage = bitmapContext.ToImage (); CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); NativeCGImage = new CGImage (cachedContext.Width, cachedContext.Height, cachedContext.BitsPerComponent, cachedContext.BitsPerPixel, cachedContext.BytesPerRow, colorSpace, cachedContext.AlphaInfo, provider, null, true, CGColorRenderingIntent.Default); colorSpace.Dispose (); }
private void InitWithCGImage(CGImage image) { int width, height; CGBitmapContext bitmap = null; bool hasAlpha; CGImageAlphaInfo alphaInfo; CGColorSpace colorSpace; int bitsPerComponent, bytesPerRow; CGBitmapFlags bitmapInfo; bool premultiplied = false; int bitsPerPixel = 0; if (image == null) { throw new ArgumentException (" image is invalid! " ); } alphaInfo = image.AlphaInfo; hasAlpha = ((alphaInfo == CGImageAlphaInfo.PremultipliedLast) || (alphaInfo == CGImageAlphaInfo.PremultipliedFirst) || (alphaInfo == CGImageAlphaInfo.Last) || (alphaInfo == CGImageAlphaInfo.First) ? true : false); imageSize.Width = image.Width; imageSize.Height = image.Height; width = image.Width; height = image.Height; // Not sure yet if we need to keep the original image information // before we change it internally. TODO look at what windows does // and follow that. bitmapInfo = image.BitmapInfo; bitsPerComponent = image.BitsPerComponent; bitsPerPixel = image.BitsPerPixel; bytesPerRow = width * bitsPerPixel/bitsPerComponent; int size = bytesPerRow * height; colorSpace = image.ColorSpace; // Right now internally we represent the images all the same // I left the call here just in case we find that this is not // possible. Read the comments for non alpha images. if(colorSpace != null) { if( hasAlpha ) { premultiplied = true; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.PremultipliedLast; } else { // even for images without alpha we will internally // represent them as RGB with alpha. There were problems // if we do not do it this way and creating a bitmap context. // The images were not drawing correctly and tearing. Also // creating a Graphics to draw on was a nightmare. This // should probably be looked into or maybe it is ok and we // can continue representing internally with this representation premultiplied = true; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.NoneSkipLast; } } else { premultiplied = true; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.NoneSkipLast; } bytesPerRow = width * bitsPerPixel/bitsPerComponent; size = bytesPerRow * height; bitmapBlock = Marshal.AllocHGlobal (size); bitmap = new CGBitmapContext (bitmapBlock, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); bitmap.ClearRect (new RectangleF (0,0,width,height)); // We need to flip the Y axis to go from right handed to lefted handed coordinate system var transform = new CGAffineTransform(1, 0, 0, -1, 0, image.Height); bitmap.ConcatCTM(transform); bitmap.DrawImage(new RectangleF (0, 0, image.Width, image.Height), image); var provider = new CGDataProvider (bitmapBlock, size, true); NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, true, image.RenderingIntent); colorSpace.Dispose(); bitmap.Dispose(); }
public void CreateFromImage(Bitmap image) { var handler = image.Handler as BitmapHandler; SourceImage = image; #if OSX var rep = handler.Control.Representations().OfType<NSBitmapImageRep>().FirstOrDefault(); if (rep.BitsPerPixel != 32) { // CoreGraphics only supports drawing to 32bpp, create a new 32-bpp image and copy back when disposed or flushed. DrawingImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb); handler = DrawingImage.Handler as BitmapHandler; rep = handler.Control.Representations().OfType<NSBitmapImageRep>().FirstOrDefault(); } graphicsContext = NSGraphicsContext.FromBitmap(rep); graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true); disposeContext = true; Control = graphicsContext.GraphicsPort; PointsPerPixel = (float)(rep.PixelsWide / handler.Control.Size.Width); #elif IOS var cgimage = handler.Control.CGImage; Control = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo); PointsPerPixel = (float)(cgimage.Width / handler.Control.Size.Width); #endif height = image.Size.Height; SetDefaults(); InitializeContext(true); if (DrawingImage != null && SourceImage != null) { // draw source image onto context, when source is incompatible for CoreGraphics drawing. DrawImage(SourceImage, 0, 0); } }
public void CreateFromImage(Bitmap image) { var handler = image.Handler as BitmapHandler; #if OSX var rep = handler.Control.Representations().OfType<NSBitmapImageRep>().FirstOrDefault(); graphicsContext = NSGraphicsContext.FromBitmap(rep); graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true); disposeContext = true; Control = graphicsContext.GraphicsPort; scale = (float)(rep.PixelsWide / handler.Control.Size.Width); #elif IOS var cgimage = handler.Control.CGImage; Control = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo); scale = cgimage.Width / handler.Control.Size.Width; #endif height = image.Size.Height; SetDefaults(); InitializeContext(true); }
public static Graphics FromImage(Image image) { if (image == null) throw new ArgumentNullException ("image"); if ((image.PixelFormat & PixelFormat.Indexed) != 0) throw new Exception ("Cannot create Graphics from an indexed bitmap."); 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. var bitmapContext = new CGBitmapContext (b.bitmapBlock, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.AlphaInfo); return new Graphics (bitmapContext); }
internal CGBitmapContext GetRenderableContext() { if (cachedContext != null && cachedContext.Handle != IntPtr.Zero) return cachedContext; var format = GetBestSupportedFormat (pixelFormat); var bitmapContext = CreateCompatibleBitmapContext (NativeCGImage.Width, NativeCGImage.Height, format); bitmapContext.DrawImage (new RectangleF (0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage); int size = bitmapContext.BytesPerRow * bitmapContext.Height; var provider = new CGDataProvider (bitmapContext.Data, size, true); CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); NativeCGImage = new CGImage (bitmapContext.Width, bitmapContext.Height, bitmapContext.BitsPerComponent, bitmapContext.BitsPerPixel, bitmapContext.BytesPerRow, colorSpace, bitmapContext.AlphaInfo, provider, null, true, CGColorRenderingIntent.Default); colorSpace.Dispose (); cachedContext = bitmapContext; return cachedContext; }
public Bitmap(int width, int height, PixelFormat format) { int bitsPerComponent, bytesPerRow; CGColorSpace colorSpace; CGBitmapFlags bitmapInfo; bool premultiplied = false; int bitsPerPixel = 0; // Don't forget to set the Image width and height for size. imageSize.Width = width; imageSize.Height = height; switch (format){ case PixelFormat.Format32bppPArgb: premultiplied = true; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.PremultipliedFirst; break; case PixelFormat.Format32bppArgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.PremultipliedFirst; break; case PixelFormat.Format32bppRgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; bitmapInfo = CGBitmapFlags.None; break; default: throw new Exception ("Format not supported: " + format); } bytesPerRow = width * bitsPerPixel/bitsPerComponent; int size = bytesPerRow * height; bitmapBlock = Marshal.AllocHGlobal (size); var bitmap = new CGBitmapContext (bitmapBlock, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedLast); // This works for now but we need to look into initializing the memory area itself // TODO: Look at what we should do if the image does not have alpha channel bitmap.ClearRect (new RectangleF (0,0,width,height)); var provider = new CGDataProvider (bitmapBlock, size, true); NativeCGImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapInfo, provider, null, false, CGColorRenderingIntent.Default); }
private CGBitmapContext CreateCompatibleBitmapContext(int width, int height, PixelFormat pixelFormat, IntPtr pixelData) { int bitsPerComponent, bytesPerRow; CGColorSpace colorSpace; CGImageAlphaInfo alphaInfo; bool premultiplied = false; int bitsPerPixel = 0; // CoreGraphics only supports a few options so we have to make do with what we have // https://developer.apple.com/library/mac/qa/qa1037/_index.html switch (pixelFormat) { case PixelFormat.Format32bppPArgb: case PixelFormat.DontCare: premultiplied = true; colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; alphaInfo = CGImageAlphaInfo.PremultipliedLast; break; case PixelFormat.Format32bppArgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; alphaInfo = CGImageAlphaInfo.PremultipliedLast; break; case PixelFormat.Format32bppRgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; alphaInfo = CGImageAlphaInfo.PremultipliedLast; break; case PixelFormat.Format24bppRgb: colorSpace = CGColorSpace.CreateDeviceRGB (); bitsPerComponent = 8; bitsPerPixel = 32; alphaInfo = CGImageAlphaInfo.PremultipliedLast; break; default: throw new Exception ("Format not supported: " + pixelFormat); } bytesPerRow = width * bitsPerPixel/bitsPerComponent; int size = bytesPerRow * height; var bitmap = new CGBitmapContext (pixelData, width, height, bitsPerComponent, bytesPerRow, colorSpace, alphaInfo); colorSpace.Dispose (); return bitmap; }
private void DrawNoise(float opacity) { if (_noiseImageRef == null) { const int width = 124; const int height = width; const int size = width * height; var rgba = new byte[size]; var random = new Random(120); for (var i = 0; i < size; i++) { rgba[i] = (byte)(random.Next() % 256); } var colorSpace = CGColorSpace.CreateDeviceGray(); var bitmapContext = new CGBitmapContext(rgba, width, height, 8, width, colorSpace, CGImageAlphaInfo.None); _noiseImageRef = bitmapContext.ToImage(); } var context = NSGraphicsContext.CurrentContext.GraphicsPort; NSGraphicsContext.CurrentContext.SaveGraphicsState(); context.SetAlpha(opacity); context.SetBlendMode(CGBlendMode.Screen); var scaleFactor = Window.BackingScaleFactor; context.ScaleCTM(1f / scaleFactor, 1f / scaleFactor); var imageRect = new RectangleF(PointF.Empty, new SizeF(_noiseImageRef.Width, _noiseImageRef.Height)); context.DrawTiledImage(imageRect, _noiseImageRef); NSGraphicsContext.CurrentContext.RestoreGraphicsState(); }