-
-
Save xelaz/a16b8fe6eb60ab55f82124163e7d719c to your computer and use it in GitHub Desktop.
Javascript function for sharpening images.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sharpen(ctx, w, h, mix) { | |
var x, sx, sy, r, g, b, a, dstOff, srcOff, wt, cx, cy, scy, scx, | |
weights = [0, -1, 0, -1, 5, -1, 0, -1, 0], | |
katet = Math.round(Math.sqrt(weights.length)), | |
half = (katet * 0.5) | 0, | |
dstData = ctx.createImageData(w, h), | |
dstBuff = dstData.data, | |
srcBuff = ctx.getImageData(0, 0, w, h).data, | |
y = h; | |
while (y--) { | |
x = w; | |
while (x--) { | |
sy = y; | |
sx = x; | |
dstOff = (y * w + x) * 4; | |
r = 0; | |
g = 0; | |
b = 0; | |
a = 0; | |
for (cy = 0; cy < katet; cy++) { | |
for (cx = 0; cx < katet; cx++) { | |
scy = sy + cy - half; | |
scx = sx + cx - half; | |
if (scy >= 0 && scy < h && scx >= 0 && scx < w) { | |
srcOff = (scy * w + scx) * 4; | |
wt = weights[cy * katet + cx]; | |
r += srcBuff[srcOff] * wt; | |
g += srcBuff[srcOff + 1] * wt; | |
b += srcBuff[srcOff + 2] * wt; | |
a += srcBuff[srcOff + 3] * wt; | |
} | |
} | |
} | |
dstBuff[dstOff] = r * mix + srcBuff[dstOff] * (1 - mix); | |
dstBuff[dstOff + 1] = g * mix + srcBuff[dstOff + 1] * (1 - mix); | |
dstBuff[dstOff + 2] = b * mix + srcBuff[dstOff + 2] * (1 - mix); | |
dstBuff[dstOff + 3] = srcBuff[dstOff + 3]; | |
} | |
} | |
ctx.putImageData(dstData, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment