Beispiel #1
0
        /// <summary>
        /// Calculate difference between provided images
        /// </summary>
        /// <param name="errorTolerance">Maximum allowed pixel delta</param>
        /// <param name="maxDifferences">Maximum differences to recognize</param>
        /// <returns>Image with highlighted differences</returns>
        public Image Calculate(double errorTolerance, int maxDifferences = int.MaxValue)
        {
            using var leftCopy  = new Bitmap(_left);
            using var rightCopy = new Bitmap(_right);

            var leftBits = leftCopy.LockBits(
                new Rectangle(0, 0, leftCopy.Width, leftCopy.Height),
                ImageLockMode.ReadWrite,
                PixelFormat.Format32bppArgb);
            var rightBits = rightCopy.LockBits(
                new Rectangle(0, 0, rightCopy.Width, rightCopy.Height),
                ImageLockMode.ReadWrite,
                PixelFormat.Format32bppArgb);

            if (Preprocess(leftBits, rightBits) && _progressObserver != null)
            {
                _progressObserver.Report(leftCopy, "leftPreprocess");
                _progressObserver.Report(rightCopy, "rightPreprocess");
            }

            using var pixels = _diffAlgorithm.GetDifferentPixels(errorTolerance, leftBits, rightBits, _progressObserver);

            leftCopy.UnlockBits(leftBits);
            rightCopy.UnlockBits(rightBits);

            var result = new Bitmap(_left);

            _diffHighlighter?.Highlight(result, pixels, maxDifferences, _progressObserver);

            return(result);
        }
        public unsafe IPixelBuffer GetDifferentPixels(double errorTolerance, BitmapData left, BitmapData right, IDiffProgressObserver progress = null)
        {
            var result = GenerateDiffMap(errorTolerance, left, right);

            if (progress == null)
            {
                return(result);
            }

            using var report = new ReportBuilder(left.Width, left.Height).AddPixels(result, Color.Red).Build();
            progress.Report(report, "diffMap");

            return(result);
        }
        public void Highlight(Image target, IPixelBuffer pixels, int limit, IDiffProgressObserver progress = null)
        {
            using var g = Graphics.FromImage(target);

            foreach (var cluster in _clusteringAlgorithm.GetClusters(pixels, limit))
            {
                g.DrawRectangle(_pen, cluster);

                if (progress == null)
                {
                    continue;
                }

                using var report = new ReportBuilder(target).AddRectangle(cluster, _pen).Build();
                progress.Report(report, "rect");
            }
        }