Skip to content

Commit

Permalink
Swap from big integer to stringbuilder creation
Browse files Browse the repository at this point in the history
  • Loading branch information
KilianB committed Jan 6, 2019
1 parent da3db29 commit afc026d
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AverageColorHash(int bitResolution) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {
FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));

int[][] grayscale = fp.getAverageGrayscale();
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/github/kilianB/hashAlgorithms/AverageHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public AverageHash(int bitResolution) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {
FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));

int[][] luminocity = fp.getLuma();
Expand All @@ -72,30 +72,30 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
return computeHash(hash, luminocity, avgPixelValue);
}

protected BigInteger computeHash(BigInteger hash, double[][] pixelValue, double compareAgainst) {
protected BigInteger computeHash(StringBuilder hash, double[][] pixelValue, double compareAgainst) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (pixelValue[x][y] < compareAgainst) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
return hash;
return new BigInteger(hash.toString(),2);
}

protected BigInteger computeHash(BigInteger hash, int[][] pixelValue, double compareAgainst) {
protected BigInteger computeHash(StringBuilder hash, int[][] pixelValue, double compareAgainst) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (pixelValue[x][y] < compareAgainst) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
return hash;
return new BigInteger(hash.toString(),2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public AverageKernelHash(int bitResolution, Kernel... kernels) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {

FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));

Expand All @@ -105,7 +105,6 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
// Calculate the average color of the entire image

// Kernel filter

double[][] filtered = null;

for (Kernel kernel : filters) {
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/github/kilianB/hashAlgorithms/DifferenceHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public DifferenceHash(int bitResolution, Precision precision) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {
FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));
// Use data buffer for faster access

Expand All @@ -103,9 +103,9 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
for (int x = 1; x < width; x++) {
for (int y = 0; y < height; y++) {
if (lum[x][y] >= lum[x - 1][y]) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
Expand All @@ -118,9 +118,9 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
for (int x = 0; x < width; x++) {
for (int y = 1; y < height; y++) {
if (lum[x][y] < lum[x][y - 1]) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
Expand All @@ -131,14 +131,14 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
for (int x = 1; x < width; x++) {
for (int y = 1; y < height; y++) {
if (lum[x][y] < lum[x - 1][y - 1]) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
}
return hash;
return new BigInteger(hash.toString(),2);
}

/**
Expand Down Expand Up @@ -188,6 +188,11 @@ public Hash createAlgorithmSpecificHash(Hash original) {
return new DHash(original, this.precision, width, height);
}

/**
* An extended hash class allowing dhashes to be visually represented.
* @author Kilian
* @since 3.0.0
*/
public static class DHash extends Hash {

private Precision precision;
Expand Down Expand Up @@ -273,9 +278,6 @@ private int drawDoublePrecision(FastPixel writer, int width, int wOffset, int he
}
}

/**
* @return
*/
public Precision getPrecision() {
return precision;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public Hash hash(BufferedImage image) {
}
}
immutableState = true;
return new Hash(hash(bi, BigInteger.ZERO), getKeyResolution(), algorithmId());

return new Hash(hash(bi, new StringBuilder(getKeyResolution())), getKeyResolution(), algorithmId());
}

/**
Expand Down Expand Up @@ -151,10 +152,10 @@ public Hash hash(File imageFile) throws IOException {
* distance requires the potential length of the key to be known.
*
* @param image Image whose hash will be calculated
* @param hash the big integer used to store the hash value
* @param hashBuilder a stringBuilder used to construct the hash
* @return the hash encoded as a big integer
*/
protected abstract BigInteger hash(BufferedImage image, BigInteger hash);
protected abstract BigInteger hash(BufferedImage image, StringBuilder hashBuilder);

/**
* A unique id identifying the settings and algorithms used to generate the
Expand Down Expand Up @@ -223,8 +224,9 @@ public int getKeyResolution() {
// return value
if (keyResolution < 0) {
BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
// By preceding a ONE bit we don't fall victim to the 0 bit truncation.
keyResolution = this.hash(bi, BigInteger.ONE).bitLength() - 1;
StringBuilder sb = new StringBuilder(this.bitResolution);
this.hash(bi, sb);
keyResolution = sb.length();
}
return keyResolution;
}
Expand Down Expand Up @@ -290,11 +292,12 @@ public boolean removeFilter(Filter filter) {
* certain behavior, in particular the
* {@link com.github.kilianB.matcher.Hash#toImage(int)} is likely to differ.
*
* <p> If the algorithm does not utilize a special hash sub class this
* method simply returns the supplied argument.
* <p>
* If the algorithm does not utilize a special hash sub class this method
* returns the supplied argument.
*
* @param original
* @return
* @param original the hash to transform
* @return a hash as it would be created by this algorithm.
* @since 3.0.0
*/
public Hash createAlgorithmSpecificHash(Hash original) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public MedianHash(int bitResolution) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {
FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));

int[] lum = fp.getLuma1D();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PerceptiveHash(int bitResolution) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {
FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));

int[][] lum = fp.getLuma();
Expand Down Expand Up @@ -82,13 +82,13 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
for (int j = 1; j < subHeight + 1; j++) {

if (lumAsDouble[i][j] < avg) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
return hash;
return new BigInteger(hash.toString(),2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public RotAverageHash(int bitResolution) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {

FastPixel fp = FastPixel.create(ImageUtil.getScaledInstance(image, width, height));

Expand Down Expand Up @@ -127,13 +127,13 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
// 0 bucket does not contain any value.
for (int i = 2; i < hashArr.length; i++) {
if (hashArr[i] >= hashArr[i - 1]) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}

return hash;
return new BigInteger(hash.toString(),2);
}

/**
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/com/github/kilianB/hashAlgorithms/RotPHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public RotPHash(int bitResolution, boolean truncateKey) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {

// 0. Preprocessing. Extract Luminosity
BufferedImage transformed = ImageUtil.getScaledInstance(image, width, height);
Expand Down Expand Up @@ -156,18 +156,18 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {

// We discard parts of the information of the last layer if we need a specific
// length key
if (this.truncateKey && length == bitResolution - 1)
if (this.truncateKey && length == bitResolution)
break;

if (arr[j] >= avg) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
length++;
}
}
return hash;
return new BigInteger(hash.toString(),2);
}

/**
Expand All @@ -185,21 +185,21 @@ protected int computePartition(double originalX, double originalY) {
return (int) (distance / widthPerSection);
}

@Override
public int getKeyResolution() {
// We can compute this more quickly than the super class. so we might as well do
// it
if (keyResolution < 0) {
if (truncateKey) {
keyResolution = this.bitResolution;
} else {
BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
keyResolution = this.hash(bi, BigInteger.ONE).bitLength() - 1;
}

}
return keyResolution;
}
// @Override
// public int getKeyResolution() {
// // We can compute this more quickly than the super class. so we might as well do
// // it
// if (keyResolution < 0) {
// if (truncateKey) {
// keyResolution = this.bitResolution;
// } else {
// BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
// keyResolution = this.hash(bi, BigInteger.ONE).bitLength() - 1;
// }
//
// }
// return keyResolution;
// }

@Override
protected int precomputeAlgoId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public HogHash(int bitResolution) {
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {

BufferedImage bi = ImageUtil.getScaledInstance(image, width, height);
FastPixel fp = FastPixel.create(bi);
Expand Down Expand Up @@ -191,14 +191,14 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
}
for (int bin = 0; bin < numBins; bin++) {
if (bin == maxIndex) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
}
return hash;
return new BigInteger(hash.toString(),2);
}

protected int[][][] computeHogFeatures(int[][] lum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public HogHashAngularEncoded(int width, int height, int cellWidth, int numBins)
}

@Override
protected BigInteger hash(BufferedImage image, BigInteger hash) {
protected BigInteger hash(BufferedImage image, StringBuilder hash) {

BufferedImage bi = ImageUtil.getScaledInstance(image, width, height);
FastPixel fp = FastPixel.create(bi);
Expand Down Expand Up @@ -84,15 +84,15 @@ protected BigInteger hash(BufferedImage image, BigInteger hash) {
for (int yCell = 0; yCell < yCells; yCell++) {
for (int bin = 0; bin < numBins; bin++) {
if (hog[xCell][yCell][bin] > binAverage[bin]) {
hash = hash.shiftLeft(1);
hash.append("0");
} else {
hash = hash.shiftLeft(1).add(BigInteger.ONE);
hash.append("1");
}
}
}
}

return hash;
return new BigInteger(hash.toString(),2);
}

}
Loading

0 comments on commit afc026d

Please sign in to comment.