Dib.cpp
资源名称:图像处理毕业设计.rar [点击查看]
上传用户:yuzhuan
上传日期:2013-03-16
资源大小:3596k
文件大小:4k
源码类别:
2D图形编程
开发平台:
Visual C++
- // Dib.cpp: implementation of the Dib class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "bs.h"
- #include "Dib.h"
- #include "windowsx.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- #define WIDTH(bytes) (((bytes*8+31)/32)*4)
- CDib::CDib()
- {
- size=0;
- m_pData=NULL;
- }
- CDib::~CDib()
- {
- GlobalFreePtr(m_pBitmapInfo);
- }
- void CDib::LoadFile(const CString dibFileName)
- {
- strcpy(m_fileName,dibFileName);
- CFile dibFile(m_fileName,CFile::modeRead);
- dibFile.Read((void*)&bitmapFileHeader,sizeof(BITMAPFILEHEADER));
- if(bitmapFileHeader.bfType==0x4d42)
- {
- DWORD fileLength=dibFile.GetLength();
- size=fileLength-sizeof(BITMAPFILEHEADER);
- pDib=(BYTE *)GlobalAllocPtr(GMEM_MOVEABLE,size);
- dibFile.Read((void*)pDib,size);
- dibFile.Close();
- m_pBitmapInfo=(BITMAPINFO *)pDib;
- m_pBitmapInfoHeader=(BITMAPINFOHEADER *)pDib;
- m_pRGB=(RGBQUAD *)(pDib+m_pBitmapInfoHeader->biSize);
- int m_numberOfColors=GetNumberOfColors();
- if(m_pBitmapInfoHeader->biClrUsed==0)
- m_pBitmapInfoHeader->biClrUsed=m_numberOfColors;
- DWORD colorTableSize=m_numberOfColors * sizeof(RGBQUAD);
- m_pData=pDib+m_pBitmapInfoHeader->biSize+colorTableSize;
- if(m_pRGB==(RGBQUAD *)m_pData)
- m_pRGB=NULL;
- m_pBitmapInfoHeader->biSizeImage=GetSize();
- m_valid=TRUE;
- }
- else
- {
- m_valid=FALSE;
- }
- }
- BOOL CDib::IsValid()
- {
- return m_valid;
- }
- char *CDib::GetFileName()
- {
- return m_fileName;
- }
- UINT CDib::GetWidth()//返回像素数
- {
- return (UINT) m_pBitmapInfoHeader->biWidth;
- }
- UINT CDib::GetHeight()//返回像素数
- {
- return (UINT) m_pBitmapInfoHeader->biHeight;
- }
- DWORD CDib::GetSize()//得到图像数据的大小,以字节为单位
- {
- if(m_pBitmapInfoHeader->biSizeImage!=0)
- return m_pBitmapInfoHeader->biSizeImage;
- else
- {
- DWORD height=(DWORD)GetHeight();
- DWORD width=(DWORD)GetWidth();
- return (height*WIDTH(width*m_pBitmapInfoHeader->biBitCount)/8);
- }
- }
- UINT CDib::GetNumberOfColors()//图像实际使用的颜色表中的颜色数
- {
- int numberOfColors;
- if((m_pBitmapInfoHeader->biClrUsed==0)&&m_pBitmapInfoHeader->biBitCount<9)
- {
- switch(m_pBitmapInfoHeader->biBitCount)
- {
- case 1:numberOfColors=2;break;
- case 4:numberOfColors=16;break;
- case 8:numberOfColors=256;
- }
- }
- else
- {
- numberOfColors=(int) m_pBitmapInfoHeader->biClrUsed;
- }
- return numberOfColors;
- }
- BYTE *CDib::GetData()//得到图像数据
- {
- return m_pData;
- }
- RGBQUAD *CDib::GetRGB()//调色板指针
- {
- return m_pRGB;
- }
- BITMAPINFO *CDib::GetInfo()//图像信息
- {
- return m_pBitmapInfo;
- }
- WORD CDib::PaletteSize(LPBYTE lpDIB)//调色板大小
- {
- return (DIBNumColors(lpDIB)*sizeof(RGBTRIPLE));
- }
- WORD CDib::DIBNumColors(LPBYTE lpDIB)//图像颜色数
- {
- WORD wBitCount=((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;
- switch(wBitCount)
- {
- case 1:
- return 2;
- case 4:
- return 16;
- case 8:
- return 256;
- default:
- return 0;
- }
- }
- void CDib::SaveFile(const CString filename)
- {//保存经处理后宽度,高度均没有改变大小的图像
- strcpy(m_fileName,filename);
- CFile dibFile(m_fileName,CFile::modeCreate|CFile::modeWrite);
- dibFile.Write((void*)&bitmapFileHeader,sizeof(BITMAPFILEHEADER));
- dibFile.Write((void*)pDib,size);
- dibFile.Close();
- }
- CPalette * CDib::CreateBmpPalette()
- {
- struct{
- WORD Version;
- WORD NumberOfEntries;
- PALETTEENTRY aEntries[256];
- }palette={0x300,256};
- LPRGBQUAD pRGBTable=GetRGB();
- UINT numberOfColors=GetNumberOfColors();
- for(UINT x=0;x<numberOfColors;++x)
- {
- palette.aEntries[x].peBlue=pRGBTable[x].rgbBlue;
- palette.aEntries[x].peGreen=pRGBTable[x].rgbGreen;
- palette.aEntries[x].peRed=pRGBTable[x].rgbRed;
- palette.aEntries[x].peFlags=0;
- }
- hPalette.CreatePalette((LPLOGPALETTE)&palette);
- return &hPalette;
- }