예제 #1
0
        /// <summary>
        /// Asynchronously 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 async Task <Image> CalculateAsync(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 (await PreprocessAsync(leftBits, rightBits) && _progressObserver != null)
            {
                await _progressObserver.ReportAsync(leftCopy, "leftPreprocess");

                await _progressObserver.ReportAsync(rightCopy, "rightPreprocess");
            }

            var pixels = await _diffAlgorithm.GetDifferentPixelsAsync(errorTolerance, leftBits, rightBits, _progressObserver);

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

            var result = new Bitmap(_left);

            if (_diffHighlighter != null)
            {
                await _diffHighlighter.HighlightAsync(result, pixels, maxDifferences, _progressObserver);
            }

            return(result);
        }
예제 #2
0
        public async Task <IPixelBuffer> GetDifferentPixelsAsync(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();
            await progress.ReportAsync(report, "diffMap");

            return(result);
        }
예제 #3
0
        public async Task HighlightAsync(Image target, IPixelBuffer pixels, int limit, IDiffProgressObserver progress = null)
        {
            using var g = Graphics.FromImage(target);

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

                if (progress == null)
                {
                    continue;
                }

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