summaryrefslogtreecommitdiff
path: root/include/mbgl/util/image.hpp
blob: 3dbab27f41d0e8035764b9da09e6f20cb3a41e3e (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
#pragma once

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/size.hpp>

#include <string>
#include <memory>
#include <algorithm>

namespace mbgl {

enum ImageAlphaMode {
    Unassociated,
    Premultiplied
};

template <ImageAlphaMode Mode>
class Image : private util::noncopyable {
public:
    Image() = default;

    Image(Size size_)
        : size(std::move(size_)),
          data(std::make_unique<uint8_t[]>(bytes())) {}

    Image(Size size_, std::unique_ptr<uint8_t[]> data_)
        : size(std::move(size_)),
          data(std::move(data_)) {}

    Image(Image&& o)
        : size(o.size),
          data(std::move(o.data)) {}

    Image& operator=(Image&& o) {
        size = o.size;
        data = std::move(o.data);
        return *this;
    }

    bool operator==(const Image& rhs) const {
        return size == rhs.size &&
               std::equal(data.get(), data.get() + bytes(), rhs.data.get(),
                          rhs.data.get() + rhs.bytes());
    }

    bool valid() const {
        return size && data.get() != nullptr;
    }

    size_t stride() const { return static_cast<size_t>(size.width) * 4; }
    size_t bytes() const { return stride() * size.height; }

    Size size;
    std::unique_ptr<uint8_t[]> data;
};

using UnassociatedImage = Image<ImageAlphaMode::Unassociated>;
using PremultipliedImage = Image<ImageAlphaMode::Premultiplied>;

// TODO: don't use std::string for binary data.
PremultipliedImage decodeImage(const std::string&);
std::string encodePNG(const PremultipliedImage&);

} // namespace mbgl