-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
165 lines (141 loc) · 5.89 KB
/
index.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!-- Reference: https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop -->
<!-- And thanks to co-pilot... -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display ESC190 *.bin image</title>
<style>
#drop_zone {
border: 5px solid blue;
/* make it the same width as the page */
width: 100%;
height: 200px;
}
/* make the canvas upscale image using nearest neighbour
and at least 100px while keeping aspect ratio */
#canvas {
image-rendering: pixelated;
min-width: 100px;
object-fit: contain;
}
</style>
</head>
<body>
<div
id="drop_zone"
ondrop="dropHandler(event);"
ondragover="dragOverHandler(event);">
<p>Drag an image file (*.jpg, *.png, *.bmp) or binary (*.bin) file <i>here</i>.</p>
</div>
<p id="description"></p>
<p><canvas id="canvas"></canvas></p>
<p><button id="download_bin" onclick="download_canvas_as_bin(event);">Save as image.bin file</button></p>
<p>To save as a PNG file, right click on the image above and select <em>Save Image As...</em></p>
<script>
function dropHandler(ev) {
console.log("File(s) dropped");
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
[...ev.dataTransfer.items].forEach((item, i) => {
// If dropped items aren't files, reject them
if (item.kind === "file") {
const file = item.getAsFile();
console.log(`… file[${i}].name = ${file.name}`);
if (file.name.endsWith(".bin")) {
displayBinFile(file);
} else {
displayImageFile(file);
}
}
});
} else {
// Use DataTransfer interface to access the file(s)
[...ev.dataTransfer.files].forEach((file, i) => {
console.log(`… file[${i}].name = ${file.name}`);
if (file.name.endsWith(".bin")) {
displayBinFile(file);
} else {
displayImageFile(file);
}
});
}
}
function displayBinFile(file) {
const reader = new FileReader();
reader.onload = function(e) {
const buffer = new Uint8Array(e.target.result);
const height = buffer[0] * 256 + buffer[1];
const width = buffer[2] * 256 + buffer[3];
const raster = buffer.slice(4);
// Add additional alpha channel to the raster
const arr = new Uint8ClampedArray(height * width * 4);
for (let i = 0; i < height * width * 3; i += 3) {
arr[Math.floor(i/3)*4 + 0] = raster[i + 0]; // R value
arr[Math.floor(i/3)*4 + 1] = raster[i + 1]; // G value
arr[Math.floor(i/3)*4 + 2] = raster[i + 2]; // B value
arr[Math.floor(i/3)*4 + 3] = 255; // A value
}
document.getElementById("description").innerHTML = `Image size: ${width} x ${height}`;
const canvas = document.getElementById("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
const imageData = new ImageData(arr, width, height);
ctx.putImageData(imageData, 0, 0);
};
reader.readAsArrayBuffer(file);
}
function displayImageFile(file) {
// Display the image file in the canvas with id "canvas"
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const canvas = document.getElementById("canvas");
canvas.width = img.width;
canvas.height = img.height;
document.getElementById("description").innerHTML = `Image size: ${img.width} x ${img.height}`;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
function download_canvas_as_bin(ev) {
// Convert the canvas to a binary file
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const raster = imageData.data;
const height = canvas.height;
const width = canvas.width;
const buffer = new Uint8Array(4 + height * width * 3);
buffer[0] = Math.floor(height / 256);
buffer[1] = height % 256;
buffer[2] = Math.floor(width / 256);
buffer[3] = width % 256;
for (let i = 0; i < height * width * 3; i += 3) {
buffer[4 + i + 0] = raster[Math.floor(i/3)*4 + 0]; // R value
buffer[4 + i + 1] = raster[Math.floor(i/3)*4 + 1]; // G value
buffer[4 + i + 2] = raster[Math.floor(i/3)*4 + 2]; // B value
}
// Create a link to download the buffer
const link = document.createElement("a");
link.download = "image.bin";
link.href = URL.createObjectURL(new Blob([buffer], {type: "application/octet-stream"}));
link.click();
}
function dragOverHandler(ev) {
console.log("File(s) in drop zone");
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
}
</script>
</body>
</html>