microsoft / Lumia-imaging-sdk

Lumia Imaging SDK is a comprehensive set of GPU/CPU imaging tools and effects that run on both mobile and desktop, with high speed and memory efficiency. Samples and extras code are included.
MIT License
75 stars 37 forks source link

Blending AutoEnhance Effect and other source throw InvalidArgumentException #15

Open Monsok opened 8 years ago

Monsok commented 8 years ago

I am trying to blend an existing image with another IImageProvider that has an Effect applied. I noticed there are are several Effects that throw InvalidArgumentException such as the Auto Enhance and Auto Levels. Many other Effects such as the Antique effect does not throw this error.

untitled-1

My code:

    . . .
SoftwareBitmapImageSource streamTextBitmapForeground = new SoftwareBitmapImageSource(normalizedTextSoftwareBitmap);

//using (SharpnessEffect sharpenText = new SharpnessEffect(streamTextBitmapForeground, SettingsPart.SharpnessLevel))
using (BlendEffect blendEffect = new BlendEffect(effectBackground, streamTextBitmapForeground, BlendFunction.Normal, 1.0f))
using (BitmapRenderer bitmapRenderer = new BitmapRenderer(blendEffect))
{

    Bitmap bitmap = await bitmapRenderer.RenderAsync();
    byte[] pixelBuffer = bitmap.Buffers[0].Buffer.ToArray();

    using (var stream = new InMemoryRandomAccessStream())
    {
        var pngEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream).AsTask().ConfigureAwait(false);

        pngEncoder.SetPixelData(BitmapPixelFormat.Bgra8,
            BitmapAlphaMode.Straight,
            (uint)bitmap.Dimensions.Width,
            (uint)bitmap.Dimensions.Height,
            displayInformation.LogicalDpi,
            displayInformation.LogicalDpi,
            pixelBuffer);

        await pngEncoder.FlushAsync().AsTask().ConfigureAwait(false);

        . . .

The error is raised at Bitmap bitmap = await bitmapRenderer.RenderAsync();

Perhaps I need to set some parameters like imagesize or something but I can't figure out what I am missing from the error message. Any ideas?

davidbozjak commented 8 years ago

I can't see a mistake in your code, however there is a known bug on GPU rendering that sometimes results itself in an error message like this. Can you try setting the RenderOptions on your renderer to CPUOnly as a test if that fixes the problem?

(In the code above it would be bitmapRenderer.RenderOptions = RenderOptions.CPUOnly right after constructing it.)

Monsok commented 8 years ago

Yes RenderOptions.CPU seems to fix the error.