Xamarin.iOS recipe using CIImage Filters to apply an Old Film effect
Xamarin.iOS recipe using Core Image Filters to apply an Old Film effect
This is my sample recipe for the Xamarin Recipe Cook-Off. This sample will show how to take an image with your device and apply an old film effect. To handle the picture taking code I used the Xamarin.Mobile component.
The process involves applying a Sepia Tone filter, generating random white-specks (colored noise) and blending them together.
Apply the Sepia Tone filter to the image:
var sep = new CISepiaTone()
{
Image = ciimage,
Intensity = 1F
};
Create random white-specks. This process involves generating colored noise by calling RandomGenerator() and then processing the noise so that we only get white specks calling ColorMatrix().
public CIImage ColorMatrix()
{
var rVector = new CIVector (0F, 0.1F, 0F, 0F);
var gVector = new CIVector (0F, 0.1F, 0F, 0F);
var bVector = new CIVector (0F, 0.1F, 0F, 0F);
var biasVector = new CIVector (0F, 0F, 0F, 0F);
var colorMatrix = new CIColorMatrix ()
{
Image = RandomGenerator(),
RVector = rVector,
GVector = gVector,
BVector = bVector,
BiasVector = biasVector
};
return colorMatrix.OutputImage;
}
public CIImage RandomGenerator()
{
var random = new CIRandomGenerator ();
return Crop (random);
}
Once we have the Sepia Tone and the white-specks images, we blend them using CISourceOverCompositing().
var comp = new CISourceOverCompositing () {
Image = ColorMatrix(),
BackgroundImage = sep.OutputImage
};
The final step is to render the filters into an image and display a preview on the image view.
var output = comp.OutputImage;
var context = CIContext.FromOptions(null);
img.Dispose (); // clear image
img = UIImage.FromImage (context.CreateCGImage (output, output.Extent));
imageView.Image = img;