Example #1
0
        public void EdgeDetectionEffectCustomizations()
        {
            var effect = new EdgeDetectionEffect();

            // Verify defaults.
            Assert.AreEqual(CanvasAlphaMode.Premultiplied, effect.AlphaMode);
            Assert.AreEqual(D2D1_ALPHA_MODE_PREMULTIPLIED, EffectAccessor.GetProperty(effect, 4));

            // Change the property, and verify that the boxed value changes to match.
            effect.AlphaMode = CanvasAlphaMode.Straight;
            Assert.AreEqual(D2D1_ALPHA_MODE_STRAIGHT, EffectAccessor.GetProperty(effect, 4));

            effect.AlphaMode = CanvasAlphaMode.Premultiplied;
            Assert.AreEqual(D2D1_ALPHA_MODE_PREMULTIPLIED, EffectAccessor.GetProperty(effect, 4));

            // Verify unsupported value throws.
            Assert.ThrowsException <ArgumentException>(() => { effect.AlphaMode = CanvasAlphaMode.Ignore; });
            Assert.AreEqual(CanvasAlphaMode.Premultiplied, effect.AlphaMode);

            // Validate that IGraphicsEffectD2D1Interop reports the right customizations.
            int index;
            EffectPropertyMapping mapping;

            EffectAccessor.GetNamedPropertyMapping(effect, "AlphaMode", out index, out mapping);
            Assert.AreEqual(4, index);
            Assert.AreEqual(EffectPropertyMapping.ColorMatrixAlphaMode, mapping);
        }
        public CanvasRenderTarget applyHueRotationEffects(CanvasBitmap workingBitmap)
        {
            //CanvasBitmap workingBitmap = SelectWorkingBitmap(useOriginalBitmap);

            if (workingBitmap != null)
            {
                int ww = (int)workingBitmap.SizeInPixels.Width;
                int hh = (int)workingBitmap.SizeInPixels.Height;

                TemperatureAndTintEffect temperatureAndTintEffect = new TemperatureAndTintEffect();
                temperatureAndTintEffect.Source      = workingBitmap;
                temperatureAndTintEffect.Temperature = (float)hueTemperature;
                temperatureAndTintEffect.Tint        = (float)hueTint;

                HueRotationEffect hueRotationEffect = new HueRotationEffect();
                hueRotationEffect.Angle  = (float)hueRotationAngle;
                hueRotationEffect.Source = temperatureAndTintEffect;

                PosterizeEffect     posterizeEffect     = null;
                EdgeDetectionEffect edgeDetectionEffect = null;
                if (hueDoPosterize)
                {
                    posterizeEffect                 = new PosterizeEffect();
                    posterizeEffect.Source          = hueRotationEffect;
                    posterizeEffect.RedValueCount   = huePosterizeRedCount;
                    posterizeEffect.BlueValueCount  = huePosterizeBlueCount;
                    posterizeEffect.GreenValueCount = huePosterizeGreenCount;

                    edgeDetectionEffect              = new EdgeDetectionEffect();
                    edgeDetectionEffect.Source       = posterizeEffect;
                    edgeDetectionEffect.Amount       = (float)0.9;
                    edgeDetectionEffect.BlurAmount   = 1;
                    edgeDetectionEffect.OverlayEdges = true;
                }

                //if (canvasRenderTarget != null)
                //    canvasRenderTarget.Dispose();
                CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), ww, hh, canvasBitmap.Dpi);
                using (var session = canvasRenderTarget.CreateDrawingSession())
                {
                    if (hueDoPosterize)
                    {
                        //session.DrawImage(posterizeEffect);
                        session.DrawImage(edgeDetectionEffect);
                    }
                    else
                    {
                        session.DrawImage(hueRotationEffect);
                    }
                }

                return(canvasRenderTarget);
            }

            return(null);
        }
        private static ICanvasImage CreateEdgeDetectionEffect(ICanvasImage canvasImage)
        {
            var ef = new EdgeDetectionEffect
            {
                Source     = canvasImage,
                Amount     = 0.9828464f,
                BlurAmount = 0.232849464f
            };

            return(ef);
        }
Example #4
0
        private ICanvasImage CreateEdgeDetection()
        {
            textLabel = requiresWin10;

            var edgeDetectionEffect = new EdgeDetectionEffect
            {
                Source = bitmapTiger
            };

            // Animation changes the edge detection settings.
            animationFunction = elapsedTime =>
            {
                edgeDetectionEffect.Amount     = 0.7f + (float)Math.Sin(elapsedTime * 2) * 0.3f;
                edgeDetectionEffect.BlurAmount = Math.Max((float)Math.Sin(elapsedTime * 0.7) * 2, 0);
            };

            return(edgeDetectionEffect);
        }
Example #5
0
        ICanvasImage GetSelectionBorder(ICanvasImage mask, float zoomFactor)
        {
            // Scale so our border will always be the same width no matter how the image is zoomed.
            var scaleToCurrentZoom = new ScaleEffect
            {
                Source = mask,
                Scale  = new Vector2(zoomFactor)
            };

            // Find edges of the selection.
            var detectEdges = new EdgeDetectionEffect
            {
                Source = scaleToCurrentZoom,
                Amount = 0.1f
            };

            // Colorize.
            var colorItMagenta = new ColorMatrixEffect
            {
                Source = detectEdges,

                ColorMatrix = new Matrix5x4
                {
                    M11 = 1,
                    M13 = 1,
                    M14 = 1,
                }
            };

            // Scale back to the original size.
            return(new ScaleEffect
            {
                Source = colorItMagenta,
                Scale = new Vector2(1 / zoomFactor)
            });
        }
Example #6
0
        protected override void LoadContent()
        {
            var pp = GraphicsDevice.PresentationParameters;
            var width = (int) (pp.BackBufferWidth * Settings.MapScale);
            var height = (int) (pp.BackBufferHeight * Settings.MapScale);

            #region Effects

            normalDepthMapEffect = EffectManager.Load<NormalDepthMapEffect>();
            edgeDetectionEffect = EffectManager.Load<EdgeDetectionEffect>();
            edgeDetectionEffect.SetEdgeWidth(Settings.EdgeWidth);
            edgeDetectionEffect.SetMapSize(width, height);

            #endregion

            #region Back buffers

            normalDepthMapBackBuffer = BackBufferManager.Load("NormalDepthMap");
            normalDepthMapBackBuffer.Width = width;
            normalDepthMapBackBuffer.Height = height;
            normalDepthMapBackBuffer.MipMap = false;
            normalDepthMapBackBuffer.SurfaceFormat = normalDepthMapFormat;
            normalDepthMapBackBuffer.MultiSampleCount = 0;
            normalDepthMapBackBuffer.Enabled = Enabled;

            #endregion

            base.LoadContent();
        }
        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            //using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
            //using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
            //using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
            //{

            //    var effect = new EdgeDetectionEffect
            //    {
            //        Source = inputBitmap,
            //        Amount = this.Amount
            //    };

            //    ds.DrawImage(effect);
            //}

            // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame

            // If we're on GPU, use InputFrame.Direct3DSurface
            if (context.InputFrame.SoftwareBitmap == null)
            {
                using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
                    using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var effect = new EdgeDetectionEffect
                            {
                                Source = inputBitmap,
                                Amount = this.Amount
                            };

                            ds.DrawImage(effect);
                        }

                return;
            }

            // If we're on CPU, use InputFrame.SoftwareBitmap
            if (context.InputFrame.Direct3DSurface == null)
            {
#if DEBUG
                if (!debugOutputFlag)
                {
                    Debug.WriteLine($"PixelFormat: {context.InputFrame.SoftwareBitmap.BitmapPixelFormat}");
                    debugOutputFlag = true;
                }
#endif

                // ********** Test for using SoftwareBitmap.Convert ********* //
                //using (var inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap(
                //    _canvasDevice,
                //    SoftwareBitmap.Convert(context.InputFrame.SoftwareBitmap, context.InputFrame.SoftwareBitmap.BitmapPixelFormat))) //PixelFormat: Bgra8

                //using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                //using (var ds = renderTarget.CreateDrawingSession())
                //{
                //    var effect = new EdgeDetectionEffect
                //    {
                //        Source = inputBitmap,
                //        Amount = this.Amount
                //    };

                //    ds.DrawImage(effect);
                //}

                // ********************************************************** //

                // InputFrame's raw pixels
                byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight];
                context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer());

                using (var inputBitmap = CanvasBitmap.CreateFromBytes(
                           _canvasDevice,
                           inputFrameBytes,
                           context.InputFrame.SoftwareBitmap.PixelWidth,
                           context.InputFrame.SoftwareBitmap.PixelHeight,
                           context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat()))

                    using (var renderTarget = new CanvasRenderTarget(
                               _canvasDevice,
                               context.OutputFrame.SoftwareBitmap.PixelWidth,
                               context.OutputFrame.SoftwareBitmap.PixelHeight,
                               (float)context.OutputFrame.SoftwareBitmap.DpiX,
                               context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(),
                               CanvasAlphaMode.Premultiplied))
                    {
                        using (var ds = renderTarget.CreateDrawingSession())
                        {
                            var effect = new EdgeDetectionEffect
                            {
                                Source = inputBitmap,
                                Amount = this.Amount
                            };

                            ds.DrawImage(effect);
                        }
                    }
            }
        }
Example #8
0
        private void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            if (!svgSupported)
            {
                args.DrawingSession.Clear(Colors.Black);
                DrawNotSupportedMessage(args.DrawingSession, sender.Size);
                return;
            }

            Size viewportSize = new Size()
            {
                Width = 1000, Height = 1000
            };

            if (CurrentEffectType == EffectType.None)
            {
                args.DrawingSession.DrawSvg(svgDocument, viewportSize);
            }
            else if (CurrentEffectType == EffectType.EdgeDetection)
            {
                CanvasCommandList commandList = new CanvasCommandList(sender);

                using (var ds = commandList.CreateDrawingSession())
                {
                    ds.DrawSvg(svgDocument, viewportSize);
                }

                var edgeDetection = new EdgeDetectionEffect
                {
                    Source     = commandList,
                    Amount     = 1,
                    BlurAmount = 1
                };

                args.DrawingSession.DrawImage(edgeDetection);
            }

            if (pointerDrag != null)
            {
                if (CurrentShapeType == ShapeType.Rectangle)
                {
                    // Show ghost
                    args.DrawingSession.DrawRectangle(pointerDrag.GetRectangle(), Colors.Magenta);
                }
                else if (CurrentShapeType == ShapeType.Ellipse)
                {
                    var ellipse = pointerDrag.GetEllipse();
                    args.DrawingSession.DrawEllipse(ellipse.CenterX, ellipse.CenterY, ellipse.RadiusX, ellipse.RadiusY, Colors.Magenta);
                }
                else if (CurrentShapeType == ShapeType.Circle)
                {
                    var circle = pointerDrag.GetCircle();
                    args.DrawingSession.DrawCircle(circle.Center, circle.Radius, Colors.Magenta);
                    args.DrawingSession.DrawLine(circle.Center, pointerDrag.CurrentLocation.ToVector2(), Colors.Magenta);
                }
                else if (CurrentShapeType == ShapeType.Line)
                {
                    args.DrawingSession.DrawLine(pointerDrag.StartLocation.ToVector2(), pointerDrag.CurrentLocation.ToVector2(), Colors.Magenta);
                }
            }
        }
        public CanvasRenderTarget applyEdgeDetectionEffects(CanvasBitmap workingBitmap)
        {
            //CanvasBitmap workingBitmap = SelectWorkingBitmap(useOriginalBitmap);


            if (workingBitmap != null)
            {
                int ww = (int)workingBitmap.SizeInPixels.Width;
                int hh = (int)workingBitmap.SizeInPixels.Height;

                //GrayscaleEffect grayscaleEffect = new GrayscaleEffect();
                //grayscaleEffect.Source=canvasBitmap;

                ContrastEffect contrastEffect = new ContrastEffect();
                contrastEffect.Contrast = (float)edgeDetectionContrast;
                contrastEffect.Source   = workingBitmap;

                ExposureEffect exposureEffect = new ExposureEffect();
                exposureEffect.Source   = contrastEffect;
                exposureEffect.Exposure = (float)edgeDetectionExposure;


                EdgeDetectionEffect edgeDetectionEffect = new EdgeDetectionEffect();
                edgeDetectionEffect.Source     = exposureEffect;
                edgeDetectionEffect.Amount     = (float)edgeDetectionAmount;
                edgeDetectionEffect.BlurAmount = (float)edgeDetectionBlurAmount;
                //edgeDetectionEffect.OverlayEdges = true;
                //edgeDetectionEffect.Mode = EdgeDetectionEffectMode.Prewitt;

                GrayscaleEffect grayscaleEffect = null;
                if (edgeDetectionGrayscale)
                {
                    grayscaleEffect            = new GrayscaleEffect();
                    grayscaleEffect.Source     = exposureEffect;
                    edgeDetectionEffect.Source = grayscaleEffect;
                }

                InvertEffect invertEdgeEffect = null;
                if (edgeDetectionMaskInvert)
                {
                    invertEdgeEffect        = new InvertEffect();
                    invertEdgeEffect.Source = edgeDetectionEffect;
                }



                BlendEffect blendEffect = null;
                if (edgeDetectionOverlayImage)
                {
                    OpacityEffect opacityEffect = new OpacityEffect();
                    opacityEffect.Opacity = (float)edgeDetectionOverlayOpacity;
                    opacityEffect.Source  = workingBitmap;

                    blendEffect            = new BlendEffect();
                    blendEffect.Foreground = edgeDetectionEffect;
                    blendEffect.Background = opacityEffect;
                    if (edgeDetectionMaskInvert)
                    {
                        //blendEffect.Background = invertEdgeEffect;
                        //blendEffect.Foreground = opacityEffect;

                        InvertEffect invertOrgEffect = new InvertEffect();
                        invertOrgEffect.Source = opacityEffect;
                        blendEffect.Background = invertOrgEffect;
                    }

                    blendEffect.Mode = edgeDetectionBlendEffectMode;
                }


                //if (canvasRenderTarget != null)
                //    canvasRenderTarget.Dispose();
                CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), ww, hh, canvasBitmap.Dpi);
                using (var session = canvasRenderTarget.CreateDrawingSession())
                {
                    if (edgeDetectionOverlayImage)
                    {
                        session.DrawImage(blendEffect);
                    }
                    else
                    {
                        if (edgeDetectionMaskInvert)
                        {
                            session.DrawImage(invertEdgeEffect);
                        }
                        else
                        {
                            session.DrawImage(edgeDetectionEffect);
                        }
                    }
                }

                return(canvasRenderTarget);
            }

            return(null);
        }