diff --git a/tests/conv-test-data/640x480-BGR888-BGR888.bin.gz b/tests/conv-test-data/640x480-BGR888-BGR888.bin.gz new file mode 100644 index 0000000..302b111 Binary files /dev/null and b/tests/conv-test-data/640x480-BGR888-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-BGR888.bin.gz b/tests/conv-test-data/640x480-BGR888.bin.gz new file mode 100644 index 0000000..5ae30f5 Binary files /dev/null and b/tests/conv-test-data/640x480-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-NV12-BGR888.bin.gz b/tests/conv-test-data/640x480-NV12-BGR888.bin.gz new file mode 100644 index 0000000..9bb7203 Binary files /dev/null and b/tests/conv-test-data/640x480-NV12-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-NV12.bin.gz b/tests/conv-test-data/640x480-NV12.bin.gz new file mode 100644 index 0000000..7f8d8d5 Binary files /dev/null and b/tests/conv-test-data/640x480-NV12.bin.gz differ diff --git a/tests/conv-test-data/640x480-RGB888-BGR888.bin.gz b/tests/conv-test-data/640x480-RGB888-BGR888.bin.gz new file mode 100644 index 0000000..4b24757 Binary files /dev/null and b/tests/conv-test-data/640x480-RGB888-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-RGB888.bin.gz b/tests/conv-test-data/640x480-RGB888.bin.gz new file mode 100644 index 0000000..0cfc675 Binary files /dev/null and b/tests/conv-test-data/640x480-RGB888.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB10-BGR888.bin.gz b/tests/conv-test-data/640x480-SRGGB10-BGR888.bin.gz new file mode 100644 index 0000000..99809a6 Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB10-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB10.bin.gz b/tests/conv-test-data/640x480-SRGGB10.bin.gz new file mode 100644 index 0000000..ef0e14b Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB10.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB10P-BGR888.bin.gz b/tests/conv-test-data/640x480-SRGGB10P-BGR888.bin.gz new file mode 100644 index 0000000..59e45ef Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB10P-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB10P.bin.gz b/tests/conv-test-data/640x480-SRGGB10P.bin.gz new file mode 100644 index 0000000..9b46419 Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB10P.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB12-BGR888.bin.gz b/tests/conv-test-data/640x480-SRGGB12-BGR888.bin.gz new file mode 100644 index 0000000..261ca6d Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB12-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB12.bin.gz b/tests/conv-test-data/640x480-SRGGB12.bin.gz new file mode 100644 index 0000000..61f31c1 Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB12.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB16-BGR888.bin.gz b/tests/conv-test-data/640x480-SRGGB16-BGR888.bin.gz new file mode 100644 index 0000000..51d461b Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB16-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB16.bin.gz b/tests/conv-test-data/640x480-SRGGB16.bin.gz new file mode 100644 index 0000000..d4fffe7 Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB16.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB8-BGR888.bin.gz b/tests/conv-test-data/640x480-SRGGB8-BGR888.bin.gz new file mode 100644 index 0000000..90e0da9 Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB8-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-SRGGB8.bin.gz b/tests/conv-test-data/640x480-SRGGB8.bin.gz new file mode 100644 index 0000000..0d45037 Binary files /dev/null and b/tests/conv-test-data/640x480-SRGGB8.bin.gz differ diff --git a/tests/conv-test-data/640x480-UYVY-BGR888.bin.gz b/tests/conv-test-data/640x480-UYVY-BGR888.bin.gz new file mode 100644 index 0000000..9bae633 Binary files /dev/null and b/tests/conv-test-data/640x480-UYVY-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-UYVY.bin.gz b/tests/conv-test-data/640x480-UYVY.bin.gz new file mode 100644 index 0000000..b47704f Binary files /dev/null and b/tests/conv-test-data/640x480-UYVY.bin.gz differ diff --git a/tests/conv-test-data/640x480-XBGR8888-BGR888.bin.gz b/tests/conv-test-data/640x480-XBGR8888-BGR888.bin.gz new file mode 100644 index 0000000..4d191ea Binary files /dev/null and b/tests/conv-test-data/640x480-XBGR8888-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-XBGR8888.bin.gz b/tests/conv-test-data/640x480-XBGR8888.bin.gz new file mode 100644 index 0000000..5f2ac8f Binary files /dev/null and b/tests/conv-test-data/640x480-XBGR8888.bin.gz differ diff --git a/tests/conv-test-data/640x480-XRGB8888-BGR888.bin.gz b/tests/conv-test-data/640x480-XRGB8888-BGR888.bin.gz new file mode 100644 index 0000000..dfd63d2 Binary files /dev/null and b/tests/conv-test-data/640x480-XRGB8888-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-XRGB8888.bin.gz b/tests/conv-test-data/640x480-XRGB8888.bin.gz new file mode 100644 index 0000000..0cc75ed Binary files /dev/null and b/tests/conv-test-data/640x480-XRGB8888.bin.gz differ diff --git a/tests/conv-test-data/640x480-YUYV-BGR888.bin.gz b/tests/conv-test-data/640x480-YUYV-BGR888.bin.gz new file mode 100644 index 0000000..de51a9e Binary files /dev/null and b/tests/conv-test-data/640x480-YUYV-BGR888.bin.gz differ diff --git a/tests/conv-test-data/640x480-YUYV.bin.gz b/tests/conv-test-data/640x480-YUYV.bin.gz new file mode 100644 index 0000000..37c9980 Binary files /dev/null and b/tests/conv-test-data/640x480-YUYV.bin.gz differ diff --git a/tests/data/test-640-480-BG16.bin.gz b/tests/data/test-640-480-BG16.bin.gz deleted file mode 100644 index 5363a22..0000000 Binary files a/tests/data/test-640-480-BG16.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-BG24.bin.gz b/tests/data/test-640-480-BG24.bin.gz deleted file mode 100644 index 0422a8d..0000000 Binary files a/tests/data/test-640-480-BG24.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-BX24.bin.gz b/tests/data/test-640-480-BX24.bin.gz deleted file mode 100644 index 833d382..0000000 Binary files a/tests/data/test-640-480-BX24.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-NV12.bin.gz b/tests/data/test-640-480-NV12.bin.gz deleted file mode 100644 index 5d768f6..0000000 Binary files a/tests/data/test-640-480-NV12.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-NV21.bin.gz b/tests/data/test-640-480-NV21.bin.gz deleted file mode 100644 index f4d0b6c..0000000 Binary files a/tests/data/test-640-480-NV21.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-RG16.bin.gz b/tests/data/test-640-480-RG16.bin.gz deleted file mode 100644 index 19d31bc..0000000 Binary files a/tests/data/test-640-480-RG16.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-RG24.bin.gz b/tests/data/test-640-480-RG24.bin.gz deleted file mode 100644 index 2820839..0000000 Binary files a/tests/data/test-640-480-RG24.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-RX24.bin.gz b/tests/data/test-640-480-RX24.bin.gz deleted file mode 100644 index 5227610..0000000 Binary files a/tests/data/test-640-480-RX24.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-UYVY.bin.gz b/tests/data/test-640-480-UYVY.bin.gz deleted file mode 100644 index 6c6cc1f..0000000 Binary files a/tests/data/test-640-480-UYVY.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-XB24.bin.gz b/tests/data/test-640-480-XB24.bin.gz deleted file mode 100644 index cc28ac6..0000000 Binary files a/tests/data/test-640-480-XB24.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-XR24.bin.gz b/tests/data/test-640-480-XR24.bin.gz deleted file mode 100644 index ac8e588..0000000 Binary files a/tests/data/test-640-480-XR24.bin.gz and /dev/null differ diff --git a/tests/data/test-640-480-YUYV.bin.gz b/tests/data/test-640-480-YUYV.bin.gz deleted file mode 100644 index e945222..0000000 Binary files a/tests/data/test-640-480-YUYV.bin.gz and /dev/null differ diff --git a/tests/gen-test-data.py b/tests/gen-test-data.py new file mode 100755 index 0000000..2f5197c --- /dev/null +++ b/tests/gen-test-data.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +import gzip +import numpy as np + +from pixutils.formats import PixelFormats +from pixutils.conv import buffer_to_bgr888 + +WIDTH = 640 +HEIGHT = 480 + +SEED = 1234 + +FMTS = [ + PixelFormats.BGR888, + PixelFormats.RGB888, + PixelFormats.XBGR8888, + PixelFormats.XRGB8888, + + PixelFormats.NV12, + PixelFormats.UYVY, + PixelFormats.YUYV, + + PixelFormats.SRGGB10, + PixelFormats.SRGGB10P, + PixelFormats.SRGGB12, + PixelFormats.SRGGB16, + PixelFormats.SRGGB8, +] + +def main(): + options = { + 'range': 'limited', + 'encoding': 'bt601', + } + + for fmt in FMTS: + rnd = np.random.default_rng(SEED) + + size = fmt.framesize(WIDTH, HEIGHT) + + buf = np.frombuffer(rnd.bytes(size), dtype=np.uint8) + rgb = buffer_to_bgr888(fmt, WIDTH, HEIGHT, 0, buf, options) + + with gzip.open(f'{WIDTH}x{HEIGHT}-{fmt}.bin.gz', 'wb') as f: + f.write(buf.tobytes()) + + with gzip.open(f'{WIDTH}x{HEIGHT}-{fmt}-BGR888.bin.gz', 'wb') as f: + f.write(rgb.tobytes()) + +if __name__ == '__main__': + main() diff --git a/tests/test_conv.py b/tests/test_conv.py index 6b1a7d6..2c622b7 100755 --- a/tests/test_conv.py +++ b/tests/test_conv.py @@ -1,85 +1,53 @@ #!/usr/bin/python3 -import os +import glob import gzip +import os import unittest +import re import numpy as np -from pixutils.formats import PixelFormats, str_to_fourcc +from pixutils.formats import PixelFormats, PixelFormat from pixutils.conv import buffer_to_bgr888 TEST_PATH = os.path.dirname(os.path.abspath(__file__)) +DATA_PATH = f'{TEST_PATH}/conv-test-data' class TestConv(unittest.TestCase): def test_conversions(self): - # Some formats are not supported yet - - fmts = [ - #'BG16', - 'BG24', - #'BX24', - 'NV12', - #'NV21', - #'RG16', - 'RG24', - #'RX24', - 'UYVY', - 'XB24', - 'XR24', - 'YUYV', - ] - - #fmts = [ 'YUYV' ] - - w = 640 - h = 480 - - fname = f'{TEST_PATH}/data/test-{w}-{h}-BG24.bin.gz' - - with gzip.open(fname, 'rb') as f: - ref_buf = np.frombuffer(f.read(), dtype=np.uint8) - ref = buffer_to_bgr888(PixelFormats.BGR888, w, h, 0, ref_buf) - ref = ref.astype(np.int16) - - for fourccstr in fmts: - fmt = PixelFormats.find_drm_fourcc(str_to_fourcc(fourccstr)) - - bytesperline = 0 #fmt.stride(w) - - fname = f'{TEST_PATH}/data/test-{w}-{h}-{fourccstr}.bin.gz' - - with gzip.open(fname, 'rb') as f: - data_in = np.frombuffer(f.read(), dtype=np.uint8) + for fname in glob.glob(f'{DATA_PATH}/*.bin.gz'): + bname = os.path.basename(fname) + m = re.match(r'(\d+)x(\d+)-(\w+).bin.gz', bname) + if not m: + continue - # Note: the yuv test images are in bt601 limited - options = { - 'range': 'limited', - 'encoding': 'bt601', - } + width = int(m.group(1)) + height = int(m.group(2)) + fmt = PixelFormats.find_by_name(m.group(3)) - rgb = buffer_to_bgr888(fmt, w, h, bytesperline, data_in, options) + self.run_test_image(width, height, fmt) - self.assertEqual(rgb.shape, ref.shape) + def run_test_image(self, width: int, height: int, fmt: PixelFormat): + with gzip.open(f'{DATA_PATH}/{width}x{height}-{fmt.name}.bin.gz', 'rb') as f: + src_buf = np.frombuffer(f.read(), dtype=np.uint8) - rgb = rgb.astype(np.int16) + with gzip.open(f'{DATA_PATH}/{width}x{height}-{fmt.name}-BGR888.bin.gz', 'rb') as f: + ref_buf = np.frombuffer(f.read(), dtype=np.uint8) - diff = rgb - ref + options = { + 'range': 'limited', + 'encoding': 'bt601', + } - # Exact match? - if not diff.any(): - continue + rgb_buf = buffer_to_bgr888(fmt, width, height, 0, src_buf, options) + rgb_buf = rgb_buf.flatten() - diff = abs(diff) + self.assertEqual(rgb_buf.shape, ref_buf.shape) - b = diff[:,:,0] - g = diff[:,:,1] - r = diff[:,:,2] + diff = rgb_buf - ref_buf - # 2.5 is an arbitrary number that seems to pass for now - self.assertLessEqual(b.mean(), 2.5) - self.assertLessEqual(g.mean(), 2.5) - self.assertLessEqual(r.mean(), 2.5) + self.assertFalse(diff.any()) if __name__ == '__main__':