From 4bbd80aab2043c3a9162448ae6cae572412417de Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Mon, 2 Sep 2013 19:14:31 -0400 Subject: [PATCH] Make sure to preserve bit depth when bilinearly upscaling --- PVRTCEncoder/src/Image.cpp | 28 ++++++++++++++++++++++++++++ PVRTCEncoder/test/ImageTest.cpp | 17 +++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/PVRTCEncoder/src/Image.cpp b/PVRTCEncoder/src/Image.cpp index 60beac4..96b992a 100644 --- a/PVRTCEncoder/src/Image.cpp +++ b/PVRTCEncoder/src/Image.cpp @@ -96,6 +96,17 @@ Image::~Image() { delete [] m_Pixels; } +#ifndef NDEBUG +static bool CompareBitDepths(const uint8 (&depth1)[4], + const uint8 (&depth2)[4]) { + bool ok = true; + for(uint32 i = 0; i < 4; i++) { + ok = ok && depth1[i] == depth2[i]; + } + return ok; +} +#endif + void Image::BilinearUpscale(uint32 times, EWrapMode wrapMode) { const uint32 newWidth = m_Width << times; const uint32 newHeight = m_Height << times; @@ -125,6 +136,23 @@ void Image::BilinearUpscale(uint32 times, EWrapMode wrapMode) { const Pixel &bottomLeft = GetPixel(lowXIdx, highYIdx, wrapMode); const Pixel &bottomRight = GetPixel(highXIdx, highYIdx, wrapMode); + // Make sure the bit depth matches the original... + uint8 bitDepth[4]; + topLeft.GetBitDepth(bitDepth); + p.ChangeBitDepth(bitDepth); +#ifndef NDEBUG + uint8 debugDepth[4]; + + topRight.GetBitDepth(debugDepth); + assert(CompareBitDepths(bitDepth, debugDepth)); + + bottomLeft.GetBitDepth(debugDepth); + assert(CompareBitDepths(bitDepth, debugDepth)); + + bottomRight.GetBitDepth(debugDepth); + assert(CompareBitDepths(bitDepth, debugDepth)); +#endif //NDEBUG + // bilerp each channel.... for(uint32 c = 0; c < 4; c++) { const uint16 left = diff --git a/PVRTCEncoder/test/ImageTest.cpp b/PVRTCEncoder/test/ImageTest.cpp index cf5984f..86d3095 100644 --- a/PVRTCEncoder/test/ImageTest.cpp +++ b/PVRTCEncoder/test/ImageTest.cpp @@ -151,6 +151,13 @@ TEST(Image, BilinearUpscale) { TEST(Image, BilinearUpscaleWrapped) { PVRTCC::Pixel pxs[16]; + + // Make sure that our bit depth is less than full... + for(int i = 0; i < 16; i++) { + const uint8 newBitDepth[4] = { 6, 5, 6, 5 }; + pxs[i].ChangeBitDepth(newBitDepth); + } + for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { pxs[j*4 + i].R() = i*4; @@ -166,6 +173,16 @@ TEST(Image, BilinearUpscaleWrapped) { for(uint32 i = 0; i < img.GetWidth(); i++) { for(uint32 j = 0; j < img.GetHeight(); j++) { const PVRTCC::Pixel &p = img(i, j); + + // First make sure that the bit depth didn't change + uint8 depth[4]; + p.GetBitDepth(depth); + EXPECT_EQ(depth[0], 6); + EXPECT_EQ(depth[1], 5); + EXPECT_EQ(depth[2], 6); + EXPECT_EQ(depth[3], 5); + + // Now make sure that the values are correct. if(i == 0) { EXPECT_EQ(p.R(), 6); } else if(i == 1) {