public void CreateRefCount() { // Bug #7117 CIImage img = new CIImage(CIColor.FromRgb(0.5f, 0.5f, 0.5f)); Selector retainCount = new Selector("retainCount"); #if MONOMAC using (var ctx = new CIContext(null)) { #else using (var ctx = CIContext.Create()) { #endif using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 5, 5))) { int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle); Assert.AreEqual(1, rc, "CreateCGImage #a1"); } using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 32, 32), CIImage.FormatARGB8, null)) { int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle); Assert.AreEqual(1, rc, "CreateCGImage #b1"); } #if !MONOMAC // CreateCGImage returning null on mac using (var v = ctx.CreateCGImage(img, new RectangleF(0, 0, 5, 5), CIFormat.ARGB8, null)) { int rc = Messaging.int_objc_msgSend(v.Handle, retainCount.Handle); Assert.AreEqual(1, rc, "CreateCGImage #c1"); } #endif } }
/// <summary> /// Tint the icon from green to red as the usage limit is approached /// </summary> /// <param name="usage">Usage as a percentage (0...1)</param> private void UpdateIconTint(float usage) { if (StatusController == null) { return; } float red, green; if (usage > 1f) { usage = 1f; } if (usage < 0f) { usage = 0f; } if (usage <= 0.5f) { green = ColourCap; //red = ColourCap * usage * 2f; red = 0f; } else { green = ((1f - usage) * 2f) * ColourCap; red = ColourCap; } red = red / 255f; green = green / 255f; StatusController.TintIcon(CIColor.FromRgb(red, green, 0f)); }
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); }
static void HandleFalseColorFilter(CIFilter filter) { if (filter.Name != "CIFalseColor") return; var inputColor0 = new CIColor(UIColor.Red); var inputColor1 = new CIColor(UIColor.Blue); filter[new NSString("inputColor0")] = inputColor0; filter[new NSString("inputColor1")] = inputColor1; }
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 CIImage ColorMonochrome() { var inputColor = new CIColor(new CGColor(100F, 0F, 100F)); // Make it Purple R + B = Purple var monoChrome = new CIColorMonochrome() { Image = flower, Color = inputColor, Intensity = 1F, // Default 1 }; return(monoChrome.OutputImage); }
public CIImage FalseColor() { var color0 = new CIColor(new CGColor(255F, 251F, 0F)); // A Yellowish Color var color1 = new CIColor(new CGColor(51F, 0F, 255F)); // A Purplish Color var falseColor = new CIFalseColor() { Image = flower, Color0 = color0, Color1 = color1 }; return(falseColor.OutputImage); }
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 CGImage GenerateCheckerboard(CGRect frame, CIColor c0, CIColor c1) { using (var board = new CICheckerboardGenerator() { Color0 = c0, Color1 = c1, Width = (float)Math.Min(frame.Height / 2f, 10), Center = new CIVector(new nfloat[] { 0, 0 }), }) { using (var context = new CIContext(null)) { return(context.CreateCGImage(board.OutputImage, new CGRect(0, 0, frame.Width, frame.Height))); } } }
public CIImage CheckerboardGenerator() { // Color 1 var c0 = CIColor.FromRgb(1, 0, 0); var c1 = CIColor.FromRgb(0, 1, 0); var checker = new CICheckerboardGenerator() { Color0 = c0, Color1 = c1, Center = new CIVector(new float[] { 10, 10 }), // Default [80 80] Sharpness = 1F // Default 1 }; return(Crop(checker)); }
/// <summary> /// Transform a colour's [H]ue, [S]aturation and [V]Value /// </summary> /// <returns>The transformed colour</returns> /// <param name="input">Colour to transform</param> /// <param name="hue">Hue adjustment</param> /// <param name="saturation">Saturation adjustment</param> /// <param name="value">Value adjustment</param> /// <remarks>Based on: http://beesbuzz.biz/code/hsv_color_transforms.php</remarks> public static CIColor TransformHSV(this CIColor input, double hue, double saturation, double value) { double VSU = value * saturation * Math.Cos(hue * Math.PI / 180.0); double VSW = value * saturation * Math.Sin(hue * Math.PI / 180.0); double red = (.299 * value + 0.701 * VSU + 0.168 * VSW) * input.Red + (0.587 * value - 0.587 * VSU + 0.330 * VSW) * input.Green + (0.114 * value - 0.114 * VSU - 0.497 * VSW) * input.Blue; double green = (0.299 * value - 0.299 * VSU - 0.328 * VSW) * input.Red + (0.587 * value + 0.413 * VSU + 0.035 * VSW) * input.Green + (0.114 * value - 0.114 * VSU + 0.292 * VSW) * input.Blue; double blue = (0.299 * value - 0.3 * VSU + 0.125 * VSW) * input.Red + (0.587 * value - 0.588 * VSU - 1.05 * VSW) * input.Green + (0.114 * value + 0.886 * VSU - 0.203 * VSW) * input.Blue; if (red > 1.0) { red = 1.0; } if (red < 0.0) { red = 0.0; } if (green > 1.0) { green = 1.0; } if (green < 0.0) { green = 0.0; } if (blue > 1.0) { blue = 1.0; } if (blue < 0.0) { blue = 0.0; } var returnValue = CIColor.FromRgb((float)red, (float)green, (float)blue); return(returnValue); }
/// <summary> /// Tint the icon with the specified colour /// </summary> /// <param name="colour">Colour to tint icon with</param> public void TintIcon(CIColor colour) { // Use CoreImage to tint the icon var statusImage = CIImage.FromUrl(NSUrl.FromFilename(NSBundle.MainBundle.PathForResource( Path.GetFileNameWithoutExtension(HighlightImagePath), Path.GetExtension(HighlightImagePath)))); var tintImage = CIImage.ImageWithColor(colour); var filter = CIFilter.FromName("CIMultiplyCompositing"); filter.SetValueForKey(tintImage, (NSString)"inputImage"); filter.SetValueForKey(statusImage, (NSString)"inputBackgroundImage"); var processedImage = (CIImage)filter.ValueForKey((NSString)"outputImage"); var outputImage = processedImage.ToNSImage(); _statusItemView.Image = outputImage; }
public CIImage GaussianGradient() { var centerVector = new CIVector(100, 100); // Default is [150 150] var color1 = CIColor.FromRgba(1, 0, 1, 1); var color0 = CIColor.FromRgba(0, 1, 1, 1); var gaussGradient = new CIGaussianGradient() { Center = centerVector, Color0 = color0, Color1 = color1, Radius = 280f // Default is 300 }; return(Crop(gaussGradient)); }
public IEditableImage ToMonochrome() { var mono = new CIColorMonochrome { Color = CIColor.FromRgb(1, 1, 1), Intensity = 1.0f, Image = CIImage.FromCGImage(_image.CGImage) }; CIImage output = mono.OutputImage; var context = CIContext.FromOptions(null); var renderedImage = context.CreateCGImage(output, output.Extent); var monoImage = UIImage.FromImage(renderedImage, _image.CurrentScale, _image.Orientation); _image.Dispose(); _image = monoImage; monoImage = null; renderedImage.Dispose(); renderedImage = null; return(this); }
public void DoHighlight(bool show, SFXCoordinator sfxCoordinator) { if (this.HighlightObject != null) { this.IsHighlighted = show; this.HighlightObject.Hidden = !show; if (show) { var seconds = (DateTime.UtcNow - Time1970).TotalSeconds; var intensity = (float)(Math.Sin(seconds.TruncatingRemainder(1) * 3.1415 * 2.0) * 0.2); if (this.HighlightObject.Geometry?.FirstMaterial != null) { var color = new CIColor(this.highlightColor); this.HighlightObject.Geometry.FirstMaterial.Diffuse.Contents = UIColor.FromRGBA((Single)DigitExtensions.Clamp(color.Red + intensity, 0, 1), (Single)DigitExtensions.Clamp(color.Green + intensity, 0, 1), (Single)DigitExtensions.Clamp(color.Blue + intensity, 0, 1), (Single)1); } } sfxCoordinator?.CatapultDidChangeHighlight(this, show); } }
/// <summary> /// Gets called by the VideoFrameSamplerDelegate if a new image has been captured. Does the rectangle detection. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">Event arguments</param> void HandleImageCaptured(object sender, ImageCaptureEventArgs e) { // Detect the rectangles in the captured image. // Important: case CGImage to CIImage. There is an implicit cast operator from CGImage to CIImage, but if we // pass the CGImage in to FeaturesInImage(), many many (implicit) CIImage instance will be created because this // method is called very often. The garbage collector cannot keep up with that and we runn out of memory. // By casting manually and using() the CIImage, it will be disposed immediately, freeing up memory. using (CIImage inputCIImage = (CIImage)e.Image) { // Let the detector do its work on the image. var rectangles = detector.FeaturesInImage(inputCIImage); // Find the biggest rectangle. Note: in my tests I have never seen that more than one rectangle would be detected, but better be prepared. nfloat maxWidth = 0f; nfloat maxHeight = 0f; CIRectangleFeature biggestRect = rectangles.Length > 0 ? (CIRectangleFeature)rectangles [0] : null; Console.WriteLine("Found " + rectangles.Length + " rectangles."); foreach (CIRectangleFeature rect in rectangles) { Console.WriteLine("Found rect: " + rect); nfloat minX = (nfloat)Math.Min(rect.TopLeft.X, rect.BottomLeft.X); nfloat minY = (nfloat)Math.Min(rect.TopLeft.Y, rect.TopRight.Y); nfloat maxX = (nfloat)Math.Min(rect.TopRight.X, rect.BottomRight.X); nfloat maxY = (nfloat)Math.Min(rect.BottomLeft.Y, rect.BottomRight.Y); if (maxX - minX > maxWidth && maxY - minY > maxHeight) { maxWidth = maxX - minX; maxHeight = maxY - minY; biggestRect = rect; } } if (biggestRect == null) { this.InvokeOnMainThread(() => { this.imageViewOverlay.Image = null; this.imageViewPerspective.Image = null; }); return; } Console.WriteLine("Highlighting: top left = " + biggestRect.TopLeft + "; top right = " + biggestRect.TopRight + "; bottom left = " + biggestRect.BottomLeft + "; bottom right = " + biggestRect.BottomRight); // We are not on the main thread here. this.InvokeOnMainThread(() => { // Adjust the overlay image to the corners of the detected rectangle with CIPerspectiveTransformWithExtent. using (var dict = new NSMutableDictionary()) { dict.Add(key: new NSString("inputExtent"), value: new CIVector(inputCIImage.Extent)); dict.Add(key: new NSString("inputTopLeft"), value: new CIVector(biggestRect.TopLeft)); dict.Add(key: new NSString("inputTopRight"), value: new CIVector(biggestRect.TopRight)); dict.Add(key: new NSString("inputBottomLeft"), value: new CIVector(biggestRect.BottomLeft)); dict.Add(key: new NSString("inputBottomRight"), value: new CIVector(biggestRect.BottomRight)); // Create a semi-transparent CIImage which will show the detected rectangle. using (var overlayCIImage = new CIImage(color: CIColor.FromRgba(red: 1.0f, green: 0f, blue: 0f, alpha: 0.5f)) // Size it to the source image. .ImageByCroppingToRect(inputCIImage.Extent) // Apply perspective distortion to the overlay rectangle to map it to the current camera picture. .CreateByFiltering("CIPerspectiveTransformWithExtent", dict) // Place overlay on the image. .CreateByCompositingOverImage(inputCIImage)) { // Must convert the CIImage into a CGImage and from there into a UIImage. // Could go directly from CIImage to UIImage but when assigning the result to a UIImageView, the ContentMode of // the image view will be ignored and no proper aspect scaling will take place. using (var ctx = CIContext.FromOptions(null)) using (CGImage convertedCGImage = ctx.CreateCGImage(overlayCIImage, overlayCIImage.Extent)) // This crashes with Xamarin.iOS //using(UIImage convertedUIImage = UIImage.FromImage(convertedCGImage, 1f, UIApplication.SharedApplication.StatusBarOrientation == UIInterfaceOrientation.LandscapeLeft ? UIImageOrientation.DownMirrored : UIImageOrientation.UpMirrored)) // This works. using (UIImage convertedUIImage = UIImage.FromImage(convertedCGImage)) { // Show converted image in UI. this.imageViewOverlay.Image = convertedUIImage; } } } // Apply a perspective correction with CIPerspectiveCorrection to the detected rectangle and display in another UIImageView. using (var dict = new NSMutableDictionary()) { dict.Add(key: new NSString("inputTopLeft"), value: new CIVector(biggestRect.TopLeft)); dict.Add(key: new NSString("inputTopRight"), value: new CIVector(biggestRect.TopRight)); dict.Add(key: new NSString("inputBottomLeft"), value: new CIVector(biggestRect.BottomLeft)); dict.Add(key: new NSString("inputBottomRight"), value: new CIVector(biggestRect.BottomRight)); // Use again CIImage -> CGImage -> UIImage to prevent scaling issues (see above). using (var perspectiveCorrectedImage = inputCIImage.CreateByFiltering("CIPerspectiveCorrection", dict)) using (var ctx = CIContext.FromOptions(null)) using (CGImage convertedCGImage = ctx.CreateCGImage(perspectiveCorrectedImage, perspectiveCorrectedImage.Extent)) using (UIImage convertedUIImage = UIImage.FromImage(convertedCGImage)) { this.imageViewPerspective.Image = convertedUIImage; } } }); } Console.WriteLine("---------------------"); }
public static CGImage GenerateCheckerboard(CGRect frame, NSColor c0, NSColor c1) { return(GenerateCheckerboard(frame, CIColor.FromCGColor(c0.CGColor), CIColor.FromCGColor(c1.CGColor))); }
// CIColor public static SKColor ToSKColor(this CIColor color) { return(UIColor.FromCIColor(color).ToSKColor()); }
public CIImage ColorMonochrome () { var inputColor = new CIColor (new CGColor (100F, 0F, 100F)); // Make it Purple R + B = Purple var monoChrome = new CIColorMonochrome () { Image = flower, Color = inputColor, Intensity = 1F, // Default 1 }; return monoChrome.OutputImage; }
public static SKColorF ToSKColorF(this CIColor color) => UIColor.FromCIColor(color).ToSKColorF();
public UIColor(CIColor CIColor) { }
public CIImage FalseColor () { var color0 = new CIColor (new CGColor (255F, 251F, 0F)); // A Yellowish Color var color1 = new CIColor (new CGColor (51F, 0F, 255F)); // A Purplish Color var falseColor = new CIFalseColor () { Image = flower, Color0 = color0, Color1 = color1 }; return falseColor.OutputImage; }