[Maintenance] Subpixels: Differentiate between pixel and subpixel types

Which does have the practical effect of communicating more information
via IntelliSense during regular development!

Part of P0112, funded by [Anonymous] and Blue Bolt.
This commit is contained in:
nmlgc 2020-08-25 14:31:22 +02:00
parent 5a1e16de60
commit b034e606bc
1 changed files with 23 additions and 21 deletions

View File

@ -8,48 +8,50 @@ typedef int subpixel_t;
#define TO_SP(v) \
(v << 4)
inline subpixel_t to_sp(float screen_v) {
return static_cast<subpixel_t>(screen_v * 16.0f);
inline subpixel_t to_sp(float pixel_v) {
return static_cast<subpixel_t>(pixel_v * 16.0f);
}
template <class T> class SubpixelBase {
template <class SubpixelType, class PixelType> class SubpixelBase {
public:
typedef SubpixelBase<SubpixelType, PixelType> SelfType;
// Code generation will require direct access to v, if performing
// arithmetic with a local variable...
T v;
SubpixelType v;
subpixel_t operator -(const SubpixelBase<T> &other) {
SubpixelType operator -(const SelfType &other) {
return (this->v - other.v);
}
void operator +=(float screen_v) {
this->v += static_cast<T>(to_sp(screen_v));
void operator +=(float pixel_v) {
this->v += static_cast<SubpixelType>(to_sp(pixel_v));
}
void operator -=(float screen_v) {
this->v -= static_cast<T>(to_sp(screen_v));
void operator -=(float pixel_v) {
this->v -= static_cast<SubpixelType>(to_sp(pixel_v));
}
// No overloads of `operator =()`, since the class needs to be trivially
// copyable.
void set(float screen_v) {
v = static_cast<T>(to_sp(screen_v));
void set(float pixel_v) {
v = static_cast<SubpixelType>(to_sp(pixel_v));
}
void set(const T &screen_v) {
v = TO_SP(screen_v);
void set(const PixelType &pixel_v) {
v = static_cast<SubpixelType>(TO_SP(pixel_v));
}
T to_pixel() const {
return v >> 4;
PixelType to_pixel() const {
return static_cast<PixelType>(v >> 4);
}
operator T() const {
operator SubpixelType() const {
return v;
}
static T None() {
return to_sp(PIXEL_NONE);
static SubpixelType None() {
return static_cast<SubpixelType>(TO_SP(PIXEL_NONE));
}
};
@ -63,9 +65,9 @@ template <class T> struct SPPointBase {
};
// 16-bit (Q12.4)
typedef SubpixelBase<subpixel_t> Subpixel;
typedef SubpixelBase<subpixel_t, pixel_t> Subpixel;
typedef SPPointBase<Subpixel> SPPoint;
// 8-bit (Q4.4)
typedef SubpixelBase<unsigned char> SubpixelLength8;
typedef SubpixelBase<char> Subpixel8;
typedef SubpixelBase<unsigned char, unsigned char> SubpixelLength8;
typedef SubpixelBase<char, char> Subpixel8;
typedef SPPointBase<Subpixel8> SPPoint8;