From a4a289c17761a057673d601363439da023a41a57 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 8 Oct 2013 18:37:38 -0400 Subject: [PATCH] Change the pixel channel size to 16 bits so that our arithmetic operations don't overflow. --- Base/include/Pixel.h | 32 +++++++++++++++++--------------- Base/src/Pixel.cpp | 8 ++++---- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Base/include/Pixel.h b/Base/include/Pixel.h index 4fa9312..bee3134 100644 --- a/Base/include/Pixel.h +++ b/Base/include/Pixel.h @@ -58,17 +58,19 @@ namespace FasTC { -class Pixel : public Vector4 { +class Pixel : public Vector4 { private: + typedef uint16 ChannelType; + typedef Vector4 VectorType; uint8 m_BitDepth[4]; public: - Pixel() : Vector4(0, 0, 0, 0) { + Pixel() : VectorType(0, 0, 0, 0) { for(int i = 0; i < 4; i++) m_BitDepth[i] = 8; } - explicit Pixel(uint32 rgba) : Vector4() { + explicit Pixel(uint32 rgba) : VectorType() { for(int i = 0; i < 4; i++) m_BitDepth[i] = 8; Unpack(rgba); @@ -76,7 +78,7 @@ class Pixel : public Vector4 { Pixel(const uint8 *bits, const uint8 channelDepth[4] = static_cast(0), - uint8 bitOffset = 0) : Vector4() { + uint8 bitOffset = 0) : VectorType() { FromBits(bits, channelDepth, bitOffset); } @@ -104,18 +106,18 @@ class Pixel : public Vector4 { // Changes the bit depth of a single component. See the comment // above for how we do this. - static uint8 ChangeBitDepth(uint8 val, uint8 oldDepth, uint8 newDepth); + static ChannelType ChangeBitDepth(ChannelType val, uint8 oldDepth, uint8 newDepth); - const uint8 &A() const { return X(); } - uint8 &A() { return X(); } - const uint8 &R() const { return Y(); } - uint8 &R() { return Y(); } - const uint8 &G() const { return Z(); } - uint8 &G() { return Z(); } - const uint8 &B() const { return W(); } - uint8 &B() { return W(); } - const uint8 &Component(uint32 idx) const { return vec[idx]; } - uint8 &Component(uint32 idx) { return vec[idx]; } + const ChannelType &A() const { return X(); } + ChannelType &A() { return X(); } + const ChannelType &R() const { return Y(); } + ChannelType &R() { return Y(); } + const ChannelType &G() const { return Z(); } + ChannelType &G() { return Z(); } + const ChannelType &B() const { return W(); } + ChannelType &B() { return W(); } + const ChannelType &Component(uint32 idx) const { return vec[idx]; } + ChannelType &Component(uint32 idx) { return vec[idx]; } void GetBitDepth(uint8 (&outDepth)[4]) const { for(int i = 0; i < 4; i++) { diff --git a/Base/src/Pixel.cpp b/Base/src/Pixel.cpp index 2b047bd..08a5bc4 100644 --- a/Base/src/Pixel.cpp +++ b/Base/src/Pixel.cpp @@ -82,7 +82,7 @@ namespace FasTC { } for(int32 i = 0; i < 4; i++) { - uint8 &channel = Component(i); + ChannelType &channel = Component(i); uint32 depth = m_BitDepth[i]; assert(depth <= 8); @@ -125,7 +125,7 @@ namespace FasTC { uint8 bitIdx = bitOffset; for(int i = 3; i >= 0; i--) { - uint8 val = Component(i); + ChannelType val = Component(i); uint8 depth = m_BitDepth[i]; if(depth + bitIdx > 8) { @@ -146,7 +146,7 @@ namespace FasTC { } } - uint8 Pixel::ChangeBitDepth(uint8 val, uint8 oldDepth, uint8 newDepth) { + Pixel::ChannelType Pixel::ChangeBitDepth(Pixel::ChannelType val, uint8 oldDepth, uint8 newDepth) { assert(newDepth <= 8); assert(oldDepth <= 8); @@ -222,7 +222,7 @@ namespace FasTC { ok = ok && m_BitDepth[i] == depths[i]; uint8 mask = (1 << depths[i]) - 1; - const uint8 c = other.Component(i) & mask; + const ChannelType c = other.Component(i) & mask; ok = ok && (c == (Component(i) & mask)); } return ok;