Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / libClipEdit / pixastic-lib / actions / unsharpmask.js
blob6c562e25fe531b476a0309ec9e9dd6eb093635dc
1 /*
2  * Pixastic Lib - USM - 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  */
8 Pixastic.Actions.unsharpmask = {
9         process : function(params) {
11                 var amount = (parseFloat(params.options.amount)||0);
12                 var blurAmount = parseFloat(params.options.radius)||0;
13                 var threshold = parseFloat(params.options.threshold)||0;
15                 amount = Math.min(500,Math.max(0,amount)) / 2;
16                 blurAmount = Math.min(5,Math.max(0,blurAmount)) / 10;
17                 threshold = Math.min(255,Math.max(0,threshold));
19                 threshold--;
20                 var thresholdNeg = -threshold;
22                 amount *= 0.016;
23                 amount++;
25                 if (Pixastic.Client.hasCanvasImageData()) {
26                         var rect = params.options.rect;
28                         var blurCanvas = document.createElement("canvas");
29                         blurCanvas.width = params.width;
30                         blurCanvas.height = params.height;
31                         var blurCtx = blurCanvas.getContext("2d");
32                         blurCtx.drawImage(params.canvas,0,0);
34                         var scale = 2;
35                         var smallWidth = Math.round(params.width / scale);
36                         var smallHeight = Math.round(params.height / scale);
38                         var copy = document.createElement("canvas");
39                         copy.width = smallWidth;
40                         copy.height = smallHeight;
42                         var steps = Math.round(blurAmount * 20);
44                         var copyCtx = copy.getContext("2d");
45                         for (var i=0;i<steps;i++) {
46                                 var scaledWidth = Math.max(1,Math.round(smallWidth - i));
47                                 var scaledHeight = Math.max(1,Math.round(smallHeight - i));
49                                 copyCtx.clearRect(0,0,smallWidth,smallHeight);
51                                 copyCtx.drawImage(
52                                         blurCanvas,
53                                         0,0,params.width,params.height,
54                                         0,0,scaledWidth,scaledHeight
55                                 );
56         
57                                 blurCtx.clearRect(0,0,params.width,params.height);
58         
59                                 blurCtx.drawImage(
60                                         copy,
61                                         0,0,scaledWidth,scaledHeight,
62                                         0,0,params.width,params.height
63                                 );
64                         }
66                         var data = Pixastic.prepareData(params);
67                         var blurData = Pixastic.prepareData({canvas:blurCanvas,options:params.options});
68                         var w = rect.width;
69                         var h = rect.height;
70                         var w4 = w*4;
71                         var y = h;
72                         do {
73                                 var offsetY = (y-1)*w4;
74                                 var x = w;
75                                 do {
76                                         var offset = offsetY + (x*4-4);
78                                         var difR = data[offset] - blurData[offset];
79                                         if (difR > threshold || difR < thresholdNeg) {
80                                                 var blurR = blurData[offset];
81                                                 blurR = amount * difR + blurR;
82                                                 data[offset] = blurR > 255 ? 255 : (blurR < 0 ? 0 : blurR);
83                                         }
85                                         var difG = data[offset+1] - blurData[offset+1];
86                                         if (difG > threshold || difG < thresholdNeg) {
87                                                 var blurG = blurData[offset+1];
88                                                 blurG = amount * difG + blurG;
89                                                 data[offset+1] = blurG > 255 ? 255 : (blurG < 0 ? 0 : blurG);
90                                         }
92                                         var difB = data[offset+2] - blurData[offset+2];
93                                         if (difB > threshold || difB < thresholdNeg) {
94                                                 var blurB = blurData[offset+2];
95                                                 blurB = amount * difB + blurB;
96                                                 data[offset+2] = blurB > 255 ? 255 : (blurB < 0 ? 0 : blurB);
97                                         }
99                                 } while (--x);
100                         } while (--y);
102                         return true;
103                 }
104         },
105         checkSupport : function() {
106                 return Pixastic.Client.hasCanvasImageData();
107         }