Merge pull request #64 from eelfroth/convolution

convolution
This commit is contained in:
Лu Лinveгa 2019-07-22 10:39:12 +12:00 committed by GitHub
commit e5542ed8db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -318,6 +318,42 @@ function Library (ronin) {
return Object.values(item) return Object.values(item)
} }
// Convolve
this.convolve = (rect, kernel) => {
const sigma = kernel.flat().reduce((a, x) => (a + x))
const kw = kernel[0].length, 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)
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
acc += img.data[x*4 + y*rect.w*4 + i%4] * kernel[kx][ky] / sigma
}
out[i] = acc
if (i%4 == 3) out[i] = 255
}
img.data.set(out, 0)
ronin.surface.context.putImageData(img, rect.x, rect.y)
return rect
}
this.blur = [[1, 2, 1],
[2, 4, 2],
[1, 2, 2]]
this.sharpen = [[ 0, -1, 0],
[-1, 5, -1],
[ 0, -1, 0]]
this.edge = [[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]]
// File System // File System
this.dir = (path = this.dirpath()) => { // Returns the content of a directory. this.dir = (path = this.dirpath()) => { // Returns the content of a directory.