-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.h
62 lines (49 loc) · 1.32 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
#pragma once
#include <span>
#include <string>
#include <vector>
struct S32Vec2 {
int x;
int y;
};
struct FVec2 {
float x;
float y;
};
struct U16Vec2 {
uint16_t x;
uint16_t y;
};
class Image {
uint32_t *data_ = nullptr;
int width = 0, height = 0;
public:
Image() {}
Image(std::span<const uint8_t> data);
Image(const std::string &path);
Image(int width, int height);
Image(S32Vec2 size) : Image(size.x, size.y) {}
Image(const char *data, size_t size);
Image(Image &&other) noexcept;
Image(const Image &) = delete;
Image &operator=(const Image &) = delete;
~Image();
void save_png(const std::string &path);
const void *get_png(int *len);
S32Vec2 size() const { return {width, height}; }
uint32_t *data() { return data_; }
const uint32_t *data() const { return data_; }
uint32_t operator()(int x, int y) const { return data_[x + y * width]; }
uint32_t &operator()(int x, int y) { return data_[x + y * width]; }
bool operator==(const Image &other) const {
if (width != other.width || height != other.height)
return false;
for (int i = 0; i < width * height; ++i) {
if (data_[i] != other.data_[i])
return false;
}
return true;
}
static void save_png_from_data(const std::string &path, const void *data,
int w, int h);
};