-
Notifications
You must be signed in to change notification settings - Fork 135
/
brightness.js
executable file
·79 lines (69 loc) · 1.81 KB
/
brightness.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
75
76
77
78
/*
* Pixastic Lib - Brightness/Contrast filter - 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.brightness = {
process : function(params) {
var brightness = parseInt(params.options.brightness,10) || 0;
var contrast = parseFloat(params.options.contrast)||0;
var legacy = !!(params.options.legacy && params.options.legacy != "false");
if (legacy) {
brightness = Math.min(150,Math.max(-150,brightness));
} else {
var brightMul = 1 + Math.min(150,Math.max(-150,brightness)) / 150;
}
contrast = Math.max(0,contrast+1);
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var rect = params.options.rect;
var w = rect.width;
var h = rect.height;
var p = w*h;
var pix = p*4, pix1, pix2;
var mul, add;
if (contrast != 1) {
if (legacy) {
mul = contrast;
add = (brightness - 128) * contrast + 128;
} else {
mul = brightMul * contrast;
add = - contrast * 128 + 128;
}
} else { // this if-then is not necessary anymore, is it?
if (legacy) {
mul = 1;
add = brightness;
} else {
mul = brightMul;
add = 0;
}
}
var r, g, b;
while (p--) {
if ((r = data[pix-=4] * mul + add) > 255 )
data[pix] = 255;
else if (r < 0)
data[pix] = 0;
else
data[pix] = r;
if ((g = data[pix1=pix+1] * mul + add) > 255 )
data[pix1] = 255;
else if (g < 0)
data[pix1] = 0;
else
data[pix1] = g;
if ((b = data[pix2=pix+2] * mul + add) > 255 )
data[pix2] = 255;
else if (b < 0)
data[pix2] = 0;
else
data[pix2] = b;
}
return true;
}
},
checkSupport : function() {
return Pixastic.Client.hasCanvasImageData();
}
}