# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# 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 typing import Any, Dict, Optional, Tuple, Union

import torch

from kornia.augmentation import random_generator as rg
from kornia.augmentation._2d.intensity.base import IntensityAugmentationBase2D
from kornia.core.utils import _clamp_unit_range, _to_device_nonblocking
from kornia.enhance import posterize


class RandomPosterize(IntensityAugmentationBase2D):
    r"""Posterize images to a random number of bits, applied per sample with probability ``p`` (default 0.5).

    ``output = RandomPosterize(bits, ...)(input)`` reduces the retained bit depth of selected images.

    .. image:: _static/img/RandomPosterize.png

    See the Convention block on :class:`~kornia.augmentation.IntensityAugmentationBase2D`.

    Args:
        p: probability of applying the transformation to each sample.
        bits: number of high bits to keep, in ``[0, 8]``, in which 0 gives a black image and 8 gives the
            original. A range of whole numbers is closed and uniform: every integer from the lower to the upper
            bound is drawn with the same probability. A non-integral argument is accepted; the drawn factor is
            rounded to an integer, half to even, so ``2.5`` gives ``2`` and ``3.5`` gives ``4``.
            If int x, bits will be generated from (x, 8) then convert to int.
            If tuple (x, y), bits will be generated from (x, y) then convert to int.
        same_on_batch: apply the same transformation across the batch.
        keepdim: whether to keep the output shape the same as input (True) or broadcast it
                 to the batch form (False).

    Shape:
        - Input: ``(C, H, W)`` or ``(B, C, H, W)``, Optional: ``(B, 3, 3)``
        - Output: ``(B, C, H, W)``

    Convention:
        - input values are clamped into ``[0, 1]`` before the operation, and the result is clamped
          into the same range. This applies only to samples selected by the probability gate.
        - ``bits=(k, k)`` with ``k < 8`` leaves at most ``2 ** k`` distinct values, so ``0`` gives a constant
          image; the reduction is a ``uint8`` round trip inside :func:`kornia.enhance.posterize`. A sample that
          draws ``8`` returns the clamped input, without the round trip.
        - the round trip is a step function: the output still has ``requires_grad=True``, but its gradient with
          respect to the input is identically ``0`` below ``bits=8``, and follows the input clamp at ``8``.
        - an ``int`` argument is the lower bound of the sampled range ``[x, 8]`` -- the opposite reading
          from :class:`RandomSharpness`, whose scalar argument is an upper bound.

    .. note::
        This function internally uses :func:`kornia.enhance.posterize`.

    See Also:
        - :class:`RandomSolarize`: thresholds values rather than reducing their bit depth.

    Examples:
        >>> rng = torch.manual_seed(0)
        >>> input = torch.rand(1, 1, 5, 5)
        >>> posterize = RandomPosterize(3., p=1.)
        >>> posterize(input)
        tensor([[[[0.4863, 0.7529, 0.0784, 0.1255, 0.2980],
                  [0.6275, 0.4863, 0.8941, 0.4549, 0.6275],
                  [0.3451, 0.3922, 0.0157, 0.1569, 0.2824],
                  [0.5176, 0.6902, 0.8000, 0.1569, 0.2667],
                  [0.6745, 0.9098, 0.3922, 0.8627, 0.4078]]]])

    To apply the exact augmenation again, you may take the advantage of the previous parameter state:
        >>> input = torch.randn(1, 3, 32, 32)
        >>> aug = RandomPosterize(3., p=1.)
        >>> (aug(input) == aug(input, params=aug._params)).all()
        tensor(True)

    """

    def __init__(
        self,
        bits: Union[float, Tuple[float, float], torch.Tensor] = 3,
        same_on_batch: bool = False,
        p: float = 0.5,
        keepdim: bool = False,
    ) -> None:
        super().__init__(p=p, same_on_batch=same_on_batch, keepdim=keepdim)
        # TODO: the generator should receive the device
        self._param_generator = rg.PosterizeGenerator(bits)

    def apply_transform(
        self,
        input: torch.Tensor,
        params: Dict[str, torch.Tensor],
        flags: Dict[str, Any],
        transform: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        input = _clamp_unit_range(input)
        return posterize(input, _to_device_nonblocking(params["bits_factor"], input.device))
