使用位集创建单色BMP

5
我可能需要一些帮助来弄清楚如何给下面的程序提供数据。我需要编写一个单色BMP文件。下面的代码(来自:如何在Windows C++中保存单色图像为BMP?)似乎能够完成这个任务。现在,我卡在了如何将std::bitset或更好的boost::dynamic_bitset转换为byte*格式上。到目前为止,我的所有尝试都失败了,我无法将8x8的棋盘格模式写入BMP中。该程序创建了BMP文件,而且可以被Photoshop读取,但内容是混乱的。因此,如果您有任何解决方案,请建议!
Save1BppImage(byte* ImageData, const char* filename, long w, long h){

    int bitmap_dx = w; // Width of image
    int bitmap_dy = h; // Height of Image

    // create file
    std::ofstream file(filename, std::ios::binary | std::ios::trunc);
    if(!file) return;

    // save bitmap file headers
    BITMAPFILEHEADER fileHeader;
    BITMAPINFOHEADER * infoHeader;
    infoHeader = (BITMAPINFOHEADER*) malloc(sizeof(BITMAPINFOHEADER) );
    RGBQUAD bl = {0,0,0,0};  //black color
    RGBQUAD wh = {0xff,0xff,0xff,0xff}; // white color


    fileHeader.bfType      = 0x4d42;
    fileHeader.bfSize      = 0;
    fileHeader.bfReserved1 = 0;
    fileHeader.bfReserved2 = 0;
    fileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + (sizeof(BITMAPINFOHEADER));

    infoHeader->biSize          = (sizeof(BITMAPINFOHEADER) );
    infoHeader->biWidth         = bitmap_dx;    
    infoHeader->biHeight        = bitmap_dy;
    infoHeader->biPlanes        = 1;
    infoHeader->biBitCount      = 1;
    infoHeader->biCompression   = BI_RGB; //no compression needed
    infoHeader->biSizeImage     = 0;
    infoHeader->biXPelsPerMeter = 0;
    infoHeader->biYPelsPerMeter = 0;
    infoHeader->biClrUsed       = 2;
    infoHeader->biClrImportant  = 2;

    file.write((char*)&fileHeader, sizeof(fileHeader)); //write bitmapfileheader
    file.write((char*)infoHeader, (sizeof(BITMAPINFOHEADER) )); //write bitmapinfoheader
    file.write((char*)&bl,sizeof(bl)); //write RGBQUAD for black
    file.write((char*)&wh,sizeof(wh)); //write RGBQUAD for white

    int bytes = (w/8) * h ; //for example for 32X64 image = (32/8)bytes X 64 = 256;

    file.write((const char*)ImageData, bytes);

    file.close();
}

我的一个天真的想法是这样的:
    byte test[64];
for(unsigned int i=0; i<64; ++i)
    if(i % 2)
        test[i] = 0;
    else
        test[i] = 1;

Save1BppImage(test, "C:/bitmap.bmp", 8, 8);
3个回答

4
您的代码非常接近正确。以下是一些可能出错的地方。 bfOffBits 的值必须包括调色板的大小。
fileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + (sizeof(BITMAPINFOHEADER)) + 2*sizeof(RGBQUAD);

一些软件可能会将0解释为白色,而将1解释为黑色,无论调色板如何。即使文件格式可以两种方式进行,最好还是按照这种顺序指定调色板,并在必要时反转位。
每行位图都将从4字节边界开始。如果您的位图宽度不是32的倍数,则需要在每行之间添加一些填充。
BMP文件的排序是从底部行到顶部行,这与大多数人组织其数组的方式相反。
最后两个建议合并起来看起来像这样:
int bytes_in = (w + 7) / 8;
int bytes_out = ((w + 31) / 32) * 4;
const char * zeros[4] = {0, 0, 0, 0};
for (int y = h - 1;  y >= 0;  --y)
{
    file.write(((const char *)ImageData) + (y * bytes_in), bytes_in);
    if (bytes_out != bytes_in)
        file.write(zeros, bytes_out - bytes_in);
}

2

仅供存档,以下是可工作的版本。它使用一个boost bitset作为输入像素存储。

void bitsetToBmp(boost::dynamic_bitset<unsigned char> bitset, const char* filename, int width, int height){
//write the bitset to file as 1-bit deep bmp
//bit order 0...n equals image pixels  top left...bottom right, row by row
//the bitset must be at least the size of width*height, this is not checked

std::ofstream file(filename, std::ios::binary | std::ios::trunc);
if(!file) return;

// save bitmap file headers
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER * infoHeader;
infoHeader = (BITMAPINFOHEADER*) malloc(sizeof(BITMAPINFOHEADER) );
RGBQUAD bl = {0,0,0,0};  //black color
RGBQUAD wh = {0xff,0xff,0xff,0xff}; // white color


fileHeader.bfType      = 0x4d42;
fileHeader.bfSize      = 0;
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits   = sizeof(BITMAPFILEHEADER) + (sizeof(BITMAPINFOHEADER)) + 2*sizeof(RGBQUAD); 

infoHeader->biSize          = (sizeof(BITMAPINFOHEADER) );
infoHeader->biWidth         = width;    
infoHeader->biHeight        = height;
infoHeader->biPlanes        = 1;
infoHeader->biBitCount      = 1;
infoHeader->biCompression   = BI_RGB; //no compression needed
infoHeader->biSizeImage     = 0;
infoHeader->biXPelsPerMeter = 0;
infoHeader->biYPelsPerMeter = 0;
infoHeader->biClrUsed       = 2;
infoHeader->biClrImportant  = 2;

file.write((char*)&fileHeader, sizeof(fileHeader)); //write bitmapfileheader
file.write((char*)infoHeader, (sizeof(BITMAPINFOHEADER) )); //write bitmapinfoheader
file.write((char*)&bl,sizeof(bl)); //write RGBQUAD for black
file.write((char*)&wh,sizeof(wh)); //write RGBQUAD for white

// convert the bits into bytes and write the file
int offset, numBytes = ((width + 31) / 32) * 4;
byte* bytes = (byte*) malloc(numBytes * sizeof(byte));

for(int y=height - 1; y>=0; --y){
    offset = y * width;
    memset(bytes, 0, (numBytes * sizeof(byte)));
    for(int x=0; x<width; ++x)
        if(bitset[offset++]){
            bytes[x / 8] |= 1 << (7 - x % 8);
    };
    file.write((const char *)bytes, numBytes);
};
free(bytes);
file.close();

我想知道是否有更简单/更快的方法将位放入文件中?整个位集可以作为行数组进行传递,以跳过子集提取。


0

我有一些非常相似的东西...

  • 这种方法不处理BMP格式的填充。因此,您只能制作宽度为4的位图。

  • 这不是单色位图。它是RGB格式,但您可以轻松调整它。

  • 这不是您的确切答案,但肯定对您有用。

享受它。

void createBitmap( byte * imageData, const char * filename, int width, int height )
{
    BITMAPFILEHEADER bitmapFileHeader;
    memset( &bitmapFileHeader, 0, sizeof( bitmapFileHeader ) );
    bitmapFileHeader.bfType = ( 'B' | 'M' << 8 );
    bitmapFileHeader.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );
    bitmapFileHeader.bfSize = bitmapFileHeader.bfOffBits + width * height * 3;

    BITMAPINFOHEADER bitmapInfoHeader;
    memset( &bitmapInfoHeader, 0, sizeof( bitmapInfoHeader ) );
    bitmapInfoHeader.biSize = sizeof( BITMAPINFOHEADER );
    bitmapInfoHeader.biWidth = width;
    bitmapInfoHeader.biHeight = height;
    bitmapInfoHeader.biPlanes = 1;
    bitmapInfoHeader.biBitCount = 24;

    std::ofstream file( filename, std::fstream::binary );

    file.write( reinterpret_cast< char * >( &bitmapFileHeader ), sizeof( bitmapFileHeader ) );
    file.write( reinterpret_cast< char * >( &bitmapInfoHeader ), sizeof( bitmapInfoHeader ) );

    // the pixels!
    file.write( imageData, width * height * 3 );

    file.close();
}

int main( int argc, const char * argv[] )
{
    int width = 12; // multiple of 4
    int height = 12;

    byte imageData[ width * height * 3 ];

    // fill imageData the way you want, this is just a sample
    // on how to set the pixel at any specific (X,Y) position

    for ( int y = 0; y < height; ++y )
    {
        for ( int x = 0; x < width; ++x )
        {
            int pos = 3 * ( y * width + x );

            byte pixelColor = ( x == 2 && y == 2 ) ? 0x00 : 0xff;

            imageData[ pos ] = pixelColor;
            imageData[ pos + 1 ] = pixelColor;
            imageData[ pos + 2 ] = pixelColor;
        }
    }

    createBitmap( imageData, "bitmap.bmp", width, height );

    return 0;
}

在这个示例中,我们想要一个白色位图,在位置X = 2,Y = 2处有一个黑色像素。

BMP格式认为Y从底部向上增长。

如果您有一个每位一个像素的位图(真正的单色位图),您可以测试位并填充imageData。要在字节中测试位,请执行以下操作:myByte >> position & 1,其中position是您想要从0到7测试的位。


谢谢回复,但是使用这段代码后,我的确切问题仍然存在:如何填充字节数组?白色和黑色在位图中的表示形式是什么,以“byte”类型表示?我尝试了像0、0x00、1、0xff之类的值,但都失败了。您能否为您的代码创建一个4x4的黑白棋盘格字节示例? - zaskar
在我的示例中,每3个字节表示RGB通道...如果我将它们设置为R=G=B,我们就会得到一张单色图像。我无法确切地了解您的示例,因为我不知道输入位图格式以获取位。但基本上这就是您所需要的。 - Wagner Patriota
好的,但在你的情况下,R/G/B 的确切值是多少?字节 rgb[16]; rgb[0] = ... - zaskar
你是指像素位置吗?我可以给你展示一下...等一下。 - Wagner Patriota

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接