Thread: continuouse loop

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    9

    continuouse loop

    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

    Code:
    void grey (void)
    {
    	fp= fopen("c:\\d.bmp", "r");
    	unsigned int rw, co, r,g,b, greys[1200][1600];
    	for (rw=0; rw<=1200; rw++)
    		for (co=0; co<=1600; co++)
    		{
    			r=fgetc(fp);
    			g=fgetc(fp);
    			b=fgetc(fp);
    
    			greys[rw][co]= (r+g+b)/3;
    			cout <<greys[rw][co]<<"  ";
    		
    		}
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Well you're looping over a million times, so that's probably it. It will stop eventually.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Memloop View Post
    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).

    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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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?)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM