G. Hazarath Reddy
G. Hazarath Reddy

Reputation: 57

How to create custom color filters by using CoreImage or Metal Performance Shaders?

I need to create color filters based on RGBA values. It's fine for me to either Core Image or Metal Performance Shaders. Core Image providing some default filters. But I need filters with RGBA color codes. Is it possible to create filters like that?

I tried this, but it's not expected result. I am expecting like Sepia Tone Filter with different colors. (Filter should apply to entire frame)

@interface CustomFilterByColor : CIFilter

@property(nonatomic, retain) CIImage *inputImage;

@end



#import "CustomFilterByColor.h"

@implementation CustomFilterByColor

-(CIColorKernel*)kernel
{
    static CIColorKernel *kernel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        kernel = [CIColorKernel kernelWithString:
                  @"kernel vec4 CustomFilter1 ( __sample s ) \
                  \n { \n if ( s.r + s.g + s.b < 0.1 ) \n \
                  { return s.rgba = vec4(1.0, 1.0, 0.0, 0.5); } \
                  \n else \n { return s.rgba; } \n }"];

    });

    return kernel;
}

-(CIImage*)outputImage
{
    return [[self kernel] applyWithExtent:_inputImage.extent arguments:@[_inputImage]];
}

It's working for me. Getting result image like this

enter image description here

But this is not expected filter. I am expecting just color filters. like

enter image description here

Hope you got my point.

Upvotes: -1

Views: 628

Answers (2)

Condy
Condy

Reputation: 1

You can try using the CIColorMonochrome filter in Harbeth.

let filter = CIColorMonochrome(color: .random)
let dest = BoxxIO.init(element: originImage, filter: filter)
ImageView.image = try? dest.output()

Upvotes: 0

G. Hazarath Reddy
G. Hazarath Reddy

Reputation: 57

I tried this, working good for me. You can give any color you want. it will give output image with that color. You can change intensity of that color also.

   UIColor *color = [UIColor blueColor];
    /*
    [UIColor colorWithRed:255.0f/255.0f
                                     green:255.0f/255.0f
                                      blue:0.0f/255.0f
                                     alpha:0.4f];;*/

    CIColor *ciColor = [CIColor colorWithCGColor:color.CGColor];


    CIImage *outputImg = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, frameImage, @"inputIntensity", [NSNumber numberWithFloat:0.6], @"inputColor", ciColor, nil].outputImage;

Upvotes: 0

Related Questions