Skip to content

Commit

Permalink
v1.10.36
Browse files Browse the repository at this point in the history
Transparent fragments now can be found by findSimilar method
  • Loading branch information
1Ridav committed Feb 27, 2019
1 parent 3765b7f commit dacec8e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Install/news
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<title></title>
</head>
<body>
<h2 style="text-align: center;"><span style="font-size:26px;"><span style="font-family: tahoma,geneva,sans-serif;">Update 1.10.35 is now released!</span></span></h2>
<h2 style="text-align: center;"><span style="font-size:26px;"><span style="font-family: tahoma,geneva,sans-serif;">Update 1.10.36 is now released!</span></span></h2>

<p><span style="font-size:16px;">Update to the latest version by clicking the update button.</span></p>
<p><span style="font-size:16px;">MatrixPosition and MatrixRectangle are deprecated since 1.10.35, use Position and Region objects</span></p>
Expand Down
2 changes: 1 addition & 1 deletion Install/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.10.35
1.10.36
4 changes: 2 additions & 2 deletions src/main/java/bot/penguee/Frag.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ else if (diff < leastDifference) { // found better match
return bestResultMatrixPosition;
}

private static int pixelDiffARGB(int rgb1, int rgb2) {// A channel being ignored
protected static int pixelDiffARGB(int rgb1, int rgb2) {// A channel being ignored
return abs(((rgb1 >> 16) & 0xff) - ((rgb2 >> 16) & 0xff)) + abs(((rgb1 >> 8) & 0xff) - ((rgb2 >> 8) & 0xff))
+ abs((rgb1 & 0xff) - (rgb2 & 0xff));
}

private static int abs(int i) {
protected static int abs(int i) {
return (i + (i >> 31)) ^ (i >> 31);
}

Expand Down
68 changes: 49 additions & 19 deletions src/main/java/bot/penguee/FragTransparent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import javax.imageio.ImageIO;

public class FragTransparent extends Frag {
private int[][] monoXY_Map = null; // much faster than using array of objects
private int[][] XY_Map = null; // much faster than using array of objects

FragTransparent(String path) throws Exception {
super();
File f = new File(path);
image = ImageIO.read(f);
rgbData = loadFromFile(image);
prepareMonoPixelMap();
preparePixelMap();
}

private void prepareMonoPixelMap() {
private void preparePixelMap() {
int[] row_cache = null;
final int line = rgbData.length;
final int column = rgbData[0].length;
Expand All @@ -34,11 +34,11 @@ private void prepareMonoPixelMap() {
al.add(new Point(x, y));
}

monoXY_Map = new int[2][al.size()];
XY_Map = new int[2][al.size()];
for (int i = 0; i < al.size(); i++) {
Point p = al.get(i);
monoXY_Map[0][i] = (int) p.getX();
monoXY_Map[1][i] = (int) p.getY();
XY_Map[0][i] = (int) p.getX();
XY_Map[1][i] = (int) p.getY();
}
}

Expand Down Expand Up @@ -74,18 +74,48 @@ public void makeFile(String name) throws Exception {
}

public Position findSimilarIn(Frag b, double rate, int x_start, int y_start, int x_stop, int y_stop) {
return null;
// precalculate all frequently used data
final int[][] small = this.rgbData;
final int[][] big = b.rgbData;
final long maxDiff = 3 * 255 * XY_Map[0].length;
// similarity rate 95% is equal to 5% difference rate.
// if differences reached this number, then no need to check the rest, continue
// to next position
final long maxBreakDiff = (long) ((1 - rate) * maxDiff);
long leastDifference = Long.MAX_VALUE;
Position bestResultMatrixPosition = null;

final int[] rowY = XY_Map[1];
final int[] rowX = XY_Map[0];
final int pixelMapLen = rowX.length;
for (int y = y_start; y < y_stop; y++) {
__columnscan: for (int x = x_start; x < x_stop; x++) {
long diff = 0;// sum difference values
for (int yy = 0; yy < pixelMapLen; yy++)
diff += pixelDiffARGB(big[y + rowY[yy]][x + rowX[yy]], small[rowY[yy]][rowX[yy]]);
if (diff > maxBreakDiff)
continue __columnscan; // no match

if (diff == 0)
return new Position(x, y); // full match
else if (diff < leastDifference) { // found better match
leastDifference = diff;
bestResultMatrixPosition = new Position(x, y);
}
}
}
return bestResultMatrixPosition;
}

public Position findIn(Frag b, int x_start, int y_start, int x_stop, int y_stop) {
final int[][] big = b.rgbData;
final int[][] small = rgbData;
final int[] monoY = monoXY_Map[1];
final int[] monoX = monoXY_Map[0];
final int jumpX = monoX[0];
final int jumpY = monoY[0];
final int[] rowY = XY_Map[1];
final int[] rowX = XY_Map[0];
final int jumpX = rowX[0];
final int jumpY = rowY[0];
final int first_pixel = small[jumpY][jumpX];
final int pixelMapLen = monoX.length;
final int pixelMapLen = rowX.length;

int[] row_cache_big = null;
for (int y = y_start; y < y_stop; y++) {
Expand All @@ -96,7 +126,7 @@ public Position findIn(Frag b, int x_start, int y_start, int x_stop, int y_stop)
// There is a match for the first element in small
// Check if all the elements in small matches those in big
for (int yy = 0; yy < pixelMapLen; yy++)
if (big[y + monoY[yy]][x + monoX[yy]] != small[monoY[yy]][monoX[yy]])
if (big[y + rowY[yy]][x + rowX[yy]] != small[rowY[yy]][rowX[yy]])
continue __columnscan;
return new Position(x, y);
}
Expand All @@ -107,13 +137,13 @@ public Position findIn(Frag b, int x_start, int y_start, int x_stop, int y_stop)
public Position[] findAllIn(Frag b, int x_start, int y_start, int x_stop, int y_stop) {
final int[][] big = b.rgbData;
final int[][] small = rgbData;
final int[] monoY = monoXY_Map[1];
final int[] monoX = monoXY_Map[0];
final int jumpX = monoX[0];
final int jumpY = monoY[0];
final int[] rowY = XY_Map[1];
final int[] rowX = XY_Map[0];
final int jumpX = rowX[0];
final int jumpY = rowY[0];
final int first_pixel = small[jumpY][jumpX];
;
final int pixelMapLen = monoX.length;
final int pixelMapLen = rowX.length;

ArrayList<Position> result = null;
Position matrix_position_list[] = null;
Expand All @@ -126,7 +156,7 @@ public Position[] findAllIn(Frag b, int x_start, int y_start, int x_stop, int y_
// There is a match for the first element in small
// Check if all the elements in small matches those in big
for (int yy = 0; yy < pixelMapLen; yy++)
if (big[y + monoY[yy]][x + monoX[yy]] != small[monoY[yy]][monoX[yy]])
if (big[y + rowY[yy]][x + rowX[yy]] != small[rowY[yy]][rowX[yy]])
continue __columnscan;
// If arrived here, then the small matches a region of big
if (result == null)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/bot/penguee/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.net.URL;

public class Update {
public final static String version = "1.10.35";
public final static String version = "1.10.36";
private static String versionURL = "https://github.com/raw/1Ridav/PengueeBot/master/Install/version";
private static String downloadURL = "https://github.com/1Ridav/PengueeBot/releases/download/v";
private static String newsURL = "https://github.com/raw/1Ridav/PengueeBot/master/Install/news";
Expand Down

0 comments on commit dacec8e

Please sign in to comment.