void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e) { if (e.Info[UIImagePickerController.MediaType].ToString() == "public.image") { UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage; if (originalImage != null) { var scaledImage = originalImage.Scale(new CGSize(300, 300)); var classifier = new ImageClassifier(); var coreImage = new CIImage(scaledImage); CVPixelBuffer buffer = new CVPixelBuffer(300, 300, CVPixelFormatType.CV32ARGB); UIGraphics.BeginImageContext(new CGSize(300, 300)); CIContext context = CIContext.FromContext(UIGraphics.GetCurrentContext(), null); context.Render(coreImage, buffer); UIGraphics.EndImageContext(); var output = classifier.GetPrediction(buffer, out NSError error); imgSelected.Image = scaledImage; lblResult.Text = $"This looks like: {output.ClassLabel}"; } } imagePicker.DismissModalViewController(true); }
void InitializeImagingContext() { if (ciContext == null) { ciContext = CIContext.FromContext(context); } }
public static NSImage Tint(this NSImage image, NSColor tint) { var colorGenerator = new CIConstantColorGenerator { Color = CIColor.FromCGColor(tint.CGColor) }; var colorFilter = new CIColorControls { Image = (CIImage)colorGenerator.ValueForKey(CIFilterOutputKey.Image), Saturation = 3f, Brightness = 0.35f, Contrast = 1f }; var monochromeFilter = new CIColorMonochrome { Image = CIImage.FromCGImage(image.CGImage), Color = CIColor.FromRgb(0.75f, 0.75f, 0.75f), Intensity = 1f }; var compositingFilter = new CIMultiplyCompositing { Image = (CIImage)colorFilter.ValueForKey(CIFilterOutputKey.Image), BackgroundImage = (CIImage)monochromeFilter.ValueForKey(CIFilterOutputKey.Image) }; var outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image); var extent = outputImage.Extent; var newsize = Size.Truncate(extent.Size.ToEto()); if (newsize.IsEmpty) { return(image); } var tintedImage = new NSImage(newsize.ToNS()); tintedImage.LockFocus(); try { var graphics = NSGraphicsContext.CurrentContext.GraphicsPort; var ciContext = CIContext.FromContext(graphics, new CIContextOptions { UseSoftwareRenderer = true }); ciContext.DrawImage(outputImage, extent, extent); } finally { tintedImage.UnlockFocus(); } var newrep = tintedImage.Representations()[0]; newrep.Size = image.Size; tintedImage.Size = image.Size; return(tintedImage); }
public void Render_Colorspace() { using (var ctx = new EAGLContext(EAGLRenderingAPI.OpenGLES2)) using (var ci = CIContext.FromContext(ctx)) using (var cv = new CVPixelBuffer(1, 1, CVPixelFormatType.CV24RGB)) using (CIImage img = new CIImage(CIColor.FromRgb(0.5f, 0.5f, 0.5f))) { // that one "null allowed" was undocumented ci.Render(img, cv, RectangleF.Empty, null); } }
public void FromContext_13983() { using (var ctx = new EAGLContext(EAGLRenderingAPI.OpenGLES2)) using (var ci = CIContext.FromContext(ctx)) { Assert.NotNull(ci); if (TestRuntime.CheckXcodeVersion(7, 0)) { Assert.That(ci.WorkingColorSpace.Model, Is.EqualTo(CGColorSpaceModel.RGB), "WorkingColorSpace"); } } }
public void End() { if (_image == null) { return; } _image.UnlockFocus(); var cgImage = _image.CGImage; var ciImage = CIImage.FromCGImage(cgImage); #pragma warning disable CS0618 // Image => InputImage in Xamarin.Mac 6.6 var filter2 = new CIColorControls(); filter2.SetDefaults(); filter2.Image = ciImage; filter2.Saturation = 0.0f; ciImage = (CIImage)filter2.ValueForKey(CIOutputImage); var filter3 = new CIColorMatrix(); filter3.SetDefaults(); filter3.Image = ciImage; #pragma warning restore CS0618 var cgColor = Color.ToCG(); var components = cgColor.Components; if (components.Length >= 3) { filter3.RVector = new CIVector(0, components[0], 0); filter3.GVector = new CIVector(components[1], 0, 0); filter3.BVector = new CIVector(0, 0, components[2]); } else if (components.Length >= 1) { // grayscale filter3.RVector = new CIVector(0, components[0], 0); filter3.GVector = new CIVector(components[0], 0, 0); filter3.BVector = new CIVector(0, 0, components[0]); } filter3.AVector = new CIVector(0, 0, 0, cgColor.Alpha); ciImage = (CIImage)filter3.ValueForKey(CIOutputImage); // create separate context so we can force using the software renderer, which is more than fast enough for this var ciContext = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, new CIContextOptions { UseSoftwareRenderer = true }); ciContext.DrawImage(ciImage, new CGRect(CGPoint.Empty, _image.Size), new CGRect(0, 0, cgImage.Width, cgImage.Height)); ciImage.Dispose(); ciContext.Dispose(); filter2.Dispose(); filter3.Dispose(); }
public void FromContext_13983() { using (var ctx = new EAGLContext(EAGLRenderingAPI.OpenGLES2)) using (var ci = CIContext.FromContext(ctx)) { Assert.NotNull(ci); if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { Assert.That(ci.WorkingColorSpace.Model, Is.EqualTo(CGColorSpaceModel.RGB), "WorkingColorSpace"); } } }
public static void Colourize(NSView control, Color color, Action drawAction) { var size = control.Frame.Size; if (size.Width <= 0 || size.Height <= 0) { return; } var image = new NSImage(size); image.LockFocusFlipped(!control.IsFlipped); drawAction(); image.UnlockFocus(); var ciImage = CIImage.FromCGImage(image.CGImage); CGSize realSize; if (control.RespondsToSelector(selConvertSizeToBacking)) { realSize = control.ConvertSizeToBacking(size); } else { realSize = control.ConvertSizeToBase(size); } var filter2 = new CIColorControls(); filter2.SetDefaults(); filter2.Image = ciImage; filter2.Saturation = 0.0f; ciImage = (CIImage)filter2.ValueForKey(CIOutputImage); var filter3 = new CIColorMatrix(); filter3.SetDefaults(); filter3.Image = ciImage; filter3.RVector = new CIVector(0, color.R, 0); filter3.GVector = new CIVector(color.G, 0, 0); filter3.BVector = new CIVector(0, 0, color.B); filter3.AVector = new CIVector(0, 0, 0, color.A); ciImage = (CIImage)filter3.ValueForKey(CIOutputImage); // create separate context so we can force using the software renderer, which is more than fast enough for this var ciContext = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, new CIContextOptions { UseSoftwareRenderer = true }); ciContext.DrawImage(ciImage, new CGRect(CGPoint.Empty, size), new CGRect(CGPoint.Empty, realSize)); }
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); }
private void InitializeImagingContext() { #if MONOTOUCH if (ciContext == null) { ciContext = CIContext.FromOptions(null); } #else if (ciContext == null) { ciContext = CIContext.FromContext(context); } #endif }
public static NSData GetJpegRepresentation(this CIImage image, float compressionQuality) { if (ciContext == null) { var eaglContext = new EAGLContext(EAGLRenderingAPI.OpenGLES2); ciContext = CIContext.FromContext(eaglContext); } using (CGImage outputImageRef = ciContext.CreateCGImage(image, image.Extent)) { using (UIImage uiImage = UIImage.FromImage(outputImageRef, 1f, UIImageOrientation.Up)) { return(uiImage.AsJPEG(compressionQuality)); } } }
public static void DrawWithColorTransform(this NSView view, Color?color, Action drawDelegate) { if (color.HasValue) { var size = view.Frame.Size; if (size.Width <= 0 || size.Height <= 0) { return; } // render view to image var image = new NSImage(size); image.LockFocusFlipped(!view.IsFlipped); drawDelegate(); image.UnlockFocus(); // create Core image for transformation var ciImage = CIImage.FromCGImage(image.CGImage); CGSize displaySize; #pragma warning disable iOSAndMacApiUsageIssue if (view.RespondsToSelector(selConvertSizeToBacking)) { displaySize = view.ConvertSizeToBacking(size); } else { displaySize = view.ConvertSizeToBase(size); } #pragma warning restore iOSAndMacApiUsageIssue // 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); using (var key = new NSString("outputImage")) ciImage = (CIImage)transformColor.ValueForKey(key); var ciCtx = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, null); ciCtx.DrawImage(ciImage, new CGRect(CGPoint.Empty, size), new CGRect(CGPoint.Empty, displaySize)); } else { drawDelegate(); } }
public override void ViewDidLoad() { base.ViewDidLoad(); // Perform any additional setup after loading the view, typically from a nib. _eaglContext = new EAGLContext(EAGLRenderingAPI.OpenGLES2); _captureSession = new AVCaptureSession(); _imageView = new GLKView(); _cIContext = CIContext.FromContext(_eaglContext); InitialiseCaptureSession(); View.AddSubview(_imageView); _imageView.Context = _eaglContext; _imageView.Delegate = this; }
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(); } }
// On load, construct the CIRawFilter public override void ViewDidLoad() { base.ViewDidLoad(); var asset = Asset; if (asset == null) { return; } // Setup options to request original image. var options = new PHImageRequestOptions { Version = PHImageRequestOptionsVersion.Original, Synchronous = true }; // Request the image data and UTI type for the image. PHImageManager.DefaultManager.RequestImageData(asset, options, (imageData, dataUTI, _, __) => { if (imageData == null || dataUTI == null) { return; } // Create a CIRawFilter from original image data. // UTI type is passed in to provide the CIRawFilter with a hint about the UTI type of the Raw file. //var rawOptions = [String (kCGImageSourceTypeIdentifierHint) : dataUTI ] var rawOptions = new NSMutableDictionary(); var imageIOLibrary = Dlfcn.dlopen("/System/Library/Frameworks/ImageIO.framework/ImageIO", 0); var key = Dlfcn.GetIntPtr(imageIOLibrary, "kCGImageSourceTypeIdentifierHint"); rawOptions.LowlevelSetObject(dataUTI, key); ciRawFilter = CIFilter.CreateRawFilter(imageData, rawOptions); if (ciRawFilter == null) { return; } // Get the native size of the image produced by the CIRawFilter. var sizeValue = ciRawFilter.ValueForKey(Keys.kCIOutputNativeSizeKey) as CIVector; if (sizeValue != null) { imageNativeSize = new CGSize(sizeValue.X, sizeValue.Y); } // Record the original value of the temperature, and setup the editing slider. var tempValue = (NSNumber)ciRawFilter.ValueForKey(Keys.kCIInputNeutralTemperatureKey); if (tempValue != null) { originalTemp = tempValue.FloatValue; TempSlider.SetValue(tempValue.FloatValue, animated: false); } // Record the original value of the tint, and setup the editing slider. var tintValue = (NSNumber)ciRawFilter.ValueForKey(Keys.kCIInputNeutralTintKey); if (tintValue != null) { originalTint = tintValue.FloatValue; TintSlider.SetValue(tintValue.FloatValue, animated: false); } }); // Create EAGL context used to render the CIImage produced by the CIRawFilter to display. ImageView.Context = new EAGLContext(EAGLRenderingAPI.OpenGLES3); ciContext = CIContext.FromContext(ImageView.Context, new CIContextOptions { CIImageFormat = CIImage.FormatRGBAh }); }