【发布时间】:2015-03-08 06:14:37
【问题描述】:
尝试通过索引包含像素数据的指针将 PPM 图像转换为灰度:
void PPMObject::greyScale()
{
const float r = 0.299F;
const float g = 0.587F;
const float b = 0.114F;
int size = this->width * this->height * 3;
for (int i = 0; i < size; i++)
{
this->m_Ptr[i] = (this->m_Ptr[i] * r) + (this->m_Ptr[i] * g) + (this->m_Ptr[i] * b);
this->m_Ptr[i+1] = (this->m_Ptr[i+1] * r) + (this->m_Ptr[i+1] * g) + (this->m_Ptr[i+1] * b);
this->m_Ptr[i+2] = (this->m_Ptr[i+2] * r) + (this->m_Ptr[i+2] * g) + (this->m_Ptr[i+2] * b);
}
}
我使用 >> 重载读取了 PPM 图像文件:
istream& operator >>(istream &inputStream, PPMObject &other)
{
inputStream >> other.magicNum >> other.width >> other.height >> other.maxColorValue;
inputStream.get();
size_t size = other.width * other.height * 3;
other.m_Ptr = new char[size];
inputStream.read(other.m_Ptr, size);
return inputStream;
}
我写数据如下:
ostream& operator <<(ostream &outputStream, const PPMObject &other)
{
outputStream << other.magicNum << " "
<< other.width << " "
<< other.height << " "
<< other.maxColorValue << " "
;
outputStream.write(other.m_Ptr, other.width * other.height * 3);
return outputStream;
}
读取或写入 PPM 文件没有问题。
问题只是将 PPM 图像转换为灰度 - 索引不是方法。该文件未更改。
问题很可能是:如何从指针中获取值来操作它们?
例如,char 指针中的像素在哪里?
平均 RGB 分量值当然是另一种方法,但是如何将平均值分配回指针?
【问题讨论】:
标签: c++ pointers grayscale ppm