From 3dd1444ff62985b0ce0a3befa97d8df5241a92c0 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Tue, 11 Mar 2014 16:48:25 -0400 Subject: [PATCH] Add clamping to our pixels --- Base/include/Pixel.h | 7 +++++++ Base/test/TestPixel.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/Base/include/Pixel.h b/Base/include/Pixel.h index 92aa414..34670d4 100644 --- a/Base/include/Pixel.h +++ b/Base/include/Pixel.h @@ -163,6 +163,13 @@ class Pixel : public Vector4 { // Tests for equality by comparing the values and the bit depths. bool operator==(const Pixel &) const; + + // Clamps the pixel to the range [0,255] + void ClampByte() { + for(uint32 i = 0; i < 4; i++) { + vec[i] = (vec[i] < 0)? 0 : ((vec[i] > 255)? 255 : vec[i]); + } + } }; REGISTER_VECTOR_TYPE(Pixel); diff --git a/Base/test/TestPixel.cpp b/Base/test/TestPixel.cpp index 770b597..abdcc94 100644 --- a/Base/test/TestPixel.cpp +++ b/Base/test/TestPixel.cpp @@ -306,6 +306,16 @@ TEST(Pixel, ScaleColor) { } } +TEST(Pixel, ByteClamp) { + FasTC::Pixel p(256, 3, -2, 10); + p.ClampByte(); + EXPECT_EQ(p.A(), 255); + EXPECT_EQ(p.R(), 3); + EXPECT_EQ(p.G(), 255); + EXPECT_EQ(p.B(), 10); +} + + TEST(YCoCgPixel, Conversion) { FasTC::Pixel p;