2 * Pixastic Lib - Noise filter - v0.1.0
3 * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
4 * MIT License [http://www.opensource.org/licenses/mit-license.php]
7 Pixastic.Actions.noise = {
9 process : function(params) {
14 if (typeof params.options.amount != "undefined")
15 amount = parseFloat(params.options.amount)||0;
16 if (typeof params.options.strength != "undefined")
17 strength = parseFloat(params.options.strength)||0;
18 if (typeof params.options.mono != "undefined")
19 mono = !!(params.options.mono);
21 amount = Math.max(0,Math.min(1,amount));
22 strength = Math.max(0,Math.min(1,strength));
24 var noise = 128 * strength;
25 var noise2 = noise / 2;
27 if (Pixastic.Client.hasCanvasImageData()) {
28 var data = Pixastic.prepareData(params);
29 var rect = params.options.rect;
34 var random = Math.random;
37 var offsetY = (y-1)*w4;
40 var offset = offsetY + (x-1)*4;
41 if (random() < amount) {
43 var pixelNoise = - noise2 + random() * noise;
44 var r = data[offset] + pixelNoise;
45 var g = data[offset+1] + pixelNoise;
46 var b = data[offset+2] + pixelNoise;
48 var r = data[offset] - noise2 + (random() * noise);
49 var g = data[offset+1] - noise2 + (random() * noise);
50 var b = data[offset+2] - noise2 + (random() * noise);
56 if (r > 255 ) r = 255;
57 if (g > 255 ) g = 255;
58 if (b > 255 ) b = 255;
69 checkSupport : function() {
70 return Pixastic.Client.hasCanvasImageData();