-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.h
135 lines (101 loc) · 3.49 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
#ifndef MAIN_IMAGE_H
#define MAIN_IMAGE_H
#include <string>
#include <functional>
#include "General.h"
constexpr int tileSize = 16;
#pragma region Pixel
struct Pixel
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
Pixel &operator+=(const Pixel &add)
{
if (add.a == 255) { r = add.r; g = add.g; b = add.b; return *this; }
if (add.a == 0)return *this;
double k = (add.a / 255.0);
r = (uint8_t)((add.r - (int)r) * k + r);
g = (uint8_t)((add.g - (int)g) * k + g);
b = (uint8_t)((add.b - (int)b) * k + b);
return *this;
}
inline bool operator< (Pixel const &other)
{
if (r != other.r) return r < other.r;
if (g != other.g) return g < other.g;
if (b != other.b) return b < other.b;
return a < other.a;
}
};
inline bool operator< (const Pixel a, const Pixel b)
{
if (a.r != b.r) return a.r < b.r;
if (a.g != b.g) return a.g < b.g;
if (a.b != b.b) return a.b < b.b;
return a.a < b.a;
}
#pragma endregion
constexpr Pixel backgroundColor{0, 0, 0, 0};
constexpr Pixel WHITE_COLOR{255, 255, 255, 255};
constexpr Pixel KW_COLOR{210, 255, 255, 255};
constexpr Pixel SHADOW_COLOR{52,52,52,255};
constexpr Pixel TRANSP_COLOR {0, 0, 0, 0};
enum class E_ImgRotation { Rot_90 };
struct Image
{
Image() {};
explicit Image(const std::string &a_path);
Image(const std::string &a_path, int scale);
Image(const std::string &a_path, int scale_x, int scale_y);
Image(int a_width, int a_height, int a_channels);
Image(const Image ©, int scale);
Image(const Image ©, int scale_x, int scale_y);
Image(const Image ©, E_ImgRotation rot);
Image(const Image ©);
Image(Image && a) noexcept;
/*Image &operator=(const Image &other)
{
error("oh no...");
return *this;
}*/
Image &operator=(Image &&other) noexcept
{
if (&other == this)return *this;
width = other.width;
height = other.height;
size = other.size;
channels = other.channels;
self_allocated = other.self_allocated;
data = other.data;
other.data = nullptr;
return *this;
}
int Save(const std::string &a_path);
inline int Width() const { return width; }
inline int Height() const { return height; }
inline int Channels() const { return channels; }
inline size_t Size() const { return size; }
inline Pixel* Data() { return data; }
inline Pixel GetPixel(int x, int y) const { return data[width * y + x]; }
inline void MixPixel(int x, int y, const Pixel &pix) { data[width * y + x] += pix; }
inline void SetPixel(int x, int y, const Pixel &pix) { data[width * y + x] = pix; }
void Draw(Image &canvas, Point p, bool flip_OX = false, bool flip_OY = false, bool not_mix = false) const;
void FastDraw(Image &canvas, int lines, int from_line = 0) const;
void Draw(Image &canvas, std::function<Pixel(Pixel)> PixFunc) const;
void Draw(Image &canvas, std::function<Pixel(Point, Pixel)> PixFunc) const;
/// <summary>chnage image : image[x,y] = PixFunc(image[x,y])</summary>
/// <param name="with_save_pixel">if we know that dif color on that image few then we can hash PixFunc result value</param>
void PixelsChange(std::function<Pixel(Pixel)> PixFunc, bool with_hash_pixel);
~Image();
private:
void ImgClone(const Image ©, int scale_x, int scale_y); // for write wo copypast and wo temp obj
int width = -1;
int height = -1;
int channels = 3;
size_t size = 0;
Pixel *data = nullptr;
bool self_allocated = false;
};
#endif //MAIN_IMAGE_H