[core][ios][osx][android] make SpriteImage accept PremultipliedImage
the SpriteImage constructor signature changes from
SpriteImage(
uint16_t width, uint16_t height, float pixelRatio,
std::string&& data, bool sdf = false);
to
SpriteImage(PremultipliedImage&&, float pixelRatio, bool sdf = false)
This commit is contained in:
@@ -165,7 +165,7 @@ void AnnotationManager::removeIcon(const std::string& name) {
|
||||
|
||||
double AnnotationManager::getTopOffsetPixelsForIcon(const std::string& name) {
|
||||
auto sprite = spriteStore.getSprite(name);
|
||||
return sprite ? -sprite->height / 2 : 0;
|
||||
return sprite ? -(sprite->image.height / sprite->pixelRatio) / 2 : 0;
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -248,8 +248,8 @@ void SymbolBucket::addFeatures(uintptr_t tileUID,
|
||||
auto image = spriteAtlas.getImage(feature.sprite, false);
|
||||
if (image) {
|
||||
shapedIcon = shapeIcon(*image, layout);
|
||||
assert((*image).texture);
|
||||
if ((*image).texture->sdf) {
|
||||
assert((*image).spriteImage);
|
||||
if ((*image).spriteImage->sdf) {
|
||||
sdfIcons = true;
|
||||
}
|
||||
if ((*image).relativePixelRatio != 1.0f) {
|
||||
|
||||
@@ -26,10 +26,10 @@ SpriteAtlas::SpriteAtlas(dimension width_, dimension height_, float pixelRatio_,
|
||||
dirty(true) {
|
||||
}
|
||||
|
||||
Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(float src_width, float src_height) {
|
||||
Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(const SpriteImage& spriteImage) {
|
||||
|
||||
const uint16_t pixel_width = std::ceil(src_width / pixelRatio);
|
||||
const uint16_t pixel_height = std::ceil(src_height / pixelRatio);
|
||||
const uint16_t pixel_width = std::ceil(spriteImage.image.width / pixelRatio);
|
||||
const uint16_t pixel_height = std::ceil(spriteImage.image.height / pixelRatio);
|
||||
|
||||
// Increase to next number divisible by 4, but at least 1.
|
||||
// This is so we can scale down the texture coordinates and pack them
|
||||
@@ -52,7 +52,7 @@ optional<SpriteAtlasElement> SpriteAtlas::getImage(const std::string& name, cons
|
||||
|
||||
auto rect_it = images.find({ name, wrap });
|
||||
if (rect_it != images.end()) {
|
||||
return SpriteAtlasElement { rect_it->second.pos, rect_it->second.texture, rect_it->second.texture->pixelRatio / pixelRatio };
|
||||
return SpriteAtlasElement { rect_it->second.pos, rect_it->second.spriteImage, rect_it->second.spriteImage->pixelRatio / pixelRatio };
|
||||
}
|
||||
|
||||
auto sprite = store.getSprite(name);
|
||||
@@ -60,7 +60,7 @@ optional<SpriteAtlasElement> SpriteAtlas::getImage(const std::string& name, cons
|
||||
return {};
|
||||
}
|
||||
|
||||
Rect<dimension> rect = allocateImage(sprite->width * sprite->pixelRatio, sprite->height * sprite->pixelRatio);
|
||||
Rect<dimension> rect = allocateImage(*sprite);
|
||||
if (rect.w == 0) {
|
||||
if (debug::spriteWarnings) {
|
||||
Log::Warning(Event::Sprite, "sprite atlas bitmap overflow");
|
||||
@@ -85,13 +85,13 @@ optional<SpriteAtlasPosition> SpriteAtlas::getPosition(const std::string& name,
|
||||
auto rect = (*img).pos;
|
||||
|
||||
const float padding = 1;
|
||||
auto image = (*img).texture;
|
||||
auto spriteImage = (*img).spriteImage;
|
||||
|
||||
const float w = image->width * (*img).relativePixelRatio;
|
||||
const float h = image->height * (*img).relativePixelRatio;
|
||||
const float w = spriteImage->getWidth() * (*img).relativePixelRatio;
|
||||
const float h = spriteImage->getHeight() * (*img).relativePixelRatio;
|
||||
|
||||
return SpriteAtlasPosition {
|
||||
{{ float(image->width), float(image->height) }},
|
||||
{{ float(spriteImage->getWidth()), spriteImage->getHeight() }},
|
||||
{{ float(rect.x + padding) / width, float(rect.y + padding) / height }},
|
||||
{{ float(rect.x + padding + w) / width, float(rect.y + padding + h) / height }}
|
||||
};
|
||||
@@ -131,15 +131,15 @@ void SpriteAtlas::copy(const Holder& holder, const bool wrap) {
|
||||
std::fill(data.get(), data.get() + pixelWidth * pixelHeight, 0);
|
||||
}
|
||||
|
||||
const uint32_t *srcData = reinterpret_cast<const uint32_t *>(holder.texture->data.data());
|
||||
const uint32_t *srcData = reinterpret_cast<const uint32_t *>(holder.spriteImage->image.data.get());
|
||||
if (!srcData) return;
|
||||
uint32_t *const dstData = data.get();
|
||||
|
||||
const int padding = 1;
|
||||
|
||||
copyBitmap(srcData, holder.texture->pixelWidth, 0, 0,
|
||||
copyBitmap(srcData, uint32_t(holder.spriteImage->image.width), 0, 0,
|
||||
dstData, pixelWidth, (holder.pos.x + padding) * pixelRatio, (holder.pos.y + padding) * pixelRatio, pixelWidth * pixelHeight,
|
||||
holder.texture->pixelWidth, holder.texture->pixelHeight, wrap);
|
||||
uint32_t(holder.spriteImage->image.width), uint32_t(holder.spriteImage->image.height), wrap);
|
||||
|
||||
dirty = true;
|
||||
}
|
||||
@@ -168,8 +168,8 @@ void SpriteAtlas::updateDirty() {
|
||||
} else {
|
||||
// The two names match;
|
||||
Holder& holder = imageIterator->second;
|
||||
holder.texture = spriteIterator->second;
|
||||
if (holder.texture != nullptr) {
|
||||
holder.spriteImage = spriteIterator->second;
|
||||
if (holder.spriteImage != nullptr) {
|
||||
copy(holder, imageIterator->first.second);
|
||||
++imageIterator;
|
||||
} else {
|
||||
@@ -255,10 +255,10 @@ SpriteAtlas::~SpriteAtlas() {
|
||||
}
|
||||
}
|
||||
|
||||
SpriteAtlas::Holder::Holder(const std::shared_ptr<const SpriteImage>& texture_,
|
||||
SpriteAtlas::Holder::Holder(const std::shared_ptr<const SpriteImage>& spriteImage_,
|
||||
const Rect<dimension>& pos_)
|
||||
: texture(texture_), pos(pos_) {
|
||||
: spriteImage(spriteImage_), pos(pos_) {
|
||||
}
|
||||
|
||||
SpriteAtlas::Holder::Holder(Holder&& h) : texture(std::move(h.texture)), pos(h.pos) {
|
||||
SpriteAtlas::Holder::Holder(Holder&& h) : spriteImage(std::move(h.spriteImage)), pos(h.pos) {
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ struct SpriteAtlasPosition {
|
||||
|
||||
struct SpriteAtlasElement {
|
||||
Rect<uint16_t> pos;
|
||||
std::shared_ptr<const SpriteImage> texture;
|
||||
std::shared_ptr<const SpriteImage> spriteImage;
|
||||
float relativePixelRatio;
|
||||
};
|
||||
|
||||
@@ -77,13 +77,13 @@ private:
|
||||
struct Holder : private util::noncopyable {
|
||||
inline Holder(const std::shared_ptr<const SpriteImage>&, const Rect<dimension>&);
|
||||
inline Holder(Holder&&);
|
||||
std::shared_ptr<const SpriteImage> texture;
|
||||
std::shared_ptr<const SpriteImage> spriteImage;
|
||||
const Rect<dimension> pos;
|
||||
};
|
||||
|
||||
using Key = std::pair<std::string, bool>;
|
||||
|
||||
Rect<SpriteAtlas::dimension> allocateImage(float width, float height);
|
||||
Rect<SpriteAtlas::dimension> allocateImage(const SpriteImage&);
|
||||
void copy(const Holder& holder, const bool wrap);
|
||||
|
||||
std::recursive_mutex mtx;
|
||||
|
||||
@@ -6,23 +6,17 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
SpriteImage::SpriteImage(const uint16_t pixelWidth_,
|
||||
const uint16_t pixelHeight_,
|
||||
SpriteImage::SpriteImage(PremultipliedImage&& image_,
|
||||
const float pixelRatio_,
|
||||
std::string&& data_,
|
||||
bool sdf_)
|
||||
: width(std::ceil(pixelWidth_ / pixelRatio_)),
|
||||
height(std::ceil(pixelHeight_ / pixelRatio_)),
|
||||
: image(std::move(image_)),
|
||||
pixelRatio(pixelRatio_),
|
||||
pixelWidth(pixelWidth_),
|
||||
pixelHeight(pixelHeight_),
|
||||
data(std::move(data_)),
|
||||
sdf(sdf_) {
|
||||
const size_t size = pixelWidth * pixelHeight * 4;
|
||||
if (size == 0) {
|
||||
|
||||
if (image.size() == 0) {
|
||||
throw util::SpriteImageException("Sprite image dimensions may not be zero");
|
||||
} else if (size != data.size()) {
|
||||
throw util::SpriteImageException("Sprite image pixel count mismatch");
|
||||
} else if (pixelRatio <= 0) {
|
||||
throw util::SpriteImageException("Sprite pixelRatio may not be <= 0");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ SpriteImagePtr createSpriteImage(const PremultipliedImage& image,
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string data(width * height * 4, '\0');
|
||||
PremultipliedImage dstImage(width, height);
|
||||
|
||||
auto srcData = reinterpret_cast<const uint32_t*>(image.data.get());
|
||||
auto dstData = reinterpret_cast<uint32_t*>(const_cast<char*>(data.data()));
|
||||
auto dstData = reinterpret_cast<uint32_t*>(dstImage.data.get());
|
||||
|
||||
const int32_t maxX = std::min(uint32_t(image.width), uint32_t(width + srcX)) - srcX;
|
||||
assert(maxX <= int32_t(image.width));
|
||||
@@ -45,7 +45,7 @@ SpriteImagePtr createSpriteImage(const PremultipliedImage& image,
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_unique<const SpriteImage>(width, height, ratio, std::move(data), sdf);
|
||||
return std::make_unique<const SpriteImage>(std::move(dstImage), ratio, sdf);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -113,7 +113,7 @@ void SpriteStore::_setSprite(const std::string& name,
|
||||
auto it = sprites.find(name);
|
||||
if (it != sprites.end()) {
|
||||
// There is already a sprite with that name in our store.
|
||||
if ((it->second->width != sprite->width || it->second->height != sprite->height)) {
|
||||
if ((it->second->image.width != sprite->image.width || it->second->image.height != sprite->image.height)) {
|
||||
Log::Warning(Event::Sprite, "Can't change sprite dimensions for '%s'", name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@ namespace mbgl {
|
||||
PositionedIcon shapeIcon(const SpriteAtlasElement& image, const SymbolLayoutProperties& layout) {
|
||||
float dx = layout.icon.offset.value[0];
|
||||
float dy = layout.icon.offset.value[1];
|
||||
float x1 = dx - image.texture->width / 2.0f;
|
||||
float x2 = x1 + image.texture->width;
|
||||
float y1 = dy - image.texture->height / 2.0f;
|
||||
float y2 = y1 + image.texture->height;
|
||||
float x1 = dx - image.spriteImage->getWidth() / 2.0f;
|
||||
float x2 = x1 + image.spriteImage->getWidth();
|
||||
float y1 = dy - image.spriteImage->getHeight() / 2.0f;
|
||||
float y2 = y1 + image.spriteImage->getHeight();
|
||||
|
||||
return PositionedIcon(image, y1, y2, x1, x2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user