Jump to content
  • Advertisement
Sign in to follow this  
AssDruid

what's wrong with this code (bmp loading, C++)

This topic is 5401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what I'm trying to do is to load a bmp from memory, I can do it from a file, but when I'm trying to do it from memory (buffer) I get some exceptions... here is the code:
	BYTE *pBits;
	BITMAPFILEHEADER bmfh;
	BITMAPINFOHEADER bmih;
	ZeroMemory(&bmih,sizeof(bmih));
	ZeroMemory(&bmfh,sizeof(bmfh));
	
	
	
	memcpy(&bmfh,pDat,sizeof(bmfh));


	memcpy(&bmih,pDat+sizeof(bmfh),sizeof(bmih));





	long len=bmfh.bfSize-sizeof(bmfh)-sizeof(bmih);
	
	pBits=new BYTE[len];


	memcpy(pBits,pDat+bmfh.bfOffBits,len);


this is how I load it from a file (this is working):
	BYTE *pBits;
	BITMAPFILEHEADER bmfh;
	BITMAPINFOHEADER bmih;
	
	
	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_SET);
	long len=bmfh.bfSize-sizeof(bmfh)-sizeof(bmih);
	
	pBits=new BYTE[len];

	fread(pBits,len,1,pFile);


Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Don't count on the bfSize parameter to be initialized in the header structure, instead use the width height and bpp to get the byte size of the image data. also make sure you skip bfOffset(not sure about the name) bytes from the beginning of the data

Share this post


Link to post
Share on other sites
Quote:
Original post by Ilici
Don't count on the bfSize parameter to be initialized in the header structure, instead use the width height and bpp to get the byte size of the image data. also make sure you skip bfOffset(not sure about the name) bytes from the beginning of the data

well, about the bfSize, it works if I read it from a file and not from memory...
and about skipping, it called bfOffBits, and I do skip them:
Quote:
from code
memcpy(pBits,pDat+bmfh.bfOffBits,len);

please, help?

Share this post


Link to post
Share on other sites
Have you tried using a debugger and looking at the values in your structures to make sure that they're sensible? I notice you're not checking for the "MB" characters in the header file in either code sample, so your file header could contain any old crap and your code would never know the difference.

My guess is that you don't need the BITMAPFILEHEADER when loading from memory. A quick look at the data you're reading into the structures in a debugger would confirm this one way or the other.

Share this post


Link to post
Share on other sites
You're putting the parameters for fseek the wrong way round. You have:

fseek(pFile,bmfh.bfOffBits,SEEK_SET);

When you should have:

fseek(pFile,SEEK_SET,bmfh.bfOffBits);


And then of course you'll have to flip your image to get it the right way round

Share this post


Link to post
Share on other sites
wow, it's amazing that people cant read my whole post before replying...
ok, now in bold:
I CAN READ FROM FILES, THE SECOND CODE WORKS, WHAT I CANT IS READING FROM MEMORY
ok, sorry about that, just let off the steam...
now, look guys, the problem is with the memcpy, becuase I did debugged it, and I get wrong values on the structres...
is there anything wrong with the memcpy function, when I do something like memcpy(&dest,mybuffer+offset,size)?
because it seems to be the problem.

Share this post


Link to post
Share on other sites
There's nothing wrong with memcpy, the problem is in your code.

Where do you get the pDat pointer from?

Share this post


Link to post
Share on other sites
well, something like this:

FILE *pFile=fopen("iPAQ File Store/10.bmp","rb");
if (!pFile)
return FALSE;

char *pDat;
long len=0;
fseek(pFile,0,SEEK_END);
len=ftell(pFile);
fseek(pFile,0,SEEK_SET);
pDat=new char[len];
fread(pDat,len,1,pFile);



[edit] NEVER MIND GUYS, I FOUND THE PROBLEM, MY LOADING CODE WAS OK, JUST THE pDat WASNT[/edit]
Thanks guys, sorry :-]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!