#!/usr/bin/env python3 """Host-side unit tests for chandra-camera frame/camera helpers (no hardware required).""" import importlib.machinery import importlib.util import os import sys import unittest try: import cv2 import numpy as np HAS_CV2_NUMPY = True except ImportError: cv2 = None np = None HAS_CV2_NUMPY = False CAM_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "chandra-camera") _loader = importlib.machinery.SourceFileLoader("chandra_camera_mod", CAM_PATH) _spec = importlib.util.spec_from_loader("chandra_camera_mod", _loader) cam_mod = importlib.util.module_from_spec(_spec) sys.modules["chandra_camera_mod"] = cam_mod _loader.exec_module(cam_mod) class TestCameraHelpers(unittest.TestCase): def test_sort_and_label_cameras(self): raw = [ ("/base/front", "libcamera", "imx371", 0, 90, 4656 * 3496), ("/base/back2", "libcamera", "imx376", 1, 270, 2592 * 1944), ("/base/back1", "libcamera", "imx519", 1, 270, 4656 * 3496), ] cams = cam_mod.sort_and_label_cameras(raw) self.assertEqual([c.model for c in cams], ["imx519", "imx371", "imx376"]) self.assertEqual([c.label for c in cams], ["Back · imx519", "Front · imx371", "Back 2 · imx376"]) self.assertEqual(cam_mod.select_initial_camera(cams, ""), 0) self.assertEqual(cam_mod.select_initial_camera(cams, "front"), 1) self.assertEqual(cam_mod.select_initial_camera(cams, "back"), 0) self.assertEqual(cam_mod.select_initial_camera(cams, "imx376"), 2) @unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy") def test_convert_abgr8888_with_stride_and_rotations(self): # 2x3 image with stride padding (row = 2*4 + 4 padding bytes = 12) h, w, stride = 3, 2, 12 buf = bytearray(h * stride) # Pixel (x=0, y=0): ABGR8888 in LE memory is [R, G, B, A] = [10, 20, 200, 255] -> BGR [200, 20, 10] buf[0:4] = bytes([10, 20, 200, 255]) # Pixel (x=1, y=0): R=40, G=50, B=60 -> BGR [60, 50, 40] buf[4:8] = bytes([40, 50, 60, 255]) bgr0 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=0, cv2_mod=cv2, np_mod=np) self.assertEqual(bgr0.shape, (3, 2, 3)) self.assertEqual(tuple(bgr0[0, 0]), (200, 20, 10)) self.assertEqual(tuple(bgr0[0, 1]), (60, 50, 40)) bgr90 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=90, cv2_mod=cv2, np_mod=np) self.assertEqual(bgr90.shape, (2, 3, 3)) # Clockwise 90 deg puts original (row=0, col=0) at (row=0, col=h-1=2) self.assertEqual(tuple(bgr90[0, 2]), (200, 20, 10)) bgr270 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=270, cv2_mod=cv2, np_mod=np) self.assertEqual(bgr270.shape, (2, 3, 3)) # Counter-clockwise 90 deg puts original (row=0, col=0) at (row=w-1=1, col=0) self.assertEqual(tuple(bgr270[1, 0]), (200, 20, 10)) @unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy") def test_convert_argb_rgb_and_cairo_bgra(self): # ARGB8888 in LE memory is [B, G, R, A] buf = bytes([210, 110, 30, 255, 10, 20, 30, 255]) bgr = cam_mod.convert_frame(buf, 2, 1, 8, "ARGB8888", rot=0, cv2_mod=cv2, np_mod=np) self.assertEqual(tuple(bgr[0, 0]), (210, 110, 30)) rgb_buf = bytes([11, 22, 33, 44, 55, 66]) bgr2 = cam_mod.convert_frame(rgb_buf, 2, 1, 6, "RGB888", rot=180, cv2_mod=cv2, np_mod=np) # Inverted horizontally+vertically and channel-flipped -> [66, 55, 44] then [33, 22, 11] self.assertEqual(tuple(bgr2[0, 0]), (66, 55, 44)) self.assertEqual(tuple(bgr2[0, 1]), (33, 22, 11)) argb32 = cam_mod.bgr_to_cairo_argb32(bgr, np_mod=np) self.assertEqual(argb32.shape, (1, 2, 4)) self.assertEqual(tuple(argb32[0, 0]), (210, 110, 30, 255)) @unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy") def test_ensure_even_frame(self): odd = np.zeros((101, 203, 3), dtype=np.uint8) even = cam_mod.ensure_even_frame(odd) self.assertEqual(even.shape, (100, 202, 3)) if __name__ == "__main__": unittest.main()