Skip to content

Commit

Permalink
tests: Update test_conv and data
Browse files Browse the repository at this point in the history
  • Loading branch information
tomba committed Dec 1, 2024
1 parent 9dc03be commit 8b3766a
Show file tree
Hide file tree
Showing 38 changed files with 80 additions and 60 deletions.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-BGR888.bin.gz
Binary file not shown.
Binary file added tests/conv-test-data/640x480-NV12-BGR888.bin.gz
Binary file not shown.
Binary file added tests/conv-test-data/640x480-NV12.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-RGB888.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-SRGGB10.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-SRGGB10P.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-SRGGB12.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-SRGGB16.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-SRGGB8.bin.gz
Binary file not shown.
Binary file added tests/conv-test-data/640x480-UYVY-BGR888.bin.gz
Binary file not shown.
Binary file added tests/conv-test-data/640x480-UYVY.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-XBGR8888.bin.gz
Binary file not shown.
Binary file not shown.
Binary file added tests/conv-test-data/640x480-XRGB8888.bin.gz
Binary file not shown.
Binary file added tests/conv-test-data/640x480-YUYV-BGR888.bin.gz
Binary file not shown.
Binary file added tests/conv-test-data/640x480-YUYV.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-BG16.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-BG24.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-BX24.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-NV12.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-NV21.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-RG16.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-RG24.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-RX24.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-UYVY.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-XB24.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-XR24.bin.gz
Binary file not shown.
Binary file removed tests/data/test-640-480-YUYV.bin.gz
Binary file not shown.
52 changes: 52 additions & 0 deletions tests/gen-test-data.py
Original file line number Diff line number Diff line change
@@ -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()
88 changes: 28 additions & 60 deletions tests/test_conv.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down

0 comments on commit 8b3766a

Please sign in to comment.