-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyosyonke.skip
362 lines (314 loc) · 9.34 KB
/
yosyonke.skip
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
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";
import { encodeTexel } from "../src/EncodeTexture";
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,
base: string,
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;
const polly: number[] = [];
for (let i = 0; i < paletteCount; i++) {
for (let k = 0; k < colorCount; k++) {
polly.push(target.readUInt16LE(ofs));
ofs += 2;
}
}
// 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 } = wordToColor(polly[colorIndex!]);
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/${base}_${pos.toString(16)}.png`, buffer);
};
const renderImg = (
src: Buffer,
base: string,
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 } = tim;
// Read Bitfield
let ofs = 0x30;
const { colorCount, paletteCount } = tim;
const polly: Pixel[][] = new Array();
for (let i = 0; i < paletteCount; i++) {
polly[i] = new Array();
for (let k = 0; k < colorCount; k++) {
const word = src.readUInt16LE(ofs);
ofs += 2;
polly[i].push(wordToColor(word));
}
}
const diff = ofs - 0x30;
ofs = 0x800;
// Read the image data
const imageData: number[] = new Array();
for (let i = 0; i < fullSize - diff; i++) {
const byte = src.readUInt8(ofs);
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);
const name = `out/${base}_${pos.toString(16)}.png`;
console.log(name);
writeFileSync(name, buffer);
};
// test("it should search for textures in the flutter", () => {
// const src = readFileSync("bin/flutter-ST1ET.BIN");
// const pals: Pixel[][] = [
// [
// { r: 0, g: 0, b: 0, a: 0 },
// { r: 0, g: 0, b: 0, a: 255 },
// { r: 16, g: 16, b: 16, a: 255 },
// { r: 32, g: 32, b: 32, a: 255 },
// { r: 48, g: 48, b: 48, a: 255 },
// { r: 64, g: 64, b: 64, a: 255 },
// { r: 72, g: 72, b: 72, a: 255 },
// { r: 90, g: 90, b: 90, a: 255 },
// { r: 110, g: 110, b: 110, a: 255 },
// { r: 120, g: 120, b: 120, a: 255 },
// { r: 130, g: 130, b: 130, a: 255 },
// { r: 140, g: 140, b: 140, a: 255 },
// { r: 150, g: 150, b: 150, a: 255 },
// { r: 160, g: 160, b: 160, a: 255 },
// { r: 190, g: 190, b: 190, a: 255 },
// { r: 210, g: 210, b: 210, a: 255 },
// { r: 220, g: 220, b: 220, a: 255 },
// { r: 255, g: 255, b: 255, a: 255 },
// ],
// ];
// 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 && tim.type !== 3) {
// continue;
// }
// if (tim.width == 0 || tim.height == 0) {
// continue;
// }
// const img = src.subarray(i);
// renderImg(img, "scene", i, pals[0]);
// }
// });
test("it should search for room203 palette", () => {
const src = readFileSync("bin/flutter-ST1ET.BIN");
const img = src.subarray(0x02a800);
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;
}
if (tim.paletteX === 0 && tim.paletteY === 0) {
continue;
}
const pal: Pixel[] = [];
for (let k = 0; k < 16; k++) {
const word = src.readUInt16LE(i + 0x30 + k * 2);
pal.push(wordToColor(word));
}
renderImage(img, "poster", i, pal);
}
});