forked from WWU-cptr143-sprouts/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.h
98 lines (92 loc) · 2.12 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
#ifndef IMAGE_H
#define IMAGE_H
#include <cstdint>
#include <cwchar>
#include <string>
#include <stdexcept>
#include <mutex>
#include <memory>
#include "color.h"
#include "stream.h"
using namespace std;
class ImageLoadError final : public runtime_error
{
public:
explicit ImageLoadError(const string &arg)
: runtime_error(arg)
{
}
};
class Image final
{
public:
explicit Image(wstring resourceName);
explicit Image(unsigned w, unsigned h);
explicit Image(Color c);
Image();
Image(nullptr_t)
: Image()
{
}
void setPixel(int x, int y, Color c);
Color getPixel(int x, int y) const;
void bind() const;
static void unbind();
unsigned width() const
{
return data->w;
}
unsigned height() const
{
return data->h;
}
operator bool() const
{
return data != nullptr;
}
bool operator !() const
{
return data == nullptr;
}
friend bool operator ==(Image l, Image r)
{
return l.data == r.data;
}
friend bool operator !=(Image l, Image r)
{
return l.data != r.data;
}
/*void write(Writer &writer, Client &client) const;
static Image read(Reader &reader, Client &client);*/
private:
enum RowOrder
{
TopToBottom,
BottomToTop
};
struct data_t
{
uint8_t * const data;
const unsigned w, h;
RowOrder rowOrder;
uint32_t texture;
size_t graphicsNumber = -1;
bool textureValid;
mutex lock;
data_t(uint8_t * data, unsigned w, unsigned h, RowOrder rowOrder)
: data(data), w(w), h(h), rowOrder(rowOrder), texture(0), textureValid(false)
{
}
data_t(uint8_t * data, shared_ptr<data_t> rt)
: data(data), w(rt->w), h(rt->h), rowOrder(rt->rowOrder), texture(0), textureValid(false)
{
}
~data_t();
};
shared_ptr<data_t> data;
static constexpr size_t BytesPerPixel = 4;
void setRowOrder(RowOrder newRowOrder) const;
void swapRows(unsigned y1, unsigned y2) const;
void copyOnWrite();
};
#endif // IMAGE_H