-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.h
148 lines (126 loc) · 4.55 KB
/
bitmap.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
//
// Copyright ( c ) 2014 Philip Wellnitz
//
//
// This file is part of "Rechteckschoner".
//
// "Rechteckschoner" is distributed to be useful, but need not
// to be. Further, it is distributed WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files ( the
// "Software" ), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
#pragma once
#include <vector>
typedef uint8_t u8;
typedef uint16_t u16;
/**
* @brief Struct to store the RGB values of a pixel.
*/
struct pixel {
u8 m_red; ///< R value
u8 m_green; ///< G value
u8 m_blue; ///< B value
u8 m_transparent; ///< pixel is transparent
constexpr pixel( u8 p_red, u8 p_green, u8 p_blue, u8 p_transparent = 0 )
: m_red( p_red ), m_green( p_green ), m_blue( p_blue ), m_transparent( p_transparent ) {
}
constexpr auto operator<=>( const pixel& p_other ) const = default;
constexpr pixel operator^=( const pixel& p_other ) {
u16 tp = 510 - ( this->m_transparent + p_other.m_transparent );
u16 ttp = 255 - ( this->m_transparent );
u16 otp = 255 - ( p_other.m_transparent );
this->m_red = this->m_red * ttp / tp + p_other.m_red * otp / tp;
this->m_green = this->m_green * ttp / tp + p_other.m_green * otp / tp;
this->m_blue = this->m_blue * ttp / tp + p_other.m_blue * otp / tp;
return *this;
}
constexpr pixel operator&=( const pixel& p_other ) {
u16 tp = 255;
u16 otp = 255 - ( p_other.m_transparent );
u16 ttp = p_other.m_transparent;
this->m_red = this->m_red * ttp / tp + p_other.m_red * otp / tp;
this->m_green = this->m_green * ttp / tp + p_other.m_green * otp / tp;
this->m_blue = this->m_blue * ttp / tp + p_other.m_blue * otp / tp;
return *this;
}
constexpr pixel operator+=( const pixel& p_other ) {
this->m_red += p_other.m_red;
this->m_green += p_other.m_green;
this->m_blue += p_other.m_blue;
return *this;
}
constexpr pixel operator-=( const pixel& p_other ) {
this->m_red -= p_other.m_red;
this->m_green -= p_other.m_green;
this->m_blue -= p_other.m_blue;
return *this;
}
constexpr pixel operator*=( u8 p_scale ) {
this->m_red *= p_scale;
this->m_green *= p_scale;
this->m_blue *= p_scale;
return *this;
}
constexpr pixel operator/=( u8 p_scale ) {
this->m_red /= p_scale;
this->m_green /= p_scale;
this->m_blue /= p_scale;
return *this;
}
friend constexpr pixel operator^( pixel p_lhs, const pixel& p_rhs ) {
p_lhs ^= p_rhs;
return p_lhs;
}
friend constexpr pixel operator&( pixel p_lhs, const pixel& p_rhs ) {
p_lhs &= p_rhs;
return p_lhs;
}
friend constexpr pixel operator+( pixel p_lhs, const pixel& p_rhs ) {
p_lhs += p_rhs;
return p_lhs;
}
friend constexpr pixel operator-( pixel p_lhs, const pixel& p_rhs ) {
p_lhs -= p_rhs;
return p_lhs;
}
friend constexpr pixel operator*( pixel p_lhs, u8 p_rhs ) {
p_lhs *= p_rhs;
return p_lhs;
}
friend constexpr pixel operator/( pixel p_lhs, u8 p_rhs ) {
p_lhs /= p_rhs;
return p_lhs;
}
};
#define SCALE( x, mx ) \
( ( ( x ) < 0 ) ? 0 : ( (int) ( 256.0 * ( (double) ( x ) / (double) ( mx ) ) ) ) )
/**
* @brief Struct to hold a bitmap.
*/
struct bitmap {
private:
std::vector<std::vector<pixel>> m_pixels; ///< The raw data
public:
size_t m_width; ///< The image width
size_t m_height; ///< The image height
bitmap( size_t p_width, size_t p_height );
bitmap( const char* p_path );
pixel& operator( )( size_t p_x, size_t p_y );
pixel operator( )( size_t p_x, size_t p_y ) const;
/**
* @param p_path String containing the path to the file to write the file to
* @return Non-zero if an error occured.
*/
int writeToFile( const char* p_path ) const;
};