add image to odd pixelRatio atlas

This commit is contained in:
Konstantin Käfer
2015-07-06 13:13:22 +02:00
parent d08ec53a0e
commit 32699333e6
3 changed files with 27 additions and 14 deletions

View File

@@ -27,7 +27,9 @@ SpriteAtlas::SpriteAtlas(dimension width_, dimension height_, float pixelRatio_,
pixelRatio(pixelRatio_),
store(store_),
bin(width_, height_),
data(std::make_unique<uint32_t[]>(pixelWidth * pixelHeight)),
dirty(true) {
std::fill(data.get(), data.get() + pixelWidth * pixelHeight, 0);
}
Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(const size_t pixel_width, const size_t pixel_height) {
@@ -103,13 +105,6 @@ SpriteAtlasPosition SpriteAtlas::getPosition(const std::string& name, bool repea
};
}
void SpriteAtlas::allocate() {
if (!data) {
data = std::make_unique<uint32_t[]>(pixelWidth * pixelHeight);
std::fill(data.get(), data.get() + pixelWidth * pixelHeight, 0);
}
}
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;
@@ -119,7 +114,6 @@ void SpriteAtlas::copy(const Holder& holder, const bool wrap) {
const int offset = 1;
allocate();
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),
@@ -222,7 +216,6 @@ void SpriteAtlas::bind(bool linear) {
if (dirty) {
std::lock_guard<std::recursive_mutex> lock(mtx);
allocate();
if (fullUploadRequired) {
MBGL_CHECK_ERROR(glTexImage2D(

View File

@@ -82,7 +82,6 @@ private:
using Key = std::pair<std::string, bool>;
void allocate();
Rect<SpriteAtlas::dimension> allocateImage(size_t width, size_t height);
void copy(const Holder& holder, const bool wrap);
@@ -91,7 +90,7 @@ private:
BinPack<dimension> bin;
std::map<Key, Holder> images;
std::set<std::string> uninitialized;
std::unique_ptr<uint32_t[]> data;
const std::unique_ptr<uint32_t[]> data;
std::atomic<bool> dirty;
bool fullUploadRequired = true;
uint32_t texture = 0;

View File

@@ -40,7 +40,7 @@ TEST(Annotations, SpriteAtlas) {
EXPECT_EQ(112, atlas.getTextureHeight());
// Image hasn't been created yet.
EXPECT_FALSE(atlas.getData());
EXPECT_TRUE(atlas.getData());
auto metro = atlas.getImage("metro", false);
EXPECT_EQ(0, metro.pos.x);
@@ -72,8 +72,6 @@ TEST(Annotations, SpriteAtlas) {
EXPECT_EQ(18, metro2.pos.originalW);
EXPECT_EQ(18, metro2.pos.originalH);
EXPECT_TRUE(atlas.getData());
const size_t bytes = atlas.getTextureWidth() * atlas.getTextureHeight() * 4;
const auto hash = crc64(reinterpret_cast<const char*>(atlas.getData()), bytes);
EXPECT_EQ(0x9875fc0595489a9fu, hash) << std::hex << hash;
@@ -85,6 +83,8 @@ TEST(Annotations, SpriteAtlas) {
TEST(Annotations, SpriteAtlasSize) {
SpriteStore store;
store.setSprites(parseSprite(util::read_file("test/fixtures/annotations/emerald.png"),
util::read_file("test/fixtures/annotations/emerald.json")));
SpriteAtlas atlas(63, 112, 1.4, store);
EXPECT_DOUBLE_EQ(1.4f, atlas.getPixelRatio());
@@ -92,4 +92,25 @@ TEST(Annotations, SpriteAtlasSize) {
EXPECT_EQ(112, atlas.getHeight());
EXPECT_EQ(89, atlas.getTextureWidth());
EXPECT_EQ(157, atlas.getTextureHeight());
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(18, metro.texture->width);
EXPECT_EQ(18, metro.texture->height);
EXPECT_EQ(18, metro.texture->pixelWidth);
EXPECT_EQ(18, metro.texture->pixelHeight);
EXPECT_EQ(1.0f, metro.texture->pixelRatio);
const size_t bytes = atlas.getTextureWidth() * atlas.getTextureHeight() * 4;
const auto hash = crc64(reinterpret_cast<const char*>(atlas.getData()), bytes);
EXPECT_EQ(0x2cdda7dbb04d116du, hash) << std::hex << hash;
// util::write_file(
// "test/fixtures/annotations/atlas2.png",
// util::compress_png(atlas.getTextureWidth(), atlas.getTextureHeight(), atlas.getData()));
}