我想将 24bpp 位图文件转换为 8bpp 位图文件。源文件已转换为单色图像(以编程方式),现在我必须将其更改为 8bpp 格式。我使用的是 ubuntu 10.10,所以我不能使用 Windows 库。我也不想使用第三方库。这是在 C++ 中。我尝试了很多方法,但在读取 3 个字节并写入 1 个字节时总是出错。最终结果始终包含全白图像。请帮助解决这个问题。
unsigned char* currentPixel;
unsigned char* currentRow;
for(int i = 0; i < height; i++)
{
currentRow = &pixels[i * rowStride];
for(int j = 0; j < width; j++)
{
currentPixel = &pixels[i * rowStride + j * n_channels];
float fThreshold = 125.0f;
float g = (float)currentPixel[0];
float b = (float)currentPixel[1];
float r = (float)currentPixel[2];
float rMul = 0.299f; //0.212f
float gMul = 0.587f; //0.7154f
float bMul = 0.114f; //0.0721f
float f = ( rMul * r) + ( gMul * g) + ( bMul * b);
float fPixel = 0.0;
if(f > fThreshold)
{
currentPixel[0] = (0xFF);
currentPixel[1] = (0xFF);
currentPixel[2] = (0xFF);
newData->push_back((0xFF));
zeroCount++;
}
else
{
currentPixel[0] = (0x00);
currentPixel[1] = (0x00);
currentPixel[2] = (0x00);
newData->push_back((0x00));
oneCount++;
}
//newData->push_back((uint8_t)currentPixel[0]);
//newData->push_back((uint8_t)currentPixel[1]);
//newData->push_back((uint8_t)currentPixel[2]);
//cout<<"f : "<<f<<" fPixel : "<<fPixel<<endl;
}