50 lines
2.1 KiB
JavaScript
50 lines
2.1 KiB
JavaScript
document.getElementById('convertBtn').onclick = function() {
|
|
const fileInput = document.getElementById('imageInput');
|
|
const width = parseInt(document.getElementById('width').value, 10);
|
|
const height = parseInt(document.getElementById('height').value, 10);
|
|
if (!fileInput.files.length) {
|
|
alert('Please select an image file.');
|
|
return;
|
|
}
|
|
const file = fileInput.files[0];
|
|
const reader = new FileReader();
|
|
reader.onload = function(e) {
|
|
const img = new Image();
|
|
img.onload = function() {
|
|
const canvas = document.getElementById('canvas');
|
|
canvas.width = width;
|
|
canvas.height = height;
|
|
const ctx = canvas.getContext('2d');
|
|
ctx.clearRect(0, 0, width, height);
|
|
ctx.drawImage(img, 0, 0, width, height);
|
|
// Preview
|
|
document.getElementById('preview').innerHTML = '';
|
|
const previewImg = new Image();
|
|
previewImg.src = canvas.toDataURL();
|
|
document.getElementById('preview').appendChild(previewImg);
|
|
// Get pixel data
|
|
const imageData = ctx.getImageData(0, 0, width, height).data;
|
|
const buf = new Uint8Array(width * height * 2);
|
|
for (let i = 0, j = 0; i < imageData.length; i += 4, j += 2) {
|
|
let r = imageData[i];
|
|
let g = imageData[i+1];
|
|
let b = imageData[i+2];
|
|
// Convert to BGR565
|
|
let bgr565 = ((b & 0xF8) << 8) | ((g & 0xFC) << 3) | (r >> 3);
|
|
// Invert
|
|
bgr565 = 0xFFFF - bgr565;
|
|
buf[j] = bgr565 & 0xFF;
|
|
buf[j+1] = (bgr565 >> 8) & 0xFF;
|
|
}
|
|
// Download
|
|
const blob = new Blob([buf], {type: 'application/octet-stream'});
|
|
const a = document.createElement('a');
|
|
a.href = URL.createObjectURL(blob);
|
|
a.download = file.name.replace(/\.[^.]+$/, '') + `_${width}x${height}_bgr565inv.raw`;
|
|
a.click();
|
|
};
|
|
img.src = e.target.result;
|
|
};
|
|
reader.readAsDataURL(file);
|
|
};
|