-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (105 loc) · 3.36 KB
/
Copy pathscript.js
File metadata and controls
138 lines (105 loc) · 3.36 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const dropzone = document.getElementById("dropzone");
const fileInput = document.getElementById("fileInput");
const preview = document.getElementById("preview");
const channelMode = document.getElementById("channelMode");
const outBase64 = document.getElementById("outBase64");
const outPixels = document.getElementById("outPixels");
const outPixelsB64 = document.getElementById("outPixelsB64");
const outCSV = document.getElementById("outCSV");
const outCSVb64 = document.getElementById("outCSVb64");
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let lastFile = null;
function isValidImage(file) {
return file && file.type && file.type.startsWith("image/");
}
function uint8ToBase64(uint8) {
let binary = "";
for (let i = 0; i < uint8.length; i++) {
binary += String.fromCharCode(uint8[i]);
}
return btoa(binary);
}
function projectPixels(data, mode) {
const out = [];
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const a = data[i + 3];
if (mode === "rgb") {
out.push(r, g, b);
} else if (mode === "r") {
out.push(r);
} else if (mode === "g") {
out.push(g);
} else if (mode === "b") {
out.push(b);
} else if (mode === "a") {
out.push(a);
} else {
out.push(r, g, b, a);
}
}
return out;
}
function processImage(file) {
if (!isValidImage(file)) {
alert("Invalid file: not an image");
return;
}
lastFile = file;
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
preview.src = e.target.result;
preview.classList.remove("hidden");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, img.width, img.height);
const pixels = imageData.data;
// 1. base64 image (unchanged)
outBase64.value = e.target.result;
// channel mode
const mode = channelMode.value;
// 2. pixel buffer (filtered)
const pixelArray = projectPixels(pixels, mode);
outPixels.value = JSON.stringify(pixelArray);
// 3. base64 pixel buffer
const pixelBytes = new Uint8Array(pixelArray);
outPixelsB64.value = uint8ToBase64(pixelBytes);
// 4. CSV
const csv = pixelArray.join(", ");
outCSV.value = csv;
// 5. base64 CSV
outCSVb64.value = btoa(csv);
};
img.onerror = () => {
alert("Failed to load image");
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
dropzone.addEventListener("click", () => fileInput.click());
dropzone.addEventListener("dragover", (e) => {
e.preventDefault();
dropzone.classList.add("border-blue-400");
});
dropzone.addEventListener("dragleave", () => {
dropzone.classList.remove("border-blue-400");
});
dropzone.addEventListener("drop", (e) => {
e.preventDefault();
dropzone.classList.remove("border-blue-400");
const file = e.dataTransfer.files[0];
processImage(file);
});
fileInput.addEventListener("change", (e) => {
processImage(e.target.files[0]);
});
channelMode.addEventListener("change", () => {
if (lastFile) processImage(lastFile);
});