"""`hhstream.jpeg`: the libjpeg-turbo binding, checked by round-tripping pixels. A lossy codec cannot be tested by looking at a file header, so these tests encode a raster and decode it again through the same library. The raster is chosen to fail loudly on the two mistakes that matter here: uniformly filled blocks (JPEG reproduces a solid block almost exactly, so a tolerance of a few counts is honest) whose channels differ enough that a BGR/RGB mix-up is unmistakable, and a padded stride, because assuming `width * 4` is the bug grim shipped in 1.5. This is also the only test of the pixel-format claim the backend rests on: `TJPF_BGRX` exists precisely so the compositor's B, G, R, X buffer can go into the encoder unconverted. If that were the wrong format, the decoded block colours come back swapped and these tests say so. """ from __future__ import annotations import ctypes import pytest from hhstream.jpeg import DEFAULT_QUALITY, JPEGError, TurboJPEG BLOCK = 16 def blocks() -> list[tuple[int, int, int]]: """Four colours whose channels differ enough that a swap cannot hide.""" return [(200, 30, 30), (30, 200, 30), (30, 30, 200), (140, 120, 100)] def as_rgb(colour: tuple[int, int, int]) -> tuple[int, int, int]: """`blocks()` writes memory order (B, G, R); assertions read (R, G, B). Getting these two the wrong way round is indistinguishable from a wrong pixel format in the encoder -- which is exactly what this file exists to detect, so the conversion is named rather than done inline. """ blue, green, red = colour return red, green, blue def colour_at(x: int, y: int, width: int) -> tuple[int, int, int]: """The block colour at a pixel, wrapping so any frame size is covered. One formula for both the raster and the assertions: they used to index the four colours independently, which ran off the end of the list for anything bigger than a 2x2 grid of blocks. """ column = x // BLOCK row = y // BLOCK return blocks()[((row * (width // BLOCK)) + column) % len(blocks())] def raster(width: int = 64, height: int = 64, stride: int | None = None) -> tuple[bytes, int]: """A BGRX frame of solid blocks; returns (bytes, stride).""" stride = stride if stride is not None else width * 4 out = bytearray() for y in range(height): row = bytearray() for x in range(width): b, g, r = colour_at(x, y, width) row += bytes((b, g, r, 0x00)) row += b"\x77" * (stride - width * 4) # padding, if any out += row return bytes(out), stride def address_of(pixels: bytes) -> int: return ctypes.addressof(ctypes.create_string_buffer(pixels, len(pixels))) def centre_colour(pixels: bytes, stride: int, x: int, y: int) -> tuple[int, int, int]: offset = y * stride + x * 4 blue, green, red, _ = pixels[offset:offset + 4] return red, green, blue def test_a_frame_round_trips_with_its_channels_in_the_right_place(): enc = TurboJPEG() try: pixels, stride = raster() encoded = enc.compress(address_of(pixels), 64, 64, stride) width, height, decoded = enc.decompress(encoded) assert (width, height) == (64, 64) for column, row in ((0, 0), (1, 0), (2, 0), (0, 1), (3, 3)): x, y = column * BLOCK + BLOCK // 2, row * BLOCK + BLOCK // 2 want = as_rgb(colour_at(x, y, 64)) got = centre_colour(decoded, width * 4, x, y) assert got == pytest.approx(want, abs=12), ( f"block ({row}, {column}) came back as {got}, expected ~{want} " "-- a swapped channel order or a wrong pixel format" ) # Being close is not enough on its own: assert the channels are # distinguishable, so a swap could not pass as "close enough". assert max(want) - min(want) > 24, "pick colours a swap would expose" finally: enc.close() def test_a_padded_stride_is_honoured(): """The padding bytes are deliberately not black; leaking them changes the last column of every row.""" enc = TurboJPEG() try: pixels, stride = raster(64, 64, stride=64 * 4 + 32) assert stride != 64 * 4 encoded = enc.compress(address_of(pixels), 64, 64, stride) _, _, decoded = enc.decompress(encoded) got = centre_colour(decoded, 64 * 4, 8, 8) assert got == pytest.approx(as_rgb(colour_at(8, 8, 64)), abs=12), got finally: enc.close() def test_quality_trades_bytes_for_fidelity(): enc = TurboJPEG() try: pixels, stride = raster() small = enc.compress(address_of(pixels), 64, 64, stride, quality=20) large = enc.compress(address_of(pixels), 64, 64, stride, quality=95) assert len(small) < len(large) finally: enc.close() def test_the_output_is_really_a_jpeg(): enc = TurboJPEG() try: pixels, stride = raster() encoded = enc.compress(address_of(pixels), 64, 64, stride) assert encoded.startswith(b"\xff\xd8"), "missing SOI marker" assert b"\xff\xc0" in encoded, "missing SOF0 marker" assert encoded.endswith(b"\xff\xd9"), "missing EOI marker" finally: enc.close() def test_the_default_quality_is_the_one_the_capture_command_used(): """`grim -c -t jpeg -q 80 -` is the deployment's command; a swap must not silently change the frames' size or quality.""" assert DEFAULT_QUALITY == 80 @pytest.mark.parametrize("quality", [0, -1, 101]) def test_an_impossible_quality_is_refused(quality): enc = TurboJPEG() try: pixels, stride = raster() with pytest.raises(ValueError, match="1..100"): enc.compress(address_of(pixels), 64, 64, stride, quality=quality) finally: enc.close() def test_a_library_that_is_not_there_is_a_clear_error_not_a_crash(): with pytest.raises(JPEGError, match="cannot load libjpeg-turbo"): TurboJPEG(path="/nonexistent/libturbojpeg.so") def test_close_is_safe_twice(): enc = TurboJPEG() enc.close() enc.close()