Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] remove bilinear, nearest scaling, fix #3164
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Jan 13, 2016
1 parent bdfa127 commit e577354
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 254 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"devDependencies": {
"aws-sdk": "^2.2.21",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#c401ec0acb064f819cddd182c63ee9e019bfb18f",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#c8982f16ef5d39164b2e4f33bc3f993f9ac17502",
"node-gyp": "^3.2.1",
"request": "^2.67.0",
"tape": "^4.2.2"
Expand Down
78 changes: 32 additions & 46 deletions src/mbgl/sprite/sprite_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <mbgl/util/math.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/scaling.hpp>
#include <mbgl/util/thread_context.hpp>

#include <cassert>
Expand Down Expand Up @@ -45,9 +44,6 @@ Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(float src_width, float s
return rect;
}

rect.originalW = pixel_width;
rect.originalH = pixel_height;

return rect;
}

Expand Down Expand Up @@ -101,6 +97,34 @@ mapbox::util::optional<SpriteAtlasPosition> SpriteAtlas::getPosition(const std::
};
}

void copyBitmap(const uint32_t *src, const uint32_t srcStride, const uint32_t srcX, const uint32_t srcY,
uint32_t *const dst, const uint32_t dstStride, const uint32_t dstX, const uint32_t dstY, int dstSize,
const int width, const int height, const bool wrap) {

int srcI = srcY * srcStride + srcX;
int dstI = dstY * dstStride + dstX;
int x, y;

if (wrap) {
// add 1 pixel wrapped padding on each side of the image
dstI -= dstStride;
for (y = -1; y <= height; y++, srcI = ((y + height) % height + srcY) * srcStride + srcX, dstI += dstStride) {
for (x = -1; x <= width; x++) {
const int dstIndex = (dstI + x + dstSize) % dstSize;
dst[dstIndex] = src[srcI + ((x + width) % width)];
}
}

} else {
for (y = 0; y < height; y++, srcI += srcStride, dstI += dstStride) {
for (x = 0; x < width; x++) {
const int dstIndex = (dstI + x + dstSize) % dstSize;
dst[dstIndex] = src[srcI + x];
}
}
}
}

void SpriteAtlas::copy(const Holder& holder, const bool wrap) {
if (!data) {
data = std::make_unique<uint32_t[]>(pixelWidth * pixelHeight);
Expand All @@ -109,51 +133,13 @@ void SpriteAtlas::copy(const Holder& holder, const bool wrap) {

const uint32_t *srcData = reinterpret_cast<const uint32_t *>(holder.texture->data.data());
if (!srcData) return;
const vec2<uint32_t> srcSize { holder.texture->pixelWidth, holder.texture->pixelHeight };
const Rect<uint32_t> srcPos { 0, 0, srcSize.x, srcSize.y };
const auto& dst = holder.pos;

const int offset = 1;

uint32_t *const dstData = data.get();
const vec2<uint32_t> dstSize{ pixelWidth, pixelHeight };
const Rect<uint32_t> dstPos{ static_cast<uint32_t>((offset + dst.x) * pixelRatio),
static_cast<uint32_t>((offset + dst.y) * pixelRatio),
static_cast<uint32_t>(dst.originalW * pixelRatio),
static_cast<uint32_t>(dst.originalH * pixelRatio) };

util::bilinearScale(srcData, srcSize, srcPos, dstData, dstSize, dstPos, wrap);
const int padding = 1;

// Add borders around the copied image if required.
if (wrap) {
// We're copying from the same image so we don't have to scale again.
const uint32_t border = 1;
const uint32_t borderX = dstPos.x != 0 ? border : 0;
const uint32_t borderY = dstPos.y != 0 ? border : 0;

// Left border
util::nearestNeighborScale(
dstData, dstSize, { dstPos.x + dstPos.w - borderX, dstPos.y, borderX, dstPos.h },
dstData, dstSize, { dstPos.x - borderX, dstPos.y, borderX, dstPos.h });

// Right border
util::nearestNeighborScale(dstData, dstSize, { dstPos.x, dstPos.y, border, dstPos.h },
dstData, dstSize,
{ dstPos.x + dstPos.w, dstPos.y, border, dstPos.h });

// Top border
util::nearestNeighborScale(
dstData, dstSize, { dstPos.x - borderX, dstPos.y + dstPos.h - borderY,
dstPos.w + border + borderX, borderY },
dstData, dstSize,
{ dstPos.x - borderX, dstPos.y - borderY, dstPos.w + 2 * borderX, borderY });

// Bottom border
util::nearestNeighborScale(
dstData, dstSize, { dstPos.x - borderX, dstPos.y, dstPos.w + 2 * borderX, border },
dstData, dstSize,
{ dstPos.x - borderX, dstPos.y + dstPos.h, dstPos.w + border + borderX, border });
}
copyBitmap(srcData, holder.texture->pixelWidth, 0, 0,
dstData, pixelWidth, (holder.pos.x + padding) * pixelRatio, (holder.pos.y + padding) * pixelRatio, pixelWidth * pixelHeight,
holder.texture->pixelWidth, holder.texture->pixelHeight, wrap);

dirty = true;
}
Expand Down
1 change: 0 additions & 1 deletion src/mbgl/util/rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct Rect {
inline Rect(T x_, T y_, T w_, T h_) : x(x_), y(y_), w(w_), h(h_) {}
T x = 0, y = 0;
T w = 0, h = 0;
T originalW = 0, originalH = 0;

template <typename Number>
inline Rect operator *(Number value) const {
Expand Down
112 changes: 0 additions & 112 deletions src/mbgl/util/scaling.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/mbgl/util/scaling.hpp

This file was deleted.

54 changes: 0 additions & 54 deletions test/miscellaneous/bilinear.cpp

This file was deleted.

24 changes: 8 additions & 16 deletions test/sprite/sprite_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ TEST(Sprite, SpriteAtlas) {
EXPECT_EQ(0, metro.pos.y);
EXPECT_EQ(20, metro.pos.w);
EXPECT_EQ(20, metro.pos.h);
EXPECT_EQ(18, metro.pos.originalW);
EXPECT_EQ(18, metro.pos.originalH);
EXPECT_EQ(18, metro.texture->width);
EXPECT_EQ(18, metro.texture->height);
EXPECT_EQ(18, metro.texture->pixelWidth);
Expand All @@ -45,12 +43,12 @@ TEST(Sprite, SpriteAtlas) {
EXPECT_TRUE(atlas.getData());

auto pos = *atlas.getPosition("metro", false);
EXPECT_DOUBLE_EQ(20, pos.size[0]);
EXPECT_DOUBLE_EQ(20, pos.size[1]);
EXPECT_DOUBLE_EQ(18, pos.size[0]);
EXPECT_DOUBLE_EQ(18, pos.size[1]);
EXPECT_DOUBLE_EQ(1.0f / 63, pos.tl[0]);
EXPECT_DOUBLE_EQ(1.0f / 112, pos.tl[1]);
EXPECT_DOUBLE_EQ(21.0f / 63, pos.br[0]);
EXPECT_DOUBLE_EQ(21.0f / 112, pos.br[1]);
EXPECT_DOUBLE_EQ(19.0f / 63, pos.br[0]);
EXPECT_DOUBLE_EQ(19.0f / 112, pos.br[1]);

auto missing = atlas.getImage("doesnotexist", false);
EXPECT_FALSE(missing);
Expand All @@ -68,12 +66,10 @@ TEST(Sprite, SpriteAtlas) {
EXPECT_EQ(0, metro2.pos.y);
EXPECT_EQ(20, metro2.pos.w);
EXPECT_EQ(20, metro2.pos.h);
EXPECT_EQ(18, metro2.pos.originalW);
EXPECT_EQ(18, metro2.pos.originalH);

const size_t bytes = atlas.getTextureWidth() * atlas.getTextureHeight() * 4;
const auto hash = test::crc64(reinterpret_cast<const char*>(atlas.getData()), bytes);
EXPECT_EQ(0x9875FC0595489A9Fu, hash) << std::hex << hash;
EXPECT_EQ(11868256915183397177u, hash) << std::hex << hash;

// util::write_file(
// "test/fixtures/annotations/atlas1.png",
Expand All @@ -98,10 +94,8 @@ TEST(Sprite, SpriteAtlasSize) {
auto metro = *atlas.getImage("metro", false);
EXPECT_EQ(0, metro.pos.x);
EXPECT_EQ(0, metro.pos.y);
EXPECT_EQ(20, metro.pos.w);
EXPECT_EQ(20, metro.pos.h);
EXPECT_EQ(18, metro.pos.originalW);
EXPECT_EQ(18, metro.pos.originalH);
EXPECT_EQ(16, metro.pos.w);
EXPECT_EQ(16, metro.pos.h);
EXPECT_EQ(18, metro.texture->width);
EXPECT_EQ(18, metro.texture->height);
EXPECT_EQ(18, metro.texture->pixelWidth);
Expand All @@ -110,7 +104,7 @@ TEST(Sprite, SpriteAtlasSize) {

const size_t bytes = atlas.getTextureWidth() * atlas.getTextureHeight() * 4;
const auto hash = test::crc64(reinterpret_cast<const char*>(atlas.getData()), bytes);
EXPECT_EQ(0x2CDDA7DBB04D116Du, hash) << std::hex << hash;
EXPECT_EQ(18324190582232646342u, hash) << std::hex << hash;

// util::write_file(
// "test/fixtures/annotations/atlas2.png",
Expand All @@ -134,8 +128,6 @@ TEST(Sprite, SpriteAtlasUpdates) {
EXPECT_EQ(0, one.pos.y);
EXPECT_EQ(20, one.pos.w);
EXPECT_EQ(16, one.pos.h);
EXPECT_EQ(16, one.pos.originalW);
EXPECT_EQ(12, one.pos.originalH);
EXPECT_EQ(16, one.texture->width);
EXPECT_EQ(12, one.texture->height);
EXPECT_EQ(16, one.texture->pixelWidth);
Expand Down
1 change: 0 additions & 1 deletion test/test.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
'miscellaneous/async_task.cpp',
'miscellaneous/clip_ids.cpp',
'miscellaneous/binpack.cpp',
'miscellaneous/bilinear.cpp',
'miscellaneous/comparisons.cpp',
'miscellaneous/functions.cpp',
'miscellaneous/geo.cpp',
Expand Down

0 comments on commit e577354

Please sign in to comment.