From 428f9c360a86cdb2d4921495179f3fc5fdb8c523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Mon, 15 Jan 2024 08:58:14 +0100 Subject: [PATCH] Small optimization in resample --- rawimage.cpp | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/rawimage.cpp b/rawimage.cpp index 1eee356..3234319 100644 --- a/rawimage.cpp +++ b/rawimage.cpp @@ -575,43 +575,40 @@ void boxResample(uint32_t w, uint32_t h, uint32_t ch, uint32_t oldw, uint32_t ol if constexpr(std::is_same::value) max = UINT16_MAX; - float sx = (float)oldw / w; - float sy = (float)oldh / h; - float sx_ = (float)w / oldw; - float sy_ = (float)h / oldh; - float s = 1.0 / (sx * sy); - for(uint32_t y = 0; y < h; y++) + float sx = (float)w / oldw; + float sy = (float)h / oldh; + for(uint32_t y = 0; y < h; y++)//iterate over destination Y { - for(uint32_t x = 0; x < w; x++) + for(uint32_t x = 0; x < w; x++)//iterate over destination X { float p[4] = {0.0f}; - uint32_t xx = x * oldw / w; + uint32_t xx = x * oldw / w;//calculate source rect uint32_t yy = y * oldh / h; uint32_t xe = std::min((x + 1) * oldw / w, oldw - 1); uint32_t ye = std::min((y + 1) * oldh / h, oldh - 1); - for(uint32_t o = yy; o <= ye; o++) + for(uint32_t o = yy; o <= ye; o++)//iterate over source Y { - float cy = o * sy_ - y; - if(cy < 0.0f)cy = 1.0f + cy * sy; - else if(sy_ + cy > 1.0f)cy = (1.0f - cy) * sy; - else cy = 1.0f; - if(yy==ye)cy = sy; - for(uint32_t i = xx; i <= xe; i++) + float cy = o * sy - y; + if(cy < 0.0f)cy = sy + cy; + else if(sy + cy > 1.0f)cy = 1.0f - cy; + else cy = sy; + if(yy==ye)cy = 1.0f; + for(uint32_t i = xx; i <= xe; i++)//iterate over source X { - float cx = i * sx_ - x; - if(cx < 0.0f)cx = 1.0f + cx * sx; - else if(sx_ + cx > 1.0f)cx = (1.0f - cx) * sx; - else cx = 1.0f; - if(xx==xe)cx = sx; + float cx = i * sx - x; + if(cx < 0.0f)cx = sx + cx; + else if(sx + cx > 1.0f)cx = 1.0f - cx; + else cx = sx; + if(xx==xe)cx = 1.0f; for(uint32_t z = 0; z < ch; z++) p[z] += in[(o * oldw + i) * ch + z] * cy * cx; } } for(uint32_t z = 0; z < ch; z++) if constexpr(std::is_floating_point::value) - out[(y * w + x) * ch + z] = p[z] * s; + out[(y * w + x) * ch + z] = p[z]; else - out[(y * w + x) * ch + z] = std::clamp(std::round(p[z] * s), 0.0f, max); + out[(y * w + x) * ch + z] = std::clamp(std::round(p[z]), 0.0f, max); } } }