summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-05-30 14:26:26 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-05-30 14:26:26 +0200
commit3a8b88dbbde2b1361b19dc5e4cc1c3edb592eea1 (patch)
treedacd0b5db5c09177bf18a87e220097454f4de60b /src
parent31d9dbefeaad90eaece85c013d355c624018db7d (diff)
downloadqtlocation-mapboxgl-3a8b88dbbde2b1361b19dc5e4cc1c3edb592eea1.tar.gz
fix nearest neighbor image scaling
refs #226
Diffstat (limited to 'src')
-rw-r--r--src/geometry/sprite_atlas.cpp19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/geometry/sprite_atlas.cpp b/src/geometry/sprite_atlas.cpp
index 47f7832bcf..1a077506ab 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<dimension>(width * newRatio);
- dimension h = static_cast<dimension>(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<uint32_t *>(data);
- uint32_t *img_old = reinterpret_cast<uint32_t *>(old_data);
- for (size_t i = 0, length = w * h; i < length; i++) {
- img_new[i] = img_old[static_cast<size_t>(s * i)];
+ const uint32_t *img_old = reinterpret_cast<const uint32_t *>(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);