use of net.hearthstats.util.Coordinate in project HearthStats.net-Uploader by HearthStats.
the class CoordinateCacheBase method calculatePixelCoordinate.
protected Coordinate calculatePixelCoordinate(UniquePixelIdentifier upi) {
if (debugLog.isDebugEnabled()) {
debugLog.debug("Calculating pixel position {},{} for width {} height {}", new Object[] { upi.x, upi.y, upi.width, upi.height });
}
Coordinate result;
if (upi.width == PixelLocation.REFERENCE_SIZE.x && upi.height == PixelLocation.REFERENCE_SIZE.y) {
// The screen size is exactly what our reference pixels are based on, so we can use their coordinates directly
result = new Coordinate(upi.x, upi.y);
debugLog.debug("Stored position as {}", result);
} else {
// The screen size is different to our reference pixels, so coordinates need to be adjusted
float ratio = (float) upi.height / (float) PixelLocation.REFERENCE_SIZE.y;
float screenRatio = (float) upi.width / (float) upi.height;
int xOffset;
if (screenRatio > 1.4) {
xOffset = (int) (((float) upi.width - (ratio * PixelLocation.REFERENCE_SIZE.x)) / 2);
} else {
xOffset = 0;
}
int x = (int) (upi.x * ratio) + xOffset;
int y = (int) (upi.y * ratio);
result = new Coordinate(x, y);
debugLog.debug("Calculated position as {}", result);
}
return result;
}
use of net.hearthstats.util.Coordinate in project HearthStats.net-Uploader by HearthStats.
the class ScreenAnalyser method calculatePixelPositions.
/**
* Calculates the relative positions of our standard pixel locations given the
* specified screen width and height. Hearthstone can run in many different
* screen sizes so all pixel locations need to be adjusted accordingly.
*
* @param width
* the screen width to calculate positions for
* @param height
* the screen height to calculate positions for
*/
Map<PixelLocation, Coordinate> calculatePixelPositions(int width, int height) {
log.trace("Recalculating pixel position for width {} height {}", width, height);
Map<PixelLocation, Coordinate> result;
if (width == PixelLocation.REFERENCE_SIZE.x() && height == PixelLocation.REFERENCE_SIZE.y()) {
// The screen size is exactly what our reference pixels are based on, so
// we can use their coordinates directly
result = new HashMap<>();
for (PixelLocation pixelLocation : PixelLocation.values()) {
Coordinate coordinate = new Coordinate(pixelLocation.x(), pixelLocation.y());
log.debug("Stored position of {} as {}", pixelLocation, coordinate);
result.put(pixelLocation, coordinate);
}
} else {
// The screen size is different to our reference pixels, so coordinates
// need to be adjusted
float ratioX = (float) width / (float) PixelLocation.REFERENCE_SIZE.x();
float ratioY = (float) height / (float) PixelLocation.REFERENCE_SIZE.y();
// ratioY is normally the correct ratio to use, but occasionally ratioX is
// smaller (usually during screen resizing?)
float ratio = Math.min(ratioX, ratioY);
float screenRatio = (float) width / (float) height;
int xOffset;
if (screenRatio > 1.4) {
xOffset = (int) (((float) width - (ratio * PixelLocation.REFERENCE_SIZE.x())) / 2);
} else {
xOffset = 0;
}
log.debug("ratio={} screenRatio={}, xOffset={}", ratio, screenRatio, xOffset);
result = new HashMap<>();
for (PixelLocation pixelLocation : PixelLocation.values()) {
int x = (int) (pixelLocation.x() * ratio) + xOffset;
int y = (int) (pixelLocation.y() * ratio);
Coordinate coordinate = new Coordinate(x, y);
log.debug("Calculated position of {} as {}", pixelLocation, coordinate);
result.put(pixelLocation, coordinate);
}
}
return result;
}
use of net.hearthstats.util.Coordinate in project HearthStats.net-Uploader by HearthStats.
the class ScreenAnalyser method checkForExactMatch.
boolean checkForExactMatch(BufferedImage image, Screen screen) {
// Skip screens that haven't yet been defined
if (screen.primary.size() == 0) {
return false;
}
for (Pixel pixel : screen.primary) {
Coordinate coordinate = pixelMap.get(pixel.pixelLocation);
int x = coordinate.x();
int y = coordinate.y();
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);
if (red < pixel.minRed || red > pixel.maxRed || green < pixel.minGreen || green > pixel.maxGreen || blue < pixel.minBlue || blue > pixel.maxBlue) {
// This pixel is outside the expected range
return false;
}
}
// All pixels matched
return true;
}
use of net.hearthstats.util.Coordinate in project HearthStats.net-Uploader by HearthStats.
the class ScreenAnalyser method checkForMatchSecondary.
boolean checkForMatchSecondary(BufferedImage image, Screen screen) {
// Skip screens that haven't yet been defined
if (screen.primary.size() == 0) {
return false;
}
for (Pixel pixel : screen.secondary) {
Coordinate coordinate = pixelMap.get(pixel.pixelLocation);
int x = coordinate.x();
int y = coordinate.y();
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);
if (red < pixel.minRed || red > pixel.maxRed || green < pixel.minGreen || green > pixel.maxGreen || blue < pixel.minBlue || blue > pixel.maxBlue) {
// This pixel is outside the expected range
return false;
}
}
// All pixels matched
return true;
}
use of net.hearthstats.util.Coordinate in project HearthStats.net-Uploader by HearthStats.
the class RelativePixelAnalyserMain method writeCanvasJavascript.
/**
* Writes an inline Javascript which zooms in on the reference pixel area and highlights the reference pixel location.
*/
private void writeCanvasJavascript(BufferedWriter output, List<Coordinate> coordinates) throws IOException {
output.write("<script type=\"text/javascript\">\n" + "function drawPixel(id, x, y) {\n" + "\n" + " var canvas = document.getElementById(\"canvas_\" + id);\n" + " var ctx = canvas.getContext(\"2d\");\n" + " \n" + " var image = new Image();\n" + " image.onload = function()\n" + " {\n" + "\t\tvar xoff = Math.floor(x / 200) * 200 - 50;\n" + "\t\tvar yoff = Math.floor(y / 200) * 200 - 50;\n" + "\t\tctx.drawImage(image, xoff, yoff, 300, 300, 0, 0, 300, 300);\n" + "\t\tctx.beginPath();\n" + "\t\tx = x - xoff;\n" + "\t\ty = y - yoff;\n" + "\t\tctx.strokeStyle = \"rgba(255,255,255,0.8)\";\n" + "\t\tctx.lineWidth = 4\n" + "\t\tctx.strokeRect(x-6, y-6, 12, 12);\n" + "\t\tctx.strokeStyle = \"rgba(0,0,0,0.8)\";\n" + "\t\tctx.lineWidth = 1\n" + "\t\tctx.strokeRect(x-4, y-4, 8, 8);\n" + "\t\tctx.strokeRect(x-8, y-8, 16, 16);\n" + " }\n" + " image.src = document.getElementById(\"img_\" + id).src;\t\n" + "}\n" + "\n" + "window.addEventListener(\"DOMContentLoaded\", function()\n" + "{\n");
for (int i = 0; i < coordinates.size(); i++) {
Coordinate coordinate = coordinates.get(i);
if (coordinate != null) {
output.write("drawPixel(\"" + i + "\", " + coordinate.x() + ", " + coordinate.y() + ");\n");
}
}
output.write("});\n" + "</script>");
}
Aggregations