Matthew
Matthew

Reputation: 4056

How to add Custom Filter in Filter Explorer Sample Project from Nokia Imaging SDK

I have been researching the CustomEffectBase class on the Nokia Imaging SDK, and I've worked with adding these in the RealtimeFilterDemo sample they have, but I'd like to add an effect to their Filter Explorer Demo as well. I am not sure how to do this. I have created a custom effect using the CustomEffectBase

InvertCustomEffect.cs

public class InvertCustomEffect : CustomEffectBase
{
    public InvertCustomEffect(IImageProvider source) : base(source)
    {
    }

    protected override void OnProcess(PixelRegion sourcePixelRegion, PixelRegion targetPixelRegion)
    {
        var sourcePixels = sourcePixelRegion.ImagePixels;
        var targetPixels = targetPixelRegion.ImagePixels;

        sourcePixelRegion.ForEachRow((index, width, position) =>
        {
            for (int x = 0; x < width; ++x, ++index)
            {

                uint pixel = sourcePixels[index];
                uint blue = pixel & 0x000000ff; // blue color component
                uint green = (pixel & 0x0000ff00) >> 8; // green color component
                uint red = (pixel & 0x00ff0000) >> 16; // red color component
                uint average = (uint)(0.0722 * blue + 0.7152 * green + 0.2126 * red); // weighted average component
                uint grayscale = 0xff000000 | average | (average << 8) | (average << 16); // use average for each color component

                targetPixels[index] = ~grayscale; // use inverse grayscale
            }
        });
    }
}

which is a basic grayscale inverted effect. In the Filter Explorer project, there is a Model called FilterModel.cs in which the effect to be performed is input. The problem is, the standard effects that come with the SDK have arguments such as ints and doubles which are just parameters to adjust the effects, but the class I created which extends the CustomEffectBase requires IImageProvider source as an argument. This is where I am stuck, and I do not know how to implement this within the Filter Explorer project and progress from here?

FilterModel.cs

public class InvertGrayscaleFilterModel : FilterModel
{
    public InvertGrayscaleFilterModel()
    {
        Name = "Invert Grayscale";
    }

    [XmlIgnore]
    public override Queue<IFilter> Components
    {
        get
        {
            Queue<IFilter> components = new Queue<IFilter>();

            components.Enqueue(new FilterAppTest.Filters.InvertCustomEffect()); //error requiring IImageProvider source

            return components;
        }
    }
}

Upvotes: 2

Views: 481

Answers (1)

David Božjak
David Božjak

Reputation: 17645

You are dealing with two different things here. The components Queue you are trying to add to in FilterModel.cs is storing a collections of IFilters, in other words different filters that can be applied to an image by adding a reference to the FilterEffect's queue.

A custom effect, in your case InvertGrayscaleFilterModel, is not a Filter, it is an (custom) Effect. That means it is on the same level in the chain as FilterEffect. That means you will not be able to just combine it with other filters, you will need to add it as a new element in the top chain, somewhere before the Renderer.

You might need to rearrange the architecture of the sample somewhat to get this working.

  • You can create a new instance of InvertCustomEffect add pass the existing instance of the FilterEffect as source.
  • Or you could create a separate branch in the chain, pass CameraPreviewImageSource as source to your InvertCustomEffect and render it separately, without applying any filters.
  • The third option is to have two instances of FilterEffect, one before and one after your custom effect. That would allow you to apply some filters before applying your effect and some after.

I strongly suggest you take a look at the Nokia Imaging SDK's Core Concepts document. The chapter "The basic building blocks" contains an explenation of the chain you need to set up. It also has a chapter devoted to custom effects.

Upvotes: 1

Related Questions