From 9dc23db287bf61d65744561320231d1fe611fc06 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 18 Feb 2014 13:25:29 -0500 Subject: [PATCH] Add YCoCg pixel type --- Base/include/Pixel.h | 24 +++++++++++++++++++++++- Base/src/Pixel.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Base/include/Pixel.h b/Base/include/Pixel.h index 31833b9..2f02b64 100644 --- a/Base/include/Pixel.h +++ b/Base/include/Pixel.h @@ -59,7 +59,7 @@ namespace FasTC { class Pixel : public Vector4 { - private: + protected: typedef uint16 ChannelType; typedef Vector4 VectorType; uint8 m_BitDepth[4]; @@ -159,6 +159,28 @@ class Pixel : public Vector4 { }; REGISTER_VECTOR_TYPE(Pixel); +class YCoCgPixel : public Pixel { + private: + void ToYCoCg(); + + public: + YCoCgPixel() : Pixel() { } + explicit YCoCgPixel(uint32 rgba) : Pixel(rgba) { ToYCoCg(); } + explicit YCoCgPixel(const Pixel &p) : Pixel(p) { ToYCoCg(); } + + Pixel ToRGBA() const; + + float ToIntensity() const { return ConvertChannelToFloat(R(), 8); } + uint32 Pack() const { return ToRGBA().Pack(); } + void Unpack(uint32 rgba) { Pixel::Unpack(rgba); ToYCoCg(); } + + const ChannelType &Co() const { return Z(); } + ChannelType &Co() { return Z(); } + const ChannelType &Cg() const { return W(); } + ChannelType &Cg() { return W(); } +}; +REGISTER_VECTOR_TYPE(YCoCgPixel); + } // namespace FasTC #endif // BASE_INCLUDE_PIXEL_H_ diff --git a/Base/src/Pixel.cpp b/Base/src/Pixel.cpp index 8364dd2..7806292 100644 --- a/Base/src/Pixel.cpp +++ b/Base/src/Pixel.cpp @@ -56,6 +56,11 @@ #include #include +template +static inline T Clamp(const T &v, const T &_min, const T &_max) { + return std::max(_min, std::min(v, _max)); +} + namespace FasTC { void Pixel::FromBits(const uint8 *bits, @@ -260,4 +265,30 @@ namespace FasTC { return ok; } + void YCoCgPixel::ToYCoCg() { + int16 Y = ((R() + (G() << 1) + B()) + 2) >> 2; + int16 Co = (R() - B() + 1) >> 1; + int16 Cg = ((-R() + (G() << 1) - B()) + 2) >> 2; + + this->Y() = Clamp(Y, 0, 255); + this->Co() = Clamp(Co + 128, 0, 255); + this->Cg() = Clamp(Cg + 128, 0, 255); + } + + Pixel YCoCgPixel::ToRGBA() const { + int16 Co = this->Co() - 128; + int16 Cg = this->Cg() - 128; + + int16 R = Y() + (Co - Cg); + int16 G = Y() + Cg; + int16 B = Y() - (Co + Cg); + + Pixel p; + p.R() = Clamp(R, 0, 255); + p.G() = Clamp(G, 0, 255); + p.B() = Clamp(B, 0, 255); + p.A() = A(); + return p; + } + } // namespace FasTC