Skip to content

Commit

Permalink
feat: ability to compare images by regions
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Feb 9, 2019
1 parent daa1d76 commit 87807ab
Show file tree
Hide file tree
Showing 18 changed files with 519 additions and 169 deletions.
52 changes: 28 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const areColorsSame = require('./lib/same-colors');
const AntialiasingComparator = require('./lib/antialiasing-comparator');
const IgnoreCaretComparator = require('./lib/ignore-caret-comparator');
const utils = require('./lib/utils');
const readPair = utils.readPair;
const getDiffPixelsCoords = utils.getDiffPixelsCoords;

const JND = 2.3; // Just noticeable difference if ciede2000 >= JND then colors difference is noticeable by human eye
const {getDiffPixelsCoords} = utils;
const {JND} = require('./lib/constants');

const makeAntialiasingComparator = (comparator, png1, png2, opts) => {
const antialiasingComparator = new AntialiasingComparator(comparator, png1, png2, opts);
Expand Down Expand Up @@ -120,36 +118,40 @@ const getToleranceFromOpts = (opts) => {
const prepareOpts = (opts) => {
opts.tolerance = getToleranceFromOpts(opts);

_.defaults(opts, {
return _.defaults(opts, {
ignoreCaret: true,
ignoreAntialiasing: true,
antialiasingTolerance: 0
});
};

const getMaxDiffBounds = (first, second) => ({
left: 0,
top: 0,
right: Math.max(first.width, second.width) - 1,
bottom: Math.max(first.height, second.height) - 1
});
const getMaxDiffBounds = (first, second) => {
const {x: left, y: top} = first.getActualCoord(0, 0);

return {
left,
top,
right: left + Math.max(first.width, second.width) - 1,
bottom: top + Math.max(first.height, second.height) - 1
};
};

module.exports = exports = function looksSame(reference, image, opts, callback) {
module.exports = exports = function looksSame(image1, image2, opts, callback) {
if (!callback) {
callback = opts;
opts = {};
}

prepareOpts(opts);
opts = prepareOpts(opts);
[image1, image2] = utils.formatImages(image1, image2);

readPair(reference, image, (error, pair) => {
utils.readPair(image1, image2, (error, pair) => {
if (error) {
return callback(error);
}

const first = pair.first;
const second = pair.second;
const refImg = {size: {width: pair.first.width, height: pair.first.height}};
const {first, second} = pair;
const refImg = {size: {width: first.width, height: first.height}};
const metaInfo = {refImg};

if (first.width !== second.width || first.height !== second.height) {
Expand All @@ -167,21 +169,21 @@ module.exports = exports = function looksSame(reference, image, opts, callback)
});
};

exports.getDiffArea = function(reference, image, opts, callback) {
exports.getDiffArea = function(image1, image2, opts, callback) {
if (!callback) {
callback = opts;
opts = {};
}

prepareOpts(opts);
opts = prepareOpts(opts);
[image1, image2] = utils.formatImages(image1, image2);

readPair(reference, image, (error, pair) => {
utils.readPair(image1, image2, (error, pair) => {
if (error) {
return callback(error);
}

const first = pair.first;
const second = pair.second;
const {first, second} = pair;

if (first.width !== second.width || first.height !== second.height) {
return process.nextTick(() => callback(null, getMaxDiffBounds(first, second)));
Expand All @@ -200,9 +202,11 @@ exports.getDiffArea = function(reference, image, opts, callback) {
};

exports.createDiff = function saveDiff(opts, callback) {
prepareOpts(opts);
opts = prepareOpts(opts);

const [image1, image2] = utils.formatImages(opts.reference, opts.current);

readPair(opts.reference, opts.current, (error, {first, second}) => {
utils.readPair(image1, image2, (error, {first, second}) => {
if (error) {
return callback(error);
}
Expand Down
7 changes: 7 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
JND: 2.3, // Just noticeable difference if ciede2000 >= JND then colors difference is noticeable by human eye
REQUIRED_IMAGE_FIELDS: ['source', 'boundingBox'],
REQUIRED_BOUNDING_BOX_FIELDS: ['left', 'top', 'right', 'bottom']
};
2 changes: 1 addition & 1 deletion lib/diff-area.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class DiffArea {
this._updated = false;
}

update(x, y) {
update({x, y}) {
const {left, top, right, bottom} = this._diffArea;

this._diffArea = {
Expand Down
129 changes: 0 additions & 129 deletions lib/png.js

This file was deleted.

35 changes: 35 additions & 0 deletions lib/png/bounded-png.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const PNGImage = require('./png');

module.exports = class BoundedPNGImage extends PNGImage {
constructor(png, boundingBox) {
super(png);

this._boundingBox = boundingBox;
}

getPixel(x, y) {
const {x: actX, y: actY} = this.getActualCoord(x, y);

return super.getPixel(actX, actY);
}

setPixel(x, y, color) {
const {x: actX, y: actY} = this.getActualCoord(x, y);

super.setPixel(actX, actY, color);
}

getActualCoord(x, y) {
return {x: x + this._boundingBox.left, y: y + this._boundingBox.top};
}

get width() {
return this._boundingBox.right - this._boundingBox.left + 1;
}

get height() {
return this._boundingBox.bottom - this._boundingBox.top + 1;
}
};
31 changes: 31 additions & 0 deletions lib/png/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const fs = require('fs');
const {PNG} = require('pngjs');
const OriginalPNG = require('./original-png');
const BoundedPNG = require('./bounded-png');

exports.create = (png, {boundingBox} = {}) => {
return boundingBox
? BoundedPNG.create(png, boundingBox)
: OriginalPNG.create(png);
};

exports.fromFile = (filePath, opts = {}, callback) => {
fs.readFile(filePath, (error, data) => {
error
? callback(error, null)
: exports.fromBuffer(data, opts, callback);
});
};

exports.fromBuffer = (buffer, opts = {}, callback) => {
const png = new PNG();
png.parse(buffer, (error) => {
error
? callback(error, null)
: callback(null, exports.create(png, opts));
});
};

exports.empty = (width, height) => exports.create(new PNG({width, height}));
17 changes: 17 additions & 0 deletions lib/png/original-png.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const PNGImage = require('./png');

module.exports = class OriginalPNGImage extends PNGImage {
getActualCoord(x, y) {
return {x, y};
}

get width() {
return this._png.width;
}

get height() {
return this._png.height;
}
};
Loading

0 comments on commit 87807ab

Please sign in to comment.