天天看点

全网首发:把一个bit数组矩阵旋转90度

接上文。 https://quantum6.blog.csdn.net/article/details/110849585 将数组旋转90度:

static char* rotate_90(char* pBuffer, int w, int h, int pitch)
{
    int i=0;
    char* pRotated;
 
    int size;
    int offset=0;
 
    if (w < h)
    {
        w = h;
    }
    else if (w > h)
    {
        w = h;
    }
 
 
    size = h * pitch;
    pRotated = (char*)malloc(size);
    memset(pRotated, 0, size);
 
    int startPos = (h - 1) * pitch*8;
    i = 0;
    for (int x = 0; x < w; x++)
    {
        int offset = startPos;
        for (int y = h - 1; y >= 0; y--)
        {
            int dstPos = i/w*pitch*8+i%w;
            int dstPosByte  = (dstPos)/8;
            int dstPosBit   = (dstPos)%8;
 
            int srcPos      = (offset + x);
            int srcPosByte  = srcPos / 8;
            int srcPosBit   = srcPos % 8;
            int srcBitValue = (pBuffer[srcPosByte] & (0x01 << srcPosBit)) != 0 ? 1 : 0;
            pRotated[dstPosByte] |= (srcBitValue << dstPosBit);
            i++;
            offset -= pitch*8;
        }
    }
    dumpBit(pRotated, h, w, pitch);
    return pRotated;
}      

结果:

原矩阵:
|              |
|              |
|              |
|              |
|              |
|              |
|1111111111111 |
|              |
|              |
|              |
|              |
|              |
|              |
|              |
 
旋转矩阵:
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|       1      |
|              |      

继续阅读