visualtest/src/windows/windows_screenshot.c
/* Saves a bitmap to a file using hdc as a device context */
Saves a bitmap to a file using hdc as a device context
Saves a bitmap to a file using hdc as a device context
static int
SaveBitmapToFile(HDC hdc, HBITMAP hbitmap, char* filename)
{
    BITMAP bitmap;
    BITMAPFILEHEADER bfh;
    BITMAPINFOHEADER bih;
    DWORD bmpsize, bytes_written;
    HANDLE hdib, hfile;
    char* bmpdata;
    int return_code = 1;
    if(!hdc)
    {
        SDLTest_LogError("hdc argument is NULL");
        return 0;
    }
    if(!hbitmap)
    {
        SDLTest_LogError("hbitmap argument is NULL");
        return 0;
    }
    if(!filename)
    {
        SDLTest_LogError("filename argument is NULL");
        return 0;
    }
    if(!GetObject(hbitmap, sizeof(BITMAP), (void*)&bitmap))
    {
        SDLTest_LogError("GetObject() failed");
        return_code = 0;
        goto savebitmaptofile_cleanup_generic;
    }
    bih.biSize = sizeof(BITMAPINFOHEADER);
    bih.biWidth = bitmap.bmWidth;
    bih.biHeight = bitmap.bmHeight;
    bih.biPlanes = 1;
    bih.biBitCount = 32;
    bih.biCompression = BI_RGB;
    bih.biSizeImage = 0;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    bih.biClrUsed = 0;
    bih.biClrImportant = 0;
    bmpsize = ((bitmap.bmWidth * bih.biBitCount + 31) / 32) * 4 * bitmap.bmHeight;
    hdib = GlobalAlloc(GHND, bmpsize);
    if(!hdib)
    {
        LogLastError("GlobalAlloc() failed");
        return_code = 0;
        goto savebitmaptofile_cleanup_generic;
    }
    bmpdata = (char*)GlobalLock(hdib);
    if(!bmpdata)
    {
        LogLastError("GlobalLock() failed");
        return_code = 0;
        goto savebitmaptofile_cleanup_hdib;
    }
    if(!GetDIBits(hdc, hbitmap, 0, (UINT)bitmap.bmHeight, bmpdata,
                  (LPBITMAPINFO)&bih, DIB_RGB_COLORS))
    {
        SDLTest_LogError("GetDIBits() failed");
        return_code = 0;
        goto savebitmaptofile_cleanup_unlockhdib;
    }
    hfile = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL, NULL);
    if(hfile == INVALID_HANDLE_VALUE)
    {
        LogLastError("CreateFile()");
        return_code = 0;
        goto savebitmaptofile_cleanup_unlockhdib;
    }
    bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bfh.bfSize = bmpsize + bfh.bfOffBits;
    bfh.bfType = 0x4D42;
    bytes_written = 0;
    if(!WriteFile(hfile, (void*)&bfh, sizeof(BITMAPFILEHEADER), &bytes_written, NULL) ||
       !WriteFile(hfile, (void*)&bih, sizeof(BITMAPINFOHEADER), &bytes_written, NULL) ||
       !WriteFile(hfile, (void*)bmpdata, bmpsize, &bytes_written, NULL))
    {
        LogLastError("WriteFile() failed");
        return_code = 0;
        goto savebitmaptofile_cleanup_hfile;
    }
savebitmaptofile_cleanup_hfile:
    CloseHandle(hfile);
#if defined(__CYGWIN__)
    if(chmod(filename, 0777) == -1)
    {
        SDLTest_LogError("chmod() failed");
        return_code = 0;
    }
#endif
savebitmaptofile_cleanup_unlockhdib:
    GlobalUnlock(hdib);
savebitmaptofile_cleanup_hdib:
    GlobalFree(hdib);
savebitmaptofile_cleanup_generic:
    return return_code;
}
static int SaveBitmapToFile ( HDC hdc , HBITMAP hbitmap , char * filename ) { BITMAP bitmap ; BITMAPFILEHEADER bfh ; BITMAPINFOHEADER bih ; DWORD bmpsize , bytes_written ; HANDLE hdib , hfile ; char * bmpdata ; int return_code = 1 ; if ( ! hdc ) { SDLTest_LogError ( " " ) ; return 0 ; } if ( ! hbitmap ) { SDLTest_LogError ( " " ) ; return 0 ; } if ( ! filename ) { SDLTest_LogError ( " " ) ; return 0 ; } if ( ! GetObject ( hbitmap , sizeof ( BITMAP ) , ( void * ) & bitmap ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto savebitmaptofile_cleanup_generic ; } bih . biSize = sizeof ( BITMAPINFOHEADER ) ; bih . biWidth = bitmap . bmWidth ; bih . biHeight = bitmap . bmHeight ; bih . biPlanes = 1 ; bih . biBitCount = 32 ; bih . biCompression = BI_RGB ; bih . biSizeImage = 0 ; bih . biXPelsPerMeter = 0 ; bih . biYPelsPerMeter = 0 ; bih . biClrUsed = 0 ; bih . biClrImportant = 0 ; bmpsize = ( ( bitmap . bmWidth * bih . biBitCount + 31 ) / 32 ) * 4 * bitmap . bmHeight ; hdib = GlobalAlloc ( GHND , bmpsize ) ; if ( ! hdib ) { LogLastError ( " " ) ; return_code = 0 ; goto savebitmaptofile_cleanup_generic ; } bmpdata = ( char * ) GlobalLock ( hdib ) ; if ( ! bmpdata ) { LogLastError ( " " ) ; return_code = 0 ; goto savebitmaptofile_cleanup_hdib ; } if ( ! GetDIBits ( hdc , hbitmap , 0 , ( UINT ) bitmap . bmHeight , bmpdata , ( LPBITMAPINFO ) & bih , DIB_RGB_COLORS ) ) { SDLTest_LogError ( " " ) ; return_code = 0 ; goto savebitmaptofile_cleanup_unlockhdib ; } hfile = CreateFile ( filename , GENERIC_WRITE , 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL ) ; if ( hfile == INVALID_HANDLE_VALUE ) { LogLastError ( " " ) ; return_code = 0 ; goto savebitmaptofile_cleanup_unlockhdib ; } bfh . bfOffBits = sizeof ( BITMAPFILEHEADER ) + sizeof ( BITMAPINFOHEADER ) ; bfh . bfSize = bmpsize + bfh . bfOffBits ; bfh . bfType = 0x4D42 ; bytes_written = 0 ; if ( ! WriteFile ( hfile , ( void * ) & bfh , sizeof ( BITMAPFILEHEADER ) , & bytes_written , NULL ) || ! WriteFile ( hfile , ( void * ) & bih , sizeof ( BITMAPINFOHEADER ) , & bytes_written , NULL ) || ! WriteFile ( hfile , ( void * ) bmpdata , bmpsize , & bytes_written , NULL ) ) { LogLastError ( " " ) ; return_code = 0 ; goto savebitmaptofile_cleanup_hfile ; } savebitmaptofile_cleanup_hfile : CloseHandle ( hfile ) ; #if defined ( __CYGWIN__ ) if ( chmod ( filename , 0777 ) == -1 ) { SDLTest_LogError ( " " ) ; return_code = 0 ; } #endif savebitmaptofile_cleanup_unlockhdib : GlobalUnlock ( hdib ) ; savebitmaptofile_cleanup_hdib : GlobalFree ( hdib ) ; savebitmaptofile_cleanup_generic : return return_code ; }
Saves a bitmap to a file using hdc as a device context
Saves a bitmap to a file using hdc as a device context
/* make the screenshot file writable on cygwin, since it could be overwritten later */