手动生成BMP图片

来源:互联网 发布:移动云商城 源码下载 编辑:程序博客网 时间:2024/05/01 14:55

在PC上,BMP图片现在一般不用16位或者8位,所以也就不用调色板,于是前面就是固定的文件头54字节,按照标准填写进去即可。从55开始填写数据。数据的顺序是红蓝绿,数据是从高往低从左往右写入。并且每一行都要4字节对齐,不足要补齐。

#include <wtypes.h>#include <fstream>using namespace std;#define USED(x) x;#define BMP_WIDTH  1024#define BMP_HEIGHT 1024#define DIM 1024#define _sq(x) ((x)*(x)) // square#define _cb(x) abs((x)*(x)*(x)) // absolute value of cube#define _cr(x) (unsigned char)(pow((x),1.0/3.0)) // cube root#if 0unsigned char RD( int i, int j ){return (char)( _sq( cos( atan2( j - 512, i - 512 ) / 2 ) ) * 255 );;// YOUR CODE HERE}unsigned char GR( int i, int j ){return (char)( _sq( cos( atan2( j - 512, i - 512 ) / 2 - 2 * acos( -1 ) / 3 ) ) * 255 );// YOUR CODE HERE}unsigned char BL( int i, int j ){return (char)( _sq( cos( atan2( j - 512, i - 512 ) / 2 + 2 * acos( -1 ) / 3 ) ) * 255 );// YOUR CODE HERE}#endif#if 0unsigned char RD( int i, int j ){#define r(n)(rand()%n)static char c[ 1024 ][ 1024 ]; return!c[ i ][ j ] ? c[ i ][ j ] = !r( 999 ) ? r( 256 ) : RD( ( i + r( 2 ) ) % 1024, ( j + r( 2 ) ) % 1024 ) : c[ i ][ j ];}unsigned char GR( int i, int j ){static char c[ 1024 ][ 1024 ]; return!c[ i ][ j ] ? c[ i ][ j ] = !r( 999 ) ? r( 256 ) : GR( ( i + r( 2 ) ) % 1024, ( j + r( 2 ) ) % 1024 ) : c[ i ][ j ];}unsigned char BL( int i, int j ){static char c[ 1024 ][ 1024 ]; return!c[ i ][ j ] ? c[ i ][ j ] = !r( 999 ) ? r( 256 ) : BL( ( i + r( 2 ) ) % 1024, ( j + r( 2 ) ) % 1024 ) : c[ i ][ j ];}#endif // Martin Büttner#if 0unsigned char RD( int i, int j ){float x = 0, y = 0; int k; for ( k = 0; k++<256; ) { float a = x*x - y*y + ( i - 768.0 ) / 512; y = 2 * x*y + ( j - 512.0 ) / 512; x = a; if ( x*x + y*y>4 )break; }return log( k ) * 47;}unsigned char GR( int i, int j ){float x = 0, y = 0; int k; for ( k = 0; k++<256; ) { float a = x*x - y*y + ( i - 768.0 ) / 512; y = 2 * x*y + ( j - 512.0 ) / 512; x = a; if ( x*x + y*y>4 )break; }return log( k ) * 47;}unsigned char BL( int i, int j ){float x = 0, y = 0; int k; for ( k = 0; k++<256; ) { float a = x*x - y*y + ( i - 768.0 ) / 512; y = 2 * x*y + ( j - 512.0 ) / 512; x = a; if ( x*x + y*y>4 )break; }return 128 - log( k ) * 23;}#endif#if 0unsigned char RD( int i, int j ){double a = 0, b = 0, c, d, n = 0;while ( ( c = a*a ) + ( d = b*b ) < 4 && n++ < 880 ){ b = 2 * a*b + j*8e-9 - .645411; a = c - d + i*8e-9 + .356888; }return 255 * pow( ( n - 80 ) / 800, 3. );}unsigned char GR( int i, int j ){double a = 0, b = 0, c, d, n = 0;while ( ( c = a*a ) + ( d = b*b ) < 4 && n++ < 880 ){ b = 2 * a*b + j*8e-9 - .645411; a = c - d + i*8e-9 + .356888; }return 255 * pow( ( n - 80 ) / 800, .7 );}unsigned char BL( int i, int j ){double a = 0, b = 0, c, d, n = 0;while ( ( c = a*a ) + ( d = b*b ) < 4 && n++ < 880 ){ b = 2 * a*b + j*8e-9 - .645411; a = c - d + i*8e-9 + .356888; }return 255 * pow( ( n - 80 ) / 800, .5 );}#endif#if 0unsigned char RD( int i, int j ){static double k; k += rand() / 1. / RAND_MAX; int l = k; l %= 512; return l > 255 ? 511 - l : l;}unsigned char GR( int i, int j ){static double k; k += rand() / 1. / RAND_MAX; int l = k; l %= 512; return l > 255 ? 511 - l : l;}unsigned char BL( int i, int j ){static double k; k += rand() / 1. / RAND_MAX; int l = k; l %= 512; return l > 255 ? 511 - l : l;}#endif#if /*githubphagocyte */0unsigned char RD( int i, int j ){float s = 3. / ( j + 99 );float y = ( j + sin( ( i*i + _sq( j - 700 ) * 5 ) / 100. / DIM ) * 35 )*s;return ( int( ( i + DIM )*s + y ) % 2 + int( ( DIM * 2 - i )*s + y ) % 2 ) * 127;}unsigned char GR( int i, int j ){float s = 3. / ( j + 99 );float y = ( j + sin( ( i*i + _sq( j - 700 ) * 5 ) / 100. / DIM ) * 35 )*s;return ( int( 5 * ( ( i + DIM )*s + y ) ) % 2 + int( 5 * ( ( DIM * 2 - i )*s + y ) ) % 2 ) * 127;}unsigned char BL( int i, int j ){float s = 3. / ( j + 99 );float y = ( j + sin( ( i*i + _sq( j - 700 ) * 5 ) / 100. / DIM ) * 35 )*s;return ( int( 29 * ( ( i + DIM )*s + y ) ) % 2 + int( 29 * ( ( DIM * 2 - i )*s + y ) ) % 2 ) * 127;}#endif#if 0unsigned char RD( int i, int j ){return i&&j ? ( i%j )&( j%i ) : 0;return (unsigned short)sqrt( (double)( _sq( i - DIM / 2 )*_sq( j - DIM / 2 ) )*2.0 );}unsigned char GR( int i, int j ){return i&&j ? ( i%j ) + ( j%i ) : 0;return (unsigned short)sqrt( (double)(( _sq( i - DIM / 2 ) | _sq( j - DIM / 2 ) )*( _sq( i - DIM / 2 )&_sq( j - DIM / 2 ) )) );}unsigned char BL( int i, int j ){return i&&j ? ( i%j ) | ( j%i ) : 0;return (unsigned short)sqrt( (double)( _sq( i - DIM / 2 )&_sq( j - DIM / 2 ) )*2.0 );}#endif#define  X 1unsigned short RD( int i, int j ){double a = sqrt( _sq( 148. - i ) + _sq( 1000 - j ) ) + 1;double b =  sqrt( abs( sin( ( sqrt( _sq( 500. - i ) + _sq( 400 - j ) ) ) / 115.0 ) ) ) + 1;return a / b/1;}unsigned short GR( int i, int j ){return( sqrt( _sq( 610. - i ) + _sq( 60 - j ) ) + 1 ) / ( sqrt( abs( sin( ( sqrt( _sq( 864. - i ) + _sq( 860 - j ) ) ) / 115.0 ) ) ) + 1 );}unsigned short BL( int i, int j ){return( sqrt( _sq( 180. - i ) + _sq( 100 - j ) ) + 1 ) / ( sqrt( abs( sin( ( sqrt( _sq( 503. - i ) + _sq( 103 - j ) ) ) / 115.0 ) ) ) + 1 )/X/27;}int main( int, char** ) {std::locale::global( std::locale( "" ) );BITMAPFILEHEADER bmpFileHeader;BITMAPINFOHEADER bmpInfoHeader;bmpInfoHeader.biSize = sizeof( bmpInfoHeader );bmpInfoHeader.biBitCount = 24;bmpInfoHeader.biClrImportant = 0;bmpInfoHeader.biCompression = BI_RGB;bmpInfoHeader.biClrUsed = 0;bmpInfoHeader.biWidth = BMP_WIDTH;bmpInfoHeader.biHeight = BMP_HEIGHT;bmpInfoHeader.biXPelsPerMeter = 0;bmpInfoHeader.biYPelsPerMeter = 0;bmpInfoHeader.biPlanes = 1;bmpInfoHeader.biSizeImage = BMP_WIDTH * bmpInfoHeader.biBitCount / 32 * 4 * BMP_HEIGHT;bmpFileHeader.bfType = 0x4d42;bmpFileHeader.bfSize = 54 + bmpInfoHeader.biSizeImage;bmpFileHeader.bfReserved1 = 0;bmpFileHeader.bfReserved2 = 0;bmpFileHeader.bfOffBits = 54;ofstream ofs( "1024_1024.bmp", ios_base::binary | ios_base::out );ofs.write((char*)&bmpFileHeader.bfType,2); // 文件标识ofs.write( (char*)&bmpFileHeader.bfSize, 4);ofs.write( (char*)&bmpFileHeader.bfReserved1, 2 );ofs.write( (char*)&bmpFileHeader.bfReserved2, 2 );ofs.write( (char*)&bmpFileHeader.bfOffBits, 4 );ofs.write( (char*)&bmpInfoHeader.biSize, 4 );ofs.write( (char*)&bmpInfoHeader.biWidth, 4 );ofs.write( (char*)&bmpInfoHeader.biHeight, 4 );ofs.write( (char*)&bmpInfoHeader.biPlanes, 2 );ofs.write( (char*)&bmpInfoHeader.biBitCount, 2 );ofs.write( (char*)&bmpInfoHeader.biCompression, 4 );ofs.write( (char*)&bmpInfoHeader.biSizeImage, 4 );ofs.write( (char*)&bmpInfoHeader.biXPelsPerMeter, 4 );ofs.write( (char*)&bmpInfoHeader.biYPelsPerMeter, 4 );ofs.write( (char*)&bmpInfoHeader.biClrImportant, 4 );ofs.write( (char*)&bmpInfoHeader.biClrUsed, 4 );int skip = 4 - (bmpInfoHeader.biWidth * bmpInfoHeader.biBitCount >> 3) & 3;char*pSkip = nullptr;if ( skip > 0 ){pSkip = new char[ skip ];memset( pSkip, 0, skip );}for ( int j = bmpInfoHeader.biHeight-1; j >=0; j-- ){for ( int i = 0; i < bmpInfoHeader.biWidth; i++ ){static unsigned char color[ 3 ];color[ 2 ] = (unsigned char)RD( i, j ) & 255;color[ 1 ] = (unsigned char)GR( i, j ) & 255;color[ 0 ] = (unsigned char)BL( i, j ) & 255;ofs.write( (char*)color, 3 );}if (skip>0){ofs.write( pSkip, skip );}}ofs.close();setlocale( LC_ALL, "C" );return 0;}

给一个好玩的网址,里面给出好多好玩的代码,生成好多美妙的图形。上面的代码已经复制了一些。

参考资料:

http://blog.chinaunix.net/uid-20737871-id-1881195.html

http://codegolf.stackexchange.com/questions/35569/tweetable-mathematical-art

0 0