static Gdk.Pixbuf GetPixbufFromNSBitmapImageRep (NSBitmapImageRep bitmap, int width, int height) { byte[] data; using (var tiff = bitmap.TiffRepresentation) { data = new byte[tiff.Length]; System.Runtime.InteropServices.Marshal.Copy (tiff.Bytes, data, 0, data.Length); } int pw = bitmap.PixelsWide, ph = bitmap.PixelsHigh; var pixbuf = new Gdk.Pixbuf (data, pw, ph); // if one dimension matches, and the other is same or smaller, use as-is if ((pw == width && ph <= height) || (ph == height && pw <= width)) return pixbuf; // otherwise scale proportionally such that the largest dimension matches the desired size if (pw == ph) { pw = width; ph = height; } else if (pw > ph) { ph = (int) (width * ((float) ph / pw)); pw = width; } else { pw = (int) (height * ((float) pw / ph)); ph = height; } var scaled = pixbuf.ScaleSimple (pw, ph, Gdk.InterpType.Bilinear); pixbuf.Dispose (); return scaled; }
public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize? imageSize = null) { var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32); newrep.Size = imageSize ?? newsize; var graphics = NSGraphicsContext.FromBitmap(newrep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphics; graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG(); image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, DrawHints); NSGraphicsContext.GlobalRestoreGraphicsState(); return newrep; }
public static NSImage Resize(this NSImage image, sd.Size newsize, ImageInterpolation interpolation = ImageInterpolation.Default) { var newimage = new NSImage(newsize); var newrep = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32); newimage.AddRepresentation(newrep); var graphics = NSGraphicsContext.FromBitmap(newrep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphics; graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG(); image.DrawInRect(new sd.RectangleF(sd.PointF.Empty, newimage.Size), new sd.RectangleF(sd.PointF.Empty, image.Size), NSCompositingOperation.SourceOver, 1f); NSGraphicsContext.GlobalRestoreGraphicsState(); return newimage; }
public override void OutputVideoFrame(CVImageBuffer videoFrame, QTSampleBuffer sampleBuffer, QTCaptureConnection connection) { if (videoFrame == null) { throw new ArgumentNullException ("videoFrame"); } if (sampleBuffer == null) { throw new ArgumentNullException ("sampleBuffer"); } if (connection == null) { throw new ArgumentNullException ("connection"); } if (!capture) { videoFrame.Dispose (); sampleBuffer.Dispose (); return; } Console.WriteLine("Capturing photo"); capture = false; try { var imagePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "captured_image.tiff"); var image = CIImage.FromImageBuffer(videoFrame); var imageRep = new NSBitmapImageRep(image); var tiffImage = imageRep.TiffRepresentation; var bytes = new byte[tiffImage.Length]; Marshal.Copy(tiffImage.Bytes, bytes, 0, (int)tiffImage.Length); File.WriteAllBytes(imagePath, bytes); // NSError error; // tiffImage.Save(imagePath, MonoMac.Foundation.NSDataWritingOptions.FileProtectionNone, out error); Console.WriteLine("Wrote image: '{0}'", imagePath); tiffImage.Dispose(); imageRep.Dispose(); videoFrame.Dispose(); sampleBuffer.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public IMemoryGraphicsContext CreateMemoryGraphicsContext(float width, float height) { MemoryGraphicsContextWrapper wrapper = null; InvokeOnMainThread(() => { //http://stackoverflow.com/questions/10627557/mac-os-x-drawing-into-an-offscreen-nsgraphicscontext-using-cgcontextref-c-funct var bitmap = new NSBitmapImageRep(IntPtr.Zero, (int)width, (int)height, 8, 4, true, false, NSColorSpace.DeviceRGB, NSBitmapFormat.AlphaFirst, 0, 0); var context = NSGraphicsContext.FromBitmap(bitmap); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = context; wrapper = new MemoryGraphicsContextWrapper(context.GraphicsPort, bitmap, width, height, new BasicRectangle(0, 0, width, height)); }); return wrapper; }
public static NSImage Tint(this NSImage image, NSColor tint) { CIFilter colorGenerator = CIFilter.FromName("CIConstantColorGenerator"); CIColor color = CIColor.FromCGColor(tint.ToCG()); colorGenerator.SetValueForKey(color, CIFilterInputKey.Color); CIFilter colorFilter = CIFilter.FromName("CIColorControls"); colorFilter.SetValueForKey(colorGenerator.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image); colorFilter.SetValueForKey(NSNumber.FromFloat(3f), CIFilterInputKey.Saturation); colorFilter.SetValueForKey(NSNumber.FromFloat(0.35f), CIFilterInputKey.Brightness); colorFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Contrast); CIFilter monochromeFilter = CIFilter.FromName("CIColorMonochrome"); CIImage baseImage = CIImage.FromCGImage(image.CGImage); monochromeFilter.SetValueForKey(baseImage, CIFilterInputKey.Image); monochromeFilter.SetValueForKey(CIColor.FromRgb(0.75f, 0.75f, 0.75f), CIFilterInputKey.Color); monochromeFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Intensity); CIFilter compositingFilter = CIFilter.FromName("CIMultiplyCompositing"); compositingFilter.SetValueForKey(colorFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image); compositingFilter.SetValueForKey(monochromeFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.BackgroundImage); CIImage outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image); var extent = outputImage.Extent; var newsize = sd.Size.Truncate(extent.Size); var tintedImage = new NSImage(newsize); var newrep = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32); tintedImage.AddRepresentation(newrep); var graphics = NSGraphicsContext.FromBitmap(newrep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphics; var ciContext = CIContext.FromContext(graphics.GraphicsPort, new CIContextOptions { UseSoftwareRenderer = true }); ciContext.DrawImage(outputImage, extent, extent); NSGraphicsContext.GlobalRestoreGraphicsState(); newrep.Size = image.Size; return tintedImage; }
public static Gdk.Pixbuf GetPixbufFromNSImage (NSImage icon, int width, int height) { var rect = new RectangleF (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 (); } }
partial void RenderImage(NSObject sender) { var size = ImageView.Bounds.Size; int pixelWidth = (int) size.Width, pixelHeight = (int) size.Height; //var bitmapData = new char[pixelWidth * pixelHeight * PIXEL_BYTES]; // TODO: Clean up this memory var bitmapData = Marshal.AllocHGlobal(pixelWidth * pixelHeight * PIXEL_BYTES); var imageRep = new NSBitmapImageRep(bitmapData, pixelWidth, pixelHeight, SAMPLE_SIZE, SAMPLE_NUMBER, false, false, NSColorSpace.CalibratedRGB, pixelWidth * PIXEL_BYTES, 0); //var imageRep = new NSBitmapImageRep(new RectangleF(PointF.Empty, size)); var image = new NSImage(size); var film = new ColorSurfaceFilm(pixelWidth, pixelHeight, 1); var scene = new SimpleScene().CreateScene(film); scene.Render(); var imageBuffer = new NSBitmapImageRepImageBuffer(imageRep); film.Present(imageBuffer); image.AddRepresentation(imageRep); ImageView.Image = image; }
public MemoryGraphicsContextWrapper(CGContext context, NSBitmapImageRep bitmap, float boundsWidth, float boundsHeight, BasicRectangle dirtyRect) : base(context, boundsWidth, boundsHeight, dirtyRect) { _bitmap = bitmap; }
protected override Gdk.Pixbuf OnGetPixbufForFile (string filename, Gtk.IconSize size) { //this only works on MacOS 10.6.0 and greater if (systemVersion < 0x1060) return base.OnGetPixbufForFile (filename, size); NSImage icon = null; if (Path.IsPathRooted (filename) && File.Exists (filename)) { icon = NSWorkspace.SharedWorkspace.IconForFile (filename); } else { string extension = Path.GetExtension (filename); if (!string.IsNullOrEmpty (extension)) icon = NSWorkspace.SharedWorkspace.IconForFileType (extension); } if (icon == null) { return base.OnGetPixbufForFile (filename, size); } int w, h; if (!Gtk.Icon.SizeLookup (Gtk.IconSize.Menu, out w, out h)) { w = h = 22; } var rect = new System.Drawing.RectangleF (0, 0, w, h); var arep = icon.BestRepresentation (rect, null, null); if (arep == null) { return base.OnGetPixbufForFile (filename, size); } var rep = arep as NSBitmapImageRep; if (rep == null) { using (var cgi = arep.AsCGImage (rect, null, null)) rep = new NSBitmapImageRep (cgi); arep.Dispose (); } try { byte[] arr; using (var tiff = rep.TiffRepresentation) { arr = new byte[tiff.Length]; System.Runtime.InteropServices.Marshal.Copy (tiff.Bytes, arr, 0, arr.Length); } int pw = rep.PixelsWide, ph = rep.PixelsHigh; var px = new Gdk.Pixbuf (arr, pw, ph); //if one dimension matches, and the other is same or smaller, use as-is if ((pw == w && ph <= h) || (ph == h && pw <= w)) return px; //else scale proportionally such that the largest dimension matches the desired size if (pw == ph) { pw = w; ph = h; } else if (pw > ph) { ph = (int) (w * ((float) ph / pw)); pw = w; } else { pw = (int) (h * ((float) pw / ph)); ph = h; } var scaled = px.ScaleSimple (pw, ph, Gdk.InterpType.Bilinear); px.Dispose (); return scaled; } finally { if (rep != null) rep.Dispose (); } }
// Create one display list based on the given image. This assumes the image // uses 8-bit chunks to represent a sample bool MakeDisplayList (int listNum, NSImage theImage) { NSBitmapImageRep bitmap; int bytesPerRow, pixelsHigh, pixelsWide, samplesPerPixel; byte currentBit, byteValue; byte[] newBuffer; int rowIndex, colIndex; bitmap = new NSBitmapImageRep ( theImage.AsTiff (NSTiffCompression.None, 0) ); pixelsHigh = bitmap.PixelsHigh; pixelsWide = bitmap.PixelsWide; bytesPerRow = bitmap.BytesPerRow; samplesPerPixel = bitmap.SamplesPerPixel; newBuffer = new byte[(int)Math.Ceiling ((float)bytesPerRow / 8.0) * pixelsHigh]; byte[] bitmapBytesArray = new byte[(pixelsWide * pixelsHigh) * samplesPerPixel]; System.Runtime.InteropServices.Marshal.Copy (bitmap.BitmapData, bitmapBytesArray, 0, (pixelsWide * pixelsHigh) * samplesPerPixel); int curIdx = 0; /* * Convert the color bitmap into a true bitmap, ie, one bit per pixel. We * read at last row, write to first row as Cocoa and OpenGL have opposite * y origins */ for (rowIndex = pixelsHigh - 1; rowIndex >= 0; rowIndex--) { currentBit = 0x80; byteValue = 0; for (colIndex = 0; colIndex < pixelsWide; colIndex++) { if (bitmapBytesArray [rowIndex * bytesPerRow + colIndex * samplesPerPixel] > 0) byteValue |= currentBit; currentBit >>= 1; if (currentBit == 0) { newBuffer [curIdx++] = byteValue; currentBit = 0x80; byteValue = 0; } } /* * Fill out the last byte; extra is ignored by OpenGL, but each row * must start on a new byte */ if (currentBit != 0x80) newBuffer[curIdx++] = byteValue; } GL.NewList( listNum, ListMode.Compile); GL.Bitmap(pixelsWide, pixelsHigh, 0, 0, pixelsWide, 0, newBuffer); GL.EndList(); return true; }
public Task Save(CompressedBitmapFormat format, float quality, Stream target) { return Task.Run(() => { #if UIKIT var data = format == CompressedBitmapFormat.Jpeg ? inner.AsJPEG((float)quality) : inner.AsPNG(); data.AsStream().CopyTo(target); #else #if UNIFIED var rect = new CoreGraphics.CGRect(); #else var rect = new RectangleF(); #endif var cgImage = inner.AsCGImage(ref rect, null, null); var imageRep = new NSBitmapImageRep(cgImage); var props = format == CompressedBitmapFormat.Png ? new NSDictionary() : new NSDictionary(new NSNumber(quality), new NSString("NSImageCompressionFactor")); var type = format == CompressedBitmapFormat.Png ? NSBitmapImageFileType.Png : NSBitmapImageFileType.Jpeg; var outData = imageRep.RepresentationUsingTypeProperties(type, props); outData.AsStream().CopyTo(target); #endif }); }
/// <summary> /// Saves the specified color data as an image with the specified format. /// </summary> /// <param name="data">An array containing the image's color data.</param> /// <param name="width">The width of the image in pixels.</param> /// <param name="height">The height of the image in pixels.</param> /// <param name="stream">The stream to which to save the image data.</param> /// <param name="format">The format with which to save the image.</param> private void Save(Color[] data, Int32 width, Int32 height, Stream stream, ImageFormat format) { using (var rep = new NSBitmapImageRep(IntPtr.Zero, width, height, 8, 4, true, false, "NSCalibratedRGBColorSpace", 0, 0)) { fixed (Color* pData = data) { for (int y = 0; y < height; y++) { var pSrc = pData + (y * width); var pDst = (UInt32*)((Byte*)rep.BitmapData + (y * rep.BytesPerRow)); for (int x = 0; x < width; x++) { *pDst++ = (*pSrc++).ToArgb(); } } } var filetype = (format == ImageFormat.Png) ? NSBitmapImageFileType.Png : NSBitmapImageFileType.Jpeg; var properties = new NSDictionary(); using (var imgData = rep.RepresentationUsingTypeProperties(filetype, properties)) { using (var imgStream = imgData.AsStream()) { imgStream.CopyTo(stream); } } } }
public static Gdk.Pixbuf GetPixbufFromNSImageRep (NSImageRep rep, int width, int height) { var rect = new System.Drawing.RectangleF (0, 0, width, height); var bitmap = rep as NSBitmapImageRep; if (bitmap == null) { using (var cgi = rep.AsCGImage (rect, null, null)) bitmap = new NSBitmapImageRep (cgi); } try { byte[] data; using (var tiff = bitmap.TiffRepresentation) { data = new byte[tiff.Length]; System.Runtime.InteropServices.Marshal.Copy (tiff.Bytes, data, 0, data.Length); } int pw = bitmap.PixelsWide, ph = bitmap.PixelsHigh; var pixbuf = new Gdk.Pixbuf (data, pw, ph); // if one dimension matches, and the other is same or smaller, use as-is if ((pw == width && ph <= height) || (ph == height && pw <= width)) return pixbuf; // otherwise scale proportionally such that the largest dimension matches the desired size if (pw == ph) { pw = width; ph = height; } else if (pw > ph) { ph = (int) (width * ((float) ph / pw)); pw = width; } else { pw = (int) (height * ((float) pw / ph)); ph = height; } var scaled = pixbuf.ScaleSimple (pw, ph, Gdk.InterpType.Bilinear); pixbuf.Dispose (); return scaled; } finally { if (bitmap != rep) bitmap.Dispose (); } }
public NSBitmapImageRepImageBuffer(NSBitmapImageRep imageRep) { _imageRep = imageRep; }