简体   繁体   English

反转PPM图像的值

[英]Inverting the Values of a PPM image

I am writing in C and I have to write code that will invert the RGB values for each pixel in an image. 我正在用C编写代码,我必须编写代码来反转图像中每个像素的RGB值。 It's a simple process in that you take the max color value and subtract the actual RGB values. 这是一个简单的过程,您可以获取最大颜色值并减去实际RGB值。 I have successfully read in the max color value, but when trying to invert the values, everything is returned as 0 and when written to a new file is not readable. 我已经成功读取了最大颜色值,但是当尝试反转颜色值时,所有内容都返回0,并且在写入新文件时不可读。 Below is the code, any ideas? 下面是代码,有什么想法吗?

Inverting the picture 反转图片

int i,j;
for(i=0; i<myPic->rows;++i) {
    //Moves up and down the columns reaching all Pixels
    //Moves across left to right across all columns
    for (j=0;j<myPic->cols;++j) {
    //Inverstion requires the actual value to be subtracted from the max
        myPic->pixels[i][j].red = myPic->colors - myPic->pixels[i][j].red;
        myPic->pixels[i][j].green = myPic->colors - myPic->pixels[i][j].green;
        myPic->pixels[i][j].blue = myPic->colors - myPic->pixels[i][j].blue;
        }
    }
return myPic;

} }

Output of the image 图像输出

fprintf(name,"P3\n%d %d\n%d\n",myPic->rows,myPic->cols,myPic->colors);
//The P3,rows,columns,and color values are all printed first
int i,j;
for(i=0; i< myPic->rows;++i) {
        for(j=0; j<myPic->cols;++j) { //Each Pixel is printed one at a time
        fprintf(name,"%d",myPic->pixels[i][j].red); //Red printed first
        fprintf(name,"%d",myPic->pixels[i][j].green); //Green printed second
        fprintf(name,"%d",myPic->pixels[i][j].blue); //Blue printed third
        fprintf("\n");
        }
    }

} }

Thanks for the help guys, this is what I'm using now and it works 感谢您的帮助,这就是我现在正在使用的并且可以正常工作

When writing the pixel data, this line 写入像素数据时,此行

myPic->pixels[i] = malloc(sizeof(Pixel) *myPic->cols);

overwrites the existing pointer, and makes it point to new (and more importantly, uninitialized) data. 覆盖现有的指针,并使其指向新的(更重要的是未初始化的)数据。

Copy-paste programming (which you seem to have been doing) can work sometimes, but you have to take care to modify the copied code properly. 复制粘贴编程(您似乎一直在进行)有时可以工作,但是您必须注意正确修改复制的代码。


On an unrelated note, you don't print a newline after each line, so the resulting PPM file will actually not be correct. 在不相关的注释上,您不必在每行之后打印换行符,因此生成的PPM文件实际上将是不正确的。

@Joachim is correct. @Joachim是正确的。 so do something like this: 所以做这样的事情:

int i,j;
for(i=0; i<myPic->rows;++i) {

    Pixel[myPic->cols] temp = *myPic->pixels[i];

    //Moves up and down the columns reaching all Pixels
    myPic->pixels[i] = malloc(sizeof(Pixel) *myPic->cols);
    //Moves across left to right across all columns
    for (j=0;j<myPic->cols;++j) {
    //Inverstion requires the actual value to be subtracted from the max
        myPic->pixels[i][j].red = myPic->colors - temp[j].red;
        myPic->pixels[i][j].green = myPic->colors - temp[j].green;
        myPic->pixels[i][j].blue = myPic->colors - temp[j].blue;
        }
    }
    return myPic;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM