#ifndef STACKER_PURE_BACKGROUND_SUBTRACTION_TPP
#define STACKER_PURE_BACKGROUND_SUBTRACTION_TPP

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

// Pure background subtraction function
template <typename T>
cv::Mat Stacker<T>::pureBackgroundSubtraction(const cv::Mat &image, const cv::Mat &backgroundModel)
{
    // Convert both images to the same type (float) for processing
    cv::Mat floatImage, floatBgModel;
    image.convertTo(floatImage, CV_32F);
    backgroundModel.convertTo(floatBgModel, CV_32F);

    // Make sure both images have the same number of channels
    if (floatImage.channels() != floatBgModel.channels())
    {
        if (floatImage.channels() == 1 && floatBgModel.channels() == 3)
        {
            // Convert grayscale image to 3-channel
            cv::cvtColor(floatImage, floatImage, cv::COLOR_GRAY2BGR);
        }
        else if (floatImage.channels() == 3 && floatBgModel.channels() == 1)
        {
            // Convert grayscale background to 3-channel
            cv::cvtColor(floatBgModel, floatBgModel, cv::COLOR_GRAY2BGR);
        }
    }

    // Make sure both images have the same size
    if (floatImage.size() != floatBgModel.size())
    {
        cv::resize(floatBgModel, floatBgModel, floatImage.size());
    }

    // Pure subtraction without scaling factors
    cv::Mat result = floatImage - floatBgModel;

    // Ensure no negative values
    cv::threshold(result, result, 0, 0, cv::THRESH_TOZERO);

    // Convert back to original type
    cv::Mat output;
    if (image.type() == CV_8UC1 || image.type() == CV_8UC3)
    {
        result.convertTo(output, image.type());
    }
    else if (image.type() == CV_16UC1 || image.type() == CV_16UC3)
    {
        result.convertTo(output, image.type());
    }
    else
    {
        output = result;
    }

    return output;
}

#endif // STACKER_PURE_BACKGROUND_SUBTRACTION_TPP
