forked from Aditya-ds-1806/DIP-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (87 loc) · 3.64 KB
/
Copy pathindex.js
File metadata and controls
98 lines (87 loc) · 3.64 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(function () {
const baseImg = document.querySelector(".base-image");
const greyscaleImg = document.querySelector(".greyscale-image");
const processedImg = document.querySelector(".processed-image");
const baseImgCtx = baseImg.getContext('2d');
const greyscaleImgCtx = greyscaleImg.getContext('2d');
const processedImgCtx = processedImg.getContext('2d');
const min = document.querySelector("#min");
const max = document.querySelector("#max");
var imgData;
loadImageFromURL();
document.querySelector("#customImage").addEventListener("change", function (e) {
const reader = new FileReader();
const file = this.files[0];
const img = new Image();
reader.readAsDataURL(file);
reader.onload = function () {
img.src = reader.result;
img.onload = () => init(img);
}
document.querySelector("#URLImage").value = "";
});
document.querySelector("#URLImage").addEventListener("input", function (e) {
loadImageFromURL(this.value);
document.querySelector("#customImage").value = "";
});
min.addEventListener("input", function () {
processedImgCtx.putImageData(reduceColorLevels(greyScale(imgData), Number(min.value), Number(max.value)), 0, 0);
});
max.addEventListener("input", function () {
processedImgCtx.putImageData(reduceColorLevels(greyScale(imgData), Number(min.value), Number(max.value)), 0, 0);
});
function loadImageFromURL(url = "https://picsum.photos/800/450") {
const image = new Image();
image.src = url;
image.crossOrigin = "Anonymous";
image.onload = () => init(image);
}
function init(image) {
baseImg.width = image.naturalWidth;
processedImg.width = image.naturalWidth;
baseImg.height = image.naturalHeight;
processedImg.height = image.naturalHeight;
greyscaleImg.width = image.naturalWidth;
greyscaleImg.height = image.naturalHeight;
baseImgCtx.drawImage(image, 0, 0);
imgData = baseImgCtx.getImageData(0, 0, image.naturalWidth, image.naturalHeight);
greyscaleImgCtx.putImageData(greyScale(imgData), 0, 0);
processedImgCtx.putImageData(reduceColorLevels(greyScale(imgData), Number(min.value), Number(max.value)), 0, 0);
}
function reduceColorLevels(imgData, start, end) {
const min = minColor(imgData);
const max = maxColor(imgData);
var reducedImg = new ImageData(
new Uint8ClampedArray(imgData.data.reduce((acc, color, i) => {
if ((i + 1) % 4 === 0) acc.push(color);
else acc.push(Math.floor((end - start) * (color - min) / (max - min) + start))
return acc;
}, [])),
imgData.width, imgData.height);
return reducedImg;
}
function greyScale(imgData) {
for (let i = 0; i <= imgData.data.length - 4; i = i + 4) {
const r = imgData.data[i];
const g = imgData.data[i + 1];
const b = imgData.data[i + 2];
const color = 0.3 * r + 0.59 * g + 0.11 * b;
imgData.data[i + 0] = color;
imgData.data[i + 1] = color;
imgData.data[i + 2] = color;
}
return imgData;
}
function minColor(imgData) {
return imgData.data.reduce((acc, next, i) => {
if ((i + 1) % 4 === 0) return acc;
else return acc < next ? acc : next;
});
}
function maxColor(imgData) {
return imgData.data.reduce((acc, next, i) => {
if ((i + 1) % 4 === 0) return acc;
else return acc > next ? acc : next;
});
}
})()