简体   繁体   English

C ++位图编辑

[英]C++ bitmap editing

I am trying to open a bitmap file, edit it, and then save the edited version as a new file. 我试图打开一个位图文件,对其进行编辑,然后将编辑后的版本另存为新文件。 This is eventually to mess with using steganography. 最终这将使使用隐写术变得混乱。 I am trying to save the bitmap information now but the saved file will not open. 我正在尝试保存位图信息,但是保存的文件无法打开。 No errors in compilation or run time. 编译或运行时无错误。 It opens fine and the rest of the functions work. 它可以正常打开,其余功能正常工作。

void cBitmap::SaveBitmap(char * filename)
{
    // attempt to open the file specified
    ofstream fout;

    // attempt to open the file using binary access
    fout.open(filename, ios::binary);

    unsigned int number_of_bytes(m_info.biWidth * m_info.biHeight * 4);
    BYTE red(0), green(0), blue(0);

    if (fout.is_open())
    {
        // same as before, only outputting now
        fout.write((char *)(&m_header), sizeof(BITMAPFILEHEADER));
        fout.write((char *)(&m_info), sizeof(BITMAPINFOHEADER));

        // read off the color data in the bass ackwards MS way
        for (unsigned int index(0); index < number_of_bytes; index += 4)
        {
            red = m_rgba_data[index];
            green = m_rgba_data[index + 1];
            blue = m_rgba_data[index + 2];

            fout.write((const char *)(&blue), sizeof(blue));
            fout.write((const char *)(&green), sizeof(green));
            fout.write((const char *)(&red), sizeof(red));
        }


    }
    else
    {
        // post file not found message
        cout <<filename << " not found";
    }
    // close the file
    fout.close();
}

You're missing the padding bytes after each RGB row. 您在每个RGB行之后都缺少填充字节。 The rows have to be a multiple of 4 bytes each. 每行必须是4字节的倍数。

Also, are you supposed to be writing a 24 or 32-bit bmp file? 另外,您是否应该编写24或32位bmp文件? If you're writing 24-bit, you're just missing padding. 如果您正在编写24位,则只是缺少填充。 If you're writing 32-bit, then you're missing each extra byte (alpha). 如果您正在编写32位,那么您将丢失每个额外的字节(alpha)。 Not enough information to fix your code sample short of writing a complete bmp writer that would support all possible options. 没有足够的信息来解决您的代码示例,而没有编写支持所有可能选项的完整bmp编写器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM