存储PPM P3文件的所有值

时间:2017-03-03 03:00:00

标签: c ppm

我是"尝试"使用C来存储P3 PPM文件,但我并不是所有经历过它的人,所以我碰到了一堵砖墙。我想知道是否有人可以帮助我完成我的任务。谢谢。

我想将它们保留为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;
}

1 个答案:

答案 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;
}