读取PPM文件并将其存储在数组中;使用C编写

11

我需要在C语言中读取PPM文件并将其存储在数组中。

有人能帮我完成这个任务吗?

非常感谢。


你遇到了哪个部分的问题?如果你发布你的不正确的代码尝试,很容易找到错误。 - caf
1
@Harry, 这个问题是针对 PGM 而不是 PPM。 PPM 有二进制像素数据。 - N8allan
2个回答

28

以下代码展示如何读取、更改像素颜色并以PPM格式写入图像。希望这可以帮到你。

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

typedef struct {
     unsigned char red,green,blue;
} PPMPixel;

typedef struct {
     int x, y;
     PPMPixel *data;
} PPMImage;

#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR 255

static PPMImage *readPPM(const char *filename)
{
         char buff[16];
         PPMImage *img;
         FILE *fp;
         int c, rgb_comp_color;
         //open PPM file for reading
         fp = fopen(filename, "rb");
         if (!fp) {
              fprintf(stderr, "Unable to open file '%s'\n", filename);
              exit(1);
         }

         //read image format
         if (!fgets(buff, sizeof(buff), fp)) {
              perror(filename);
              exit(1);
         }

    //check the image format
    if (buff[0] != 'P' || buff[1] != '6') {
         fprintf(stderr, "Invalid image format (must be 'P6')\n");
         exit(1);
    }

    //alloc memory form image
    img = (PPMImage *)malloc(sizeof(PPMImage));
    if (!img) {
         fprintf(stderr, "Unable to allocate memory\n");
         exit(1);
    }

    //check for comments
    c = getc(fp);
    while (c == '#') {
    while (getc(fp) != '\n') ;
         c = getc(fp);
    }

    ungetc(c, fp);
    //read image size information
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
         fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
         exit(1);
    }

    //read rgb component
    if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
         fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
         exit(1);
    }

    //check rgb component depth
    if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
         fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
         exit(1);
    }

    while (fgetc(fp) != '\n') ;
    //memory allocation for pixel data
    img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));

    if (!img) {
         fprintf(stderr, "Unable to allocate memory\n");
         exit(1);
    }

    //read pixel data from file
    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
         fprintf(stderr, "Error loading image '%s'\n", filename);
         exit(1);
    }

    fclose(fp);
    return img;
}
void writePPM(const char *filename, PPMImage *img)
{
    FILE *fp;
    //open file for output
    fp = fopen(filename, "wb");
    if (!fp) {
         fprintf(stderr, "Unable to open file '%s'\n", filename);
         exit(1);
    }

    //write the header file
    //image format
    fprintf(fp, "P6\n");

    //comments
    fprintf(fp, "# Created by %s\n",CREATOR);

    //image size
    fprintf(fp, "%d %d\n",img->x,img->y);

    // rgb component depth
    fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);

    // pixel data
    fwrite(img->data, 3 * img->x, img->y, fp);
    fclose(fp);
}

void changeColorPPM(PPMImage *img)
{
    int i;
    if(img){

         for(i=0;i<img->x*img->y;i++){
              img->data[i].red=RGB_COMPONENT_COLOR-img->data[i].red;
              img->data[i].green=RGB_COMPONENT_COLOR-img->data[i].green;
              img->data[i].blue=RGB_COMPONENT_COLOR-img->data[i].blue;
         }
    }
}

int main(){
    PPMImage *image;
    image = readPPM("can_bottom.ppm");
    changeColorPPM(image);
    writePPM("can_bottom2.ppm",image);
    printf("Press any key...");
    getchar();
}

1
这段代码在许多PNM文件上会失败,因为PNM在头条目之间使用空格,并且不仅限于使用LF。注释行也可以位于头部的任何位置,并且从技术上讲,它是唯一必须以CR或LF终止的行。PNM二进制文件格式设计混乱不堪。Sourceforge netpbm甚至指出,即使注释是有效的头对象,但其包含实际上会破坏文件格式。 - deegee
2
你使用了malloc,为什么没有使用free呢? - imre

3
这里是PPM规范
PPM文件由9个以空格分隔的部分组成。
  • 打开文件
  • 读取直到第一个空格,并检查是否为P6。然后跳过其他空格。
  • 读取直到下一个空格,将您的缓冲区转换为整数宽度。然后跳过其他空格。
  • 读取直到下一个空格,将您的缓冲区转换为整数高度。然后跳过其他空格。
  • 分配一个大小为height*width的二维整数数组。
  • 读取max-val。
  • 逐行读取并填充数组。

3
我认为你需要澄清这个回答中正在发生的事情,它并不是很清楚。 - James Morris
2
这段代码在许多PNM文件上会失败,因为它没有考虑到注释行。 - deegee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接