Skip to content

Commit 5669786

Browse files
committed
optimize CRC32 computation
1 parent f30171d commit 5669786

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/main/java/com/pdiff/core/ImageSignature.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,40 @@ private ImageSignature(int width, int height, long crc) {
2222
}
2323

2424
private ImageSignature(BufferedImage im, boolean ignoreFirstPoint) {
25+
final int w = im.getWidth();
26+
final int h = im.getHeight();
27+
2528
final CRC32 crc = new CRC32();
26-
for (int x = 0; x < im.getWidth(); x++) {
27-
for (int y = 0; y < im.getHeight(); y++) {
28-
if (ignoreFirstPoint) {
29-
ignoreFirstPoint = false;
30-
} else {
31-
final int c = im.getRGB(x, y);
32-
updateCRC(crc, c);
33-
}
29+
30+
final int[] line = new int[w];
31+
final byte[] buf = new byte[w * 3];
32+
33+
for (int y = 0; y < h; y++) {
34+
im.getRGB(0, y, w, 1, line, 0, w);
35+
36+
int startX = 0;
37+
if (ignoreFirstPoint) {
38+
startX = 1;
39+
ignoreFirstPoint = false;
3440
}
41+
42+
int bi = 0;
43+
for (int x = startX; x < w; x++) {
44+
final int c = line[x];
45+
buf[bi++] = (byte) (c >>> 16); // R
46+
buf[bi++] = (byte) (c >>> 8); // G
47+
buf[bi++] = (byte) c; // B
48+
}
49+
50+
crc.update(buf, 0, bi);
51+
3552
}
36-
this.width = im.getWidth();
37-
this.height = im.getHeight();
53+
54+
this.width = w;
55+
this.height = h;
3856
this.crc = crc.getValue();
3957
}
40-
41-
private void updateCRC(CRC32 crc, int c) {
42-
final int r = (c & 0xFF0000) >> 16;
43-
final int g = (c & 0x00FF00) >> 8;
44-
final int b = (c & 0x0000FF);
45-
crc.update(r);
46-
crc.update(g);
47-
crc.update(b);
48-
}
58+
4959

5060
public static ImageSignature fromImage(BufferedImage im) {
5161
return new ImageSignature(im, false);
@@ -90,8 +100,6 @@ public boolean equals(Object arg) {
90100
// return fromImage(im, ignoreFirstPoint);
91101
// }
92102

93-
94-
95103
public final long getCrc() {
96104
return crc;
97105
}

0 commit comments

Comments
 (0)