What is wrong with this

This is a discussion on What is wrong with this within the C Programming forums, part of the General Programming Boards category; The assignment was to convert .ppm image file to bytes to do something else with them, and I have a ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    What is wrong with this

    The assignment was to convert .ppm image file to bytes to do something else with them, and I have a small problem with this bit of code:

    Code:
    	
    unsigned char *grey;
    int width, height, i;
    
    grey=malloc(sizeof(unsigned char)*width*height);
    for (i=0;i<width*height;i++)
    {
           grey[i] = greyfunction(f);
           printf("&#37;d\n", grey[i]);
    }	
    
    return 0;
    
    
    
    unsigned char greyfunction(FILE *f)
    {
    	int i;
    	unsigned char R, G, B, H;
    	fread(&R,1,1,f);
    	fread(&G,1,1,f);
    	fread(&B,1,1,f);
    	H = (0.3*R + 0.59*G + 0.11*B);                                                                                                                                                                                                                                                    
    	return H;
                   
    }
    It seems that it never gets out of that for. And besides it prints nr. 204 forever (I guess that one of the bytes in the original picture.
    Last edited by Slither; 12-04-2007 at 07:23 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > for (i=0;i<leveys*korkeus;i++)
    Why isn't this width * height ?

    The file would seem to be more than just an array of bytes encoding RGB in 3 bytes per pixel.
    http://en.wikipedia.org/wiki/Portable_pixmap

    For example, each colour value is encoded as two characters representing 1 hex byte.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Whoops, I wrote this in my own language so obviously those should be width*height. I have read the header from the file in to variables and the pointer is currently pointing to the first byte which defines the picture.

    edit:
    I found out that I read the header the wrong way..
    Last edited by Slither; 12-04-2007 at 08:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21