UniquePixelIdentifier upiTopLeft = new UniquePixelIdentifier(boundingBoxTopLeft.x(),
boundingBoxTopLeft.y(), image.getWidth(), image.getHeight());
UniquePixelIdentifier upiBottomRight = new UniquePixelIdentifier(boundingBoxBottomRight.x(),
boundingBoxBottomRight.y(), image.getWidth(), image.getHeight());
Coordinate coordinateTopLeft = getCachedCoordinate(upiTopLeft);
Coordinate coordinateBottomRight = getCachedCoordinate(upiBottomRight);
float xStepSize = (float) (coordinateBottomRight.x() - coordinateTopLeft.x())
/ (float) (xSamples - 1);
float yStepSize = (float) (coordinateBottomRight.y() - coordinateTopLeft.y())
/ (float) (ySamples - 1);
debugLog.debug("relative pixel bounding box: topLeft={},{} bottomRight={},{} stepSize={},{}",
coordinateTopLeft.x(), coordinateTopLeft.y(), coordinateBottomRight.x(),
coordinateBottomRight.y(), xStepSize, yStepSize);
for (int yCount = 0; yCount < ySamples; yCount++) {
int y = coordinateTopLeft.y() + (int) (yCount * xStepSize);
for (int xCount = 0; xCount < xSamples; xCount++) {
int x = coordinateTopLeft.x() + (int) (xCount * xStepSize);
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);
// The two bounding box pixels might have different colour ranges, so
// test both: if either one matches than this pixel is considered to be
// a match
if (red >= boundingBoxTopLeft.minRed && red <= boundingBoxTopLeft.maxRed
&& green >= boundingBoxTopLeft.minGreen && green <= boundingBoxTopLeft.maxGreen
&& blue >= boundingBoxTopLeft.minBlue && blue <= boundingBoxTopLeft.maxBlue) {
// This pixel is inside the expected range so it's an immediate match
debugLog.debug("a matched reference pixel at {},{}", x, y);
return new Coordinate(x, y);
}
if (red >= boundingBoxBottomRight.minRed && red <= boundingBoxBottomRight.maxRed
&& green >= boundingBoxBottomRight.minGreen && green <= boundingBoxBottomRight.maxGreen
&& blue >= boundingBoxBottomRight.minBlue && blue <= boundingBoxBottomRight.maxBlue) {
// This pixel is inside the expected range so it's an immediate match
debugLog.debug("b matched reference pixel at {},{}", x, y);
return new Coordinate(x, y);
}
}
}