Verified Commit eeadc20b authored by Bruno Petrus's avatar Bruno Petrus
Browse files

feat(LUT): Optimized applying a LUT

parent 570fb81f
Loading
Loading
Loading
Loading
+5 −15
Original line number Diff line number Diff line
from typing import Dict, List

import numpy as np
from numpy.typing import ArrayLike

LUT = Dict[int, List[int]]
LUT = ArrayLike

LUTS = {
    "Gray": "grayscale.lut",
@@ -17,25 +15,17 @@ def get_lut(name: str) -> LUT:
        return None

    with open("luts/" + LUTS[name]) as lut_file:
        lut = dict()
        lut = [0 for _ in range(256)]
        for line in lut_file:
            parts = [int(x) for x in line.rstrip().split(";")]
            val, map_val = parts[0], parts[1:]
            lut[val] = map_val
    return lut
    return np.array(lut)


def apply_lut(data: ArrayLike, lut: LUT) -> ArrayLike:
    frames, rows, cols = data.shape
    max_val = lut[max(lut.keys())]  # Clamp all values outside the LUT
    result = np.zeros(shape=(frames, rows, cols, 3),
                      dtype=np.uint8)  # 3 channels for RGB
    for f in range(frames):
        for r in range(rows):
            for c in range(cols):
                val = data[f, r, c]
                result[f, r, c] = lut.get(val, max_val)
    return result
    result = lut[data]
    return result.astype(np.uint8)


if __name__ == "__main__":