2016-02-11 109 views
0

我的程序是用來讀取和顯示PPM圖像,而不是打印正在讀取的圖像的實際格式或高度或寬度。這可能是一個非常基本的錯誤,但我沒有長時間使用C語言。PPM圖像格式,高度和寬度不顯示 - C

編輯︰我實際上只是注意到我檢查圖像格式的位置是有效的,我檢查它是否== 2,但它應該是!= 2(糾正我,如果我錯了)所以它說我的圖片格式無論如何都是無效的。我會嘗試在另一張圖片上運行我的代碼。

如果任何人都可以提供幫助,那就太好了。

電流輸出:

PPM FILE PROGRAM 
Memory allocation successful 
File format is correct 
89~ 
Image size: 3680602 544108293 

所需的輸出:

PPM FILE PROGRAM 
Memory allocation successful 
File format is correct 
P3 
Image size: 125 100 

代碼:

#include <stdio.h> 
#include <stdlib.h> 

#define MAX_HEIGHT 600 
#define MAX_WIDTH 400 

struct PPM { 
    char format[3]; //PPM format code 
    int height, width; //image pixel height and width 
    int max; //max rgb colour value 
}; 


struct PPM_Pixel { 
    //Create variables to hold the rgb pixel values 
    int red; 
    int green; 
    int blue; 
}; 


struct PPM *getPPM(FILE * file); 
void showPPM(struct PPM * image); 

int main(void){ 

    printf("PPM FILE PROGRAM \n"); 
    FILE *file; 
    // get image file 
    // FILE *file; 
    file = fopen("aab(1).ppm", "r"); 
    //if there is no file then return an error 
    if(file == NULL){ 
     fprintf(stderr, "File does not exist\n"); 
     return 0; 
    } 

    struct PPM *newPPM = getPPM(file); 
    showPPM(file); 
    fclose(file); 
} 


struct PPM *getPPM(FILE * file){ 

    char buffer[3]; 
    int c; 

    struct PPM *image = NULL; 
    if(NULL == (image = malloc(sizeof(struct PPM)))){ 
     perror("memory allocation for PPM file failed\n"); 
     exit(1); 
    } 
    else { 
     printf("Memory allocation succesful\n"); 
    } 

    //read the image of the format 
    if(!fgets(buffer, sizeof(buffer), file)){ 
     exit(1); 
    } 

    //checks the format of the ppm file is correct 
    if(buffer[0] != 'P' || buffer[1] != '3'){ 
     fprintf(stderr, "Invalid image format! \n"); 
     exit(1); 
    }else{ 
     printf("File format is correct\n"); 
     printf("%s\n", image->format); 
    } 

    //checks whether the next character is a comment and skips it 
    c = getc(file); 
    while(c == '#'){ 
     while(getc(file) != '\n'){ 
     c = getc(file); 
     } 
    } 

    //check the image size is valid 
    if(fscanf(file, "%d %d", &image->height, &image->width) == 2){ 
     printf("Invalid imaze size\n"); 
     exit(1); 
    }else{ 
     printf("Image size: %d %d ", image->height, image->width); 
    } 

    return image; 
} 

void showPPM(struct PPM * image){ 

    struct PPM_Pixel rgb_array[MAX_HEIGHT][MAX_WIDTH]; 
    int i; 
    int j; 

    for(i = 0; i<MAX_HEIGHT; i++){ 
     for(j = 0; j<MAX_WIDTH; j++){ 
      struct PPM_Pixel newPPM_Pixel; 
      if(fscanf(image, "%d %d %d", &newPPM_Pixel.red, &newPPM_Pixel.green, &newPPM_Pixel.blue) == 3){ 
       rgb_array[i][j] = newPPM_Pixel; 
      } 
     } 
    } 
} 

道歉,我不確定如何將輸出文本更改爲文本。

回答

1

雖然你忘了包括showPPM()的代碼,他的原型是

void showPPM(struct PPM * image); 

,你傳遞一個FILE *

FILE *file; 
... 
showPPM(file); 
+0

我將如何運行與最近讀取圖像文件showPPM功能? –

+0

由於代碼中沒有這樣的功能,你不能運行'showPPM' –

+0

我已經添加了showPPM功能。任何幫助將是偉大的:) –