Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Tomi Valkeinen <[email protected]>
  • Loading branch information
tomba committed Oct 31, 2024
1 parent f1b6217 commit eb14bd7
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 13 deletions.
5 changes: 3 additions & 2 deletions pixutils/conv/conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .rgb import rgb_to_bgr888
from .raw import raw_to_bgr888


def to_bgr888(fmt: PixelFormat, w, h, bytesperline, arr: npt.NDArray[np.uint8],
options: None | dict = None):
if fmt.color == PixelColorEncoding.YUV:
Expand All @@ -25,8 +26,8 @@ def to_bgr888(fmt: PixelFormat, w, h, bytesperline, arr: npt.NDArray[np.uint8],

raise RuntimeError(f'Unsupported format {fmt}')

def buffer_to_bgr888(fmt: PixelFormat, w, h, bytesperline, buffer,
options: None | dict = None):

def buffer_to_bgr888(fmt: PixelFormat, w, h, bytesperline, buffer, options: None | dict = None):
arr = np.frombuffer(buffer, dtype=np.uint8)
rgb = to_bgr888(fmt, w, h, bytesperline, arr, options)
return rgb
4 changes: 2 additions & 2 deletions pixutils/conv/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pixutils.formats import PixelFormat, PixelFormats
from .conv import buffer_to_bgr888


def bgr888_to_pix(rgb):
# QImage doesn't seem to like a numpy view
if rgb.base is not None:
Expand All @@ -22,8 +23,7 @@ def bgr888_to_pix(rgb):
return pix


def buffer_to_pix(fmt: PixelFormat, w, h, bytesperline, buffer,
options: None | dict = None):
def buffer_to_pix(fmt: PixelFormat, w, h, bytesperline, buffer, options: None | dict = None):
if fmt == PixelFormats.MJPEG:
pix = QtGui.QPixmap(w, h)
pix.loadFromData(buffer)
Expand Down
9 changes: 6 additions & 3 deletions pixutils/conv/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

from pixutils.formats import PixelFormat

__all__ = [ 'raw_to_bgr888' ]
__all__ = ['raw_to_bgr888']

# Debayering code from PiCamera documentation


def demosaic(data: npt.NDArray[np.uint16], r0, g0, g1, b0):
# Separate the components from the Bayer data to RGB planes

Expand Down Expand Up @@ -75,12 +76,13 @@ def demosaic(data: npt.NDArray[np.uint16], r0, g0, g1, b0):

return output


def raw_packed_to_bgr888(data: npt.NDArray[np.uint8], w, h, bytesperline, fmt: PixelFormat):
fmtname = fmt.name

bayer_pattern = fmtname[1:5]

packed = fmtname.endswith("P")
packed = fmtname.endswith('P')

if packed:
bitspp = int(fmtname[5:-1])
Expand Down Expand Up @@ -124,12 +126,13 @@ def raw_packed_to_bgr888(data: npt.NDArray[np.uint8], w, h, bytesperline, fmt: P

return rgb


def raw_to_bgr888(data: npt.NDArray[np.uint8], w, h, bytesperline, fmt: PixelFormat):
fmtname = fmt.name

bayer_pattern = fmtname[1:5]

packed = fmtname.endswith("P")
packed = fmtname.endswith('P')

if packed:
bitspp = int(fmtname[5:-1])
Expand Down
1 change: 1 addition & 0 deletions pixutils/conv/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pixutils.formats import PixelFormat, PixelFormats


def rgb_to_bgr888(fmt: PixelFormat, w, h, data: npt.NDArray[np.uint8]):
if fmt == PixelFormats.RGB888:
rgb = data.reshape((h, w, 3))
Expand Down
7 changes: 5 additions & 2 deletions pixutils/conv/yuv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
'matrix': [
[1.1643854428931106, 1.1643854428931106, 1.1643854428931106],
[0.0, -0.3917753871976806, 2.01722743572137],
[1.596032654306859, -0.8129854276340887, 0.0]
[1.596032654306859, -0.8129854276340887, 0.0],
],
},
'full': {
'offsets': (0, -128, -128),
'matrix': [
[1.0, 1.0, 1.0],
[0.0, -0.3441367208361944, 1.7720149538414587],
[1.4019989318684671, -0.714152742809186, 0.0]
[1.4019989318684671, -0.714152742809186, 0.0],
],
},
},
Expand Down Expand Up @@ -75,6 +75,7 @@ def uyvy_to_bgr888(data, w, h, options):

return ycbcr_to_bgr888(yuv, options)


def nv12_to_bgr888(data, w, h, options):
plane1 = data[:w * h]
plane2 = data[w * h:]
Expand All @@ -90,6 +91,7 @@ def nv12_to_bgr888(data, w, h, options):

return ycbcr_to_bgr888(yuv, options)


def y8_to_bgr888(data, w, h):
y = data.reshape((h, w))

Expand All @@ -101,6 +103,7 @@ def y8_to_bgr888(data, w, h):

return yuv


def yuv_to_bgr888(arr, w, h, fmt, options):
if fmt == PixelFormats.Y8:
return y8_to_bgr888(arr, w, h)
Expand Down
4 changes: 3 additions & 1 deletion pixutils/dmaheap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pixutils.ioctl import IOWR

__all__ = [ 'DMAHeap', 'DMAHeapBuffer' ]
__all__ = ['DMAHeap', 'DMAHeapBuffer']

# pylint: disable=invalid-name
class struct_dma_heap_allocation_data(ctypes.Structure):
Expand All @@ -20,6 +20,7 @@ class struct_dma_heap_allocation_data(ctypes.Structure):

DMA_HEAP_IOCTL_ALLOC = IOWR(DMA_HEAP_IOC_MAGIC, 0x0, struct_dma_heap_allocation_data)


class DMAHeap:
def __init__(self, name: str):
self.fd = os.open(f'/dev/dma_heap/{name}', os.O_CLOEXEC | os.O_RDWR)
Expand All @@ -35,6 +36,7 @@ def alloc(self, length: int):

return DMAHeapBuffer(buf_data.fd, buf_data.len)


class DMAHeapBuffer:
def __init__(self, fd: int, length: int):
self.fd = fd
Expand Down
3 changes: 2 additions & 1 deletion pixutils/formats/fourcc_str.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

__all__ = [ 'fourcc_to_str', 'str_to_fourcc' ]
__all__ = ['fourcc_to_str', 'str_to_fourcc']


def fourcc_to_str(fourcc: int):
return ''.join((
Expand Down
2 changes: 1 addition & 1 deletion pixutils/formats/metaformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .fourcc_str import str_to_fourcc

__all__ = [ 'MetaFormat', 'MetaFormats' ]
__all__ = ['MetaFormat', 'MetaFormats']


class MetaFormat:
Expand Down
3 changes: 2 additions & 1 deletion pixutils/formats/pixelformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from .fourcc_str import str_to_fourcc

__all__ = [ 'PixelColorEncoding', 'PixelFormat', 'PixelFormats' ]
__all__ = ['PixelColorEncoding', 'PixelFormat', 'PixelFormats']


class PixelColorEncoding(Enum):
RGB = 0
Expand Down
1 change: 1 addition & 0 deletions tests/print-fmts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pixutils.formats import PixelFormats, fourcc_to_str


def main():
for fmt in PixelFormats.get_formats():
if fmt.drm_fourcc:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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


class TestConv(unittest.TestCase):
def test_conversions(self):
# Some formats are not supported yet
Expand Down Expand Up @@ -80,5 +81,6 @@ def test_conversions(self):
self.assertLessEqual(g.mean(), 2.5)
self.assertLessEqual(r.mean(), 2.5)


if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions tests/test_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import pixutils
import pixutils.formats


class TestDummy(unittest.TestCase):
def test_dummy(self):
self.assertEqual(pixutils.formats.PixelFormats.RGB888.name, 'RGB888')


class TestFourcc(unittest.TestCase):
def test_fourcc(self):
self.assertEqual(pixutils.formats.fourcc_str.str_to_fourcc('XR24'), 0x34325258)
Expand All @@ -17,5 +19,6 @@ def test_fourcc(self):
with self.assertRaises(ValueError):
pixutils.formats.fourcc_str.str_to_fourcc('ABC')


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions tests/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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


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

Expand Down Expand Up @@ -113,5 +114,6 @@ def main():

qapp.exec()


if __name__ == '__main__':
main()

0 comments on commit eb14bd7

Please sign in to comment.