public override void DrawImage(object backend, ImageDescription img, Rectangle srcRect, Rectangle destRect) { var cb = (CGContextBackend)backend; CGContext ctx = cb.Context; // Add the styles that have been globaly set to the context img.Styles = img.Styles.AddRange(cb.Styles); ctx.SaveState(); ctx.SetAlpha((float)img.Alpha); double rx = destRect.Width / srcRect.Width; double ry = destRect.Height / srcRect.Height; ctx.AddRect(new CGRect((nfloat)destRect.X, (nfloat)destRect.Y, (nfloat)destRect.Width, (nfloat)destRect.Height)); ctx.Clip(); ctx.TranslateCTM((float)(destRect.X - (srcRect.X * rx)), (float)(destRect.Y - (srcRect.Y * ry))); ctx.ScaleCTM((float)rx, (float)ry); NSImage image = (NSImage)img.Backend; if (image is CustomImage) { ((CustomImage)image).DrawInContext((CGContextBackend)backend, img); } else { var size = new CGSize((nfloat)img.Size.Width, (nfloat)img.Size.Height); var rr = new CGRect(0, 0, size.Width, size.Height); ctx.ScaleCTM(1f, -1f); ctx.DrawImage(new CGRect(0, -size.Height, size.Width, size.Height), image.AsCGImage(ref rr, NSGraphicsContext.CurrentContext, null)); } ctx.RestoreState(); }
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 static Texture2D FromStream(GraphicsDevice graphicsDevice, NSImage nsImage) { var rectangle = RectangleF.Empty; var cgImage = nsImage.AsCGImage(ref rectangle, null, null); return(PlatformFromStream(graphicsDevice, cgImage)); }
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); }
public static NSImage ResizeImage(NSImage image, CGSize maxSize) { nfloat ratio = 0; var imageWidth = image.Size.Width; var imageHeight = image.Size.Height; var maxWidth = maxSize.Width; var maxHeight = maxSize.Height; if (imageWidth > imageHeight) { ratio = maxWidth / imageWidth; } else { ratio = maxHeight / imageHeight; } var newWidth = imageWidth * ratio; var newHeight = imageHeight * ratio; var newSize = new CGSize(newWidth, newHeight); var imageRect = new CGRect(0, 0, imageWidth, imageHeight); var imageRef = image.AsCGImage(ref imageRect, null, null); return(new NSImage(imageRef, newSize)); }
public ESTexture2D(NSImage nsImage, All filter) { // TODO InitWithCGImage(nsImage,filter); CGImage image = nsImage.AsCGImage(RectangleF.Empty, null, null); InitWithCGImage(image, filter); }
void GetImagaDataFromPath (string path) { NSImage src; CGImage image; CGContext context = null; src = new NSImage (path); var rect = CGRect.Empty; image = src.AsCGImage (ref rect, null, null); width =(int)image.Width; height = (int) 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 CGRect (0, 0, width, height), image); }
public static Gdk.Pixbuf GetPixbufFromNSImage(NSImage icon, int width, int height) { var rect = new CGRect(0, 0, width, height); var rep = icon.BestRepresentation(rect, null, null); var bitmap = rep as NSBitmapImageRep; try { if (bitmap == null) { if (rep != null) { rep.Dispose(); } using (var cgi = icon.AsCGImage(ref rect, null, null)) { if (cgi == null) { return(null); } bitmap = new NSBitmapImageRep(cgi); } } return(GetPixbufFromNSBitmapImageRep(bitmap, width, height)); } finally { if (bitmap != null) { bitmap.Dispose(); } } }
public IImage ImageFromFile(string filename) { #if MONOMAC var img = new NSImage("Images/" + filename); var rect = new CGRect(CGPoint.Empty, img.Size); return(new UIKitImage(img.AsCGImage(ref rect, NSGraphicsContext.CurrentContext, new Foundation.NSDictionary()))); #else return(new UIKitImage(UIImage.FromFile("Images/" + filename).CGImage)); #endif }
public static Texture2D FromStream(GraphicsDevice graphicsDevice, NSImage nsImage) { #if PLATFORM_MACOS_LEGACY var rectangle = RectangleF.Empty; #else var rectangle = CGRect.Empty; #endif var cgImage = nsImage.AsCGImage(ref rectangle, null, null); return(PlatformFromStream(graphicsDevice, cgImage)); }
public static CGImage ToCGImage(this NSImage self) { var image = self?.CGImage; if (image == null) { var rect = new CGRect(CGPoint.Empty, self.Size); image = self.AsCGImage(ref rect, null, null); } return(image); }
internal Image GetBitmap(NSPasteboard pboard) { var nsimage = new NSImage(pboard); var cgimage = nsimage?.CGImage; if (cgimage == null) { var rect = new CGRect(0, 0, nsimage.Size.Width, nsimage.Size.Height); cgimage = nsimage.AsCGImage(ref rect, null, null); } return(cgimage?.ToBitmap()); }
public static NSData AsBmp(this NSImage target) { var representations = target.Representations(); if (representations[0] is NSBitmapImageRep rep) { return(rep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Bmp, new NSMutableDictionary())); } var rect = new CGRect(); var cgImage = target.AsCGImage(ref rect, null, null); var bitmap = new NSBitmapImageRep(cgImage); return(bitmap.RepresentationUsingTypeProperties(NSBitmapImageFileType.Bmp, new NSMutableDictionary())); }
LiteHtmlSharp.CoreGraphics.ImageHolder LoadImage(string url) { try { var nsImage = new NSImage(NSData.FromArray(DownloadResource(url))); var rect = new CGRect(new CGPoint(0, 0), nsImage.Size); var image = nsImage.AsCGImage(ref rect, null, null); return(new LiteHtmlSharp.CoreGraphics.ImageHolder { Image = image, Size = nsImage.Size }); } catch (Exception) { return(null); } }
/// <summary> /// Tos the DS bitmap. /// </summary> /// <returns>The DS bitmap.</returns> /// <param name="Image">Image.</param> public static DSBitmap ToDSBitmap(this NSImage Image) { var aMem = new MemoryStream(); CGRect rect = CGRect.Empty; var cgImage = Image.AsCGImage(ref rect, null, null); var bitmap = new NSBitmapImageRep(cgImage); bitmap.Size = Image.Size; var data = bitmap.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, null); data.AsStream().CopyTo(aMem); return(new DSBitmap(aMem.ToArray())); }
private NSData CreateRepresentation(ImageFormat format = ImageFormat.Png, float quality = 1) { var previous = NSApplication.CheckForIllegalCrossThreadCalls; NSApplication.CheckForIllegalCrossThreadCalls = false; NSBitmapImageFileType type; NSDictionary dictionary; switch (format) { case ImageFormat.Jpeg: type = NSBitmapImageFileType.Jpeg; dictionary = new NSDictionary(new NSNumber(quality), AppKitConstants.NSImageCompressionFactor); break; case ImageFormat.Tiff: type = NSBitmapImageFileType.Tiff; dictionary = new NSDictionary(); break; case ImageFormat.Gif: type = NSBitmapImageFileType.Gif; dictionary = new NSDictionary(); break; case ImageFormat.Bmp: type = NSBitmapImageFileType.Bmp; dictionary = new NSDictionary(); break; default: type = NSBitmapImageFileType.Png; dictionary = new NSDictionary(); break; } var rect = new CGRect(); var cgimage = _image.AsCGImage(ref rect, null, null); var imageRep = new NSBitmapImageRep(cgimage); var data = imageRep.RepresentationUsingTypeProperties(type, dictionary); NSApplication.CheckForIllegalCrossThreadCalls = previous; return(data); }
void LoadExamplePage() { var testWindow = new LiteHtmlNSWindow( new CGRect(0, 0, 400, 200), NSWindowStyle.Closable | NSWindowStyle.Titled | NSWindowStyle.Resizable, File.ReadAllText("master.css", Encoding.UTF8) ); testWindow.WillClose += (s, e) => NSApplication.SharedApplication.Terminate(this); testWindow.Center(); testWindow.MakeKeyAndOrderFront(this); testWindow.LiteHtmlContainer.ImportCssCallback = (url, baseUrl) => File.ReadAllText(Path.Combine("ExampleWebPage", url), Encoding.UTF8); testWindow.LiteHtmlContainer.LoadImageCallback = (url) => { var nsImage = new NSImage(NSData.FromArray(File.ReadAllBytes(Path.Combine("ExampleWebPage", url)))); var rect = new CGRect(new CGPoint(0, 0), nsImage.Size); var image = nsImage.AsCGImage(ref rect, null, null); return(new LiteHtmlSharp.CoreGraphics.ImageHolder { Image = image, Size = nsImage.Size }); }; Stopwatch stop = new Stopwatch(); stop.Start(); var htmlStr = File.ReadAllText(Path.Combine("ExampleWebPage", "index.html")); testWindow.LiteHtmlView.LoadHtml(htmlStr); Action drawnCallback = null; drawnCallback = () => { testWindow.LiteHtmlView.Drawn -= drawnCallback; stop.Stop(); var result = String.Format("Cold start took: {0} ms", stop.ElapsedMilliseconds); Console.WriteLine(result); testWindow.Title = result; }; testWindow.LiteHtmlView.Drawn += drawnCallback; }
public static Gdk.Pixbuf GetPixbufFromNSImage (NSImage icon, int width, int height) { var rect = new CGRect (0, 0, width, height); var rep = icon.BestRepresentation (rect, null, null); var bitmap = rep as NSBitmapImageRep; try { if (bitmap == null) { if (rep != null) rep.Dispose (); using (var cgi = icon.AsCGImage (ref rect, null, null)) { if (cgi == null) return null; bitmap = new NSBitmapImageRep (cgi); } } return GetPixbufFromNSBitmapImageRep (bitmap, width, height); } finally { if (bitmap != null) bitmap.Dispose (); } }
public static void DrawWithColorTransform(this NSView view, Color?color, Action drawDelegate) { if (color.HasValue) { if (view.Frame.Size.Width <= 0 || view.Frame.Size.Height <= 0) { return; } // render view to image var image = new NSImage(view.Frame.Size); image.LockFocusFlipped(!view.IsFlipped); drawDelegate(); image.UnlockFocus(); // create Core image for transformation var rr = new RectangleF(0, 0, view.Frame.Size.Width, view.Frame.Size.Height); var ciImage = CIImage.FromCGImage(image.AsCGImage(ref rr, NSGraphicsContext.CurrentContext, null)); // apply color matrix var transformColor = new CIColorMatrix(); transformColor.SetDefaults(); transformColor.Image = ciImage; transformColor.RVector = new CIVector(0, (float)color.Value.Red, 0); transformColor.GVector = new CIVector((float)color.Value.Green, 0, 0); transformColor.BVector = new CIVector(0, 0, (float)color.Value.Blue); ciImage = (CIImage)transformColor.ValueForKey(new NSString("outputImage")); var ciCtx = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, null); ciCtx.DrawImage(ciImage, rr, rr); } else { drawDelegate(); } }
public void GetData <T>(int level, Rectangle?rect, T[] data, int startIndex, int elementCount) { if (data == null) { throw new ArgumentException("data cannot be null"); } if (data.Length < startIndex + elementCount) { throw new ArgumentException("The data passed has a length of " + data.Length + " but " + elementCount + " pixels have been requested."); } Rectangle r; if (rect != null) { r = rect.Value; } else { r = new Rectangle(0, 0, Width, Height); } int sz = 0; byte[] pixel = new byte[4]; int pos; IntPtr pixelOffset; // Get the Color values if ((typeof(T) == typeof(Color))) { // Load up texture into memory NSImage nsImage = NSImage.ImageNamed(this.Name); if (nsImage == null) { throw new ContentLoadException("Error loading file via UIImage: " + Name); } CGImage image = nsImage.AsCGImage(RectangleF.Empty, null, null); if (image == null) { throw new ContentLoadException("Error with CGIamge: " + Name); } int width, height, i; CGContext context = null; IntPtr imageData; CGColorSpace colorSpace; IntPtr tempData; bool hasAlpha; CGImageAlphaInfo info; CGAffineTransform transform; Size imageSize; SurfaceFormat pixelFormat; bool sizeToFit = false; info = image.AlphaInfo; hasAlpha = ((info == CGImageAlphaInfo.PremultipliedLast) || (info == CGImageAlphaInfo.PremultipliedFirst) || (info == CGImageAlphaInfo.Last) || (info == CGImageAlphaInfo.First) ? true : false); if (image.ColorSpace != null) { pixelFormat = SurfaceFormat.Color; } 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.Color: colorSpace = CGColorSpace.CreateDeviceRGB(); imageData = Marshal.AllocHGlobal(height * width * 4); context = new CGBitmapContext(imageData, width, height, 8, 4 * width, colorSpace, CGImageAlphaInfo.PremultipliedLast); colorSpace.Dispose(); break; case SurfaceFormat.Alpha8: imageData = Marshal.AllocHGlobal(height * width); context = new CGBitmapContext(imageData, 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(imageData,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(imageData); * imageData = tempData; * } */ int count = 0; // Loop through and extract the data for (int y = r.Top; y < r.Bottom; y++) { for (int x = r.Left; x < r.Right; x++) { var result = new Color(0, 0, 0, 0); switch (this.Format) { case SurfaceFormat.Color /*kTexture2DPixelFormat_RGBA8888*/: case SurfaceFormat.Dxt3: sz = 4; pos = ((y * imageSize.Width) + x) * sz; pixelOffset = new IntPtr(imageData.ToInt64() + pos); Marshal.Copy(pixelOffset, pixel, 0, 4); result.R = pixel[0]; result.G = pixel[1]; result.B = pixel[2]; result.A = pixel[3]; break; case SurfaceFormat.Bgra4444 /*kTexture2DPixelFormat_RGBA4444*/: sz = 2; pos = ((y * imageSize.Width) + x) * sz; pixelOffset = new IntPtr(imageData.ToInt64() + pos); Marshal.Copy(pixelOffset, pixel, 0, 4); result.R = pixel[0]; result.G = pixel[1]; result.B = pixel[2]; result.A = pixel[3]; break; case SurfaceFormat.Bgra5551 /*kTexture2DPixelFormat_RGB5A1*/: sz = 2; pos = ((y * imageSize.Width) + x) * sz; pixelOffset = new IntPtr(imageData.ToInt64() + pos); Marshal.Copy(pixelOffset, pixel, 0, 4); result.R = pixel[0]; result.G = pixel[1]; result.B = pixel[2]; result.A = pixel[3]; break; case SurfaceFormat.Alpha8 /*kTexture2DPixelFormat_A8*/: sz = 1; pos = ((y * imageSize.Width) + x) * sz; pixelOffset = new IntPtr(imageData.ToInt64() + pos); Marshal.Copy(pixelOffset, pixel, 0, 4); result.A = pixel[0]; break; default: throw new NotSupportedException("Texture format"); } data[((y * imageSize.Width) + x)] = (T)(object)result; count++; if (count >= elementCount) { return; } } } context.Dispose(); Marshal.FreeHGlobal(imageData); } else { throw new NotImplementedException(); } }
public IImage ImageFromFile (string filename) { #if MONOMAC var img = new NSImage ("Images/" + filename); var rect = new NativeRect (NativePoint.Empty, img.Size); return new UIKitImage (img.AsCGImage (ref rect, NSGraphicsContext.CurrentContext, new Foundation.NSDictionary ())); #else return new UIKitImage (UIImage.FromFile ("Images/" + filename).CGImage); #endif }
public static byte[] grabScreenAsPNG(CGPoint containing) { // Grab screen // Mac stuff from http://stackoverflow.com/questions/18851247/screen-capture-on-osx-using-monomac NSScreen screen = NSScreen.MainScreen; if ((containing.X >= 0) && (containing.Y >= 0)) { int idx = 0; bool found = false; while (!found && (idx < NSScreen.Screens.Length)) { if (NSScreen.Screens[idx].Frame.Contains(containing)) { found = true; screen = NSScreen.Screens[idx]; } idx++; } } //System.Drawing.RectangleF bounds = new RectangleF(0,0,screen.Frame.GetMaxX(),screen.Frame.GetMaxY()); CGRect bounds = new CGRect((float)screen.Frame.GetMinX(), (float)screen.Frame.GetMinY(), (float)screen.Frame.GetMaxX(), (float)screen.Frame.GetMaxY()); //System.Drawing.Image si2; //NSImage si2; // CGImage screenImage = MonoMac.CoreGraphics.CGImage.ScreenImage(0,bounds); screenImage = ScreenImage2(0, bounds, CGWindowListOption.All, CGWindowImageOption.Default); #pragma warning disable XS0001 // Find usages of mono todo items /*using(NSBitmapImageRep imageRep = new NSBitmapImageRep(screenImage)) * { * NSDictionary properties = NSDictionary.FromObjectAndKey(new NSNumber(1.0), new NSString("NSImageCompressionFactor")); * using(NSData tiffData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, properties)) * { * * using (var ms = new MemoryStream()) * * { * tiffData.AsStream().CopyTo(ms); * si2 = NSImage.FromStream(ms); * //si2 = System.Drawing.Image.FromStream (ms, true); * } * } * }*/ NSBitmapImageRep si2 = new NSBitmapImageRep(screenImage); int newHeight = (int)((si2.Size.Height * imgWidth) / si2.Size.Width); CGSize destSize = new CGSize(imgWidth, newHeight); NSImage resized2 = new NSImage(destSize); resized2.LockFocus(); CGRect sz = new CGRect(0, 0, imgWidth, newHeight); //si2.DrawInRect(sz, new CGRect(0, 0, si2.Size.Width, si2.Size.Height), NSCompositingOperation.SourceOver, 1); si2.DrawInRect(sz); resized2.UnlockFocus(); resized2.Size = destSize; //Bitmap resized = new Bitmap(imgWidth, newHeight, PixelFormat.Format24bppRgb); //Graphics g = Graphics.FromImage (resized); //g.DrawImage (si2, 0, 0, imgWidth, newHeight); NSBitmapImageRep newRep = new NSBitmapImageRep(resized2.AsCGImage(ref sz, null, null)); NSData pngData = newRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png); screenImage.Dispose(); byte[] result = null; result = pngData.ToArray(); //using (MemoryStream stream = new MemoryStream()) //{ // // // resized.Save(stream, ImageFormat.Png); // result = stream.ToArray(); //} return(result); }
public override void DrawImage(object backend, ImageDescription img, Rectangle srcRect, Rectangle destRect) { CGContext ctx = ((CGContextBackend)backend).Context; NSImage image = img.ToNSImage(); ctx.SaveState(); ctx.SetAlpha((float)img.Alpha); double rx = destRect.Width / srcRect.Width; double ry = destRect.Height / srcRect.Height; ctx.AddRect(new RectangleF((float)destRect.X, (float)destRect.Y, (float)destRect.Width, (float)destRect.Height)); ctx.Clip(); ctx.TranslateCTM((float)(destRect.X - (srcRect.X * rx)), (float)(destRect.Y - (srcRect.Y * ry))); ctx.ScaleCTM((float)rx, (float)ry); if (image is CustomImage) { ((CustomImage)image).DrawInContext((CGContextBackend)backend); } else { RectangleF rr = new RectangleF(0, 0, (float)image.Size.Width, image.Size.Height); ctx.ScaleCTM(1f, -1f); ctx.DrawImage(new RectangleF(0, -image.Size.Height, image.Size.Width, image.Size.Height), image.AsCGImage(ref rr, NSGraphicsContext.CurrentContext, null)); } ctx.RestoreState(); }