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

📄 bmp2rgb.cpp

📁 蒙特卡罗方法可以有效地解决复杂的工程问题
💻 CPP
字号:
#include "stdafx.h"
#include "dibapi.h"

#include <math.h>
#include <io.h>
#include <direct.h>
#include <errno.h>

#define DIB_HEADER_MARKER   ((WORD) ('M' << 8) | 'B')



int Bmp2Arry(CString BmpFileName,int &height,int &width,unsigned char* &p)
{
	
	CFile file;
	CFileException fe;
	
	if( !file.Open(BmpFileName, CFile::modeRead | CFile::shareDenyWrite, &fe) )
	{
		AfxMessageBox( " 不能打开纹理文件 "+BmpFileName );
		return 0 ;
	}
	HDIB m_hDIB = ::ReadDIBFile(file);
	
	LPSTR lpDIB = (LPSTR)::GlobalLock((HGLOBAL)(m_hDIB));
	
	LPBITMAPINFOHEADER lpbmi;  
	
	lpbmi = (LPBITMAPINFOHEADER)lpDIB;
	
	int ColorNumber = DIBNumColors(lpDIB);
	LPSTR lpDIB_data = FindDIBBits(lpDIB);
	RGBQUAD * RGBQ = ((LPBITMAPINFO)lpbmi)->bmiColors;
	
	height = (int)::DIBHeight(lpDIB);        
	width = (int)::DIBWidth(lpDIB);         
	
	p = (unsigned char *)malloc( height * width * sizeof(unsigned char) * 3 );

	int count = lpbmi->biBitCount;
	int BiteWidth = count*width;
	
	if( BiteWidth%32 == 0)
		BiteWidth = BiteWidth / 8;
	else
		BiteWidth = ( BiteWidth / 32 + 1 ) * 4;
	
	int i,j;
	LPSTR line_p,line_head;
	
	for( i = 0; i < height; i++ )
	{
		line_head = lpDIB_data + i * BiteWidth;

		for( j = 0; j < width; j++ )
		{
			line_p = line_head + ( j * count ) / 8;
			
			switch ( count )
			{
			case 1:
				{
					BYTE b = *line_p;
					int tt = ( j * count ) % 8;
					b = b >> ( 7 - tt );
					BYTE t=b&0x01;
					*( p + ( i * width + j ) * 3 ) = RGBQ[t].rgbRed;
					*( p + ( i * width + j ) * 3 + 1 ) = RGBQ[t].rgbGreen;
					*( p + ( i * width + j ) * 3 + 2 ) = RGBQ[t].rgbBlue;
					
					break;
				}

			case 4:
				{
					BYTE b = *line_p;
					int tt =( j * count ) % 8;
					b = b >> ( 4 - tt );
					int t = b & 0x0f;
					*( p + ( i * width + j ) * 3 ) = RGBQ[t].rgbRed;
					*( p + ( i * width + j ) * 3 + 1 ) = RGBQ[t].rgbGreen;
					*( p + ( i * width + j ) * 3 + 2) = RGBQ[t].rgbBlue;
					break;
				}
				
			case 8:
				{
					BYTE t = *line_p;
					*( p + ( i* width + j ) * 3 ) = RGBQ[t].rgbRed;
					*( p + ( i* width + j ) *3 + 1 ) = RGBQ[t].rgbGreen;
					*( p + ( i* width + j ) *3 + 2 ) = RGBQ[t].rgbBlue;
					break;
				}
				
			case 24:
				{
					BYTE *b = (BYTE *)line_p;
					*( p + ( i * width + j ) * 3 ) = *(b+2);
					*( p + ( i * width + j ) * 3 +1 )  = *(b+1);
					*( p + (  i * width + j ) * 3 +2 ) = *(b+0);
					break;
				}
				
			default:
				return  0;
			}
		}
	}
	
	::GlobalUnlock( (HGLOBAL) m_hDIB );
	::GlobalFree( (HGLOBAL) m_hDIB );
	return 1;
}

⌨️ 快捷键说明

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