Exemplo n.º 1
0
void savePicAsHeightMap( std::string const& infile, std::string const& outfile ) {

	ImageIO::init();

	BitMap*	in	= ImageIO::load( infile );
	BitMap* out	= new BitMap( in->getWidth(), in->getHeight() );

	for( int j=0; j<in->getHeight(); j++ ) {
		for( int i=0; i<in->getWidth(); i++ ) {
			unsigned int col = in->get( i, j );
			int r = ( col >> 16 ) & 0xff;
			int g = ( col >> 8 ) & 0xff;
			int b = col & 0xff;
			int v = (int)( (double)r * 0.2125 + (double)g * 0.7154 + (double)b * 0.0721 ) & 0xff;
			out->set( i, j, ( 0xff << 24 ) | ( v << 16 ) | ( v << 8 ) | v );
		}
	}

	ImageIO::save( outfile, *out );

	delete out;
	delete in;
}
Exemplo n.º 2
0
bool BMP::save( std::string const& filename, BitMap const& map ) {

	FILE*	f	= fopen( filename.c_str(), "wb" );

	assert( f );

	int	width	= map.getWidth();
	int	height	= map.getHeight();

	// bitmap file header
	unsigned short	bfType			= 0x4D42;								// "BM"
	unsigned int	bfSize			= 54 + width * height * 3;
	unsigned short	bfReserved1		= 0;
	unsigned short	bfReserved2		= 0;
	unsigned int	bfOffbits		= 54;
	
	fwrite( &bfType, 2, 1, f );
	fwrite( &bfSize, 4, 1, f );
	fwrite( &bfReserved1, 2, 1, f );
	fwrite( &bfReserved2, 2, 1, f );
	fwrite( &bfOffbits, 4, 1, f );

	// bitmap info header
    unsigned int	biSize			= 40;							// bmih size
    int				biWidth			= width;
    int				biHeight		= height;
    unsigned short	biPlanes		= 1;
    unsigned short	biBitCount		= 24;							// RGB
    unsigned int	biCompression	= 0;							// type: RGB
    unsigned int	biSizeImage		= width * height * 3;
    int				biXPelsPerMeter	= 2925;
    int				biYPelsPerMeter	= 2925;
    unsigned int	biClrUsed		= 0;
    unsigned int	biClrImportant	= 0;

	fwrite( &biSize, 4, 1, f );
	fwrite( &biWidth, 4, 1, f );
	fwrite( &biHeight, 4, 1, f );
	fwrite( &biPlanes, 2, 1, f );
	fwrite( &biBitCount, 2, 1, f );
	fwrite( &biCompression, 4, 1, f );
	fwrite( &biSizeImage, 4, 1, f );
	fwrite( &biXPelsPerMeter, 4, 1, f );
	fwrite( &biYPelsPerMeter, 4, 1, f );
	fwrite( &biClrUsed, 4, 1, f );
	fwrite( &biClrImportant, 4, 1, f );

	// write data
	for( int j=0; j<height; j++ ) {

		for( int i=0; i<width; i++ ) {

			unsigned int	c	= map.get( i, height - j - 1 );

			unsigned char	b	= ( c >> 16 ) & 0xff;
			unsigned char	g	= ( c >> 8 ) & 0xff;
			unsigned char	r	= c & 0xff;

			fwrite( &b, 1, 1, f );
			fwrite( &g, 1, 1, f );
			fwrite( &r, 1, 1, f );
		}
	}

	fclose( f );

	return true;
}