Skip to content

Commit 201b24b

Browse files
committed
Add the ability to define a default color for opaque pixels. #38
1 parent bbb5424 commit 201b24b

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

Diff for: pom.xml

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<modelVersion>4.0.0</modelVersion>
88
<groupId>com.github.kilianB</groupId>
99
<artifactId>JImageHash</artifactId>
10-
<version>3.0.0</version>
10+
<version>3.0.1-SNAPSHOT</version>
1111

1212
<properties>
1313
<bintrayRepository>maven</bintrayRepository>
@@ -46,11 +46,17 @@
4646

4747
<dependencies>
4848

49+
<dependency>
50+
<groupId>com.github.sarxos</groupId>
51+
<artifactId>webcam-capture</artifactId>
52+
<version>0.3.12</version>
53+
</dependency>
54+
4955
<!-- Fast image access methods and overall utility code -->
5056
<dependency>
5157
<groupId>com.github.kilianB</groupId>
5258
<artifactId>UtilityCode</artifactId>
53-
<version>1.5.7</version>
59+
<version>1.5.10-SNAPSHOT</version>
5460
</dependency>
5561

5662
<!-- Required for perceptive hasher -->

Diff for: src/main/java/com/github/kilianB/hashAlgorithms/HashingAlgorithm.java

+60-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.kilianB.hashAlgorithms;
22

3+
import java.awt.Color;
34
import java.awt.image.BufferedImage;
45
import java.io.File;
56
import java.io.IOException;
@@ -64,6 +65,12 @@ public abstract class HashingAlgorithm implements Serializable {
6465
*/
6566
private int algorithmId;
6667

68+
/** Color used in replacement of opaque pixels */
69+
protected Color opaqueReplacementColor = Color.WHITE;
70+
71+
/** Maximum alpha value a pixel must have in order to be replaced */
72+
protected double opaqueReplacementThreshold = 2;
73+
6774
/**
6875
* After a hash was created or the id was calculated the object may not be
6976
* altered anymore.
@@ -78,10 +85,9 @@ public abstract class HashingAlgorithm implements Serializable {
7885
+ "and therefore invalidate further modification requests";
7986

8087
/**
81-
* Promises a key with approximately bit resolution. Due to
82-
* geometric requirements the key might be marginally larger or smaller than
83-
* specified. Hashing algorithms shall try to at least provide the number of
84-
* bits specified
88+
* Promises a key with approximately bit resolution. Due to geometric
89+
* requirements the key might be marginally larger or smaller than specified.
90+
* Hashing algorithms shall try to at least provide the number of bits specified
8591
*
8692
* @param bitResolution The bit count of the final hash
8793
*/
@@ -91,6 +97,48 @@ public HashingAlgorithm(int bitResolution) {
9197
"The bit resolution for hashing algorithms has to be positive");
9298
}
9399

100+
/**
101+
* Define how the algorithm shall handle images with alpha value. Hashing
102+
* algorithms usually depend on the luminosity value, which by default will be
103+
* treated as being black.
104+
* <p>
105+
* Sometimes display software may choose to display missing pixels in a
106+
* different color e.g. white. For the algorithm this would result in an
107+
* entirely black image while for the user these images are perceptually
108+
* different.
109+
*
110+
* @param replacementColor The color used to replace opaque values
111+
* @param alphaThreshold All colors with a value lower or equal value [0-1]
112+
* will be replaced.
113+
* <ul>
114+
* <li>0 means only invisible (entirely opaque pixels
115+
* will be replaced)</li>
116+
* <li></li>
117+
* </ul>
118+
* @since 3.0.1
119+
*/
120+
public void setOpaqueHandling(Color replacementColor, double alphaThreshold) {
121+
this.opaqueReplacementColor = replacementColor;
122+
this.opaqueReplacementThreshold = alphaThreshold;
123+
}
124+
125+
/**
126+
* @return color used in replacement of opaque pixels
127+
* @since 3.0.1
128+
*/
129+
public Color getOpaqueReplacementColor() {
130+
return opaqueReplacementColor;
131+
}
132+
133+
/**
134+
* @return the maximum alpha value a pixel must have in order to be replaced by
135+
* the opaque replacement color.
136+
* @since 3.0.1
137+
*/
138+
public double getOpaqueReplacementThreshold() {
139+
return opaqueReplacementThreshold;
140+
}
141+
94142
/**
95143
* Calculate hashes for the given images. Invoking the hash function on the same
96144
* image has to return the same hash value. A comparison of the hashes relates
@@ -104,13 +152,13 @@ public HashingAlgorithm(int bitResolution) {
104152
*/
105153
public Hash[] hash(BufferedImage... images) {
106154
Hash[] returnValue = new Hash[images.length];
107-
108-
for(int i = 0; i < images.length; i++) {
155+
156+
for (int i = 0; i < images.length; i++) {
109157
returnValue[i] = this.hash(images[i]);
110158
}
111159
return returnValue;
112160
}
113-
161+
114162
/**
115163
* Calculate hashes for the given images. Invoking the hash function on the same
116164
* image has to return the same hash value. A comparison of the hashes relates
@@ -125,14 +173,13 @@ public Hash[] hash(BufferedImage... images) {
125173
*/
126174
public Hash[] hash(File... imageFiles) throws IOException {
127175
Hash[] returnValue = new Hash[imageFiles.length];
128-
129-
for(int i = 0; i < imageFiles.length; i++) {
176+
177+
for (int i = 0; i < imageFiles.length; i++) {
130178
returnValue[i] = this.hash(imageFiles[i]);
131179
}
132180
return returnValue;
133181
}
134-
135-
182+
136183
/**
137184
* Calculate a hash for the given image. Invoking the hash function on the same
138185
* image has to return the same hash value. A comparison of the hashes relates
@@ -224,8 +271,8 @@ public Hash hash(File imageFile) throws IOException {
224271
public final int algorithmId() {
225272
if (algorithmId == 0) {
226273
algorithmId = 31 * precomputeAlgoId();
227-
//Make sure the algo id doesn't collide with version 2.0.0 id's
228-
algorithmId = 31 * algorithmId + 5 + preProcessing.hashCode();
274+
// Make sure the algo id doesn't collide with version 2.0.0 id's
275+
algorithmId = 31 * algorithmId + 5 + preProcessing.hashCode();
229276
immutableState = true;
230277
}
231278
return algorithmId;

0 commit comments

Comments
 (0)