Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / libClipEdit / pixastic-lib / actions / noise.js
blob97ba8258304c08a0202ea1061814c9a945e753a7
1 /*
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]
5  */
7 Pixastic.Actions.noise = {
9         process : function(params) {
10                 var amount = 0;
11                 var strength = 0;
12                 var mono = false;
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;
30                         var w = rect.width;
31                         var h = rect.height;
32                         var w4 = w*4;
33                         var y = h;
34                         var random = Math.random;
36                         do {
37                                 var offsetY = (y-1)*w4;
38                                 var x = w;
39                                 do {
40                                         var offset = offsetY + (x-1)*4;
41                                         if (random() < amount) {
42                                                 if (mono) {
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;
47                                                 } else {
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);
51                                                 }
53                                                 if (r < 0 ) r = 0;
54                                                 if (g < 0 ) g = 0;
55                                                 if (b < 0 ) b = 0;
56                                                 if (r > 255 ) r = 255;
57                                                 if (g > 255 ) g = 255;
58                                                 if (b > 255 ) b = 255;
60                                                 data[offset] = r;
61                                                 data[offset+1] = g;
62                                                 data[offset+2] = b;
63                                         }
64                                 } while (--x);
65                         } while (--y);
66                         return true;
67                 }
68         },
69         checkSupport : function() {
70                 return Pixastic.Client.hasCanvasImageData();
71         }