public LockBitsTools.IndexArray[] findColors(Color[] col, int[] tolerance = null, int[] limit = null) { // Create field for the matches of the maximum possible size LockBitsTools.IndexArray[] colorMatches = new LockBitsTools.IndexArray[col.Length]; for (int i = 0; i < colorMatches.Length; i++) { colorMatches[i] = new LockBitsTools.IndexArray(new int[limit[i]]); } int[] matchesFound = new int[col.Length]; for (int iColor = 0; iColor < col.Length; iColor++) { // Go through all the pixels for (int iPixel = 0; iPixel < Bytes.Length; iPixel += bytesPerPixel) { // If all the pixel colors match, save the current int // BEWARE! THE BYTES ARE IN REVERSE ORDER BECAUSE OF THE BYTE SIGNIFICANCE (BGR instead of RGB) if (MathTools.Compare(Bytes[iPixel], col[iColor].B, tolerance[iColor]) && MathTools.Compare(Bytes[iPixel + 1], col[iColor].G, tolerance[iColor]) && MathTools.Compare(Bytes[iPixel + 2], col[iColor].R, tolerance[iColor])) { // Save the index colorMatches[iColor].Indexes[matchesFound[iColor]] = iPixel; matchesFound[iColor]++; // If there are enough matches found already, break the loop if (matchesFound[iColor] >= limit[iColor]) { break; } } } } // Return the actual matches LockBitsTools.IndexArray[] foundMatches = new LockBitsTools.IndexArray[col.Length]; for (int i = 0; i < foundMatches.Length; i++) { foundMatches[i] = new LockBitsTools.IndexArray(new int[matchesFound[i]]); for (int j = 0; j < matchesFound[i]; j++) { foundMatches[i].Indexes[j] = colorMatches[i].Indexes[j]; } } return(foundMatches); }
public LockBitsTools.IndexArray[] findBitmaps(LockedBitmap[] lbTarget, int[] tolerance = null, int[] limit = null) { // If the limit is not set, use the default value if (limit == null) { limit = new int[lbTarget.Length]; for (int i = 0; i < limit.Length; i++) { limit[i] = defaultLimit; } } // If the tolerance is not set, use the default value if (tolerance == null) { tolerance = new int[lbTarget.Length]; for (int i = 0; i < tolerance.Length; i++) { tolerance[i] = defaultTolerance; } } // Get the parameters for findColor // BEWARE! THE BYTES ARE IN REVERSE ORDER BECAUSE OF THE BYTE SIGNIFICANCE (BGR instead of RGB) Color[] firstPixelColors = new Color[lbTarget.Length]; for (int i = 0; i < firstPixelColors.Length; i++) { firstPixelColors[i] = Color.FromArgb(lbTarget[i].Bytes[2], lbTarget[i].Bytes[1], lbTarget[i].Bytes[0]); } int searchAreaSize = Size.Width * Size.Height; int[] firstPixelColorsLimits = new int[limit.Length]; for (int i = 0; i < firstPixelColorsLimits.Length; i++) { firstPixelColorsLimits[i] = searchAreaSize; } // Search bitmap for all occurances of the first pixel color LockBitsTools.IndexArray[] firstPixelMatches = new LockBitsTools.IndexArray[lbTarget.Length]; firstPixelMatches = findColors(firstPixelColors, tolerance, firstPixelColorsLimits); // Create field for the matches of the maximum possible size LockBitsTools.IndexArray[] targetMatches = new LockBitsTools.IndexArray[lbTarget.Length]; for (int i = 0; i < targetMatches.Length; i++) { targetMatches[i] = new LockBitsTools.IndexArray(new int[limit[i]]); } int[] matchesFound = new int[lbTarget.Length]; // Go through all the targets for (int iTarget = 0; iTarget < targetMatches.Length; iTarget++) { // Go through all the first pixel color matches for (int iMatch = 0; iMatch < firstPixelMatches[iTarget].Indexes.Length; iMatch++) { bool isMatch = true; // Get the pixel offsets int matchIndex = firstPixelMatches[iTarget].Indexes[iMatch]; int xOffset = matchIndex % Stride; int yOffset = matchIndex - xOffset; // Both x and y are indexes in bytes for (int yTarget = 0, ySource = yOffset; yTarget < lbTarget[iTarget].Bytes.Length && isMatch; yTarget += lbTarget[iTarget].Stride, ySource += Stride) { for (int xTarget = 0, xSource = xOffset; xTarget < lbTarget[iTarget].ByteWidth && isMatch; xTarget += bytesPerPixel, xSource += bytesPerPixel) { // If all the pixel colors match, save the current point for (int j = 0; j < bytesPerPixel && isMatch; j++) { if (xSource + ySource < Bytes.Length) { isMatch = MathTools.Compare(Bytes[xSource + ySource + j], lbTarget[iTarget].Bytes[xTarget + yTarget + j], tolerance[iTarget]); } else { isMatch = false; } } } } if (isMatch) { // Save the found match targetMatches[iTarget].Indexes[matchesFound[iTarget]] = firstPixelMatches[iTarget].Indexes[iMatch]; matchesFound[iTarget]++; // If there are enough matches found already, break the loop if (matchesFound[iTarget] >= limit[iTarget]) { break; } } } } // Return the actual matches LockBitsTools.IndexArray[] foundMatches = new LockBitsTools.IndexArray[targetMatches.Length]; for (int i = 0; i < foundMatches.Length; i++) { foundMatches[i] = new LockBitsTools.IndexArray(new int[matchesFound[i]]); for (int j = 0; j < matchesFound[i]; j++) { foundMatches[i].Indexes[j] = targetMatches[i].Indexes[j]; } } return(foundMatches); }