-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer.html
68 lines (51 loc) · 1.99 KB
/
answer.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Image Processing</title>
</head>
<body>
<h1>Image Processing!</h1>
<canvas id="canvas" style="border: solid thin black;"></canvas>
<canvas id="canvas_result" style="border: solid thin black;"></canvas>
<br>
<button onclick="process();" style="margin-left: 90px; font-size: 20px;">process!</button>
<script type="text/javascript">
let image = new Image();
var src = "imori.jpg";
image.src = src;
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const canvas_result = document.getElementById("canvas_result");
const ctx_result = canvas_result.getContext("2d");
image.onload = () => {
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
canvas_result.width = image.width;
canvas_result.height = image.height;
}
function process(){
let src = ctx.getImageData(0, 0, image.width, image.height);
let dst = ctx_result.createImageData(image.width, image.height);
// copy and exchange R with B
for (var i = 0; i < src.data.length; i += 4){
x = (i / 4) % image.width;
y = Math.floor((i / 4) / image.width);
if ((x < image.width / 2) && (y < image.height / 2)){
dst.data[i] = src.data[i+2];
dst.data[i+2] = src.data[i];
}
else {
dst.data[i] = src.data[i];
dst.data[i+2] = src.data[i+2];
}
dst.data[i+1] = src.data[i+1];
dst.data[i+3] = src.data[i+3];
}
// exchange R and B
ctx_result.putImageData(dst, 0, 0);
}
</script>
</body>
</html>