private unsafe void CompareData(ComparisonResult result) { BitmapData sampleData = null; BitmapData masterData = MasterImage.LockBits(MasterRectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); try { sampleData = SampleImage.LockBits(SampleRectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); int width = MasterScanLineWidth; int errorCount = 0; PixelData *masterPixels; PixelData *samplePixels = (PixelData *)sampleData.Scan0; for (int y = 0; y < MasterSize.Height; y++) { masterPixels = (PixelData *) ((byte *)masterData.Scan0.ToPointer() + y * width); samplePixels = (PixelData *) ((byte *)sampleData.Scan0.ToPointer() + y * width); for (int x = 0; x < MasterSize.Width; x++) { bool match = PixelsEqual(*masterPixels, *samplePixels); if (!match) { result.AddDifference( new PixelColorDifference(x, y, masterPixels->red, masterPixels->green, masterPixels->blue, samplePixels->red, samplePixels->green, samplePixels->blue)); result.SetIdentical(false); errorCount++; if (errorCount > MaxErrorCount) { result.SetCriteriaMet(false); return; } } masterPixels++; samplePixels++; } } } finally { MasterImage.UnlockBits(masterData); if (sampleData != null) { SampleImage.UnlockBits(sampleData); } } }
/// <summary> /// Verifies that the master and sample images match in size. /// Otherwise, the difference is added to the result. /// </summary> /// <param name="result">The comparison result to add the difference /// to. May be null.</param> /// <returns>true if the sizes match, false otherwise.</returns> protected bool RequireSizeMatch(ComparisonResult result) { if (MasterSize.Equals(SampleSize)) { return(true); } else { result.AddDifference( new SizeDifference(MasterSize, SampleSize)); result.SetIdentical(false); result.SetCriteriaMet(false); return(false); } }
/// <summary> /// Checks whether a pixel has an acceptable neighbouring value. /// </summary> private unsafe void CheckPixel(int x, int y, ComparisonResult result, ref int errorCount) { // // Locals used to mark whether we're below or above range. // If both, below wins arbitrarily. // float belowDistance = 0; float aboveDistance = 0; bool belowFound = false; bool aboveFound = false; PixelData *masterPixel = GetPixelDataAt( masterData, x, y, MasterScanLineWidth); for (int i = 0; i < positions.Length; i++) { PositionDelta d = positions[i]; int sampleX = x + d.X; int sampleY = y + d.Y; if (sampleX < 0 || sampleX > SampleSize.Width) { continue; } if (sampleY < 0 || sampleY > SampleSize.Height) { continue; } PixelData *samplePixel = GetPixelDataAt( sampleData, sampleX, sampleY, SampleScanLineWidth); float distance = ColorUtils.GetSquareLinearDistance( masterPixel, samplePixel); if (minSquareDistance > distance) { aboveFound = true; aboveDistance = distance; } else if (distance > maxSquareDistance) { belowFound = true; belowDistance = distance; } else { // Acceptable value found. return; } } if (aboveFound) { result.AddDifference( new ColorDistanceDifference(x, y, minSquareDistance, aboveDistance, ValueComparison.AboveValue)); } else { System.Diagnostics.Debug.Assert(belowFound); result.AddDifference(new ColorDistanceDifference( x, y, maxSquareDistance, belowDistance, ValueComparison.BelowValue)); } errorCount++; }