Thread: What is wrong with this

  1. #1
    Registered User
    Join Date
    Oct 2007

    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:

    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;
    	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 08:23 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

    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.

  3. #3
    Registered User
    Join Date
    Oct 2007
    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.

    I found out that I read the header the wrong way..
    Last edited by Slither; 12-04-2007 at 09: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
    Replies: 1
    Last Post: 11-03-2003, 10: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
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM