javascript - Fastest Gaussian Blur Not Working -
i not @ javascript , have been trying ivan kuckir's fastest gaussian blur code work no success. when load page, becomes unresponsive have close window somehow. code use follows. doing wrong?
<html> <head> <script src="path_to_gaussianblur.js"></script> </head> <body> <img id="sourceimage" src="someimage_200px_x_200px.jpg" /> <canvas id="canvas1" width="200" height="200" style="border: 1px solid blue"></canvas> <canvas id="canvas2" width="200" height="200" style="border: 1px solid red"></canvas> <script> document.getelementbyid("sourceimage").onload = function () { var c1 = document.getelementbyid("canvas1"); var c2 = document.getelementbyid("canvas2"); var ctx1 = c1.getcontext("2d"); var ctx2 = c2.getcontext("2d"); var img = document.getelementbyid("sourceimage"); ctx1.drawimage(img, 0, 0); ctx2.drawimage(img, 0, 0); var source = ctx1.getimagedata(0, 0, c1.width, c1.height); var target = ctx2.getimagedata(0, 0, c2.width, c2.height); (var = 0; < source.data.length; += 4) { // red chanel gaussblur_4(source.data[i], target.data[i], c1.width, c1.height, 2); // green channel gaussblur_4(source.data[i + 1], target.data[i + 1], c1.width, c1.height, 2); // blue channel gaussblur_4(source.data[i + 2], target.data[i + 2], c1.width, c1.height, 2); } ctx2.putimagedata(target, 0, 0); }; </script> </body> </html>
as stated in comments need split rgba array separate channels. can in several ways, here one:
split
var idata = ctx.getimagedata(0, 0, w, h), // assumes ctx/w/h defined rgba = idata.data, len = w * h, rsrc = new uint8array(len), // source arrays gsrc = new uint8array(len), bsrc = new uint8array(len), asrc = new uint8array(len), // define target arrays same way above = 0, offset = 0; for(; < len; i++) { rsrc[i] = rgba[offset++]; gsrc[i] = rgba[offset++]; bsrc[i] = rgba[offset++]; asrc[i] = rgba[offset++]; }
now can pass each of arrays blur function using target arrays set in similar manner source arrays, merge result rgba format.
apply
gaussblur_4(rsrc, rtrg, w, h, radius); gaussblur_4(gsrc, gtrg, w, h, radius); gaussblur_4(bsrc, btrg, w, h, radius); gaussblur_4(asrc, atrg, w, h, radius);
additional tip: if you're using images (as in photos) can skip blurring alpha channel there none (or technically, opaque in canvas).
merge
to merge, do:
for(i = 0, offset = 0; < len; i++) { rgba[offset++] = rtrg[i]; rgba[offset++] = gtrg[i]; rgba[offset++] = btrg[i]; rgba[offset++] = atrg[i]; // or increase offset if skipped alpha } ctx.putimagedata(idata, 0, 0);
due license conflict on running example can found in fiddle (and here alternative split/merge using 32-bits approach).
Comments
Post a Comment