Example #1
0
        public async Task GetClustersAsync_SameFiles_ReturnsZero()
        {
            using var leftImgCopy1 = new Bitmap(leftImg1);
            using var leftImgCopy2 = new Bitmap(leftImg1);

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

            await diff.ImagePreprocessor.ProcessAsync(leftBits);

            await diff.ImagePreprocessor.ProcessAsync(rightBits);

            var pixels = await diff.GetDifferentPixelsAsync(25, leftBits, rightBits);

            var clusters = new List <Rectangle>();

            await foreach (var cluster in _testClass.GetClustersAsync(pixels, int.MaxValue))
            {
                clusters.Add(cluster);
            }

            Assert.AreEqual(0, clusters.Count);

            leftImgCopy1.UnlockBits(leftBits);
            leftImgCopy2.UnlockBits(rightBits);
        }
Example #2
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);
        }