Skip to content

Commit

Permalink
Add test image data and view.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tomba committed Aug 29, 2024
1 parent 53e77c1 commit 6c5b20b
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 23 deletions.
Binary file added tests/data/test-640-480-BG16.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-BG24.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-BX24.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-NV12.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-NV21.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-RG16.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-RG24.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-RX24.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-UYVY.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-XB24.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-XR24.bin.gz
Binary file not shown.
Binary file added tests/data/test-640-480-YUYV.bin.gz
Binary file not shown.
89 changes: 66 additions & 23 deletions tests/test_conv.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,85 @@
#!/usr/bin/python3

import os
import gzip
import unittest
import numpy as np

from pixutils import PixelFormats
from pixutils.conv.conv import to_bgr888
from pixutils.conv import buffer_to_bgr888
from pixutils.fourcc_str import str_to_fourcc

TEST_PATH = os.path.dirname(os.path.abspath(__file__))

class TestRGBConv(unittest.TestCase):
def test_rgb(self):
tests = [
(PixelFormats.BGR888, 2, 2, 0,
(1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6),
(1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6))
#(PixelFormats.RGB888, 64, 48, 0, "rgb888-in.raw", "rgb888-out.raw"),
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',
]

for fmt, w, h, bytesperline, data_in, data_ref in tests:
if isinstance(data_in, str):
data_in = np.fromfile(TEST_PATH + '/' + data_in, dtype=np.uint8)
elif isinstance(data_in, (list, tuple)):
data_in = np.array(data_in, dtype=np.uint8)
#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)

# Note: the yuv test images are in bt601 limited
options = {
'range': 'limited',
'encoding': 'bt601',
}

rgb = buffer_to_bgr888(fmt, w, h, bytesperline, data_in, options)

self.assertEqual(rgb.shape, ref.shape)

rgb = rgb.astype(np.int16)

diff = rgb - ref

if isinstance(data_ref, str):
data_ref = np.fromfile(TEST_PATH + '/' + data_ref, dtype=np.uint8)
elif isinstance(data_ref, (list, tuple)):
data_ref = np.array(data_ref, dtype=np.uint8)
# Exact match?
if not diff.any():
continue

data_out = to_bgr888(fmt, w, h, bytesperline, data_in)
diff = abs(diff)

# Flatten for comparison
data_out = data_out.flatten()
b = diff[:,:,0]
g = diff[:,:,1]
r = diff[:,:,2]

self.assertTrue(np.array_equal(data_out, data_ref))
# 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)

if __name__ == '__main__':
unittest.main()
119 changes: 119 additions & 0 deletions tests/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/python3

# XXX I have not been able to get PyQt6 imported to pylint
# pylint: skip-file

import os
import gzip
import sys

import numpy as np
from PyQt6.QtCore import Qt
from PyQt6 import QtWidgets

from pixutils import PixelFormats
from pixutils.conv import buffer_to_bgr888
from pixutils.conv.qt import bgr888_to_pix
from pixutils.fourcc_str import str_to_fourcc

TEST_PATH = os.path.dirname(os.path.abspath(__file__))

def main():
qApp = QtWidgets.QApplication(sys.argv)

# 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:
try:
fmt = PixelFormats.find_drm_fourcc(str_to_fourcc(fourccstr))
except StopIteration:
print(f'fourcc {fourccstr} not supported')
continue

print(f'Showing {fmt} ({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)

# Note: the yuv test images are in bt601 limited
options = {
'range': 'limited',
'encoding': 'bt601',
}

rgb = buffer_to_bgr888(fmt, w, h, bytesperline, data_in, options)

assert rgb.shape == ref.shape

rgb = rgb.astype(np.int16)

diff = rgb - ref

if not diff.any():
print(' Match')
continue

diff = abs(diff)

b = diff[:,:,0]
g = diff[:,:,1]
r = diff[:,:,2]

print(' min() {:5} {:5} {:5}'.format(b.min(), g.min(), r.min()))
print(' max() {:5} {:5} {:5}'.format(b.max(), g.max(), r.max()))
print(' mean() {:5.2} {:5.2} {:5.2}'.format(b.mean(), g.mean(), r.mean()))

widget = QtWidgets.QWidget()
widget.setWindowTitle(fourccstr)
layout = QtWidgets.QHBoxLayout()

label = QtWidgets.QLabel()
label.setPixmap(bgr888_to_pix(ref.astype(np.uint8)))
layout.addWidget(label)

label = QtWidgets.QLabel()
label.setPixmap(bgr888_to_pix(rgb.astype(np.uint8)))
layout.addWidget(label)

label = QtWidgets.QLabel()
label.setPixmap(bgr888_to_pix(diff.astype(np.uint8)))
layout.addWidget(label)

widget.setLayout(layout)
widget.showMaximized()

qApp.exec()

if __name__ == '__main__':
main()

0 comments on commit 6c5b20b

Please sign in to comment.