summaryrefslogtreecommitdiff
path: root/src/mbgl/style/image_impl.cpp
blob: c43bb552eeddc1a31f0c09d9a8c0bfa2f6c37892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <mbgl/style/image_impl.hpp>
#include <mbgl/util/exception.hpp>

namespace mbgl {
namespace style {

namespace {

bool validateStretch(const ImageStretches& stretches, const float size) {
    if (stretches.empty()) {
        return true;
    }
    float last = 0;
    for (auto& part : stretches) {
        if (part.first < last || part.second < part.first || size < part.second) {
            return false;
        }
        last = part.second;
    }
    return true;
}

bool validateContent(const ImageContent& content, const Size& size) {
    if (content.left < 0 || size.width < content.left) return false;
    if (content.top < 0 || size.height < content.top) return false;
    if (content.right < 0 || size.width < content.right) return false;
    if (content.bottom < 0 || size.height < content.bottom) return false;
    if (content.right < content.left) return false;
    if (content.bottom < content.top) return false;
    return true;
}

} // namespace

Image::Impl::Impl(std::string id_,
                  PremultipliedImage&& image_,
                  const float pixelRatio_,
                  bool sdf_,
                  ImageStretches stretchX_,
                  ImageStretches stretchY_,
                  optional<ImageContent> content_)
    : id(std::move(id_)),
      image(std::move(image_)),
      pixelRatio(pixelRatio_),
      sdf(sdf_),
      stretchX(std::move(stretchX_)),
      stretchY(std::move(stretchY_)),
      content(content_
                  ? std::move(*content_)
                  : ImageContent{0, 0, static_cast<float>(image.size.width), static_cast<float>(image.size.height)}) {
    if (!image.valid()) {
        throw util::StyleImageException("dimensions may not be zero");
    } else if (pixelRatio <= 0) {
        throw util::StyleImageException("pixelRatio may not be <= 0");
    } else if (!validateStretch(stretchX, image.size.width)) {
        throw util::StyleImageException("stretchX is out of bounds or overlapping");
    } else if (!validateStretch(stretchY, image.size.height)) {
        throw util::StyleImageException("stretchY is out of bounds or overlapping");
    } else if (!validateContent(content, image.size)) {
        throw util::StyleImageException("content area is invalid");
    }
}

} // namespace style
} // namespace mbgl