在我的图像类中,我想在将像素传递给图像后更改它们,它们仍然应该更改图片。
int main(int argc, char* argv[]){
Image theImage(4, 8);//width/height
Pixel aPixel(2,1);
Pixel* p = &aPixel;
theImage.setPixel(p);
aPixel.setBlue(100);//change the blue (RGB) value of the pixel, but Pixel color doesnt change
theImage.saveAsPixelMap("/Users/dan/Desktop/test.ppm");
return 0;
}
我以为Pixel的颜色会改变,因为Imageclass持有指针,当指针仍然指向同一个Pixel时,改变了哪个Color,Image中Pixel的Color不应该改变吗?
这是 Pixel 构造函数:
Pixel::Pixel(int tx, int ty){
red = 255;
green = 0;
blue = 0;
x = tx;
y = ty;
hasBeenChanged = false;
}
和 setPixel 方法
void Image::setPixel(Pixel *aPixel){
int tX = aPixel->getX();
int tY = aPixel->getY();
imageData.at(tY).at(tX)->setRed(aPixel->getRed());//value 0 - 255
imageData.at(tY).at(tX)->setGreen(aPixel->getGreen());
imageData.at(tY).at(tX)->setBlue(aPixel->getBlue());
}
imageData 看起来像这样
std::vector< std::vector<Pixel*> > imageData;
和 saveAsPixelmap 方法。
void Image::saveAsPixelMap(char aPath[]){
std::ofstream myfile;
myfile.open(aPath);
myfile << "P3\n" << this->getWidth() <<" "<< this->getHeight() <<"\n255\n";
std::vector < Pixel* > row;
for (int y = 0; y < this->getHeight(); y++){
row = imageData.at(y);
for (int x = 0; x < this->getWidth(); x++){
myfile << row.at(x)->getRed() << " ";
myfile << row.at(x)->getGreen() << " ";
myfile << row.at(x)->getBlue() << " ";
std::cout <<"rot: "<< imageData.at(y).at(x)->getRed();
}
}
std::cout << "\n Writing File to " << aPath << "\n \n";
myfile.close();
}
好的,这是很多代码,如果您需要更多关于某事的信息,或者我的问题不够清楚,请询问我。任何提示如何解决这个非常感谢