#ifndef STACKER_COMPRESSION_TPP
#define STACKER_COMPRESSION_TPP

template <typename T>
std::shared_ptr<ImageBuffer<T>> Stacker<T>::applyCompression(const std::shared_ptr<ImageBuffer<T>> &frame, float percentage)
{
    // Create a copy of the frame
    auto result = frame->clone();

    // Apply compression
    float exponent = 0.1f + (0.9f - percentage * 0.009f);
    float maxValue = std::is_same<T, uint8_t>::value ? 255.0f : 65535.0f;
    float cnt = std::pow(maxValue, exponent);

    for (int y = 0; y < result->getHeight(); ++y)
    {
        for (int x = 0; x < result->getWidth(); ++x)
        {
            for (int c = 0; c < result->getChannels(); ++c)
            {
                float value = result->getPixel(x, y, c);
                float newValue = std::pow(value, exponent) * maxValue / cnt;
                newValue = std::max(0.0f, std::min(maxValue, newValue));
                result->setPixel(x, y, static_cast<T>(newValue), c);
            }
        }
    }

    return result;
}

#endif // STACKER_COMPRESSION_TPP
