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

📄 dib.cpp

📁 本原码为为《嵌入式Linux应用开发详解》一书的配套代码
💻 CPP
字号:
// Dib.cpp : implementation file
//

#include "stdafx.h"
#include "Dib.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define pi 3.14159265

CDib::CDib()
{
	m_lWidth = 0;
	m_lHeight = 0;
	m_wBitCount = 0;
	m_nClrUsed = 0;
	m_dwSizeImage = 0;
	m_bIsLoadFile = false;

	m_lpBMPInfo = NULL;
	m_lpLogPal = NULL;
	m_lpImage = NULL;
	m_hdd = 0;
	m_hPal = 0;

}

CDib::~CDib()
{
	Empty();
}

CDib::CDib(LPCTSTR lpszPathName)
{
   	m_lWidth = 0;
	m_lHeight = 0;
	m_wBitCount = 0;
	m_nClrUsed = 0;
	m_dwSizeImage = 0;
	m_bIsLoadFile = false;

	m_lpBMPInfo = NULL;
	m_lpLogPal = NULL;
	m_lpImage = NULL;
	m_hdd = 0;
	m_hPal = 0;

	LoadBMP(lpszPathName);
}

BOOL CDib::LoadBMP(LPCTSTR lpszPathName)
{
   Empty();

   //open a file
   CFile file;
   if(!file.Open(lpszPathName,CFile::modeRead | 
	                                  CFile::shareDenyNone))
   {
	   AfxMessageBox("cann't open the file");
	   return false;
   }

   //read the header of the file
   LPBITMAPFILEHEADER lpBMPFileHeader = (LPBITMAPFILEHEADER)
	           new BYTE[sizeof(BITMAPFILEHEADER)];
   if(!lpBMPFileHeader)
   {
	   AfxMessageBox("not enough memory");
	   file.Close();
	   return false;
   }
   file.Read(lpBMPFileHeader,sizeof(BITMAPFILEHEADER));
   if(lpBMPFileHeader->bfType != 0x4d42)
   {
	   AfxMessageBox("It is not a BMP's file");
	   delete[] lpBMPFileHeader;
	   lpBMPFileHeader = NULL;
	   file.Close();
	   return false;
   }

   //read the structure of the BITMAPINFO 
   m_lpBMPInfo = (LPBITMAPINFO) new BYTE[lpBMPFileHeader->bfOffBits
	                          - sizeof(BITMAPFILEHEADER)];
   if(!m_lpBMPInfo)
   {
	   AfxMessageBox("not enough memory");
	   delete[] lpBMPFileHeader;
	   lpBMPFileHeader = NULL;
	   file.Close();
	   return false;
   }
   file.ReadHuge(m_lpBMPInfo,lpBMPFileHeader->bfOffBits
	                          - sizeof(BITMAPFILEHEADER) );
   m_lWidth = m_lpBMPInfo->bmiHeader.biWidth;
   m_lHeight = m_lpBMPInfo->bmiHeader.biHeight;
   m_wBitCount = m_lpBMPInfo->bmiHeader.biBitCount;
   m_nClrUsed = GetClrUsed();
   BytesPerLine=(m_lWidth * (m_wBitCount / 8) + 3)&~3;//m_lpBMPInfo->bmiHeader.biWidth*m_lpBMPInfo->bmiHeader.biPlanes
							//*m_lpBMPInfo->bmiHeader.biBitCount;
   
   if(m_nClrUsed<=16 && m_nClrUsed !=0)
   {
	   AfxMessageBox("The picture need more biBitCount");
	   delete[] lpBMPFileHeader;
	   lpBMPFileHeader = NULL;
	   delete[] m_lpBMPInfo;
	   m_lpBMPInfo = NULL;
	   file.Close();
	   return false;
   }
   //calculate the bytes of the picture
   m_dwSizeImage = ((m_lWidth * (m_wBitCount / 8) + 3)&~3)*m_lHeight;
   m_lpImage = new BYTE[m_dwSizeImage];
   if(!m_lpImage)
   {
	   AfxMessageBox("not enough memory");
	   delete[] lpBMPFileHeader;
	   lpBMPFileHeader = NULL;
	   delete[] m_lpBMPInfo;
	   m_lpBMPInfo = NULL;
	   file.Close();
	   return false;
   }
   file.ReadHuge(m_lpImage,m_dwSizeImage);

   //set the imformation of the palette;
   m_lpLogPal = (LPLOGPALETTE) new BYTE[sizeof(LPLOGPALETTE)
	                            +m_nClrUsed*sizeof(PALETTEENTRY)];
   m_lpLogPal->palVersion = 0x300;
   m_lpLogPal->palNumEntries = m_nClrUsed;
   for(int i = 0;i<m_nClrUsed;i++)
   {
	   m_lpLogPal->palPalEntry[i].peRed = m_lpBMPInfo->
		                                      bmiColors[i].rgbRed;
	   m_lpLogPal->palPalEntry[i].peBlue = m_lpBMPInfo->
		                                      bmiColors[i].rgbBlue;
	   m_lpLogPal->palPalEntry[i].peGreen = m_lpBMPInfo->
		                                      bmiColors[i].rgbGreen;
	   m_lpLogPal->palPalEntry[i].peFlags = 0;
   }
   m_hPal = ::CreatePalette(m_lpLogPal);
   delete[] lpBMPFileHeader;
   lpBMPFileHeader = NULL;
   file.Close();
   m_bIsLoadFile = true;
	
   return true;

}

void CDib::Empty()
{
	m_lWidth = 0;
	m_lHeight = 0;
	m_wBitCount = 0;
	m_nClrUsed = 0;
	m_dwSizeImage = 0;
	m_bIsLoadFile = false;

	if(m_lpBMPInfo)
		delete[] m_lpBMPInfo;
	m_lpBMPInfo = NULL;

	if(m_lpLogPal)
		delete[] m_lpLogPal;
    m_lpLogPal = NULL;

	if(m_lpImage)
		delete[] m_lpImage;
	m_lpImage = NULL;
//	if(m_lpImage1)
//		delete[] m_lpImage1;
//	m_lpImage1=NULL;

	if(m_hdd)
		::DrawDibClose(m_hdd);
	m_hdd = 0;

	if(m_hPal)
		::DeleteObject(m_hPal);
	m_hPal = 0;
}

DWORD CDib::GetClrUsed()
{
    if(m_lpBMPInfo->bmiHeader.biClrUsed == 0)
	{
		switch(m_wBitCount)
		{
		case 1:
			return 2;
		case 4:
			return 16;
		case 8:
			return 256;
		default:
			return 0;
		}
	}
	return m_lpBMPInfo->bmiHeader.biClrUsed;
}



BOOL CDib::Init(CDC *pDC)
{
	if(!m_bIsLoadFile)
		return false;
	m_hdd = ::DrawDibOpen();
	::DrawDibBegin(m_hdd,pDC->m_hDC,-1,-1,&m_lpBMPInfo->bmiHeader
		,m_lWidth,m_lHeight,DDF_ANIMATE);
	return true;

}

BOOL CDib::draw(CDC *pDC, CPoint originDes)
{
	CRect src;
	src.left = src.top = 0;
	src.right = m_lWidth;
	src.bottom = m_lHeight;
	::DrawDibDraw(m_hdd,pDC->m_hDC,originDes.x ,
				 originDes.y ,m_lWidth,-m_lHeight,&m_lpBMPInfo->bmiHeader,
				 (LPVOID)m_lpImage,src.left,src.top,m_lWidth,src.Height(),
				 DDF_SAME_HDC);
	return true;
}

/*
LONG CDib::GetPixOffset(LONG x, LONG y)
{
	return (m_lHeight-y-1)*BytesPerLine+x*m_wBitCount/8;
}



int CDib::ComputePixel(float x, float y, float &x1, float &y1)
{
	float r, nn;

  if (x==0 && y==0) 
  {
	 x1 = x;
	 y1 = y;
	 return 1;
  }

  nn = (float)sqrt(x*x + y*y);
  r = (float) (fabs(x) > fabs(y)) ? fabs(nn/x): fabs(nn/y);

  x1 = (r*x);
  y1 = (r*y);

  return 1;

}

void CDib::ComputerEllipseEffect(LPBYTE src,LPBYTE des)
{
   // CWaitCursor wait;
  int x, y, x1, y1,tmp1,tmp2;
  float fx, fy, xmid, ymid, ar;


  xmid = (float) (m_lWidth/2.0);
  ymid = (float) (m_lHeight/2.0);
  ar = (float)(m_lHeight)/(float)(m_lWidth);
  for (y=0; y<m_lHeight; y++)
  {
	 for (x=0; x<m_lWidth; x++)
	 {
		ComputePixel(ar*(x-xmid), y-ymid, fx, fy);
		x1 = (int)(xmid+fx/ar);
		y1 = (int)(ymid+fy);
	   if(x1>=0&&y1>=0&&x1<m_lWidth&&y1<m_lHeight)
		{
		tmp2=GetPixOffset(x1,y1);
		tmp1=GetPixOffset(x,y);
		des[tmp1] = src[tmp2];
		des[tmp1+1] = src[tmp2+1];
		des[tmp1+2] = src[tmp2+2];
		}
	 }
  }
  

}
*/

⌨️ 快捷键说明

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