0

I have an SDI mfc application in which a bitmap image loaded, i want to rotate that bitmap 90Deg, 180Deg & 270Deg. Am able to rotate it 180 degree. But am stuck at 90 and 270 deg. Here is the code.

void CBmpView::OnRotate180() 
{
BYTE ptempBit ;
CBmpViewerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

int nLenght = (pDoc->dwBitsSize);
if(pBitsView != NULL){
    DeleteObject(pBitsView);    
}
if(pDoc->m_bRotationFlag == FALSE){
pDoc->m_bmpHandle = CreateDIBSection(NULL, pDoc->m_bitmapinfo,DIB_RGB_COLORS,
    (void **)&pBitsView, NULL, 0);
CopyMemory(pBitsView,pDoc->m_pBits,pDoc->dwBitsSize);

for(int i=0; i < ((nLenght-1)) ; i++ ){
    ptempBit = pBitsView[i];
    pBitsView[i] = pBitsView[nLenght - 1];
    pBitsView[nLenght - 1] = ptempBit;
    nLenght --;
}
pDoc->m_bRotationFlag = TRUE;
Invalidate();
}else if(pDoc->m_bRotationFlag == TRUE){
    pDoc->m_bmpHandle = CreateDIBSection(NULL, pDoc->m_bitmapinfo,DIB_RGB_COLORS,
        (void **)&pBitsView, NULL, 0);
    CopyMemory(pBitsView,pDoc->m_pBits,pDoc->dwBitsSize);
    pDoc->m_bRotationFlag = FALSE;
    Invalidate();
}
}

Please tell me if any other method can be used. Thanks in advance

4

1 回答 1

0

只是一些伪代码。让我们假设一个具有 iMaxRows 和 iMaxCols 的网格具有行和列。应该:

// Sourcegrid is always 
Pixels oldGrid[iMaxRows][iMaxCols]

+

// 90°
Pixels newGrid[iMaxCols][iMaxRows];
for (int iRow=0; iRow<iMaxRows; ++iRow)
{
  for (int iCol=0; iCol<iMaxCols; ++iCol)
    newGrid[iCol][iMaxRow-iRow-1] = oldGrid[iRow][iCol];
}

+

// 180°
Pixels newGrid[iMaxRows][iMaxCols];
for (int iRow=0; iRow<iMaxRows; ++iRow)
{
  for (int iCol=0; iCol<iMaxCols; ++iCol)
     newGrid[iMaxRow-iRow-1][iMaxCols-iCol-1] = oldGrid[iRow][iCol];
}

+

// 270°
Pixels newGrid[iMaxCols][iMaxRows];
for (int iRow=0; iRow<iMaxRows; ++iRow)
{
  for (int iCol=0; iCol<iMaxCols; ++iCol)
     newGrid[iMaxCols-iCol-1][iRow] = oldGrid[iRow][iCol];
}
于 2013-09-26T07:05:35.360 回答