This commit is contained in:
Devine Lu Linvega 2019-07-22 07:40:34 +09:00
parent e5542ed8db
commit 62747aaf23

View File

@ -322,16 +322,16 @@ function Library (ronin) {
this.convolve = (rect, kernel) => {
const sigma = kernel.flat().reduce((a, x) => (a + x))
const kw = kernel[0].length, kh = kernel.length
const kw = kernel[0].length; const kh = kernel.length
const img = ronin.surface.context.getImageData(rect.x, rect.y, rect.w, rect.h)
const out = new Uint8ClampedArray(rect.w * 4 * rect.h)
for (let i = 0, outer = img.data.length; i < outer; i++) { // bytes
const ix = Math.floor(i/4) % rect.w, iy = Math.floor((i/4) / rect.w)
const ix = Math.floor(i / 4) % rect.w; const iy = Math.floor((i / 4) / rect.w)
let acc = 0.0
for (let k = 0, inner = kw * kh; k < inner; k++) { // kernel
const kx = (k%kw), ky = (Math.floor(k/kw))
const x = Math.ceil(ix + kx - kw/2), y = Math.ceil(iy + ky - kh/2)
if( x < 0 || x >= rect.w || y < 0 || y >= rect.h ) continue; // edge case
const kx = (k % kw); const ky = (Math.floor(k / kw))
const x = Math.ceil(ix + kx - kw / 2); const y = Math.ceil(iy + ky - kh / 2)
if (x < 0 || x >= rect.w || y < 0 || y >= rect.h) continue // edge case
acc += img.data[x * 4 + y * rect.w * 4 + i % 4] * kernel[kx][ky] / sigma
}
out[i] = acc