/
setMatrix.js
42 lines (37 loc) · 1.02 KB
/
setMatrix.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
import { Matrix } from 'ml-matrix';
/**
* We set the data of the image from a matrix. The size of the matrix and the data have to be the same.
* @memberof Image
* @instance
* @param {Matrix} matrix
* @param {object} [options]
* @param {number} [options.channel]
*
* @return {this}
*/
export default function setMatrix(matrix, options = {}) {
matrix = new Matrix(matrix);
let { channel } = options;
this.checkProcessable('getMatrix', {
bitDepth: [8, 16],
});
if (channel === undefined) {
if (this.components > 1) {
throw new RangeError(
'You need to define the channel for an image that contains more than one channel',
);
}
channel = 0;
}
if (this.width !== matrix.columns || this.height !== matrix.rows) {
throw new RangeError(
'The size of the matrix must be equal to the size of the image',
);
}
for (let x = 0; x < this.height; x++) {
for (let y = 0; y < this.width; y++) {
this.setValueXY(y, x, channel, matrix.get(x, y));
}
}
return this;
}