Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up convesions for color arrays #1454

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions rerun_py/rerun_sdk/rerun/color_conversion.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
"""Color conversion utilities."""
from typing import Sequence, Union
from typing import Union

import numpy as np
import numpy.typing as npt


def u8_array_to_rgba(arr: Sequence[int]) -> np.uint32:
def u8_array_to_rgba(arr: npt.NDArray[np.uint8]) -> npt.NDArray[np.uint32]:
"""
Convert an array[4] of uint8 values into a uint32.
Convert an array with inner dimension [R,G,B,A] into packed uint32 values.

Parameters
----------
arr : Sequence[int]
The array of uint8 values to convert in RGBA order.
arr :
Nx3 or Nx4 `[[r,g,b,a], ... ]` of uint8 values

Returns
-------
int
The uint32 value as 0xRRGGBBAA.
npt.NDArray[np.uint32]
Array of uint32 value as 0xRRGGBBAA.

"""
red = arr[0]
green = arr[1]
blue = arr[2]
alpha = arr[3] if len(arr) == 4 else 0xFF
return np.uint32((red << 24) + (green << 16) + (blue << 8) + alpha)
r = arr[:, 0]
g = arr[:, 1]
b = arr[:, 2]
a = arr[:, 3] if arr.shape[1] == 4 else np.repeat(0xFF, len(arr))
# Reverse the byte order because this is how we encode into uint32
arr = np.vstack([a, b, g, r]).T
# Make contiguous and then reinterpret
arr = np.ascontiguousarray(arr, dtype=np.uint8)
arr = arr.view(np.uint32)
arr = np.squeeze(arr, axis=1)
return arr # type: ignore[return-value]


def linear_to_gamma_u8_value(linear: npt.NDArray[Union[np.float32, np.float64]]) -> npt.NDArray[np.uint8]:
Expand Down
2 changes: 1 addition & 1 deletion rerun_py/rerun_sdk/rerun/components/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class ColorRGBAArray(pa.ExtensionArray): # type: ignore[misc]
def from_numpy(array: npt.NDArray[np.uint8]) -> ColorRGBAArray:
"""Build a `ColorRGBAArray` from an numpy array."""
storage = pa.array([u8_array_to_rgba(c) for c in array], type=ColorRGBAType.storage_type)
storage = pa.array(u8_array_to_rgba(array), type=ColorRGBAType.storage_type)
# TODO(john) enable extension type wrapper
# return cast(ColorRGBAArray, pa.ExtensionArray.from_storage(ColorRGBAType(), storage))
return storage # type: ignore[no-any-return]
Expand Down