Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / libClipEdit / pixastic-lib / actions / histogram.js
blob4b88404c4198542de0d99d4900074981995656ed
1 /*
2  * Pixastic Lib - Histogram - 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.histogram = {
8         process : function(params) {
10                 var average = !!(params.options.average);
11                 var paint = !!(params.options.paint);
12                 var color = params.options.color || "rgba(255,255,255,0.5)";
13                 var values = [];
14                 if (typeof params.options.returnValue != "object") {
15                         params.options.returnValue = {values:[]};
16                 }
17                 var returnValue = params.options.returnValue;
18                 if (typeof returnValue.values != "array") {
19                         returnValue.values = [];
20                 }
21                 values = returnValue.values;
23                 if (Pixastic.Client.hasCanvasImageData()) {
24                         var data = Pixastic.prepareData(params);
25                         params.useData = false;
27                         for (var i=0;i<256;i++) {
28                                 values[i] = 0;
29                         }
31                         var rect = params.options.rect;
32                         var w = rect.width;
33                         var h = rect.height;
34                         var w4 = w*4;
35                         var y = h;
36                         do {
37                                 var offsetY = (y-1)*w4;
38                                 var x = w;
39                                 do {
40                                         var offset = offsetY + (x*4-4);
41                                         var brightness = average ? 
42                                                 Math.round((data[offset]+data[offset+1]+data[offset+2])/3)
43                                                 : Math.round(data[offset]*0.3 + data[offset+1]*0.59 + data[offset+2]*0.11);
44                                         values[brightness]++;
46                                 } while (--x);
47                         } while (--y);
49                         if (paint) {
50                                 var maxValue = 0;
51                                 for (var i=0;i<256;i++) {
52                                         if (values[i] > maxValue) {
53                                                 maxValue = values[i];
54                                         }
55                                 }
56                                 var heightScale = params.height / maxValue;
57                                 var widthScale = params.width / 256;
58                                 var ctx = params.canvas.getContext("2d");
59                                 ctx.fillStyle = color;
60                                 for (var i=0;i<256;i++) {
61                                         ctx.fillRect(
62                                                 i * widthScale, params.height - heightScale * values[i],
63                                                 widthScale, values[i] * heightScale
64                                         );
65                                 }
66                         }
68                         returnValue.values = values;
70                         return true;
71                 }
72         },
73         checkSupport : function() {
74                 return Pixastic.Client.hasCanvasImageData();
75         }