private void MainGridLoaded(object sender, RoutedEventArgs e)
        {
            m_compositor = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor;
            m_root       = m_compositor.CreateContainerVisual();
            ElementCompositionPreview.SetElementChildVisual(MainGrid, m_root);

            Size imageSize;

            m_noEffectBrush = CreateBrushFromAsset(
                "Bruno'sFamily2015 (13)-X2.jpg",
                out imageSize);
            m_imageAspectRatio = (imageSize.Width == 0 && imageSize.Height == 0) ? 1 : imageSize.Width / imageSize.Height;

            m_sprite = m_compositor.CreateSpriteVisual();
            ResizeImage(new Size(MainGrid.ActualWidth, MainGrid.ActualHeight));
            m_root.Children.InsertAtTop(m_sprite);

            // Image with alpha channel as an mask.
            var alphaMaskEffectDesc = new CompositeEffect
            {
                Mode    = CanvasComposite.DestinationIn,
                Sources =
                {
                    new CompositionEffectSourceParameter("Image"),
                    new Transform2DEffect
                    {
                        Name   = "MaskTransform",
                        Source = new CompositionEffectSourceParameter("Mask")
                    }
                }
            };

            m_alphaMaskEffectBrush = m_compositor.CreateEffectFactory(
                alphaMaskEffectDesc,
                new[] { "MaskTransform.TransformMatrix" }
                ).CreateBrush();
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Mask",
                CreateBrushFromAsset("CircleMask.png"));

            // Arithmetic operations between two images.
            var arithmeticEffectDesc = new ArithmeticCompositeEffect
            {
                Name        = "effect",
                ClampOutput = false,
                Source1     = new CompositionEffectSourceParameter("Source1"),
                Source2     = new CompositionEffectSourceParameter("Source2")
            };

            m_arithmeticEffectBrush = m_compositor.CreateEffectFactory(
                arithmeticEffectDesc,
                new[]
            {
                "effect.MultiplyAmount",
                "effect.Source1Amount",
                "effect.Source2Amount",
                "effect.Offset"
            }
                ).CreateBrush();
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source1",
                m_noEffectBrush);
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source2",
                CreateBrushFromAsset("_P2A8041.jpg"));

            // Creates a blend effect that combines two images.
            var foregroundBrush = CreateBrushFromAsset("Checkerboard_100x100.png");

            m_blendEffectBrushes = new CompositionEffectBrush[m_supportedBlendModes.Length];
            for (int i = 0; i < m_supportedBlendModes.Length; i++)
            {
                var blendEffectDesc = new BlendEffect
                {
                    Mode       = m_supportedBlendModes[i],
                    Background = new CompositionEffectSourceParameter("Background"),
                    Foreground = new CompositionEffectSourceParameter("Foreground")
                };
                m_blendEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    blendEffectDesc
                    ).CreateBrush();
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Background",
                    m_noEffectBrush);
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Foreground",
                    foregroundBrush);
            }

            // Generates an image containing a solid color.
            var colorSourceEffectDesc = new ColorSourceEffect // FloodEffect
            {
                Name = "effect"
            };

            m_colorSourceEffectBrush = m_compositor.CreateEffectFactory(
                colorSourceEffectDesc,
                new[] { "effect.Color" }
                ).CreateBrush();

            // Changes the contrast of an image.
            var contrastEffectDesc = new ContrastEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_contrastEffectBrush = m_compositor.CreateEffectFactory(
                contrastEffectDesc,
                new[] { "effect.Contrast" }
                ).CreateBrush();
            m_contrastEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Changes the exposure of an image.
            var exposureEffectDesc = new ExposureEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_exposureEffectBrush = m_compositor.CreateEffectFactory(
                exposureEffectDesc,
                new[] { "effect.Exposure" }
                ).CreateBrush();
            m_exposureEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the colors of an image by applying a per-channel gamma transfer function.
            var gammaTransferEffectDesc = new GammaTransferEffect
            {
                Name         = "effect",
                RedDisable   = false,
                GreenDisable = false,
                BlueDisable  = false,
                AlphaDisable = false,
                Source       = new CompositionEffectSourceParameter("Image")
            };

            m_gammaTransferEffectBrush = m_compositor.CreateEffectFactory(
                gammaTransferEffectDesc,
                new[]
            {
                "effect.RedAmplitude",
                "effect.RedExponent",
                "effect.RedOffset",
                "effect.GreenAmplitude",
                "effect.GreenExponent",
                "effect.GreenOffset",
                "effect.BlueAmplitude",
                "effect.BlueExponent",
                "effect.BlueOffset"
            }
                ).CreateBrush();
            m_gammaTransferEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to monochromatic gray.
            var grayscaleEffectDesc = new GrayscaleEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_grayscaleEffectBrush = m_compositor.CreateEffectFactory(
                grayscaleEffectDesc
                ).CreateBrush();
            m_grayscaleEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the color of an image by rotating its hue values.
            var hueRotationEffectDesc = new HueRotationEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_hueRotationEffectBrush = m_compositor.CreateEffectFactory(
                hueRotationEffectDesc,
                new[] { "effect.Angle" }
                ).CreateBrush();
            m_hueRotationEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Inverts the colors of an image.
            var invertEffectDesc = new InvertEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_invertEffectBrush = m_compositor.CreateEffectFactory(
                invertEffectDesc
                ).CreateBrush();
            m_invertEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the saturation of an image.
            var saturationEffectDesc = new SaturationEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_saturateEffectBrush = m_compositor.CreateEffectFactory(
                saturationEffectDesc,
                new[] { "effect.Saturation" }
                ).CreateBrush();
            m_saturateEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to sepia tones.
            var supportedAlphaModes = new[]
            {
                CanvasAlphaMode.Premultiplied,
                CanvasAlphaMode.Straight
            };

            m_sepiaEffectBrushes = new CompositionEffectBrush[supportedAlphaModes.Length];
            for (int i = 0; i < supportedAlphaModes.Length; i++)
            {
                var sepiaEffectDesc = new SepiaEffect
                {
                    Name      = "effect",
                    AlphaMode = supportedAlphaModes[i],
                    Source    = new CompositionEffectSourceParameter("Image")
                };
                m_sepiaEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    sepiaEffectDesc,
                    new[] { "effect.Intensity" }
                    ).CreateBrush();
                m_sepiaEffectBrushes[i].SetSourceParameter(
                    "Image",
                    m_noEffectBrush);
            }

            // Adjusts the temperature and/or tint of an image.
            var temperatureAndTintEffectDesc = new TemperatureAndTintEffect
            {
                Name   = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_temperatureAndTintEffectBrush = m_compositor.CreateEffectFactory(
                temperatureAndTintEffectDesc,
                new[]
            {
                "effect.Temperature",
                "effect.Tint"
            }
                ).CreateBrush();
            m_temperatureAndTintEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Applies a 2D affine transform matrix to an image.
            var transform2DEffectDesc = new Transform2DEffect
            {
                TransformMatrix = new Matrix3x2(
                    -1, 0,
                    0, 1,
                    m_sprite.Size.X, 0),
                Source = new CompositionEffectSourceParameter("Image")
            };

            m_transform2DEffectBrush = m_compositor.CreateEffectFactory(
                transform2DEffectDesc
                ).CreateBrush();
            m_transform2DEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // For simplying UI states switch, put effect parameter grids in an array
            m_effectParamsGrids = new Grid[(int)EffectType.NumEffectTypes];
            m_effectParamsGrids[(int)EffectType.NoEffect]           = null;
            m_effectParamsGrids[(int)EffectType.AlphaMask]          = AlphaMaskParams;
            m_effectParamsGrids[(int)EffectType.Arithmetic]         = ArithmeticParams;
            m_effectParamsGrids[(int)EffectType.Blend]              = BlendParams;
            m_effectParamsGrids[(int)EffectType.ColorSource]        = ColorSourceParams;
            m_effectParamsGrids[(int)EffectType.Contrast]           = ContrastParams;
            m_effectParamsGrids[(int)EffectType.Exposure]           = ExposureParams;
            m_effectParamsGrids[(int)EffectType.GammaTransfer]      = GammaTransferParams;
            m_effectParamsGrids[(int)EffectType.Grayscale]          = null;
            m_effectParamsGrids[(int)EffectType.HueRotation]        = HueRotationParams;
            m_effectParamsGrids[(int)EffectType.Invert]             = null;
            m_effectParamsGrids[(int)EffectType.Saturation]         = SaturationParams;
            m_effectParamsGrids[(int)EffectType.Sepia]              = SepiaParams;
            m_effectParamsGrids[(int)EffectType.TemperatureAndTint] = TemperatureAndTintParams;
            m_effectParamsGrids[(int)EffectType.Transform2D]        = null;

            // Same as grids
            m_effectBrushes = new CompositionBrush[(int)EffectType.NumEffectTypes];
            m_effectBrushes[(int)EffectType.NoEffect]           = m_noEffectBrush;
            m_effectBrushes[(int)EffectType.AlphaMask]          = m_alphaMaskEffectBrush;
            m_effectBrushes[(int)EffectType.Arithmetic]         = m_arithmeticEffectBrush;
            m_effectBrushes[(int)EffectType.Blend]              = m_blendEffectBrushes[m_activeBlendMode];
            m_effectBrushes[(int)EffectType.ColorSource]        = m_colorSourceEffectBrush;
            m_effectBrushes[(int)EffectType.Contrast]           = m_contrastEffectBrush;
            m_effectBrushes[(int)EffectType.Exposure]           = m_exposureEffectBrush;
            m_effectBrushes[(int)EffectType.GammaTransfer]      = m_gammaTransferEffectBrush;
            m_effectBrushes[(int)EffectType.Grayscale]          = m_grayscaleEffectBrush;
            m_effectBrushes[(int)EffectType.HueRotation]        = m_hueRotationEffectBrush;
            m_effectBrushes[(int)EffectType.Invert]             = m_invertEffectBrush;
            m_effectBrushes[(int)EffectType.Saturation]         = m_saturateEffectBrush;
            m_effectBrushes[(int)EffectType.Sepia]              = m_sepiaEffectBrushes[m_activeSepiaAlphaMode];
            m_effectBrushes[(int)EffectType.TemperatureAndTint] = m_temperatureAndTintEffectBrush;
            m_effectBrushes[(int)EffectType.Transform2D]        = m_transform2DEffectBrush;

            this.InitializeValues();
        }
Пример #2
0
        private void MainGridLoaded(object sender, RoutedEventArgs e)
        {
            m_compositor = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor;
            m_root = m_compositor.CreateContainerVisual();
            ElementCompositionPreview.SetElementChildVisual(MainGrid, m_root);

            Size imageSize;
            m_noEffectBrush = CreateBrushFromAsset(
                "Bruno'sFamily2015 (13)-X2.jpg",
                out imageSize);
            m_imageAspectRatio = (imageSize.Width == 0 && imageSize.Height == 0) ? 1 : imageSize.Width / imageSize.Height;

            m_sprite = m_compositor.CreateSpriteVisual();
            ResizeImage(new Size(MainGrid.ActualWidth, MainGrid.ActualHeight));
            m_root.Children.InsertAtTop(m_sprite);

            // Image with alpha channel as an mask.
            var alphaMaskEffectDesc = new CompositeEffect
            {
                Mode = CanvasComposite.DestinationIn,
                Sources =
                {
                    new CompositionEffectSourceParameter("Image"),
                    new Transform2DEffect
                    {
                        Name = "MaskTransform",
                        Source = new CompositionEffectSourceParameter("Mask")
                    }
                }
            };
            m_alphaMaskEffectBrush = m_compositor.CreateEffectFactory(
                alphaMaskEffectDesc,
                new[] { "MaskTransform.TransformMatrix" }
            ).CreateBrush();
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);
            m_alphaMaskEffectBrush.SetSourceParameter(
                "Mask",
                CreateBrushFromAsset("CircleMask.png"));

            // Arithmetic operations between two images.
            var arithmeticEffectDesc = new ArithmeticCompositeEffect
            {
                Name = "effect",
                ClampOutput = false,
                Source1 = new CompositionEffectSourceParameter("Source1"),
                Source2 = new CompositionEffectSourceParameter("Source2")
            };
            m_arithmeticEffectBrush = m_compositor.CreateEffectFactory(
                arithmeticEffectDesc,
                new[]
                {
                    "effect.MultiplyAmount",
                    "effect.Source1Amount",
                    "effect.Source2Amount",
                    "effect.Offset"
                }
            ).CreateBrush();
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source1",
                m_noEffectBrush);
            m_arithmeticEffectBrush.SetSourceParameter(
                "Source2",
                CreateBrushFromAsset("_P2A8041.jpg"));

            // Creates a blend effect that combines two images.
            var foregroundBrush = CreateBrushFromAsset("Checkerboard_100x100.png");
            m_blendEffectBrushes = new CompositionEffectBrush[m_supportedBlendModes.Length];
            for (int i = 0; i < m_supportedBlendModes.Length; i++)
            {
                var blendEffectDesc = new BlendEffect
                {
                    Mode = m_supportedBlendModes[i],
                    Background = new CompositionEffectSourceParameter("Background"),
                    Foreground = new CompositionEffectSourceParameter("Foreground")
                };
                m_blendEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    blendEffectDesc
                ).CreateBrush();
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Background",
                    m_noEffectBrush);
                m_blendEffectBrushes[i].SetSourceParameter(
                    "Foreground",
                    foregroundBrush);
            }

            // Generates an image containing a solid color.
            var colorSourceEffectDesc = new ColorSourceEffect // FloodEffect
            {
                Name = "effect"
            };
            m_colorSourceEffectBrush = m_compositor.CreateEffectFactory(
                colorSourceEffectDesc,
                new[] { "effect.Color" }
            ).CreateBrush();

            // Changes the contrast of an image.
            var contrastEffectDesc = new ContrastEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_contrastEffectBrush = m_compositor.CreateEffectFactory(
                contrastEffectDesc,
                new[] { "effect.Contrast" }
            ).CreateBrush();
            m_contrastEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Changes the exposure of an image.
            var exposureEffectDesc = new ExposureEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_exposureEffectBrush = m_compositor.CreateEffectFactory(
                exposureEffectDesc,
                new[] { "effect.Exposure" }
            ).CreateBrush();
            m_exposureEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the colors of an image by applying a per-channel gamma transfer function.
            var gammaTransferEffectDesc = new GammaTransferEffect
            {
                Name = "effect",
                RedDisable = false,
                GreenDisable = false,
                BlueDisable = false,
                AlphaDisable = false,
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_gammaTransferEffectBrush = m_compositor.CreateEffectFactory(
                gammaTransferEffectDesc,
                new[]
                {
                    "effect.RedAmplitude",
                    "effect.RedExponent",
                    "effect.RedOffset",
                    "effect.GreenAmplitude",
                    "effect.GreenExponent",
                    "effect.GreenOffset",
                    "effect.BlueAmplitude",
                    "effect.BlueExponent",
                    "effect.BlueOffset"
                }
            ).CreateBrush();
            m_gammaTransferEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to monochromatic gray.
            var grayscaleEffectDesc = new GrayscaleEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_grayscaleEffectBrush = m_compositor.CreateEffectFactory(
                grayscaleEffectDesc
            ).CreateBrush();
            m_grayscaleEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the color of an image by rotating its hue values.
            var hueRotationEffectDesc = new HueRotationEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_hueRotationEffectBrush = m_compositor.CreateEffectFactory(
                hueRotationEffectDesc,
                new[] { "effect.Angle" }
            ).CreateBrush();
            m_hueRotationEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Inverts the colors of an image.
            var invertEffectDesc = new InvertEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_invertEffectBrush = m_compositor.CreateEffectFactory(
                invertEffectDesc
            ).CreateBrush();
            m_invertEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Alters the saturation of an image.
            var saturationEffectDesc = new SaturationEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_saturateEffectBrush = m_compositor.CreateEffectFactory(
                saturationEffectDesc,
                new[] { "effect.Saturation" }
            ).CreateBrush();
            m_saturateEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Converts an image to sepia tones.
            var supportedAlphaModes = new[]
            {
                CanvasAlphaMode.Premultiplied,
                CanvasAlphaMode.Straight
            };
            m_sepiaEffectBrushes = new CompositionEffectBrush[supportedAlphaModes.Length];
            for (int i = 0; i < supportedAlphaModes.Length; i++)
            {
                var sepiaEffectDesc = new SepiaEffect
                {
                    Name = "effect",
                    AlphaMode = supportedAlphaModes[i],
                    Source = new CompositionEffectSourceParameter("Image")
                };
                m_sepiaEffectBrushes[i] = m_compositor.CreateEffectFactory(
                    sepiaEffectDesc,
                    new[] { "effect.Intensity" }
                ).CreateBrush();
                m_sepiaEffectBrushes[i].SetSourceParameter(
                    "Image",
                    m_noEffectBrush);
            }

            // Adjusts the temperature and/or tint of an image.
            var temperatureAndTintEffectDesc = new TemperatureAndTintEffect
            {
                Name = "effect",
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_temperatureAndTintEffectBrush = m_compositor.CreateEffectFactory(
                temperatureAndTintEffectDesc,
                new[]
                {
                    "effect.Temperature",
                    "effect.Tint"
                }
            ).CreateBrush();
            m_temperatureAndTintEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // Applies a 2D affine transform matrix to an image.
            var transform2DEffectDesc = new Transform2DEffect
            {
                TransformMatrix = new Matrix3x2(
                    -1, 0,
                    0, 1,
                    m_sprite.Size.X, 0),
                Source = new CompositionEffectSourceParameter("Image")
            };
            m_transform2DEffectBrush = m_compositor.CreateEffectFactory(
                transform2DEffectDesc
            ).CreateBrush();
            m_transform2DEffectBrush.SetSourceParameter(
                "Image",
                m_noEffectBrush);

            // For simplying UI states switch, put effect parameter grids in an array
            m_effectParamsGrids = new Grid[(int)EffectType.NumEffectTypes];
            m_effectParamsGrids[(int)EffectType.NoEffect] = null;
            m_effectParamsGrids[(int)EffectType.AlphaMask] = AlphaMaskParams;
            m_effectParamsGrids[(int)EffectType.Arithmetic] = ArithmeticParams;
            m_effectParamsGrids[(int)EffectType.Blend] = BlendParams;
            m_effectParamsGrids[(int)EffectType.ColorSource] = ColorSourceParams;
            m_effectParamsGrids[(int)EffectType.Contrast] = ContrastParams;
            m_effectParamsGrids[(int)EffectType.Exposure] = ExposureParams;
            m_effectParamsGrids[(int)EffectType.GammaTransfer] = GammaTransferParams;
            m_effectParamsGrids[(int)EffectType.Grayscale] = null;
            m_effectParamsGrids[(int)EffectType.HueRotation] = HueRotationParams;
            m_effectParamsGrids[(int)EffectType.Invert] = null;
            m_effectParamsGrids[(int)EffectType.Saturation] = SaturationParams;
            m_effectParamsGrids[(int)EffectType.Sepia] = SepiaParams;
            m_effectParamsGrids[(int)EffectType.TemperatureAndTint] = TemperatureAndTintParams;
            m_effectParamsGrids[(int)EffectType.Transform2D] = null;

            // Same as grids
            m_effectBrushes = new CompositionBrush[(int)EffectType.NumEffectTypes];
            m_effectBrushes[(int)EffectType.NoEffect] = m_noEffectBrush;
            m_effectBrushes[(int)EffectType.AlphaMask] = m_alphaMaskEffectBrush;
            m_effectBrushes[(int)EffectType.Arithmetic] = m_arithmeticEffectBrush;
            m_effectBrushes[(int)EffectType.Blend] = m_blendEffectBrushes[m_activeBlendMode];
            m_effectBrushes[(int)EffectType.ColorSource] = m_colorSourceEffectBrush;
            m_effectBrushes[(int)EffectType.Contrast] = m_contrastEffectBrush;
            m_effectBrushes[(int)EffectType.Exposure] = m_exposureEffectBrush;
            m_effectBrushes[(int)EffectType.GammaTransfer] = m_gammaTransferEffectBrush;
            m_effectBrushes[(int)EffectType.Grayscale] = m_grayscaleEffectBrush;
            m_effectBrushes[(int)EffectType.HueRotation] = m_hueRotationEffectBrush;
            m_effectBrushes[(int)EffectType.Invert] = m_invertEffectBrush;
            m_effectBrushes[(int)EffectType.Saturation] = m_saturateEffectBrush;
            m_effectBrushes[(int)EffectType.Sepia] = m_sepiaEffectBrushes[m_activeSepiaAlphaMode];
            m_effectBrushes[(int)EffectType.TemperatureAndTint] = m_temperatureAndTintEffectBrush;
            m_effectBrushes[(int)EffectType.Transform2D] = m_transform2DEffectBrush;

            this.InitializeValues();
        }