0

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

1 Answers1

0

You're missing the padding bytes after each RGB row. The rows have to be a multiple of 4 bytes each.

Also, are you supposed to be writing a 24 or 32-bit bmp file? If you're writing 24-bit, you're just missing padding. If you're writing 32-bit, then you're missing each extra byte (alpha). Not enough information to fix your code sample short of writing a complete bmp writer that would support all possible options.

Rob L
  • 2,351
  • 13
  • 23