4

我可能需要一些帮助来弄清楚如何提供下面的过程。我需要编写一个单色 BMP 文件。下面的代码(来自:如何在 windows C++ 中将单色图像另存为 bmp ?)看起来能够做到这一点。我现在坚持如何将 astd::bitset或最好boost::dynamic_bitset转换为这种byte*格式。到目前为止,我所有的尝试都失败了,我无法将 8x8 棋盘格模式写入 BMP。proc 创建了 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);
4

3 回答 3

3

您拥有的代码非常接近。这里有一些关于它可能在哪里关闭的想法。

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);
}
于 2014-12-25T04:10:05.197 回答
1

仅用于存档,低于工作版本。它需要一个提升位集作为输入像素存储。

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();

}

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

于 2014-12-26T17:56:42.350 回答
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。要测试一个字节中的位,请执行您要从 0 到 7测试的位在myByte >> position & 1哪里。position

于 2014-12-25T00:07:14.787 回答