-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencode-image.skip.ts
80 lines (66 loc) · 2.19 KB
/
encode-image.skip.ts
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
/**
Miku-Legends-2
Copyright (C) 2024, DashGL Project
By Kion (kion@dashgl.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
import { test, expect } from "bun:test";
import { encodeImage } from "../src/EncodeTexture";
import { readFileSync, writeFileSync } from "fs";
import { PNG } from "pngjs";
type Pixel = {
r: number;
g: number;
b: number;
a: number;
};
const wordToColor = (word: number): Pixel => {
const r = ((word >> 0x00) & 0x1f) << 3;
const g = ((word >> 0x05) & 0x1f) << 3;
const b = ((word >> 0x0a) & 0x1f) << 3;
const a = word > 0 ? 255 : 0;
return { r, g, b, a };
};
test("it should encode and render an image", () => {
const src = readFileSync("miku/body-1.png");
const [pal, img] = encodeImage(src);
const palette: Pixel[] = new Array();
for (let i = 0; i < 16; i++) {
const word = pal.readUInt16LE(i * 2);
palette.push(wordToColor(word));
}
// Read the image data
const imageData: number[] = new Array();
for (let ofs = 0; ofs < img.length; ofs++) {
const byte = img.readUInt8(ofs);
imageData.push(byte & 0xf);
imageData.push(byte >> 4);
}
const width = 256;
const height = 256;
const png = new PNG({ width, height });
let index = 0;
let dst = 0;
for (let y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
const colorIndex = imageData[index++];
const { r, g, b, a } = palette[colorIndex!];
png.data[dst++] = r;
png.data[dst++] = g;
png.data[dst++] = b;
png.data[dst++] = a;
}
}
// Export file
const buffer = PNG.sync.write(png);
writeFileSync("fixtures/0-miku-body.png", buffer);
});