在C中显示ppm图像的rgb值和冲突类型错误
我是C语言的新手,我的任务是创建一个程序来读取图像并显示该图像。我已经创建了(我认为是)一个函数来读取文件格式/注释等,并尝试创建一个函数来显示每个像素的rgb值 我无法测试它,因为我的程序中有一个错误-“getPPM的冲突类型”。有人能解释一下我为什么会犯这个错误吗?任何可能的解决办法都会很好 如果有人能建议一种方法来存储评论(可能是在数组中?)并在以后打印它们,那也会非常有用!谢谢 另外,请随意检查整个程序并给我反馈,我不完全确定一旦上述错误被修复,它是否真的会做任何事情。 非常感谢在C中显示ppm图像的rgb值和冲突类型错误,c,image,printing,rgb,C,Image,Printing,Rgb,我是C语言的新手,我的任务是创建一个程序来读取图像并显示该图像。我已经创建了(我认为是)一个函数来读取文件格式/注释等,并尝试创建一个函数来显示每个像素的rgb值 我无法测试它,因为我的程序中有一个错误-“getPPM的冲突类型”。有人能解释一下我为什么会犯这个错误吗?任何可能的解决办法都会很好 如果有人能建议一种方法来存储评论(可能是在数组中?)并在以后打印它们,那也会非常有用!谢谢 另外,请随意检查整个程序并给我反馈,我不完全确定一旦上述错误被修复,它是否真的会做任何事情。 非常感谢 #in
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 600
#define MAX_WIDTH 400
int main(int argc, char ** argv){
// get image file
FILE *file;
file = fopen("aab(1).ppm", "r");
//if there is no file then return an error
if(file == NULL){
fprintf(stderr, "File is not valid");
return 0;
}
else {
struct PPM *newPPM = getPPM(file);
return 0;
}
}
struct PPM {
char format[2]; //PPM format code
int height, width; //image pixel height and width
int max; //max rgb colour value
} PPM;
struct PPM_Pixel {
//Create variables to hold the rgb pixel values
int red, green, blue;
} PPM_Pixel;
struct PPM *getPPM(FILE * fd){
struct PPM *image;
char buffer[16];
char c;
char commentArray[256];
fd = fopen(fd, "r");
struct PPM *newPPM = malloc(sizeof(PPM));
//checks if the file being read is valid/the correct file
if(fd == NULL){
exit(1);
}
//read the image of the format
if(!fgets(buffer, sizeOf(buffer), fd)){
exit(1);
}
//checks the format of the ppm file is correct
if(buffer[0] != 'P' || buffer[1] != '3'){
fprintf(stderr, "Invalid image format! \n");
exit(1);
}else {
printf("%s", newPPM->format);
}
//allocate memory for the image
image = malloc(sizeof(PPM));
if(!image){
fprintf(stderr, "Cannot allocate memory");
exit(1);
}
//checks whether the next character is a comment and store it in a linked list
c = getc(fd);
while(c == '#'){
while(getc(fd) != '\n'){
c = getc(fd);
}
}
if(fscaf(fd, "%d %d", &image->height, &image->width)!= 2){
fprintf(stderr, "Size of image is invalid");
exit(1);
}
fclose(fd);
return newPPM;
};
showPPM(struct PPM * image){
//two-dimensional array to show the rows and columns of pixels.
int rgb_array[MAX_HEIGHT][MAX_WIDTH];
int i;
int j;
for(i = 0; i<MAX_HEIGHT; i++){
for(j = 0; j<MAX_WIDTH; j++){
printf("%d", rgb_array[i][j]);
}
}
};
#包括
#包括
#定义最大高度600
#定义最大宽度400
int main(int argc,字符**argv){
//获取图像文件
文件*文件;
file=fopen(“aab(1).ppm”,“r”);
//如果没有文件,则返回一个错误
if(file==NULL){
fprintf(stderr,“文件无效”);
返回0;
}
否则{
结构PPM*newPPM=getPPM(文件);
返回0;
}
}
结构PPM{
字符格式[2];//PPM格式代码
int height,width;//图像像素高度和宽度
int max;//最大rgb颜色值
}PPM;
结构PPM_像素{
//创建变量以保存rgb像素值
红色、绿色、蓝色;
}PPM_像素;
结构PPM*getPPM(文件*fd){
结构PPM*图像;
字符缓冲区[16];
字符c;
字符数组[256];
fd=fopen(fd,“r”);
结构PPM*newPPM=malloc(sizeof(PPM));
//检查正在读取的文件是否有效/正确
如果(fd==NULL){
出口(1);
}
//读取格式的图像
如果(!fgets(缓冲区,sizeOf(缓冲区),fd)){
出口(1);
}
//检查ppm文件的格式是否正确
如果(缓冲区[0]!=“P”|缓冲区[1]!=“3”){
fprintf(stderr,“无效图像格式!\n”);
出口(1);
}否则{
printf(“%s”,newPPM->format);
}
//为映像分配内存
图像=malloc(sizeof(PPM));
如果(!图像){
fprintf(stderr,“无法分配内存”);
出口(1);
}
//检查下一个字符是否为注释,并将其存储在链接列表中
c=getc(fd);
而(c='#'){
而(getc(fd)!='\n'){
c=getc(fd);
}
}
如果(fscaf(fd,“%d%d”,&image->高度,&image->宽度)!=2){
fprintf(stderr,“图像大小无效”);
出口(1);
}
fclose(fd);
返回newPPM;
};
showPPM(结构PPM*图像){
//显示像素行和像素列的二维数组。
int rgb_阵列[最大高度][最大宽度];
int i;
int j;
对于(i=0;i以下代码
包含大量关于错误和/或如何纠正的评论
执行适当的错误检查
更正某些函数签名
正确地为每个函数声明一个原型
更正了一些“keypunch”错误
更正“语法”错误
在第一次引用这些定义之前,已将结构定义移动到
警告:此代码无法纠正访问PPM图像文件时出现的问题
警告:此代码仍然不会将malloc的内存区域传递到free()
,因此仍然需要添加这些语句
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 600
#define MAX_WIDTH 400
// 1) giving the struct tag name the same as an instance name is a bad idea
// 2) good programming practice is to define a struct separately
// from the instance of that struct
struct PPM
{
char format[2]; //PPM format code
int height, width; //image pixel height and width
int max; //max rgb colour value
};
struct PPM myPPM;
struct PPM_Pixel
{
//Create variables to hold the rgb pixel values
//int red, green, blue;
// follow the axiom:
// only one statement per line and (at most) one variable declaration per statement
int red;
int green;
int blue;
};
struct PPM_Pixel myPPM_Pixel;
// prototypes
struct PPM *getPPM(FILE * fp);
void showPPM(struct PPM * image);
// this line will cause the compiler to raise two warnings
// about unused variable 'argc' and 'argv'
//int main(int argc, char ** argv)
int main( void )
{
// get image file
//FILE *file;
//file = fopen("aab(1).ppm", "r");
//if there is no file then return an error
//if(file == NULL)
FILE *fp == NULL;
if( NUL == (fp = fopen("aab(1).ppm", "r") ) )
{
//fprintf(stderr, "File is not valid");
// should include system error message
perror( "fopen for read of aab(1).ppm failed" );
//return 0;
// need to indicate an error occurred
exit( EXIT_FAILURE );
}
// implied else, fopen was successful
// following statement will raise a compiler warning
// about: 'set but not used variable'
struct PPM *newPPM = getPPM(fp);
fclose( fp ); // moved to here from 'getPPM()'
} // end function: main
struct PPM *getPPM(FILE * fp)
{
struct PPM *image;
char buffer[16];
//char c;
// the returned value from 'getc()' is an integer, not a character
int c;
char commentArray[256];
// following line is nonsence and the passed in parameter is already open
//fd = fopen(fd, "r");
// when allocating memory, always check the returned value
// to assure the operation was successful
struct PPM *newPPM = NULL;
if( NULL == (newPPM = malloc( sizeof struct PPM ) ) )
{
perror( "malloc for struct PPM failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// already done in main()
//checks if the file being read is valid/the correct file
//if(fd == NULL){
// exit(1);
//}
//read the image of the format
if(!fgets(buffer, sizeOf(buffer), fd)){
exit(1);
}
//checks the format of the ppm file is correct
if(buffer[0] != 'P' || buffer[1] != '3'){
fprintf(stderr, "Invalid image format! \n");
exit( EXIT_FAILURE );
}
// implied else, file format correct
printf("%s", newPPM->format);
//allocate memory for the image
// note: there is no item named 'PPM'
// image = malloc(sizeof(PPM));
//if(!image){
// best to keep things together
if( NULL == (image = malloc( sizeof struct PPM ) ) )
{
//fprintf(stderr, "Cannot allocate memory");
// should include the OS message for the failure
perror( "malloc for struct PPM failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// the following code does not store a comment in a linked list
// and
// skips over 1/2 the characters in the line due to the double call
// to 'getc()' in the inner while loop
//checks whether the next character is a comment and store it in a linked list
//c = getc(fd);
//while(c == '#'){
// while(getc(fd) != '\n'){
// c = getc(fd);
// }
//}
// this line does not compile! did you actually mean to call: 'fscanf()'?
//if(fscaf(fd, "%d %d", &image->height, &image->width)!= 2)
if( 2 == fscanf( fp, "%d %d", &image->height, &image->width) )
{
// next line NOT true, what actually happened is the call to 'fscanf()' failed
//fprintf(stderr, "Size of image is invalid");
perror( "fscanf for image height and width failed" );
exit( EXIT_FAILURE );
}
// implied else, fscanf successful
// best practice to close a file, where it was opened.
//fclose(fd);
return newPPM;
//}; do not follow a function definition with a semicolon
}
// Note: the following function is never called
// showPPM(struct PPM * image) each function signature needs a return type, even if 'void'
void showPPM(struct PPM * image)
{
// Note: PPM images have the number of pixels in each row rounded up to a multiple of 4
// but this posted code fails to handle that
//two-dimensional array to show the rows and columns of pixels.
// bad idea, suggest passing the (already input) width and height
// and use those passed in values to define the array sizes
int rgb_array[MAX_HEIGHT][MAX_WIDTH];
int i;
int j;
for(i = 0; i<MAX_HEIGHT; i++)
{
for(j = 0; j<MAX_WIDTH; j++)
{
printf("%d", rgb_array[i][j]);
}
}
// }; do not follow a function definition with a semicolon
}
#包括
#包括
#定义最大高度600
#定义最大宽度400
//1)将结构标记名与实例名相同是个坏主意
//2)良好的编程实践是单独定义结构
//从该结构的实例
结构PPM
{
字符格式[2];//PPM格式代码
int height,width;//图像像素高度和宽度
int max;//最大rgb颜色值
};
结构PPM-myPPM;
结构PPM_像素
{
//创建变量以保存rgb像素值
//红色、绿色、蓝色;
//遵循公理:
//每行仅一条语句,每条语句最多一个变量声明
红色;
绿色;
蓝色;
};
结构PPM_像素myPPM_像素;
//原型
结构PPM*getPPM(文件*fp);
void showPPM(结构PPM*图像);
//此行将导致编译器发出两个警告
//关于未使用的变量“argc”和“argv”
//int main(int argc,字符**argv)
内部主(空)
{
//获取图像文件
//文件*文件;
//file=fopen(“aab(1).ppm”,“r”);
//如果没有文件,则返回一个错误
//if(file==NULL)
文件*fp==NULL;
如果(NUL==(fp=fopen(“aab(1.ppm)”,“r”))
{
//fprintf(stderr,“文件无效”);
//应包括系统错误消息
perror(“读取aab(1.ppm)的fopen失败”);
//返回0;
//需要指出发生了错误
退出(退出失败);
}
//除此之外,福彭成功了
//下面的语句将引发编译器警告
//关于:“设置但未使用变量”
结构PPM*newPPM=getPPM(fp);
fclose(fp);//从“getPPM()移动到此处”
}//结束函数:main
结构PPM*getPPM(文件*fp)
{
结构PPM*图像;
字符缓冲区[16];
//字符c;
//“getc()”返回的值是整数,而不是字符
INTC;
字符数组[256];
//以下行不存在,传入的参数已打开
//fd=fopen(fd,“r”);
//分配内存时,始终检查返回值
//确保手术成功
结构PPM*newPPM=NULL;
if(NULL==(newPPM=malloc(sizeof struct PPM)))
{
perror(“结构PPM的malloc失败”);
退出(退出失败);
}
//否则,malloc成功了
//已在main()中完成
//检查正在读取的文件是否有效/正确
//如果(fd==NULL){
//出口(1);
//}
//读取格式的图像