Add integer resample

This commit is contained in:
2025-02-25 17:41:29 +01:00
parent e4b9fefa5a
commit 5249b277ec
4 changed files with 105 additions and 2 deletions
+59 -2
View File
@@ -9,10 +9,9 @@
using F16 = qfloat16;
#else
#include <algorithm>
using F16 = uint16_t;
using F16 = _Float16;
#endif
int THUMB_SIZE = 128;
int THUMB_SIZE_BORDER = 138;
int THUMB_SIZE_BORDER_Y = 158;
@@ -755,6 +754,64 @@ void RawImage::resize(uint32_t w, uint32_t h)
}
}
template<typename T, typename U>
void integerResample(uint32_t w, uint32_t h, uint32_t ch, uint32_t oldw, uint32_t down, bool avg, const uint8_t *in_, uint8_t *out_)
{
const T *in = reinterpret_cast<const T*>(in_);
T *out = reinterpret_cast<T*>(out_);
uint32_t down2 = down * down;
U m = std::numeric_limits<T>::max();
if constexpr(std::is_floating_point_v<T>)m = down2;
for(uint64_t i = 0; i < h; i++)
{
for(uint64_t o = 0; o < w; o++)
{
for(uint64_t p = 0; p < ch; p++)
{
U pix = 0;
for(uint32_t y = 0; y < down; y++)
for(uint32_t x = 0; x < down; x++)
pix += in[((i * down) + y) * oldw * ch + ((o * down) + x) * ch + p];
if (avg)
out[(i * w + o) * ch + p] = pix / down2;
else
out[(i * w + o) * ch + p] = std::min(pix, m);
}
}
}
}
void RawImage::resizeInt(int downsample, bool avg)
{
uint32_t oldw = m_width;
std::unique_ptr<PixelType[]> old_pixels = std::move(m_pixels);
allocate(m_width / downsample, m_height / downsample, m_channels, m_type);
switch(m_type)
{
case RawImage::UINT8:
integerResample<uint8_t, uint32_t>(m_width, m_height, m_ch, oldw, downsample, avg, old_pixels.get(), m_pixels.get());
break;
case RawImage::UINT16:
integerResample<uint16_t, uint32_t>(m_width, m_height, m_ch, oldw, downsample, avg, old_pixels.get(), m_pixels.get());
break;
case RawImage::UINT32:
integerResample<uint32_t, uint64_t>(m_width, m_height, m_ch, oldw, downsample, avg, old_pixels.get(), m_pixels.get());
break;
case RawImage::FLOAT32:
integerResample<float, double>(m_width, m_height, m_ch, oldw, downsample, avg, old_pixels.get(), m_pixels.get());
break;
case RawImage::FLOAT64:
integerResample<double, double>(m_width, m_height, m_ch, oldw, downsample, avg, old_pixels.get(), m_pixels.get());
break;
default:
break;
}
}
std::pair<float, float> RawImage::unitScale() const
{
float min = *std::min_element(m_stats.m_min, m_stats.m_min + 4);