Jump to content
  • Advertisement
Sign in to follow this  
Florian22222

Reading pixels from bmp

This topic is 2551 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

Hello!
I have wrote a program to read from a bmp.
the bmp itself has 40x40 pixels with 8bit(says windows explorer)

my code:


int main()
{
FILE *f;
BITMAPINFOHEADER header;
f=fopen("apply.a.bmp","r");

fread(&header,sizeof(header),1,f);

int size = header.biBitCount; //in byte

int i = 0;
void *pixel = malloc(size);
while(feof(f) == 0)
{
i++;
fread(&pixel,sizeof(pixel),1,f);
cout << (int)pixel << endl;
};

cout << i << " pixels red" << endl;

fclose(f);

system("pause");
return 0;
}



but after i red all the pixels my program prints "373 pixels red". thats not correct(40x40 = 1600)
whats my fault?
thank you in advance

Share this post


Link to post
Share on other sites
Advertisement

... int i = 0;
void *pixel = malloc(size);
while(feof(f) == 0)
{
i++;
fread(&pixel,sizeof(pixel),1,f);
cout << (int)pixel << endl;
};
...

[/quote]

You are telling fread to read into the location of the pointer variable itself. sizeof(pixel) is (probably) 4 or 8, so you are reading that many bytes each iteration.

Perhaps you want to do a 'int num_read = fread(pixel, 1, size, f);' instead of the loop. (you said each pixel is 8 bits).





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!