c - 存储 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] = '\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;
}

最佳答案

声明结构和读取数据的方式存在一些错误。检查以下代码中如何读取数据,如果您想更清楚,请返回此处。

#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;
}

关于c - 存储 PPM P3 文件的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569861/

相关文章:

c - 如何解决“程序接收到的信号SIGSEGV,段错误”?

c - 哈希数组链表冲突解决错误

c++ - 分离 .raw 图像文件的 4 个 channel (R、G、G、B)并将它们保存为 C++ 中的有效图像

c - 确定消息是否太长而无法嵌入图像

python - 在python中为RGB图像添加噪声

c - 从右到左在 C 中的数组中滚动一个单词?

c# - 将结构数组从 c 返回到 c#

c - 读取大型二进制文件每 30 个字节的最快方法?

c - 我的图像解码程序出了什么问题?

c++ - OpenGl 纹理....... ppm 背景