Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reading pixels from bmp


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 IceBreaker23   Members   -  Reputation: 609

Like
0Likes
Like

Posted 23 December 2011 - 01:28 AM

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

Sponsor:

#2 bradbobak   Members   -  Reputation: 1067

Like
1Likes
Like

Posted 23 December 2011 - 01:41 AM

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


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







#3 IceBreaker23   Members   -  Reputation: 609

Like
0Likes
Like

Posted 23 December 2011 - 02:16 AM

solved it with d3dxtexture and lockrect but thank you :D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS