Make sure that when changing from higher to lower bit depth (i.e. perform a division) that we round to the nearest value

This commit is contained in:
Pavel Krajcevski 2013-09-24 17:45:19 -04:00
parent 0cfca89da2
commit 1636232717
2 changed files with 9 additions and 4 deletions

View File

@ -134,7 +134,10 @@ namespace PVRTCC {
return 0xFF;
} else {
uint8 bitsWasted = oldDepth - newDepth;
return val >> bitsWasted;
uint16 v = static_cast<uint16>(val);
v = (v + (1 << (bitsWasted - 1))) >> bitsWasted;
v = ::std::min<uint16>(::std::max<uint16>(0, v), (1 << newDepth) - 1);
return v;
}
}

View File

@ -139,7 +139,7 @@ TEST(Pixel, ChangeChannelBitDepth) {
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 8), 0x87);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 7), 0x43);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 6), 0x21);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 6), 0x22);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 2), 0x2);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 0), 0xFF);
@ -149,7 +149,7 @@ TEST(Pixel, ChangeChannelBitDepth) {
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 8), 0x6D);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 6), 0x1B);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 3), 0x03);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 2), 0x01);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 2), 0x02);
EXPECT_EQ(PVRTCC::Pixel::ChangeBitDepth(val, depth, 0), 0xFF);
}
@ -212,11 +212,13 @@ TEST(Pixel, UnpackRGBA) {
EXPECT_EQ(p.G(), 0xB3);
EXPECT_EQ(p.R(), 0xFE);
p = PVRTCC::Pixel();
uint8 newBitDepth[4] = { 3, 5, 2, 1 }; // A R G B
p.ChangeBitDepth(newBitDepth);
p.UnpackRGBA(rgba);
EXPECT_EQ(p.A(), 0x2);
EXPECT_EQ(p.B(), 0x0);
EXPECT_EQ(p.G(), 0x2);
EXPECT_EQ(p.G(), 0x3);
EXPECT_EQ(p.R(), 0x1f);
}