-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexture.h
106 lines (76 loc) · 1.97 KB
/
texture.h
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
#pragma once
#include "pvl/Vector.hpp"
#include <QImage>
#include <cstdint>
#include <memory>
namespace Mpcv {
enum class ImageFormat {
GRAY,
RGB,
BGR,
RGBA,
BGRA,
};
class ITexture {
public:
virtual ~ITexture() = default;
virtual Pvl::Vec2i size() const = 0;
virtual ImageFormat format() const = 0;
virtual uint8_t* data() = 0;
};
class QtTexture : public ITexture {
QImage image_;
public:
QtTexture(QImage&& image)
: image_(std::move(image)) {
if (image_.format() == QImage::Format_Invalid) {
throw std::runtime_error("Error reading texture image");
}
}
virtual Pvl::Vec2i size() const override {
return Pvl::Vec2i(image_.width(), image_.height());
}
virtual ImageFormat format() const override {
switch (image_.depth()) {
case 32:
return ImageFormat::BGRA;
case 24:
return ImageFormat::BGR;
case 8:
return ImageFormat::GRAY;
default:
throw std::runtime_error("Unknown image depth = " + std::to_string(image_.depth()));
}
}
virtual uint8_t* data() override {
return image_.bits();
}
};
#ifdef HAS_JPEG
class JpegTexture : public ITexture {
uint8_t* data_;
std::size_t width_, height_;
std::size_t channels_;
public:
JpegTexture(const std::string& filename);
~JpegTexture();
virtual Pvl::Vec2i size() const override;
virtual ImageFormat format() const override;
virtual uint8_t* data() override;
};
#endif
#ifdef HAS_PNG
class PngTexture : public ITexture {
uint8_t* data_;
std::size_t width_, height_;
std::size_t channels_;
public:
PngTexture(const std::string& filename);
~PngTexture();
virtual Pvl::Vec2i size() const override;
virtual ImageFormat format() const override;
virtual uint8_t* data() override;
};
#endif
std::unique_ptr<ITexture> makeTexture(const QString& filename);
} // namespace Mpcv