summaryrefslogtreecommitdiff
path: root/utils/tuning/libtuning/modules/lsc/lsc.py
blob: e0ca22ebe9bdfb6a055496aca73b52352db2f99d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (C) 2019, Raspberry Pi Ltd
# Copyright (C) 2022, Paul Elder <paul.elder@ideasonboard.com>

from ..module import Module

import libtuning as lt
import libtuning.utils as utils

import numpy as np


class LSC(Module):
    type = 'lsc'
    hr_name = 'LSC (Base)'
    out_name = 'GenericLSC'

    def __init__(self, *,
                 debug: list,
                 sector_shape: tuple,
                 sector_x_gradient: lt.Gradient,
                 sector_y_gradient: lt.Gradient,
                 sector_average_function: lt.Average,
                 smoothing_function: lt.Smoothing):
        super().__init__()

        self.debug = debug

        self.sector_shape = sector_shape
        self.sector_x_gradient = sector_x_gradient
        self.sector_y_gradient = sector_y_gradient
        self.sector_average_function = sector_average_function

        self.smoothing_function = smoothing_function

    def _enumerate_lsc_images(self, images):
        for image in images:
            if image.lsc_only:
                yield image

    def _get_grid(self, channel, img_w, img_h):
        # List of number of pixels in each sector
        sectors_x = self.sector_x_gradient.distribute(img_w / 2, self.sector_shape[0])
        sectors_y = self.sector_y_gradient.distribute(img_h / 2, self.sector_shape[1])

        grid = []

        r = 0
        for y in sectors_y:
            c = 0
            for x in sectors_x:
                grid.append(self.sector_average_function.average(channel[r:r + y, c:c + x]))
                c += x
            r += y

        return np.array(grid)

    def _lsc_single_channel(self, channel: np.array,
                            image: lt.Image, green_grid: np.array = None):
        grid = self._get_grid(channel, image.w, image.h)
        # Clamp the values to a small positive, so that the following 1/grid
        # doesn't produce negative results.
        grid = np.maximum(grid - image.blacklevel_16, 0.1)

        if green_grid is None:
            table = np.reshape(1 / grid, self.sector_shape[::-1])
        else:
            table = np.reshape(green_grid / grid, self.sector_shape[::-1])
        table = self.smoothing_function.smoothing(table)

        if green_grid is None:
            table = table / np.min(table)

        return table, grid