Skip to content

Commit c51422c

Browse files
committed
Merge pull request #310 from Mokosha/SpecializeYCoCg
Specialize integer implementation of YCoCg-R to use bitshifts #310
2 parents 387df50 + 2ba1ae9 commit c51422c

File tree

1 file changed

+68
-18
lines changed

1 file changed

+68
-18
lines changed

glm/gtx/color_space_YCoCg.inl

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,84 @@ namespace glm
4646
}
4747

4848
template <typename T, precision P>
49-
GLM_FUNC_QUALIFIER tvec3<T, P> rgb2YCoCgR
49+
GLM_FUNC_QUALIFIER tvec3<T, P> YCoCg2rgb
5050
(
51-
tvec3<T, P> const & rgbColor
51+
tvec3<T, P> const & YCoCgColor
5252
)
5353
{
5454
tvec3<T, P> result;
55-
result.x/*Y */ = rgbColor.g / T(2) + (rgbColor.r + rgbColor.b) / T(4);
56-
result.y/*Co*/ = rgbColor.r - rgbColor.b;
57-
result.z/*Cg*/ = rgbColor.g - (rgbColor.r + rgbColor.b) / T(2);
55+
result.r = YCoCgColor.x + YCoCgColor.y - YCoCgColor.z;
56+
result.g = YCoCgColor.x + YCoCgColor.z;
57+
result.b = YCoCgColor.x - YCoCgColor.y - YCoCgColor.z;
5858
return result;
5959
}
6060

61+
template <typename T, precision P, bool isInteger>
62+
class compute_YCoCgR {
63+
public:
64+
static GLM_FUNC_QUALIFIER tvec3<T, P> rgb2YCoCgR
65+
(
66+
tvec3<T, P> const & rgbColor
67+
)
68+
{
69+
tvec3<T, P> result;
70+
result.x/*Y */ = rgbColor.g / T(2) + (rgbColor.r + rgbColor.b) / T(4);
71+
result.y/*Co*/ = rgbColor.r - rgbColor.b;
72+
result.z/*Cg*/ = rgbColor.g - (rgbColor.r + rgbColor.b) / T(2);
73+
return result;
74+
}
75+
76+
static GLM_FUNC_QUALIFIER tvec3<T, P> YCoCgR2rgb
77+
(
78+
tvec3<T, P> const & YCoCgRColor
79+
)
80+
{
81+
tvec3<T, P> result;
82+
T tmp = YCoCgRColor.x - (YCoCgRColor.z / T(2));
83+
result.g = YCoCgRColor.z + tmp;
84+
result.b = tmp - (YCoCgRColor.y / T(2));
85+
result.r = result.b + YCoCgRColor.y;
86+
return result;
87+
}
88+
};
89+
6190
template <typename T, precision P>
62-
GLM_FUNC_QUALIFIER tvec3<T, P> YCoCg2rgb
91+
class compute_YCoCgR<T, P, true> {
92+
public:
93+
static GLM_FUNC_QUALIFIER tvec3<T, P> rgb2YCoCgR
94+
(
95+
tvec3<T, P> const & rgbColor
96+
)
97+
{
98+
tvec3<T, P> result;
99+
result.y/*Co*/ = rgbColor.r - rgbColor.b;
100+
T tmp = rgbColor.b + (result.y >> 1);
101+
result.z/*Cg*/ = rgbColor.g - tmp;
102+
result.x/*Y */ = tmp + (result.z >> 1);
103+
return result;
104+
}
105+
106+
static GLM_FUNC_QUALIFIER tvec3<T, P> YCoCgR2rgb
107+
(
108+
tvec3<T, P> const & YCoCgRColor
109+
)
110+
{
111+
tvec3<T, P> result;
112+
T tmp = YCoCgRColor.x - (YCoCgRColor.z >> 1);
113+
result.g = YCoCgRColor.z + tmp;
114+
result.b = tmp - (YCoCgRColor.y >> 1);
115+
result.r = result.b + YCoCgRColor.y;
116+
return result;
117+
}
118+
};
119+
120+
template <typename T, precision P>
121+
GLM_FUNC_QUALIFIER tvec3<T, P> rgb2YCoCgR
63122
(
64-
tvec3<T, P> const & YCoCgColor
123+
tvec3<T, P> const & rgbColor
65124
)
66125
{
67-
tvec3<T, P> result;
68-
result.r = YCoCgColor.x + YCoCgColor.y - YCoCgColor.z;
69-
result.g = YCoCgColor.x + YCoCgColor.z;
70-
result.b = YCoCgColor.x - YCoCgColor.y - YCoCgColor.z;
71-
return result;
126+
return compute_YCoCgR<T, P, std::numeric_limits<T>::is_integer>::rgb2YCoCgR(rgbColor);
72127
}
73128

74129
template <typename T, precision P>
@@ -77,11 +132,6 @@ namespace glm
77132
tvec3<T, P> const & YCoCgRColor
78133
)
79134
{
80-
tvec3<T, P> result;
81-
T tmp = YCoCgRColor.x - (YCoCgRColor.z / T(2));
82-
result.g = YCoCgRColor.z + tmp;
83-
result.b = tmp - (YCoCgRColor.y / T(2));
84-
result.r = result.b + YCoCgRColor.y;
85-
return result;
135+
return compute_YCoCgR<T, P, std::numeric_limits<T>::is_integer>::YCoCgR2rgb(YCoCgRColor);
86136
}
87137
}//namespace glm

0 commit comments

Comments
 (0)