Search code examples
c#windows-phone-8blendingnokia-imaging-sdklumia-imaging-sdk

Missing EditingSession and FilterFactory in Nokia Imaging SDK v1.2.115.0 HDR example


I am trying to use this code in my project but it appears that EditingSession and FilterFactory classes are no longer supported in SDK v1.2.115.0

Original code

WriteableBitmap toneMap1 = new WriteableBitmap(CapturedImage.PixelWidth, CapturedImage.PixelHeight);

using (EditingSession editsession = new EditingSession(image1.AsBitmap()))
using (EditingSession blendSession = new EditingSession(image1.AsBitmap()))
{
// Create the blurred version of original image
editsession.AddFilter(FilterFactory.CreateBlurFilter(BlurLevel.Blur1));

// Perform the difference between the original image and the blurred copy
editsession.AddFilter(FilterFactory.CreateBlendFilter(blendSession, BlendFunction.Difference));               

// Create the Laplacian of the original image using the emboss filter
blendSession.AddFilter(FilterFactory.CreateEmbossFilter(1.0f));

// Add the result of blur with emboss filter
editsession.AddFilter(FilterFactory.CreateBlendFilter(blendSession, BlendFunction.Add));

// Perform a gray scale as we need just informations on radiance not colours
editsession.AddFilter(FilterFactory.CreateGrayscaleFilter());

// Render the result
await editsession.RenderToWriteableBitmapAsync(toneMap1, OutputOption.PreserveAspectRatio);
}

This is what I've attempted so far

        IList<IFilter> filtersList = new List<IFilter>();

        var blurFilter = new BlurFilter()
        { 
            BlurRegionShape = BlurRegionShape.Rectangular,
            KernelSize = 10
        };


        var blendFilter = new BlendFilter()
        {
            BlendFunction = BlendFunction.Difference,                
        };

        var embossFilter = new EmbossFilter()
        {
            Level = 1.0f
        };

        var blendFilter2 = new BlendFilter()
        {
            BlendFunction = BlendFunction.Add
        };

        var grayScaleFilter = new GrayscaleFilter();

        filtersList.Add(blurFilter);
        filtersList.Add(blendFilter);
        filtersList.Add(embossFilter);
        filtersList.Add(blendFilter2);
        filtersList.Add(grayScaleFilter);


            using (var ms = new MemoryStream())
            {
                image1.SaveJpeg(ms, image1.PixelWidth, image1.PixelHeight, 0, 100);
                ms.Position = 0;
                using (var streamImageSource1 = new StreamImageSource(ms))
                using (var filterEffect1 = new FilterEffect(streamImageSource1) { Filters = filtersList })
                using (var writableBitmapRenderer1 = new WriteableBitmapRenderer(filterEffect1, toneMap1))
                {
                    toneMap1 = await writableBitmapRenderer1.RenderAsync();
                }
            }

Error is raised because the ForegroundSource of the BlendFilter is empty. The ForegroundSource should be the result of the previous filters (in this case blurFilter and embossFilter), no?

But since I can't use EditingSession and FilterFactory, how do I properly change the code to work with the updated SDK?


Solution

  • I think I figured it out. For those who are in the same boat, here's what I did

                using (var ms = new MemoryStream())
                {
                    image2.SaveJpeg(ms, image2.PixelWidth, image2.PixelHeight, 0, 100);
                    ms.Position = 0;
                    using (var oriStreamImageSource2 = new StreamImageSource(ms))
                    {
                        //Create the blurred version of original image
                        var blurEffect = new FilterEffect(oriStreamImageSource2)
                        {
                            Filters = new[] { new BlurFilter() { KernelSize = BlurLevel } }
                        };
    
                        var blendEffect1 = new FilterEffect(blurEffect)
                        {
                            Filters = new[] { new BlendFilter(oriStreamImageSource2, BlendFunction.Difference) }
                        };
    
                        //Create the Laplacian of the original image using the emboss filter
                        var embossEffect = new FilterEffect(oriStreamImageSource2)
                        {
                            Filters = new[] { new EmbossFilter(1.0f) }
                        };
    
                        //Add the result of blur with emboss filter
                        var blendEffect2 = new FilterEffect(blendEffect1)
                        {
                            Filters = new[] { new BlendFilter(embossEffect, BlendFunction.Add) }
                        };
    
                        //Perform a gray scale as we need just informations on radiance not colours
                        var grayScaleEffect = new FilterEffect(blendEffect2)
                        {
                            Filters = new[] { new GrayscaleFilter() }
                        };
    
                        using (var writableBitmapRenderer = new WriteableBitmapRenderer(grayScaleEffect, toneMap2))
                        {
                            toneMap2 = await writableBitmapRenderer.RenderAsync();
    
                            blurEffect.Dispose();
                            blendEffect1.Dispose();
                            embossEffect.Dispose();
                            blendEffect2.Dispose();
                            grayScaleEffect.Dispose();
                        }
                    };
                }