PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

I'm trying to read a Bitmap... I get weird result...

I got a Surface class, very simple, just containing Width,Height, the buffer(WORD) and a SetPixel function.
now I'm trying to load a bitmap to my surface, but I get really really weird results...
here is my code:
Code:

WORD rgb(BYTE r,BYTE g,BYTE b)
{
return (WORD) (r & 0xff) << 11 | (g & 0xff)<< 6 | (b & 0xff);
}

Surface *Display::CreateSurfaceFromFile(const char *FileName)
{
Surface *pRetSurface;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD *pBits;

FILE *pFile=fopen(FileName,"rb");
if (!pFile)
return NULL;

fread(&bmfh,sizeof(bmfh),1,pFile);
fread(&bmih,sizeof(bmih),1,pFile);
fseek(pFile,bmfh.bfOffBits,SEEK_CUR);
pBits=new RGBQUAD[bmih.biWidth*bmih.biHeight];
fread(pBits,sizeof(RGBQUAD),bmih.biWidth*bmih.biHeight,pFile);



pRetSurface=new Surface(bmih.biWidth,bmih.biHeight);
long i=0;
for (long x=0;x<bmih.biWidth;x++)
{
for (long y=0;y<bmih.biHeight;y++)
{
BYTE R,G,B;
R=pBits[i].rgbRed;
G=pBits[i].rgbGreen;
B=pBits[i].rgbBlue;
pRetSurface->SetPixel(x,y,rgb(255,255,0));
i++;
}
}
fclose(pFile);
delete pBits;
return pRetSurface;


}


Can someone help me please?
I'm kind of stuck because of this...
the result I get is just a weird grayscale picture...
I think something is wrong when converting from 24bit to 16bit...
Try this:

Code:

WORD rgb(BYTE r,BYTE g,BYTE b)
{
return (WORD) (r >> 3) << 11 | (g >> 2)<< 5 | (b >> 3);
}
Code:
pRetSurface->SetPixel(x,y,rgb(255,255,0));
You also probably want to use R,G,B there:

Code:
pRetSurface->SetPixel(x,y,rgb(R,G,B));
(didn't sanity-check the rest, but Andy's code is correct too).
refractor wrote:
Code:
pRetSurface->SetPixel(x,y,rgb(255,255,0));
You also probably want to use R,G,B there:

Code:
pRetSurface->SetPixel(x,y,rgb(R,G,B));
(didn't sanity-check the rest, but Andy's code is correct too).

ohh yes, I had RGB in the SetPixel, I was just checking something...
I cant get this working...
if anyone has a way to load a bitmap file from a file, It will be very helpfull...
TIA.
I always just convert the BMPs to .H and #include the files because a PDAs RAM is also the file storage, so if you have to load a file during run-time then you're using more RAM than if you include the data and use the color data as a surface. Am I right ?