summaryrefslogtreecommitdiff
path: root/include/mbgl/util
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/util')
-rw-r--r--include/mbgl/util/chrono.hpp2
-rw-r--r--include/mbgl/util/compression.hpp4
-rw-r--r--include/mbgl/util/constants.hpp4
-rw-r--r--include/mbgl/util/feature.hpp17
-rw-r--r--include/mbgl/util/geo.hpp8
-rw-r--r--include/mbgl/util/image.hpp72
-rw-r--r--include/mbgl/util/size.hpp4
7 files changed, 98 insertions, 13 deletions
diff --git a/include/mbgl/util/chrono.hpp b/include/mbgl/util/chrono.hpp
index 4adf030331..723cd131e3 100644
--- a/include/mbgl/util/chrono.hpp
+++ b/include/mbgl/util/chrono.hpp
@@ -29,7 +29,7 @@ std::string rfc1123(Timestamp);
std::string iso8601(Timestamp);
Timestamp parseTimestamp(const char *);
-
+
Timestamp parseTimestamp(const int32_t timestamp);
// C++17 polyfill
diff --git a/include/mbgl/util/compression.hpp b/include/mbgl/util/compression.hpp
index 5e232187c3..93a3ddb8bc 100644
--- a/include/mbgl/util/compression.hpp
+++ b/include/mbgl/util/compression.hpp
@@ -4,9 +4,9 @@
namespace mbgl {
namespace util {
-
+
std::string compress(const std::string& raw);
std::string decompress(const std::string& raw);
-
+
} // namespace util
} // namespace mbgl
diff --git a/include/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp
index 85e19c2ff0..0f2779e33b 100644
--- a/include/mbgl/util/constants.hpp
+++ b/include/mbgl/util/constants.hpp
@@ -44,11 +44,11 @@ constexpr Duration DEFAULT_FADE_DURATION = Milliseconds(300);
constexpr Seconds CLOCK_SKEW_RETRY_TIMEOUT { 30 };
constexpr UnitBezier DEFAULT_TRANSITION_EASE = { 0, 0, 0.25, 1 };
-
+
constexpr int DEFAULT_RATE_LIMIT_TIMEOUT = 5;
constexpr const char* API_BASE_URL = "https://api.mapbox.com";
-
+
} // namespace util
namespace debug {
diff --git a/include/mbgl/util/feature.hpp b/include/mbgl/util/feature.hpp
index b72aa15ddd..4eeceda944 100644
--- a/include/mbgl/util/feature.hpp
+++ b/include/mbgl/util/feature.hpp
@@ -12,4 +12,21 @@ using PropertyMap = mapbox::geometry::property_map;
using FeatureIdentifier = mapbox::geometry::identifier;
using Feature = mapbox::geometry::feature<double>;
+template <class T>
+optional<T> numericValue(const Value& value) {
+ return value.match(
+ [] (uint64_t t) {
+ return optional<T>(t);
+ },
+ [] (int64_t t) {
+ return optional<T>(t);
+ },
+ [] (double t) {
+ return optional<T>(t);
+ },
+ [] (auto) {
+ return optional<T>();
+ });
+}
+
} // namespace mbgl
diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp
index 9dca10eb84..57a028e467 100644
--- a/include/mbgl/util/geo.hpp
+++ b/include/mbgl/util/geo.hpp
@@ -187,10 +187,10 @@ enum class NorthOrientation : uint8_t {
/// The distance on each side between a rectangle and a rectangle within.
class EdgeInsets {
public:
- double top = 0; ///< Number of pixels inset from the top edge.
- double left = 0; ///< Number of pixels inset from the left edge.
- double bottom = 0; ///< Number of pixels inset from the bottom edge.
- double right = 0; ///< Number of pixels inset from the right edge.
+ double top = 0; // Number of pixels inset from the top edge.
+ double left = 0; // Number of pixels inset from the left edge.
+ double bottom = 0; // Number of pixels inset from the bottom edge.
+ double right = 0; // Number of pixels inset from the right edge.
EdgeInsets() {}
diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp
index 1d84d4824a..5d1462e7c4 100644
--- a/include/mbgl/util/image.hpp
+++ b/include/mbgl/util/image.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <mbgl/util/noncopyable.hpp>
+#include <mbgl/util/geometry.hpp>
#include <mbgl/util/size.hpp>
#include <string>
@@ -24,6 +25,15 @@ public:
: size(std::move(size_)),
data(std::make_unique<uint8_t[]>(bytes())) {}
+ Image(Size size_, const uint8_t* srcData, std::size_t srcLength)
+ : size(std::move(size_)) {
+ if (srcLength != bytes()) {
+ throw std::invalid_argument("mismatched image size");
+ }
+ data = std::make_unique<uint8_t[]>(bytes());
+ std::copy(srcData, srcData + srcLength, data.get());
+ }
+
Image(Size size_, std::unique_ptr<uint8_t[]> data_)
: size(std::move(size_)),
data(std::move(data_)) {}
@@ -38,19 +48,73 @@ public:
return *this;
}
- bool operator==(const Image& rhs) const {
- return size == rhs.size &&
- std::equal(data.get(), data.get() + bytes(), rhs.data.get(),
- rhs.data.get() + rhs.bytes());
+ friend bool operator==(const Image& lhs, const Image& rhs) {
+ return std::equal(lhs.data.get(), lhs.data.get() + lhs.bytes(),
+ rhs.data.get(), rhs.data.get() + rhs.bytes());
+ }
+
+ friend bool operator!=(const Image& lhs, const Image& rhs) {
+ return !(lhs == rhs);
}
bool valid() const {
return size && data.get() != nullptr;
}
+ template <typename T = Image>
+ T clone() const {
+ T copy(size);
+ std::copy(data.get(), data.get() + bytes(), copy.data.get());
+ return copy;
+ }
+
size_t stride() const { return channels * size.width; }
size_t bytes() const { return stride() * size.height; }
+ void fill(uint8_t value) {
+ std::fill(data.get(), data.get() + bytes(), value);
+ }
+
+ // Copy image data within `rect` from `src` to the rectangle of the same size at `pt`
+ // in `dst`. If the specified bounds exceed the bounds of the source or destination,
+ // throw `std::out_of_range`. Must not be used to move data within a single Image.
+ static void copy(const Image& srcImg, Image& dstImg, const Point<uint32_t>& srcPt, const Point<uint32_t>& dstPt, const Size& size) {
+ if (!srcImg.valid()) {
+ throw std::invalid_argument("invalid source for image copy");
+ }
+
+ if (!dstImg.valid()) {
+ throw std::invalid_argument("invalid destination for image copy");
+ }
+
+ if (size.width > srcImg.size.width ||
+ size.height > srcImg.size.height ||
+ srcPt.x > srcImg.size.width - size.width ||
+ srcPt.y > srcImg.size.height - size.height) {
+ throw std::out_of_range("out of range source coordinates for image copy");
+ }
+
+ if (size.width > dstImg.size.width ||
+ size.height > dstImg.size.height ||
+ dstPt.x > dstImg.size.width - size.width ||
+ dstPt.y > dstImg.size.height - size.height) {
+ throw std::out_of_range("out of range destination coordinates for image copy");
+ }
+
+ const uint8_t* srcData = srcImg.data.get();
+ uint8_t* dstData = dstImg.data.get();
+
+ assert(srcData != dstData);
+
+ for (uint32_t y = 0; y < size.height; y++) {
+ const std::size_t srcOffset = (srcPt.y + y) * srcImg.stride() + srcPt.x * channels;
+ const std::size_t dstOffset = (dstPt.y + y) * dstImg.stride() + dstPt.x * channels;
+ std::copy(srcData + srcOffset,
+ srcData + srcOffset + size.width * channels,
+ dstData + dstOffset);
+ }
+ }
+
Size size;
static constexpr size_t channels = Mode == ImageAlphaMode::Exclusive ? 1 : 4;
std::unique_ptr<uint8_t[]> data;
diff --git a/include/mbgl/util/size.hpp b/include/mbgl/util/size.hpp
index 1af85bcff5..79679a92fb 100644
--- a/include/mbgl/util/size.hpp
+++ b/include/mbgl/util/size.hpp
@@ -13,6 +13,10 @@ public:
constexpr Size(const uint32_t width_, const uint32_t height_) : width(width_), height(height_) {
}
+ constexpr uint32_t area() const {
+ return width * height;
+ }
+
constexpr explicit operator bool() const {
return width > 0 && height > 0;
}