This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
162 lines (139 loc) · 4.03 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*!
* This file is part of Rembrandt.js
* Copyright (c) 2016 PhotoEditorSDK.com
* Licensed under MIT license (https://opensource.org/licenses/MIT)
* @license
*/
import fs from 'fs'
import Utils from './lib/utils'
import Promise from './vendor/promise'
import Constants from './constants'
import Image from './lib/image'
import Color from './lib/color'
import ImageComparator from './lib/image-comparator'
// @ifdef BROWSER
if (typeof BROWSER !== 'undefined') {
global.Buffer = () => {}
}
// @endif
class Rembrandt {
constructor (options) {
this._imageA = null
this._imageB = null
this._options = Utils.defaults(options, {
imageA: null,
imageB: null,
thresholdType: Rembrandt.THRESHOLD_PERCENT,
maxThreshold: 0.01,
maxDelta: 0.02,
renderComposition: false,
compositionMaskColor: Color.RED,
maxOffset: 0
})
this._validateOptions()
}
// -------------------------------------------------------------------------- PUBLIC API
/**
* Compares the input images
* @return {Promise}
*/
compare () {
return this._loadImages()
.then(() => {
const comparator = new ImageComparator(this._imageA, this._imageB, this._options)
return comparator.compare()
})
}
// -------------------------------------------------------------------------- STATIC PUBLIC API
/**
* Creates an image
* @param {Number} width
* @param {Number} height
* @return {Rembrandt.Image}
*/
static createImage (width, height) {
return new Image(width, height)
}
// -------------------------------------------------------------------------- PRIVATE API
/**
* Validates the options
* @private
*/
_validateOptions () {
// Image options validation
const checkImageValid = (optionName) => {
const image = this._options[optionName]
if (!(typeof image === 'string' || Buffer.isBuffer(image) || image instanceof Image)) {
throw new Error(`Option \`${optionName}\` must either be a String, Buffer or Rembrandt.Image.`)
}
}
checkImageValid('imageA')
checkImageValid('imageB')
const { thresholdType, threshold } = this._options
// Threshold type validation
const validThresholdTypes = [Rembrandt.THRESHOLD_PERCENT, Rembrandt.THRESHOLD_PIXELS]
if (validThresholdTypes.indexOf(thresholdType) === -1) {
throw new Error(
'`thresholdType` must be either Rembrandt.THRESHOLD_PERCENT or Rembrandt.THRESHOLD_PIXELS'
)
}
// Threshold validation
if (thresholdType === Rembrandt.THRESHOLD_PERCENT && (threshold < 0 || threshold > 1)) {
throw new Error('`threshold` must be between 0 and 1')
}
}
/**
* Loads the images
* @private
*/
_loadImages () {
return this._loadImage(this._options.imageA)
.then((imageA) => {
this._imageA = imageA
return this._loadImage(this._options.imageB)
})
.then((imageB) => {
this._imageB = imageB
})
}
/**
* Loads the given image
* @param {String|Buffer} image
* @return {Buffer}
* @private
*/
_loadImage (image) {
return new Promise((resolve, reject) => {
if (image instanceof Image) {
return resolve(image)
}
if (image instanceof Buffer) {
return resolve(Image.fromBuffer(image))
}
// @ifndef BROWSER
fs.readFile(image, (err, buf) => {
if (err) return reject(err)
resolve(Image.fromBuffer(image))
})
// @endif
// @ifdef BROWSER
if (typeof BROWSER !== 'undefined') {
const browserImage = Utils.createImage()
browserImage.addEventListener('load', () => {
resolve(Image.fromImage(browserImage))
})
browserImage.crossOrigin = 'Anonymous'
browserImage.src = image
}
// @endif
})
}
}
Rembrandt.Image = Image
Rembrandt.Color = Color
Rembrandt.version = require('./package.json').version
// Copy constants to Rembrandt object
for (let key in Constants) {
Rembrandt[key] = Constants[key]
}
module.exports = Rembrandt