# 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.

## 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;
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;

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

fseek(pFile,bmfh.bfOffBits,SEEK_SET);
long len=bmfh.bfSize-sizeof(bmfh)-sizeof(bmih);

pBits=new BYTE[len];


Thanks.

##### Share on other sites
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 on other sites
Quote:
 Original post by IliciDon'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 codememcpy(pBits,pDat+bmfh.bfOffBits,len);

##### Share on other sites
Quote:
 Original post by CaptainJesterHow to load a bitmap article

now, I figured out that the problem is with the memcpy, it dosent copy the right chunks of memory...
can someone tell me why is that?

##### 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 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 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 on other sites
There's nothing wrong with memcpy, the problem is in your code.

Where do you get the pDat pointer from?

##### 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);

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

• 18
• 29
• 11
• 21
• 16