Esempio n. 1
0
            public void ShouldChangeTheBithDepth()
            {
                using (var image = new MagickImage(Files.RoseSparkleGIF))
                {
                    image.BitDepth(1);

                    Assert.Equal(1, image.BitDepth());
                }
            }
Esempio n. 2
0
            public void ShouldCalculateTheBitDepth()
            {
                using (var image = new MagickImage(Files.RoseSparkleGIF))
                {
                    Assert.Equal(8, image.BitDepth());

                    image.Threshold((Percentage)50);
                    Assert.Equal(1, image.BitDepth());
                }
            }
Esempio n. 3
0
            public void ShouldChangeTheBithDepthForTheSpecifiedChannel()
            {
                using (var image = new MagickImage(Files.RoseSparkleGIF))
                {
                    image.BitDepth(Channels.Red, 1);

                    Assert.Equal(1, image.BitDepth(Channels.Red));
                    Assert.Equal(8, image.BitDepth(Channels.Green));
                    Assert.Equal(8, image.BitDepth(Channels.Blue));
                }
            }
 private static byte[] CalculateLuminance(MagickImage src)
 {
     if (src == null)
     {
         throw new ArgumentNullException(nameof(src));
     }
     if (src.BitDepth() < 8)
     {
         src.BitDepth(8);
     }
     return(src.ToByteArray(MagickFormat.Gray));
 }
Esempio n. 5
0
 public void ShouldCalculateTheBitDepthForTheSpecifiedChannel()
 {
     using (var image = new MagickImage(Files.RoseSparkleGIF))
     {
         Assert.Equal(1, image.BitDepth(Channels.Alpha));
     }
 }
Esempio n. 6
0
        public static void Erase(string path, List <PatternType> patterns, int scale, MagickColor color)
        {
            if (patterns.Count < 1)
            {
                return;
            }
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            PatternType chosenPattern = GetRandomPatternType(patterns);

            PreProcessing(path, "- Pattern: " + chosenPattern.ToString());
            Random      rand       = new Random();
            MagickImage patternImg = new MagickImage(GetInpaintImage(chosenPattern));

            patternImg.FilterType = FilterType.Point;
            patternImg.Colorize(color, new Percentage(100));
            patternImg.BackgroundColor = MagickColors.Transparent;
            patternImg.Rotate(RandRange(0, 360));
            double geomW = img.Width * RandRange(1.0f, 2.0f) * GetScaleMultiplier(scale);
            double geomH = img.Height * RandRange(1.0f, 2.0f) * GetScaleMultiplier(scale);
            //Logger.Log("geomW: " + geomW + " - geomH: " + geomW);
            MagickGeometry upscaleGeom = new MagickGeometry(Math.Round(geomW) + "x" + Math.Round(geomH) + "!");

            patternImg.Resize(upscaleGeom);
            patternImg.BitDepth(Channels.Alpha, 1);
            img.Composite(patternImg, Gravity.Center, CompositeOperator.Over);
            img.Write(path);
            PostProcessing(img, path, path);
        }
Esempio n. 7
0
        public static void SetColorDepth(string path, int bits)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.BitDepth(bits);
            img.Quality = Program.GetDefaultQuality(img);
            img.Write(path);
        }
Esempio n. 8
0
        public static async Task SetColorDepth(string path, int bits)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.BitDepth(bits);
            img.Quality = await Program.GetFormatQuality(img);

            img.Write(path);
        }
Esempio n. 9
0
        public static byte[] GetImageDataMagick(Stream stream, out int width, out int height, out int bitPerPixel, out int mipMap)
        {
            var img     = new MagickImage(stream);
            int width1  = img.Width;
            int height1 = img.Height;
            int sizex   = GetAlign(width1);
            int sizey   = GetAlign(height1);

            var size = new MagickGeometry(sizex, sizey)
            {
                IgnoreAspectRatio = true,
            };

            if (width1 != sizex || height1 != sizey)
            {
                img.Resize(size);
            }
            width1  = sizex;
            height1 = sizey;
            width   = sizex;
            height  = sizey;

            bitPerPixel = Math.Max(img.BitDepth(), 8) * 4;
            int bytePerPixel = bitPerPixel / 8;

            int totalCount = sizex * sizey * bytePerPixel;
            int totalSize  = GetTotalSize(totalCount, width, height, out mipMap);

            byte[] bytes = new byte[totalSize];

            img.ToByteArray(MagickFormat.Rgba).CopyTo(new Span <byte>(bytes, 0, bytePerPixel * width1 * height1));

            for (int i = 1; i < mipMap; i++)
            {
                width1  /= 2;
                height1 /= 2;
                int d = bytePerPixel * width1 * height1;
                img.Resize(width1, height1);

                img.ToByteArray(MagickFormat.Rgba).CopyTo(new Span <byte>(bytes, totalCount, d));
                totalCount += d;
            }
            img.Dispose();
            return(bytes);
        }
Esempio n. 10
0
        internal static void CropAndSave(MagickImage img, string fileName, int dimension, System.Windows.Shapes.Rectangle rect, double scale, System.Windows.Controls.Image pictureBox)
        {
            Directory.CreateDirectory(Path.GetFileNameWithoutExtension(fileName));

            img = new(img);

            cropImage(img, rect, scale, pictureBox);

            string iconFilename = Path.Combine(Path.GetFileNameWithoutExtension(fileName), $"CroppedIcon_{dimension}.ico");

            using MemoryStream ms = new(img.write);

            Bitmap bmp = Bitmap.FromStream();

            img.Scale(new MagickGeometry(dimension)
            {
                IgnoreAspectRatio = true
            });
            Console.WriteLine(img.HasAlpha + " " + img.BitDepth());
            if (dimension == 256)
            {
                if ((img.Format == MagickFormat.Jpeg || img.Format == MagickFormat.Jpg) && MessageBox.Show("Compact into jpg .ico?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    IconFileWriter x = new();
                    img.HasAlpha = true;
                    x.Images.Add(img.ToBitmap());
                    x.Save(iconFilename);

                    return;
                }
                else
                {
                    img.HasAlpha = true;
                }
            }

            img.Write(iconFilename);
        }
Esempio n. 11
0
        private string GetFileInfo()
        {
            if (File.Exists(_inputFilePath))
            {
                try
                {
                    using (MagickImage magickImage = new MagickImage(_inputFilePath))
                    {
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.AppendLine("File path: " + _inputFilePath);
                        stringBuilder.AppendLine("File size: " + magickImage.FileSize.ToString() + " bytes");
                        stringBuilder.AppendLine("Format: " + magickImage.Format.ToString() + " - " + magickImage.FormatInfo.Description);
                        stringBuilder.AppendLine("Bit Depth: " + magickImage.BitDepth().ToString() + " bits");
                        stringBuilder.AppendLine($"Dimensions: {magickImage.Width}x{magickImage.Height}");
                        for (var i = 0; i < magickImage.Channels.ToList().Count; i++)
                        {
                            var magickImageChannel = magickImage.Channels.ToList()[i];
                            stringBuilder.AppendLine($"Channel {i+1}: " + magickImageChannel.ToString());
                        }
                        stringBuilder.AppendLine("Color space: " + magickImage.ColorSpace.ToString());
                        stringBuilder.AppendLine("Compression: " + magickImage.Compression.ToString());
                        stringBuilder.AppendLine("Density: " + magickImage.Density);
                        stringBuilder.AppendLine("Quality: " + magickImage.Quality);
                        stringBuilder.AppendLine("Comment: " + magickImage.Comment);

                        return(stringBuilder.ToString());
                    }
                }
                catch (Exception e)
                {
                    return("Exception: " + e.Message);
                }
            }
            else
            {
                return("Unable to find file");
            }
        }