C Read Ppm File Header How to Skip Whitespace
-
12-28-2021 #1
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 ;
-
12-28-2021 #2
and the lid of int overfl
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;
-
12-28-2021 #3
and the chapeau of int overfl
> 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'.
-
12-28-2021 #four
Registered User
Originally Posted by Salem
-
12-28-2021 #v
and the hat of int overfl
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.
-
12-28-2021 #6
Registered User
Well this is i of the ppm files I was having trouble with: PPM File - Pastebin.com
Thanks again.
-
12-28-2021 #7
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
-
12-28-2021 #8
and the hat of int overfl
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); }
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
-
12-28-2021 #9
Registered User
Originally Posted by john.c
-
12-28-2021 #10
Registered User
Originally Posted past Salem
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"
แสดงความคิดเห็น