[core, node, darwin, android, qt] Make image ID part of Image
More like Source and Layer.
This commit is contained in:
@@ -25,7 +25,7 @@ public:
|
||||
|
||||
map.setStyleJSON(util::read_file("benchmark/fixtures/api/query_style.json"));
|
||||
map.setLatLngZoom({ 40.726989, -73.992857 }, 15); // Manhattan
|
||||
map.addImage("test-icon", std::make_unique<style::Image>(
|
||||
map.addImage(std::make_unique<style::Image>("test-icon",
|
||||
decodeImage(util::read_file("benchmark/fixtures/api/default_marker.png")), 1.0));
|
||||
|
||||
mbgl::benchmark::render(map, view);
|
||||
|
||||
@@ -148,7 +148,7 @@ public:
|
||||
LatLng latLngForPixel(const ScreenCoordinate&) const;
|
||||
|
||||
// Annotations
|
||||
void addAnnotationImage(const std::string&, std::unique_ptr<style::Image>);
|
||||
void addAnnotationImage(std::unique_ptr<style::Image>);
|
||||
void removeAnnotationImage(const std::string&);
|
||||
double getTopOffsetPixelsForAnnotationImage(const std::string&);
|
||||
|
||||
@@ -169,7 +169,7 @@ public:
|
||||
std::unique_ptr<style::Layer> removeLayer(const std::string& layerID);
|
||||
|
||||
// Images
|
||||
void addImage(const std::string&, std::unique_ptr<style::Image>);
|
||||
void addImage(std::unique_ptr<style::Image>);
|
||||
void removeImage(const std::string&);
|
||||
const style::Image* getImage(const std::string&);
|
||||
|
||||
|
||||
@@ -3,12 +3,16 @@
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/immutable.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
class Image {
|
||||
public:
|
||||
Image(PremultipliedImage&&, float pixelRatio, bool sdf = false);
|
||||
Image(std::string id, PremultipliedImage&&, float pixelRatio, bool sdf = false);
|
||||
|
||||
std::string getID() const;
|
||||
|
||||
const PremultipliedImage& getImage() const;
|
||||
|
||||
|
||||
@@ -735,8 +735,8 @@ void NativeMapView::addAnnotationIcon(JNIEnv& env, jni::String symbol, jint w, j
|
||||
}
|
||||
|
||||
jni::GetArrayRegion(env, *jpixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
|
||||
map->addAnnotationImage(symbolName,
|
||||
std::make_unique<mbgl::style::Image>(std::move(premultipliedImage), float(scale)));
|
||||
map->addAnnotationImage(std::make_unique<mbgl::style::Image>(
|
||||
symbolName, std::move(premultipliedImage), float(scale)));
|
||||
}
|
||||
|
||||
jdouble NativeMapView::getTopOffsetPixelsForAnnotationSymbol(JNIEnv& env, jni::String symbolName) {
|
||||
@@ -1037,8 +1037,10 @@ void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::jint w, jni::ji
|
||||
|
||||
jni::GetArrayRegion(env, *pixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
|
||||
|
||||
map->addImage(jni::Make<std::string>(env, name),
|
||||
std::make_unique<mbgl::style::Image>(std::move(premultipliedImage), float(scale)));
|
||||
map->addImage(std::make_unique<mbgl::style::Image>(
|
||||
jni::Make<std::string>(env, name),
|
||||
std::move(premultipliedImage),
|
||||
float(scale)));
|
||||
}
|
||||
|
||||
void NativeMapView::removeImage(JNIEnv& env, jni::String name) {
|
||||
|
||||
@@ -510,7 +510,7 @@ static NSURL *MGLStyleURL_emerald;
|
||||
format:@"Cannot assign image %@ to a nil name.", image];
|
||||
}
|
||||
|
||||
self.mapView.mbglMap->addImage([name UTF8String], image.mgl_styleImage);
|
||||
self.mapView.mbglMap->addImage([image mgl_styleImageWithIdentifier:name]);
|
||||
}
|
||||
|
||||
- (void)removeImageForName:(NSString *)name
|
||||
|
||||
@@ -131,7 +131,7 @@ GLFWView::~GLFWView() {
|
||||
|
||||
void GLFWView::setMap(mbgl::Map *map_) {
|
||||
map = map_;
|
||||
map->addAnnotationImage("default_marker", makeImage(22, 22, 1));
|
||||
map->addAnnotationImage(makeImage("default_marker", 22, 22, 1));
|
||||
}
|
||||
|
||||
void GLFWView::updateAssumedState() {
|
||||
@@ -255,7 +255,7 @@ mbgl::Point<double> GLFWView::makeRandomPoint() const {
|
||||
}
|
||||
|
||||
std::unique_ptr<mbgl::style::Image>
|
||||
GLFWView::makeImage(int width, int height, float pixelRatio) {
|
||||
GLFWView::makeImage(const std::string& id, int width, int height, float pixelRatio) {
|
||||
const int r = 255 * (double(std::rand()) / RAND_MAX);
|
||||
const int g = 255 * (double(std::rand()) / RAND_MAX);
|
||||
const int b = 255 * (double(std::rand()) / RAND_MAX);
|
||||
@@ -280,7 +280,7 @@ GLFWView::makeImage(int width, int height, float pixelRatio) {
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_unique<mbgl::style::Image>(std::move(image), pixelRatio);
|
||||
return std::make_unique<mbgl::style::Image>(id, std::move(image), pixelRatio);
|
||||
}
|
||||
|
||||
void GLFWView::nextOrientation() {
|
||||
@@ -297,7 +297,7 @@ void GLFWView::addRandomCustomPointAnnotations(int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
static int spriteID = 1;
|
||||
const auto name = std::string{ "marker-" } + mbgl::util::toString(spriteID++);
|
||||
map->addAnnotationImage(name, makeImage(22, 22, 1));
|
||||
map->addAnnotationImage(makeImage(name, 22, 22, 1));
|
||||
spriteIDs.push_back(name);
|
||||
annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation { makeRandomPoint(), name }));
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ private:
|
||||
|
||||
mbgl::Color makeRandomColor() const;
|
||||
mbgl::Point<double> makeRandomPoint() const;
|
||||
static std::unique_ptr<mbgl::style::Image> makeImage(int width, int height, float pixelRatio);
|
||||
static std::unique_ptr<mbgl::style::Image> makeImage(const std::string& id, int width, int height, float pixelRatio);
|
||||
|
||||
void nextOrientation();
|
||||
|
||||
|
||||
@@ -3449,7 +3449,7 @@ public:
|
||||
annotationImage.delegate = self;
|
||||
|
||||
// add sprite
|
||||
_mbglMap->addAnnotationImage(iconIdentifier.UTF8String, annotationImage.image.mgl_styleImage);
|
||||
_mbglMap->addAnnotationImage([annotationImage.image mgl_styleImageWithIdentifier:iconIdentifier]);
|
||||
|
||||
// Create a slop area with a “radius” equal in size to the annotation
|
||||
// image’s alignment rect, allowing the eventual tap to be on any point
|
||||
|
||||
@@ -8,7 +8,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)styleImage;
|
||||
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage;
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -22,9 +22,10 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage {
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier {
|
||||
BOOL isTemplate = self.renderingMode == UIImageRenderingModeAlwaysTemplate;
|
||||
return std::make_unique<mbgl::style::Image>(MGLPremultipliedImageFromCGImage(self.CGImage),
|
||||
return std::make_unique<mbgl::style::Image>([identifier UTF8String],
|
||||
MGLPremultipliedImageFromCGImage(self.CGImage),
|
||||
float(self.scale), isTemplate);
|
||||
}
|
||||
|
||||
|
||||
@@ -1949,7 +1949,7 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
_mbglMap->addAnnotationImage(iconIdentifier.UTF8String, annotationImage.image.mgl_styleImage);
|
||||
_mbglMap->addAnnotationImage([annotationImage.image mgl_styleImageWithIdentifier:iconIdentifier]);
|
||||
|
||||
// Create a slop area with a “radius” equal to the annotation image’s entire
|
||||
// size, allowing the eventual click to be on any point within this image.
|
||||
|
||||
@@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
- (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image *)image;
|
||||
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage;
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImage {
|
||||
- (std::unique_ptr<mbgl::style::Image>)mgl_styleImageWithIdentifier:(NSString *)identifier {
|
||||
// Create a bitmap image representation from the image, respecting backing
|
||||
// scale factor and any resizing done on the image at runtime.
|
||||
// http://www.cocoabuilder.com/archive/cocoa/82430-nsimage-getting-raw-bitmap-data.html#82431
|
||||
@@ -40,9 +40,10 @@
|
||||
|
||||
mbgl::PremultipliedImage cPremultipliedImage({ static_cast<uint32_t>(rep.pixelsWide), static_cast<uint32_t>(rep.pixelsHigh) });
|
||||
std::copy(rep.bitmapData, rep.bitmapData + cPremultipliedImage.bytes(), cPremultipliedImage.data.get());
|
||||
return std::make_unique<mbgl::style::Image>(std::move(cPremultipliedImage),
|
||||
(float)(rep.pixelsWide / self.size.width),
|
||||
[self isTemplate]);
|
||||
return std::make_unique<mbgl::style::Image>([identifier UTF8String],
|
||||
std::move(cPremultipliedImage),
|
||||
(float)(rep.pixelsWide / self.size.width),
|
||||
[self isTemplate]);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -664,7 +664,7 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
|
||||
mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data));
|
||||
mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage));
|
||||
nodeMap->map->addImage(*Nan::Utf8String(info[0]), std::make_unique<mbgl::style::Image>(std::move(cPremultipliedImage), pixelRatio));
|
||||
nodeMap->map->addImage(std::make_unique<mbgl::style::Image>(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio));
|
||||
}
|
||||
|
||||
void NodeMap::RemoveImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
|
||||
@@ -78,7 +78,7 @@ mbgl::Size sanitizedSize(const QSize& size) {
|
||||
};
|
||||
};
|
||||
|
||||
std::unique_ptr<mbgl::style::Image> toStyleImage(const QImage &sprite) {
|
||||
std::unique_ptr<mbgl::style::Image> toStyleImage(const QString &id, const QImage &sprite) {
|
||||
const QImage swapped = sprite
|
||||
.rgbSwapped()
|
||||
.convertToFormat(QImage::Format_ARGB32_Premultiplied);
|
||||
@@ -87,6 +87,7 @@ std::unique_ptr<mbgl::style::Image> toStyleImage(const QImage &sprite) {
|
||||
memcpy(img.get(), swapped.constBits(), swapped.byteCount());
|
||||
|
||||
return std::make_unique<mbgl::style::Image>(
|
||||
id.toStdString(),
|
||||
mbgl::PremultipliedImage(
|
||||
{ static_cast<uint32_t>(swapped.width()), static_cast<uint32_t>(swapped.height()) },
|
||||
std::move(img)),
|
||||
@@ -1027,7 +1028,7 @@ void QMapboxGL::addAnnotationIcon(const QString &name, const QImage &icon)
|
||||
{
|
||||
if (icon.isNull()) return;
|
||||
|
||||
d_ptr->mapObj->addAnnotationImage(name.toStdString(), toStyleImage(icon));
|
||||
d_ptr->mapObj->addAnnotationImage(toStyleImage(name, icon));
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -1329,7 +1330,7 @@ void QMapboxGL::addImage(const QString &id, const QImage &image)
|
||||
{
|
||||
if (image.isNull()) return;
|
||||
|
||||
d_ptr->mapObj->addImage(id.toStdString(), toStyleImage(image));
|
||||
d_ptr->mapObj->addImage(toStyleImage(id, image));
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -191,9 +191,9 @@ void AnnotationManager::removeTile(AnnotationTile& tile) {
|
||||
tiles.erase(&tile);
|
||||
}
|
||||
|
||||
void AnnotationManager::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas.addImage(id, added.impl);
|
||||
void AnnotationManager::addImage(std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas.addImage(added.impl);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
Update updateAnnotation(const AnnotationID&, const Annotation&, const uint8_t maxZoom);
|
||||
void removeAnnotation(const AnnotationID&);
|
||||
|
||||
void addImage(const std::string&, std::unique_ptr<style::Image>);
|
||||
void addImage(std::unique_ptr<style::Image>);
|
||||
void removeImage(const std::string&);
|
||||
double getTopOffsetPixelsForImage(const std::string&);
|
||||
SpriteAtlas& getSpriteAtlas() { return spriteAtlas; }
|
||||
|
||||
@@ -788,8 +788,8 @@ LatLng Map::latLngForPixel(const ScreenCoordinate& pixel) const {
|
||||
|
||||
#pragma mark - Annotations
|
||||
|
||||
void Map::addAnnotationImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
impl->annotationManager->addImage(id, std::move(image));
|
||||
void Map::addAnnotationImage(std::unique_ptr<style::Image> image) {
|
||||
impl->annotationManager->addImage(std::move(image));
|
||||
}
|
||||
|
||||
void Map::removeAnnotationImage(const std::string& id) {
|
||||
@@ -936,13 +936,13 @@ std::unique_ptr<Layer> Map::removeLayer(const std::string& id) {
|
||||
return removedLayer;
|
||||
}
|
||||
|
||||
void Map::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
void Map::addImage(std::unique_ptr<style::Image> image) {
|
||||
if (!impl->style) {
|
||||
return;
|
||||
}
|
||||
|
||||
impl->styleMutated = true;
|
||||
impl->style->addImage(id, std::move(image));
|
||||
impl->style->addImage(std::move(image));
|
||||
}
|
||||
|
||||
void Map::removeImage(const std::string& id) {
|
||||
|
||||
@@ -42,25 +42,20 @@ SpriteAtlas::SpriteAtlas(Size size_, float pixelRatio_)
|
||||
|
||||
SpriteAtlas::~SpriteAtlas() = default;
|
||||
|
||||
void SpriteAtlas::onSpriteLoaded(Images&& result) {
|
||||
void SpriteAtlas::onSpriteLoaded() {
|
||||
markAsLoaded();
|
||||
|
||||
for (auto& pair : result) {
|
||||
addImage(pair.first, pair.second->impl);
|
||||
}
|
||||
|
||||
for (auto requestor : requestors) {
|
||||
requestor->onIconsAvailable(buildIconMap());
|
||||
}
|
||||
requestors.clear();
|
||||
}
|
||||
|
||||
void SpriteAtlas::addImage(const std::string& id, Immutable<style::Image::Impl> image_) {
|
||||
void SpriteAtlas::addImage(Immutable<style::Image::Impl> image_) {
|
||||
icons.clear();
|
||||
|
||||
auto it = entries.find(id);
|
||||
auto it = entries.find(image_->id);
|
||||
if (it == entries.end()) {
|
||||
entries.emplace(id, Entry { std::move(image_), {}, {} });
|
||||
entries.emplace(image_->id, Entry { image_, {}, {} });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -43,12 +43,10 @@ public:
|
||||
|
||||
class SpriteAtlas : public util::noncopyable {
|
||||
public:
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
SpriteAtlas(Size, float pixelRatio);
|
||||
~SpriteAtlas();
|
||||
|
||||
void onSpriteLoaded(Images&&);
|
||||
void onSpriteLoaded();
|
||||
|
||||
void markAsLoaded() {
|
||||
loaded = true;
|
||||
@@ -61,7 +59,7 @@ public:
|
||||
void dumpDebugLogs() const;
|
||||
|
||||
const style::Image::Impl* getImage(const std::string&) const;
|
||||
void addImage(const std::string&, Immutable<style::Image::Impl>);
|
||||
void addImage(Immutable<style::Image::Impl>);
|
||||
void removeImage(const std::string&);
|
||||
|
||||
void getIcons(IconRequestor& requestor);
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
void addSpriteImage(Images& images, const std::string& id,
|
||||
void addSpriteImage(Images& images,
|
||||
std::unique_ptr<style::Image> image_,
|
||||
std::function<void (style::Image&)> onAdded) {
|
||||
|
||||
std::string id = image_->getID();
|
||||
auto it = images.find(id);
|
||||
if (it == images.end()) {
|
||||
// Add new
|
||||
|
||||
@@ -12,7 +12,6 @@ namespace mbgl {
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
void addSpriteImage(Images&,
|
||||
const std::string&,
|
||||
std::unique_ptr<style::Image>,
|
||||
std::function<void (style::Image&)> onAdded = [] (style::Image&){});
|
||||
|
||||
|
||||
@@ -89,10 +89,7 @@ void SpriteLoader::emitSpriteLoadedIfComplete() {
|
||||
loader->worker.invoke(&SpriteLoaderWorker::parse, loader->image, loader->json);
|
||||
}
|
||||
|
||||
void SpriteLoader::onParsed(Images&& result) {
|
||||
|
||||
|
||||
|
||||
void SpriteLoader::onParsed(std::vector<std::unique_ptr<style::Image>>&& result) {
|
||||
observer->onSpriteLoaded(std::move(result));
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
@@ -18,8 +18,6 @@ class SpriteLoaderObserver;
|
||||
|
||||
class SpriteLoader : public util::noncopyable {
|
||||
public:
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
SpriteLoader(float pixelRatio);
|
||||
~SpriteLoader();
|
||||
|
||||
@@ -32,7 +30,7 @@ private:
|
||||
|
||||
// Invoked by SpriteAtlasWorker
|
||||
friend class SpriteLoaderWorker;
|
||||
void onParsed(Images&& result);
|
||||
void onParsed(std::vector<std::unique_ptr<style::Image>>&&);
|
||||
void onError(std::exception_ptr);
|
||||
|
||||
const float pixelRatio;
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -13,11 +12,9 @@ class Image;
|
||||
|
||||
class SpriteLoaderObserver {
|
||||
public:
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
|
||||
virtual ~SpriteLoaderObserver() = default;
|
||||
|
||||
virtual void onSpriteLoaded(Images&&) {}
|
||||
virtual void onSpriteLoaded(std::vector<std::unique_ptr<style::Image>>&&) {}
|
||||
virtual void onSpriteError(std::exception_ptr) {}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,13 +13,14 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<style::Image> createStyleImage(const PremultipliedImage& image,
|
||||
const uint32_t srcX,
|
||||
const uint32_t srcY,
|
||||
const uint32_t width,
|
||||
const uint32_t height,
|
||||
const double ratio,
|
||||
const bool sdf) {
|
||||
std::unique_ptr<style::Image> createStyleImage(const std::string& id,
|
||||
const PremultipliedImage& image,
|
||||
const uint32_t srcX,
|
||||
const uint32_t srcY,
|
||||
const uint32_t width,
|
||||
const uint32_t height,
|
||||
const double ratio,
|
||||
const bool sdf) {
|
||||
// Disallow invalid parameter configurations.
|
||||
if (width <= 0 || height <= 0 || width > 1024 || height > 1024 ||
|
||||
ratio <= 0 || ratio > 10 ||
|
||||
@@ -37,7 +38,7 @@ std::unique_ptr<style::Image> createStyleImage(const PremultipliedImage& image,
|
||||
// Copy from the source image into our individual sprite image
|
||||
PremultipliedImage::copy(image, dstImage, { srcX, srcY }, { 0, 0 }, { width, height });
|
||||
|
||||
return std::make_unique<style::Image>(std::move(dstImage), ratio, sdf);
|
||||
return std::make_unique<style::Image>(id, std::move(dstImage), ratio, sdf);
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -84,7 +85,7 @@ bool getBoolean(const JSValue& value, const char* name, const bool def = false)
|
||||
|
||||
} // namespace
|
||||
|
||||
Images parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
const PremultipliedImage raster = decodeImage(encodedImage);
|
||||
|
||||
JSDocument doc;
|
||||
@@ -96,7 +97,7 @@ Images parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
} else if (!doc.IsObject()) {
|
||||
throw std::runtime_error("Sprite JSON root must be an object");
|
||||
} else {
|
||||
Images images;
|
||||
std::vector<std::unique_ptr<style::Image>> images;
|
||||
for (const auto& property : doc.GetObject()) {
|
||||
const std::string name = { property.name.GetString(), property.name.GetStringLength() };
|
||||
const JSValue& value = property.value;
|
||||
@@ -109,9 +110,9 @@ Images parseSprite(const std::string& encodedImage, const std::string& json) {
|
||||
const double pixelRatio = getDouble(value, "pixelRatio", 1);
|
||||
const bool sdf = getBoolean(value, "sdf", false);
|
||||
|
||||
auto image = createStyleImage(raster, x, y, width, height, pixelRatio, sdf);
|
||||
auto image = createStyleImage(name, raster, x, y, width, height, pixelRatio, sdf);
|
||||
if (image) {
|
||||
images.emplace(name, std::move(image));
|
||||
images.push_back(std::move(image));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/variant.hpp>
|
||||
#include <mbgl/util/geo.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -16,17 +13,16 @@ class Image;
|
||||
} // namespace style
|
||||
|
||||
// Extracts an individual image from a spritesheet from the given location.
|
||||
std::unique_ptr<style::Image> createStyleImage(const PremultipliedImage&,
|
||||
uint32_t srcX,
|
||||
uint32_t srcY,
|
||||
uint32_t srcWidth,
|
||||
uint32_t srcHeight,
|
||||
double ratio,
|
||||
bool sdf);
|
||||
|
||||
using Images = std::unordered_map<std::string, std::unique_ptr<style::Image>>;
|
||||
std::unique_ptr<style::Image> createStyleImage(const std::string& id,
|
||||
const PremultipliedImage&,
|
||||
uint32_t srcX,
|
||||
uint32_t srcY,
|
||||
uint32_t srcWidth,
|
||||
uint32_t srcHeight,
|
||||
double ratio,
|
||||
bool sdf);
|
||||
|
||||
// Parses an image and an associated JSON file and returns the sprite objects.
|
||||
Images parseSprite(const std::string& image, const std::string& json);
|
||||
std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& image, const std::string& json);
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -5,10 +5,15 @@
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
Image::Image(PremultipliedImage &&image,
|
||||
Image::Image(std::string id,
|
||||
PremultipliedImage &&image,
|
||||
const float pixelRatio,
|
||||
bool sdf)
|
||||
: impl(makeMutable<Impl>(std::move(image), pixelRatio, sdf)) {
|
||||
: impl(makeMutable<Impl>(std::move(id), std::move(image), pixelRatio, sdf)) {
|
||||
}
|
||||
|
||||
std::string Image::getID() const {
|
||||
return impl->id;
|
||||
}
|
||||
|
||||
const PremultipliedImage& Image::getImage() const {
|
||||
|
||||
@@ -4,10 +4,12 @@
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
Image::Impl::Impl(PremultipliedImage&& image_,
|
||||
const float pixelRatio_,
|
||||
bool sdf_)
|
||||
: image(std::move(image_)),
|
||||
Image::Impl::Impl(std::string id_,
|
||||
PremultipliedImage&& image_,
|
||||
const float pixelRatio_,
|
||||
bool sdf_)
|
||||
: id(std::move(id_)),
|
||||
image(std::move(image_)),
|
||||
pixelRatio(pixelRatio_),
|
||||
sdf(sdf_) {
|
||||
|
||||
|
||||
@@ -7,7 +7,9 @@ namespace style {
|
||||
|
||||
class Image::Impl {
|
||||
public:
|
||||
Impl(PremultipliedImage&&, float pixelRatio, bool sdf = false);
|
||||
Impl(std::string id, PremultipliedImage&&, float pixelRatio, bool sdf = false);
|
||||
|
||||
const std::string id;
|
||||
|
||||
PremultipliedImage image;
|
||||
|
||||
|
||||
@@ -505,9 +505,9 @@ bool Style::isLoaded() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Style::addImage(const std::string& id, std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas->addImage(id, added.impl);
|
||||
void Style::addImage(std::unique_ptr<style::Image> image) {
|
||||
addSpriteImage(spriteImages, std::move(image), [&](style::Image& added) {
|
||||
spriteAtlas->addImage(added.impl);
|
||||
observer->onUpdate(Update::Repaint);
|
||||
});
|
||||
}
|
||||
@@ -735,19 +735,11 @@ void Style::onTileError(RenderSource& source, const OverscaledTileID& tileID, st
|
||||
observer->onResourceError(error);
|
||||
}
|
||||
|
||||
void Style::onSpriteLoaded(SpriteLoader::Images&& images) {
|
||||
// Add images to collection
|
||||
Images addedImages;
|
||||
for (auto& entry : images) {
|
||||
addSpriteImage(spriteImages, entry.first, std::move(entry.second), [&] (style::Image& added) {
|
||||
addedImages.emplace(entry.first, std::make_unique<Image>(added));
|
||||
});
|
||||
void Style::onSpriteLoaded(std::vector<std::unique_ptr<Image>>&& images) {
|
||||
for (auto& image : images) {
|
||||
addImage(std::move(image));
|
||||
}
|
||||
|
||||
// Update render sprite atlas
|
||||
spriteAtlas->onSpriteLoaded(std::move(addedImages));
|
||||
|
||||
// Update observer
|
||||
spriteAtlas->onSpriteLoaded();
|
||||
observer->onUpdate(Update::Repaint); // For *-pattern properties.
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ public:
|
||||
const RenderLight& getRenderLight() const;
|
||||
|
||||
const style::Image* getImage(const std::string&) const;
|
||||
void addImage(const std::string&, std::unique_ptr<style::Image>);
|
||||
void addImage(std::unique_ptr<style::Image>);
|
||||
void removeImage(const std::string&);
|
||||
|
||||
RenderData getRenderData(MapDebugOptions, float angle) const;
|
||||
@@ -151,7 +151,7 @@ private:
|
||||
|
||||
// SpriteLoaderObserver implementation.
|
||||
std::unordered_map<std::string, std::unique_ptr<style::Image>> spriteImages;
|
||||
void onSpriteLoaded(SpriteLoaderObserver::Images&&) override;
|
||||
void onSpriteLoaded(std::vector<std::unique_ptr<Image>>&&) override;
|
||||
void onSpriteError(std::exception_ptr) override;
|
||||
|
||||
// SourceObserver implementation.
|
||||
|
||||
@@ -17,8 +17,8 @@ using namespace mbgl;
|
||||
namespace {
|
||||
|
||||
std::unique_ptr<style::Image> namedMarker(const std::string& name) {
|
||||
PremultipliedImage image = decodeImage(util::read_file("test/fixtures/sprites/" + name));
|
||||
return std::make_unique<style::Image>(std::move(image), 1.0);
|
||||
PremultipliedImage image = decodeImage(util::read_file("test/fixtures/sprites/" + name + ".png"));
|
||||
return std::make_unique<style::Image>(name, std::move(image), 1.0);
|
||||
}
|
||||
|
||||
class AnnotationTest {
|
||||
@@ -43,7 +43,7 @@ TEST(Annotations, SymbolAnnotation) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotation(SymbolAnnotation { Point<double>(0, 0), "default_marker" });
|
||||
test.checkRendering("point_annotation");
|
||||
|
||||
@@ -148,7 +148,7 @@ TEST(Annotations, AddMultiple) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotation(SymbolAnnotation { Point<double> { -10, 0 }, "default_marker" });
|
||||
|
||||
test::render(test.map, test.view);
|
||||
@@ -175,8 +175,8 @@ TEST(Annotations, UpdateSymbolAnnotationGeometry) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage("flipped_marker", namedMarker("flipped_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotationImage(namedMarker("flipped_marker"));
|
||||
AnnotationID point = test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" });
|
||||
|
||||
test::render(test.map, test.view);
|
||||
@@ -189,8 +189,8 @@ TEST(Annotations, UpdateSymbolAnnotationIcon) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage("flipped_marker", namedMarker("flipped_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotationImage(namedMarker("flipped_marker"));
|
||||
AnnotationID point = test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" });
|
||||
|
||||
test::render(test.map, test.view);
|
||||
@@ -271,7 +271,7 @@ TEST(Annotations, RemovePoint) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
AnnotationID point = test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" });
|
||||
|
||||
test::render(test.map, test.view);
|
||||
@@ -310,7 +310,7 @@ TEST(Annotations, SwitchStyle) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" });
|
||||
|
||||
test::render(test.map, test.view);
|
||||
@@ -323,7 +323,7 @@ TEST(Annotations, QueryRenderedFeatures) {
|
||||
AnnotationTest test;
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" });
|
||||
test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 50 }, "default_marker" });
|
||||
|
||||
@@ -347,7 +347,7 @@ TEST(Annotations, QueryFractionalZoomLevels) {
|
||||
auto box = ScreenBox { {}, { double(viewSize.width), double(viewSize.height) } };
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
|
||||
std::vector<mbgl::AnnotationID> ids;
|
||||
for (int longitude = 0; longitude < 10; ++longitude) {
|
||||
@@ -379,7 +379,7 @@ TEST(Annotations, VisibleFeatures) {
|
||||
auto box = ScreenBox { {}, { double(viewSize.width), double(viewSize.height) } };
|
||||
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.setLatLngZoom({ 5, 5 }, 3);
|
||||
|
||||
std::vector<mbgl::AnnotationID> ids;
|
||||
@@ -432,7 +432,7 @@ TEST(Annotations, DebugSparse) {
|
||||
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
|
||||
test.map.setDebug(MapDebugOptions::TileBorders);
|
||||
test.map.setZoom(1);
|
||||
test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png"));
|
||||
test.map.addAnnotationImage(namedMarker("default_marker"));
|
||||
test.map.addAnnotation(SymbolAnnotation { Point<double>(10, 10), "default_marker" });
|
||||
|
||||
test.checkRendering("debug_sparse");
|
||||
|
||||
@@ -20,7 +20,7 @@ class QueryTest {
|
||||
public:
|
||||
QueryTest() {
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/query_style.json"));
|
||||
map.addImage("test-icon", std::make_unique<style::Image>(
|
||||
map.addImage(std::make_unique<style::Image>("test-icon",
|
||||
decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")), 1.0));
|
||||
|
||||
test::render(map, view);
|
||||
|
||||
@@ -445,14 +445,14 @@ TEST(Map, AddImage) {
|
||||
Map map(test.backend, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still);
|
||||
auto decoded1 = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png"));
|
||||
auto decoded2 = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png"));
|
||||
auto image1 = std::make_unique<style::Image>(std::move(decoded1), 1.0);
|
||||
auto image2 = std::make_unique<style::Image>(std::move(decoded2), 1.0);
|
||||
auto image1 = std::make_unique<style::Image>("test-icon", std::move(decoded1), 1.0);
|
||||
auto image2 = std::make_unique<style::Image>("test-icon", std::move(decoded2), 1.0);
|
||||
|
||||
// No-op.
|
||||
map.addImage("test-icon", std::move(image1));
|
||||
map.addImage(std::move(image1));
|
||||
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/icon_style.json"));
|
||||
map.addImage("test-icon", std::move(image2));
|
||||
map.addImage(std::move(image2));
|
||||
test::checkImage("test/fixtures/map/add_icon", test::render(map, test.view));
|
||||
}
|
||||
|
||||
@@ -461,10 +461,10 @@ TEST(Map, RemoveImage) {
|
||||
|
||||
Map map(test.backend, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still);
|
||||
auto decoded = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png"));
|
||||
auto image = std::make_unique<style::Image>(std::move(decoded), 1.0);
|
||||
auto image = std::make_unique<style::Image>("test-icon", std::move(decoded), 1.0);
|
||||
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/icon_style.json"));
|
||||
map.addImage("test-icon", std::move(image));
|
||||
map.addImage(std::move(image));
|
||||
map.removeImage("test-icon");
|
||||
test::checkImage("test/fixtures/map/remove_icon", test::render(map, test.view));
|
||||
}
|
||||
@@ -474,10 +474,10 @@ TEST(Map, GetImage) {
|
||||
|
||||
Map map(test.backend, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still);
|
||||
auto decoded = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png"));
|
||||
auto image = std::make_unique<style::Image>(std::move(decoded), 1.0);
|
||||
auto image = std::make_unique<style::Image>("test-icon", std::move(decoded), 1.0);
|
||||
|
||||
map.setStyleJSON(util::read_file("test/fixtures/api/icon_style.json"));
|
||||
map.addImage("test-icon", std::move(image));
|
||||
map.addImage(std::move(image));
|
||||
test::checkImage("test/fixtures/map/get_icon", map.getImage("test-icon")->getImage());
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@ TEST(SpriteAtlas, Basic) {
|
||||
|
||||
auto images = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"),
|
||||
util::read_file("test/fixtures/annotations/emerald.json"));
|
||||
for (auto& pair : images) {
|
||||
atlas.addImage(pair.first, pair.second->impl);
|
||||
for (auto& image : images) {
|
||||
atlas.addImage(image->impl);
|
||||
}
|
||||
|
||||
EXPECT_EQ(1.0f, atlas.getPixelRatio());
|
||||
@@ -79,8 +79,8 @@ TEST(SpriteAtlas, Size) {
|
||||
|
||||
auto images = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"),
|
||||
util::read_file("test/fixtures/annotations/emerald.json"));
|
||||
for (auto& pair : images) {
|
||||
atlas.addImage(pair.first, pair.second->impl);
|
||||
for (auto& image : images) {
|
||||
atlas.addImage(image->impl);
|
||||
}
|
||||
|
||||
EXPECT_DOUBLE_EQ(1.4f, atlas.getPixelRatio());
|
||||
@@ -113,7 +113,7 @@ TEST(SpriteAtlas, Updates) {
|
||||
EXPECT_EQ(32u, atlas.getSize().width);
|
||||
EXPECT_EQ(32u, atlas.getSize().height);
|
||||
|
||||
atlas.addImage("one", makeMutable<style::Image::Impl>(PremultipliedImage({ 16, 12 }), 1));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("one", PremultipliedImage({ 16, 12 }), 1));
|
||||
auto one = *atlas.getIcon("one");
|
||||
float imagePixelRatio = one.relativePixelRatio * atlas.getPixelRatio();
|
||||
EXPECT_EQ(0, one.pos.x);
|
||||
@@ -137,7 +137,7 @@ TEST(SpriteAtlas, Updates) {
|
||||
for (size_t i = 0; i < image2.bytes(); i++) {
|
||||
image2.data.get()[i] = 255;
|
||||
}
|
||||
atlas.addImage("one", makeMutable<style::Image::Impl>(std::move(image2), 1));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("one", std::move(image2), 1));
|
||||
|
||||
test::checkImage("test/fixtures/sprite_atlas/updates_after", atlas.getAtlasImage());
|
||||
}
|
||||
@@ -146,9 +146,9 @@ TEST(SpriteAtlas, AddRemove) {
|
||||
FixtureLog log;
|
||||
SpriteAtlas atlas({ 32, 32 }, 1);
|
||||
|
||||
atlas.addImage("one", makeMutable<style::Image::Impl>(PremultipliedImage({ 16, 16 }), 2));
|
||||
atlas.addImage("two", makeMutable<style::Image::Impl>(PremultipliedImage({ 16, 16 }), 2));
|
||||
atlas.addImage("three", makeMutable<style::Image::Impl>(PremultipliedImage({ 16, 16 }), 2));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("one", PremultipliedImage({ 16, 16 }), 2));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("two", PremultipliedImage({ 16, 16 }), 2));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("three", PremultipliedImage({ 16, 16 }), 2));
|
||||
|
||||
atlas.removeImage("one");
|
||||
atlas.removeImage("two");
|
||||
@@ -176,12 +176,12 @@ TEST(SpriteAtlas, RemoveReleasesBinPackRect) {
|
||||
|
||||
SpriteAtlas atlas({ 36, 36 }, 1);
|
||||
|
||||
atlas.addImage("big", makeMutable<style::Image::Impl>(PremultipliedImage({ 32, 32 }), 1));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("big", PremultipliedImage({ 32, 32 }), 1));
|
||||
EXPECT_TRUE(atlas.getIcon("big"));
|
||||
|
||||
atlas.removeImage("big");
|
||||
|
||||
atlas.addImage("big", makeMutable<style::Image::Impl>(PremultipliedImage({ 32, 32 }), 1));
|
||||
atlas.addImage(makeMutable<style::Image::Impl>("big", PremultipliedImage({ 32, 32 }), 1));
|
||||
EXPECT_TRUE(atlas.getIcon("big"));
|
||||
EXPECT_TRUE(log.empty());
|
||||
}
|
||||
|
||||
@@ -14,16 +14,16 @@ TEST(SpriteImageCollection, OtherPixelRatio) {
|
||||
Images images;
|
||||
|
||||
// Adding mismatched sprite image
|
||||
addSpriteImage(images, "one", std::make_unique<style::Image>(PremultipliedImage({ 8, 8 }), 2));
|
||||
addSpriteImage(images, std::make_unique<style::Image>("one", PremultipliedImage({ 8, 8 }), 2));
|
||||
}
|
||||
|
||||
TEST(SpriteImageCollection, Replace) {
|
||||
FixtureLog log;
|
||||
Images images;
|
||||
|
||||
addSpriteImage(images, "sprite", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2));
|
||||
addSpriteImage(images, std::make_unique<style::Image>("sprite", PremultipliedImage({ 16, 16 }), 2));
|
||||
auto image = images.find("sprite")->second.get();
|
||||
addSpriteImage(images, "sprite", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2));
|
||||
addSpriteImage(images, std::make_unique<style::Image>("sprite", PremultipliedImage({ 16, 16 }), 2));
|
||||
EXPECT_NE(image, images.find("sprite")->second.get());
|
||||
}
|
||||
|
||||
@@ -31,8 +31,8 @@ TEST(SpriteImageCollection, ReplaceWithDifferentDimensions) {
|
||||
FixtureLog log;
|
||||
Images images;
|
||||
|
||||
addSpriteImage(images, "sprite", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2));
|
||||
addSpriteImage(images, "sprite", std::make_unique<style::Image>(PremultipliedImage({ 18, 18 }), 2));
|
||||
addSpriteImage(images, std::make_unique<style::Image>("sprite", PremultipliedImage({ 16, 16 }), 2));
|
||||
addSpriteImage(images, std::make_unique<style::Image>("sprite", PremultipliedImage({ 18, 18 }), 2));
|
||||
|
||||
EXPECT_EQ(1u, log.count({
|
||||
EventSeverity::Warning,
|
||||
|
||||
@@ -18,7 +18,7 @@ using namespace mbgl::style;
|
||||
|
||||
class StubSpriteLoaderObserver : public SpriteLoaderObserver {
|
||||
public:
|
||||
void onSpriteLoaded(Images&& images) override {
|
||||
void onSpriteLoaded(std::vector<std::unique_ptr<style::Image>>&& images) override {
|
||||
if (spriteLoaded) spriteLoaded(std::move(images));
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public:
|
||||
if (spriteError) spriteError(error);
|
||||
}
|
||||
|
||||
std::function<void (Images&&)> spriteLoaded;
|
||||
std::function<void (std::vector<std::unique_ptr<style::Image>>&&)> spriteLoaded;
|
||||
std::function<void (std::exception_ptr)> spriteError;
|
||||
};
|
||||
|
||||
@@ -94,8 +94,8 @@ TEST(SpriteLoader, LoadingSuccess) {
|
||||
test.end();
|
||||
};
|
||||
|
||||
test.observer.spriteLoaded = [&] (SpriteLoaderObserver::Images&& images) {
|
||||
EXPECT_EQ(images.size(), Images::size_type(367));
|
||||
test.observer.spriteLoaded = [&] (std::vector<std::unique_ptr<style::Image>>&& images) {
|
||||
EXPECT_EQ(images.size(), 367u);
|
||||
test.end();
|
||||
};
|
||||
|
||||
@@ -171,7 +171,7 @@ TEST(SpriteLoader, LoadingCancel) {
|
||||
return optional<Response>();
|
||||
};
|
||||
|
||||
test.observer.spriteLoaded = [&] (const SpriteLoaderObserver::Images&) {
|
||||
test.observer.spriteLoaded = [&] (const std::vector<std::unique_ptr<style::Image>>&) {
|
||||
FAIL() << "Should never be called";
|
||||
};
|
||||
|
||||
|
||||
@@ -27,19 +27,19 @@ TEST(Sprite, SpriteImageCreationInvalid) {
|
||||
ASSERT_EQ(200u, image_1x.size.width);
|
||||
ASSERT_EQ(299u, image_1x.size.height);
|
||||
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 0, 16, 1, false)); // width == 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, 0, 1, false)); // height == 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, -1, 16, 1, false)); // width < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, -1, 1, false)); // height < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 1, 1, 0, false)); // ratio == 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 1, 1, -1, false)); // ratio < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 1, 1, 23, false)); // ratio too large
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 2048, 16, 1, false)); // too wide
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, 1025, 1, false)); // too tall
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, -1, 0, 16, 16, 1, false)); // srcX < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, -1, 16, 16, 1, false)); // srcY < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, image_1x.size.width + 1, 16, 1, false)); // right edge out of bounds
|
||||
ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, image_1x.size.height + 1, 1, false)); // bottom edge out of bounds
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 0, 16, 1, false)); // width == 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 16, 0, 1, false)); // height == 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, -1, 16, 1, false)); // width < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 16, -1, 1, false)); // height < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 1, 1, 0, false)); // ratio == 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 1, 1, -1, false)); // ratio < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 1, 1, 23, false)); // ratio too large
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 2048, 16, 1, false)); // too wide
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 16, 1025, 1, false)); // too tall
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, -1, 0, 16, 16, 1, false)); // srcX < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, -1, 16, 16, 1, false)); // srcY < 0
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, image_1x.size.width + 1, 16, 1, false)); // right edge out of bounds
|
||||
ASSERT_EQ(nullptr, createStyleImage("test", image_1x, 0, 0, 16, image_1x.size.height + 1, 1, false)); // bottom edge out of bounds
|
||||
|
||||
EXPECT_EQ(1u, log.count({
|
||||
EventSeverity::Error,
|
||||
@@ -141,7 +141,7 @@ TEST(Sprite, SpriteImageCreation1x) {
|
||||
ASSERT_EQ(299u, image_1x.size.height);
|
||||
|
||||
{ // "museum_icon":{"x":177,"y":187,"width":18,"height":18,"pixelRatio":1,"sdf":false}
|
||||
const auto sprite = createStyleImage(image_1x, 177, 187, 18, 18, 1, false);
|
||||
const auto sprite = createStyleImage("test", image_1x, 177, 187, 18, 18, 1, false);
|
||||
ASSERT_TRUE(sprite.get());
|
||||
EXPECT_EQ(18, sprite->getWidth());
|
||||
EXPECT_EQ(18, sprite->getHeight());
|
||||
@@ -157,7 +157,7 @@ TEST(Sprite, SpriteImageCreation2x) {
|
||||
const PremultipliedImage image_2x = decodeImage(util::read_file("test/fixtures/annotations/emerald@2x.png"));
|
||||
|
||||
// "museum_icon":{"x":354,"y":374,"width":36,"height":36,"pixelRatio":2,"sdf":false}
|
||||
const auto sprite = createStyleImage(image_2x, 354, 374, 36, 36, 2, false);
|
||||
const auto sprite = createStyleImage("test", image_2x, 354, 374, 36, 36, 2, false);
|
||||
ASSERT_TRUE(sprite.get());
|
||||
EXPECT_EQ(18, sprite->getWidth());
|
||||
EXPECT_EQ(18, sprite->getHeight());
|
||||
@@ -172,7 +172,7 @@ TEST(Sprite, SpriteImageCreation1_5x) {
|
||||
const PremultipliedImage image_2x = decodeImage(util::read_file("test/fixtures/annotations/emerald@2x.png"));
|
||||
|
||||
// "museum_icon":{"x":354,"y":374,"width":36,"height":36,"pixelRatio":2,"sdf":false}
|
||||
const auto sprite = createStyleImage(image_2x, 354, 374, 36, 36, 1.5, false);
|
||||
const auto sprite = createStyleImage("test", image_2x, 354, 374, 36, 36, 1.5, false);
|
||||
ASSERT_TRUE(sprite.get());
|
||||
EXPECT_EQ(24, sprite->getWidth());
|
||||
EXPECT_EQ(24, sprite->getHeight());
|
||||
@@ -183,7 +183,7 @@ TEST(Sprite, SpriteImageCreation1_5x) {
|
||||
sprite->getImage());
|
||||
|
||||
// "hospital_icon":{"x":314,"y":518,"width":36,"height":36,"pixelRatio":2,"sdf":false}
|
||||
const auto sprite2 = createStyleImage(image_2x, 314, 518, 35, 35, 1.5, false);
|
||||
const auto sprite2 = createStyleImage("test", image_2x, 314, 518, 35, 35, 1.5, false);
|
||||
ASSERT_TRUE(sprite2.get());
|
||||
EXPECT_EQ(float(35 / 1.5), sprite2->getWidth());
|
||||
EXPECT_EQ(float(35 / 1.5), sprite2->getHeight());
|
||||
@@ -202,7 +202,7 @@ TEST(Sprite, SpriteParsing) {
|
||||
|
||||
std::set<std::string> names;
|
||||
std::transform(images.begin(), images.end(), std::inserter(names, names.begin()),
|
||||
[](const auto& pair) { return pair.first; });
|
||||
[](const auto& image) { return image->getID(); });
|
||||
|
||||
EXPECT_EQ(std::set<std::string>({ "airfield_icon",
|
||||
"airport_icon",
|
||||
@@ -280,7 +280,7 @@ TEST(Sprite, SpriteParsing) {
|
||||
names);
|
||||
|
||||
{
|
||||
auto& sprite = images.find("generic-metro")->second;
|
||||
auto& sprite = *std::find_if(images.begin(), images.end(), [] (const auto& image) { return image->getID() == "generic-metro"; });
|
||||
EXPECT_EQ(18, sprite->getWidth());
|
||||
EXPECT_EQ(18, sprite->getHeight());
|
||||
EXPECT_EQ(18u, sprite->getImage().size.width);
|
||||
|
||||
@@ -8,7 +8,7 @@ using namespace mbgl;
|
||||
|
||||
TEST(StyleImage, ZeroWidth) {
|
||||
try {
|
||||
style::Image(PremultipliedImage({ 0, 16 }), 2.0);
|
||||
style::Image("test", PremultipliedImage({ 0, 16 }), 2.0);
|
||||
FAIL() << "Expected exception";
|
||||
} catch (util::SpriteImageException& ex) {
|
||||
EXPECT_STREQ("Sprite image dimensions may not be zero", ex.what());
|
||||
@@ -17,7 +17,7 @@ TEST(StyleImage, ZeroWidth) {
|
||||
|
||||
TEST(StyleImage, ZeroHeight) {
|
||||
try {
|
||||
style::Image(PremultipliedImage({ 16, 0 }), 2.0);
|
||||
style::Image("test", PremultipliedImage({ 16, 0 }), 2.0);
|
||||
FAIL() << "Expected exception";
|
||||
} catch (util::SpriteImageException& ex) {
|
||||
EXPECT_STREQ("Sprite image dimensions may not be zero", ex.what());
|
||||
@@ -26,7 +26,7 @@ TEST(StyleImage, ZeroHeight) {
|
||||
|
||||
TEST(StyleImage, ZeroRatio) {
|
||||
try {
|
||||
style::Image(PremultipliedImage({ 16, 16 }), 0.0);
|
||||
style::Image("test", PremultipliedImage({ 16, 16 }), 0.0);
|
||||
FAIL() << "Expected exception";
|
||||
} catch (util::SpriteImageException& ex) {
|
||||
EXPECT_STREQ("Sprite pixelRatio may not be <= 0", ex.what());
|
||||
@@ -34,7 +34,7 @@ TEST(StyleImage, ZeroRatio) {
|
||||
}
|
||||
|
||||
TEST(StyleImage, Retina) {
|
||||
style::Image image(PremultipliedImage({ 32, 24 }), 2.0);
|
||||
style::Image image("test", PremultipliedImage({ 32, 24 }), 2.0);
|
||||
EXPECT_EQ(16, image.getWidth());
|
||||
EXPECT_EQ(32u, image.getImage().size.width);
|
||||
EXPECT_EQ(12, image.getHeight());
|
||||
@@ -43,7 +43,7 @@ TEST(StyleImage, Retina) {
|
||||
}
|
||||
|
||||
TEST(StyleImage, FractionalRatio) {
|
||||
style::Image image(PremultipliedImage({ 20, 12 }), 1.5);
|
||||
style::Image image("test", PremultipliedImage({ 20, 12 }), 1.5);
|
||||
EXPECT_EQ(float(20.0 / 1.5), image.getWidth());
|
||||
EXPECT_EQ(20u, image.getImage().size.width);
|
||||
EXPECT_EQ(float(12.0 / 1.5), image.getHeight());
|
||||
|
||||
@@ -14,7 +14,7 @@ TEST(getIconQuads, normal) {
|
||||
Anchor anchor(2.0, 3.0, 0.0, 0.5f, 0);
|
||||
SpriteAtlasElement image = {
|
||||
Rect<uint16_t>( 0, 0, 15, 11 ),
|
||||
style::Image::Impl(PremultipliedImage({1,1}), 1.0),
|
||||
style::Image::Impl("test", PremultipliedImage({1,1}), 1.0),
|
||||
{ 0, 0 },
|
||||
1.0f
|
||||
};
|
||||
@@ -47,7 +47,7 @@ TEST(getIconQuads, style) {
|
||||
Anchor anchor(0.0, 0.0, 0.0, 0.5f, 0);
|
||||
SpriteAtlasElement image = {
|
||||
Rect<uint16_t>( 0, 0, 20, 20 ),
|
||||
style::Image::Impl(PremultipliedImage({1,1}), 1.0),
|
||||
style::Image::Impl("test", PremultipliedImage({1,1}), 1.0),
|
||||
{ 0, 0 },
|
||||
1.0f
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user