2024-01-02 18:16:18 +01:00
|
|
|
from splat.segtypes.n64.segment import N64Segment
|
|
|
|
from splat.util import options
|
2023-06-26 12:27:37 +02:00
|
|
|
import png # type: ignore
|
|
|
|
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
def parse_ci4(data, width, height):
|
|
|
|
raster = bytearray()
|
|
|
|
|
|
|
|
for i in range(width * height // 2):
|
|
|
|
raster.append(data[i] >> 4)
|
|
|
|
raster.append(data[i] & 0xF)
|
|
|
|
|
|
|
|
return raster
|
|
|
|
|
|
|
|
|
2023-03-04 04:20:43 +01:00
|
|
|
def get_palette_idx(charset_name, char_id):
|
|
|
|
pal_id = 0
|
|
|
|
if charset_name == "standard":
|
|
|
|
if char_id == 0x98:
|
|
|
|
pal_id = 0x10
|
|
|
|
elif char_id == 0x99:
|
|
|
|
pal_id = 0x11
|
|
|
|
elif char_id == 0x9A:
|
|
|
|
pal_id = 0x15
|
|
|
|
elif char_id == 0x9B:
|
|
|
|
pal_id = 0x15
|
|
|
|
elif char_id == 0x9C:
|
|
|
|
pal_id = 0x15
|
|
|
|
elif char_id == 0x9D:
|
|
|
|
pal_id = 0x13
|
|
|
|
elif char_id == 0x9E:
|
|
|
|
pal_id = 0x13
|
|
|
|
elif char_id == 0x9F:
|
|
|
|
pal_id = 0x13
|
|
|
|
elif char_id == 0xA0:
|
|
|
|
pal_id = 0x13
|
|
|
|
elif char_id == 0xA1:
|
|
|
|
pal_id = 0x12
|
|
|
|
return pal_id
|
|
|
|
|
2023-06-26 12:27:37 +02:00
|
|
|
|
2021-07-16 11:28:37 +02:00
|
|
|
class N64SegPm_charset(N64Segment):
|
|
|
|
def scan(self, rom_bytes):
|
2023-06-26 12:27:37 +02:00
|
|
|
data = rom_bytes[self.rom_start : self.rom_end]
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
# start, type, name, WIDTH, HEIGHT, NUM_RASTERS
|
|
|
|
self.width = self.yaml[3]
|
|
|
|
self.height = self.yaml[4]
|
|
|
|
|
|
|
|
# pm_charset_palettes sibling
|
2023-06-26 12:27:37 +02:00
|
|
|
self.sibling = next(
|
|
|
|
filter(
|
|
|
|
lambda s: s.type == "pm_charset_palettes" and s.name == self.name,
|
|
|
|
self.parent.subsegments,
|
|
|
|
)
|
|
|
|
)
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
self.rasters = []
|
|
|
|
|
2023-06-26 12:27:37 +02:00
|
|
|
step = self.width * self.height // 2
|
|
|
|
# align step to 8
|
|
|
|
step = (step + 7) & ~7
|
|
|
|
|
|
|
|
for i in range(0, self.size, step):
|
2021-07-16 11:28:37 +02:00
|
|
|
raster = parse_ci4(data[i:], self.width, self.height)
|
|
|
|
self.rasters.append(raster)
|
|
|
|
|
|
|
|
def split(self, rom_bytes):
|
2022-09-28 22:52:12 +02:00
|
|
|
fs_dir = options.opts.asset_path / self.dir / self.name
|
2021-07-16 11:28:37 +02:00
|
|
|
fs_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
for i, raster in enumerate(self.rasters):
|
2023-03-04 04:20:43 +01:00
|
|
|
pal_idx = get_palette_idx(self.name, i)
|
|
|
|
palette = self.sibling.palettes[pal_idx]
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
w = png.Writer(self.width, self.height, palette=palette)
|
|
|
|
with open(fs_dir / f"{i:02X}.png", "wb") as f:
|
|
|
|
w.write_array(f, raster)
|
|
|
|
|
|
|
|
def get_linker_entries(self):
|
2024-01-02 18:16:18 +01:00
|
|
|
from splat.segtypes.linker_entry import LinkerEntry
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
# start, type, name, WIDTH, HEIGHT
|
|
|
|
self.width = self.yaml[3]
|
|
|
|
self.height = self.yaml[4]
|
|
|
|
|
2022-09-28 22:52:12 +02:00
|
|
|
fs_dir = options.opts.asset_path / self.dir / self.name
|
2021-07-16 11:28:37 +02:00
|
|
|
|
2023-06-26 12:27:37 +02:00
|
|
|
return [
|
|
|
|
LinkerEntry(
|
|
|
|
self,
|
|
|
|
[fs_dir / f"{i:02X}.png" for i in range(self.yaml[5])],
|
|
|
|
fs_dir.with_suffix(".dat"),
|
|
|
|
".data",
|
2023-11-10 03:48:23 +01:00
|
|
|
".data",
|
2023-06-26 12:27:37 +02:00
|
|
|
),
|
2021-07-16 11:28:37 +02:00
|
|
|
]
|