summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/image_atlas.cpp
blob: 36da19bbce3b181c9beb05d68494cc5045355a53 (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
#include <mbgl/renderer/image_atlas.hpp>
#include <mbgl/renderer/image_manager.hpp>

#include <mapbox/shelf-pack.hpp>

namespace mbgl {

static constexpr uint32_t padding = 1;

ImagePosition::ImagePosition(const mapbox::Bin& bin, const style::Image::Impl& image, uint32_t version_)
    : pixelRatio(image.pixelRatio),
      paddedRect(bin.x, bin.y, bin.w, bin.h),
      version(version_),
      stretchX(image.stretchX),
      stretchY(image.stretchY),
      content(image.content) {}

const mapbox::Bin& _packImage(mapbox::ShelfPack& pack, const style::Image::Impl& image, ImageAtlas& resultImage, ImageType imageType) {
    const mapbox::Bin& bin = *pack.packOne(-1,
        image.image.size.width + 2 * padding,
        image.image.size.height + 2 * padding);

    resultImage.image.resize({
        static_cast<uint32_t>(pack.width()),
        static_cast<uint32_t>(pack.height())
    });

    PremultipliedImage::copy(image.image,
                             resultImage.image,
                             { 0, 0 },
                             {
                                bin.x + padding,
                                bin.y + padding
                             },
                             image.image.size);
    uint32_t x = bin.x + padding;
    uint32_t y = bin.y + padding;
    uint32_t w = image.image.size.width;
    uint32_t h = image.image.size.height;

    if (imageType == ImageType::Pattern) {
            // Add 1 pixel wrapped padding on each side of the image.
        PremultipliedImage::copy(image.image, resultImage.image, { 0, h - 1 }, { x, y - 1 }, { w, 1 }); // T
        PremultipliedImage::copy(image.image, resultImage.image, { 0,     0 }, { x, y + h }, { w, 1 }); // B
        PremultipliedImage::copy(image.image, resultImage.image, { w - 1, 0 }, { x - 1, y }, { 1, h }); // L
        PremultipliedImage::copy(image.image, resultImage.image, { 0,     0 }, { x + w, y }, { 1, h }); // R
    }
    return bin;
}

namespace {

void populateImagePatches(
    ImagePositions& imagePositions, 
    const ImageManager& imageManager,
    std::vector<ImagePatch>& /*out*/ patches) {
    for (auto& updatedImageVersion : imageManager.updatedImageVersions) {
        const std::string& name = updatedImageVersion.first;
        const uint32_t version = updatedImageVersion.second;
        auto it = imagePositions.find(updatedImageVersion.first);
        if (it != imagePositions.end()) {
            auto& position = it->second;
            if (position.version == version) continue;

            auto updatedImage = imageManager.getSharedImage(name);
            if (updatedImage == nullptr) continue;

            patches.emplace_back(*updatedImage, position.paddedRect);
            position.version = version;
        }
    }
}

} // namespace

std::vector<ImagePatch> ImageAtlas::getImagePatchesAndUpdateVersions(const ImageManager& imageManager) {
    std::vector<ImagePatch> imagePatches;
    populateImagePatches(iconPositions, imageManager, imagePatches);
    populateImagePatches(patternPositions, imageManager, imagePatches);
    return imagePatches;
}

ImageAtlas makeImageAtlas(const ImageMap& icons, const ImageMap& patterns, const std::unordered_map<std::string, uint32_t>& versionMap) {
    ImageAtlas result;

    mapbox::ShelfPack::ShelfPackOptions options;
    options.autoResize = true;
    mapbox::ShelfPack pack(0, 0, options);

    for (const auto& entry : icons) {
        const style::Image::Impl& image = *entry.second;
        const mapbox::Bin& bin = _packImage(pack, image, result, ImageType::Icon);
        auto it = versionMap.find(entry.first);
        auto version = it != versionMap.end() ? it->second : 0;
        result.iconPositions.emplace(image.id, ImagePosition { bin, image, version });
    }

    for (const auto& entry : patterns) {
        const style::Image::Impl& image = *entry.second;
        const mapbox::Bin& bin = _packImage(pack, image, result, ImageType::Pattern);
        auto it = versionMap.find(entry.first);
        auto version = it != versionMap.end() ? it->second : 0;
        result.patternPositions.emplace(image.id, ImagePosition { bin, image, version });
    }

    pack.shrink();
    result.image.resize({
        static_cast<uint32_t>(pack.width()),
        static_cast<uint32_t>(pack.height())
    });

    return result;
}

} // namespace mbgl