⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 this source create a red bmp.
💻 CPP
字号:
#include <iostream>
#include <fstream>

using namespace std; //keep this in or it breaks and dies horribly


struct BMFH { //declares BitMap File Header structure
unsigned short bfType; //might need to be a char
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
};

struct BMIH { //declares BitMap Info Header structure
unsigned long biSize;
unsigned long biWidth;
unsigned long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};

struct RGBQUAD{ //declares RGBQUAD
int rgbBlue;
int rgbGreen;
int rgbRed;
int rgbReserved;
};


struct pixel { //declares pixel structure

char R;
char G; //RGB values for each pixel
char B;
//unsigned short unused; //needed?
};


void setfileheader(BMFH& fileheader) //must pass by reference!
{ // Fills the file header with default values...

fileheader.bfType = 19778;
fileheader.bfSize = 108;
fileheader.bfReserved1 = 0;
fileheader.bfReserved2 = 0;
fileheader.bfOffBits = 1078;

}

void setinfoheader(BMIH& infoheader) //must pass by reference!
{ // Fills the file header with default values...

infoheader.biSize = sizeof(infoheader);
infoheader.biWidth = 128;
infoheader.biHeight = 128;
infoheader.biPlanes = 1;
infoheader.biBitCount = 24;
infoheader.biCompression = 0;
infoheader.biSizeImage = 0;
infoheader.biXPelsPerMeter = 0;
infoheader.biYPelsPerMeter = 0;
infoheader.biClrUsed = 0;
infoheader.biClrImportant = 0;

}



//char* ReadBMPFile ( const char* szFileName );

//BOOL WriteBMPFile( LPCTSTR szFileName, LPBYTE lpDIB ) ;


void main()
{

	//------------Setting up pixels and co-//

	pixel PICTAR[128][128]; //declares an array of pixels



	int i;

	int j;

	for (i=0; i<128; i++){ //horizontal axis of pixels

		for (j=0; j<128; j++){ //vertical axis

		PICTAR[i][j].R = 100; //make pixels red
		PICTAR[i][j].G = 0;
		PICTAR[i][j].B = 0;

		}
	}


	//-------------------//

	ifstream::pos_type size; //special type used for file positioning; can treat as an integer

	BMFH fileheader; //makes a header called "fileheader"

	BMIH infoheader;

	setfileheader(fileheader); //fills header with values

	setinfoheader(infoheader);

	ofstream myfile ("testbmp.bmp",ios::out | ios::binary); //creates an ofstream object called myfile

															//and opens it for binary input



	/////////////////////Writing////////////////////////////

	//myfile.write ((char*) &fileheader,sizeof(fileheader)); //write header

	//myfile.write ((char*) &infoheader,sizeof(infoheader)); //write header

	myfile.write ((char*) &fileheader,14); //write header

	myfile.write ((char*) &infoheader,40); //write header

	myfile.write ((char*) &PICTAR,sizeof(pixel)*128*128);

	myfile.close();


}














/**************************************************************************** 
* 
* FUNCTION: ReadBMPFile 
* 
* PURPOSE: Reads a BMP file into CF_DIB format 
* 
* PARAMS: LPCTSTR szFileName - the name of the file to read 
* 
* RETURNS: LPBYTE - pointer to the CF_DIB, NULL for failure 
* 
* History: 
* July '95 - Created 
* 
\***************************************************************************
char* ReadBMPFile( const char* szFileName ) 
{ 
HANDLE hFile; 
BITMAPFILEHEADER bfh; 
DWORD dwBytes; 
LPBYTE lpDIB = NULL, lpTemp = NULL; 
WORD wPaletteSize = 0; 
DWORD dwBitsSize = 0; 

// Open the file 
if( (hFile=CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ) 
{ 
MessageBox( NULL, "Error opening file", szFileName, MB_OK ); 
return NULL; 
} 
// Read the header 
if( ( ! ReadFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes, NULL ) ) || ( dwBytes != sizeof( BITMAPFILEHEADER ) ) ) 
{ 
CloseHandle( hFile ); 
MessageBox( NULL, "Error reading file", szFileName, MB_OK ); 
return NULL; 
} 
// Does it look like a BMP file? 
if( ( bfh.bfType != 0x4d42 ) || (bfh.bfReserved1!=0) || (bfh.bfReserved2!=0) ) 
{ 
CloseHandle( hFile ); 
MessageBox( NULL, "Not a recognised BMP format file", szFileName, MB_OK ); 
return NULL; 
} 
// Allocate some memory 
if( (lpDIB = malloc( sizeof( BITMAPINFO ) )) == NULL ) 
{ 
CloseHandle( hFile ); 
MessageBox( NULL, "Failed to allocate memory for DIB", szFileName, MB_OK ); 
return NULL; 
} 
// Read in the BITMAPINFOHEADER 
if( (!ReadFile( hFile, lpDIB, sizeof(BITMAPINFOHEADER), &dwBytes, NULL )) || (dwBytes!=sizeof(BITMAPINFOHEADER)) ) 
{ 
CloseHandle( hFile ); 
free( lpDIB ); 
MessageBox( NULL, "Error reading file", szFileName, MB_OK ); 
return NULL; 
} 
if( ((LPBITMAPINFOHEADER)lpDIB)->biSize != sizeof( BITMAPINFOHEADER ) ) 
{ 
CloseHandle( hFile ); 
free( lpDIB ); 
MessageBox( NULL, "OS/2 style BMPs Not Supported", szFileName, MB_OK ); 
return NULL; 
} 
// How big are the elements? 
wPaletteSize = PaletteSize((LPSTR)lpDIB); 
dwBitsSize = ((LPBITMAPINFOHEADER)lpDIB)->biHeight * BytesPerLine((LPBITMAPINFOHEADER)lpDIB); 
// realloc to account for the total size of the DIB 
if( (lpTemp = realloc( lpDIB, sizeof( BITMAPINFOHEADER ) + wPaletteSize + dwBitsSize )) == NULL ) 
{ 
CloseHandle( hFile ); 
MessageBox( NULL, "Failed to allocate memory for DIB", szFileName, MB_OK ); 
free( lpDIB ); 
return NULL; 
} 
lpDIB = lpTemp; 
// If there is a color table, read it 
if( wPaletteSize != 0 ) 
{ 
if( (!ReadFile( hFile, ((LPBITMAPINFO)lpDIB)->bmiColors, wPaletteSize, &dwBytes, NULL )) || (dwBytes!=wPaletteSize) ) 
{ 
CloseHandle( hFile ); 
free( lpDIB ); 
MessageBox( NULL, "Error reading file", szFileName, MB_OK ); 
return NULL; 
} 
} 
// Seek to the bits 
// checking against 0 in case some bogus app didn't set this element 
if( bfh.bfOffBits != 0 ) 
{ 
if( SetFilePointer( hFile, bfh.bfOffBits, NULL, FILE_BEGIN ) == 0xffffffff ) 
{ 
CloseHandle( hFile ); 
free( lpDIB ); 
MessageBox( NULL, "Error reading file", szFileName, MB_OK ); 
return NULL; 
} 
} 
// Read the image bits 
if( (!ReadFile( hFile, FindDIBBits(lpDIB), dwBitsSize, &dwBytes, NULL )) || (dwBytes!=dwBitsSize) ) 
{ 
CloseHandle( hFile ); 
free( lpDIB ); 
MessageBox( NULL, "Error reading file", szFileName, MB_OK ); 
return NULL; 
} 
// clean up 
CloseHandle( hFile ); 
return lpDIB; 
} 



  */
/* End ReadBMPFile() ********************************************************/ 


/**************************************************************************** 
* 
* FUNCTION: WriteBMPFile 
* 
* PURPOSE: Writes a BMP file from CF_DIB format 
* 
* PARAMS: LPCTSTR szFileName - the name of the file to read 
* LPBYTE - pointer to the CF_DIB, NULL for failure 
* 
* RETURNS: BOOL - TRUE for success, FALSE for Failure 
* 
* History: 
* July '95 - Created 
* 
\****************************************************************************/ 

/*
BOOL WriteBMPFile( LPCTSTR szFileName, LPBYTE lpDIB ) 
{ 
HANDLE hFile; 
BITMAPFILEHEADER bfh; 
DWORD dwBytes, dwBytesToWrite; 
LPBITMAPINFOHEADERlpbmih; 

// Open the file 
if( (hFile=CreateFile( szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ) 
{ 
MessageBox( NULL, "Error opening file", szFileName, MB_OK ); 
return FALSE; 
} 
bfh.bfType = 0x4d42; 
bfh.bfReserved1 = 0; 
bfh.bfReserved2 = 0; 
bfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + PaletteSize( lpDIB ); 
bfh.bfSize = (bfh.bfOffBits + ((LPBITMAPINFOHEADER)lpDIB)->biHeight * BytesPerLine((LPBITMAPINFOHEADER)lpDIB))/4; 
// Write the header 
if( ( ! WriteFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes, NULL ) ) || ( dwBytes != sizeof( BITMAPFILEHEADER ) ) ) 
{ 
CloseHandle( hFile ); 
MessageBox( NULL, "Error Writing file", szFileName, MB_OK ); 
return FALSE; 
} 
lpbmih = (LPBITMAPINFOHEADER)lpDIB; 
lpbmih->biHeight /= 2; 
dwBytesToWrite = bfh.bfOffBits + (lpbmih->biHeight * BytesPerLine(lpbmih)); 
if( ( ! WriteFile( hFile, lpDIB, dwBytesToWrite, &dwBytes, NULL ) ) || ( dwBytes != dwBytesToWrite ) ) 
{ 
CloseHandle( hFile ); 
MessageBox( NULL, "Error Writing file", szFileName, MB_OK ); 
return FALSE; 
} 
lpbmih->biHeight *= 2; 
CloseHandle( hFile ); 
return TRUE; 
} 
*/
/* End WriteBMPFile() *******************************************************/ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -