Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
more cleanup & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrcho committed Jun 15, 2014
1 parent c6ec3fd commit ebdaf7f
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,74 @@
import org.slf4j.LoggerFactory;

/**
* Tests whether an individual pixels, or set of pixels, matched the expected colour value.
*
* Tests whether an individual pixels, or set of pixels, matched the expected
* colour value.
*
* @author gtch
*/
public class IndividualPixelAnalyser extends CoordinateCacheBase {

private final static Logger debugLog = LoggerFactory.getLogger(IndividualPixelAnalyser.class);
private final static Logger debugLog = LoggerFactory.getLogger(IndividualPixelAnalyser.class);

boolean testAllPixelsMatch(BufferedImage image, UniquePixel[] uniquePixels) {
for (UniquePixel uniquePixel : uniquePixels) {

boolean testAllPixelsMatch(BufferedImage image, UniquePixel[] uniquePixels) {
for (UniquePixel uniquePixel : uniquePixels) {
UniquePixelIdentifier upi = new UniquePixelIdentifier(uniquePixel.x(), uniquePixel.y(),
image.getWidth(), image.getHeight());

UniquePixelIdentifier upi = new UniquePixelIdentifier(
uniquePixel.x(), uniquePixel.y(), image.getWidth(),
image.getHeight());
Coordinate coordinate = getCachedCoordinate(upi);

Coordinate coordinate = getCachedCoordinate(upi);
int x = coordinate.x();
int y = coordinate.y();

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);

int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);

if (red < uniquePixel.minRed || red > uniquePixel.maxRed
|| green < uniquePixel.minGreen || green > uniquePixel.maxGreen
|| blue < uniquePixel.minBlue || blue > uniquePixel.maxBlue) {
// This pixel is outside the expected range
return false;
}

}

// No pixel texts failed, so this is a match
debugLog.debug("matched all pixels {}", (Object[]) uniquePixels);
if (red < uniquePixel.minRed || red > uniquePixel.maxRed || green < uniquePixel.minGreen
|| green > uniquePixel.maxGreen || blue < uniquePixel.minBlue
|| blue > uniquePixel.maxBlue) {
// This pixel is outside the expected range
return false;
}

return true;
}

boolean testAnyPixelsMatch(BufferedImage image, UniquePixel[] uniquePixels) {
for (UniquePixel uniquePixel : uniquePixels) {
// No pixel texts failed, so this is a match
debugLog.debug("matched all pixels {}", (Object[]) uniquePixels);

UniquePixelIdentifier upi = new UniquePixelIdentifier(
uniquePixel.x(), uniquePixel.y(), image.getWidth(),
image.getHeight());
return true;
}

Coordinate coordinate = getCachedCoordinate(upi);
boolean testAnyPixelsMatch(BufferedImage image, UniquePixel[] uniquePixels) {
for (UniquePixel uniquePixel : uniquePixels) {

int x = coordinate.x();
int y = coordinate.y();
UniquePixelIdentifier upi = new UniquePixelIdentifier(uniquePixel.x(), uniquePixel.y(),
image.getWidth(), image.getHeight());

int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);
Coordinate coordinate = getCachedCoordinate(upi);

if (red >= uniquePixel.minRed && red <= uniquePixel.maxRed
&& green >= uniquePixel.minGreen && green <= uniquePixel.maxGreen
&& blue >= uniquePixel.minBlue && blue <= uniquePixel.maxBlue) {
// This pixel is inside the expected range so it's an immediate match
debugLog.debug("matched {} any from pixel {}", uniquePixel, uniquePixels);
return true;
}
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 >= uniquePixel.minRed && red <= uniquePixel.maxRed && green >= uniquePixel.minGreen
&& green <= uniquePixel.maxGreen && blue >= uniquePixel.minBlue
&& blue <= uniquePixel.maxBlue) {
// This pixel is inside the expected range so it's an immediate match
debugLog.debug("matched {} any from pixel {}", uniquePixel, uniquePixels);
return true;
}

// All pixel text failed, so this is not a match
return false;
}

// All pixel text failed, so this is not a match
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,121 +10,125 @@
import org.slf4j.LoggerFactory;

/**
* <p>Tests whether a set of pixels relative to a reference point match expected colour values.
* Unlike the {@link net.hearthstats.analysis.IndividualPixelAnalyser} which expects pixels to be in a particular spot,
* the RelativePixelAnalyser can handle objects in unknown position by finding a reference point across a broad section
* of the screen and then calculating all the other pixels relative to that reference point.</p>
* <p>This makes the RelativePixelAnalyser most suitable for moving objects like the victory and defect popups</p>
*
* <p>
* Tests whether a set of pixels relative to a reference point match expected
* colour values. Unlike the
* {@link net.hearthstats.analysis.IndividualPixelAnalyser} which expects pixels
* to be in a particular spot, the RelativePixelAnalyser can handle objects in
* unknown position by finding a reference point across a broad section of the
* screen and then calculating all the other pixels relative to that reference
* point.
* </p>
* <p>
* This makes the RelativePixelAnalyser most suitable for moving objects like
* the victory and defect popups
* </p>
*
* @author gtch
*/
public class RelativePixelAnalyser extends CoordinateCacheBase {

private final static Logger debugLog = LoggerFactory.getLogger(RelativePixelAnalyser.class);
private final static Logger debugLog = LoggerFactory.getLogger(RelativePixelAnalyser.class);

private float cachedRatio = 0;
private float lastImageHeight = 0;
private float cachedRatio = 0;
private float lastImageHeight = 0;

Coordinate findRelativePixel(BufferedImage image, UniquePixel boundingBoxTopLeft,
UniquePixel boundingBoxBottomRight, int xSamples, int ySamples) {

Coordinate findRelativePixel(BufferedImage image, UniquePixel boundingBoxTopLeft, UniquePixel boundingBoxBottomRight, int xSamples, int ySamples) {
UniquePixelIdentifier upiTopLeft = new UniquePixelIdentifier(boundingBoxTopLeft.x(),
boundingBoxTopLeft.y(), image.getWidth(), image.getHeight());
UniquePixelIdentifier upiBottomRight = new UniquePixelIdentifier(boundingBoxBottomRight.x(),
boundingBoxBottomRight.y(), image.getWidth(), image.getHeight());

boolean foundPixel = false;
Coordinate coordinateTopLeft = getCachedCoordinate(upiTopLeft);
Coordinate coordinateBottomRight = getCachedCoordinate(upiBottomRight);

UniquePixelIdentifier upiTopLeft = new UniquePixelIdentifier(
boundingBoxTopLeft.x(), boundingBoxTopLeft.y(),
image.getWidth(), image.getHeight());
UniquePixelIdentifier upiBottomRight = new UniquePixelIdentifier(
boundingBoxBottomRight.x(), boundingBoxBottomRight.y(),
image.getWidth(), image.getHeight());
float xStepSize = (float) (coordinateBottomRight.x() - coordinateTopLeft.x())
/ (float) (xSamples - 1);
float yStepSize = (float) (coordinateBottomRight.y() - coordinateTopLeft.y())
/ (float) (ySamples - 1);

Coordinate coordinateTopLeft = getCachedCoordinate(upiTopLeft);
Coordinate coordinateBottomRight = getCachedCoordinate(upiBottomRight);
debugLog.debug("relative pixel bounding box: topLeft={},{} bottomRight={},{} stepSize={},{}",
coordinateTopLeft.x(), coordinateTopLeft.y(), coordinateBottomRight.x(),
coordinateBottomRight.y(), xStepSize, yStepSize);

float xStepSize = (float) (coordinateBottomRight.x() - coordinateTopLeft
.x()) / (float) (xSamples - 1);
float yStepSize = (float) (coordinateBottomRight.y() - coordinateTopLeft
.y()) / (float) (ySamples - 1);
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);

debugLog.debug("relative pixel bounding box: topLeft={},{} bottomRight={},{} stepSize={},{}",
coordinateTopLeft.x(), coordinateTopLeft.y(),
coordinateBottomRight.x(), coordinateBottomRight.y(),
xStepSize, yStepSize);
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);

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);
// 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

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);
}

}
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);
}

return null;
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);
}

}
}

int countMatchingRelativePixels(BufferedImage image, Coordinate referencePixel, UniquePixel[] relativePixels) {
return null;

int matches = 0;
}

float ratio;
if (lastImageHeight == image.getHeight()) {
// Use the stored ratio
ratio = cachedRatio;
} else {
// Calculate the ratio and store it for next time
lastImageHeight = image.getHeight();
ratio = (float) lastImageHeight
/ (float) PixelLocation.REFERENCE_SIZE.y();
cachedRatio = ratio;
}
int countMatchingRelativePixels(BufferedImage image, Coordinate referencePixel,
UniquePixel[] relativePixels) {

int matches = 0;

for (UniquePixel relativePixel : relativePixels) {
int x = referencePixel.x() + (int) (relativePixel.x() * ratio);
int y = referencePixel.y() + (int) (relativePixel.y() * ratio);

int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);
float ratio;
if (lastImageHeight == image.getHeight()) {
// Use the stored ratio
ratio = cachedRatio;
} else {
// Calculate the ratio and store it for next time
lastImageHeight = image.getHeight();
ratio = (float) lastImageHeight / (float) PixelLocation.REFERENCE_SIZE.y();
cachedRatio = ratio;
}

if (red >= relativePixel.minRed && red <= relativePixel.maxRed
&& green >= relativePixel.minGreen && green <= relativePixel.maxGreen
&& blue >= relativePixel.minBlue && blue <= relativePixel.maxBlue) {
// This pixel is inside the expected range so it's an immediate match
debugLog.debug("relative pixel at {}, {} matched {}", x, y, relativePixel);
matches++;
} else {
debugLog.debug("relative pixel at {}, {} did not match {}", x, y, relativePixel);
}
for (UniquePixel relativePixel : relativePixels) {
int x = referencePixel.x() + (int) (relativePixel.x() * ratio);
int y = referencePixel.y() + (int) (relativePixel.y() * ratio);

int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = (rgb & 0xFF);

if (red >= relativePixel.minRed && red <= relativePixel.maxRed
&& green >= relativePixel.minGreen && green <= relativePixel.maxGreen
&& blue >= relativePixel.minBlue && blue <= relativePixel.maxBlue) {
// This pixel is inside the expected range so it's an immediate match
debugLog.debug("relative pixel at {}, {} matched {}", x, y, relativePixel);
matches++;
} else {
debugLog.debug("relative pixel at {}, {} did not match {}", x, y, relativePixel);
}

}
}

return matches;
return matches;

}
}

}
10 changes: 5 additions & 5 deletions uploader/src/main/java/net/hearthstats/ui/MatchPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import net.hearthstats.Constants;
import net.hearthstats.HearthstoneMatch;
import net.hearthstats.Main;
import net.miginfocom.swing.MigLayout;
import net.hearthstats.analysis.HearthstoneAnalyser;
import net.miginfocom.swing.MigLayout;


public class MatchPanel extends JPanel {
Expand Down Expand Up @@ -80,7 +80,7 @@ public MatchPanel() {
_currentOpponentNameField.setMinimumSize(new Dimension(100, 1));
_currentOpponentNameField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
HearthstoneAnalyser.getMatch().opponentName_$eq(
HearthstoneAnalyser.hsMatch().opponentName_$eq(
_currentOpponentNameField.getText().replaceAll("(\r\n|\n)", "<br/>"));
}
});
Expand All @@ -92,7 +92,7 @@ public void keyReleased(KeyEvent e) {
_currentGameCoinField.setSelected(Config.showHsClosedNotification());
_currentGameCoinField.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
HearthstoneAnalyser.getMatch().coin_$eq(_currentGameCoinField.isSelected());
HearthstoneAnalyser.hsMatch().coin_$eq(_currentGameCoinField.isSelected());
}
});
add(_currentGameCoinField, "wrap");
Expand All @@ -107,7 +107,7 @@ public void stateChanged(ChangeEvent e) {
_currentNotesField.setBackground(Color.WHITE);
_currentNotesField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
HearthstoneAnalyser.getMatch().notes_$eq(_currentNotesField.getText());
HearthstoneAnalyser.hsMatch().notes_$eq(_currentNotesField.getText());
}
});
add(_currentNotesField, "skip,span");
Expand Down Expand Up @@ -135,7 +135,7 @@ public void actionPerformed(ActionEvent arg0) {
}

public void updateCurrentMatchUi() {
HearthstoneMatch match = HearthstoneAnalyser.getMatch();
HearthstoneMatch match = HearthstoneAnalyser.hsMatch();
updateMatchClassSelectorsIfSet(match);
if (_currentMatchEnabled)
_currentMatchLabel.setText(match.mode() + " Match - " + " Turn " + match.numTurns());
Expand Down

0 comments on commit ebdaf7f

Please sign in to comment.