C ++将24bpp位图转换为c ++中的8bpp位图

时间:2013-09-10 08:48:36

标签: c++ image-processing bitmap image-manipulation

我想将24bpp位图文件转换为8bpp位图文件。源文件已转换为单色图像(编程),现在我必须将其更改为8bpp格式。我使用的是ubuntu 10.10所以我不能使用windows libs。此外,我不想使用第三方库。这是用C ++编写的。我尝试了很多方法,但在读取3个字节并写入一个字节时总是出错。最终结果始终包含全白图像。请帮助解决这个问题。

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

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

0 个答案:

没有答案