Example #1
0
        private void Distort(MagickImage output, PointD[] corners, MagickGeometry inputDimensions, MagickGeometry trimmedDimensions, MagickColor backgroundColor)
        {
            MagickGeometry outputDimensions = GetDimensions(output, corners, inputDimensions, trimmedDimensions);

            double[] arguments = new double[16]
            {
                corners[0].X, corners[0].Y, 0, 0,
                corners[1].X, corners[1].Y, 0, outputDimensions.Height,
                corners[2].X, corners[2].Y, outputDimensions.Width, outputDimensions.Height,
                corners[3].X, corners[3].Y, outputDimensions.Width, 0
            };

            output.VirtualPixelMethod = VirtualPixelMethod.Background;
            output.BackgroundColor    = backgroundColor;
            if (!DisableViewportCrop)
            {
                output.SetArtifact("distort:viewport", GetViewport(arguments, corners).ToString());
            }
            output.Distort(DistortMethod.Perspective, true, arguments);
            output.BorderColor = backgroundColor;
            output.Border(2);
            output.ColorFuzz = ColorFuzz;
            output.Trim();
            output.RePage();
        }
Example #2
0
        private void DistortImageWithMagnification(MagickImage input, MagickImage image)
        {
            var delX = (input.Width - _width) / 2;
            var delY = (input.Height - _height) / 2;

            SetDistortViewport(image, (int)delX, (int)delY);

            image.Distort(DistortMethod.ScaleRotateTranslate, Magnification.Value, 0);
        }
Example #3
0
 ///<inheritdoc cref="IMagick.MakeCircle(byte[])"/>
 public string MakeCircle(byte[] img)
 {
     using (var image = new MagickImage(img))
     {
         image.Format = MagickFormat.Png;
         image.Alpha(AlphaOption.Set);
         using (MagickImage copy = new MagickImage(image.Clone()))
         {
             copy.Distort(DistortMethod.DePolar, 0);
             copy.VirtualPixelMethod = VirtualPixelMethod.HorizontalTile;
             copy.BackgroundColor    = MagickColors.None;
             copy.Distort(DistortMethod.Polar, 0);
             image.Compose = CompositeOperator.DstIn;
             image.Composite(copy, CompositeOperator.CopyAlpha);
         }
         return(image.ToBase64());
     }
 }
 public void ShouldThrowAnExceptionWhenArgumentsIsEmptyAndSettingsIsNot()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentException>("arguments", () =>
         {
             image.Distort(DistortMethod.Perspective, new DistortSettings(), Array.Empty <double>());
         });
     }
 }
 public void ShouldThrowAnExceptionWhenArgumentsIsEmpty()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentException>("arguments", () =>
         {
             image.Distort(DistortMethod.Perspective, new double[] { });
         });
     }
 }
Example #6
0
 public void ShouldThrowAnExceptionWhenArgumentsIsNull()
 {
     using (IMagickImage image = new MagickImage())
     {
         ExceptionAssert.ThrowsArgumentNullException("arguments", () =>
         {
             image.Distort(DistortMethod.Perspective, (double[])null);
         });
     }
 }
Example #7
0
 public void ShouldThrowAnExceptionWhenArgumentsIsEmptyAndSettingsIsNot()
 {
     using (IMagickImage image = new MagickImage())
     {
         ExceptionAssert.ThrowsArgumentException("arguments", () =>
         {
             image.Distort(DistortMethod.Perspective, new DistortSettings(), new double[] { });
         });
     }
 }
Example #8
0
 public void ShouldThrowAnExceptionWhenSettingsIsNull()
 {
     using (IMagickImage image = new MagickImage())
     {
         ExceptionAssert.ThrowsArgumentNullException("settings", () =>
         {
             image.Distort(DistortMethod.Perspective, null, new double[] { 0 });
         });
     }
 }
 public void ShouldThrowAnExceptionWhenArgumentsIsNullAndSettingsIsNot()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentNullException>("arguments", () =>
         {
             image.Distort(DistortMethod.Perspective, new DistortSettings(), null);
         });
     }
 }
Example #10
0
        private void DistortImageWithCoords(MagickImage image)
        {
            SetDistortViewport(image, 0, 0);

            var arguments = new double[16]
            {
                _coords[0].X, _coords[0].Y, 0, 0, _coords[1].X, _coords[1].Y, _width, 0,
                _coords[2].X, _coords[2].Y, _width, _height, _coords[3].X, _coords[3].Y, 0, _height
            };

            image.Distort(DistortMethod.Perspective, arguments);
        }
Example #11
0
        private void DistortImageWithDimensions(MagickImage input, MagickImage image)
        {
            var delX = (input.Width - _width) / 2;
            var delY = (input.Height - _height) / 2;

            SetDistortViewport(image, (int)delX, (int)delY);

            var cX   = input.Width / 2;
            var cY   = input.Height / 2;
            var magX = _width / input.Width;
            var magY = _height / input.Height;

            image.Distort(DistortMethod.ScaleRotateTranslate, cX, cY, magX, magY, 0);
        }
Example #12
0
        private void DistortImageWithDimensions(MagickImage input, MagickImage image)
        {
            double delX = (input.Width - _Width) / 2;
            double delY = (input.Height - _Height) / 2;

            SetDistortViewport(image, (int)delX, (int)delY);

            double cx   = input.Width / 2;
            double cy   = input.Height / 2;
            double magX = input.Width / _Width;
            double magy = input.Height / _Height;

            image.Distort(DistortMethod.ScaleRotateTranslate, cx, cy, magX, magy, 0);
        }
Example #13
0
            public void ShouldSetAnArtifactWhenTheViewportOfTheSettingsIsNotNull()
            {
                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    DistortSettings settings = new DistortSettings()
                    {
                        Viewport = new MagickGeometry(1, 2, 300, 400),
                    };

                    image.Distort(DistortMethod.Barrel, settings, new double[] { 0, 0, 0, 0, 0 });

                    Assert.AreEqual("300x400+1+2", image.GetArtifact("distort:viewport"));
                }
            }
Example #14
0
            public void ShouldSetAnArtifactWhenTheScaleOfTheSettingsIsNotNull()
            {
                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    DistortSettings settings = new DistortSettings()
                    {
                        Scale = 5.2,
                    };

                    image.Distort(DistortMethod.Barrel, settings, new double[] { 0, 0, 0, 0, 0 });

                    Assert.AreEqual("5.2", image.GetArtifact("distort:scale"));
                }
            }
Example #15
0
            public void ShouldBeAbleToPerformPerspectiveDistortion()
            {
                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    image.BackgroundColor    = MagickColors.Cornsilk;
                    image.VirtualPixelMethod = VirtualPixelMethod.Background;
                    image.Distort(DistortMethod.Perspective, new double[] { 0, 0, 0, 0, 0, 90, 0, 90, 90, 0, 90, 25, 90, 90, 90, 65 });
                    image.Clamp();

                    ColorAssert.AreEqual(new MagickColor("#0000"), image, 1, 64);
                    ColorAssert.AreEqual(MagickColors.Cornsilk, image, 104, 50);
                    ColorAssert.AreEqual(new MagickColor("#aa4de148f9cb"), image, 66, 62);
                }
            }
Example #16
0
        private void DistortImageWithCoords(MagickImage image)
        {
            SetDistortViewport(image, 0, 0);

            double[] arguments = new double[16]
            {
                _Coords[0].X, _Coords[0].Y, 0, 0, _Coords[1].X, _Coords[1].Y, _Width, 0,
                _Coords[2].X, _Coords[2].Y, _Width, _Height, _Coords[3].X, _Coords[3].Y, 0, _Height
            };
            try {
                image.Distort(DistortMethod.Perspective, arguments);
            }
            catch
            {
            }
        }
Example #17
0
        private Bitmap DistortImage(Bitmap bitmap, IntPoint p1, IntPoint p2, IntPoint p3, IntPoint p4)
        {
            if (p1.X != 0 && p2.Y != 0 && p3.Y != 0 && p4.X != 0)
            {
                using (MagickImage image = new MagickImage(bitmap))
                {
                    if (cbNormalize.IsChecked ?? true)
                    {
                        image.Normalize();
                    }

                    if (cbAutoGamma.IsChecked ?? true)
                    {
                        image.AutoGamma();
                    }

                    if (cbAutoLevel.IsChecked ?? true)
                    {
                        image.AutoLevel();
                    }

                    image.Threshold(new Percentage(int.Parse(tbThreshold.Text)));
                    image.Distort(DistortMethod.Perspective, new double[] {
                        p1.X, p1.Y, 0, 0,
                        p2.X, p2.Y, image.Width, 0,
                        p3.X, p3.Y, 0, image.Height,
                        p4.X, p4.Y, image.Width, image.Height
                    });

                    MagickGeometry size = new MagickGeometry(561, 795)
                    {
                        IgnoreAspectRatio = true
                    };

                    image.Resize(size);
                    //image.Write("C:\\Users\\RLabonde\\Desktop\\test\\test.bmp");

                    return(image.ToBitmap());
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("No point was detected!", "No points", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return(bitmap);
            }
        }
Example #18
0
        public async Task <MagickImage> GenerateCaptionAsync(WheelTextSetting setting)
        {
            var image = new MagickImage(MagickColors.Transparent, setting.Width, setting.Height);

            await Task.Run(() =>
            {
                var captionString = "caption:" + setting.PreviewText;

                image.Settings.FillColor =
                    new MagickColor(Converters.ColorConvert.ColorFromMediaColor(setting.TextColor));

                image.Settings.StrokeColor =
                    new MagickColor(Converters.ColorConvert.ColorFromMediaColor(setting.TextStrokeColor));

                image.Settings.StrokeWidth = setting.StrokeWidth;

                image.Settings.Font = setting.FontName;

                image.Settings.TextGravity = (Gravity)Enum.Parse(typeof(Gravity), setting.Gravity);

                image.Read(captionString);

                if (setting.ArcAmount > 0)
                {
                    image.Distort(DistortMethod.Arc, setting.ArcAmount);
                }

                if (setting.ShadeOn)
                {
                    image.Shade(setting.ShadeAzimuth, setting.ShadeElevation, false);
                }

                var shadowColor = new MagickColor(Converters.ColorConvert.ColorFromMediaColor(setting.ShadowColor));
                image.Shadow(setting.ShadowX, setting.ShadowY, setting.ShadowSigma,
                             new Percentage(setting.ShadowPercentage), new MagickColor(shadowColor));

                //image.RePage();

                if (setting.Trim)
                {
                    image.Trim();
                }
            });

            return(image);
        }
Example #19
0
        public async Task wall(CommandContext ctx)
        {
            // type a thing(tm)
            await ctx.TriggerTypingAsync();

            // check for attachments
            string dank;

            if (ctx.Message.Attachments.Count < 1)
            {
                // check msg attachments
                dank = await GetPastAttachment(ctx);

                if (dank == null)
                {
                    await ctx.RespondAsync("I couldn't find any attachments!");

                    return;
                }
            }
            else
            {
                dank = ctx.Message.Attachments[0].Url;
            }
            // download attachment
            using (var client = new WebClient()) {
                Stream stream = await client.OpenReadTaskAsync(new Uri(dank));

                IMagickImage img = new MagickImage(stream);
                img.VirtualPixelMethod = VirtualPixelMethod.Tile;
                img.Resize(512, 512);
                img.Distort(DistortMethod.Perspective, 0, 0, 57, 42, 0, 128, 63, 130, 128, 0, 140, 60, 128, 128, 140, 140);
                img.Format = MagickFormat.Png64;
                var memory = new MemoryStream(img.ToByteArray());
                await ctx.RespondWithFileAsync("wall.png", memory);

                // Dispose of everything used
                await memory.DisposeAsync();

                await stream.DisposeAsync();

                img.Dispose();
            }
        }
Example #20
0
        private void InternalGenerateSquareImage(string inputPath, string outputPath)
        {
            using (var image = new MagickImage(inputPath))
            {
                image.VirtualPixelMethod = VirtualPixelMethod.Background;
                image.BackgroundColor    = MagickColors.White;

                // square out
                int size     = Math.Max(image.Width, image.Height);
                int x        = -Math.Max((image.Height - image.Width) / 2, 0);
                int y        = -Math.Max((image.Width - image.Height) / 2, 0);
                var geometry = new MagickGeometry(x, y, size, size);
                image.SetArtifact("distort:viewport", geometry.ToString());

                // filter
                image.FilterType = FilterType.Point;
                image.Distort(DistortMethod.ScaleRotateTranslate, 0);

                image.RePage();
                image.Resize(new MagickGeometry(256, 256));
                image.Write(outputPath);
            }
        }
Example #21
0
        public static void ApplyXmpRecipe(XmpRecipeContainer xmpRecipeContainer, MagickImage image)
        {
            foreach (IXmpRecipeAction action in xmpRecipeContainer.Actions)
            {
                if (action.GetType() == typeof(XmpRotate))
                {
                    XmpRotate xmpRotate = (XmpRotate)action;
                    image.VirtualPixelMethod = VirtualPixelMethod.Black;
                    image.Rotate(xmpRotate.Angle);
                }
                else if (action.GetType() == typeof(XmpResize))
                {
                    XmpResize xmpResize = (XmpResize)action;

                    if (image.Width > xmpResize.Width && image.Height > xmpResize.Height)
                    {
                        image.Resize(xmpResize.Width, xmpResize.Height);
                    }
                }
                else if (action.GetType() == typeof(XmpCrop))
                {
                    XmpCrop xmpCrop = (XmpCrop)action;

                    int intLeftPixel   = Convert.ToInt32(image.Width * xmpCrop.Left);
                    int intTopPixel    = Convert.ToInt32(image.Height * xmpCrop.Top);
                    int intRightPixel  = Convert.ToInt32(image.Width * xmpCrop.Right);
                    int intBottomPixel = Convert.ToInt32(image.Height * xmpCrop.Bottom);
                    int intWidth       = intRightPixel - intLeftPixel;
                    int intHeight      = intBottomPixel - intTopPixel;

                    MagickGeometry magickGeometry = new MagickGeometry(intLeftPixel, intTopPixel, intWidth, intHeight);
                    image.Crop(magickGeometry);
                }
                else if (action.GetType() == typeof(XmpFlip))
                {
                    XmpFlip xmpFlip = (XmpFlip)action;

                    if (xmpFlip.FlipVertical)
                    {
                        image.Flop();
                    }

                    if (xmpFlip.FlipHorizontal)
                    {
                        image.Flip();
                    }
                }
                else if (action.GetType() == typeof(XmpStraighten))
                {
                    XmpStraighten xmpStraighten = (XmpStraighten)action;
                    image.VirtualPixelMethod = VirtualPixelMethod.Black;

                    if (xmpStraighten.Crop)
                    {
                        //http://www.imagemagick.org/Usage/distorts/#rotate_methods

                        int    w   = image.Width;
                        int    h   = image.Height;
                        double aa  = xmpStraighten.Angle * Math.PI / 180;
                        double srt = (w * Math.Abs(Math.Sin(aa)) + h * Math.Abs(Math.Cos(aa))) / Math.Min(w, h);

                        image.Distort(DistortMethod.ScaleRotateTranslate, srt, xmpStraighten.Angle);
                    }
                    else
                    {
                        image.Rotate(xmpStraighten.Angle);
                    }
                }
            }
        }
Example #22
0
 private void distor(MagickImage image)
 {
     image.Distort(DistortMethod.Perspective, new double[] { 0, 0, 0, 0, 1300, 0, 1300, 0, 0, 720, 0, 720, 1300, 720, 1300, 770 });
 }