0

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;
            }

Abel Pandian
  • 93
  • 1
  • 6
  • 2
    Well, it seems to me you're turning your 24-pixel 3-byte into a 8 all-black or all-white pixels stored into a single byte. What am I missing? – Paul Evans Sep 10 '13 at 08:59
  • 1
    It is not totally clear to me, what you want to achieve. Do you want a 8bpp monochrome image or grayscale image? At the moment, you are calculating a monochrome 8bpp. Using a static threshold can lead to a totally black or totally white image. It is better to calculate at least the average gray. Best of course would be the Otzu. – Martin Schlott Sep 10 '13 at 09:09
  • In case you are changing opinion about the use of third party libraries, [OpenCV](http://opencv.org/) are multiplatform and quite simple to use. For the Otzu method see also [here](http://stackoverflow.com/questions/17141535/how-to-use-the-otsu-threshold-in-opencv) and [here](http://stackoverflow.com/questions/12953993/otsu-thresholding-for-depth-image) – Paolo Gibellini Sep 11 '13 at 12:56

0 Answers0