From 0188006ce3ddc196e58ef493cd04337046067c75 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Mon, 14 Oct 2013 17:34:19 -0400 Subject: [PATCH] Assert that our width and height are powers of two. --- PVRTCEncoder/src/Compressor.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/PVRTCEncoder/src/Compressor.cpp b/PVRTCEncoder/src/Compressor.cpp index 24d94b8..66b0e15 100644 --- a/PVRTCEncoder/src/Compressor.cpp +++ b/PVRTCEncoder/src/Compressor.cpp @@ -148,10 +148,22 @@ namespace PVRTCC { eExtremaResult_LocalMax }; +#ifndef NDEBUG + template + void AssertPOT(const T &t) { + assert(t & (t - 1) == 0); + } +#else + #define AssertPOT(x) (void)(0) +#endif + static EExtremaResult ComputeLocalExtrema( CompressionLabel *labels, const uint8 *inBuf, const uint32 x, const uint32 y, const uint32 width, const uint32 height) { + AssertPOT(width); + AssertPOT(height); + assert(x < width); assert(y < height); @@ -266,6 +278,10 @@ namespace PVRTCC { static void LabelImageForward(CompressionLabel *labels, const uint8 *inBuf, const uint32 w, const uint32 h) { + + AssertPOT(w); + AssertPOT(h); + for(uint32 j = 0; j < h+3; j++) { for(uint32 i = 0; i < w; i++) { EExtremaResult result = ComputeLocalExtrema(labels, inBuf, i, j % h, w, h); @@ -335,6 +351,10 @@ namespace PVRTCC { static void LabelImageBackward(CompressionLabel *labels, const uint32 w, const uint32 h) { + + AssertPOT(w); + AssertPOT(h); + CompressionLabel *neighbors[5] = { 0 }; for(int32 j = static_cast(h)+2; j >= 0; j--) { for(int32 i = static_cast(w)-1; i >= 0; i--) { @@ -379,6 +399,8 @@ namespace PVRTCC { const uint32 w, const uint32 h) { assert((w % 4) == 0); assert((h % 4) == 0); + AssertPOT(w); + AssertPOT(h); uint32 blocksW = w / 4; uint32 blocksH = h / 4; @@ -526,6 +548,10 @@ namespace PVRTCC { } static void GenerateModulationValues(uint8 *outBuf, const uint8 *inBuf, uint32 w, uint32 h) { + + AssertPOT(w); + AssertPOT(h); + // Start from the beginning block and generate the lerp values for the intermediate values uint64 *outBlocks = reinterpret_cast(outBuf); const uint32 blocksW = w >> 2; @@ -642,6 +668,10 @@ namespace PVRTCC { const uint32 width = cj.width; const uint32 height = cj.height; + // Make sure that width and height are a power of two. + assert(width & (width - 1) == 0); + assert(height & (height - 1) == 0); + memset(cj.outBuf, 0, (width * height / 16) * kBlockSize); CompressionLabel *labels =