I am currently writing a BMP screenshot function for an OpenGL framework and am running into an odd issue writing the data out. The data being brought in from glReadPixels along with the BMP headers are all correct, but it seems the ofstream.write operation is randomly inserting invalid values.
Excerpts from a correct BMP of the image (created by Paint) and that of the one made by my function follows, highlighting the incorrect bytes. The row that is correct will always be the first one.
Row 0x08C
3B 0D 4A 3A 0A 48 38 08 45 36
3B 0D 4A 3A 0D 0A 48 38 08 45
^^
Row 0x0DC (already off by one at this point)
3E 2F 07 3D 2E 0A 3F 31 0E 44
07 3E 2F 07 3D 2E 0D 0A 3F 31
^^
Row 0x0E68 (very next row, and off by two)
35 13 48 3A 10 44 36 0A 3F 31
0E 44 35 13 48 3A 10 44 36 0D
^^
So there seems to a pattern where the invalid value is always 0x0D and it is inserted in front of a 0x0A. I have no idea why this might be happening, as I have confirmed that the headers and data from glReadPixels are all correct. The code for the method follows.
bool captureScreen( const char* name, unsigned originX, unsigned originY, unsigned width, unsigned height )
{
//...
GLubyte* imageData = new GLubyte[ width * height * 3 ];
glReadPixels( originX, originY, width, height, GL_BGR, GL_UNSIGNED_BYTE, imageData );
//...
captureAsBMP( imageData, width, height, path.c_str( ) );
//...
}
bool captureAsBMP( GLubyte* data, GLuint width, GLuint height, const char* path )
{
std::ofstream stream( path, std::ios_base::out );
if( !stream.is_open( ) )
return false;
unsigned char BMPHeader[ 14 ] = { /* cut for brevity */ };
unsigned char DIBHeader[ 40 ] = { /* cut for brevity */ };
unsigned char padding[ 4 ] = { 0x00, 0x00, 0x00, 0x00 };
unsigned int paddingLength = 4 - (( width * 3 ) % 4);
paddingLength = ( paddingLength == 4 ? 0 : paddingLength );
long fileSize = ( width * height * 3 ) + ( paddingLength * height ) + 54;
long dataSize = fileSize - 54;
memcpy( &BMPHeader[ 2 ], &fileSize, sizeof( long ) );
memcpy( &DIBHeader[ 4 ], &width, sizeof( unsigned int ) );
memcpy( &DIBHeader[ 8 ], &height, sizeof( unsigned int ) );
memcpy( &DIBHeader[ 20 ], &dataSize, sizeof( long ) );
stream.write( reinterpret_cast< char* >( BMPHeader ), 14 );
stream.write( reinterpret_cast< char* >( DIBHeader ), 40 );
unsigned pos = 0;
// Write out one row at a time
for( int i = 0; i < height; i++ )
{
stream.write( reinterpret_cast< char* >( &data[ pos ] ), ( width * 3 ) );
// Is there padding that needs to be added?
if( paddingLength != 0 )
stream.write( reinterpret_cast< char* >( padding ), paddingLength );
// Update where we are in data
pos += width * 3;
}
stream.close( );
return true;
}
Also of note, the image in question is a 800×600 and so this error is occurring during the first stream write of the rows, prior to padding and pos incrementing.
Finally, how it should appear: https://i.stack.imgur.com/APfPJ.jpg
And how it does: https://i.stack.imgur.com/AHaRS.jpg (re-saved in Paint as Imgur complained of it being corrupted of course)
You need to open your files in binary mode by adding the
std::ios::binaryflag to avoid CRLF conversion. On Windows, newlines are represented as the two bytes 0x0D 0x0A (carriage return + linefeed, or CR LF)—the C and C++ runtimes automatically translate those into just plain linefeeds on input, and they automatically translate plain linefeeds'\n'into CRLF pairs on output, when files are opened in text mode, which is the default.To suppress these newline conversions, you need to tell the runtime not to translate. With C++’s
iostreams, this is done with thestd::ios::binaryflag; with C’sFILE*streams, this is done with the"b"mode flag, e.g."rb"for reading or"wb"for writing.