streams and imported bitmaps

H

HGallon

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 ?
 
R

red floyd

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)
 
A

Alf P. Steinbach

* (e-mail address removed):
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top