seounggue kang
seounggue kang

Reputation: 31

How can I fix reverse left and right images when using VFW in MFC?

i making video program for MFC but there is 1 condition that is can't use openCV library so i try to using VFW but for now images are reverse left and right how can i fix this problem?

i try to fix this problem using capture_dc.BitBlt function .. but is not working well please let me know how to fix this

{
    // TODO: 
    if (nIDEvent == 1)
    {
        capGrabFrame(hWindow);
    }

    RECT rect;
    m_picture2.GetClientRect(&rect); 
    int window_width = rect.right;
    int window_height = rect.bottom;
    int image_width = window_width;
    int image_height = window_height;

    int b = 4;

    arrImage1 = new unsigned char[b * image_width * image_height];
    arrImage2 = new unsigned char[b * image_width * image_height];
    BITMAPINFO bmpinfo;
    bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpinfo.bmiHeader.biWidth = image_width;
    bmpinfo.bmiHeader.biHeight = -image_height;

    bmpinfo.bmiHeader.biPlanes = 1;
    bmpinfo.bmiHeader.biBitCount = 32;
    bmpinfo.bmiHeader.biCompression = BI_RGB;
    bmpinfo.bmiHeader.biSizeImage = 0;
    bmpinfo.bmiHeader.biXPelsPerMeter = 0;
    bmpinfo.bmiHeader.biYPelsPerMeter = 0;
    bmpinfo.bmiHeader.biClrUsed = 0;
    bmpinfo.bmiHeader.biClrImportant = 0;

    
    CDC dc;
    HDC hdc = ::GetDC(m_picture1.m_hWnd);
    dc.Attach(hdc);
    CDC    capture_dc;

    if (iBitmapFlag == 0)
    {
        capture_map.CreateCompatibleBitmap(&dc, image_width, image_height);
        iBitmapFlag = 1;
    }

    capture_dc.CreateCompatibleDC(&dc);
    capture_dc.SelectObject(&capture_map);
    capture_dc.BitBlt(0, 0, image_width, image_height, &dc, 0, 0, SRCCOPY);

    //capture_dc.BitBlt(image_width, 0, image_width, image_height, &dc, 0, 0, SRCCOPY); 
    //capture_dc.BitBlt(0, 0, -image_width, image_height, &dc, 0, 0, SRCCOPY); 
    //capture_dc.BitBlt(image_width, 0, image_width, image_height, &dc, 0, 0, SRCCOPY); 
    //capture_dc.BitBlt(image_width, 0, -image_width, image_height, &dc, 0, 0, SRCCOPY);
    ///it didn't work using this gray scale


    LPBYTE lpBits = arrImage1;
    int err;
    int ret = GetDIBits(capture_dc, capture_map, 0, image_height, lpBits, &bmpinfo, DIB_RGB_COLORS);
    
    if (ret == 0)  err = GetLastError();
    capture_dc.DeleteDC();
    ReleaseDC(&dc);

    int iFlagGray = 1;

    for (int y = 0; y < image_height; y++)
    {
        for (int x = 0; x < image_width; x++)
        {
            int index1 = ((y * image_width) + x);
            index1 *= 4;
            if (iFlagGray == 1) //grayscale
            {
                
                int grayblue = arrImage1[index1] * 0.11;       // 11%
                int graygreen = arrImage1[index1 + 1] * 0.59;  // 59%
                int grayred = arrImage1[index1 + 2] * 0.30;    // 30%
                int grayall = grayblue + graygreen + grayred;
                arrImage2[index1] = grayall;    // blue
                arrImage2[index1 + 1] = grayall;    // green
                arrImage2[index1 + 2] = grayall;    // red
                arrImage2[index1 + 3] = 0;         // this byte is not used
            }
            else //color
            {
                arrImage2[index1] = arrImage1[index1];    //blue
                arrImage2[index1 + 1] = arrImage1[index1 + 1]; //green
                arrImage2[index1 + 2] = arrImage1[index1 + 2]; //red
                arrImage2[index1 + 3] = 0;
            }
        }
    }

    int bpp = 32;
    CImage cimage_mfc;
    cimage_mfc.Create(window_width, window_height, 32);
    BITMAPINFO* bitInfo = (BITMAPINFO*)malloc(sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD));
    bitInfo->bmiHeader.biBitCount = bpp;
    bitInfo->bmiHeader.biWidth = window_width;
    bitInfo->bmiHeader.biHeight = -window_height;
    bitInfo->bmiHeader.biPlanes = 1;
    bitInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bitInfo->bmiHeader.biCompression = BI_RGB;
    bitInfo->bmiHeader.biClrImportant = 0;
    bitInfo->bmiHeader.biClrUsed = 0;
    bitInfo->bmiHeader.biSizeImage = 0;
    bitInfo->bmiHeader.biXPelsPerMeter = 0;
    bitInfo->bmiHeader.biYPelsPerMeter = 0;

    /*
    if (bpp == 8) 
    {
        RGBQUAD* palette = bitInfo->bmiColors; 
        for (int i = 0; i < 256; i++)
        {
            palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i; 
            palette[i].rgbReserved = 0;
        }
    }
    */


    if (image_width == window_width && image_height == window_height)
    {
        SetDIBitsToDevice(cimage_mfc.GetDC(), 0, 0, window_width, window_height, 0, 0, 0, image_height, arrImage2, bitInfo, DIB_RGB_COLORS);
    }
    else
    {
        int destx = 0, desty = 0;
        int destw = window_width;
        int desth = window_height;
        int imgx = 0, imgy = 0;
        int imgWidth = image_width;
        int imgHeight = image_height;
        StretchDIBits(cimage_mfc.GetDC(), destx, desty, destw, desth, imgx, imgy, imgWidth, imgHeight, arrImage2, bitInfo, DIB_RGB_COLORS, SRCCOPY);
    }

    HDC dc2 = ::GetDC(m_picture2.m_hWnd);
    cimage_mfc.BitBlt(dc2, 0, 0);
    ::ReleaseDC(m_picture2.m_hWnd, dc2);
    cimage_mfc.ReleaseDC();
    cimage_mfc.Destroy();
    delete arrImage1;
    delete arrImage2;

    CDialogEx::OnTimer(nIDEvent);
}

fix for reverse left and right side

Upvotes: 1

Views: 47

Answers (0)

Related Questions