"""Unit tests for tools/bc_feature.py covering BC1-BC7 block compression detection and bit-depth verification. python -I tests/test_bc_feature.py """ import math import os import struct import sys import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOLS_DIR = os.path.join(ROOT, "tools") if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) try: import numpy as np import bc_feature HAVE_NUMPY = True except ImportError: HAVE_NUMPY = False # Block compression specifications: bytes per element (4x4 block), bits per pixel, and channels BC_SPECS = { "BC1": {"bpe": 8, "bpp": 4.0, "channels": 3, "alpha_bits": 1, "name": "BC1_UNORM"}, "BC2": {"bpe": 16, "bpp": 8.0, "channels": 4, "alpha_bits": 4, "name": "BC2_UNORM"}, "BC3": {"bpe": 16, "bpp": 8.0, "channels": 4, "alpha_bits": 8, "name": "BC3_UNORM"}, "BC4": {"bpe": 8, "bpp": 4.0, "channels": 1, "alpha_bits": 0, "name": "BC4_UNORM"}, "BC5": {"bpe": 16, "bpp": 8.0, "channels": 2, "alpha_bits": 0, "name": "BC5_UNORM"}, "BC6H": {"bpe": 16, "bpp": 8.0, "channels": 3, "alpha_bits": 0, "name": "BC6H_UF16"}, "BC7": {"bpe": 16, "bpp": 8.0, "channels": 4, "alpha_bits": 8, "name": "BC7_UNORM"}, } def detect_bc_format_from_bpe_and_channels(bpe: int, channels: int) -> str: """Identify BC block compression format from block byte size and channel count.""" for fmt, spec in BC_SPECS.items(): if spec["bpe"] == bpe and spec["channels"] == channels: return fmt raise ValueError(f"Unknown BC format with bpe={bpe}, channels={channels}") def decode_bc7_mode(first_byte: int) -> int: """Detect BC7 compression mode (0-7) from the unary mode header in the first byte. BC7 modes are encoded in least-significant bits as a 1 preceded by zeros: bit 0 = 1 -> mode 0 bit 1 = 1 -> mode 1 ... bit 7 = 1 -> mode 7 """ if first_byte == 0: return -1 for mode in range(8): if (first_byte >> mode) & 1: return mode return -1 def decode_bc6h_mode(low_bits: int) -> int: """Detect BC6H compression mode (1-14) from the low header bits. Modes 1-2 use a 2-bit header (bits [1:0] = 00 or 01). Modes 3-14 use a 5-bit header (bits [1:0] = 10 or 11 with bits [4:2] selecting mode). """ two_bits = low_bits & 0x03 if two_bits == 0: return 1 if two_bits == 1: return 2 five_bits = low_bits & 0x1F mode_map = { 0x02: 3, 0x06: 4, 0x0A: 5, 0x0E: 6, 0x12: 7, 0x16: 8, 0x1A: 9, 0x1E: 10, 0x03: 11, 0x07: 12, 0x0B: 13, 0x0F: 14, } return mode_map.get(five_bits, -1) def make_synthetic_bc1_block(c0: int, c1: int, indices: list) -> bytes: """Create a synthetic 8-byte BC1 block from two 16-bit 565 colors and 16 2-bit indices.""" assert len(indices) == 16 bits = 0 for k, idx in enumerate(indices): assert 0 <= idx <= 3 bits |= (idx & 3) << (2 * k) return struct.pack(" bytes: """Create a synthetic 16-byte BC3 block: 8-byte alpha ramp + 8-byte BC1 colour.""" assert len(a_indices) == 16 # 48-bit index packed into 6 bytes abits = 0 for k, idx in enumerate(a_indices): assert 0 <= idx <= 7 abits |= (idx & 7) << (3 * k) alpha_part = struct.pack(" c1) verifies 4 palette entries and interpolation weights.""" c0 = 0xF800 # Red (255, 0, 0) c1 = 0x001F # Blue (0, 0, 255) self.assertGreater(c0, c1) # Block where all 16 pixels select index 0 (color0) b0 = make_synthetic_bc1_block(c0, c1, [0] * 16) arr0 = np.frombuffer(b0, dtype=np.uint8).reshape(1, 8) avg0 = bc_feature.bc1_average(arr0)[0] self.assertAlmostEqual(float(avg0[0]), 255.0, places=3) self.assertAlmostEqual(float(avg0[1]), 0.0, places=3) self.assertAlmostEqual(float(avg0[2]), 0.0, places=3) # Block where all 16 pixels select index 1 (color1) b1 = make_synthetic_bc1_block(c0, c1, [1] * 16) arr1 = np.frombuffer(b1, dtype=np.uint8).reshape(1, 8) avg1 = bc_feature.bc1_average(arr1)[0] self.assertAlmostEqual(float(avg1[0]), 0.0, places=3) self.assertAlmostEqual(float(avg1[1]), 0.0, places=3) self.assertAlmostEqual(float(avg1[2]), 255.0, places=3) # Block where all 16 pixels select index 2: (2 * p0 + p1) / 3 b2 = make_synthetic_bc1_block(c0, c1, [2] * 16) arr2 = np.frombuffer(b2, dtype=np.uint8).reshape(1, 8) avg2 = bc_feature.bc1_average(arr2)[0] self.assertAlmostEqual(float(avg2[0]), 255.0 * 2 / 3, places=3) self.assertAlmostEqual(float(avg2[1]), 0.0, places=3) self.assertAlmostEqual(float(avg2[2]), 255.0 * 1 / 3, places=3) # Block where all 16 pixels select index 3: (p0 + 2 * p1) / 3 b3 = make_synthetic_bc1_block(c0, c1, [3] * 16) arr3 = np.frombuffer(b3, dtype=np.uint8).reshape(1, 8) avg3 = bc_feature.bc1_average(arr3)[0] self.assertAlmostEqual(float(avg3[0]), 255.0 * 1 / 3, places=3) self.assertAlmostEqual(float(avg3[1]), 0.0, places=3) self.assertAlmostEqual(float(avg3[2]), 255.0 * 2 / 3, places=3) # Uniform mix: exactly 4 pixels of each index mixed_indices = [0, 1, 2, 3] * 4 b_mix = make_synthetic_bc1_block(c0, c1, mixed_indices) arr_mix = np.frombuffer(b_mix, dtype=np.uint8).reshape(1, 8) avg_mix = bc_feature.bc1_average(arr_mix)[0] expected_r = (255.0 + 0.0 + (255.0 * 2 / 3) + (255.0 * 1 / 3)) / 4 expected_b = (0.0 + 255.0 + (255.0 * 1 / 3) + (255.0 * 2 / 3)) / 4 self.assertAlmostEqual(float(avg_mix[0]), expected_r, places=3) self.assertAlmostEqual(float(avg_mix[2]), expected_b, places=3) def test_bc1_average_three_color_transparent_mode(self): """bc1_average in 3-color mode (c0 <= c1) sets p2=(p0+p1)/2 and p3=0 (transparent black).""" c0 = 0x001F # Blue (c0 = 31) c1 = 0xF800 # Red (c1 = 63488) self.assertLess(c0, c1) # When all pixels select index 3, output must be transparent black (0, 0, 0) b3 = make_synthetic_bc1_block(c0, c1, [3] * 16) arr3 = np.frombuffer(b3, dtype=np.uint8).reshape(1, 8) avg3 = bc_feature.bc1_average(arr3)[0] self.assertAlmostEqual(float(avg3[0]), 0.0, places=4) self.assertAlmostEqual(float(avg3[1]), 0.0, places=4) self.assertAlmostEqual(float(avg3[2]), 0.0, places=4) # When all pixels select index 2, output is midpoint (p0 + p1) / 2 b2 = make_synthetic_bc1_block(c0, c1, [2] * 16) arr2 = np.frombuffer(b2, dtype=np.uint8).reshape(1, 8) avg2 = bc_feature.bc1_average(arr2)[0] self.assertAlmostEqual(float(avg2[0]), 127.5, places=3) self.assertAlmostEqual(float(avg2[1]), 0.0, places=3) self.assertAlmostEqual(float(avg2[2]), 127.5, places=3) # Boundary condition: c0 == c1 b_equal = make_synthetic_bc1_block(0x07E0, 0x07E0, [2] * 16) arr_equal = np.frombuffer(b_equal, dtype=np.uint8).reshape(1, 8) avg_equal = bc_feature.bc1_average(arr_equal)[0] self.assertAlmostEqual(float(avg_equal[1]), 255.0, places=3) def test_block_average_bpe_selection(self): """block_average uses full 8 bytes for bpe=8 and second 8 bytes (8:16) for bpe=16.""" c0 = 0xF800 # Red c1 = 0x07E0 # Green b_color = make_synthetic_bc1_block(c0, c1, [0] * 16) # bpe = 8: whole block is color arr8 = np.frombuffer(b_color, dtype=np.uint8).reshape(1, 8) res8 = bc_feature.block_average(arr8, bpe=8)[0] self.assertAlmostEqual(float(res8[0]), 255.0, places=3) self.assertAlmostEqual(float(res8[1]), 0.0, places=3) # bpe = 16: first 8 bytes arbitrary alpha payload, bytes 8:16 are color b16 = make_synthetic_bc3_block(255, 0, [0] * 16, c0, c1, [0] * 16) self.assertEqual(len(b16), 16) arr16 = np.frombuffer(b16, dtype=np.uint8).reshape(1, 16) res16 = bc_feature.block_average(arr16, bpe=16)[0] self.assertAlmostEqual(float(res16[0]), 255.0, places=3) self.assertAlmostEqual(float(res16[1]), 0.0, places=3) def test_bc3_uses_four_colors_with_ascending_endpoints(self): c0, c1 = 0x001F, 0xF800 block = make_synthetic_bc3_block(255, 0, [0] * 16, c0, c1, [3] * 16) data = np.frombuffer(block, dtype=np.uint8).reshape(1, 16) np.testing.assert_allclose(bc_feature.block_average(data, bpe=16)[0], [170, 0, 85]) def test_smoothness_metric(self): """smoothness returns neighbor difference relative to spread (lower = more coherent).""" # Flat/constant image has zero neighbor differences -> 0.0 flat = np.ones((8, 8, 3), dtype=np.float32) * 128.0 self.assertAlmostEqual(bc_feature.smoothness(flat), 0.0, places=5) # Smooth horizontal gradient grad = np.tile(np.linspace(0, 255, 16, dtype=np.float32).reshape(1, 16, 1), (16, 1, 3)) # High-frequency alternating checkerboard noise noisy = np.zeros((16, 16, 3), dtype=np.float32) noisy[::2, ::2] = 255.0 noisy[1::2, 1::2] = 255.0 s_grad = bc_feature.smoothness(grad) s_noisy = bc_feature.smoothness(noisy) self.assertGreater(s_noisy, s_grad, "Noisy image must have higher smoothness score than gradient") self.assertTrue(math.isfinite(s_grad)) self.assertTrue(math.isfinite(s_noisy)) if __name__ == "__main__": unittest.main()