-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.hpp
177 lines (133 loc) · 3.65 KB
/
image.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
170
171
172
173
174
175
176
177
#ifndef PPMPP_IMAGE_HPP
#define PPMPP_IMAGE_HPP
#include <stdexcept>
#include <vector>
#include <cstdlib>
#include <utility>
#include <algorithm>
#include <iostream>
#include "pixel.hpp"
namespace ppmpp {
template<typename P>
class image;
template<typename P>
class row {
public:
P& operator[](uint16_t x) const {
check_bounds(x);
return pixels[x];
}
P& operator[](uint16_t x) {
check_bounds(x);
return pixels[x];
}
private:
void check_bounds(uint16_t x) const {
if (x >= width)
throw std::out_of_range("X coordinate is out of bounds");
}
friend class image<P>;
row(P* pixels, size_t width) : pixels(pixels), width(width) {}
P* pixels;
uint16_t width;
};
template<typename P>
class image {
public:
image(uint16_t width, uint16_t height, const P& color = P()) : width(width), height(height) {
pixels.resize((size_t)(width * height), color);
if (pixels.size() == 0)
throw std::domain_error("Width and height of an image has to be non-zero");
}
row<P> operator[](size_t y) {
check_bounds(y);
return row(&pixels[y * width], width);
}
row<const P> operator[](size_t y) const {
check_bounds(y);
return row(&pixels[y * width], width);
}
uint16_t get_width() const {
return width;
}
uint16_t get_height() const {
return height;
}
std::vector<unsigned char> serialize_binary() const {
auto buffer = make_header(true);
if constexpr(std::is_same<bw_pixel, P>::value) {
for (size_t y = 0; y < height; y++) {
size_t done = 0;
while (done < width) {
unsigned char b = 0;
auto n = std::min((size_t)8, width - done);
for (size_t i = 0; i < n; i++, done++)
b |= (pixels[y * width + done].value ? 1 : 0) << (8 - i - 1);
write_binary(buffer, (unsigned char)~b);
}
}
} else {
for (const auto& pixel : pixels) {
for (size_t i = 0; i < detail::pixel_traits<P>::ELEMENT_COUNT; i++)
write_binary(buffer, pixel[i]);
}
}
return std::move(buffer);
}
std::vector<unsigned char> serialize_text() const {
auto buffer = make_header(false);
for (const auto& pixel : pixels) {
for (size_t i = 0; i < detail::pixel_traits<P>::ELEMENT_COUNT; i++) {
write_text(buffer, +pixel[i]);
buffer.push_back((unsigned char)' ');
}
}
return std::move(buffer);
}
private:
std::vector<unsigned char> make_header(bool binary) const {
std::vector<unsigned char> buffer;
buffer.push_back((unsigned char)'P');
write_text(buffer, detail::pixel_traits<P>::ID + (binary ? 3 : 0));
buffer.push_back((unsigned char)' ');
write_text(buffer, width);
buffer.push_back((unsigned char)' ');
write_text(buffer, height);
buffer.push_back((unsigned char)'\n');
if constexpr(detail::pixel_traits<P>::MAX_VALUE) {
write_text(buffer, *detail::pixel_traits<P>::MAX_VALUE);
buffer.push_back((unsigned char)'\n');
}
return std::move(buffer);
}
void check_bounds(uint16_t y) const {
if (y >= height)
throw std::out_of_range("Y coordinate is out of bounds");
}
template<typename T>
static void write_binary(std::vector<unsigned char>& buffer, T n) {
for (size_t i = 0; i < sizeof(n); i++)
buffer.push_back((unsigned char)((n >> (i * 8)) & (T)0xFF));
}
template<typename T>
static void write_text(std::vector<unsigned char>& buffer, T n) {
if (n == 0) {
buffer.push_back((unsigned char)'0');
return;
}
size_t count = 0;
unsigned char digits[128] = { 0 };
while (n != 0) {
digits[count++] = ((unsigned char)(n % 10) + (unsigned char)'0');
n /= 10;
}
buffer.reserve(count);
for (size_t i = 0; i < count; i++)
buffer.push_back(digits[count - i - 1]);
}
uint16_t width;
uint16_t height;
std::vector<P> pixels;
};
}
#endif