2017-03-03 36 views
0

我正在嘗試使用C來存儲P3 PPM文件,但我並不是那麼熟悉它,所以我碰到了一堵磚牆。我想知道是否有人可以幫助我完成我的任務。謝謝。存儲PPM P3文件的所有值

我想保留它們作爲struct PPM和struct PPM * getPPM(File * fd),但內容可以改變。

我也不知道如何存儲評論,也想這樣做。

基本上我想存儲數據如下:

P3 
#comment.1 
. . . 
#comment.n 
width height 
max 
r1 g1 b1 
r2 g2 b2 
r3 g3 b3 
. . . 

編輯:

我也做了改變它正確編譯,現在我試圖通過的argv如此該文件可能會讀取argv [1]以獲取文件名。當我這樣做時,我收到一個分段錯誤。

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

//Holds the data 
struct data {int r,g,b;}; 
struct PPM {char code[4]; char comments; int width; int height; int max; struct data *Data;}; 

//Gets the PPM data 
struct PPM* GetPPM(FILE * fd, argv) 
{ 
    char readChars[256] = {0}; 
    //Allocates memory for PPM 
    struct PPM *image = (struct PPM *)calloc(1, sizeof(struct PPM)); 
    int i; 
    fgets(image->code, sizeof(image->code), fd); 
    fd = fopen(argv[1], "r"); 
    //Checks if file is type P3 
    if((image->code[0] != 'P') && (image->code[0] != '3')) 
    { 
      return NULL; 
    } 
    image->code[2] = '\0'; 
    //Checks for comments then continues around the loop until there's no more 
    fgets(readChars, sizeof(readChars), fd); 
    while(readChars[0] == '#') 
    { 
      fgets(readChars, sizeof(readChars), fd); 
    } 
    //Checks for PPM width, height and max 
    sscanf(readChars, "%d %d", &image->width, &image->height); 

    fgets(readChars, sizeof(readChars), fd); 
    sscanf(readChars, "%d", &image->max); 

    image->Data = (struct data*)malloc(image->width * image->height * sizeof(struct data)); 
    i = 0; 
    while(fgets(readChars, sizeof(readChars), fd)); 
    { 
      sscanf(readChars, "%d %d %d", &(image->Data[i].r), &(image->Data[i].g), &(image->Data[i].b)); 
      ++i; 
    } 
    fclose(fd); 
    return image; 
} 


//Deallocates memory 
void freePPM(struct PPM *image) 
{ 
    free(image->Data); 
    free(image); 
} 

//Displays PPM 
void showPPM(struct PPM *image) 
{ 
    int i = 0; 
    int totalpixels = image->width * image->height; 
    printf("%s\n", image->code); 
    printf("%d %d\n", image->width, image->height); 
    printf("%d\n", image->max); 

    for(i = 0; i < totalpixels; ++i) 
    { 
      printf("%d %d %d\n", image->Data[i].r, image->Data[i].g, image->Data[i].b); 
    } 
} 



int main(int argc, char ** argv) 
{ 
    struct PPM *image = GetPPM(argv); 
    showPPM(image); 
    freePPM(image); 

    return 0; 
} 

回答

0

聲明結構和讀取數據的方式有一些錯誤。檢查如何在下面的代碼中讀取數據,並在需要更清晰時回到此處。

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

struct data {int r,g,b;}; 
struct PPM {char code[4]; char comments; int width; int height; int max; struct data *Data;}; 

struct PPM* GetPPM(FILE * fd) 
{ 
    char readChars[256] = {0}; 
    struct PPM *image = (struct PPM *)calloc(1, sizeof(struct PPM)); 
    int i; 

    fgets(image->code, sizeof(image->code), stdin); 

    if((image->code[0] != 'P') && (image->code[0] != '3')) 
    { 
     return NULL; 
    } 
    image->code[2] = '\0'; 

    fgets(readChars, sizeof(readChars), stdin); 
    while(readChars[0] == '#') 
    { 
     fgets(readChars, sizeof(readChars), stdin); 
    } 

    sscanf(readChars, "%d %d", &image->width, &image->height); 

    fgets(readChars, sizeof(readChars), stdin); 
    sscanf(readChars, "%d", &image->max); 

    image->Data = (struct data*)malloc(image->width * image->height * sizeof(struct data)); 
    i = 0; 
    while(fgets(readChars, sizeof(readChars), stdin)) 
    { 
     sscanf(readChars, "%d %d %d", &(image->Data[i].r), &(image->Data[i].g), &(image->Data[i].b)); 
     ++i; 
    } 

    return image; 
} 

void FreePPM(struct PPM *image) 
{ 
    free(image->Data); 
    free(image); 
} 

void PrintPPM(struct PPM *image) 
{ 
    int i = 0; 
    int totalpixels = image->width * image->height; 
    printf("%s\n", image->code);  
    printf("%d %d\n", image->width, image->height); 
    printf("%d\n", image->max); 

    for(i = 0; i < totalpixels; ++i) 
    { 
     printf("%d %d %d\n", image->Data[i].r, image->Data[i].g, image->Data[i].b); 
    } 
} 

int main() 
{  
    struct PPM *image = GetPPM(stdin); 
    PrintPPM(image); 
    FreePPM(image); 

    return 0; 
} 
+0

我只是想知道爲什麼你從未在代碼中使用fd?如果看不到PPM文件的數據,我該如何查看PPM文件的數據?或者只是將每個stdin的實例更改爲fd修復?除此之外,它大部分看起來不錯,而且大部分我都明白I/O如何更好地工作 – Jason

+0

@Jason:'GetPPM(FILE * fd)'函數接收'FILE *'作爲參數。因此,您可以隨時打開所需的圖像文件並將其有效句柄傳遞給'GetPPM'函數。 – sameerkn