diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-24 10:07:18 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-25 15:57:36 -0800 |
commit | 0c1e378bc9555f6cf826bb38b1a36fa742f8ce9b (patch) | |
tree | 9aec8e4f475ff715645072503b3e0ec78f0573af /src | |
parent | 2de0a351a0635192bd05116cebdf0103c2638d05 (diff) | |
download | qtlocation-mapboxgl-0c1e378bc9555f6cf826bb38b1a36fa742f8ce9b.tar.gz |
[core] Rewrite image handling
* Consolidate Image and StillImage
* Typecheck unassociated vs premultiplied images
* Rewrite default platform image decoding implementation
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/map/map_context.cpp | 13 | ||||
-rw-r--r-- | src/mbgl/map/view.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/renderer/raster_bucket.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/renderer/raster_bucket.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_parser.cpp | 21 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_parser.hpp | 9 | ||||
-rw-r--r-- | src/mbgl/util/raster.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/util/raster.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/util/worker.cpp | 7 |
9 files changed, 38 insertions, 41 deletions
diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index cf06dd06eb..f00b3eaf55 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -1,7 +1,6 @@ #include <mbgl/map/map_context.hpp> #include <mbgl/map/map_data.hpp> #include <mbgl/map/view.hpp> -#include <mbgl/map/still_image.hpp> #include <mbgl/platform/log.hpp> @@ -200,22 +199,22 @@ void MapContext::renderStill(const TransformState& state, const FrameData& frame } if (data.mode != MapMode::Still) { - fn(std::make_exception_ptr(util::MisuseException("Map is not in still image render mode")), nullptr); + fn(std::make_exception_ptr(util::MisuseException("Map is not in still image render mode")), {}); return; } if (callback) { - fn(std::make_exception_ptr(util::MisuseException("Map is currently rendering an image")), nullptr); + fn(std::make_exception_ptr(util::MisuseException("Map is currently rendering an image")), {}); return; } if (!style) { - fn(std::make_exception_ptr(util::MisuseException("Map doesn't have a style")), nullptr); + fn(std::make_exception_ptr(util::MisuseException("Map doesn't have a style")), {}); return; } if (style->getLastError()) { - fn(style->getLastError(), nullptr); + fn(style->getLastError(), {}); return; } @@ -246,7 +245,7 @@ bool MapContext::renderSync(const TransformState& state, const FrameData& frame) painter->render(*style, frame); if (data.mode == MapMode::Still) { - callback(nullptr, view.readStillImage()); + callback(nullptr, std::move(view.readStillImage())); callback = nullptr; } @@ -320,7 +319,7 @@ void MapContext::onResourceLoadingFailed(std::exception_ptr error) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); if (data.mode == MapMode::Still && callback) { - callback(error, nullptr); + callback(error, {}); callback = nullptr; } } diff --git a/src/mbgl/map/view.cpp b/src/mbgl/map/view.cpp index 5da290534a..fa8b7584cc 100644 --- a/src/mbgl/map/view.cpp +++ b/src/mbgl/map/view.cpp @@ -1,6 +1,5 @@ #include <mbgl/map/view.hpp> #include <mbgl/map/map.hpp> -#include <mbgl/map/still_image.hpp> #include <cassert> @@ -11,8 +10,8 @@ void View::initialize(Map *map_) { map = map_; } -std::unique_ptr<StillImage> View::readStillImage() { - return nullptr; +UnassociatedImage View::readStillImage() { + return {}; } void View::notifyMapChange(MapChange) { diff --git a/src/mbgl/renderer/raster_bucket.cpp b/src/mbgl/renderer/raster_bucket.cpp index 4a7dfeda05..37e5c31cc4 100644 --- a/src/mbgl/renderer/raster_bucket.cpp +++ b/src/mbgl/renderer/raster_bucket.cpp @@ -23,7 +23,7 @@ void RasterBucket::render(Painter& painter, painter.renderRaster(*this, dynamic_cast<const RasterLayer&>(layer), id, matrix); } -bool RasterBucket::setImage(std::unique_ptr<util::Image> image) { +bool RasterBucket::setImage(PremultipliedImage image) { return raster.load(std::move(image)); } diff --git a/src/mbgl/renderer/raster_bucket.hpp b/src/mbgl/renderer/raster_bucket.hpp index 2882a94bbd..e12257d864 100644 --- a/src/mbgl/renderer/raster_bucket.hpp +++ b/src/mbgl/renderer/raster_bucket.hpp @@ -18,7 +18,7 @@ public: void render(Painter&, const StyleLayer&, const TileID&, const mat4&) override; bool hasData() const override; - bool setImage(std::unique_ptr<util::Image> image); + bool setImage(PremultipliedImage); void drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array); diff --git a/src/mbgl/sprite/sprite_parser.cpp b/src/mbgl/sprite/sprite_parser.cpp index a8ed4f0e12..3b6464be9e 100644 --- a/src/mbgl/sprite/sprite_parser.cpp +++ b/src/mbgl/sprite/sprite_parser.cpp @@ -14,7 +14,7 @@ namespace mbgl { -SpriteImagePtr createSpriteImage(const util::Image& image, +SpriteImagePtr createSpriteImage(const PremultipliedImage& image, const uint16_t srcX, const uint16_t srcY, const uint16_t srcWidth, @@ -37,18 +37,18 @@ SpriteImagePtr createSpriteImage(const util::Image& image, std::string data(dstWidth * dstHeight * 4, '\0'); - auto srcData = reinterpret_cast<const uint32_t*>(image.getData()); + auto srcData = reinterpret_cast<const uint32_t*>(image.data.get()); auto dstData = reinterpret_cast<uint32_t*>(const_cast<char*>(data.data())); - const int32_t maxX = std::min(image.getWidth(), uint32_t(srcWidth + srcX)) - srcX; - assert(maxX <= int32_t(image.getWidth())); - const int32_t maxY = std::min(image.getHeight(), uint32_t(srcHeight + srcY)) - srcY; - assert(maxY <= int32_t(image.getHeight())); + const int32_t maxX = std::min(uint32_t(image.width), uint32_t(srcWidth + srcX)) - srcX; + assert(maxX <= int32_t(image.width)); + const int32_t maxY = std::min(uint32_t(image.height), uint32_t(srcHeight + srcY)) - srcY; + assert(maxY <= int32_t(image.height)); // Copy from the source image into our individual sprite image for (uint16_t y = 0; y < maxY; ++y) { const auto dstRow = y * dstWidth; - const auto srcRow = (y + srcY) * image.getWidth() + srcX; + const auto srcRow = (y + srcY) * image.width + srcX; for (uint16_t x = 0; x < maxX; ++x) { dstData[dstRow + x] = srcData[srcRow + x]; } @@ -105,10 +105,11 @@ SpriteParseResult parseSprite(const std::string& image, const std::string& json) using namespace rapidjson; Sprites sprites; + PremultipliedImage raster; - // Parse the sprite image. - const util::Image raster(image); - if (!raster) { + try { + raster = decodeImage(image); + } catch (...) { return std::string("Could not parse sprite image"); } diff --git a/src/mbgl/sprite/sprite_parser.hpp b/src/mbgl/sprite/sprite_parser.hpp index d385d77aea..468ba13c53 100644 --- a/src/mbgl/sprite/sprite_parser.hpp +++ b/src/mbgl/sprite/sprite_parser.hpp @@ -3,6 +3,7 @@ #include <mapbox/variant.hpp> +#include <mbgl/util/image.hpp> #include <mbgl/util/noncopyable.hpp> #include <mbgl/util/geo.hpp> @@ -12,18 +13,12 @@ namespace mbgl { -namespace util { - -class Image; - -} // namespace util - class SpriteImage; using SpriteImagePtr = std::shared_ptr<const SpriteImage>; // Extracts an individual image from a spritesheet from the given location. -SpriteImagePtr createSpriteImage(const util::Image& image, +SpriteImagePtr createSpriteImage(const PremultipliedImage&, uint16_t srcX, uint16_t srcY, uint16_t srcWidth, diff --git a/src/mbgl/util/raster.cpp b/src/mbgl/util/raster.cpp index 1c277040cd..c4d65ff320 100644 --- a/src/mbgl/util/raster.cpp +++ b/src/mbgl/util/raster.cpp @@ -25,13 +25,13 @@ bool Raster::isLoaded() const { return loaded; } -bool Raster::load(std::unique_ptr<util::Image> image) { +bool Raster::load(PremultipliedImage image) { img = std::move(image); - width = img->getWidth(); - height = img->getHeight(); + width = GLsizei(img.width); + height = GLsizei(img.height); std::lock_guard<std::mutex> lock(mtx); - if (img->getData()) { + if (img.data.get()) { loaded = true; } return loaded; @@ -44,7 +44,7 @@ void Raster::bind(bool linear) { return; } - if (img && !textured) { + if (img.data && !textured) { upload(); } else if (textured) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); @@ -59,7 +59,7 @@ void Raster::bind(bool linear) { } void Raster::upload() { - if (img && !textured) { + if (img.data && !textured) { texture = texturePool.getTextureID(); MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture)); #ifndef GL_ES_VERSION_2_0 @@ -67,8 +67,8 @@ void Raster::upload() { #endif MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); - MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData())); - img.reset(); + MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data.get())); + img.data.reset(); textured = true; } } diff --git a/src/mbgl/util/raster.hpp b/src/mbgl/util/raster.hpp index adf3b2cbf7..5c65c46e9d 100644 --- a/src/mbgl/util/raster.hpp +++ b/src/mbgl/util/raster.hpp @@ -20,7 +20,7 @@ public: ~Raster(); // load image data - bool load(std::unique_ptr<util::Image> image); + bool load(PremultipliedImage); // bind current texture void bind(bool linear = false); @@ -58,7 +58,7 @@ private: GLint filter = 0; // the raw pixels - std::unique_ptr<util::Image> img; + PremultipliedImage img; }; } diff --git a/src/mbgl/util/worker.cpp b/src/mbgl/util/worker.cpp index 9940fb649c..00bdb5baee 100644 --- a/src/mbgl/util/worker.cpp +++ b/src/mbgl/util/worker.cpp @@ -17,8 +17,11 @@ public: void parseRasterTile(std::unique_ptr<RasterBucket> bucket, const std::shared_ptr<const std::string> data, std::function<void(RasterTileParseResult)> callback) { - std::unique_ptr<util::Image> image(new util::Image(*data)); - if (!(*image)) { + PremultipliedImage image; + + try { + image = decodeImage(*data); + } catch (...) { callback(RasterTileParseResult("error parsing raster image")); } |