Skip to content

Commit becfb05

Browse files
Move small channel loop into third level instead.
Move properties into variables.
1 parent 11396c0 commit becfb05

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/utils/image.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -705,24 +705,28 @@ export class RawImage {
705705
const output = new Uint8ClampedArray(this.data.length);
706706
const halfSize = Math.floor(kernelSize / 2);
707707

708-
for (let c = 0; c < this.channels; c++) {
709-
for (let y = 0; y < this.height; y++) {
710-
for (let x = 0; x < this.width; x++) {
708+
const height = this.height;
709+
const width = this.width;
710+
const channels = this.channels;
711+
712+
for (let y = 0; y < height; y++) {
713+
for (let x = 0; x < width; x++) {
714+
for (let c = 0; c < channels; c++) {
711715
let sum = 0;
712716

713-
for (let ky = -halfSize; ky < halfSize; ky++) {
714-
for (let kx = -halfSize; kx < halfSize; kx++) {
715-
const pixelX = Math.min(Math.max(x + kx, 0), this.width - 1);
716-
const pixelY = Math.min(Math.max(y + ky, 0), this.height - 1);
717+
for (let ky = -halfSize; ky <= halfSize; ky++) {
718+
for (let kx = -halfSize; kx <= halfSize; kx++) {
719+
const pixelX = Math.min(Math.max(x + kx, 0), width - 1);
720+
const pixelY = Math.min(Math.max(y + ky, 0), height - 1);
717721

718722
const kernelValue = kernel[ky + halfSize][kx + halfSize];
719-
const dataIndex = (pixelY * this.width + pixelX) * this.channels + c;
723+
const dataIndex = (((pixelY * width) + pixelX) * channels) + c;
720724

721725
sum += this.data[dataIndex] * kernelValue;
722726
}
723727
}
724728

725-
const outputIndex = (y * this.width + x) * this.channels + c;
729+
const outputIndex = (((y * width) + x) * channels) + c;
726730
output[outputIndex] = sum;
727731
}
728732
}

0 commit comments

Comments
 (0)