image-in-browser
Version:
Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)
76 lines • 2.71 kB
JavaScript
import { Channel } from '../color/channel.js';
import { ArrayUtils } from '../common/array-utils.js';
import { MathUtils } from '../common/math-utils.js';
export class SeparableKernel {
get length() {
return this._coefficients.length;
}
constructor(size) {
this._size = size;
this._coefficients = ArrayUtils.fill(2 * size + 1, 0);
}
reflect(max, x) {
if (x < 0) {
return -x;
}
if (x >= max) {
return max - (x - max) - 1;
}
return x;
}
applyCoefficientsLine(src, dst, y, width, horizontal, maskChannel, mask) {
for (let x = 0; x < width; x++) {
let r = 0;
let g = 0;
let b = 0;
let a = 0;
for (let j = -this._size, j2 = 0; j <= this._size; ++j, ++j2) {
const c = this._coefficients[j2];
const gr = this.reflect(width, x + j);
const sc = horizontal ? src.getPixel(gr, y) : src.getPixel(y, gr);
r += c * sc.r;
g += c * sc.g;
b += c * sc.b;
a += c * sc.a;
}
const p = horizontal ? dst.getPixel(x, y) : dst.getPixel(y, x);
const msk = mask === null || mask === void 0 ? void 0 : mask.getPixel(p.x, p.y).getChannelNormalized(maskChannel);
if (msk === undefined) {
p.setRgba(r, g, b, a);
}
else {
p.r = MathUtils.mix(p.r, r, msk);
p.g = MathUtils.mix(p.g, g, msk);
p.b = MathUtils.mix(p.b, b, msk);
p.a = MathUtils.mix(p.a, a, msk);
}
}
}
getCoefficient(index) {
return this._coefficients[index];
}
setCoefficient(index, c) {
this._coefficients[index] = c;
}
apply(opt) {
var _a, _b;
const horizontal = (_a = opt.horizontal) !== null && _a !== void 0 ? _a : true;
const maskChannel = (_b = opt.maskChannel) !== null && _b !== void 0 ? _b : Channel.luminance;
if (horizontal) {
for (let y = 0; y < opt.src.height; ++y) {
this.applyCoefficientsLine(opt.src, opt.dst, y, opt.src.width, horizontal, maskChannel, opt.mask);
}
}
else {
for (let x = 0; x < opt.src.width; ++x) {
this.applyCoefficientsLine(opt.src, opt.dst, x, opt.src.height, horizontal, maskChannel, opt.mask);
}
}
}
scaleCoefficients(s) {
for (let i = 0; i < this._coefficients.length; ++i) {
this._coefficients[i] *= s;
}
}
}
//# sourceMappingURL=separable-kernel.js.map