-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmilimg.html
More file actions
346 lines (297 loc) · 9.46 KB
/
milimg.html
File metadata and controls
346 lines (297 loc) · 9.46 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MilImg 图像解码器</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background: #1a1a1a;
color: #e0e0e0;
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 800px;
margin: 0 auto;
}
h1 {
text-align: center;
margin-bottom: 30px;
color: #fff;
font-weight: 400;
}
.upload-section {
background: #252525;
padding: 30px;
border-radius: 8px;
text-align: center;
margin-bottom: 20px;
border: 2px dashed #444;
transition: border-color 0.3s;
}
.upload-section:hover {
border-color: #4a90e2;
}
.upload-btn {
display: inline-block;
background: #4a90e2;
color: white;
padding: 12px 30px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background 0.2s;
}
.upload-btn:hover {
background: #357abd;
}
#load-btn {
display: none;
}
.info-section {
background: #252525;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
display: none;
}
.info-section h3 {
color: #4a90e2;
margin-bottom: 15px;
font-weight: 400;
}
#info {
background: #1a1a1a;
padding: 15px;
border-radius: 4px;
font-family: "Consolas", "Monaco", monospace;
font-size: 13px;
line-height: 1.6;
white-space: pre-wrap;
word-break: break-all;
max-height: 300px;
overflow-y: auto;
}
.image-section {
background: #252525;
padding: 20px;
border-radius: 8px;
text-align: center;
display: none;
}
.image-section h3 {
color: #4a90e2;
margin-bottom: 15px;
font-weight: 400;
}
#decoded-img {
max-width: 100%;
max-height: 500px;
border-radius: 4px;
background: #1a1a1a;
}
.action-btns {
margin-top: 20px;
display: flex;
gap: 10px;
justify-content: center;
flex-wrap: wrap;
}
.btn {
background: #4a90e2;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background 0.2s;
text-decoration: none;
display: inline-block;
}
.btn:hover {
background: #357abd;
}
.btn-secondary {
background: #555;
}
.btn-secondary:hover {
background: #666;
}
.loading {
display: none;
text-align: center;
padding: 40px;
}
.spinner {
width: 40px;
height: 40px;
border: 3px solid rgba(255,255,255,0.3);
border-top: 3px solid #4a90e2;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 0 auto 15px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error {
background: #ff6b6b;
color: white;
padding: 15px;
border-radius: 4px;
margin-top: 20px;
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>🖼️ MilImg 图像解码器</h1>
<div class="upload-section" id="drop-zone">
<label class="upload-btn" for="load-btn">选择 MilImg 文件</label>
<input id="load-btn" type="file" accept="*/*">
<p style="margin-top: 15px; color: #888; font-size: 14px;">或将文件拖放到此处</p>
</div>
<div class="loading" id="loading">
<div class="spinner"></div>
<p>正在解码...</p>
</div>
<div class="error" id="error"></div>
<div class="info-section" id="info-section">
<h3>📋 图像信息</h3>
<div id="info"></div>
</div>
<div class="image-section" id="image-section">
<h3>🎨 解码结果</h3>
<img id="decoded-img" alt="解码后的图像">
<div class="action-btns">
<a id="download-btn" class="btn" download="decoded.png">💾 导出 PNG</a>
<button class="btn btn-secondary" id="reset-btn">🔄 重置</button>
</div>
</div>
</div>
<script>
// 状态管理
let currentDecodedData = null;
let currentCanvas = null;
// DOM 元素
const els = {
loadBtn: document.getElementById('load-btn'),
dropZone: document.getElementById('drop-zone'),
loading: document.getElementById('loading'),
error: document.getElementById('error'),
infoSection: document.getElementById('info-section'),
info: document.getElementById('info'),
imageSection: document.getElementById('image-section'),
decodedImg: document.getElementById('decoded-img'),
downloadBtn: document.getElementById('download-btn'),
resetBtn: document.getElementById('reset-btn')
};
// RGBA 转 PNG Data URL
const rgbaToPng = (decoded) => {
const imgData = new ImageData(
new Uint8ClampedArray(decoded.data),
decoded.width,
decoded.height
);
const canvas = document.createElement("canvas");
canvas.width = decoded.width;
canvas.height = decoded.height;
const ctx = canvas.getContext("2d");
ctx.putImageData(imgData, 0, 0);
return {
dataUrl: canvas.toDataURL("image/png"),
canvas: canvas
};
};
// 显示错误
const showError = (msg) => {
els.error.textContent = msg;
els.error.style.display = 'block';
els.loading.style.display = 'none';
};
// 隐藏错误
const hideError = () => {
els.error.style.display = 'none';
};
// 重置界面
const resetUI = () => {
els.infoSection.style.display = 'none';
els.imageSection.style.display = 'none';
els.loading.style.display = 'none';
hideError();
els.loadBtn.value = '';
currentDecodedData = null;
currentCanvas = null;
};
// 处理文件
const processFile = async (file) => {
if (!file) return;
hideError();
els.loading.style.display = 'block';
els.infoSection.style.display = 'none';
els.imageSection.style.display = 'none';
try {
const h5bind = await import("./src/h5bind.js");
const decoder = new h5bind.default.MilImgDecoder({
buildDirectory: "./build/",
});
await decoder.init();
const arrayBuffer = await file.arrayBuffer();
const imgPtr = await decoder.load(arrayBuffer);
const info = decoder.get_info(imgPtr);
const decoded = await decoder.decode(imgPtr);
// 转换为 PNG
const { dataUrl, canvas } = rgbaToPng(decoded);
currentDecodedData = dataUrl;
currentCanvas = canvas;
// 显示信息
els.info.textContent = JSON.stringify(info, null, 2);
els.infoSection.style.display = 'block';
// 显示图像
els.decodedImg.src = dataUrl;
els.downloadBtn.href = dataUrl;
els.downloadBtn.download = file.name.replace(/\.[^/.]+$/, '') + '.png';
els.imageSection.style.display = 'block';
els.loading.style.display = 'none';
} catch (err) {
console.error(err);
showError('解码失败: ' + err.message);
}
};
// 文件选择事件
els.loadBtn.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
processFile(e.target.files[0]);
}
});
// 拖放支持
els.dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
els.dropZone.style.borderColor = '#4a90e2';
});
els.dropZone.addEventListener('dragleave', (e) => {
e.preventDefault();
els.dropZone.style.borderColor = '#444';
});
els.dropZone.addEventListener('drop', (e) => {
e.preventDefault();
els.dropZone.style.borderColor = '#444';
if (e.dataTransfer.files.length > 0) {
processFile(e.dataTransfer.files[0]);
}
});
// 重置按钮
els.resetBtn.addEventListener('click', resetUI);
</script>
</body>
</html>