I want to convert 24bpp bitmap file to a 8bpp bitmap file. The Source file has been converted to monochrome image(programattically) and now i have to change it to a 8bpp format. I am using ubuntu 10.10 so i cannot use windows libs. Also i don't want to use third party libs. This is in C++. I tried many methods but i always go wrong when reading the 3 bytes and writing the one byte. The end result always contain a full white image. Please help with this issue.
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;
}