Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #995 from farkam135/SpeedOptimizations
Browse files Browse the repository at this point in the history
Speed optimizations
  • Loading branch information
nahojjjen authored Oct 20, 2018
2 parents d2941d4 + 97f3d63 commit c0b760a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@
import com.kamron.pogoiv.scanlogic.Pokemon;
import com.kamron.pogoiv.scanlogic.PokemonNameCorrector;
import com.kamron.pogoiv.utils.LevelRange;
import com.kamron.pogoiv.utils.StringUtils;
import com.kamron.pogoiv.utils.fractions.Fraction;
import com.kamron.pogoiv.widgets.PokemonSpinnerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand Down Expand Up @@ -74,16 +71,12 @@ public class InputFraction extends Fraction {

private Pokefly pokefly;
private PokeInfoCalculator pokeInfoCalculator;
private final Map<String, Pokemon> normalizedPokemonNameMap;

public InputFraction(@NonNull Pokefly pokefly) {
this.pokefly = pokefly;
this.pokeInfoCalculator = PokeInfoCalculator.getInstance();
Map<String, Pokemon> pokemap = new HashMap<>();
for (Pokemon pokemon : pokeInfoCalculator.getPokedex()) {
pokemap.put(StringUtils.normalize(pokemon.toString()), pokemon); // set display pokemon name as key
}
this.normalizedPokemonNameMap = pokemap;


}

@Override public int getLayoutResId() {
Expand Down Expand Up @@ -294,13 +287,20 @@ public String apply(T input) {
*/
private Pokemon interpretWhichPokemonUserInput() {
//below picks a pokemon from either the pokemon spinner or the user text input
Pokemon pokemon;
Pokemon pokemon = null;
if (pokeInputSpinner.getVisibility() == View.VISIBLE) { //user picked pokemon from spinner
//This could be pokemon = pokeInputSpinner.getSelectedItem(); if they didn't give it type Object.
pokemon = pokeInputAdapter.getItem(pokeInputSpinner.getSelectedItemPosition());
} else { //user typed manually
String userInput = autoCompleteTextView1.getText().toString();
pokemon = normalizedPokemonNameMap.get(StringUtils.normalize(userInput));
int lowestDist = Integer.MAX_VALUE;
for (Pokemon poke : pokeInfoCalculator.getPokedex()) {
int dist = Data.levenshteinDistance(poke.name, userInput);
if (dist < lowestDist) {
lowestDist = dist;
pokemon = poke;
}
}
if (pokemon == null) { //no such pokemon was found, show error toast and abort showing results
Toast.makeText(pokefly, userInput + pokefly.getString(R.string.wrong_pokemon_name_input),
Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,50 +158,6 @@ private static Bitmap replaceColors(Bitmap srcBitmap, boolean mutateSrc, int kee
return dstBitmap;
}

private static Bitmap replaceColors(Bitmap srcBitmap, boolean mutateSrc,
int r, int g, int b, @Nullable Integer replaceColor,
@Nullable Float dH, @Nullable Float dS, @Nullable Float dV) {
int[] allPixels = new int[srcBitmap.getHeight() * srcBitmap.getWidth()];
srcBitmap.getPixels(allPixels, 0, srcBitmap.getWidth(), 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());

final int bgColor;
if (replaceColor != null) {
bgColor = replaceColor;
} else {
bgColor = allPixels[0]; // Sample the top left color to use as background
}

float[] targetHsv = new float[3];
Color.RGBToHSV(r, g, b, targetHsv);

float[] currentHsv = new float[3];
for (int i = 0; i < allPixels.length; i++) {
if (allPixels[i] == bgColor) {
continue;
}

Color.colorToHSV(allPixels[i], currentHsv);

if (dH != null && Math.abs(targetHsv[0] - currentHsv[0]) > dH) { // Check hue
allPixels[i] = bgColor;

} else if (dS != null && Math.abs(targetHsv[1] - currentHsv[1]) > dS) { // Check saturation
allPixels[i] = bgColor;

} else if (dV != null && Math.abs(targetHsv[2] - currentHsv[2]) > dV) { // Check value
allPixels[i] = bgColor;
}
}

Bitmap dstBitmap;
if (mutateSrc) {
dstBitmap = srcBitmap;
} else {
dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
}
dstBitmap.setPixels(allPixels, 0, srcBitmap.getWidth(), 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
return dstBitmap;
}

/**
* Scans the arc and tries to determine the pokemon level, returns 1 if nothing found.
Expand Down Expand Up @@ -424,7 +380,7 @@ private static Optional<Integer> getPokemonEvolutionCostFromImg(@NonNull Bitmap
}
}

movesetImage = replaceColors(movesetImage, true, 68, 105, 108, null, 3f, null, 0.1f);
movesetImage = replaceColors(movesetImage, true, 68, 105, 108, Color.BLACK, 50, false);

tesseract.setImage(movesetImage);
tesseract.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK);
Expand Down Expand Up @@ -1084,10 +1040,14 @@ public ScanData scanPokemon(@NonNull GoIVSettings settings,
}
Optional<Integer> evolutionCost = getPokemonEvolutionCostFromImg(pokemonImage,
ScanArea.calibratedFromSettings(POKEMON_EVOLUTION_COST_AREA, settings, luckyOffset));
Pair<String, String> moveset = getMovesetFromImg(pokemonImage,
estimatedLevelRange,
ScanArea.calibratedFromSettings(POKEMON_POWER_UP_CANDY_COST, settings, luckyOffset),
ScanArea.calibratedFromSettings(POKEMON_EVOLUTION_COST_AREA, settings, luckyOffset));
Pair<String, String> moveset = null;
if (requestFullScan) {
moveset = getMovesetFromImg(pokemonImage,
estimatedLevelRange,
ScanArea.calibratedFromSettings(POKEMON_POWER_UP_CANDY_COST, settings, luckyOffset),
ScanArea.calibratedFromSettings(POKEMON_EVOLUTION_COST_AREA, settings, luckyOffset));
}

String moveFast = null;
String moveCharge = null;
if (moveset != null) {
Expand Down

0 comments on commit c0b760a

Please sign in to comment.