summaryrefslogtreecommitdiff
path: root/kms++util/src/color.cpp
blob: 2e6f217e8fd723c79298fe639bc6b6ce6bceb7f0 (plain)
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
#include <kms++util/color.h>

namespace kms
{
RGB::RGB()
{
	r = g = b = 0;
	a = 255;
}

RGB::RGB(uint8_t r, uint8_t g, uint8_t b)
	:RGB(255, r, g, b)
{
}

RGB::RGB(uint8_t a, uint8_t r, uint8_t g, uint8_t b)
{
	this->r = r;
	this->g = g;
	this->b = b;
	this->a = a;
}

RGB::RGB(uint32_t argb)
{
	this->b = (argb >> 0) & 0xff;
	this->g = (argb >> 8) & 0xff;
	this->r = (argb >> 16) & 0xff;
	this->a = (argb >> 24) & 0xff;
}

uint32_t RGB::rgb888() const
{
	return (r << 16) | (g << 8) | (b << 0);
}

uint32_t RGB::bgr888() const
{
	return (b << 16) | (g << 8) | (r << 0);
}

uint32_t RGB::argb8888() const
{
	return (a << 24) | (r << 16) | (g << 8) | (b << 0);
}

uint32_t RGB::abgr8888() const
{
	return (a << 24) | (b << 16) | (g << 8) | (r << 0);
}

uint16_t RGB::rgb565() const
{
	return ((r >> 3) << 11) | ((g >> 2) << 5) | ((b >> 3) << 0);
}

uint16_t RGB::bgr565() const
{
	return ((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0);
}

YUV RGB::yuv(YUVType type) const
{
	return YUV(*this, type);
}

#define CF_ONE (256)
#define CF(a, b, c) { ((int) ((a) * CF_ONE)), ((int) ((b) * CF_ONE)), ((int) ((c) * CF_ONE)) }
#define CLAMP(a) ((a) > (CF_ONE-1) ? (CF_ONE-1) : (a) < 0 ? 0 : (a))

const int YUVcoef[static_cast<unsigned>(YUVType::MAX)][3][3] = {
	[static_cast<unsigned>(YUVType::BT601_Lim)] = {
		CF( 0.257,  0.504,  0.098),
		CF(-0.148, -0.291,  0.439),
		CF( 0.439, -0.368, -0.071) },
	[static_cast<unsigned>(YUVType::BT601_Full)] = {
		CF( 0.299,  0.587,  0.114),
		CF(-0.169, -0.331,  0.500),
		CF( 0.500, -0.419, -0.081) },
	[static_cast<unsigned>(YUVType::BT709_Lim)] = {
		CF( 0.1826,  0.6142,  0.0620),
		CF(-0.1006, -0.3386,  0.4392),
		CF( 0.4392, -0.3989, -0.0403) },
	[static_cast<unsigned>(YUVType::BT709_Full)] = {
		CF( 0.2126,  0.7152,  0.0722),
		CF(-0.1146, -0.3854,  0.5000),
		CF( 0.5000, -0.4542, -0.0468) },
};

const int YUVoffset[static_cast<unsigned>(YUVType::MAX)][3] = {
	[static_cast<unsigned>(YUVType::BT601_Lim)]  = CF(0.0625,  0.5,  0.5),
	[static_cast<unsigned>(YUVType::BT601_Full)] = CF(     0,  0.5,  0.5),
	[static_cast<unsigned>(YUVType::BT709_Lim)]  = CF(0.0625,  0.5,  0.5),
	[static_cast<unsigned>(YUVType::BT709_Full)] = CF(     0,  0.5,  0.5),
};

YUV::YUV()
{
	y = u = v = a = 0;
}

YUV::YUV(uint8_t y, uint8_t u, uint8_t v)
{
	this->y = y;
	this->u = u;
	this->v = v;
	this->a = 0;
}

static inline
uint8_t MAKE_YUV_Y(uint8_t r, uint8_t g, uint8_t b, YUVType type)
{
	unsigned tidx = static_cast<unsigned>(type);

	return CLAMP(((YUVcoef[tidx][0][0] * r + YUVcoef[tidx][0][1] * g +
		      YUVcoef[tidx][0][2] * b + CF_ONE/2) / CF_ONE) +
		     YUVoffset[tidx][0]);
}

static inline
uint8_t MAKE_YUV_U(uint8_t r, uint8_t g, uint8_t b, YUVType type)
{
	unsigned tidx = static_cast<unsigned>(type);

	return CLAMP(((YUVcoef[tidx][1][0] * r + YUVcoef[tidx][1][1] * g +
		       YUVcoef[tidx][1][2] * b + CF_ONE/2) / CF_ONE) +
		     YUVoffset[tidx][1]);
}

static inline
uint8_t MAKE_YUV_V(uint8_t r, uint8_t g, uint8_t b, YUVType type)
{
	unsigned tidx = static_cast<unsigned>(type);

	return CLAMP(((YUVcoef[tidx][2][0] * r + YUVcoef[tidx][2][1] * g +
		       YUVcoef[tidx][2][2] * b + CF_ONE/2) / CF_ONE) +
		     YUVoffset[tidx][2]);
}

YUV::YUV(const RGB& rgb, YUVType type)
{
	this->y = MAKE_YUV_Y(rgb.r, rgb.g, rgb.b, type);
	this->u = MAKE_YUV_U(rgb.r, rgb.g, rgb.b, type);
	this->v = MAKE_YUV_V(rgb.r, rgb.g, rgb.b, type);
	this->a = rgb.a;
}
}