Jump to content

  • Log In with Google      Sign In   
  • Create Account

MaxFire

Member Since 09 Jun 2011
Offline Last Active Today, 08:59 AM

Posts I've Made

In Topic: Efficiently when compressing char* to std::string

11 December 2014 - 11:08 AM

I feel like a tool... it was that simple *dies inside*. Thank you so much


In Topic: Building Boost with zlib

10 December 2014 - 10:38 AM

You are a legend my friend, worked like a charm


In Topic: C++ Serialize OpengGL pixel data in bitmap formatted byte array

21 October 2014 - 02:17 PM

Sorry to post again but I am unable to find the answer to this anywhere.

 

In my c++ server application when I save my bitmap to a file using an ofstream in binary mode and then read that file in c# using File.ReadAllBytes which returns a BYTE array the bytes read in are different than when I send my data (unsigned char*) over a network from the server and read them as bytes on the c# client. 

 

I know a byte in c# is an unsigned integer of 8 bits and an unsigned char is the same as uint_8 so why is the data different? What's the best approach to send my data to c# in the correct format? Am I missing some sort of encoding?

 

Heres some server code that shows the writing to memory

void MyBitmap::WriteToFile( std::string sFileName )
{
	std::ofstream file;
	file.open( sFileName.c_str(), std::ios::binary );

	//write headers
	file.write( ( char* ) ( &_header ), sizeof( _header ) );
	file.write( ( char* ) ( &_info ), sizeof( _info ) );

	//write pixel data
	file.write( reinterpret_cast<char*>( &_pPixelData[0] ), _sPixelDataSize );
	//file.write( ( &_pPixelData[ 0 ] ), _sPixelDataSize );

	file.close();
}

MyBitmapMemoryBuffer* MyBitmap::WriteToMemoryChar()
{
	size_t stHeaderSize = sizeof( BITMAPFILEHEADER );
	size_t stInfoSize = sizeof( BITMAPINFOHEADER );
	size_t stTotalImageSize = stHeaderSize + stInfoSize + _sPixelDataSize;

	MyBitmapMemoryBuffer* pMem = new MyBitmapMemoryBuffer();
	pMem->pMemory = new char[ stTotalImageSize ];
	pMem->size = stTotalImageSize;

	//write headers
	std::memcpy( &pMem->pMemory[ 0 ], ( char* ) ( &_header ), stHeaderSize );
	std::memcpy( &pMem->pMemory[ stHeaderSize ], ( char* ) ( &_info ), stInfoSize );

	//write pixels
	std::memcpy( &pMem->pMemory[ stHeaderSize + stInfoSize + 1 ], reinterpret_cast<char*>( &_pPixelData[ 0 ] ), _sPixelDataSize );

	return pMem;
}

MyBitmapMemoryBuffer* MyBitmap::WriteToMemoryByte()
{
	size_t stHeaderSize = sizeof( BITMAPFILEHEADER );
	size_t stInfoSize = sizeof( BITMAPINFOHEADER );
	size_t stTotalImageSize = stHeaderSize + stInfoSize + _sPixelDataSize;
	 
	MyBitmapMemoryBuffer* pMem = new MyBitmapMemoryBuffer();
	pMem->pMemoryByte = new BYTE[ stTotalImageSize ];
	pMem->size = stTotalImageSize;
	 
	//write headers
	std::memcpy( &pMem->pMemoryByte[ 0 ], ( BYTE* ) ( &_header ), stHeaderSize );
	std::memcpy( &pMem->pMemoryByte[ stHeaderSize ], ( BYTE* ) ( &_info ), stInfoSize );
	 
	//write pixels
	std::memcpy( &pMem->pMemoryByte[ stHeaderSize + stInfoSize + 1 ], ( &_pPixelData[ 0 ] ), _sPixelDataSize );
	 
	return pMem;
}

Here's the c# code that reads the data

        protected void ChekcData()
        {
            int messageSize = 240054;
            int readSoFar = 0;
            byte[] msg = new byte[ messageSize ];

            while ( readSoFar < messageSize )
            {
                var read = stream.Read( msg, readSoFar, msg.Length - readSoFar );
                readSoFar += read;
                if ( read == 0 )
                    break;   // connection was broken
            }

			//compare
            byte[] tmp = File.ReadAllBytes( @"C:\Users\Max\Documents\Git Repositories\Advanced Programming\test.bmp" );

            for ( int i = 0; i < tmp.Length; i++ )
            {
                if ( tmp[ i ] != msg[ i ] )
                {
                    textBox.Text = "not same";
                }
            }
        }

In Topic: C++ Serialize OpengGL pixel data in bitmap formatted byte array

21 October 2014 - 05:02 AM

*face smash* just kill me now xD. Thank you wintertime


In Topic: C++ Serialize OpengGL pixel data in bitmap formatted byte array

20 October 2014 - 04:14 PM

Sorry for yet another post, after 6 hours I'm pulling my hair out.. So everything seemed to work fine until I started looking at the bitmap file/buffer generated. The colour is different from what is actually rendered. I have searched high and low for Bitmap Headerfile examples but can't find any example that are hugely different from my implementation. Here is the code I use to save to a file and a screenshot.

 

Capture_zps7d44b0ed.jpg

MyBitmap::MyBitmap( int iWidth, int iHeight, int iNoOfBits, BYTE* pPixelData, size_t sPixelDataSize  )
{
	_header.bfType = 0x4d42;
	_header.bfSize = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + sPixelDataSize;
	_header.bfReserved1 = 0;
	_header.bfReserved2 = 0;
	_header.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );

	_info.biSize = sizeof( _info );
	_info.biWidth = iWidth;
	_info.biHeight = iHeight;
	_info.biPlanes = 1;
	_info.biBitCount = 24;//iNoOfBits;
	_info.biCompression = BI_RGB;
	_info.biSizeImage = iWidth* iHeight * 3;
	_info.biXPelsPerMeter = 0;
	_info.biYPelsPerMeter = 0;
	_info.biClrUsed = 0;
	_info.biClrImportant = 0;
	_info.biXPelsPerMeter = 0;
	_info.biYPelsPerMeter = 0;

	_pPixelData = pPixelData;
	_sPixelDataSize = sPixelDataSize;
}

MyBitmap::~MyBitmap()
{
	if ( _pPixelData ) delete _pPixelData;
}

void MyBitmap::WriteToFile( std::string sFileName )
{
	std::ofstream file;
	file.open( sFileName.c_str(), std::ios::binary );

	//write headers
	file.write( ( char* ) ( &_header ), sizeof( _header ) );
	file.write( ( char* ) ( &_info ), sizeof( _info ) );

	//write pixel data
	file.write( reinterpret_cast<char*>( &_pPixelData[0] ), _sPixelDataSize );

	file.close();
}

And here is how its used

		glReadPixels( 0, 0, g_iScreenWidth, g_iScreenHeight, GL_RGB, GL_UNSIGNED_BYTE, g_pixels );

		MyBitmap bitmap( g_iScreenWidth, g_iScreenHeight, 24, g_pixels, 3 * g_iScreenWidth * g_iScreenHeight );

		bitmap.WriteToFile( "test.bmp" );

The only thing I can think of is that I need to specify an RGB colour mask but that would require switching to BITMAPV4HEADER structure, which I have yet to see anyone recommend to use. 

 

I am missing something painfully obvious?


PARTNERS