UNPKG

@thi.ng/pixel-convolve

Version:

Extensible bitmap image convolution, kernel presets, normal map & image pyramid generation

34 lines (33 loc) 917 B
import { ensureChannel } from "@thi.ng/pixel/checks"; import { FloatBuffer } from "@thi.ng/pixel/float"; import { FLOAT_NORMAL } from "@thi.ng/pixel/format/float-norm"; import { __asVec } from "@thi.ng/pixel/internal/utils"; import { convolveChannel } from "./convolve.js"; const normalMap = (src, opts = {}) => { const { channel = 0, step = 0, scale = 1, z = 1 } = opts; ensureChannel(src.format, channel); const spec = [-1, ...new Array(step).fill(0), 1]; const [sx, sy] = __asVec(scale); const dest = new FloatBuffer(src.width, src.height, FLOAT_NORMAL); dest.setChannel( 0, convolveChannel(src, { kernel: { spec, size: [step + 2, 1] }, scale: sx, channel }) ); dest.setChannel( 1, convolveChannel(src, { kernel: { spec, size: [1, step + 2] }, scale: sy, channel }) ); dest.setChannel(2, z); return dest; }; export { normalMap };