Skip to content

Commit 832f6ad

Browse files
author
mouafa
committed
support more diffing methods
1 parent 699d480 commit 832f6ad

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/diff.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const { rgb2yiqDistance } = require('./YIQ')
2-
const { rgbDistance } = require('./RGB')
1+
const methods = require('./methods')
32
const { highlight, blendAlpha } = require('./util')
43
const log = require('./log')
54

@@ -8,11 +7,13 @@ const defaultOptions = {
87
highlightFade: true,
98
highlightColor: [255, 0, 0],
109
transparent: false,
11-
overlapse: false
10+
overlapse: false,
11+
method: 'rgb'
1212
}
1313

1414
function diff(img1Buffer, img2Buffer, option = {}) {
1515
option = Object.assign({}, defaultOptions, option)
16+
const method = methods[option.method] ? methods[option.method] : methods[defaultOptions.method]
1617

1718
const out = {
1819
data: Buffer.from(img1Buffer)
@@ -25,7 +26,7 @@ function diff(img1Buffer, img2Buffer, option = {}) {
2526
for (let i = 0; i < length; i = i + 4) {
2627
pixel1 = [img1Buffer[i], img1Buffer[i + 1], img1Buffer[i + 2]]
2728
pixel2 = [img2Buffer[i], img2Buffer[i + 1], img2Buffer[i + 2]]
28-
const distance = rgbDistance(pixel1, pixel2)
29+
const distance = method(pixel1, pixel2)
2930

3031
if (distance > option.threshold) {
3132
diffPixels++

testdrive/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_solid.png', { h
1313
diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_transparent.png', { transparent: true, highlightFade: false })
1414
diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_overlapse.png', { overlapse: true, transparent: true })
1515

16+
diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_method_rgb.png', { method: 'rgb', highlightFade: false })
17+
diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_method_rgbTuned.png', { method: 'rgbTuned', highlightFade: false })
18+
diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_method_yiq.png', { method: 'yiq', highlightFade: false })
19+
diff('./fixture/img1.png', './fixture/img2.png', './fixture/diff_method_yiqTuned.png', { method: 'yiqTuned', highlightFade: false })
20+
1621
function diff(inputImg1, inputImg2, outputImg, options = {}) {
1722
var img1 = fs
1823
.createReadStream(inputImg1)

0 commit comments

Comments
 (0)