C 如何将ppm文件的高度和宽度加倍

C 如何将ppm文件的高度和宽度加倍,c,ppm,C,Ppm,我正在尝试构建一个非常基本的PPM图片编辑器。我已经把它放到了可以打开文件的地方,然后在另一个文件中打印出图片的副本。现在我正在努力扩大/缩小图片。现在,我正试图找出如何将PPM文件的大小增加一倍,使图片比以前大2倍。这都在命令行上,因为它不太复杂,而且我对编程也很陌生。有人知道我该怎么做吗?下面是我用来读取/打印文件的代码(从这里的其他人那里): #包括 #包括 类型定义结构{ 无符号字符红色、绿色、蓝色; }PPMPixel; 类型定义结构{ int x,y; PPMPixel*数据; }P

我正在尝试构建一个非常基本的PPM图片编辑器。我已经把它放到了可以打开文件的地方,然后在另一个文件中打印出图片的副本。现在我正在努力扩大/缩小图片。现在,我正试图找出如何将PPM文件的大小增加一倍,使图片比以前大2倍。这都在命令行上,因为它不太复杂,而且我对编程也很陌生。有人知道我该怎么做吗?下面是我用来读取/打印文件的代码(从这里的其他人那里):

#包括
#包括
类型定义结构{
无符号字符红色、绿色、蓝色;
}PPMPixel;
类型定义结构{
int x,y;
PPMPixel*数据;
}PPMImage;
#定义创建者“RPFELGUEIRAS”
#定义RGB_组件_颜色255
静态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]!=“6”){
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);
}
//读取rgb组件
如果(fscanf(fp、%d、&rgb\u comp\u color)!=1){
fprintf(stderr,“无效的rgb组件(加载“%s”时出错)”\n,“文件名”;
出口(1);
}
//检查rgb组件深度
if(rgb_组件颜色!=rgb_组件颜色){
fprintf(stderr,“'%s'没有8位组件\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 writePPM(常量字符*文件名,PPMImage*img)
{
文件*fp;
//打开文件进行输出
fp=fopen(文件名,“wb”);
如果(!fp){
fprintf(stderr,“无法打开文件“%s”\n”,文件名);
出口(1);
}
//写入头文件
//图像格式
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);
}
无效更改颜色ppm(PPMImage*img)
{
int 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]。蓝色;
}
}
}
int main(){
PPMImage*图像;
image=readPPM(“can_bottom.ppm”);
改变颜色PPM(图像);
writePPM(“can_bottom2.ppm”,图像);
printf(“按任意键…”);
getchar();
}

你的问题有点宽泛。你有什么特别的问题吗?同上。第一次尝试将大小加倍可能是复制中间像素,也就是说,没有更好的有效分辨率。当然,您也需要更改标题信息。下一个尝试是平均相邻像素的中间像素的R、G、B分量。但是,如果不将图像文件解码为相关结构和数组,而不是将读取的内容写入输出,则无法执行任何操作。
#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();
}