-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrast.js
71 lines (65 loc) · 2.22 KB
/
contrast.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
"use strict";
/*
* 2021/10/09- (c) [email protected]
*/
document.addEventListener("DOMContentLoaded", function(event) {
main();
});
function main() {
// console.debug("main");
const srcCanvas = document.getElementById("srcCanvas");
const dstCanvas = document.getElementById("dstCanvas");
let srcImage = new Image(srcCanvas.width, srcCanvas.height);
const params = {};
dropFunction(document, function(dataURL) {
srcImage = new Image();
srcImage.onload = function() {
drawSrcImageAndContrast(srcImage, srcCanvas, dstCanvas, params);
}
srcImage.src = dataURL;
}, "DataURL");
bindFunction({"maxWidthHeightRange":"maxWidthHeightText",
"amountRange":"amountText"},
function() {
drawSrcImageAndContrast(srcImage, srcCanvas, dstCanvas,
params);
}, params);
}
function drawSrcImageAndContrast(srcImage, srcCanvas, dstCancas, params) {
const maxWidthHeight = params.maxWidthHeightRange;
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
drawContrast(srcCanvas, dstCanvas, params);
}
function contrast(rgba, slope, intercept) {
return [
rgba[0] * slope + intercept * 255,
rgba[1] * slope + intercept * 255,
rgba[2] * slope + intercept * 255,
rgba[3]
];
}
function drawContrast(srcCanvas, dstCanvas, params) {
const amount = params.amountRange;
console.debug("drawContrast", amount);
const srcCtx = srcCanvas.getContext("2d");
const dstCtx = dstCanvas.getContext("2d");
const width = srcCanvas.width, height = srcCanvas.height;
dstCanvas.width = width;
dstCanvas.height = height;
//
const srcImageData = srcCtx.getImageData(0, 0, width, height);
const dstImageData = dstCtx.createImageData(width, height);
//
// https://drafts.fxtf.org/filter-effects/#contrastEquivalent
const slope = amount;
const intercept = - (0.5 * amount) + 0.5;
//
console.log("slope:"+slope, " intercept:"+intercept);
for (let y = 0 ; y < height; y++) {
for (let x = 0 ; x < width; x++) {
const rgba = getRGBA(srcImageData, x, y);
setRGBA(dstImageData, x, y, contrast(rgba, slope, intercept));
}
}
dstCtx.putImageData(dstImageData, 0, 0);
}