summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/image_atlas.cpp
blob: 8eee7c20951c093d5d2cf638e186bd22f26289d1 (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
#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);

    for (const auto& entry : images) {
        const style::Image::Impl& image = *entry.second;

        const mapbox::Bin& bin = *pack.packOne(-1,
            image.image.size.width + 2 * padding,
            image.image.size.height + 2 * padding);

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

        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 });
    }

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

    return result;
}

} // namespace mbgl