-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogo.skip.ts
203 lines (174 loc) · 5.26 KB
/
logo.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
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
/**
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 { 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 };
};
const renderImage = (src: Buffer, pos: number, palette: Pixel[]) => {
const tim = {
type: src.readUInt32LE(0x00),
fullSize: src.readUInt32LE(0x04),
paletteX: src.readUInt16LE(0x0c),
paletteY: src.readUInt16LE(0x0e),
colorCount: src.readUInt16LE(0x10),
paletteCount: src.readUInt16LE(0x12),
imageX: src.readUInt16LE(0x14),
imageY: src.readUInt16LE(0x16),
width: src.readUInt16LE(0x18),
height: src.readUInt16LE(0x1a),
bitfieldSize: src.readUInt16LE(0x24),
payloadSize: src.readUInt16LE(0x26),
};
switch (tim.colorCount) {
case 16:
tim.width *= 4;
break;
case 256:
tim.width *= 2;
break;
default:
tim.paletteCount *= tim.colorCount / 16;
tim.colorCount = 16;
tim.width *= 4;
break;
}
const { fullSize, bitfieldSize } = tim;
const bitfield: number[] = new Array();
const target = Buffer.alloc(fullSize);
// Read Bitfield
const bitfieldBuffer = src.subarray(0x30, 0x30 + bitfieldSize);
let ofs = 0x30;
for (let i = 0; i < bitfieldSize; i += 4) {
const dword = src.readUInt32LE(ofs + i);
for (let k = 31; k > -1; k--) {
bitfield.push(dword & (1 << k) ? 1 : 0);
}
}
ofs += bitfieldSize;
const payloadStart = 0;
// Decompress
let outOfs = 0;
let windowOfs = 0;
let cmdCount = 0;
let bytes = 0;
for (let i = 0; i < bitfield.length; i++) {
const bit = bitfield[i];
if (outOfs === fullSize) {
const payload = src.subarray(0x30 + bitfieldSize, ofs);
break;
}
const word = src.readUInt16LE(ofs);
ofs += 2;
switch (bit) {
case 0:
target.writeUInt16LE(word, outOfs);
outOfs += 2;
break;
case 1:
if (word === 0xffff) {
windowOfs += 0x2000;
cmdCount = 0;
bytes = 0;
} else {
cmdCount++;
const copyFrom = windowOfs + ((word >> 3) & 0x1fff);
const copyLen = ((word & 0x07) + 2) * 2;
bytes += copyLen;
for (let i = 0; i < copyLen; i++) {
target[outOfs++] = target[copyFrom + i];
}
}
break;
}
}
ofs = 0;
const { colorCount, paletteCount } = tim;
// Read the image data
const imageData: number[] = new Array();
for (ofs; ofs < target.length; ofs++) {
const byte = target.readUInt8(ofs);
if (colorCount === 256) {
imageData.push(byte);
} else {
imageData.push(byte & 0xf);
imageData.push(byte >> 4);
}
}
const { width, height } = tim;
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(`out/miku_${pos.toString(16)}.png`, buffer);
};
test("it should search for textures in the logo", () => {
const src = readFileSync("bin/GAME.BIN");
const pals: Pixel[][] = [];
for (let i = 0; i < src.length; i += 0x800) {
const tim = {
type: src.readUInt32LE(i + 0x00),
fullSize: src.readUInt32LE(i + 0x04),
paletteX: src.readUInt16LE(i + 0x0c),
paletteY: src.readUInt16LE(i + 0x0e),
colorCount: src.readUInt16LE(i + 0x10),
paletteCount: src.readUInt16LE(i + 0x12),
imageX: src.readUInt16LE(i + 0x14),
imageY: src.readUInt16LE(i + 0x16),
width: src.readUInt16LE(i + 0x18),
height: src.readUInt16LE(i + 0x1a),
bitfieldSize: src.readUInt16LE(i + 0x24),
payloadSize: src.readUInt16LE(i + 0x26),
};
if (tim.type !== 2) {
continue;
}
const palette: Pixel[] = [];
for (let k = 0; k < 16; k++) {
const word = src.readUInt16LE(i + 0x30 + k * 2);
palette.push(wordToColor(word));
}
console.log(pals.length.toString(16));
console.log("Offset: 0x%s", i.toString(16));
pals.push(palette);
}
const img = src.subarray(0x041800);
pals.forEach((palette, index) => {
renderImage(img, index, palette);
});
});