/
divide.js
46 lines (42 loc) · 1.18 KB
/
divide.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
import { validateArrayOfChannels } from '../../util/channel';
import { checkNumberArray } from '../../util/value';
/**
* @memberof Image
* @instance
* @param {*} value
* @param {object} [options]
* @return {this}
*/
export default function divide(value, options = {}) {
let { channels } = options;
this.checkProcessable('divide', {
bitDepth: [8, 16],
});
channels = validateArrayOfChannels(this, { channels: channels });
value = checkNumberArray(value);
if (!isNaN(value)) {
for (let j = 0; j < channels.length; j++) {
let c = channels[j];
for (let i = 0; i < this.data.length; i += this.channels) {
this.data[i + c] = Math.min(
this.maxValue,
(this.data[i + c] / value) >> 0,
);
}
}
} else {
if (this.data.length !== value.length) {
throw new Error('divide: the: the data size is different');
}
for (let j = 0; j < channels.length; j++) {
let c = channels[j];
for (let i = 0; i < this.data.length; i += this.channels) {
this.data[i + c] = Math.max(
0,
Math.min(this.maxValue, (this.data[i + c] / value[i + c]) >> 0),
);
}
}
}
return this;
}