forked from jseidelin/pixastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.js
executable file
·74 lines (64 loc) · 1.89 KB
/
histogram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Pixastic Lib - Histogram - v0.1.1
* Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.histogram = {
process : function(params) {
var average = !!(params.options.average && params.options.average != "false");
var paint = !!(params.options.paint && params.options.paint != "false");
var color = params.options.color || "rgba(255,255,255,0.5)";
var values = [];
if (typeof params.options.returnValue != "object") {
params.options.returnValue = {values:[]};
}
var returnValue = params.options.returnValue;
if (typeof returnValue.values != "array") {
returnValue.values = [];
}
values = returnValue.values;
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
params.useData = false;
for (var i=0;i<256;i++) {
values[i] = 0;
}
var rect = params.options.rect;
var p = rect.width * rect.height;
var pix = p*4, pix1 = pix + 1, pix2 = pix + 2, pix3 = pix + 3;
var round = Math.round;
if (average) {
while (p--) {
values[ round((data[pix-=4]+data[pix+1]+data[pix+2])/3) ]++;
}
} else {
while (p--) {
values[ round(data[pix-=4]*0.3 + data[pix+1]*0.59 + data[pix+2]*0.11) ]++;
}
}
if (paint) {
var maxValue = 0;
for (var i=0;i<256;i++) {
if (values[i] > maxValue) {
maxValue = values[i];
}
}
var heightScale = params.height / maxValue;
var widthScale = params.width / 256;
var ctx = params.canvas.getContext("2d");
ctx.fillStyle = color;
for (var i=0;i<256;i++) {
ctx.fillRect(
i * widthScale, params.height - heightScale * values[i],
widthScale, values[i] * heightScale
);
}
}
returnValue.values = values;
return true;
}
},
checkSupport : function() {
return Pixastic.Client.hasCanvasImageData();
}
}