-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathutils.js
More file actions
83 lines (65 loc) · 2.45 KB
/
utils.js
File metadata and controls
83 lines (65 loc) · 2.45 KB
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
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const _ = require('lodash');
const png = require('./png');
const DiffArea = require('./diff-area');
const DiffClusters = require('./diff-clusters');
const validators = require('./validators');
function read({source, ...opts}) {
const readFunc = Buffer.isBuffer(source) ? png.fromBuffer : png.fromFile;
return readFunc(source, opts);
}
exports.readPair = async (first, second) => {
const [firstPng, secondPng] = await Promise.all([first, second].map(read));
return {first: firstPng, second: secondPng};
};
const getDiffClusters = (diffClusters, diffArea, {shouldCluster}) => {
return shouldCluster ? diffClusters.clusters : [diffArea.area];
};
exports.getDiffPixelsCoords = (png1, png2, predicate, opts, callback) => {
if (!callback) {
callback = opts;
opts = {};
}
const stopOnFirstFail = opts.hasOwnProperty('stopOnFirstFail') ? opts.stopOnFirstFail : false;
const width = Math.min(png1.width, png2.width);
const height = Math.min(png1.height, png2.height);
const diffArea = new DiffArea();
const diffClusters = new DiffClusters(opts.clustersSize);
const processRow = (y) => {
setImmediate(() => {
for (let x = 0; x < width; x++) {
const color1 = png1.getPixel(x, y);
const color2 = png2.getPixel(x, y);
const result = predicate({
color1, color2,
png1, png2,
x, y,
width, height
});
if (!result) {
const {x: actX, y: actY} = png1.getActualCoord(x, y);
diffArea.update(actX, actY);
if (opts.shouldCluster) {
diffClusters.update(actX, actY);
}
if (stopOnFirstFail) {
return callback({diffArea, diffClusters: getDiffClusters(diffClusters, diffArea, opts)});
}
}
}
y++;
if (y < height) {
processRow(y);
} else {
callback({diffArea, diffClusters: getDiffClusters(diffClusters, diffArea, opts)});
}
});
};
processRow(0);
};
exports.formatImages = (img1, img2) => {
validators.validateImages(img1, img2);
return [img1, img2].map((i) => {
return _.isObject(i) && !Buffer.isBuffer(i) ? i : {source: i, boundingBox: null};
});
};