C-存储PPM P3文件的所有值

  • 本文关键字:文件 存储 PPM P3 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] = '';
    //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("%sn", image->code);
    printf("%d %dn", image->width, image->height);
    printf("%dn", image->max);
    for(i = 0; i < totalpixels; ++i)
    {
            printf("%d %d %dn", 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;
}

在声明结构和读取数据的方式中存在一些错误。检查以下代码中读取数据的方式,并在需要更清晰的情况下返回此处。

#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] = '';
    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("%sn", image->code);    
    printf("%d %dn", image->width, image->height);
    printf("%dn", image->max); 
    for(i = 0; i < totalpixels; ++i)
    {
        printf("%d %d %dn", 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;
}

相关内容

最新更新