-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathImage.h
199 lines (193 loc) · 3.05 KB
/
Image.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef IMAGE_H
#define IMAGE_H
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <utility>
#include <atomic>
class RefCounter {
private:
std::atomic_uint32_t ref;
public:
void operator = (RefCounter const &r)
{
ref.store(r.ref.load());
}
operator unsigned int () const
{
return ref;
}
void operator ++ (int)
{
ref++;
}
void operator -- (int)
{
ref--;
}
};
class Image {
public:
enum class Format {
None,
RGB8,
UYVY8,
YUYV8,
UINT8,
UINT16,
UINT32,
};
static int bytesPerPixel(Format f)
{
switch (f) {
case Format::UINT8:
return 1;
case Format::RGB8:
return 3;
case Format::UYVY8:
case Format::YUYV8:
case Format::UINT16:
return 2;
case Format::UINT32:
return 4;
}
return 0;
}
private:
struct Core {
RefCounter ref;
int width = 0;
int height = 0;
enum Format format = Format::RGB8;
char *data() const
{
return (char *)this + sizeof(Core);
}
// char data[0];
};
Core *core_ = nullptr;
void assign(Core *p)
{
if (p) {
p->ref++;
}
if (core_) {
if (core_->ref > 1) {
core_->ref--;
} else {
core_->~Core();
free(core_);
}
}
core_ = p;
}
void copy_on_write()
{
if (core_ && core_->ref > 1) {
Image img = copy();
assign(img.core_);
}
}
public:
Image() = default;
Image(int w, int h, Format format)
{
create(w, h, format);
}
~Image()
{
clear();
}
Image(Image const &t)
{
assign(t.core_);
}
void operator = (Image const &t)
{
assign(t.core_);
}
void clear()
{
assign(nullptr);
}
bool isNull() const
{
return !core_;
}
operator bool () const
{
return !isNull();
}
void create(int w, int h, Format format)
{
size_t datalen = bytesPerPixel(format) * w * h;
Core *p = (Core *)malloc(sizeof(Core) + datalen);
*p = {};
p->width = w;
p->height = h;
p->format = format;
assign(p);
}
int width() const
{
return core_ ? core_->width : 0;
}
int height() const
{
return core_ ? core_->height : 0;
}
Format format() const
{
return core_ ? core_->format : Format::None;
}
int bytesPerPixel() const
{
return bytesPerPixel(format());
}
int bytesPerLine() const
{
return bytesPerPixel() * width();
}
uint8_t const *scanLine(int y) const
{
return core_ ? ((uint8_t const *)core_->data() + bytesPerLine() * y) : nullptr;
}
uint8_t *scanLine(int y)
{
copy_on_write();
return core_ ? ((uint8_t *)core_->data() + bytesPerLine() * y) : nullptr;
}
uint8_t const *bits() const
{
return core_ ? (uint8_t const *)core_->data() : nullptr;
}
uint8_t *bits()
{
copy_on_write();
return core_ ? (uint8_t *)core_->data() : nullptr;
}
Image copy() const
{
Image newimg;
if (core_) {
int w = width();
int h = height();
Format f = format();
newimg.create(w, h, f);
memcpy(newimg.core_->data(), core_->data(), bytesPerPixel(f) * w * h);
}
return newimg;
}
Image convertToFormat(Image::Format dformat) const;
void swap(Image &r)
{
std::swap(core_, r.core_);
}
};
namespace std {
template <> inline void swap<Image>(Image &l, Image &r)
{
l.swap(r);
}
}
#endif // IMAGE_H