"""Preview synthetic DDS mip levels, array layers, cube faces and volume slices. python -I tests/test_dds_subresources.py """ import contextlib import io import json import struct import sys import tempfile import unittest from pathlib import Path from unittest.mock import patch sys.path.insert(0, str(Path(__file__).resolve().parent)) import test_ttpf_mips as pool_fixtures from test_dds_png import dds, dds_buffer FACES = ("px", "nx", "py", "ny", "pz", "nz") ttpf = pool_fixtures.ttpf def dx10(width, height, payload, *, mips=1, dimension=3, layers=1, cube=False, depth=1, fmt=28, pitch=None): data = bytearray(dds_buffer(width, height, payload, flags=4, fourcc=b"DX10", dxgi=fmt, mips=mips, pitch=pitch)) flags = 0x1007 | (0x8 if pitch is not None else 0) | (0x20000 if mips > 1 else 0) struct.pack_into(" 1 else 0) struct.pack_into(" 1 or dimension == 4 else 0)) struct.pack_into(" normal export path (3D detile) -> preview its last slice. w, h, depth = 8, 8, 8 with tempfile.TemporaryDirectory() as tmp: for fmt, last_pixel, expected in ( (54, (0.5,), bytes((128, 0, 0, 255))), (10, (-1.0, 0.25, 2.0, 0.5), bytes((0, 64, 255, 128))), ): with self.subTest(fmt=fmt): sample = struct.pack("<" + "e" * len(last_pixel), *last_pixel) linear = bytes((depth - 1) * w * h * len(sample)) + sample * (w * h) layout = ttpf.volume_surface_layout(w, h, depth, 1, len(sample)) tiled = pool_fixtures.retile_volume(layout, linear) rec = pool_fixtures.pack_etpf_record(w, h, 1, 0, fmt, depth, 3, 2, 4096) pool_path = Path(tmp, f"vol{fmt}.ttpf") dds_path = pool_path.with_suffix(".dds") pool_fixtures.write_rds5_pool(pool_path, [rec], [tiled]) with contextlib.closing(ttpf.Pool(pool_path)) as pool, contextlib.redirect_stdout(io.StringIO()): self.assertEqual(ttpf.export(pool, 0, str(dds_path)), 0) self.assertEqual(dds.dds_file_to_png_bytes(dds_path, z=7), dds.encode_png(w, h, expected * (w * h))) if fmt == 54: dst = Path(tmp, "slice.png") self.assertEqual(dds.main([str(dds_path), str(dst), "--z", "7"]), 0) self.assertEqual(dst.read_bytes(), dds.encode_png(w, h, expected * (w * h))) def test_cli_header_only_info_and_selected_pixels(self): data = dx10(2, 2, b"", mips=2, layers=2, cube=True) with tempfile.TemporaryDirectory() as tmp: src, dst = Path(tmp, "cube.dds"), Path(tmp, "out.png") src.write_bytes(data) output = io.StringIO() with contextlib.redirect_stdout(output): self.assertEqual(dds.main([str(src), "--info"]), 0) info = json.loads(output.getvalue()) self.assertEqual((info["width"], info["height"], info["mip_count"]), (2, 2, 2)) self.assertEqual((info["dimension"], info["array_size"], info["faces"]), ("2d", 2, list(FACES))) self.assertEqual(list(Path(tmp).iterdir()), [src]) last_pixel = bytes((12, 34, 56, 78)) src.write_bytes(data + bytes(236) + last_pixel) self.assertEqual(dds.main([str(src), str(dst), "--mip", "1", "--layer", "1", "--face", "nz"]), 0) self.assertEqual(dst.read_bytes(), dds.encode_png(1, 1, last_pixel)) if __name__ == "__main__": unittest.main()