C 以文本形式显示PPM图像
我是C编程新手,正在尝试文件操作,我正在尝试输出一个PPM文件,其中包含注释和rgb数据值,如下所示:C 以文本形式显示PPM图像,c,file,ppm,C,File,Ppm,我是C编程新手,正在尝试文件操作,我正在尝试输出一个PPM文件,其中包含注释和rgb数据值,如下所示: P3 # The same image with width 3 and height 2, # using 0 or 1 per color (red, green, blue) 3 2 1 1 0 0 0 1 0 0 0 1 1 1 0 1 1 1 0 0 0 在这个程序中,我已经能够检查它是否具有正确的格式并读入到结构中,但我遇到的问题是如何收集rgb数据,然后将其打印
P3
# The same image with width 3 and height 2,
# using 0 or 1 per color (red, green, blue)
3 2 1
1 0 0 0 1 0 0 0 1
1 1 0 1 1 1 0 0 0
在这个程序中,我已经能够检查它是否具有正确的格式并读入到结构中,但我遇到的问题是如何收集rgb数据,然后将其打印出来。这是我到目前为止所拥有的。显示此信息的方法是showPPM结构,我已经启动了它,但不知道如何读取图像结构并收集其rgb值并显示它,任何帮助都将非常有用
#include<stdio.h>
#include<stdlib.h>
typedef struct {
unsigned char red,green,blue;
} PPMPixel;
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
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] != '3') {
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);
}
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 showPPM(struct * image){
int rgb_array[600][400];
int i;
int j;
for(i = 0; i<600; i++)
{
for(j = 0; j<400; j++)
{
printf("%d", rgb_array[i][j]);
}
}
}
int main(){
PPMImage *image;
image = readPPM("aab.ppm");
showPPM(image);
}
#包括
#包括
类型定义结构{
无符号字符红色、绿色、蓝色;
}PPMPixel;
类型定义结构{
int x,y;
PPMPixel*数据;
}PPMImage;
静态PPMImage*readPPM(常量字符*文件名)
{
字符buff[16];
PPMImage*img;
文件*fp;
INTC,rgb_复合色;
//打开PPM文件进行读取
fp=fopen(文件名,“rb”);
如果(!fp){
fprintf(stderr,“无法打开文件“%s”\n”,文件名);
出口(1);
}
//读取图像格式
如果(!fgets(buff,sizeof(buff),fp)){
perror(文件名);
出口(1);
}
//检查图像格式
如果(buff[0]!=“P”| buff[1]!=“3”){
fprintf(stderr,“无效图像格式(必须为'P6'))\n”);
出口(1);
}
//异速记忆形式图像
img=(PPMImage*)malloc(sizeof(PPMImage));
如果(!img){
fprintf(stderr,“无法分配内存”);
出口(1);
}
//查看评论
c=getc(fp);
而(c='#'){
while(getc(fp)!='\n');
c=getc(fp);
}
ungetc(c,fp);
//读取图像大小信息
如果(fscanf(fp,“%d%d”,&img->x,&img->y)!=2){
fprintf(stderr,“无效图像大小(加载“%s”时出错)”\n,“文件名”;
出口(1);
}
while(fgetc(fp)!='\n');
//像素数据的内存分配
img->data=(PPMPixel*)malloc(img->x*img->y*sizeof(PPMPixel));
如果(!img){
fprintf(stderr,“无法分配内存”);
出口(1);
}
//从文件中读取像素数据
if(fread(img->data,3*img->x,img->y,fp)!=img->y){
fprintf(stderr,“加载映像“%s”时出错,\n”,文件名);
出口(1);
}
fclose(fp);
返回img;
}
void showPPM(结构*图像){
int rgb_阵列[600][400];
int i;
int j;
对于(i=0;i而言,您的代码看起来很可疑:
在这方面也有一些非常好的评论和帮助
因此,我留下以下其他内容作为进一步的帮助:
从这里开始有点模糊,让你有机会自己解决它
首先,您需要将整个ppm文件读入一个缓冲区。fread()
可能是选择的函数。您可以使用ftell()
获取文件的大小。接下来,您可以在fread()使用的缓冲区上键入PPMImage结构
,从那里,您应该能够通过数据
字段访问数据,如果您愿意,甚至可以使用数组表示法(我认为这是您现在缺少的步骤…)
一旦可以访问数据,您应该能够迭代数据和printf()
或根据输入数据访问控制台所需的任何内容。您的问题不够具体。“不知道去哪里”这不是一个问题。您具体不了解什么?对于初学者,为什么不至少尝试访问传递到showPPM
函数的image
?该image
包含一个结构,其中包含一个数据数组
。只需在该数组上迭代并打印出所需内容。谢谢,更改了我的设置问题更具体地说,我遇到的麻烦是我不知道如何打印图像结构,因为我不习惯c。你说不知道是什么意思?调用printf
。如果你不能问一个特定的问题,那么就很难帮上忙。例如,你的意思是你不知道如何编写循环来迭代数据?或者具体是什么?另外,我认为你的readPPM
函数是错误的。fread(img->data,3*img->x,img->y,fp)
这不起作用。输入中有空格,所以3
字节用于每个像素是不够的。我只是想让你问一个特定的问题,这样我们才能回答这个问题。你的问题最初是这样说的“不知道如何打印”。但你可能是说“不知道如何迭代数组”?这就是我想问的问题无法解释抱歉,我如何循环迭代图像结构中的数据?谢谢