From 3a8b88dbbde2b1361b19dc5e4cc1c3edb592eea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 30 May 2014 14:26:26 +0200 Subject: [PATCH] fix nearest neighbor image scaling refs #226 --- src/geometry/sprite_atlas.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/geometry/sprite_atlas.cpp b/src/geometry/sprite_atlas.cpp index 47f7832bc..1a077506a 100644 --- a/src/geometry/sprite_atlas.cpp +++ b/src/geometry/sprite_atlas.cpp @@ -35,15 +35,22 @@ bool SpriteAtlas::resize(const float newRatio) { data = nullptr; allocate(); - dimension w = static_cast(width * newRatio); - dimension h = static_cast(height * newRatio); - float s = std::pow(oldRatio / newRatio, 2); + const int old_w = width * oldRatio; + const int old_h = height * oldRatio; + const int new_w = width * newRatio; + const int new_h = height * newRatio; // Basic image scaling. TODO: Replace this with better image scaling. uint32_t *img_new = reinterpret_cast(data); - uint32_t *img_old = reinterpret_cast(old_data); - for (size_t i = 0, length = w * h; i < length; i++) { - img_new[i] = img_old[static_cast(s * i)]; + const uint32_t *img_old = reinterpret_cast(old_data); + + for (int y = 0; y < new_h; y++) { + const int old_yoffset = ((y * old_h) / new_h) * old_w; + const int new_yoffset = y * new_w; + for (int x = 0; x < new_w; x++) { + const int old_x = (x * old_w) / new_w; + img_new[new_yoffset + x] = img_old[old_yoffset + old_x]; + } } free(old_data);