Exemplo n.º 1
0
 public override MagickImage CompositeLayers()
 {
     if (HasEmissiveFreak())
     {
         using (var baseImage = base.CompositeLayers())
         {
             var image = new MagickImage(baseImage)
             {
                 HasAlpha = false
             };
             for (var i = 0; i < 2; i++)
             {
                 using (var ef = GetEmissiveFreakLayer(i))
                     using (var efImage = ef.GetMagickImage())
                     {
                         efImage.HasAlpha = false;
                         efImage.Resize(image.Width, image.Height);
                         image.Composite(efImage, CompositeOperator.Screen);
                     }
             }
             if (baseImage.HasAlpha)
             {
                 image.HasAlpha = true;
                 image.CopyPixels(baseImage, Channels.Alpha);
             }
             return(image);
         }
     }
     return(base.CompositeLayers());
 }
Exemplo n.º 2
0
 public override MagickImage CompositeLayers()
 {
     using (var main = GetMainLayer())
         using (var emission = GetEmissionLayer())
         {
             var(width, height) = DecideCompositionSize(main, emission);
             var newImage = new MagickImage(MagickColors.Black, width, height);
             using (var mainImage = main.GetMagickImage())
             {
                 mainImage.HasAlpha = false;
                 mainImage.Resize(width, height);
                 newImage.Composite(mainImage, CompositeOperator.Plus);
             }
             if (HasEmission())
             {
                 using (var emissionImage = emission.GetMagickImage())
                 {
                     emissionImage.Resize(width, height);
                     emissionImage.HasAlpha = false;
                     newImage.Composite(emissionImage, CompositeOperator.Screen);
                 }
             }
             if (main.image != null && main.image.HasAlpha)
             {
                 newImage.HasAlpha = true;
                 newImage.CopyPixels(main.image, Channels.Alpha);
             }
             return(newImage);
         }
 }
        private static byte[] PreprocessImage(IMagickImage sourceImage, Rectangle cropRegion, float scale,
                                              string cropRegionPath = "")
        {
            using var mImage = new MagickImage(MagickColors.White, cropRegion.Width, cropRegion.Height);

            mImage.Format = MagickFormat.Png;

            if (cropRegion.Width > 0 && cropRegion.Height > 0)
            {
                mImage.CopyPixels(sourceImage,
                                  new MagickGeometry(cropRegion.X, cropRegion.Y, cropRegion.Width, cropRegion.Height));
            }
            else
            {
                mImage.CopyPixels(sourceImage);
            }

            mImage.Threshold(new Percentage(65f)); // 60 is OK
            mImage.Depth = 1;

            // mImage.GammaCorrect(0.20); //0.20
            // mImage.BlackThreshold(new Percentage(50f), Channels.RGB);

            mImage.ColorSpace = ColorSpace.Gray;

            mImage.Negate(Channels.RGB);

            if (scale != 0f && !(Math.Abs(scale - 1f) < float.Epsilon))
            {
                mImage.FilterType = FilterType.Cubic;
                mImage.Resize(new Percentage(scale * 100.0f));
            }

            mImage.BlackThreshold(new Percentage(55f), Channels.RGB);
            mImage.GaussianBlur(0.85, 0.85);

            // mImage.BlackThreshold(new Percentage(50f), Channels.RGB);
            // mImage.MedianFilter(2);
            // mImage.BlackThreshold(new Percentage(20f), Channels.RGB);

            if (!string.IsNullOrEmpty(cropRegionPath))
            {
                mImage.Write(cropRegionPath);
            }

            return(mImage.ToByteArray());
        }
Exemplo n.º 4
0
 /// <summary>
 /// Multiply a image and a color.
 /// </summary>
 /// <param name="image">Base image.</param>
 /// <param name="color">Color to multiply.</param>
 /// <returns>Composed image.</returns>
 internal static MagickImage Multiply(MagickImage image, MagickColor color)
 {
     using (var colorTex = new MagickImage(color, image.Width, image.Height))
     {
         var newImage = new MagickImage(image);
         newImage.HasAlpha = false;
         newImage.Composite(colorTex, CompositeOperator.Multiply);
         if (image.HasAlpha)
         {
             newImage.HasAlpha = true;
             newImage.CopyPixels(image, Channels.Alpha);
         }
         return(newImage);
     }
 }
Exemplo n.º 5
0
        public void TestAlphaChannel()
        {
            using (var image = new MagickImage(TestUtils.LoadMagickImage("alpha_test.png")))
                using (var dest = new MagickImage(image))
                    using (var emission = new MagickImage(MagickColors.Black, image.Width, image.Height))
                    {
                        Assert.True(image.HasAlpha);

                        dest.HasAlpha = false;
                        dest.Composite(emission, CompositeOperator.Screen);
                        dest.HasAlpha = true;
                        dest.CopyPixels(image, Channels.Alpha);

                        var result = image.Compare(dest, ErrorMetric.MeanErrorPerPixel, Channels.All);
                        Assert.AreEqual(0.0, result);
                    }
        }
Exemplo n.º 6
0
        private void ApplyImage(int resizeWidth, int resizeHeight, Channels channel, string path, int x, int y)
        {
            int imgPadding = padding / 2;

            MagickImage imageToApply = new MagickImage(path);

            imageToApply.Transparent(new MagickColor("#000000"));
            imageToApply.FilterType = FilterType.Quadratic;
            imageToApply.Quality    = 100;
            imageToApply.Format     = MagickFormat.Png;
            imageToApply.ColorAlpha(new MagickColor("#000000"));
            MagickGeometry size = new MagickGeometry(resizeWidth - padding, resizeHeight - padding);

            size.IgnoreAspectRatio = true;
            imageToApply.Resize(size);
            imageToApply.ColorSpace = ColorSpace.LinearGray;

            workingImage.CopyPixels(imageToApply, size, (x * resizeWidth) + imgPadding, (y * resizeHeight) + imgPadding, channel);
        }
Exemplo n.º 7
0
        public byte[] Get(string name, float scale = 1)
        {
            Character c;

            if (!font.Characters.TryGetValue(name[0], out c))
            {
                return(null);
            }

            MagickImage page = getTexturePage(c.TexturePage);

            MagickImage glyph = new MagickImage(new MagickColor(65535, 65535, 65535, 0), c.Bounds.Width + c.Offset.X, c.Bounds.Height + c.Offset.Y);

            glyph.CopyPixels(page, new MagickGeometry(c.Bounds.X, c.Bounds.Y, c.Bounds.Width, c.Bounds.Height), c.Offset.X, c.Offset.Y);
            glyph.RePage();

            //todo: we can return MagickImage here instead of Bmp with a bit of refactoring.
            return(glyph.ToByteArray(MagickFormat.Bmp));
        }