Reverse byte order (and interpret the data as a 64 bit word) for PVRTC blocks

This commit is contained in:
Pavel Krajcevski 2013-09-02 19:13:53 -04:00
parent df81c73456
commit 3f4ffa61b5
3 changed files with 27 additions and 17 deletions

View File

@ -61,7 +61,7 @@ namespace PVRTCC {
: m_ColorACached(false)
, m_ColorBCached(false) {
assert(data);
memcpy(m_ByteData, data, sizeof(m_ByteData));
m_LongData = *(reinterpret_cast<const uint64 *>(data));
}
Pixel Block::GetColorA() {
@ -69,11 +69,16 @@ namespace PVRTCC {
return m_ColorA;
}
bool isOpaque = static_cast<bool>(m_ByteData[0] & 0x80);
bool isOpaque = static_cast<bool>((m_LongData >> 63) & 0x1);
const uint8 opaqueBitDepths[4] = { 0, 5, 5, 5 };
const uint8 transBitDepths[4] = { 3, 4, 4, 4 };
const uint8 *bitDepths = isOpaque? opaqueBitDepths : transBitDepths;
m_ColorA = Pixel(m_ByteData, isOpaque? opaqueBitDepths : transBitDepths, 1);
uint8 pixelBytes[2];
pixelBytes[0] = (m_LongData >> 56) & 0xFF;
pixelBytes[1] = (m_LongData >> 48) & 0xFF;
m_ColorA = Pixel(pixelBytes, bitDepths, 1);
m_ColorACached = true;
return m_ColorA;
}
@ -83,12 +88,16 @@ namespace PVRTCC {
return m_ColorB;
}
bool isOpaque = static_cast<bool>(m_ByteData[2] & 0x80);
bool isOpaque = static_cast<bool>((m_LongData >> 47) & 0x1);
const uint8 opaqueBitDepths[4] = { 0, 5, 5, 4 };
const uint8 transBitDepths[4] = { 3, 4, 4, 3 };
const uint8 *bitDepths = isOpaque? opaqueBitDepths : transBitDepths;
m_ColorB = Pixel(m_ByteData + 2, bitDepths, 1);
uint8 pixelBytes[2];
pixelBytes[0] = (m_LongData >> 40) & 0xFF;
pixelBytes[1] = (m_LongData >> 32) & 0xFF;
m_ColorB = Pixel(pixelBytes, bitDepths, 1);
m_ColorBCached = true;
return m_ColorB;
}
@ -97,9 +106,7 @@ namespace PVRTCC {
assert(texelIdx >= 0);
assert(texelIdx <= 15);
const uint8 texel = 15 - texelIdx;
const uint8 *texelData = m_ByteData + 4;
return (texelData[texel / 4] >> (2 * (3 - (texel % 4)))) & 0x3;
return (m_LongData >> (texelIdx * 2)) & 0x3;
}
} // namespace PVRTCC

View File

@ -67,7 +67,7 @@ class Block {
Pixel GetColorB();
bool GetModeBit() const {
return static_cast<bool>(m_ByteData[3] & 0x1);
return static_cast<bool>((m_LongData >> 32) & 0x1);
}
// Returns the modulation value for the texel in the 4x4 block. The texels are
@ -79,7 +79,10 @@ class Block {
uint8 GetLerpValue(uint32 texelIdx) const;
private:
uint8 m_ByteData[8];
union {
uint8 m_ByteData[8];
uint64 m_LongData;
};
bool m_ColorACached;
Pixel m_ColorA;

View File

@ -60,7 +60,7 @@ TEST(Block, ConstructorFromBytes) {
}
TEST(Block, CopyConstructor) {
const uint8 data[8] = { 0, 0, 0, 0, 0xAA, 0xAA, 0xAA, 0xAA };
const uint8 data[8] = { 0xAA, 0xAA, 0xAA, 0xAA, 0, 0, 0, 0 };
PVRTCC::Block b (data);
PVRTCC::Block b2 (b);
for(int i = 0; i < 16; i++) {
@ -71,7 +71,7 @@ TEST(Block, CopyConstructor) {
TEST(Block, GetColorA) {
// Test a 555 opaque block
uint8 data[8] = { 0x96, 0x8A, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
uint8 data[8] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8A, 0x96 };
PVRTCC::Block b(data);
PVRTCC::Pixel p = b.GetColorA();
EXPECT_EQ(p.A(), 255);
@ -90,7 +90,7 @@ TEST(Block, GetColorA) {
}
// Test a 3444 transparent block
data[0] = 0x16;
data[7] = 0x16;
b = PVRTCC::Block(data);
p = b.GetColorA();
EXPECT_EQ(p.A(), 1);
@ -111,7 +111,7 @@ TEST(Block, GetColorA) {
TEST(Block, GetColorB) {
// Test a 554 opaque block
uint8 data[8] = { 0x0, 0x0, 0x96, 0x8A, 0x0, 0x0, 0x0, 0x0 };
uint8 data[8] = { 0x0, 0x0, 0x0, 0x0, 0x8A, 0x96, 0x0, 0x0 };
PVRTCC::Block b(data);
PVRTCC::Pixel p = b.GetColorB();
EXPECT_EQ(p.A(), 255);
@ -132,7 +132,7 @@ TEST(Block, GetColorB) {
}
// Test a 3443 transparent block
data[2] = 0x16;
data[5] = 0x16;
b = PVRTCC::Block(data);
p = b.GetColorB();
EXPECT_EQ(p.A(), 1);
@ -151,7 +151,7 @@ TEST(Block, GetColorB) {
}
TEST(Block, GetModeBit) {
const uint8 modeData[8] = { 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0 };
const uint8 modeData[8] = { 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0 };
const uint8 noModeData[8] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
PVRTCC::Block b (modeData);
@ -162,7 +162,7 @@ TEST(Block, GetModeBit) {
}
TEST(Block, GetLerpValue) {
const uint8 data[8] = { 0x0, 0x0, 0x0, 0x0, 0x1B, 0xE4, 0x27, 0xD8 };
const uint8 data[8] = { 0xD8, 0x27, 0xE4, 0x1B, 0x0, 0x0, 0x0, 0x0 };
PVRTCC::Block b(data);
EXPECT_EQ(b.GetLerpValue(0), 0);