C Read Ppm File Header How to Skip Whitespace

  1. #1

    DGB_684 is offline

    Registered User


    Assistance With Reading The Image Size Of A PPM File?

    I've used this lawmaking earlier to test it and it did piece of work. Unfortunately it now reads two random numbers for a pgm file and the code ends earlier it fifty-fifty gets to read the image dimensions when I effort to read a ppm file in. I know I'm missing something but I'm not quite sure where withal. Any help is much appreciated. Thanks. (the code is as well unfinished)

    Lawmaking:

                                                      #include                                                                          <stdio.h>                                                                                                                                #include                            <stdlib.h>                            #include                            <string.h>                            #define                            RGB_COMPONENT_COLOUR                            255                            #define                            height                            1080                            #define                            width                            1920                            typedef                                                          struct                                                        {                                                          unsigned                                                                                      char                                                        ruby-red,                            dark-green,                            bluish; }                            PPMPixel;                            typedef                                                          struct                                                        {                                                          int                                                        x,                            y;                            PPMPixel                            *                            information; }                            PPMImage;                                                          void                                                        clear_to_end(FILE                            *                                                          fp                            ) {                                                          int                                                        c;                            while                            ((c                            =                            fgetc(                              fp                            ))                            !=                            -                            1                            &&                            c                            !=                            '                            \n                            '); }                            PPMImage                            *                            load_file() {                            FILE                            *                            fp;                            PPMImage                            *                            img;                                                          int                                                        d;                                                          char                                                        buff[16];                                                          char                                                        fname[100];                            printf("Enter file proper noun: ");                            scanf("                            %southward                            ",                            fname);                            fseek(stdin,0,SEEK_END);                            fp                            =                            fopen(fname,                            "rb");                            if                            (fp                            ==                            Cipher                            ||                            !                            fp                            ||                            ferror(fp))     {                            printf("                            \t                            Mistake while opening the file                            \n                            ");     }                            else                            {                            printf("                            \t                            Reading in                                                        %southward\due north                            ",                            fname);     }                            //Checking for comments                            //Skip whitespace and comments                            d                            =                            getc(fp);                            while                            (d                            ==                            '#')     {                            while(getc(fp)!=                            '                            \n                            ')                            d                            =                            getc(fp);     }                            ungetc(d,fp);                            //Checking header                            if                            (!                            fgets(buff,sizeof(buff),fp))     {                            perror(fname);                            leave(1);     }                            if                            (buff[0]                            ==                            'P'                            ||                            buff[1]                            ==                            two                            ||                            buff[one]                            ==                            iii)     {                            printf("                            \t                            Reading PGM/PPM image in...                            \n                            ");     }                            else                            {                            printf("                            \t                            Incorrect image format                            \n                            ");     }                            //Read prototype size data                            if                            (fscanf(fp,                            "                            %u                            %u                            ",                            &                            img->y,                            &                            img->ten))     {                            printf("                            \t                            Invalid image size                            \n                            ");                            printf("                            \t                            Epitome size is:                                                        %u                                                          10                                                        %u\north                            ",                            img->y,                            img->x);     }                            else                            {                            printf("                            \t                            Image size is valid                            \n                            ");                            printf("                            \t                            Image size is:                                                        %u                                                          x                                                        %u\n                            ",                            img->y,                            img->x);     }                                                                                                    }                                                                                                                                                              void                                                        save_file(PPMImage                            *                                                          img                            ) {           }                                                          int                                                        main() {                                                          char                                                        central                            =                            0;                            PPMImage                            *                            img                            =                            Aught;                            do                            {                            puts("                            \t                            Press r to read in an prototype in ppm format");                            puts("                            \t                            Press s to save image in ppm format");                            puts("                            \t                            Printing q to quit");                            scanf("                                                        %c                            ",                            &                            key);                            clear_to_end(stdin);                            switch                            (key) {                            case                            'r':                            // TODO: if img != NULL, free it                            img                            =                            load_file();                            break;                            example                            's':                            save_file(img);                            break;                            case                            'q':                            puts("                            \t                            Terminating program...");                            break;                            default:                            puts("                            \t                            Invalid Input");                            break;         }     }                            while                            (fundamental                            !=                            'q');                                                                                                    return                                                                          0                                                                          ;                                              


  2. #2

    Salem is offline

    and the lid of int overfl Salem's Avatar


    Reposted without the horrid colour scheme.

    Code:

    #include<stdio.h> #include <stdlib.h> #include <string.h>  #define RGB_COMPONENT_COLOUR 255 #ascertain height 1080 #define width 1920  typedef struct {     unsigned char red, light-green, bluish; } PPMPixel;  typedef struct {     int x, y;     PPMPixel *data; } PPMImage;  void clear_to_end(FILE *fp) {     int c;     while ((c = fgetc(fp)) != -one && c != '\north'); }  PPMImage* load_file() {     FILE *fp;     PPMImage *img;     int d;     char buff[16];     char fname[100];     printf("Enter file proper name: ");     scanf("%due south", fname);     fseek(stdin,0,SEEK_END);     fp = fopen(fname, "rb");     if (fp == Nil || !fp || ferror(fp))     {            printf("\tError while opening the file\n");     }                             else         {         printf("\tReading in %s\north", fname);     }      //Checking for comments     //Skip whitespace and comments     d = getc(fp);     while (d=='#')     {         while(getc(fp)!='\north')         d=getc(fp);     }     ungetc(d,fp);      //Checking header     if (!fgets(buff,sizeof(buff),fp))     {         perror(fname);         exit(1);     }      if (buff[0] =='P' || buff[1] == 2 || vitrify[1] == iii)     {         printf("\tReading PGM/PPM image in...\n");     }     else     {         printf("\tIncorrect image format\northward");     }      //Read paradigm size data     if (fscanf(fp, "%u%u", &img->y, &img->10))     {         printf("\tInvalid image size\n");         printf("\tImage size is: %u x %u\due north", img->y, img->x);     }     else     {         printf("\tImage size is valid\northward");         printf("\tImage size is: %u x %u\n", img->y, img->x);     }   } void save_file(PPMImage *img) {           }   int main() {     char primal = 0;     PPMImage* img = NULL;     practice {         puts("\tPress r to read in an image in ppm format");         puts("\tPress southward to save prototype in ppm format");         puts("\tPress q to quit");                  scanf(" %c", &key);         clear_to_end(stdin);         switch (cardinal) {             example 'r':                 // TODO: if img != NULL, complimentary it                 img = load_file();                 break;             case 's':                 save_file(img);                 suspension;             instance 'q':                 puts("\tTerminating program...");                 break;             default:                 puts("\tInvalid Input");                 interruption;         }     } while (key != 'q');  return0;


  3. #3

    Salem is offline

    and the chapeau of int overfl Salem's Avatar


    > while ((c = fgetc(fp)) != -1 && c != '\n');
    Use EOF, don't presume EOF is -i

    > if (fp == Zippo || !fp || ferror(fp))
    fp == Nada and !fp are the same test, there'southward cipher gained by writing both.
    Also, ferror is only prepare after performing some read/write operation on the file.

    > if (!fgets(buff,sizeof(vitrify),fp))
    How long is the start line?

    Netpbm - Wikipedia
    You lot're assuming a very specific layout of spaces and newlines, which isn't at all guaranteed.

    > if (buff[0] =='P' || buff[ane] == 2 || buff[1] == iii)
    The file contains characters, not numbers.
    So yous should be comparing '2' or '3'.

    Also, it should be say
    if (buff[0] =='P' && ( buff[ane] == '2' || vitrify[1] == 'iii') )
    Kickoff char is 'P'
    Second char is either '2' or 'three'.


  4. #four

    DGB_684 is offline

    Registered User


    Quote Originally Posted by Salem View Post

    >
    > if (!fgets(buff,sizeof(buff),fp))
    How long is the first line?

    Netpbm - Wikipedia
    Y'all're assuming a very specific layout of spaces and newlines, which isn't at all guaranteed.

    > if (buff[0] =='P' || buff[1] == ii || buff[1] == 3)
    The file contains characters, not numbers.
    So you should be comparing '2' or '3'.

    Also, it should be say
    if (buff[0] =='P' && ( buff[1] == '2' || vitrify[i] == 'iii') )
    First char is 'P'
    Second char is either '2' or '3'.

    Thanks for the tips! I don't know how long the kickoff line is because any pgm or ppm file could be read in merely I'm not sure how to exactly work effectually that nonetheless. Also I tried using if (vitrify[0] =='P' && ( buff[i] == '2' || buff[1] == '3') ) before but all information technology seems to do is impress my error message "wrong image format".


  5. #v

    Salem is offline

    and the hat of int overfl Salem's Avatar


    Maybe y'all could adhere your PBM file (or post it at say Pastebin.com - #ane paste tool since 2002! ) then we can see how your lawmaking differs from the file you're trying to parse.


  6. #6

    DGB_684 is offline

    Registered User


    Well this is i of the ppm files I was having trouble with: PPM File - Pastebin.com

    Thanks again.


  7. #7

    john.c is offline

    Registered User


    If you actually made the changes you said you made so you wouldn't go the error you say you're getting.
    At any charge per unit, yous should open the file in text mode, not binary mode.
    And you need to malloc some memory for the epitome.
    Also, you're non handling comments properly at all. Comments tin can get anywhere just at the very beginning (which seems to exist where you lot think they can go) or in the image data. And then this is valid:

    Lawmaking:

    P2   # Magic number 5    # Width 2    # Peak 255  # Max color value # Image data 10 20 30 40 50 5 15 25 35 45

    Afterwards midnight begins the intoxication of pernicious truths. - Emil Cioran


  8. #8

    Salem is offline

    and the hat of int overfl Salem's Avatar


    A few tweaks.

    Code:

    #include<stdio.h> #include <stdlib.h> #include <string.h>  #ascertain RGB_COMPONENT_COLOUR 255 #define height 1080 #define width 1920  typedef struct {     unsigned char red, green, blue; } PPMPixel;  typedef struct {     int x, y;     PPMPixel *data; } PPMImage;  void clear_to_end(FILE *fp) {     int c;     while ((c = fgetc(fp)) != -1 && c != '\n'); }  PPMImage* load_file(const char *fname) {     FILE *fp;     PPMImage *img = malloc(sizeof(*img));	//!! classify space!     int d;     // https://en.wikipedia.org/wiki/Netpbm#PPM_example     // co-ordinate to this, max length of whatever line is lxx chars     char buff[80];     fp = fopen(fname, "r");  //!! non rb for text files!!     if (fp == NULL)     {            printf("\tError while opening the file\n");         return Nothing;     }                             else         {         printf("\tReading in %s\northward", fname);     }      //Checking for comments     //Skip whitespace and comments     d = getc(fp);     while (d=='#')     {         while(getc(fp)!='\northward')         d=getc(fp);     }     ungetc(d,fp);      //Checking header     if (!fgets(buff,sizeof(buff),fp))     {         perror(fname);         exit(ane);     }      if (buff[0] =='P' && (buff[1] == '2' || buff[one] == 'iii'))     {         printf("\tReading PGM/PPM image in...\northward");     }     else     {         printf("\tIncorrect epitome format\n");         return NULL;     }      //Read paradigm size information     if (fscanf(fp, "%u%u", &img->y, &img->x))     {         printf("\tInvalid image size\n");         printf("\tImage size is: %u ten %u\n", img->y, img->x);     }     else     {         printf("\tImage size is valid\northward");         printf("\tImage size is: %u x %u\northward", img->y, img->x);     }      return img; }  int main() {     PPMImage* img = load_file("foo.pbm");     free(img); }
    Results

    Lawmaking:

    $ gcc foo.c $ caput foo.pbm  P3 107 48 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  $ ./a.out  	Reading in foo.pbm 	Reading PGM/PPM prototype in... 	Invalid epitome size 	Image size is: 107 x 48


  9. #9

    DGB_684 is offline

    Registered User


    Quote Originally Posted by john.c View Post

    If you actually made the changes you said you made then yous wouldn't get the error you say you're getting.
    At any rate, you lot should open the file in text mode, not binary mode.
    And yous need to malloc some memory for the prototype.
    Besides, you're not handling comments properly at all. Comments can go anywhere just at the very offset (which seems to be where you think they can go) or in the image data. So this is valid:

    Code:

    P2   # Magic number v    # Width ii    # Superlative 255  # Max color value # Paradigm data 10 20 30 xl fifty 5 fifteen 25 35 45
    I forgot to add the ' ' around two and 3 that's why it wasn't working in the end.


  10. #10

    DGB_684 is offline

    Registered User


    Quote Originally Posted past Salem View Post

    A few tweaks.

    Lawmaking:

    #include<stdio.h> #include <stdlib.h> #include <string.h>  #define RGB_COMPONENT_COLOUR 255 #ascertain height 1080 #ascertain width 1920  typedef struct {     unsigned char reddish, green, blueish; } PPMPixel;  typedef struct {     int x, y;     PPMPixel *information; } PPMImage;  void clear_to_end(FILE *fp) {     int c;     while ((c = fgetc(fp)) != -1 && c != '\n'); }  PPMImage* load_file(const char *fname) {     FILE *fp;     PPMImage *img = malloc(sizeof(*img));    //!! allocate space!     int d;     // https://en.wikipedia.org/wiki/Netpbm#PPM_example     // according to this, max length of any line is lxx chars     char buff[80];     fp = fopen(fname, "r");  //!! not rb for text files!!     if (fp == NULL)     {            printf("\tError while opening the file\north");         return Cypher;     }                             else         {         printf("\tReading in %s\due north", fname);     }      //Checking for comments     //Skip whitespace and comments     d = getc(fp);     while (d=='#')     {         while(getc(fp)!='\due north')         d=getc(fp);     }     ungetc(d,fp);      //Checking header     if (!fgets(buff,sizeof(buff),fp))     {         perror(fname);         exit(1);     }      if (buff[0] =='P' && (buff[1] == '2' || vitrify[ane] == '3'))     {         printf("\tReading PGM/PPM image in...\northward");     }     else     {         printf("\tIncorrect prototype format\n");         return Null;     }      //Read epitome size data     if (fscanf(fp, "%u%u", &img->y, &img->x))     {         printf("\tInvalid prototype size\n");         printf("\tImage size is: %u x %u\northward", img->y, img->x);     }     else     {         printf("\tImage size is valid\northward");         printf("\tImage size is: %u x %u\due north", img->y, img->ten);     }      return img; }  int principal() {     PPMImage* img = load_file("foo.pbm");     free(img); }

    Results

    Code:

    $ gcc foo.c $ caput foo.pbm  P3 107 48 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  255 255 255 255 255 255 255 255 255 255 255 255 255 255 255  $ ./a.out      Reading in foo.pbm     Reading PGM/PPM image in...     Invalid paradigm size     Image size is: 107 x 48
    Thanks a lot that's really helped to smoothen out some bug in my code. Nonetheless I'g all the same getting ridiculously high numbers for pgm image dimensions and my switch instance doesn't loop afterward r (though the latter is kind of an unrelated consequence).


weeksvocassithand.blogspot.com

Source: https://cboard.cprogramming.com/c-programming/180764-help-reading-image-size-ppm-file.html

0 Response to "C Read Ppm File Header How to Skip Whitespace"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel