fix nearest neighbor image scaling

refs #226
This commit is contained in:
Konstantin Käfer
2014-05-30 14:26:26 +02:00
parent 31d9dbefea
commit 3a8b88dbbd

View File

@@ -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);