public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;

                var channels = Channels.None;
                if (ChannelA)
                {
                    channels = channels | Channels.Alpha;
                }
                if (ChannelR)
                {
                    channels = channels | Channels.Red;
                }
                if (ChannelG)
                {
                    channels = channels | Channels.Green;
                }
                if (ChannelB)
                {
                    channels = channels | Channels.Blue;
                }

                image.FilterType = FilterType.Lanczos;
                image.SelectiveBlur(Radius, Sigma, Threshold, channels);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 2
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;
                image.FilterType      = FilterType.Lanczos;
                image.AdaptiveBlur(Radius, Sigma);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (Strength == 0)
            {
                return(layer);
            }

            int  totalAlpha      = 0;
            long totalBrightness = 0;

            using (layer.UseRead())
                unsafe
                {
                    for (int y = 0; y < layer.Height; y++)
                    {
                        byte *linePtr    = layer.Data + y * layer.Stride;
                        byte *lineEndPtr = linePtr + layer.Width * 4;
                        while (linePtr < lineEndPtr)
                        {
                            int brightness = *(linePtr) * 722 + *(linePtr + 1) * 7152 + *(linePtr + 2) * 2126;
                            totalBrightness += brightness * *(linePtr + 3);
                            totalAlpha      += *(linePtr + 3);
                            linePtr         += 4;
                        }
                    }
                }
            if (totalAlpha == 0)
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                var averageBrightness = (double)totalBrightness * 100.0 / (double)totalAlpha / 255.0 / 10000.0;
                image.BackgroundColor = MagickColors.Transparent;
                double strength        = Strength / 100;
                double scaleValue      = 1 + (Brightness / averageBrightness - 1) * strength;
                double scaleSaturation = Saturation == SaturationMode.Zero ? 0 : 1;
                if (Saturation == SaturationMode.Reduce && scaleValue < 1)
                {
                    scaleSaturation = 1 - (1 - scaleValue * scaleValue) * strength;
                }
                image.Modulate(new Percentage(100 * scaleValue), new Percentage(100 * scaleSaturation), new Percentage(100));

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (Hue == 0 && Saturation == 100 && Lightness == 100)
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;
                image.Modulate(new Percentage(Lightness), new Percentage(Saturation), new Percentage(Hue * 100.0 / 180.0 + 100));

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 5
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (Angle == 0)
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;
                image.Distort(DistortMethod.ScaleRotateTranslate, new double[] { RotateX, RotateY, Angle });

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 6
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (Amplitude == 0)
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;
                image.Wave(PixelInterpolateMethod.Bilinear, Amplitude, Length);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 7
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (GammaRed == 1 && GammaGreen == 1 && GammaBlue == 1)
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;
                image.GammaCorrect(GammaRed, Channels.Red);
                image.GammaCorrect(GammaGreen, Channels.Green);
                image.GammaCorrect(GammaBlue, Channels.Blue);
                //image.GammaCorrect(GammaRed, GammaGreen, GammaBlue);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 8
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (Angle == 0)
            {
                return(layer);
            }

            var channels = Channels.None;

            if (ChannelA)
            {
                channels = channels | Channels.Alpha;
            }
            if (ChannelR)
            {
                channels = channels | Channels.Red;
            }
            if (ChannelG)
            {
                channels = channels | Channels.Green;
            }
            if (ChannelB)
            {
                channels = channels | Channels.Blue;
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;
                image.FilterType      = FilterType.Lanczos;
                image.RotationalBlur(Angle, channels);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (!(ChannelA || ChannelR || ChannelG || ChannelB) || (_Brightness == 0 && _Contrast == 0))
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;

                var channels = Channels.None;
                if (ChannelA)
                {
                    channels = channels | Channels.Alpha;
                }
                if (ChannelR)
                {
                    channels = channels | Channels.Red;
                }
                if (ChannelG)
                {
                    channels = channels | Channels.Green;
                }
                if (ChannelB)
                {
                    channels = channels | Channels.Blue;
                }

                image.BrightnessContrast(new Percentage(Brightness), new Percentage(Contrast), channels);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 10
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (!(ChannelA || ChannelR || ChannelG || ChannelB) || (BlackPoint == 0 && WhitePoint == 255 && MidPoint == 1))
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;

                var channels = Channels.None;
                if (ChannelA)
                {
                    channels = channels | Channels.Alpha;
                }
                if (ChannelR)
                {
                    channels = channels | Channels.Red;
                }
                if (ChannelG)
                {
                    channels = channels | Channels.Green;
                }
                if (ChannelB)
                {
                    channels = channels | Channels.Blue;
                }

                image.Level(new Percentage(BlackPoint), new Percentage(WhitePoint), MidPoint, channels);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 11
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            Tank tank = renderTask.Tank;

            if (!(ChannelA || ChannelR || ChannelG || ChannelB))
            {
                return(layer);
            }

            using (var image = layer.ToMagickImage())
            {
                image.BackgroundColor = MagickColors.Transparent;

                var channels = Channels.None;
                if (ChannelA)
                {
                    channels = channels | Channels.Alpha;
                }
                if (ChannelR)
                {
                    channels = channels | Channels.Red;
                }
                if (ChannelG)
                {
                    channels = channels | Channels.Green;
                }
                if (ChannelB)
                {
                    channels = channels | Channels.Blue;
                }

                image.Negate(channels);

                layer.CopyPixelsFrom(image.ToBitmapSource());
                return(layer);
            }
        }
Exemplo n.º 12
0
        public override BitmapBase Apply(RenderTask renderTask, BitmapBase layer)
        {
            var calculator = new SizeCalculator(renderTask, layer);
            Func <string, string> describe = property =>
                                             "*{0}:* {1}\n".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Layer), EggsML.Escape((string.IsNullOrEmpty(Layer.Name) ? "" : (Layer.Name + " – ")) + Layer.TypeName)) +
                                             "*{0}:* {1}\n".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Effect), EggsML.Escape((string.IsNullOrEmpty(Name) ? "" : (Name + " – ")) + TypeName)) +
                                             "*{0}:* {1}".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Property), EggsML.Escape(property));

            double ParsedWidth  = Math.Max(0, calculator.Parse(Width, describe(WidthTr(App.Translation).DisplayName))),
                   ParsedHeight = Math.Max(0, calculator.Parse(Height, describe(HeightTr(App.Translation).DisplayName))),
                   ParsedX      = calculator.Parse(X, describe(XTr(App.Translation).DisplayName)),
                   ParsedY      = calculator.Parse(Y, describe(YTr(App.Translation).DisplayName));

            Tank tank   = renderTask.Tank;
            var  pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height);

            if (ShowPixelBorders || PositionByPixels || (SizeByPixels && SizeMode2 != SizeMode2.NoChange && SizeMode2 != SizeMode2.ByPercentage))
            {
                pixels = layer.PreciseSize(PixelAlphaThreshold);
            }
            bool emptyPixels = pixels.Width <= 0 || pixels.Height <= 0;

            if (emptyPixels)
            {
                pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height);
            }

            double scaleWidth, scaleHeight;
            int    sourceWidth  = SizeByPixels ? pixels.Width : layer.Width;
            int    sourceHeight = SizeByPixels ? pixels.Height : layer.Height;

            switch (SizeMode2)
            {
            case SizeMode2.NoChange:
                scaleWidth = scaleHeight = 1;
                break;

            case SizeMode2.ByPercentage:
                scaleWidth = scaleHeight = Percentage / 100.0;
                break;

            case SizeMode2.BySizeWidthOnly:
                scaleWidth = scaleHeight = ParsedWidth / (double)sourceWidth;
                break;

            case SizeMode2.BySizeHeightOnly:
                scaleWidth = scaleHeight = ParsedHeight / (double)sourceHeight;
                break;

            case SizeMode2.BySizeFit:
                scaleWidth = scaleHeight = Math.Min(ParsedWidth / (double)sourceWidth, ParsedHeight / (double)sourceHeight);
                break;

            case SizeMode2.BySizeStretch:
                scaleWidth  = ParsedWidth / (double)sourceWidth;
                scaleHeight = ParsedHeight / (double)sourceHeight;
                break;

            default:
                throw new Exception("7924688");
            }

            if (GrowShrinkMode == GrowShrinkMode.GrowOnly)
            {
                scaleWidth  = Math.Max(1.0, scaleWidth);
                scaleHeight = Math.Max(1.0, scaleHeight);
            }
            else if (GrowShrinkMode == GrowShrinkMode.ShrinkOnly)
            {
                scaleWidth  = Math.Min(1.0, scaleWidth);
                scaleHeight = Math.Min(1.0, scaleHeight);
            }

            var anchor       = (AnchorRaw)Anchor;
            int anchorWidth  = (int)Math.Ceiling((PositionByPixels ? pixels.Width : layer.Width) * scaleWidth);
            int anchorHeight = (int)Math.Ceiling((PositionByPixels ? pixels.Height : layer.Height) * scaleHeight);
            // Location of the top left corner of the anchored rectangle
            int tgtX = (int)ParsedX - (anchor.HasFlag(AnchorRaw.Right) ? anchorWidth - 1 : anchor.HasFlag(AnchorRaw.Center) ? (anchorWidth - 1) / 2 : 0);
            int tgtY = (int)ParsedY - (anchor.HasFlag(AnchorRaw.Bottom) ? anchorHeight - 1 : anchor.HasFlag(AnchorRaw.Mid) ? (anchorHeight - 1) / 2 : 0);
            // Location of the top left corner of the whole scaled layer image
            double x       = tgtX - (PositionByPixels ? pixels.Left * scaleWidth : 0);
            double y       = tgtY - (PositionByPixels ? pixels.Top * scaleHeight : 0);
            int    offsetX = (PositionByPixels ? pixels.Left : 0);
            int    offsetY = (PositionByPixels ? pixels.Top : 0);

            if (ShowLayerBorders || ShowPixelBorders)
            {
                using (var image = layer.ToMagickImage())
                {
                    image.Settings.StrokeWidth = 1;
                    if (ShowLayerBorders)
                    {
                        image.Settings.FillColor   = ImageMagick.MagickColors.Transparent;
                        image.Settings.StrokeColor = new ImageMagick.MagickColor("aqua");
                        image.Draw(new ImageMagick.DrawableRectangle(0, 0, layer.Width - 1, layer.Height - 1));
                    }
                    if (ShowPixelBorders && !emptyPixels)
                    {
                        image.Settings.FillColor   = ImageMagick.MagickColors.Transparent;
                        image.Settings.StrokeColor = new ImageMagick.MagickColor("red");
                        image.Draw(new ImageMagick.DrawableRectangle(pixels.Left, pixels.Top, pixels.Right, pixels.Bottom));
                    }
                    layer.CopyPixelsFrom(image.ToBitmapSource());
                }
            }
            BitmapResampler.Filter filter;
            switch (Filter)
            {
            case Filter.Auto: filter = null; break;

            case Filter.Mitchell: filter = new BitmapResampler.MitchellFilter(); break;

            case Filter.Bicubic: filter = new BitmapResampler.CatmullRomFilter(); break;

            case Filter.Lanczos: filter = new BitmapResampler.LanczosFilter(); break;

            case Filter.Sinc256: filter = new BitmapResampler.LanczosFilter(8); break;

            case Filter.Sinc1024: filter = new BitmapResampler.LanczosFilter(16); break;

            default: throw new Exception("SizePosEffect.Filter 4107");
            }
            layer = BitmapResampler.SizePos(layer, scaleWidth, scaleHeight, offsetX, offsetY, tgtX, tgtY, Math.Max(layer.Width, Layer.ParentStyle.IconWidth), Math.Max(layer.Height, Layer.ParentStyle.IconHeight), filter);
            if (ShowAnchor)
            {
                using (var image = layer.ToMagickImage())
                {
                    image.Settings.StrokeWidth = 1;
                    image.Settings.StrokeColor = new ImageMagick.MagickColor(255, 255, 0, 120);
                    image.Draw(new ImageMagick.DrawableLine((int)ParsedX - 1, (int)ParsedY, (int)ParsedX + 1, (int)ParsedY));
                    image.Draw(new ImageMagick.DrawableLine((int)ParsedX, (int)ParsedY - 1, (int)ParsedX, (int)ParsedY + 1));
                    layer.CopyPixelsFrom(image.ToBitmapSource());
                }
            }
            return(layer);
        }