C 读取、更改和打印PPM图像
我在堆栈溢出上找到了执行此操作的代码,但使用它时出现了一些错误。我不确定我做错了什么,因为我对ppm文件没有太多经验。谢谢大家的帮助C 读取、更改和打印PPM图像,c,arrays,function,parsing,ppm,C,Arrays,Function,Parsing,Ppm,我在堆栈溢出上找到了执行此操作的代码,但使用它时出现了一些错误。我不确定我做错了什么,因为我对ppm文件没有太多经验。谢谢大家的帮助 typedef struct { int x, y; PPMPixel *data; } PPMImage; #define CREATOR "RPFELGUEIRAS" #define RGB_COMPONENT_COLOR 255 static PPMImage *readPPM(const char *filename = "
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR 255
static PPMImage *readPPM(const char *filename = "ukraine.ppm")
{
char buff[16];
PPMImage *img;
FILE *fp;
int c, rgb_comp_color;
//open PPM file for reading
fp = fopen("ukraine.ppm", "rb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//read image format
if (!fgets(buff, sizeof(buff), fp)) {
perror(ukraine.ppm);
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", ukraine.ppm);
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 = "ukraine.ppm", PPMImage *img)
{
FILE *fp;
//open file for output
fp = fopen(ukraine.ppm, "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();
return(0);
}
下面的代码编译得很干净 它是为文件ukraine.ppm硬编码的 您可以轻松地修改文件以使用文件全局字符数组 输入文件的主设置 您可以轻松地修改文件以使用文件全局字符数组 为输出文件设置的主文件
#include <stdio.h>
#include <stdlib.h>
struct PPMPixel
{
char red;
char blue;
char green;
};
struct PPMImage
{
unsigned x, y;
struct PPMPixel *data;
};
#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR (255)
static struct PPMImage *readPPM()
{
char buff[16];
struct PPMImage *img;
FILE *fp;
int c;
int rgb_comp_color;
//open PPM file for reading
fp = fopen("ukraine.ppm", "rb");
if (!fp)
{
perror( "fopen for ukraine.ppm failed" );
// fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
// implied els,e fopen successful
//read image format // get first 15 bytes of image file
if (!fgets(buff, sizeof(buff), fp))
{
perror("ukraine.ppm");
exit(1);
}
// implied else, fgets successful
//check the image format
if (buff[0] != 'P' || buff[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
exit(1);
}
// implied else, image file file has correct format indicators
//alloc memory for image
img = malloc(sizeof(struct PPMImage));
if (!img)
{
perror( "malloc for PPMImage failed" );
//fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
// implied else, malloc successful
//check for comments
c = getc(fp);
while (c == '#')
{
while( (c = getc(fp)) != '\n') ;
}
//read image size information
if (fscanf(fp, "%u %u", &img->x, &img->y) != 2)
{
perror( "fscanf for ukraine.ppm image size parameters failed" );
exit(1);
}
// implied else, fscanf successful
//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1)
{
perror( "fscanf for rgb_comp_color in ukraine.ppm failed" );
//fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}
// implied else, fscanf successful
//check rgb component depth
if (rgb_comp_color!= RGB_COMPONENT_COLOR)
{
fprintf(stderr, "'ukraine.ppm' does not have 8-bits components\n");
exit(1);
}
// implied else, rgb component depth correct
// consume rest of line
while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = malloc(img->x * img->y * sizeof( struct PPMPixel));
if ( !img->data ) //< corrected element being checked
{
perror( "malloc for pixel data failed" );
//fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
// implied else, malloc successful
//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y)
{
perror( "fread for pixel data failed" );
// fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
}
// implied else, fread successful
fclose(fp);
return img;
} // end function: readPPM
void writePPM(struct PPMImage *img)
{
FILE *fp;
//open file for output
fp = fopen("ukraine.ppm", "wb");
if (!fp)
{
perror( "fopen for ukraine.ppm for write failed" );
// fprintf(stderr, "Unable to open file '%s'\n",filename);
exit(1);
}
// implied else, fopen successful
//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);
} // end function: writePPM
void changeColorPPM(struct PPMImage *img)
{
unsigned 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;
}
}
} // end function: changeColorPPM
int main()
{
struct PPMImage *image;
image = readPPM();
changeColorPPM(image);
writePPM(image);
printf("Press any key...");
getchar();
return(0);
} // end function: main
#包括
#包括
结构PPMPixel
{
焦红;
炭蓝;
炭绿;
};
结构PPMImage
{
无符号x,y;
结构PPMPixel*数据;
};
#定义创建者“RPFELGUEIRAS”
#定义RGB_组件_颜色(255)
静态结构PPMImage*readPPM()
{
字符buff[16];
结构PPMImage*img;
文件*fp;
INTC;
int rgb_comp_color;
//打开PPM文件进行读取
fp=fopen(“乌克兰百万分之几”,“rb”);
如果(!fp)
{
perror(“fopen for ukraine.ppm失败”);
//fprintf(stderr,“无法打开文件“%s”\n”,文件名);
出口(1);
}
//暗示els,e fopen成功
//读取图像格式//获取图像文件的前15个字节
如果(!fgets(buff,sizeof(buff),fp))
{
perror(“ukraine.ppm”);
出口(1);
}
//否则,fgets将获得成功
//检查图像格式
如果(buff[0]!=“P”| buff[1]!=“6”){
fprintf(stderr,“无效图像格式(必须为'P6'))\n”);
出口(1);
}
//否则,图像文件具有正确的格式指示符
//图像分配存储器
img=malloc(sizeof(struct-PPMImage));
如果(!img)
{
perror(“PPMImage的malloc失败”);
//fprintf(stderr,“无法分配内存”);
出口(1);
}
//否则,malloc成功了
//查看评论
c=getc(fp);
而(c='#')
{
而((c=getc(fp))!='\n');
}
//读取图像大小信息
如果(fscanf(fp,%u%u),&img->x,&img->y)!=2)
{
perror(“乌克兰的fscanf.ppm图像大小参数失败”);
出口(1);
}
//否则,fscanf成功
//读取rgb组件
如果(fscanf(fp、%d、&rgb\u comp\u color)!=1)
{
perror(“乌克兰rgb_comp_color的fscanf.ppm失败”);
//fprintf(stderr,“无效的rgb组件(加载“%s”时出错)”\n,“文件名”;
出口(1);
}
//否则,fscanf成功
//检查rgb组件深度
if(rgb_组件颜色!=rgb_组件颜色)
{
fprintf(stderr,“'ukraine.ppm'没有8位组件\n”);
出口(1);
}
//隐含else,rgb组件深度正确
//消耗生产线的剩余部分
while(fgetc(fp)!='\n');
//像素数据的内存分配
img->data=malloc(img->x*img->y*sizeof(struct-PPMPixel));
如果(!img->data)/data,3*img->x,img->y,fp)!=img->y)
{
perror(“像素数据的fread失败”);
//fprintf(stderr,“加载映像“%s”时出错,\n”,文件名);
出口(1);
}
//否则,fread成功了
fclose(fp);
返回img;
}//结束函数:readPPM
void writePPM(结构PPMImage*img)
{
文件*fp;
//打开文件进行输出
fp=fopen(“乌克兰ppm”,“wb”);
如果(!fp)
{
perror(“fopen for ukraine.ppm for write failed”);
//fprintf(stderr,“无法打开文件“%s”\n”,文件名);
出口(1);
}
//否则,fopen成功了
//写入头文件
//图像格式
fprintf(fp,“P6\n”);
//评论
fprintf(fp,#由%s\n创建,创建者);
//图像大小
fprintf(fp,“%d%d\n”,img->x,img->y);
//rgb分量深度
fprintf(fp,“%d\n”,RGB\u分量\u颜色);
//像素数据
fwrite(img->data,3*img->x,img->y,fp);
fclose(fp);
}//结束函数:writePPM
void changeColorPPM(结构PPMImage*img)
{
未签名的i;
如果(img)
{
对于(i=0;ix*img->y;i++)
{
img->数据[i]。红色=RGB\U组件\U颜色-img->数据[i]。红色;
img->data[i]。绿色=RGB_COMPONENT_COLOR-img->data[i]。绿色;
img->data[i]。蓝色=RGB_组件_COLOR-img->data[i]。蓝色;
}
}
}//结束函数:changeColorPPM
int main()
{
结构PPMImage*图像;
image=readPPM();
改变颜色PPM(图像);
writePPM(图像);
printf(“按任意键…”);
getchar();
返回(0);
}//结束函数:main
几个字符串缺少引号。似乎如果要传递文件名参数,就应该实际使用它。也不能将默认参数后跟非默认参数。这些问题都不是PPM问题,它们只是不正确的C。为了避免混淆,请在每个左大括号“{”之后缩进(比如4)个空格,在每个右大括号“}之前取消缩进'即使在'if','do','while','else'语句后面没有大括号,也要使用这种一致的缩进。这是第18行?这是第137行?一般来说,不要'typedef'结构定义。它只会使代码混乱,导致我们理解错误,并使编译器名称空间混乱。没有'default'参数值例如printf、fgets、getchar等使用默认值的函数缺少stdio.h的#include(也就是说,返回值和所有参数都被认为是int’谢谢,当我编译它的时候,它说这个像素数据的fread失败了:成功,我想这意味着它工作了。我不知道它在哪里创建了被操纵的图像?@user3629249
#include <stdio.h>
#include <stdlib.h>
struct PPMPixel
{
char red;
char blue;
char green;
};
struct PPMImage
{
unsigned x, y;
struct PPMPixel *data;
};
#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR (255)
static struct PPMImage *readPPM()
{
char buff[16];
struct PPMImage *img;
FILE *fp;
int c;
int rgb_comp_color;
//open PPM file for reading
fp = fopen("ukraine.ppm", "rb");
if (!fp)
{
perror( "fopen for ukraine.ppm failed" );
// fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
// implied els,e fopen successful
//read image format // get first 15 bytes of image file
if (!fgets(buff, sizeof(buff), fp))
{
perror("ukraine.ppm");
exit(1);
}
// implied else, fgets successful
//check the image format
if (buff[0] != 'P' || buff[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
exit(1);
}
// implied else, image file file has correct format indicators
//alloc memory for image
img = malloc(sizeof(struct PPMImage));
if (!img)
{
perror( "malloc for PPMImage failed" );
//fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
// implied else, malloc successful
//check for comments
c = getc(fp);
while (c == '#')
{
while( (c = getc(fp)) != '\n') ;
}
//read image size information
if (fscanf(fp, "%u %u", &img->x, &img->y) != 2)
{
perror( "fscanf for ukraine.ppm image size parameters failed" );
exit(1);
}
// implied else, fscanf successful
//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1)
{
perror( "fscanf for rgb_comp_color in ukraine.ppm failed" );
//fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}
// implied else, fscanf successful
//check rgb component depth
if (rgb_comp_color!= RGB_COMPONENT_COLOR)
{
fprintf(stderr, "'ukraine.ppm' does not have 8-bits components\n");
exit(1);
}
// implied else, rgb component depth correct
// consume rest of line
while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = malloc(img->x * img->y * sizeof( struct PPMPixel));
if ( !img->data ) //< corrected element being checked
{
perror( "malloc for pixel data failed" );
//fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
// implied else, malloc successful
//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y)
{
perror( "fread for pixel data failed" );
// fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
}
// implied else, fread successful
fclose(fp);
return img;
} // end function: readPPM
void writePPM(struct PPMImage *img)
{
FILE *fp;
//open file for output
fp = fopen("ukraine.ppm", "wb");
if (!fp)
{
perror( "fopen for ukraine.ppm for write failed" );
// fprintf(stderr, "Unable to open file '%s'\n",filename);
exit(1);
}
// implied else, fopen successful
//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);
} // end function: writePPM
void changeColorPPM(struct PPMImage *img)
{
unsigned 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;
}
}
} // end function: changeColorPPM
int main()
{
struct PPMImage *image;
image = readPPM();
changeColorPPM(image);
writePPM(image);
printf("Press any key...");
getchar();
return(0);
} // end function: main