streams and imported bitmaps

Discussion in 'C++' started by HGallon@teranews.com, Jan 25, 2009.

  1. Guest

    I am merging several projects. One of them requires loading bitmaps from
    files (OK, I know I can use LoadBitmap () which is deprecated and
    LoadImage () which isn't, but I am reading the information in the
    BITMAPINFOHEADER to get the image height and width.)

    This works:
    HANDLE hFile = CreateFile (szFileName, GENERIC_READ,
    FILE_SHARE_READ, NULL, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, NULL);
    .
    .
    .
    // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);

    // bmfHeader is the BITMAPFILEHEADER structure, already read
    PBYTE pBitmapBits;
    SetFilePointer (hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);

    DWORD dwBytesRead;
    ReadFile (hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
    &dwBytesRead, NULL);

    This doesn't:
    ifstream iFile;
    iFile.open (szFileName);
    DWORD dwBytesRead;
    .
    .
    .
    // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    PBYTE pBitmapBits;
    HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);

    // bmfHeader is the BITMAPFILEHEADER structure, already read
    // dwBytesRead is count of bytes imported from file stream
    iFile.ignore (bmfHeader.bfOffBits - dwBytesRead);

    iFile.read ((UCHAR*) pBitmapBits,
    pBitmapInfoHeader->bmiHeader.biSizeImage);


    The bitmap resulting from the second code fragment is black halfway from
    the top down, before the coloured image appears. Evidently, the system
    is assuming that 256 RGBQUAD structures initialized to zero (i.e. black)
    are pixels rather than palette information.

    Has anyone an inkling why the use of input file streams rather than
    ReadFile () calls are failing in this respect ?
    , Jan 25, 2009
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > I am merging several projects. One of them requires loading bitmaps from
    > files (OK, I know I can use LoadBitmap () which is deprecated and
    > LoadImage () which isn't, but I am reading the information in the
    > BITMAPINFOHEADER to get the image height and width.)
    >
    > This works:
    > HANDLE hFile = CreateFile (szFileName, GENERIC_READ,
    > FILE_SHARE_READ, NULL, OPEN_EXISTING,
    > FILE_ATTRIBUTE_NORMAL, NULL);
    > .
    > .
    > .
    > // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    > HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    > DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
    >
    > // bmfHeader is the BITMAPFILEHEADER structure, already read
    > PBYTE pBitmapBits;
    > SetFilePointer (hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
    >
    > DWORD dwBytesRead;
    > ReadFile (hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
    > &dwBytesRead, NULL);
    >
    > This doesn't:
    > ifstream iFile;
    > iFile.open (szFileName);
    > DWORD dwBytesRead;
    > .
    > .
    > .
    > // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    > PBYTE pBitmapBits;
    > HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    > DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
    >
    > // bmfHeader is the BITMAPFILEHEADER structure, already read
    > // dwBytesRead is count of bytes imported from file stream
    > iFile.ignore (bmfHeader.bfOffBits - dwBytesRead);
    >
    > iFile.read ((UCHAR*) pBitmapBits,
    > pBitmapInfoHeader->bmiHeader.biSizeImage);
    >
    >
    > The bitmap resulting from the second code fragment is black halfway from
    > the top down, before the coloured image appears. Evidently, the system
    > is assuming that 256 RGBQUAD structures initialized to zero (i.e. black)
    > are pixels rather than palette information.
    >
    > Has anyone an inkling why the use of input file streams rather than
    > ReadFile () calls are failing in this respect ?


    Try opening your stream in binary mode (ios::binary)
    red floyd, Jan 25, 2009
    #2
    1. Advertising

  3. * :
    >
    > This works:
    > HANDLE hFile = CreateFile (szFileName, GENERIC_READ,
    > FILE_SHARE_READ, NULL, OPEN_EXISTING,
    > FILE_ATTRIBUTE_NORMAL, NULL);
    > .
    > .
    > .
    > // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    > HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    > DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
    >
    > // bmfHeader is the BITMAPFILEHEADER structure, already read
    > PBYTE pBitmapBits;
    > SetFilePointer (hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
    >
    > DWORD dwBytesRead;
    > ReadFile (hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
    > &dwBytesRead, NULL);


    It absolutely shouldn't work reliably. 'pBitmapBits' is uninitialized when you
    call 'ReadFile'. This assuming that 'ReadFile' is a C style API function.



    Cheers, & hth.,

    - Alf
    Alf P. Steinbach, Jan 25, 2009
    #3
  4. Guest

    red floyd wrote:
    > wrote:
    >> I am merging several projects. One of them requires loading bitmaps
    >> from files (OK, I know I can use LoadBitmap () which is deprecated and
    >> LoadImage () which isn't, but I am reading the information in the
    >> BITMAPINFOHEADER to get the image height and width.)
    >>
    >> This works:
    >> HANDLE hFile = CreateFile (szFileName, GENERIC_READ,
    >> FILE_SHARE_READ, NULL, OPEN_EXISTING,
    >> FILE_ATTRIBUTE_NORMAL, NULL);
    >> .
    >> .
    >> .
    >> // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    >> HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    >> DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
    >>
    >> // bmfHeader is the BITMAPFILEHEADER structure, already read
    >> PBYTE pBitmapBits;
    >> SetFilePointer (hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
    >>
    >> DWORD dwBytesRead;
    >> ReadFile (hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
    >> &dwBytesRead, NULL);
    >>
    >> This doesn't:
    >> ifstream iFile;
    >> iFile.open (szFileName);
    >> DWORD dwBytesRead;
    >> .
    >> .
    >> .
    >> // pBitmapInfo is the BITMAPINFOHEADER structure, already read
    >> PBYTE pBitmapBits;
    >> HBITMAP hBitmap = CreateDIBSection (hDC, pBitmapInfo,
    >> DIB_RGB_COLORS, (PVOID*) &pBitmapBits, NULL, 0);
    >>
    >> // bmfHeader is the BITMAPFILEHEADER structure, already read
    >> // dwBytesRead is count of bytes imported from file stream
    >> iFile.ignore (bmfHeader.bfOffBits - dwBytesRead);
    >>
    >> iFile.read ((UCHAR*) pBitmapBits,
    >> pBitmapInfoHeader->bmiHeader.biSizeImage);
    >>
    >>
    >> The bitmap resulting from the second code fragment is black halfway
    >> from the top down, before the coloured image appears. Evidently, the
    >> system is assuming that 256 RGBQUAD structures initialized to zero
    >> (i.e. black) are pixels rather than palette information.
    >>
    >> Has anyone an inkling why the use of input file streams rather than
    >> ReadFile () calls are failing in this respect ?

    >
    > Try opening your stream in binary mode (ios::binary)


    iFile.open (szFileName, ios_base::in | ios_base::binary) worked, thanks.

    hlg
    , Feb 14, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rithesh Pai
    Replies:
    1
    Views:
    462
  2. =?Utf-8?B?bWpjYXN0?=

    byte[]s, images and bitmaps with ASP.NET

    =?Utf-8?B?bWpjYXN0?=, Jun 7, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    12,665
    Pedro Lopez
    Aug 21, 2008
  3. Speed
    Replies:
    5
    Views:
    415
    Speed
    Mar 11, 2007
  4. Dun Peal
    Replies:
    10
    Views:
    456
    Chris Rebert
    May 3, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    937
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page