# Copyright 2025 The HuggingFace Inc. team. 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.
"""Image processor class for ViTMatte."""

from typing import Union

import torch
from torchvision.transforms.v2 import functional as tvF

from ...image_processing_backends import TorchvisionBackend
from ...image_processing_utils import BatchFeature
from ...image_transforms import group_images_by_shape, reorder_images
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    get_image_size,
)
from ...processing_utils import ImagesKwargs, Unpack
from ...utils import TensorType, auto_docstring


class VitMatteImageProcessorKwargs(ImagesKwargs, total=False):
    r"""
    size_divisor (`int`, *optional*, defaults to `self.size_divisor`):
        The width and height of the image will be padded to be divisible by this number.
    """

    size_divisor: int


@auto_docstring
class VitMatteImageProcessor(TorchvisionBackend):
    do_rescale = True
    rescale_factor = 1 / 255
    do_normalize = True
    image_mean = IMAGENET_STANDARD_MEAN
    image_std = IMAGENET_STANDARD_STD
    do_pad = True
    size_divisor = 32
    valid_kwargs = VitMatteImageProcessorKwargs

    def __init__(self, **kwargs: Unpack[VitMatteImageProcessorKwargs]) -> None:
        # Backward compatibility
        size_divisibility = kwargs.pop("size_divisibility", None)
        if size_divisibility is not None:
            kwargs.setdefault("size_divisor", size_divisibility)
        super().__init__(**kwargs)

    def _pad_image(
        self,
        images: "torch.Tensor",
        size_divisor: int = 32,
    ) -> "torch.Tensor":
        """
        Pads an image or batched images constantly so that width and height are divisible by size_divisor

        Args:
            images (`torch.Tensor`):
                Image to pad.
            size_divisor (`int`, *optional*, defaults to 32):
                The width and height of the image will be padded to be divisible by this number.
        """
        height, width = get_image_size(images, channel_dim=ChannelDimension.FIRST)

        pad_height = 0 if height % size_divisor == 0 else size_divisor - height % size_divisor
        pad_width = 0 if width % size_divisor == 0 else size_divisor - width % size_divisor

        if pad_width + pad_height > 0:
            padding = (0, 0, pad_width, pad_height)
            images = tvF.pad(images, padding)

        return images

    @auto_docstring
    def preprocess(
        self,
        images: ImageInput,
        trimaps: ImageInput,
        **kwargs: Unpack[VitMatteImageProcessorKwargs],
    ) -> BatchFeature:
        r"""
        trimaps (`ImageInput`):
            The trimaps to preprocess.
        """
        return super().preprocess(images, trimaps, **kwargs)

    def _preprocess_image_like_inputs(
        self,
        images: ImageInput,
        trimaps: ImageInput,
        do_convert_rgb: bool,
        input_data_format: ChannelDimension,
        device: Union[str, "torch.device"] | None = None,
        **kwargs: Unpack[VitMatteImageProcessorKwargs],
    ) -> BatchFeature:
        """
        Preprocess image-like inputs.
        """
        images = self._prepare_image_like_inputs(
            images=images, do_convert_rgb=do_convert_rgb, input_data_format=input_data_format, device=device
        )
        trimaps = self._prepare_image_like_inputs(images=trimaps, expected_ndims=2, device=device)

        return self._preprocess(images, trimaps, **kwargs)

    def _preprocess(
        self,
        images: list["torch.Tensor"],
        trimaps: list["torch.Tensor"],
        do_rescale: bool,
        rescale_factor: float,
        do_normalize: bool,
        image_mean: float | list[float] | None,
        image_std: float | list[float] | None,
        do_pad: bool | None,
        size_divisor: int | None,
        disable_grouping: bool | None,
        return_tensors: str | TensorType | None,
        **kwargs,
    ) -> BatchFeature:
        grouped_images, grouped_images_index = group_images_by_shape(images, disable_grouping=disable_grouping)
        grouped_trimaps, grouped_trimaps_index = group_images_by_shape(trimaps, disable_grouping=disable_grouping)
        processed_images_grouped = {}
        for shape in grouped_images:
            stacked_images = grouped_images[shape]
            stacked_trimaps = grouped_trimaps[shape]
            # Fused rescale and normalize
            stacked_images = self.rescale_and_normalize(
                stacked_images, do_rescale, rescale_factor, do_normalize, image_mean, image_std
            )
            stacked_trimaps = self.rescale_and_normalize(
                stacked_trimaps, do_rescale, rescale_factor, False, image_mean, image_std
            )
            stacked_images = torch.cat([stacked_images, stacked_trimaps], dim=1)
            if do_pad:
                stacked_images = self._pad_image(stacked_images, size_divisor)
            processed_images_grouped[shape] = stacked_images

        processed_images = reorder_images(processed_images_grouped, grouped_images_index)

        return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors)


__all__ = ["VitMatteImageProcessor"]
