Files
WoW-Hybrid-gateway/tests/character_atlas_blocks_test.py

71 lines
2.8 KiB
Python

import io
from pathlib import Path
import struct
import sys
import unittest
import numpy as np
from PIL import Image
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
from character_atlas_blocks import BC1, BC3, BGRA, encode_character_atlas
def atlas(fmt, channels, width=16, height=16):
parts = []
offsets = [0] * 16
lengths = [0] * 16
for i in range(max(width, height).bit_length()):
w, h = max(1, width >> i), max(1, height >> i)
offsets[i] = 1172 + sum(lengths)
if fmt == BGRA:
data = bytes(channels[j] for j in (2, 1, 0, 3)) * w * h
else:
data = bytes(((w + 3) // 4) * ((h + 3) // 4) * (8 if fmt == BC1 else 16))
parts.append(data)
lengths[i] = len(data)
return struct.pack('<4sI4sII32I', b'BLP2', 1, fmt, width, height,
*offsets, *lengths) + bytes(1024) + b''.join(parts)
class AtlasBlocksTest(unittest.TestCase):
def test_decode_native_format_and_alpha(self):
rgba = (210, 120, 45, 153)
for fmt in (BC1, BC3):
with self.subTest(fmt=fmt):
template = atlas(fmt, rgba)
result = encode_character_atlas(template, atlas(BGRA, rgba))
self.assertEqual(result[:1172], template[:1172])
with Image.open(io.BytesIO(result)) as image:
actual = image.convert('RGBA').getpixel((5, 5))
self.assertLessEqual(max(abs(a - b) for a, b in zip(actual[:3], rgba)), 8)
self.assertEqual(actual[3], 153 if fmt == BC3 else 255)
def test_rectangular_mips_match_full_coverage_in_both_orientations(self):
rgba = (185, 103, 62, 255)
for w, h in ((32, 8), (8, 32)):
base = atlas(BC1, rgba, w, h)
source = atlas(BGRA, rgba, w, h)
mask = np.ones((h, w), dtype=bool)
result = encode_character_atlas(base, source)
self.assertEqual(result, encode_character_atlas(base, source, coverage=mask, occupied=~mask))
self.assertNotEqual(result[-8:], base[-8:])
with self.assertRaises(ValueError):
encode_character_atlas(base, atlas(BGRA, rgba, h, w))
def test_face_preserves_shared_blocks_and_coarse_levels(self):
rgba = (180, 100, 80, 255)
base = atlas(BC1, rgba)
coverage = np.zeros((16, 16), dtype=bool)
coverage[:4, :8] = True
occupied = np.zeros_like(coverage)
occupied[3, 6] = True
result = encode_character_atlas(base, atlas(BGRA, rgba),
coverage=coverage, occupied=occupied)
self.assertNotEqual(result[1172:1180], base[1172:1180])
self.assertEqual(result[1180:], base[1180:])
if __name__ == '__main__':
unittest.main()