i am converting 24bit rgb bmp image into 8bit greyscale. i am reading the r,g and b values of each pixel and then taking its average. then i am storing it in the greys matrix. but i dnt know why the program does not come to an end. it keeps on displaying the numbers and doesnto stop. kindly help
void grey (void)
fp= fopen("c:\\d.bmp", "r");
unsigned int rw, co, r,g,b, greys;
for (rw=0; rw<=1200; rw++)
for (co=0; co<=1600; co++)
cout <<greys[rw][co]<<" ";
Well you're looping over a million times, so that's probably it. It will stop eventually.
Usually a loop of a million times should take less than a second (if it's a simple loop).
Originally Posted by Memloop
The problem here is probably that you OUTPUT something a million times. If you remove the output, I bet that it would finish in less than a second... Solution: don't output anything there :P.
Also, you could optimize the file reading by reading large chunks every time and then reading from there.
Reading from a file is slow too.
However, I very much doubt your code works correctly in the first place. A "bmp" file contains more than just a series of 3 bytes per pixel (it contains a header, possibly padding at the end of lines).
It is probably simplest to use some 3rd-party library to load the bitmap (and hopefully it also has conversion to grey-scale as well).
(Is taking the average a good way to convert something to greyscale in the first place? Aren't there some subjective factors, a la the green parts should be more intensive etc?)