从HDC转换到leptonica PIX

void CAssistDlg::OnBnClickedTest()
{
    HDC hdc = ::GetDC(NULL);
    HDC hdcMem = CreateCompatibleDC(hdc);
    HBITMAP hBmpMem = CreateCompatibleBitmap(hdc, 234, 234);
    HBITMAP hBmpOld = (HBITMAP)SelectObject(hdcMem, hBmpMem);
    BitBlt(hdcMem, 0, 0, 234, 234, hdc, 127, 518, SRCCOPY);

    {
        HDC hdcThis = GetDC()->GetSafeHdc();
        BitBlt(hdcThis, 0, 0, 234, 234, hdcMem, 0, 0, SRCCOPY);
        
        BITMAP bmp = {0};//BITMAPINFO;BITMAPINFOHEADER;
        int ret = ::GetObject(hBmpMem, sizeof(bmp), &bmp);

        BITMAPINFOHEADER bmi = {0};
        bmi.biSize = sizeof(bmi);
        bmi.biWidth = bmp.bmWidth;
        bmi.biHeight = -bmp.bmHeight;
        bmi.biPlanes = bmp.bmPlanes;
        bmi.biBitCount = bmp.bmBitsPixel;
        bmi.biSizeImage = bmp.bmWidth * bmp.bmHeight * bmp.bmBitsPixel / 8;
        bmi.biCompression = BI_RGB;

        BYTE *pBuf = new BYTE[bmi.biSizeImage];
        int ret2 = GetDIBits(hdcMem, hBmpMem, 0, bmp.bmHeight, pBuf, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);    

        for (int i = 0;i < bmp.bmHeight;++i)
        {
            for (int j = 0;j < bmp.bmWidth;++j)
            {
                BYTE r = pBuf[j * 4 + i * bmp.bmWidthBytes + 2];
                BYTE g = pBuf[j * 4 + i * bmp.bmWidthBytes + 1];
                BYTE b = pBuf[j * 4 + i * bmp.bmWidthBytes + 0];
                
                pBuf[j * 4 + i * bmp.bmWidthBytes + 3] = r; //r
                pBuf[j * 4 + i * bmp.bmWidthBytes + 2] = g; //g
                pBuf[j * 4 + i * bmp.bmWidthBytes + 1] = b; //b
                pBuf[j * 4 + i * bmp.bmWidthBytes + 0] = 0xff; //a
            }
        }

        
        PIX pix = {0};
        pix.w = bmp.bmWidth;
        pix.h = bmp.bmHeight;
        pix.informat = IFF_BMP;
        pix.d = bmp.bmBitsPixel;
        pix.wpl = bmp.bmWidthBytes / 4;
        pix.data = (l_uint32 *)pBuf;
        pix.refcount = 1;
        pixWrite("D:\\pix.bmp", &pix, IFF_BMP);

   delete []pBuf; } SelectObject(hdcMem,hBmpOld); DeleteDC(hdcMem); DeleteObject(hBmpMem); ::ReleaseDC(NULL, hdc); }

 

posted @ 2013-06-18 12:11  荷包蛋  阅读(756)  评论(0编辑  收藏  举报