1
1
package com .github .kilianB .hashAlgorithms ;
2
2
3
+ import java .awt .Color ;
3
4
import java .awt .image .BufferedImage ;
4
5
import java .io .File ;
5
6
import java .io .IOException ;
@@ -64,6 +65,12 @@ public abstract class HashingAlgorithm implements Serializable {
64
65
*/
65
66
private int algorithmId ;
66
67
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
+
67
74
/**
68
75
* After a hash was created or the id was calculated the object may not be
69
76
* altered anymore.
@@ -78,10 +85,9 @@ public abstract class HashingAlgorithm implements Serializable {
78
85
+ "and therefore invalidate further modification requests" ;
79
86
80
87
/**
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
85
91
*
86
92
* @param bitResolution The bit count of the final hash
87
93
*/
@@ -91,6 +97,48 @@ public HashingAlgorithm(int bitResolution) {
91
97
"The bit resolution for hashing algorithms has to be positive" );
92
98
}
93
99
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
+
94
142
/**
95
143
* Calculate hashes for the given images. Invoking the hash function on the same
96
144
* image has to return the same hash value. A comparison of the hashes relates
@@ -104,13 +152,13 @@ public HashingAlgorithm(int bitResolution) {
104
152
*/
105
153
public Hash [] hash (BufferedImage ... images ) {
106
154
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 ++) {
109
157
returnValue [i ] = this .hash (images [i ]);
110
158
}
111
159
return returnValue ;
112
160
}
113
-
161
+
114
162
/**
115
163
* Calculate hashes for the given images. Invoking the hash function on the same
116
164
* image has to return the same hash value. A comparison of the hashes relates
@@ -125,14 +173,13 @@ public Hash[] hash(BufferedImage... images) {
125
173
*/
126
174
public Hash [] hash (File ... imageFiles ) throws IOException {
127
175
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 ++) {
130
178
returnValue [i ] = this .hash (imageFiles [i ]);
131
179
}
132
180
return returnValue ;
133
181
}
134
-
135
-
182
+
136
183
/**
137
184
* Calculate a hash for the given image. Invoking the hash function on the same
138
185
* 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 {
224
271
public final int algorithmId () {
225
272
if (algorithmId == 0 ) {
226
273
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 ();
229
276
immutableState = true ;
230
277
}
231
278
return algorithmId ;
0 commit comments