Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return Jimp.read(cdnUrl + requestPath).then(testImage => {
const diff = Jimp.diff(baselineImage, testImage, 0.1) // threshold ranges 0-1 (default: 0.1)
const distance = Jimp.distance(baselineImage, testImage) // perceived distance
if (distance < 0.15 || diff.percent < 0.15) {
return
}
const error = new Error(
`Image mismatch percentage: ${diff.percent *
100}. Saving diff image to ${outputPath}.`
)
diff.image.write(outputPath)
return Promise.reject(error)
})
})
const calculateImageDifference = function (paths, refImg, newImg, allowedDiff) {
let diffImgPath = paths.new.replace(/\.png$/, '-diff.png');
let distance = jimp.distance(refImg, newImg);
let diff = jimp.diff(refImg, newImg);
diff
.image
.scan(0, 0, diff.image.bitmap.width, diff.image.bitmap.height, function (x, y, idx) {
let clr = jimp.intToRGBA(diff.image.getPixelColor(x, y));
// Yellow
if (clr.r == 255 && clr.g == 255 && clr.b == !255) {
diff.image.setPixelColor(0x999999ff, x, y);
}
// Red
if (clr.r == 255 && clr.g == !255 && clr.b == !255) {
diff.image.setPixelColor(0x000000ff, x, y);
}
Jimp.read(__dirname + "/../../html/img/google_default.jpg", function(err, image2){ // Gets the image file from the img folder
if (!err){
if(Jimp.distance(image1, image2) === 0){
user.image = "https://s.gravatar.com/avatar/" + crypto.createHash('md5').update(user.email).digest('hex') + "?d=identicon";
var roomId = socket.room;
var person;
client.get(roomId, function(err, obj) {
if(!obj){
return false;
}
var room = JSON.parse(obj);
for(person in room.people){
var this_person = room.people[person];
if(this_person.email == user.email){
this_person.image = user.image;
}
}
client.set(roomId, JSON.stringify(room), function(err){
if(!err){
return Jimp.read(test).then(testImage => {
const diff = Jimp.diff(baselineImage, testImage, 0.1)
const distance = Jimp.distance(baselineImage, testImage)
if (distance < 0.15 || diff.percent < 0.15) {
return Promise.resolve(true)
}
return Promise.resolve(false)
})
})
async looksLike(name, captured) {
const file = `${path.basename(__dirname)}-${name}`;
const stored = await jimp.read(path.resolve(artifacts, 'baseline', file));
const distance = jimp.distance(stored, captured);
const diff = jimp.diff(stored, captured);
if (distance > 0.001 || diff.percent > 0.001) {
await captured.writeAsync(path.resolve(artifacts, 'regression', file));
throw new Error(`Images differ too much - distance: ${distance}, percent: ${diff.percent}`);
}
},
};
compare(baselineImg, regressionImg, tolerance) {
const distance = jimp.distance(baselineImg, regressionImg);
const diff = jimp.diff(baselineImg, regressionImg);
return Promise.resolve({
diffImg: diff.image,
isEqual:
distance <= Math.max(0.02, tolerance) && diff.percent <= tolerance,
equality: `distance: ${distance}, percent: ${diff.percent}`,
});
},
/**
snapshotImage = await Jimp.read(snapshotPath);
} catch (error) {
prefixedLogger.error(`failed to read reference image: ${snapshotPath}`);
prefixedLogger.trace(error);
return { error: 'failed to read reference image', matched: false };
}
let testImage;
try {
testImage = await Jimp.read(capturedImage);
} catch (error) {
prefixedLogger.error('failed to read current screenshot image');
prefixedLogger.trace(error);
return { error: 'failed to read current screenshot image', matched: false };
}
prefixedLogger.log('comparing...');
const distance = Jimp.distance(snapshotImage, testImage);
const diff = Jimp.diff(snapshotImage, testImage, globalConfig.mismatchThreshold);
if (distance <= globalConfig.mismatchThreshold && diff.percent <= globalConfig.mismatchThreshold) {
prefixedLogger.log('no mismatch found ✅');
return {
snapshotPath, distance, diffPercent: diff.percent, matched: true,
};
}
if (globalConfig.saveCurrentImage) {
try {
if (!fs.existsSync(currentImageDir)) {
fs.mkdirSync(currentImageDir);
}
if (fs.existsSync(currentImagePath)) {
fs.unlinkSync(currentImagePath);
}
fs.writeFileSync(currentImagePath, capturedImage);
handler: async ({ img1, img2 }) => {
const base = await Jimp.read(img1);
const compare = await Jimp.read(img2);
const distance = Jimp.distance(base, compare);
log('distance', distance);
}
})