bmp 数据左右旋转90度

bmp  数据旋转90度

{
                        BITMAPFILEHEADER bmpheader;
                        BITMAPINFOHEADER bmpinfo;
                        memset(&bmpheader, 0, sizeof(BITMAPFILEHEADER));
                        memset(&bmpinfo, 0, sizeof(BITMAPINFOHEADER));

                        // set BITMAPFILEHEADER value
                        bmpheader.bfType        = ('M' << 8) | 'B';
                        bmpheader.bfReserved1    = 0;
                        bmpheader.bfReserved2    = 0;
                        bmpheader.bfOffBits        = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
                        bmpheader.bfSize        = bmpheader.bfOffBits + m_pCodecCtx->width * m_pCodecCtx->height * 24 / 8;
                        // set BITMAPINFOHEADER value
                        bmpinfo.biSize            = sizeof(BITMAPINFOHEADER);
                        bmpinfo.biWidth            = m_pCodecCtx->width;
                        bmpinfo.biHeight        = 0 - m_pCodecCtx->height;
                        bmpinfo.biPlanes        = 1;
                        bmpinfo.biBitCount        = 24;
                        bmpinfo.biCompression    = BI_RGB;
                        bmpinfo.biSizeImage        = 0;
                        bmpinfo.biXPelsPerMeter = 100;
                        bmpinfo.biYPelsPerMeter = 100;
                        bmpinfo.biClrUsed        = 0;
                        bmpinfo.biClrImportant    = 0;

                        if (m_hbitmap)
                            ::DeleteObject(m_hbitmap);
                        if (g_IsHorizontalMode)
                        {
                            m_hbitmap = RotateBmp(true, bmpinfo, m_pFrameRGB->data[0]);
                        }
                        else
                        {
                            CClientDC dc(NULL);
                            m_hbitmap = CreateDIBitmap(dc.GetSafeHdc(),                            //设备上下文的句柄 
                                (LPBITMAPINFOHEADER)&bmpinfo,                //位图信息头指针 
                                (long)CBM_INIT,                                //初始化标志 
                                m_pFrameRGB->data[0],                        //初始化数据指针 
                                (LPBITMAPINFO)&bmpinfo,                        //位图信息指针 
                                DIB_RGB_COLORS);                            //颜色数据的使用方式 
                        }

                        ::InvalidateRect(m_hWnd, NULL, FALSE);
                    }

 

HBITMAP CVedioDlg::RotateBmp(bool bRotateLeft, BITMAPINFOHEADER bmpinfo, byte* pBmpData)
{
    int bmpHeight            = abs(bmpinfo.biHeight);
    int bmpWidth            = abs(bmpinfo.biWidth);

    int byteCount            = bmpinfo.biBitCount/8;                                                    //一个像素几个字节
    int iAddLineData        = (4 - (bmpHeight * byteCount) % 4) % 4;                            //旋转后一行追加字节数

    int eddySize            = (bmpHeight * byteCount + iAddLineData) * bmpWidth;        //旋转后需要多大内存
    byte *bEddyData            = new byte[eddySize];

    int bEddyDataRowCount    = bmpHeight * byteCount + iAddLineData;                            //当前一行追加字节数

    int iBmpAddLineData        = (4 - bmpWidth * byteCount % 4) % 4;                                //旋转后一行需要追加几个字节

    //对数据进行操作
    int iImageSize            = (bmpWidth * byteCount + iBmpAddLineData) * bmpHeight;    //当前图像数据多大内存
    for (int i = 0; i< bmpHeight; i++)
    {

        for (int j = 0; j < bmpWidth * byteCount; j += byteCount)
        {
            int bmDataIndex            = 0;
            int bEddyDataRowIndex    = 0;        //旋转后行索引
            int bEddyDataColIndex    = 0;        //旋转后列索引
            int locIndex            = 0;        //旋转后内存中的索引值

            if (bRotateLeft)
            {
                bmDataIndex            = (bmpWidth * byteCount + iBmpAddLineData) * i;                //当前像素所在当前图像数据中的内存位置
                bEddyDataRowIndex    = (bmpWidth - 1 - (j / byteCount));                            //旋转后该像素在行索引
                bEddyDataColIndex    = i;                                                                    //旋转后该像素在列索引
                locIndex            = bEddyDataRowIndex  * (bmpHeight * byteCount + iAddLineData) + bEddyDataColIndex * byteCount;
            }
            else
            {
                bmDataIndex            = (bmpWidth * byteCount + iBmpAddLineData) * i;                //当前像素所在当前图像数据中的内存位置
                bEddyDataRowIndex    = j / byteCount;                                                        //旋转后该像素在行索引
                bEddyDataColIndex    = bmpHeight - 1 - i;                                            //旋转后该像素在列索引
                locIndex            = bEddyDataRowIndex  * (bmpHeight * byteCount + iAddLineData) + bEddyDataColIndex * byteCount;

            }
            bEddyData[locIndex]        = pBmpData[j+bmDataIndex];   //B
            bEddyData[locIndex+1]    = pBmpData[j+bmDataIndex+1]; //G
            bEddyData[locIndex+2]    = pBmpData[j+bmDataIndex+2]; //R

        }
    }
    //byte * bTemp = bmData;
    //bmData = bEddyData;
    ////删除临时信息
    //deletebTemp;
    ////更改头文件信息
    //bmpheader.bfSize                = bmpheader.bfOffBits + eddySize;

    bmpinfo.biWidth                    = bmpHeight;
    bmpinfo.biHeight                = bmpinfo.biHeight > 0?bmpWidth : -bmpWidth;

    CClientDC dc(NULL);
    HBITMAP hBitmap                    = CreateDIBitmap(dc.GetSafeHdc(),                //设备上下文的句柄 
                                        (LPBITMAPINFOHEADER)&bmpinfo,                //位图信息头指针 
                                        (long)CBM_INIT,                                //初始化标志 
                                        bEddyData,                                    //初始化数据指针 
                                        (LPBITMAPINFO)&bmpinfo,                        //位图信息指针 
                                        DIB_RGB_COLORS);

    delete[] bEddyData;


    return hBitmap;
}

 

posted @ 2016-10-11 21:45  高_山_流_水  阅读(910)  评论(0)    收藏  举报