Reading pixels from bmp

Started by
1 comment, last by Florian22222 12 years, 4 months ago
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
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).





solved it with d3dxtexture and lockrect but thank you :D

This topic is closed to new replies.

Advertisement