我有一個Visual Studio 2008 C++應用程序,其中我接收的位圖(未的圖像)。翻轉的每個位對應於解碼映射上的位置。尋找一種更有效的位域解碼算法
typedef unsigned char BYTE;
const unsigned int COL_COUNT = 8;
const unsigned int ROW_COUNT = 4;
static char g_decode_map[ ROW_COUNT ][ COL_COUNT ] =
{
{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' },
{ 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' },
{ 'q', 'r', 's', 't', 'u', 'v', 'w', 'x' },
{ 'y', 'z', ',', '.', ' ', ':', '-', '+' }
};
// current implementation
void Decode(const BYTE bitmap[ ROW_COUNT ],
const char decode_map[ ROW_COUNT ][ COL_COUNT ],
char decoded[ ROW_COUNT * COL_COUNT ])
{
int found = 0;
for(int i = 0; i < ROW_COUNT; ++i)
{
for(int j = 0; j < COL_COUNT; ++j)
{
if(std::bitset<COL_COUNT>(bitmap[ i ]).test(j))
{
decoded[ found++ ] = g_decode_map[ i ][ COL_COUNT - j - 1 ];
}
}
}
}
int main(int argc, char* argv[])
{
BYTE bitmap[ ROW_COUNT ] = { 0x01, 0x80, 0x00, 0x00 };
// expected output { 'h', 'i' } or { 'i', 'h' } order is unimportant
char decoded[ ROW_COUNT * COL_COUNT + 1 ] = { };
Decode(bitmap, g_decode_map, decoded);
printf("Decoded: %s\r\n", decoded);
return 0;
}
我目前的解碼實現工作正常,但它讓我感到可能有一個更有效的方法來做到這一點。任何人都可以提出更高性能的算法嗎?
COL_COUNT可能不能改變(8位爲一個字節)。但是ROW_COUNT可以在編譯時改變。 – PaulH 2012-04-29 17:02:26
如果你嘗試做一個編譯時間循環,你將不得不使用一個字節數組,並通過引用 – JustinDanielson 2012-04-29 19:16:06