0

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

4

0 に答える 0