Thread: "Parsing" a PPM header...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19

    "Parsing" a PPM header...

    Need help here...

    Writing a program that reads in a PPM image, modifies it, and spits it back out.

    I can make it work for a regular PPM header, like this one:

    Code:
    P6
    690 461 255
    but there are some that are like this:
    Code:
    P6
    # CREATOR: GIMP PNM Filter Version 1.1
    600 399
    255
    This is a school assignment and people in my class frequent these forums, so I can't post my complete code. The file needs to account for ALL types of PPM headers, not just the first example.

    As I said, I can't copy my entire code here, but here's the snippet that reads in PPM header data..

    Code:
            if (fscanf(inFile, "P6 %d %d", &width, &height) != 2)
            {
                fprintf(stderr, "Invalid image size.\n");
                exit(1);
            }
    any ideas?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'd recommend you retrieve the complete lines with fgets() then you can test the strings to see if:

    1. The first line contains "P6".
    2. If the line starts with a '#' that indicates a comment line that should be skipped.
    3. If the line starts with numeric data you can use sscanf() to process the line and if you don't get all three parameters read the next line, then process it for the rest of the parameters.


    Jim

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Start by reading the specification for the file format: PPM Format Specification

    As for your code, I suggest beginning with something like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char buf[8192];
    
        while (scanf("%8191s", buf) == 1)        // read next token
            if (buf[0] != '#')
                printf("\"%s\"\n", buf);
            else if (scanf("%*[^\n]") == EOF)    // eat the line
                break;
        return 0;
    }
    That will go through stdin, discarding comments, and reading and emitting tokens. For further information you should look up the documentation for the *scanf family as well as some other functions you might find useful, like strcmp and strtol.

    EDIT: Depending on how thoroughly you want to support the format, you may need to search for a '#' in every token. According to the specification:

    Before the whitespace character that delimits the raster, any characters from a "#" through the next carriage return or newline character, is a comment and is ignored. Note that this is rather unconventional, because a comment can actually be in the middle of what you might consider a token. Note also that this means if you have a comment right before the raster, the newline at the end of the comment is not sufficient to delimit the raster.
    Last edited by Barney McGrew; 04-18-2013 at 12:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question: "do while" loop parsing twice?
    By Toscano in forum C Programming
    Replies: 2
    Last Post: 12-19-2011, 06:39 PM
  2. Replies: 5
    Last Post: 06-01-2006, 04:37 PM
  3. problem with -> notation and using a "general" header file
    By Yourhighness in forum C Programming
    Replies: 2
    Last Post: 06-08-2003, 08:39 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM