Beispiel #1
0
        public static UIImage Blur(this UIImage image, float radius)
        {
            try
            {
                var imageToBlur = CIImage.FromCGImage(image.CGImage);

                var transform = new CIAffineClamp();
                transform.Transform = CGAffineTransform.MakeIdentity();
                transform.Image     = imageToBlur;


                var gaussianBlurFilter = new CIGaussianBlur();

                gaussianBlurFilter.Image  = transform.OutputImage;
                gaussianBlurFilter.Radius = radius;
                if (context == null)
                {
                    context = CIContext.FromOptions(null);
                }

                var resultImage = gaussianBlurFilter.OutputImage;

                var finalImage = UIImage.FromImage(
                    context.CreateCGImage(resultImage, new RectangleF(PointF.Empty, image.Size)), 1, UIImageOrientation.Up);
                return(finalImage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(image);
            }
        }
Beispiel #2
0
        CIColorControls colorCtrls; //CIFilter

        UIImage AdjustImage(UIImage image)
        {
            if (colorCtrls == null)
            {
                colorCtrls = new CIColorControls()
                {
                    Image = CIImage.FromCGImage(image.CGImage)
                }
            }
            ;
            else
            {
                colorCtrls.Image = CIImage.FromCGImage(image.CGImage);
            }

            if (context == null)
            {
                context = CIContext.FromOptions(null);
            }

            colorCtrls.Brightness = sliderB.Value;
            colorCtrls.Saturation = sliderS.Value;
            colorCtrls.Contrast   = sliderC.Value;

            using (var outputImage = colorCtrls.OutputImage) {
                var result = context.CreateCGImage(outputImage, outputImage.Extent);
                return(UIImage.FromImage(result));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a blurred version of our image in memory.
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static UIImage Blur(this UIImage image)
        {
            if (image != null)
            {
                // Create a new blurred image.
                var beginImage = new CIImage(image);
                var blur       = new CIGaussianBlur();
                blur.Image  = beginImage;
                blur.Radius = BLUR_RADIUS;

                var outputImage = blur.OutputImage;
                //var context = CIContext.FromOptions(null);
                var context = CIContext.FromOptions(new CIContextOptions()
                {
                    UseSoftwareRenderer = true
                });                                                                                         // CPU
                var cgImage  = context.CreateCGImage(outputImage, new RectangleF(new PointF(0, 0), image.Size));
                var newImage = UIImage.FromImage(cgImage);

                // Clear up some resources.
                beginImage  = null;
                context     = null;
                blur        = null;
                outputImage = null;
                cgImage     = null;

                return(newImage);
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        void HandleValueChanged(object sender, EventArgs e)
        {               // use the low-res version
            if (colorCtrls == null)
            {
                colorCtrls = new CIColorControls()
                {
                    Image = CIImage.FromCGImage(sourceImage.CGImage),
                }
            }
            ;

            colorCtrls.Brightness = sliderBrightness.Value;
            colorCtrls.Saturation = sliderSaturation.Value;
            colorCtrls.Contrast   = sliderContrast.Value;

            var output  = colorCtrls.OutputImage;
            var context = CIContext.FromOptions(null);
            var result  = context.CreateCGImage(output, output.Extent);

            imageView.Image = UIImage.FromImage(result);

            // UNCOMMENT to save the image each time it is generated
//			var someImage = imageView.Image;
//			someImage.SaveToPhotosAlbum((image, error) => {
//				// Called on completion...
//				//new UIAlertView("Saved", "Photo saved to Camera Roll", null, "OK", null).Show ();
//			    Console.WriteLine("CIColorControls image saved to Photos");
//			});
        }
    }
Beispiel #5
0
        void monovoid()
        {
            byte[]  ImageConv = new byte[0];
            var     imageData = NSData.FromArray(ImageConv);
            CIImage flower    = CIImage.FromCGImage(UIImage.LoadFromData(imageData).CGImage);
            var     sepia     = new CISepiaTone()
            {
                Image     = flower,
                Intensity = 0.9f
            };
            CIContext ctx     = CIContext.FromOptions(null);
            var       output  = sepia.OutputImage;
            var       cgImage = ctx.CreateCGImage(output, output.Extent);

            CGSize size = new CGSize(400, 400); //modify as necessary

            UIGraphics.BeginImageContext(size);
            CGRect rect = new CGRect(CGPoint.Empty, size);

            UIImage.FromImage(cgImage).Draw(rect);

            UIImage image = UIGraphics.GetImageFromCurrentImageContext();
            //NSData jpegData = image.AsJPEG(1);
            NSData jpegData = image.AsPNG();

            UIGraphics.EndImageContext();

            byte[] bt = jpegData.ToArray();
        }
Beispiel #6
0
        //public byte[] Sepia(Stream imageStream)
        //{
        //    byte[] ImageConv = new byte[0];
        //    var imageData = NSData.FromStream(imageStream);
        //    CIImage flower = CIImage.FromCGImage(UIImage.LoadFromData(imageData).CGImage);
        //    //var flower = UIImage.LoadFromData(imageData);
        //    var sepia = new CISepiaTone()
        //    {
        //        Image = flower,
        //        Intensity = .8f
        //    };
        //    CIImage output = sepia.OutputImage;
        //    var context = CIContext.FromOptions(null);
        //    var cgimage = context.CreateCGImage(output, output.Extent);

        //    //-------------------Return
        //    CGSize size = new CGSize(100, 100); //modify as necessary
        //    UIGraphics.BeginImageContext(size);
        //    CGRect rect = new CGRect(CGPoint.Empty, size);
        //    UIImage.FromImage(cgimage).Draw(rect);
        //    UIImage image = UIGraphics.GetImageFromCurrentImageContext();
        //    NSData jpegData = image.AsJPEG();
        //    UIGraphics.EndImageContext();


        //    byte[] dataBytes = new byte[jpegData.Length];
        //    System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, dataBytes, 0, Convert.ToInt32(jpegData.Length));
        //    return dataBytes;
        //}

        public byte[] Sepia(byte[] imageIos)
        {
            byte[]  ImageConv     = new byte[0];
            var     imageData     = NSData.FromArray(imageIos);
            CIImage ImageOriginal = CIImage.FromCGImage(UIImage.LoadFromData(imageData).CGImage);
            var     sepia         = new CISepiaTone()
            {
                Image     = ImageOriginal,
                Intensity = 1.0f
            };

            CIContext ctx     = CIContext.FromOptions(null);
            var       output  = sepia.OutputImage;
            var       cgImage = ctx.CreateCGImage(output, output.Extent);
            //---------
            CGSize size = new CGSize(400, 400); //Tamaño que se desplegará la imagen

            UIGraphics.BeginImageContext(size);
            CGRect rect = new CGRect(CGPoint.Empty, size);

            //UIImage.FromImage(flower).Draw(rect);
            UIImage.FromImage(cgImage).Draw(rect);

            UIImage image = UIGraphics.GetImageFromCurrentImageContext();
            //NSData jpegData = image.AsJPEG(1);
            NSData jpegData = image.AsPNG();

            UIGraphics.EndImageContext();

            byte[] iamgenEnSepia = jpegData.ToArray();

            return(iamgenEnSepia);
        }
Beispiel #7
0
        void DarkenImage()
        {
            var img       = image.Image;
            var ciImage   = new CIImage(img);
            var hueAdjust = new CIHueAdjust();   // first filter

            hueAdjust.Image = ciImage;
            hueAdjust.Angle = 2.094f;
            var sepia = new CISepiaTone();           // second filter

            sepia.Image     = hueAdjust.OutputImage; // output from last filter, input to this one
            sepia.Intensity = 0.3f;
            CIFilter color = new CIColorControls()
            { // third filter
                Saturation = 2,
                Brightness = 1,
                Contrast   = 3,
                Image      = sepia.OutputImage // output from last filter, input to this one
            };
            var output  = color.OutputImage;
            var context = CIContext.FromOptions(null);
            // ONLY when CreateCGImage is called do all the effects get rendered
            var cgimage = context.CreateCGImage(output, output.Extent);
            var ui      = UIImage.FromImage(cgimage);

            image.Image = ui;
        }
        void ApplyNoirFilter(object sender, EventArgs e)
        {
            Asset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), (input, options) => {
                // perform the editing operation, which applies a noir filter in this case
                var image = CIImage.FromUrl(input.FullSizeImageUrl);
                image     = image.CreateWithOrientation((CIImageOrientation)input.FullSizeImageOrientation);
                var noir  = new CIPhotoEffectNoir {
                    Image = image
                };
                var ciContext = CIContext.FromOptions(null);
                var output    = noir.OutputImage;

                var uiImage     = UIImage.FromImage(ciContext.CreateCGImage(output, output.Extent));
                imageView.Image = uiImage;

                // save the filtered image data to a PHContentEditingOutput instance
                var editingOutput  = new PHContentEditingOutput(input);
                var adjustmentData = new PHAdjustmentData();
                var data           = uiImage.AsJPEG();
                NSError error;
                data.Save(editingOutput.RenderedContentUrl, false, out error);
                editingOutput.AdjustmentData = adjustmentData;

                // make a change request to publish the changes form the editing output
                PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(
                    () => {
                    PHAssetChangeRequest request = PHAssetChangeRequest.ChangeRequest(Asset);
                    request.ContentEditingOutput = editingOutput;
                },
                    (ok, err) => Console.WriteLine("photo updated successfully: {0}", ok));
            });
        }
Beispiel #9
0
        void OnApplyFilter(object sender, EventArgs e)
        {
            Asset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(),
                                             (input, options) => {
                var image = CIImage.FromUrl(input.FullSizeImageUrl);
                image     = image.CreateWithOrientation(input.FullSizeImageOrientation);

                var updatedPhoto = new CIPhotoEffectNoir {
                    Image = image
                };
                var ciContext = CIContext.FromOptions(null);
                var output    = updatedPhoto.OutputImage;

                // Get the upated image
                var uiImage    = UIImage.FromImage(ciContext.CreateCGImage(output, output.Extent));
                TheImage.Image = uiImage;

                // Save the image data to a PHContentEditingOutput instance
                var editingOutput = new PHContentEditingOutput(input);

                NSError error;
                var data = uiImage.AsJPEG();
                data.Save(editingOutput.RenderedContentUrl, false, out error);
                editingOutput.AdjustmentData = new PHAdjustmentData();;

                // Request to publish the changes form the editing output back to the photo library
                PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(
                    () => {
                    PHAssetChangeRequest request = PHAssetChangeRequest.ChangeRequest(Asset);
                    request.ContentEditingOutput = editingOutput;
                },
                    (ok, err) => Console.WriteLine("Photo updated : {0}, {1}", ok, err));
            });
        }
 void InitializeImagingContext()
 {
     if (ciContext == null)
     {
         ciContext = CIContext.FromOptions(null);
     }
 }
Beispiel #11
0
        // https://gist.github.com/foxxjnm/e452f2aebc2f6a01874b
        public static UIImage Blur(this UIImage image, float blurRadius = 25f)
        {
            if (image != null)
            {
                // Create a new blurred image.
                var imageToBlur = new CIImage(image);
                var blur        = new CIGaussianBlur();
                blur.Image  = imageToBlur;
                blur.Radius = blurRadius;

                var blurImage = blur.OutputImage;
                var context   = CIContext.FromOptions(new CIContextOptions {
                    UseSoftwareRenderer = false
                });
                var cgImage  = context.CreateCGImage(blurImage, new RectangleF(0f, 0f, (float)image.Size.Width, (float)image.Size.Height));
                var newImage = UIImage.FromImage(cgImage);

                // Clean up
                imageToBlur.Dispose();
                context.Dispose();
                blur.Dispose();
                blurImage.Dispose();
                cgImage.Dispose();

                return(newImage);
            }
            return(null);
        }
Beispiel #12
0
        public void CIKernel_BasicTest()
        {
            if (!TestRuntime.CheckSystemAndSDKVersion(8, 0))
            {
                Assert.Inconclusive("Custom filters require iOS8+");
            }

            Exception ex = null;
            var       t  = new Thread(() => {
                // This code will crash if an MKMapView has been created previously on
                // the same thread, so just run it on a different thread (MKMapViews can
                // only be created on the main thread). This is obviously an Apple bug,
                // and a radar has been filed: 19249153. ObjC test case: https://github.com/rolfbjarne/CIKernelMKMapViewCrash
                try {
                    PlatformImage uiImg = new PlatformImage(NSBundle.MainBundle.PathForResource("Xam", "png", "CoreImage"));
#if MONOMAC
                    CIImage ciImg     = new CIImage(uiImg.CGImage);
                    CIContext context = new CIContext(null);
#else
                    CIImage ciImg     = new CIImage(uiImg);
                    CIContext context = CIContext.FromOptions(null);
#endif


                    foreach (CustomerFilterType type in Enum.GetValues(typeof(CustomerFilterType)))
                    {
                        MyCustomFilter filter = new MyCustomFilter(type);
                        filter.MyImage        = ciImg;

                        CIImage outputImage = filter.OutputImage;

                        CGImage cgImage = context.CreateCGImage(outputImage, outputImage.Extent);
#if MONOMAC
                        NSImage finalImg = new NSImage(cgImage, new CGSize());
#else
                        UIImage finalImg = new UIImage(cgImage);
#endif
                        Assert.IsNotNull(finalImg, "CIKernel_BasicTest should not be null");
                        Assert.IsTrue(filter.CallbackHit, "CIKernel_BasicTest callback must be hit");
                        if (filter.IsColorKernel)
                        {
                            Assert.IsTrue(filter.kernel is CIColorKernel, "CIKernel_BasicTest we disagree that it is a color kernel");
                        }
                        else
                        {
                            Assert.IsTrue(filter.kernel is CIWarpKernel, "CIKernel_BasicTest we disagree that it is a warp kernel");
                        }
                    }
                } catch (Exception ex2) {
                    ex = ex2;
                }
            });

            t.Start();
            t.Join();
            if (ex != null)
            {
                throw ex;
            }
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Setup collection view
            CollectionView.AlwaysBounceHorizontal  = true;
            CollectionView.AllowsMultipleSelection = false;
            CollectionView.AllowsSelection         = true;

            FetchAvailableFilters();

            ciContext = CIContext.FromOptions(null);

            // Add the background image and UIEffectView for the blur
            UIVisualEffectView effectView = new UIVisualEffectView(UIBlurEffect.FromStyle(UIBlurEffectStyle.Dark));

            effectView.TranslatesAutoresizingMaskIntoConstraints = false;
            View.InsertSubviewAbove(effectView, BackgroundImageView);

            var views = NSDictionary.FromObjectAndKey(effectView, new NSString("effectView"));

            View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[effectView]|",
                                                                    (NSLayoutFormatOptions)0, null, views));
            View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[effectView]|",
                                                                    (NSLayoutFormatOptions)0, null, views));
        }
        private void EditPicture()
        {
            if (clicked)
            {
                PictureView.Image = originalImage;
                clicked           = false;
            }
            else
            {
                var uiimage = PictureView.Image;
                originalImage = uiimage;
                var image = new CIImage(uiimage.CGImage);
                var originialOrientation = PictureView.Image.Orientation;

                var filter = new CIMinimumComponent()
                {
                    Image = image
                };

                var output = filter.OutputImage;

                var context = CIContext.FromOptions(null);
                var cgimage = context.CreateCGImage(output, output.Extent);

                PictureView.Image = UIImage.FromImage(cgimage, 1.0f, originialOrientation);
                clicked           = true;
            }
        }
Beispiel #15
0
        void ApplyFilters()
        {
            float hue        = hueSlider.Value;
            float saturation = saturationSlider.Value;
            float brightness = brightnessSlider.Value;

            // Apply effects to image
            CIImage result = null;

            using (var colorControlsFilter = new CIColorControls()) {
                colorControlsFilter.Image      = baseCIImage;
                colorControlsFilter.Saturation = saturation;
                colorControlsFilter.Brightness = brightness;
                result = colorControlsFilter.OutputImage;
            }

            using (var hueAdjustFilter = new CIHueAdjust()) {
                hueAdjustFilter.Image = result;
                hueAdjustFilter.Angle = hue;
                result = hueAdjustFilter.OutputImage;
            }

            using (var context = CIContext.FromOptions(null)) {
                using (CGImage cgImage = context.CreateCGImage(result, result.Extent)) {
                    if (imageView.Image != null)
                    {
                        imageView.Image.CGImage.Dispose();
                        imageView.Image.Dispose();
                    }
                    imageView.Image = UIImage.FromImage(cgImage);
                }
            }

            result.Dispose();
        }
Beispiel #16
0
        CIColorControls colorCtrls;         //CIFilter

        void HandleValueChanged(object sender, EventArgs e)
        {               // use the low-res version
            if (colorCtrls == null)
            {
                colorCtrls = new CIColorControls()
                {
                    Image = CIImage.FromCGImage(sourceImage.CGImage)
                }
            }
            ;
            else
            {
                colorCtrls.Image = CIImage.FromCGImage(sourceImage.CGImage);
            }

            if (context == null)
            {
                context = CIContext.FromOptions(null);
            }

            colorCtrls.Brightness = sliderBrightness.Value;
            colorCtrls.Saturation = sliderSaturation.Value;
            colorCtrls.Contrast   = sliderContrast.Value;

            using (var outputImage = colorCtrls.OutputImage) {
                var result = context.CreateCGImage(outputImage, outputImage.Extent);
                imageView.Image = UIImage.FromImage(result);
            }
        }
        CIContext CreateContext()
        {
            var options = new CIContextOptions {
                UseSoftwareRenderer = false
            };

            return(CIContext.FromOptions(options));
        }
Beispiel #18
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            this.Title   = "IISS";
            faceDetector = CIDetector.CreateFaceDetector(CIContext.FromOptions(null), false);
            borderImage  = UIImage.FromFile("square.png");

            UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();

            // Perform any additional setup after loading the view, typically from a nib.
        }
Beispiel #19
0
        UIImage ApplyEffect(CIFilter effect, UIImage originalImage)
        {
            var    holder     = effect.OutputImage;
            CGRect extent     = holder.Extent;
            var    context    = CIContext.FromOptions(null);
            var    cgImage    = context.CreateCGImage(holder, extent);
            var    fixedImage = UIImage.FromImage(cgImage, originalImage.CurrentScale, originalImage.Orientation);

            return(fixedImage);

            //cgImage.Dispose();
        }
        private UIImage GetUIImage(CMSampleBuffer sampleBuffer)
        {
            using (var imageBuffer = sampleBuffer.GetImageBuffer())
                using (var ciImage = new CIImage(imageBuffer))
                    using (var ciContext = CIContext.FromOptions(null))
                        using (var cgImage = ciContext.CreateCGImage(ciImage, ciImage.Extent))
                        {
                            var uiImage = new UIImage(cgImage);

                            return(uiImage);
                        }
        }
        protected override UIImage Transform(UIImage sourceBitmap, string path, ImageSource source, bool isPlaceholder, string key)
        {
            var effect = new CIPhotoEffectMono()
            {
                InputImage = sourceBitmap.CGImage
            };
            var output  = effect.OutputImage;
            var context = CIContext.FromOptions(null);

            CoreGraphics.CGImage cgimage = context.CreateCGImage(output, output.Extent);
            return(UIImage.FromImage(cgimage));
        }
 protected override UIImage Transform(UIImage sourceBitmap, string path, Work.ImageSource source, bool isPlaceholder, string key)
 {
     using (var effect = new CIPhotoEffectMono()
     {
         Image = sourceBitmap.CGImage
     })
         using (var output = effect.OutputImage)
             using (var context = CIContext.FromOptions(null))
                 using (var cgimage = context.CreateCGImage(output, output.Extent))
                 {
                     return(UIImage.FromImage(cgimage));
                 }
 }
 public static UIImage ToSepia(UIImage source)
 {
     using (var effect = new CISepiaTone()
     {
         Image = source.CGImage
     })
         using (var output = effect.OutputImage)
             using (var context = CIContext.FromOptions(null))
                 using (var cgimage = context.CreateCGImage(output, output.Extent))
                 {
                     return(UIImage.FromImage(cgimage));
                 }
 }
Beispiel #24
0
        private void InitializeImagingContext()
        {
#if MONOTOUCH
            if (ciContext == null)
            {
                ciContext = CIContext.FromOptions(null);
            }
#else
            if (ciContext == null)
            {
                ciContext = CIContext.FromContext(context);
            }
#endif
        }
Beispiel #25
0
        public UIViewController Demo(ImageFilter makeDemo)
        {
            var v         = new UIViewController();
            var imageView = new UIImageView(v.View.Bounds);

            v.View.AddSubview(imageView);

            var output  = makeDemo();
            var context = CIContext.FromOptions(null);
            var result  = context.CreateCGImage(output, output.Extent);

            imageView.Image = UIImage.FromImage(result);
            return(v);
        }
Beispiel #26
0
 public static UIImage InvertColors(this UIImage img)
 {
     using (var coreImg = new CIImage(img.CGImage))
     {
         var filter = new CIColorInvert
         {
             Image = coreImg
         };
         var output  = filter.OutputImage;
         var ctx     = CIContext.FromOptions(null);
         var cgimage = ctx.CreateCGImage(output, output.Extent);
         return(UIImage.FromImage(cgimage));
     }
 }
Beispiel #27
0
 public static UIImage ToFilter(UIImage source, CIFilter filter)
 {
     using (var context = CIContext.FromOptions(new CIContextOptions {
         UseSoftwareRenderer = false
     }))
         using (var inputImage = CIImage.FromCGImage(source.CGImage))
         {
             filter.Image = inputImage;
             using (var resultImage = context.CreateCGImage(filter.OutputImage, inputImage.Extent))
             {
                 return(new UIImage(resultImage));
             }
         }
 }
 private static UIImage CreateBlurImage(UIImage image)
 {
     using (CIImage inputImage = new CIImage(image))
         using (CIGaussianBlur blur = new CIGaussianBlur())
             using (CIContext context = CIContext.FromOptions(new CIContextOptions {
                 UseSoftwareRenderer = false
             })) {
                 blur.Image  = inputImage;
                 blur.Radius = 3;
                 using (CIImage outputImage = blur.OutputImage)
                     using (CIImage cgImage = context.CreateCGImage(outputImage, new CGRect(new CGPoint(0, 0), image.Size))) {
                         return(UIImage.FromImage(cgImage));
                     }
             }
 }
Beispiel #29
0
 public static UIImage ToBlurred(UIImage source, float rad)
 {
     using (var context = CIContext.FromOptions(new CIContextOptions {
         UseSoftwareRenderer = false
     }))
         using (var inputImage = CIImage.FromCGImage(source.CGImage))
             using (var filter = new CIGaussianBlur()
             {
                 Image = inputImage, Radius = rad
             })
                 using (var resultImage = context.CreateCGImage(filter.OutputImage, inputImage.Extent))
                 {
                     return(new UIImage(resultImage));
                 }
 }
Beispiel #30
0
        protected virtual async Task VisitFiltersWithAction()
        {
            try {
                BarButton.Enabled = false;
                ShouldStop        = false;

                using (var context = CIContext.FromOptions(null)) {
                    foreach (var filter in FilterList)
                    {
                        Title = filter.Name;

                        if (ShouldStop)
                        {
                            break;
                        }

                        var resultImageTask = Task.Factory.StartNew(() => {
                            var output = filter.Callback();

                            using (var result = context.CreateCGImage(output, output.Extent)) {
                                return(UIImage.FromImage(result));
                            }
                        });

                        var resultImage = await resultImageTask;

                        if (ShouldStop)
                        {
                            break;
                        }

                        if (ImageView.Image != null)
                        {
                            ImageView.Image.Dispose();
                        }

                        ImageView.Image = resultImage;

                        await PerformActionOnTestImage(resultImage, filter);
                    }
                }
            }
            finally {
                BarButton.Enabled = true;
            }
        }