"""Utilities for SAM mask post-processing."""

import numpy as np


def _resize_nearest(arr: np.ndarray, target_h: int, target_w: int) -> np.ndarray:
    """Nearest-neighbor resize using numpy (no PIL/cv2 dependency)."""
    src_h, src_w = arr.shape[:2]  # pragma: no mutate (2D input, slice equiv)
    row_idx = (np.arange(target_h) * src_h / target_h).astype(int)
    col_idx = (np.arange(target_w) * src_w / target_w).astype(int)
    np.clip(
        row_idx, 0, src_h - 1, out=row_idx
    )  # pragma: no mutate (indices never negative or >= src_h)
    np.clip(
        col_idx, 0, src_w - 1, out=col_idx
    )  # pragma: no mutate (indices never negative or >= src_w)
    return arr[np.ix_(row_idx, col_idx)]
