# Copyright 2020 The Lucent Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from __future__ import absolute_import, division, print_function

from typing import Callable, Optional

import numpy as np
import torch


color_correlation_svd_sqrt = np.asarray([[0.26, 0.09, 0.02],
                                         [0.27, 0.00, -0.05],
                                         [0.27, -0.09, 0.03]]).astype("float32")

max_norm_svd_sqrt = np.max(np.linalg.norm(color_correlation_svd_sqrt, axis=0))

color_correlation_normalized = color_correlation_svd_sqrt / max_norm_svd_sqrt

color_mean = [0.48, 0.46, 0.41]


def _linear_decorrelate_color(tensor: torch.Tensor) -> torch.Tensor:
    """Decorrelates color channels.

    :param tensor: Input tensor
    :type tensor: torch.Tensor
    :return: color-decorrelated tensor
    :rtype: torch.Tensor
    """
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    t_permute = tensor.permute(0, 2, 3, 1)
    t_permute = torch.matmul(t_permute, torch.tensor(color_correlation_normalized.T).to(device))
    tensor = t_permute.permute(0, 3, 1, 2)
    return tensor


def to_valid_rgb(
    image_f: Callable, 
    decorrelate: Optional[bool] = False
) -> Callable:
    """Converts image function to one with valid rgb values in [0,1] via sigmoid and potentially decorrelates the color channels

    :param image_f: Original image function
    :type image_f: Callable
    :param decorrelate: Whether to decorrelate colors, defaults to False
    :type decorrelate: Optional[bool], optional
    :return: New image function
    :rtype: Callable
    """
    def inner():
        image = image_f()
        if decorrelate:
            image = _linear_decorrelate_color(image)
        return torch.sigmoid(image)
    return inner

# Copyright 2020 The Lucent Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from __future__ import absolute_import, division, print_function

from typing import Callable, Optional

import numpy as np
import torch


color_correlation_svd_sqrt = np.asarray([[0.26, 0.09, 0.02],
                                         [0.27, 0.00, -0.05],
                                         [0.27, -0.09, 0.03]]).astype("float32")

max_norm_svd_sqrt = np.max(np.linalg.norm(color_correlation_svd_sqrt, axis=0))

color_correlation_normalized = color_correlation_svd_sqrt / max_norm_svd_sqrt

color_mean = [0.48, 0.46, 0.41]


def _linear_decorrelate_color(tensor: torch.Tensor) -> torch.Tensor:
    """Decorrelates color channels.

    :param tensor: Input tensor
    :type tensor: torch.Tensor
    :return: color-decorrelated tensor
    :rtype: torch.Tensor
    """
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    t_permute = tensor.permute(0, 2, 3, 1)
    t_permute = torch.matmul(t_permute, torch.tensor(color_correlation_normalized.T).to(device))
    tensor = t_permute.permute(0, 3, 1, 2)
    return tensor


def to_valid_rgb(
    image_f: Callable, 
    decorrelate: Optional[bool] = False
) -> Callable:
    """Converts image function to one with valid rgb values in [0,1] via sigmoid and potentially decorrelates the color channels

    :param image_f: Original image function
    :type image_f: Callable
    :param decorrelate: Whether to decorrelate colors, defaults to False
    :type decorrelate: Optional[bool], optional
    :return: New image function
    :rtype: Callable
    """
    def inner():
        image = image_f()
        if decorrelate:
            image = _linear_decorrelate_color(image)
        return torch.sigmoid(image)
    return inner

# Copyright 2020 The Lucent Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== Decorrelates color channels.      :param tensor: Input tensor     :type tensor: torch.Tensor     :return: color-decorrelated tensor     :rtype: torch.Tensor Converts image function to one with valid rgb values in [0,1] via sigmoid and potentially decorrelates the color channels      :param image_f: Original image function     :type image_f: Callable     :param decorrelate: Whether to decorrelate colors, defaults to False     :type decorrelate: Optional[bool], optional     :return: New image function     :rtype: Callable