summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/image_atlas.cpp
blob: b53c2162ea26b3051b43fbc9c8478db8d20e7119 (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
#include <mbgl/renderer/image_atlas.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)
    : pixelRatio(image.pixelRatio),
      textureRect(
        bin.x + padding,
        bin.y + padding,
        bin.w - padding * 2,
        bin.h - padding * 2
      ) {
}

ImageAtlas makeImageAtlas(const ImageMap& images) {
    ImageAtlas result;

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

    std::vector<const style::Image::Impl*> pointers;
    pointers.reserve(images.size());

    std::vector<mapbox::Bin> bins;
    bins.reserve(images.size());

    for (const auto& entry : images) {
        const style::Image::Impl& image = *entry.second;
        pointers.emplace_back(&image);
        bins.emplace_back(pointers.size() - 1,
                          image.image.size.width + 2 * padding,
                          image.image.size.height + 2 * padding);
    }

    mapbox::ShelfPack::PackOptions packOptions;
    packOptions.inPlace = true;
    pack.pack(bins, packOptions);

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

    for (const auto& bin : bins) {
        const style::Image::Impl& image = *pointers.at(bin.id);

        PremultipliedImage::copy(image.image,
                                 result.image,
                                 { 0, 0 },
                                 {
                                    bin.x + padding,
                                    bin.y + padding
                                 },
                                 image.image.size);

        result.positions.emplace(image.id,
                                 ImagePosition { bin, image });
    }

    return result;
}

} // namespace mbgl