86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
import hashlib
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
|
|
sys.path.insert(0, str(SCRIPTS))
|
|
|
|
from build_raster_assets import (
|
|
build_opaque_cover_crop,
|
|
build_opaque_resize,
|
|
extract_warm_gold_frame,
|
|
save_png,
|
|
)
|
|
|
|
|
|
class RasterAssetBuilderTests(unittest.TestCase):
|
|
"""锁定迁移后的两类背景处理与 G01 暖金边框提取语义。"""
|
|
|
|
def test_opaque_resize_uses_locked_output_size_and_rgb(self):
|
|
source = Image.new("RGBA", (2, 3), (120, 80, 40, 128))
|
|
output = build_opaque_resize(source, (4, 6))
|
|
self.assertEqual((4, 6), output.size)
|
|
self.assertEqual("RGB", output.mode)
|
|
|
|
def test_cover_crop_centers_the_scaled_source(self):
|
|
source = Image.new("RGB", (2, 1), (255, 0, 0))
|
|
source.putpixel((1, 0), (0, 0, 255))
|
|
output = build_opaque_cover_crop(source, (2, 2))
|
|
self.assertEqual((2, 2), output.size)
|
|
self.assertEqual("RGB", output.mode)
|
|
self.assertNotEqual(output.getpixel((0, 0)), output.getpixel((1, 0)))
|
|
|
|
def test_warm_gold_frame_keeps_only_the_edge_band(self):
|
|
source = Image.new("RGBA", (5, 5), (180, 140, 80, 255))
|
|
config = {
|
|
"borderBand": 1,
|
|
"redGreenMin": 15,
|
|
"greenBlueMin": 12,
|
|
"redBlueMin": 35,
|
|
"redMaxExclusive": 245,
|
|
"blueMaxExclusive": 180,
|
|
"alphaOffset": 25,
|
|
"alphaScale": 6,
|
|
}
|
|
output = extract_warm_gold_frame(source, config)
|
|
self.assertGreater(output.getpixel((0, 2))[3], 0)
|
|
self.assertEqual((0, 0, 0, 0), output.getpixel((2, 2)))
|
|
|
|
def test_warm_gold_frame_rejects_non_gold_and_clears_hidden_rgb(self):
|
|
source = Image.new("RGBA", (1, 1), (100, 150, 100, 255))
|
|
config = {
|
|
"borderBand": 1,
|
|
"redGreenMin": 15,
|
|
"greenBlueMin": 12,
|
|
"redBlueMin": 35,
|
|
"redMaxExclusive": 245,
|
|
"blueMaxExclusive": 180,
|
|
"alphaOffset": 25,
|
|
"alphaScale": 6,
|
|
}
|
|
output = extract_warm_gold_frame(source, config)
|
|
self.assertEqual((0, 0, 0, 0), output.getpixel((0, 0)))
|
|
|
|
def test_png_save_is_srgb_and_byte_deterministic(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
first = Path(directory) / "first.png"
|
|
second = Path(directory) / "second.png"
|
|
image = Image.new("RGB", (4, 4), (230, 220, 200))
|
|
save_png(image, first)
|
|
save_png(image, second)
|
|
self.assertEqual(first.read_bytes(), second.read_bytes())
|
|
self.assertEqual(
|
|
hashlib.sha256(first.read_bytes()).hexdigest(),
|
|
hashlib.sha256(second.read_bytes()).hexdigest(),
|
|
)
|
|
with Image.open(first) as opened:
|
|
self.assertIn("srgb", opened.info)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|