c语言读取.ppm文件,孙其功陪你学之——C语言读取.ppm文件

C语言读取.ppm图像文件,读取的是每一个像素点的R、G、B的之0——255之间

在写读取程序时遇到好多问题,比如类型转换问题最是头疼,数据量特别大,开始读出的数据

可能有好大一部分为0,导致误认为读取错误,本人将读出的数据放在一个一维数组中,你也

可以把他们放在自己定义的三维数组中,分别存放一个像素点的RGB值,这样会更加形象,容易使用。

但功夫不负有心人,终于搞定,

在此分享

希望能给大家有所帮助,如果分析中有不恰当的地方,望批评指正

unsigned char matppm[640*480*3];

int nextline(char *line, FILE *fp)

{

char *p;

do {

p = fgets(line, HEADER_MAXLINE, fp);

} while ( p != NULL && *p == '#' );

if( p==NULL )

return -1;

return 0;

}

int read_header(FILE *fin, int *widthp, int *heightp, int *maxvalp, int *comp)

{

char line[HEADER_MAXLINE];

int  cols,rows,maxval;

if(nextline(line, fin)!=0) return -10;

if(strncmp(line,"P5",2)==0)

*comp=1;

else if (strncmp(line,"P6",2)==0)

*comp=3;

else if (strncmp(line,"P7",2)==0)

*comp=4;

else

return -1;

if(strlen(line)>3)

{

if(sscanf(line+2,"%d %d %d",&cols,&rows,&maxval )!=3 )

return -1;

}

else

{

if(nextline(line, fin)!=0) return -10;

if(sscanf(line,"%d %d",&cols,&rows)!=2 )

return -1;

if(nextline(line, fin)!=0) return -10;

if(sscanf(line,"%d",&maxval)!=1)

return -1;

}

*widthp = cols;

*heightp = rows;

*maxvalp = maxval;

return 0;

}

void read_ppm_image(char * image_name , int * rows, int * cols)

{

FILE * fp;

int widthp;

int heightp;

int maxvalp;

int comp;

short max=0;

fp = fopen(image_name, "rb");

read_header(fp, &widthp, &heightp, &maxvalp,&comp);

*rows = heightp;

*cols = widthp;

fread(matppm,1,widthp * heightp*3,fp);

fclose(fp);

}

在main()函数中应用如下:

int rows=0,cols=0;

*matppm  = (unsigned char )malloc(640*480*3);

read_ppm_image("rgb_1.ppm" ,  &rows,  &cols);

读入 ppm 文件需要使用文件操作函数和一些图像处理的知识。下面是一个读取 ppm 文件的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> typedef struct { int width; int height; int max_color; unsigned char *data; } PpmImage; PpmImage *read_ppm_image(const char *filename) { FILE *fp = fopen(filename, "rb"); if (fp == NULL) { fprintf(stderr, "Cannot open file: %s\n", filename); return NULL; } char magic[3]; int width, height, max_color; if (fscanf(fp, "%2s %d %d %d\n", magic, &width, &height, &max_color) != 4) { fclose(fp); fprintf(stderr, "Invalid PPM file format.\n"); return NULL; } if (magic[0] != 'P' || magic[1] != '6') { fclose(fp); fprintf(stderr, "Unsupported PPM format: %s\n", magic); return NULL; } // Allocate memory for the image data PpmImage *image = (PpmImage *) malloc(sizeof(PpmImage)); image->width = width; image->height = height; image->max_color = max_color; image->data = (unsigned char *) malloc(width * height * 3); // Read the image data if (fread(image->data, sizeof(unsigned char), width * height * 3, fp) != width * height * 3) { fclose(fp); free(image->data); free(image); fprintf(stderr, "Failed to read image data.\n"); return NULL; } fclose(fp); return image; } int main() { PpmImage *image = read_ppm_image("test.ppm"); if (image == NULL) { return 1; } // Do something with the image data free(image->data); free(image); return 0; } ``` 这个代码中,`PpmImage` 结构体表示一个 PPM 图像,包含宽度、高度、最大颜色值和像素数据。`read_ppm_image` 函数读取 PPM 图像文件,返回一个指向 `PpmImage` 结构体的指针。在主函数中,你可以使用 `image->data` 指向的像素数据进行图像处理操作。 注意,PPM 图像格式有多种,这个代码只支持 P6 格式(二进制编码的 RGB 像素数据)。如果你需要支持其他格式,可以对代码进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值