Click here to Skip to main content
16,006,065 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalstring question Pin
Anonymous10-Oct-02 8:43
Anonymous10-Oct-02 8:43 
GeneralRe: string question Pin
Paul M Watt10-Oct-02 9:02
mentorPaul M Watt10-Oct-02 9:02 
GeneralRe: string question Pin
Anonymous10-Oct-02 9:19
Anonymous10-Oct-02 9:19 
GeneralRe: string question Pin
Shog910-Oct-02 9:08
sitebuilderShog910-Oct-02 9:08 
GeneralRe: string question Pin
valikac10-Oct-02 13:12
valikac10-Oct-02 13:12 
QuestionSetup works fine as admin, but no one else? Pin
paulccc10-Oct-02 8:24
paulccc10-Oct-02 8:24 
AnswerRe: Setup works fine as admin, but no one else? Pin
Anonymous10-Oct-02 8:59
Anonymous10-Oct-02 8:59 
GeneralBitmap Problems Pin
Mike Savoie10-Oct-02 8:26
Mike Savoie10-Oct-02 8:26 
Hi, I'm sorry but this might be a long description. It's about the only way I can explain what is happening.

I've been trying to crop a piece from one bmp file to put into another with an MFC app. The thing is I'm really new to programming, and don't know how to do very much. I've tried a few examples on here, which I didn't get to work because they all seem to use the sceen as a display. I need this to be done behind the scene. I've been trying to learn about DC's but again all the examples I've run into are for the monitor. haha so you see my dilema.

Anyway, what I've come up with is reading in the bmp using a CFile and malloc some memory for it. Then I read out the BITMAPFILEHEADER, and BITMAPINFO. Which I write to the new file. Then from my understanding, the data that follows should be the actual pixels for the image. So to test this I reverse the bits and then write the reversed bits into the new bmp. In theory this should result in an upside down image. It does but, the colors are totally distorted. This is what is confusing me. Am I missing the color palette? Or am I not reading the RGBQUAD correctly? Any help with this would really be appreciated.

Here's my code, maybe you can see where my problem is. Thanks.
<br />
<br />
	CFile dibFile(_T("BITMAP1.bmp"), CFile::modeRead);<br />
	<br />
	<br />
	//open file<br />
<br />
	//create memory for the file header<br />
<br />
	BITMAPFILEHEADER bmpFileHead;<br />
	<br />
	//read the header into memory<br />
<br />
	if(!dibFile.Read((void*)&bmpFileHead, sizeof(BITMAPFILEHEADER)))<br />
		AfxMessageBox("Read failed.");<br />
<br />
	<br />
	//check to make sure the file is a valid bitmap <br />
<br />
	if(bmpFileHead.bfType == ((WORD) ('M' << 8) | 'B'))<br />
	{<br />
		//need the length of the file for further processing<br />
		<br />
		DWORD fileLength = dibFile.GetLength();<br />
				<br />
		//take the size of the header away from the length, we already have the header in memory<br />
		<br />
		DWORD size = fileLength - sizeof(BITMAPFILEHEADER);<br />
				<br />
		//create a pointer to the bitmap data as a chunk of memory<br />
		<br />
		BYTE* pDib = (BYTE*)malloc(size);<br />
		if(!pDib)<br />
			AfxMessageBox("Allocation failed");<br />
			<br />
		//Create second pointer so we have somewhere to reverse the data<br />
		BYTE* pTest = (BYTE*)malloc(size);<br />
		if(!pTest)<br />
			AfxMessageBox("Allocation failed");<br />
<br />
		//read the data into the array<br />
		<br />
		dibFile.Read((void*)pDib, size);<br />
		dibFile.Close();<br />
<br />
		//The following structs contain display information about the bitmap,<br />
		//so we read into them for use later<br />
<br />
		//fill the BITMAPINFO struct from the file data<br />
	<br />
		BITMAPINFO* pBmpInfo = (BITMAPINFO*)pDib;<br />
	<br />
		<br />
		//fill the BITMAPINFOHEADER struct from the file<br />
	<br />
		BITMAPINFOHEADER* pBmpInfoHead = (BITMAPINFOHEADER*) pDib;<br />
	<br />
		<br />
		//fill the RGBQUAD struct from the file<br />
	<br />
		RGBQUAD* pRGB = (RGBQUAD*)(pDib + pBmpInfoHead->biSize);<br />
<br />
<br />
		//determine the number of colors used in the bitmap<br />
		/*<br />
		int numColors;<br />
		if((pBmpInfoHead->biClrUsed == 0) && (pBmpInfoHead->biBitCount < 9))<br />
		{<br />
			switch(pBmpInfoHead->biBitCount)<br />
			{<br />
			case 1: numColors = 2; break;<br />
			case 4: numColors = 16; break;<br />
			case 8: numColors = 256;<br />
			}<br />
		}<br />
		else<br />
			numColors = (int)pBmpInfoHead->biClrUsed;<br />
<br />
		if(pBmpInfoHead->biClrUsed == 0)<br />
			pBmpInfoHead->biClrUsed = numColors;<br />
<br />
		<br />
		//now that we have the number of colors we can determine where the actual image <br />
		//data starts, and read it into an array so we can work with it<br />
<br />
		DWORD clrTable = numColors * sizeof(RGBQUAD);<br />
		BYTE* pData = pDib + pBmpInfoHead->biSize + clrTable;<br />
<br />
		if(pBmpInfoHead->biSizeImage == 0)<br />
		{<br />
			DWORD height = pBmpInfoHead->biHeight;<br />
			DWORD width = pBmpInfoHead->biWidth;<br />
<br />
			pBmpInfoHead->biSize = height * width;<br />
		}<br />
		*/<br />
		//reverse the data to flip the image.<br />
		//using 44 as first subscript to read, to offset the BITMAPINFO <br />
		int s = 0;<br />
		for(unsigned g = 44;g < size;g++)<br />
		{<br />
			pTest[s] = pDib[g];<br />
			s++;<br />
		}<br />
		//create and write to the new file<br />
		CFile wFile(_T("test.bmp"), CFile::modeWrite | CFile::modeCreate | CFile::typeBinary);<br />
<br />
		wFile.Write((void*)&bmpFileHead, sizeof(BITMAPFILEHEADER));<br />
		<br />
		wFile.Write((void*)pDib, sizeof(BITMAPINFO));<br />
		<br />
		wFile.Write((void*)pTest, size);<br />
<br />
		wFile.Close();<br />
		<br />
		//clean up<br />
<br />
		free(pTest);<br />
		free(pDib);<br />
	}<br />

GeneralRe: Bitmap Problems Pin
Joe Woodbury10-Oct-02 8:58
professionalJoe Woodbury10-Oct-02 8:58 
GeneralRe: Bitmap Problems Pin
Paul M Watt10-Oct-02 9:22
mentorPaul M Watt10-Oct-02 9:22 
GeneralRe: Bitmap Problems Pin
Mike Savoie11-Oct-02 1:50
Mike Savoie11-Oct-02 1:50 
GeneralRe: Bitmap Problems Pin
Atlantys21-Oct-02 7:56
Atlantys21-Oct-02 7:56 
GeneralI'm Confused... Pin
JohnnyG10-Oct-02 7:48
JohnnyG10-Oct-02 7:48 
GeneralRe: I'm Confused... Pin
Carlos Antollini10-Oct-02 8:02
Carlos Antollini10-Oct-02 8:02 
GeneralRe: I'm Confused... Pin
JohnnyG10-Oct-02 8:32
JohnnyG10-Oct-02 8:32 
GeneralRe: I'm Confused... Pin
Stephane Rodriguez.10-Oct-02 8:06
Stephane Rodriguez.10-Oct-02 8:06 
GeneralRe: I'm Confused... Pin
JohnnyG10-Oct-02 9:15
JohnnyG10-Oct-02 9:15 
GeneralRe: I'm Confused... Pin
Ranjan Banerji10-Oct-02 8:10
Ranjan Banerji10-Oct-02 8:10 
GeneralRe: I'm Confused... Pin
JohnnyG10-Oct-02 8:35
JohnnyG10-Oct-02 8:35 
GeneralRe: I'm Confused... Pin
Stephane Rodriguez.10-Oct-02 9:07
Stephane Rodriguez.10-Oct-02 9:07 
GeneralRe: I'm Confused... Pin
JohnnyG10-Oct-02 9:52
JohnnyG10-Oct-02 9:52 
GeneralRe: I'm Confused... Pin
Gary R. Wheeler10-Oct-02 14:56
Gary R. Wheeler10-Oct-02 14:56 
Generalsplash screen Pin
ns10-Oct-02 7:41
ns10-Oct-02 7:41 
GeneralRe: splash screen Pin
jmkhael10-Oct-02 7:48
jmkhael10-Oct-02 7:48 
GeneralRe: splash screen Pin
ns10-Oct-02 8:05
ns10-Oct-02 8:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.