private static void TestPixels(IMagickImage image, MagickColor firstRow, MagickColor secondRow)
        {
            using (PixelCollection pixels = image.GetPixels())
            {
                for (int y = 0; y < 2; y++)
                {
                    for (int x = 0; x < 10; x++)
                    {
                        ColorAssert.AreEqual(y == 0 ? firstRow : secondRow, pixels.GetPixel(x, y).ToColor());
                    }
                }
            }

            using (MemoryStream memStream = new MemoryStream())
            {
                image.Format = MagickFormat.Bmp;
                image.Write(memStream);
                memStream.Position = 0;

                using (IMagickImage output = new MagickImage(memStream))
                {
                    using (PixelCollection pixels = output.GetPixels())
                    {
                        for (int y = 0; y < 2; y++)
                        {
                            for (int x = 0; x < 10; x++)
                            {
                                ColorAssert.AreEqual(y == 0 ? firstRow : secondRow, pixels.GetPixel(x, y).ToColor());
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private IMagickImage GenerateMask(IMagickImage original, IMagickImage modified)
        {
            int brushWidth = 10;
            int scale      = 10;

            IMagickImage mask = new MagickImage(MagickColors.Transparent, original.Width, original.Height);

            mask.Format = MagickFormat.Png;

            IPixelCollection originalPC = original.GetPixels();
            IPixelCollection modifiedPC = modified.GetPixels();
            IPixelCollection maskPC     = mask.GetPixels();

            for (int y = 0; y < original.Height; y++)
            {
                for (int x = 0; x < original.Width; x++)
                {
                    Pixel originalPixel = originalPC.GetPixel(x, y);
                    Pixel modifiedPixel = modifiedPC.GetPixel(x, y);

                    if (!originalPixel.Equals(modifiedPixel))
                    {
                        Pixel p = maskPC.GetPixel(x, y);
                        p.SetChannel(0, ushort.MaxValue);
                        p.SetChannel(1, 0);
                        p.SetChannel(2, 0);
                        p.SetChannel(3, ushort.MaxValue / 3);
                    }
                }
            }

            return(mask);
        }
Beispiel #3
0
            private static void AssertClipColors(bool inside, QuantumType value)
            {
                using (IMagickImage image = new MagickImage(Files.InvitationTIF))
                {
                    image.Alpha(AlphaOption.Transparent);
                    image.Clip("Pad A", inside);
                    image.Alpha(AlphaOption.Opaque);

                    using (IMagickImage mask = image.GetWriteMask())
                    {
                        Assert.IsNotNull(mask);
                        Assert.AreEqual(false, mask.HasAlpha);

                        using (IPixelCollection pixels = mask.GetPixels())
                        {
                            MagickColor pixelA = pixels.GetPixel(0, 0).ToColor();
                            MagickColor pixelB = pixels.GetPixel(mask.Width - 1, mask.Height - 1).ToColor();

                            Assert.AreEqual(pixelA, pixelB);
                            Assert.AreEqual(value, pixelA.R);
                            Assert.AreEqual(value, pixelA.G);
                            Assert.AreEqual(value, pixelA.B);

                            MagickColor pixelC = pixels.GetPixel(mask.Width / 2, mask.Height / 2).ToColor();
                            Assert.AreEqual(Quantum.Max - value, pixelC.R);
                            Assert.AreEqual(Quantum.Max - value, pixelC.G);
                            Assert.AreEqual(Quantum.Max - value, pixelC.B);
                        }
                    }
                }
            }
Beispiel #4
0
 public static void AreEqual(MagickColor expected, IMagickImage image, int x, int y)
 {
     using (PixelCollection pixels = image.GetPixels())
     {
         AreEqual(expected, pixels.GetPixel(x, y));
     }
 }
Beispiel #5
0
 public static void AreNotEqual(MagickColor notExpected, IMagickImage image, int x, int y)
 {
     using (PixelCollection collection = image.GetPixels())
     {
         AreNotEqual(notExpected, collection.GetPixel(x, y));
     }
 }
Beispiel #6
0
 private MagickColor GetBorderColor(IMagickImage image)
 {
     using (IPixelCollection pixels = image.GetPixels())
     {
         return(pixels.GetPixel((int)BorderColorLocation.X, (int)BorderColorLocation.Y).ToColor());
     }
 }
        public static void CompositeClear(this IMagickImage <byte> image, MagickColor color)
        {
            const int BufferSize = 50;

            using (var pixels = image.GetPixels())
            {
                var colorChannels = pixels.Channels;
                var colorArray    = color.ToByteArray();

                var buffer = new byte[BufferSize * BufferSize * colorChannels];

                // Create a buffer to reduce native calls and keep memory footprint low.
                for (var x = 0; x < BufferSize; x++)
                {
                    for (var y = 0; y < BufferSize; y++)
                    {
                        var destination = colorChannels * (x + (y * BufferSize));

                        Array.Copy(colorArray, 0, buffer, destination, colorChannels);
                    }
                }

                for (var x = 0; x < image.Width; x += BufferSize)
                {
                    for (var y = 0; y < image.Height; y += BufferSize)
                    {
                        var w = Math.Min(BufferSize, image.Width - x);
                        var h = Math.Min(BufferSize, image.Height - y);

                        var bufferLength = w * h * colorChannels;

                        var actualBuffer = buffer.AsSpan()[..bufferLength];
Beispiel #8
0
 private static void ResetMaxList(List <PixelValue> maxList, IMagickImage image)
 {
     using (var pixels = image.GetPixels())
     {
         var values = pixels.ToShortArray(0, 0, image.Width, 1, "R");
         for (int i = 0; i < maxList.Count; i++)
         {
             maxList[i].Value = values[maxList[i].Position];
         }
     }
 }
        private void PickImages(bool duplicates)
        {
            using var pixels = _template.GetPixels();
            var total = pixels.Count();

            //Picker
            var picker = new ImagePicker(_archive, duplicates, total);

            //Bar
            var bar = new ProgressBar(total, "Picking images");

            foreach (var pixel in pixels)
            {
                var point = new Point(pixel.X, pixel.Y);

                var filename = picker.MostFittingImageFilename(pixel.ToColor());
                _images.Add(point, filename);

                bar.Tick($"Picking image for pixel {point}");
            }
        }
        public void CreateImage_WithColor_ReturnsMagickImage()
        {
            var color = MagickColors.Goldenrod;

            MagickFactory factory = new MagickFactory();

            using (IMagickImage image = factory.CreateImage(color, 10, 5))
            {
                Assert.IsInstanceOfType(image, typeof(MagickImage));
                Assert.AreEqual(10, image.Width);
                Assert.AreEqual(5, image.Height);

                using (PixelCollection pixels = image.GetPixels())
                {
                    ColorAssert.AreEqual(MagickColors.Goldenrod, image, 0, 0);
                }
            }
        }
Beispiel #11
0
            public void ShouldReadByteArrayWithPixelStorageSettings()
            {
                byte[] data = new byte[]
                {
                    0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
                    0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
                    0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0,
                };

                var pixelStorageSettings = new PixelStorageSettings(2, 1, StorageType.Double, PixelMapping.RGBA);

                MagickFactory factory = new MagickFactory();

                using (IMagickImage image = factory.CreateImage(data, pixelStorageSettings))
                {
                    Assert.AreEqual(2, image.Width);
                    Assert.AreEqual(1, image.Height);

                    using (IPixelCollection pixels = image.GetPixels())
                    {
                        Pixel pixel = pixels.GetPixel(0, 0);
                        Assert.AreEqual(4, pixel.Channels);
                        Assert.AreEqual(0, pixel.GetChannel(0));
                        Assert.AreEqual(0, pixel.GetChannel(1));
                        Assert.AreEqual(0, pixel.GetChannel(2));
                        Assert.AreEqual(Quantum.Max, pixel.GetChannel(3));

                        pixel = pixels.GetPixel(1, 0);
                        Assert.AreEqual(4, pixel.Channels);
                        Assert.AreEqual(0, pixel.GetChannel(0));
                        Assert.AreEqual(Quantum.Max, pixel.GetChannel(1));
                        Assert.AreEqual(0, pixel.GetChannel(2));
                        Assert.AreEqual(0, pixel.GetChannel(3));
                    }
                }
            }
Beispiel #12
0
        private MagickGeometry GetLargestArea(IMagickImage image, MagickColor borderColor)
        {
            var points = new Line[4];

            using (IPixelCollection pixels = image.GetPixels())
            {
                var line = new Line(0, 0);

                while (IsBorderColor(pixels, line.X1, line.Y, borderColor) && line.Y < image.Height - 1 && line.X1 < image.Width - 1)
                {
                    line.Y++;
                    line.X1++;
                }

                line.X2 = image.Width - 1;
                while (IsBorderColor(pixels, line.X2, line.Y, borderColor) && line.X2 > 0)
                {
                    line.X2--;
                }

                points[0] = line;

                line = new Line(image.Width - 1, 0);

                while (IsBorderColor(pixels, line.X2, line.Y, borderColor) && line.Y < image.Height && line.X2 > 0)
                {
                    line.Y++;
                    line.X2--;
                }

                line.X1 = 0;
                while (IsBorderColor(pixels, line.X1, line.Y, borderColor) && line.X1 < image.Width - 1)
                {
                    line.X1++;
                }

                points[1] = line;

                line = new Line(0, image.Height - 1);

                while (IsBorderColor(pixels, line.X1, line.Y, borderColor) && line.Y > 0 && line.X1 < image.Width - 1)
                {
                    line.Y--;
                    line.X1++;
                }

                line.X2 = image.Width - 1;
                while (IsBorderColor(pixels, line.X2, line.Y, borderColor) && line.X2 > 0)
                {
                    line.X2--;
                }

                points[2] = line;

                line = new Line(image.Width - 1, image.Height - 1);

                while (IsBorderColor(pixels, line.X2, line.Y, borderColor) && line.Y > 0 && line.X2 > 0)
                {
                    line.Y--;
                    line.X2--;
                }

                line.X1 = 0;
                while (IsBorderColor(pixels, line.X1, line.Y, borderColor) && line.X1 < image.Width - 1)
                {
                    line.X1++;
                }

                points[3] = line;
            }

            var geometry = new MagickGeometry(0, 0);

            SwapPoints(points);

            geometry = TestGeometry(geometry, points[0], points[3]);
            geometry = TestGeometry(geometry, points[1], points[2]);
            geometry = TestGeometry(geometry, points[0], points[2]);
            geometry = TestGeometry(geometry, points[1], points[3]);

            return(geometry);
        }
Beispiel #13
0
        private List <List <System.Drawing.Color> > ToPixels(IMagickImage image)
        {
            var pixels = image.GetPixels();

            return(LinqExtensions.FromTo(0, image.Height).Select(y => LinqExtensions.FromTo(0, image.Width).Select(x => pixels.GetPixel(x, y).ToColor().ToColor()).ToList()).ToList());
        }
Beispiel #14
0
        private NeedleResult FindHighEntropyStrip(IMagickImage image, Gravity gravity, double NeedleSize, StitchTask task)
        {
            IProgress <double> progress = task;
            var pixels = image.GetPixels();

            var t1 = DateTime.UtcNow;

            IEnumerable <int> rows    = null;
            IEnumerable <int> columns = null;

            Debug.Assert(image.Height > 1 && image.Width > 1, "Assumes non-empty image");
            Debug.Assert(image.Width >= NeedleSize, "Assumes image is at least as big as needle size");

            var searchArea = NeedleSearchArea(image, gravity);

            rows    = searchArea.Item1;
            columns = searchArea.Item2;

            var minY = rows.Min();
            var maxY = rows.Max();

            var minX            = columns.Min();
            var maxX            = columns.Max();
            var imageDimensions = Tuple.Create(image.Width, image.Height);

            List <List <Pixel> > pixelGrid      = LinqExtensions.FromTo(minY, maxY).Select(y => LinqExtensions.FromTo(minX, maxX).Select(x => pixels.GetPixel(x, y)).ToList()).ToList();
            List <List <float> > brightnessGrid = pixelGrid.Select(xs => xs.Select(p => p.ToColor().ToColor().GetBrightness()).ToList()).ToList();

            var gridWidth  = maxX - minX;
            var gridHeight = maxY - minY;

            var   bestNeedleStddev = 0.0;
            Point bestNeedle       = default(Point);

            double totalCycles  = rows.Count() * columns.Count();
            double currentCycle = 0;

            Console.WriteLine(brightnessGrid);
            foreach (var y in rows)
            {
                foreach (var x in columns)
                {
                    progress.Report(currentCycle / totalCycles);
                    currentCycle++;
                    if (y - minY + NeedleSize >= gridHeight)
                    {
                        continue;
                    }

                    if (x - minX + NeedleSize >= gridWidth)
                    {
                        continue;
                    }

                    var    count      = 0;
                    var    mean       = 0.0;
                    var    m2         = 0.0;
                    double blackCount = 0.0;
                    for (var x2 = x - minX; x2 < x - minX + NeedleSize; x2++)
                    {
                        for (var y2 = y - minY; y2 < y - minY + NeedleSize; y2++)
                        {
                            var b = brightnessGrid[y2][x2];
                            var p = pixelGrid[y2][x2].ToColor();

                            if (b < 0.08)
                            {
                                blackCount++;
                            }

                            count++;
                            var delta = b - mean;
                            mean = mean + delta / count;
                            var delta2 = b - mean;
                            m2 = m2 + delta * delta2;
                        }
                    }
                    var variance = m2 / (count - 1);
                    var stddev   = variance;

                    //Console.WriteLine("{0}, {1}, {2}", blackCount, NeedleSize * NeedleSize, blackCount / (NeedleSize * NeedleSize));
                    if (stddev > bestNeedleStddev && blackCount / (NeedleSize * NeedleSize) < 0.5)
                    {
                        bestNeedleStddev = stddev;
                        bestNeedle       = new Point(x, y);
                    }
                }
            }

            return(new NeedleResult()
            {
                Point = bestNeedle, Entropy = bestNeedleStddev
            });
        }