-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
72 lines (66 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
var gm = require('gm').subClass({
imageMagick: true
});
var PNG = require('png-js');
var toArray = require('stream-to-array');
var DEFAULT_HASH_SIZE = 8;
var PIXEL_LENGTH = 4;
function px(pixels, width, x, y) {
return pixels[width * PIXEL_LENGTH * y + x * PIXEL_LENGTH];
}
function binaryToHex(s) {
var output = '';
for (var i = 0; i < s.length; i += 4) {
var bytes = s.substr(i, 4);
var decimal = parseInt(bytes, 2);
var hex = decimal.toString(16);
output += hex.toUpperCase();
}
return output;
}
module.exports = function(path, callback, hashSize) {
var height = hashSize || DEFAULT_HASH_SIZE;
var width = height + 1;
// Covert to small gray image
gm(path)
.colorspace('GRAY')
.resize(width, height, '!')
.stream('png', function(err, stream) {
if (err) {
if (callback) {
callback(err);
}
} else {
// Get pixel data
toArray(stream, function(toArrayErr, arr) {
if (toArrayErr) {
if (callback) {
callback(toArrayErr);
}
} else {
try {
var png = new PNG(Buffer.concat(arr));
} catch (pngErr) {
return callback && callback(pngErr);
}
png.decode(function(pixels) {
// Compare adjacent pixels.
var difference = '';
for (var row = 0; row < height; row++) {
for (var col = 0; col < height; col++) { // height is not a mistake here...
var left = px(pixels, width, col, row);
var right = px(pixels, width, col + 1, row);
difference += left < right ? 1 : 0;
}
}
// Convert difference to hex string
if (callback) {
callback(false, binaryToHex(difference));
}
});
}
});
}
});
};