Problem with 24-bit bitmaps

Windows specific forum
MrTATaod
User
User
Posts: 20
Joined: Wed Jun 28, 2006 6:11 pm

Problem with 24-bit bitmaps

Post by MrTATaod »

At the moment, I'm having trouble loading the following 24-bit colour bitmap http://www.nicholaskingsley.co.uk/MiscT ... 000001.BMP - for some reason LoadImage fails, although if its converted to 256 colours, its fine.

The only thing I can think of is that the bitmap format is slightly different to what it should be (its actually created in a DLL), but then Paint and most other programs can load it okay.

Mind you, it appears that 24-bitmaps saved from Paint wont work - although 256-bit colours will.

This is the code for loading the bitmap :

Code: Select all

If LoadImage(1,"c:\G000003.BMP")=0
     MessageRequester("Failed","Failed")
   EndIf
And this is the code that saves it :

Code: Select all

signed short CMappy::saveBitmaps(unsigned char *temp24Buffer,DWORD numToDo,DWORD i)
{
BITMAPINFOHEADER bih;
BITMAPFILEHEADER bmfh;
int nBitsOffset;
LONG lImageSize;
LONG lFileSize;
TCHAR fileName[MAX_PATH+1];
FILE *pFile;

	memset (&bih, 0, sizeof(BITMAPINFOHEADER));
	bih.biSize = sizeof (BITMAPINFOHEADER);
	bih.biWidth =m_dBlockWidth;
	bih.biHeight = 0-m_dBlockHeight;
	bih.biPlanes = 1;
	bih.biBitCount = 24;
	bih.biCompression = BI_RGB;
	bih.biSizeImage = ((((bih.biWidth * bih.biBitCount) 
                           + 31) & ~31) >> 3) * bih.biHeight;

	nBitsOffset = sizeof(BITMAPFILEHEADER) + bih.biSize; 
    lImageSize = bih.biSizeImage;
    lFileSize = nBitsOffset + lImageSize;
    bmfh.bfType = 'B'+('M'<<8);
    bmfh.bfOffBits = nBitsOffset;
    bmfh.bfSize = lFileSize;
    bmfh.bfReserved1 = bmfh.bfReserved2 = 0;

    //Write the bitmap file header
	swprintf(fileName,_T("%s\\G%06d.BMP"),GRAPHIC_PATH,i);
	if ((pFile=_wfopen(fileName,_T("wb")))!=NULL)
	{
	size_t nWrittenFileHeaderSize;
	size_t nWrittenInfoHeaderSize;
	size_t nWrittenDIBDataSize;

		nWrittenFileHeaderSize = fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), pFile);
		//And then the bitmap info header
		nWrittenInfoHeaderSize = fwrite(&bih,1, sizeof(BITMAPINFOHEADER), pFile);
    	//Finally, write the image data itself 
		//-- the data represents our drawing
		nWrittenDIBDataSize = fwrite(temp24Buffer, 1, numToDo, pFile);
		fflush(pFile);
		fclose(pFile);
		return ERROR_OK;
	}
	else
	{
		return ERROR_FILENOTOPENED;
	}
}
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Well, I'm suffering from the same problem and not only with your example. I recently needed some toolbar images (the newer ones, Xp style) and exported the systems ones from the shell32.dll.

Now, there are 3 possiblities here:

1.) It's has something to do with the DLL. You mentioned your file is created in a DLL, mine is exported from one.
2.) Incorrect, incomplete or uknown fileheader/color data. Maybe Paint can read but PB got problems. E.g. both don't officially support 16/15Bit images.
3.) PureBasic doesn't handle file reading correctly.

Personally I think it's #3 because I compiled an EXE a while ago containg only

Code: Select all

LoadImage("blah.bmp")
and opened it with a HEX editor. I looked at the used Win32 API functions and I noticed the similar 'LoadImage_()' command. I tried to load mine and your image with it, it failed on both too.

Although, I'm not 100% sure that PB uses this API function to load images or it's own image importer.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I believe PureBasic uses the LoadImageA function from user32.dll to get the image into memory and then goes on to create the compatible bitmap that will become the PB image based on the flags specified in the program. As you point out, the LoadImageA function isn't getting a valid image handle and so the process can't proceed any further.
BERESHEIT
Post Reply