-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel.hpp
169 lines (124 loc) · 2.77 KB
/
pixel.hpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef PPMPP_PIXEL_HPP
#define PPMPP_PIXEL_HPP
#include <cstdint>
#include <type_traits>
#include <limits>
#include <optional>
namespace ppmpp {
template<typename P>
class image;
class bw_pixel {
public:
using type = bool;
bw_pixel() : value(false) {}
bw_pixel(bool value) : value(value) {}
const bool& operator[](size_t idx) const {
if (idx != 0)
out_of_bounds();
return value;
}
bool& operator[](size_t idx) {
if (idx != 0)
out_of_bounds();
return value;
}
bool value;
private:
static void out_of_bounds() {
throw std::out_of_range("Black and white pixel value index out of range");
}
};
template<typename T>
class grayscale_pixel {
public:
static_assert(std::is_unsigned_v<T>);
using type = T;
grayscale_pixel() : value(0) {}
grayscale_pixel(T value) : value(value) {}
const T& operator[](size_t idx) const {
if (idx != 0)
out_of_bounds();
return value;
}
T& operator[](size_t idx) {
if (idx != 0)
out_of_bounds();
return value;
}
T value;
private:
static void out_of_bounds() {
throw std::out_of_range("Grayscale pixel value index out of range");
}
};
using grayscale8_pixel = grayscale_pixel<uint8_t>;
using grayscale16_pixel = grayscale_pixel<uint16_t>;
template<typename T>
class rgb_pixel {
public:
static_assert(std::is_unsigned_v<T>);
using type = T;
rgb_pixel() : r(0), g(0), b(0) {}
rgb_pixel(T r, T g, T b) : r(r), g(g), b(b) {}
const T& operator[](size_t idx) const {
switch (idx) {
case 0:
return r;
case 1:
return g;
case 2:
return b;
default:
out_of_bounds();
}
}
T& operator[](size_t idx) {
switch (idx) {
case 0:
return r;
case 1:
return g;
case 2:
return b;
default:
out_of_bounds();
}
}
T r;
T g;
T b;
private:
[[noreturn]]
static void out_of_bounds() {
throw std::out_of_range("RGB value index out of range");
}
};
using rgb8_pixel = rgb_pixel<uint8_t>;
using rgb16_pixel = rgb_pixel<uint16_t>;
namespace detail {
template<typename P>
class pixel_traits {};
template<>
struct pixel_traits<bw_pixel> {
public:
static constexpr int ID = 1;
static constexpr size_t ELEMENT_COUNT = 1;
static constexpr std::optional<typename bw_pixel::type> MAX_VALUE = {};
};
template<typename T>
struct pixel_traits<grayscale_pixel<T>> {
public:
static constexpr int ID = 2;
static constexpr size_t ELEMENT_COUNT = 1;
static constexpr std::optional<typename grayscale_pixel<T>::type> MAX_VALUE = std::numeric_limits<typename grayscale_pixel<T>::type>::max();
};
template<typename T>
struct pixel_traits<rgb_pixel<T>> {
public:
static constexpr int ID = 3;
static constexpr size_t ELEMENT_COUNT = 3;
static constexpr std::optional<typename grayscale_pixel<T>::type> MAX_VALUE = std::numeric_limits<typename rgb_pixel<T>::type>::max();
};
}
}
#endif