|
3 | 3 |
|
4 | 4 | from __future__ import annotations |
5 | 5 |
|
6 | | -from typing import Callable, cast |
| 6 | +from typing import cast |
7 | 7 |
|
8 | 8 | import cv2 # type: ignore[import-not-found] |
9 | 9 | import numpy as np |
10 | 10 | import numpy.typing as npt |
| 11 | +from numpy.lib.stride_tricks import as_strided |
11 | 12 |
|
12 | 13 | from pixutils.formats import PixelFormat, PixelColorEncoding |
13 | 14 |
|
|
20 | 21 | 'GBRG': cv2.COLOR_BAYER_GB2BGR, |
21 | 22 | } |
22 | 23 |
|
23 | | -# Tuple is (cv2 color code or None, reshape function) |
24 | | -RGB_FORMAT_MAP: dict[str, tuple[int | None, Callable]] = { |
| 24 | +RGB_FORMAT_MAP: dict[str, int | None] = { |
25 | 25 | # 32-bit BGRA formats |
26 | | - 'XRGB8888': (cv2.COLOR_BGRA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
27 | | - 'BGRX8888': (cv2.COLOR_BGRA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
28 | | - 'ARGB8888': (cv2.COLOR_BGRA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
29 | | - 'BGRA8888': (cv2.COLOR_BGRA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
| 26 | + 'XRGB8888': cv2.COLOR_BGRA2BGR, |
| 27 | + 'BGRX8888': cv2.COLOR_BGRA2BGR, |
| 28 | + 'ARGB8888': cv2.COLOR_BGRA2BGR, |
| 29 | + 'BGRA8888': cv2.COLOR_BGRA2BGR, |
30 | 30 | # 32-bit RGBA formats |
31 | | - 'XBGR8888': (cv2.COLOR_RGBA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
32 | | - 'RGBX8888': (cv2.COLOR_RGBA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
33 | | - 'ABGR8888': (cv2.COLOR_RGBA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
34 | | - 'RGBA8888': (cv2.COLOR_RGBA2BGR, lambda b, w, h: b.reshape(h, w, 4)), |
| 31 | + 'XBGR8888': cv2.COLOR_RGBA2BGR, |
| 32 | + 'RGBX8888': cv2.COLOR_RGBA2BGR, |
| 33 | + 'ABGR8888': cv2.COLOR_RGBA2BGR, |
| 34 | + 'RGBA8888': cv2.COLOR_RGBA2BGR, |
35 | 35 | # 24-bit formats |
36 | | - 'RGB888': (cv2.COLOR_RGB2BGR, lambda b, w, h: b.reshape(h, w, 3)), |
37 | | - 'BGR888': (None, lambda b, w, h: b.reshape(h, w, 3)), |
| 36 | + 'RGB888': cv2.COLOR_RGB2BGR, |
| 37 | + 'BGR888': None, # Already BGR |
38 | 38 | } |
39 | 39 |
|
40 | | -# Tuple is (cv2 color code or None, reshape function) |
41 | | -YUV_FORMAT_MAP: dict[str, tuple[int, Callable]] = { |
42 | | - 'YUYV': (cv2.COLOR_YUV2BGR_YUY2, lambda b, w, h: b.reshape(h, w, 2)), |
43 | | - 'UYVY': (cv2.COLOR_YUV2BGR_UYVY, lambda b, w, h: b.reshape(h, w, 2)), |
44 | | - 'YVYU': (cv2.COLOR_YUV2BGR_YVYU, lambda b, w, h: b.reshape(h, w, 2)), |
45 | | - 'NV12': (cv2.COLOR_YUV2BGR_NV12, lambda b, w, h: b.reshape(h * 3 // 2, w)), |
46 | | - 'NV21': (cv2.COLOR_YUV2BGR_NV21, lambda b, w, h: b.reshape(h * 3 // 2, w)), |
| 40 | +YUV_FORMAT_MAP: dict[str, int] = { |
| 41 | + 'YUYV': cv2.COLOR_YUV2BGR_YUY2, |
| 42 | + 'UYVY': cv2.COLOR_YUV2BGR_UYVY, |
| 43 | + 'YVYU': cv2.COLOR_YUV2BGR_YVYU, |
| 44 | + 'NV12': cv2.COLOR_YUV2BGR_NV12, |
| 45 | + 'NV21': cv2.COLOR_YUV2BGR_NV21, |
47 | 46 | } |
48 | 47 |
|
49 | 48 |
|
50 | 49 | def _convert_yuv( |
51 | | - fmt: PixelFormat, width: int, height: int, arr: npt.NDArray[np.uint8] |
| 50 | + fmt: PixelFormat, width: int, height: int, stride: int, arr: npt.NDArray[np.uint8] |
52 | 51 | ) -> npt.NDArray[np.uint8]: |
53 | | - cv_code, reshape_func = YUV_FORMAT_MAP[fmt.name] |
54 | | - reshaped = reshape_func(arr, width, height) |
| 52 | + cv_code = YUV_FORMAT_MAP[fmt.name] |
| 53 | + |
| 54 | + if len(fmt.planes) == 1: |
| 55 | + # Packed formats (YUYV, UYVY, YVYU) |
| 56 | + plane = fmt.planes[0] |
| 57 | + bytes_per_pixel = plane.bytes_per_block // plane.pixels_per_block |
| 58 | + |
| 59 | + # OpenCV requires 3D array with channel dimension |
| 60 | + reshaped = as_strided( |
| 61 | + arr, |
| 62 | + shape=(height, width, bytes_per_pixel), |
| 63 | + strides=(stride, bytes_per_pixel, 1), |
| 64 | + writeable=False, |
| 65 | + ) |
| 66 | + else: |
| 67 | + # Multi-plane formats (NV12, NV21) |
| 68 | + # OpenCV expects concatenated layout: (h * 3/2, w) |
| 69 | + reshaped = arr.reshape(height * 3 // 2, width) |
| 70 | + |
55 | 71 | return cv2.cvtColor(reshaped, cv_code) |
56 | 72 |
|
57 | 73 |
|
58 | 74 | def _convert_rgb( |
59 | | - fmt: PixelFormat, width: int, height: int, arr: npt.NDArray[np.uint8] |
| 75 | + fmt: PixelFormat, width: int, height: int, stride: int, arr: npt.NDArray[np.uint8] |
60 | 76 | ) -> npt.NDArray[np.uint8]: |
61 | | - cv_code, reshape_func = RGB_FORMAT_MAP[fmt.name] |
62 | | - reshaped = reshape_func(arr, width, height) |
| 77 | + cv_code = RGB_FORMAT_MAP[fmt.name] |
| 78 | + |
| 79 | + # Generic bytes_per_pixel from plane info |
| 80 | + plane = fmt.planes[0] |
| 81 | + bytes_per_pixel = plane.bytes_per_block // plane.pixels_per_block |
| 82 | + |
| 83 | + # OpenCV requires 3D array with channel dimension |
| 84 | + reshaped = as_strided( |
| 85 | + arr, |
| 86 | + shape=(height, width, bytes_per_pixel), |
| 87 | + strides=(stride, bytes_per_pixel, 1), |
| 88 | + writeable=False, |
| 89 | + ) |
63 | 90 |
|
64 | 91 | if cv_code is None: |
65 | | - # Already BGR, just return a copy |
66 | 92 | return reshaped.copy() |
67 | 93 |
|
68 | 94 | return cv2.cvtColor(reshaped, cv_code) |
69 | 95 |
|
70 | 96 |
|
71 | 97 | def _convert_raw( |
72 | | - fmt: PixelFormat, width: int, height: int, arr: npt.NDArray[np.uint8] |
| 98 | + fmt: PixelFormat, width: int, height: int, stride: int, arr: npt.NDArray[np.uint8] |
73 | 99 | ) -> npt.NDArray[np.uint8] | None: |
74 | 100 | pattern = fmt.bayer_pattern |
75 | 101 | assert pattern is not None |
76 | 102 | cv_code = BAYER_PATTERN_MAP[pattern] |
77 | 103 |
|
78 | 104 | name = fmt.name |
| 105 | + plane = fmt.planes[0] |
| 106 | + |
| 107 | + # Determine element size from plane info |
| 108 | + bytes_per_pixel = plane.bytes_per_block // plane.pixels_per_block |
79 | 109 |
|
80 | | - # Determine bit depth from format name |
81 | | - if '8' in name: |
82 | | - # 8-bit: reshape to (h, w) uint8 |
83 | | - bayer = arr.reshape(height, width) |
| 110 | + if bytes_per_pixel == 1: |
| 111 | + # 8-bit formats |
| 112 | + bayer = as_strided(arr, shape=(height, width), strides=(stride, 1), writeable=False) |
84 | 113 | return cast(npt.NDArray[np.uint8], cv2.cvtColor(bayer, cv_code)) |
85 | | - elif '16' in name: |
86 | | - # 16-bit: reshape to (h, w) uint16, convert, then scale to 8-bit |
87 | | - bayer = arr.view(np.uint16).reshape(height, width) |
88 | | - bgr16 = cast(npt.NDArray[np.uint16], cv2.cvtColor(bayer, cv_code)) |
89 | | - return (bgr16 >> 8).astype(np.uint8) |
90 | | - elif '10' in name or '12' in name: |
91 | | - # 10/12-bit unpacked (stored in 16-bit): shift up to use full 16-bit range |
92 | | - bits = 10 if '10' in name else 12 |
93 | | - bayer = arr.view(np.uint16).reshape(height, width) |
94 | | - bayer = bayer << (16 - bits) |
95 | | - bgr16 = cast(npt.NDArray[np.uint16], cv2.cvtColor(bayer, cv_code)) |
96 | | - return (bgr16 >> 8).astype(np.uint8) |
97 | | - else: |
98 | | - # Unknown bit depth |
99 | | - return None |
| 114 | + elif bytes_per_pixel == 2: |
| 115 | + # 16-bit formats (could be 10, 12, or 16 bit stored in 16) |
| 116 | + arr16 = arr.view(np.uint16) |
| 117 | + bayer = as_strided(arr16, shape=(height, width), strides=(stride, 2), writeable=False) |
| 118 | + |
| 119 | + # Detect actual bit depth from format name for scaling |
| 120 | + if '16' in name: |
| 121 | + bgr16 = cast(npt.NDArray[np.uint16], cv2.cvtColor(bayer, cv_code)) |
| 122 | + return (bgr16 >> 8).astype(np.uint8) |
| 123 | + elif '10' in name or '12' in name: |
| 124 | + bits = 10 if '10' in name else 12 |
| 125 | + bayer = bayer << (16 - bits) |
| 126 | + bgr16 = cast(npt.NDArray[np.uint16], cv2.cvtColor(bayer, cv_code)) |
| 127 | + return (bgr16 >> 8).astype(np.uint8) |
| 128 | + |
| 129 | + return None |
100 | 130 |
|
101 | 131 |
|
102 | 132 | def opencv_convert( |
103 | | - fmt: PixelFormat, width: int, height: int, arr: npt.NDArray[np.uint8] |
| 133 | + fmt: PixelFormat, width: int, height: int, bytesperline: int, arr: npt.NDArray[np.uint8] |
104 | 134 | ) -> npt.NDArray[np.uint8] | None: |
| 135 | + stride = bytesperline if bytesperline > 0 else fmt.stride(width, 0) |
| 136 | + |
105 | 137 | if fmt.color == PixelColorEncoding.YUV: |
106 | | - return _convert_yuv(fmt, width, height, arr) |
| 138 | + return _convert_yuv(fmt, width, height, stride, arr) |
107 | 139 |
|
108 | 140 | if fmt.color == PixelColorEncoding.RAW: |
109 | | - return _convert_raw(fmt, width, height, arr) |
| 141 | + return _convert_raw(fmt, width, height, stride, arr) |
110 | 142 |
|
111 | 143 | if fmt.color == PixelColorEncoding.RGB: |
112 | | - return _convert_rgb(fmt, width, height, arr) |
| 144 | + return _convert_rgb(fmt, width, height, stride, arr) |
113 | 145 |
|
114 | 146 | return None |
0 commit comments