summaryrefslogtreecommitdiff
path: root/src/mbgl/sprite/sprite_parser.cpp
blob: 59dce9324195ba7ab71fffe5b0df94fec7c6822b (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <mbgl/sprite/sprite_parser.hpp>
#include <mbgl/sprite/sprite_image.hpp>

#include <mbgl/platform/log.hpp>

#include <mbgl/util/image.hpp>
#include <mbgl/util/rapidjson.hpp>

#include <cmath>
#include <limits>
#include <sstream>

namespace mbgl {

SpriteImagePtr createSpriteImage(const PremultipliedImage& image,
                                 const uint16_t srcX,
                                 const uint16_t srcY,
                                 const uint16_t srcWidth,
                                 const uint16_t srcHeight,
                                 const double ratio,
                                 const bool sdf) {
    // Disallow invalid parameter configurations.
    if (srcWidth == 0 || srcHeight == 0 || ratio <= 0 || ratio > 10 || srcWidth > 1024 ||
        srcHeight > 1024) {
        Log::Warning(Event::Sprite, "Can't create sprite with invalid metrics");
        return nullptr;
    }

    const uint16_t width = std::ceil(double(srcWidth) / ratio);
    const uint16_t dstWidth = std::ceil(width * ratio);
    assert(dstWidth >= srcWidth);
    const uint16_t height = std::ceil(double(srcHeight) / ratio);
    const uint16_t dstHeight = std::ceil(height * ratio);
    assert(dstHeight >= srcHeight);

    std::string data(dstWidth * dstHeight * 4, '\0');

    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(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.width + srcX;
        for (uint16_t x = 0; x < maxX; ++x) {
            dstData[dstRow + x] = srcData[srcRow + x];
        }
    }

    return std::make_unique<const SpriteImage>(width, height, ratio, std::move(data), sdf);
}

namespace {

inline uint16_t getUInt16(const JSValue& value, const char* name, const uint16_t def = 0) {
    if (value.HasMember(name)) {
        auto& v = value[name];
        if (v.IsUint() && v.GetUint() <= std::numeric_limits<uint16_t>::max()) {
            return v.GetUint();
        } else {
            Log::Warning(Event::Sprite, "Value of '%s' must be an integer between 0 and 65535",
                         name);
        }
    }

    return def;
}

inline double getDouble(const JSValue& value, const char* name, const double def = 0) {
    if (value.HasMember(name)) {
        auto& v = value[name];
        if (v.IsNumber()) {
            return v.GetDouble();
        } else {
            Log::Warning(Event::Sprite, "Value of '%s' must be a number", name);
        }
    }

    return def;
}

inline bool getBoolean(const JSValue& value, const char* name, const bool def = false) {
    if (value.HasMember(name)) {
        auto& v = value[name];
        if (v.IsBool()) {
            return v.GetBool();
        } else {
            Log::Warning(Event::Sprite, "Value of '%s' must be a boolean", name);
        }
    }

    return def;
}

} // namespace

SpriteParseResult parseSprite(const std::string& image, const std::string& json) {
    Sprites sprites;
    PremultipliedImage raster;

    try {
        raster = decodeImage(image);
    } catch (...) {
        return std::string("Could not parse sprite image");
    }

    JSDocument doc;
    doc.Parse<0>(json.c_str());

    if (doc.HasParseError()) {
        std::stringstream message;
        message << "Failed to parse JSON: " << rapidjson::GetParseError_En(doc.GetParseError()) << " at offset " << doc.GetErrorOffset();
        return message.str();
    } else if (!doc.IsObject()) {
        return std::string("Sprite JSON root must be an object");
    } else {
        for (JSValue::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr) {
            const std::string name = { itr->name.GetString(), itr->name.GetStringLength() };
            const JSValue& value = itr->value;

            if (value.IsObject()) {
                const uint16_t x = getUInt16(value, "x", 0);
                const uint16_t y = getUInt16(value, "y", 0);
                const uint16_t width = getUInt16(value, "width", 0);
                const uint16_t height = getUInt16(value, "height", 0);
                const double pixelRatio = getDouble(value, "pixelRatio", 1);
                const bool sdf = getBoolean(value, "sdf", false);

                auto sprite = createSpriteImage(raster, x, y, width, height, pixelRatio, sdf);
                if (sprite) {
                    sprites.emplace(name, sprite);
                }
            }
        }
    }

    return sprites;
}

} // namespace mbgl