吾现在有一个一维数组,试图转换为bit。
数组为14x14:
static char word_bitmap[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
转换为bit数组14x2:
//picth 为一行的字节数 static char* byteToBit(char* pBuffer, int w, int h, int pitch) { int i=0; int j=0; int size = h * pitch; char* bits = (char*)malloc(size); memset(bits, 0, size); char* pRaw = bits; for (i=0; i<h; i++) { for (j=0; j<w; j++) { int srcPos = j; int srcPosByte = srcPos / 8; int srcPosBit = srcPos % 8; int srcBitValue = pBuffer[i*w+j] == 0 ? 0 : 1; bits[srcPosByte] |= ((srcBitValue << srcPosBit)); } bits += pitch; } return pRaw; }
输出验证:
static void dumpBit(char* pBuffer, int w, int h, int pitch) { int i=0; int j=0; printf("w=%d, h=%d\n", w, h); for (i=0; i<h; i++) { printf("|"); for (j=0; j<w; j++) { int srcPos = j; int srcPosByte = srcPos / 8; int srcPosBit = srcPos % 8; int srcBitValue = (pBuffer[srcPosByte] & (0x01 << srcPosBit)) != 0 ? 1 : 0; printf(srcBitValue ? "1" : " "); } pBuffer += pitch; printf("|\n"); } }