📄 imgp.cpp
字号:
// ImgP.cpp : implementation file
//
#include "stdafx.h"
#include "ImgP.h"
#include <math.h>
#include "Testlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImgP
CImgP::CImgP()
{
flag=0;
Scale=1.0f;
fData = NULL;
orderData = NULL;
}
CImgP::~CImgP()
{
if((flag==1)&&(bData))
delete []bData;
flag=0;
if( fData )
{
delete []fData;
fData = NULL;
}
if( orderData )
{
delete []orderData;
orderData = NULL;
}
}
BEGIN_MESSAGE_MAP(CImgP, CWnd)
//{{AFX_MSG_MAP(CImgP)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImgP message handlers
BOOL CImgP::Load(LPCTSTR filename)
{
CString str(filename,256);
if(str.Find(".bmp")!=-1)
{
CFile file;
CFileException e;
if(!file.Open(filename,CFile::modeRead,&e))
{
#ifdef _DEBUG
AfxMessageBox("Can't open img file!");
#endif
return FALSE;
}
file.Seek(0L,CFile::begin);
unsigned long int FileLenth;
BYTE *FileData;
FileLenth=file.GetLength();
lenth=FileLenth;
FileData=new BYTE[lenth];
bData=FileData;
if(!FileData)
{
AfxMessageBox("Fail to allocate memory!");
return FALSE;
}
if(!file.ReadHuge(FileData,FileLenth))
{
AfxMessageBox("Fail to read data!");
return FALSE;
}
flag=1;
bFileHeader=(LPBITMAPFILEHEADER)FileData;
bInfoHeader=(LPBITMAPINFOHEADER)(FileData+sizeof(BITMAPFILEHEADER));
lenth = bInfoHeader->biSizeImage;
bBMI=(LPBITMAPINFO)bInfoHeader;
bitData=bData+54;
if(((unsigned char)bFileHeader->bfType!='B')||\
((unsigned char)(bFileHeader->bfType>>8)!='M') )
{
AfxMessageBox("It's not a legal bmp file!");
return FALSE;
}
file.Close();
BytesPerLine=bInfoHeader->biWidth*(bInfoHeader->biBitCount/8);
if(BytesPerLine%4!=0)BytesPerLine=(BytesPerLine/4+1)*4;
// fbitnum = bInfoHeader->biBitCount;
////////////////////////////////////////////
if(bInfoHeader->biBitCount==24)
{
int i, j;//, k;
for(j=0;j<bInfoHeader->biHeight;j++)
{
for(i=0;i<bInfoHeader->biWidth;i++)
{
COLORREF color = GetPixel(i,j);
BYTE r=GetRValue(color);
BYTE g=GetGValue(color);
BYTE b=GetBValue(color);
}
}
return TRUE;
}
else if(bInfoHeader->biBitCount==8)
{
BYTE Palette[256][3];
BYTE *ImageData=bData+bFileHeader->bfOffBits;
int i,j;
int width=bInfoHeader->biWidth;
int height=bInfoHeader->biHeight;
int OldBytes=BytesPerLine;
/// save the raw color info to a file:
for(i=0;i<256;i++)
{
for(j=0;j<3;j++)
{
Palette[i][j]=*(bitData+i*4+j);
}
}
flag=0;
CreateDirect(width,height);
for(j=0;j<height;j++)
{
for(i=0;i<width;i++)
{
BYTE Index=*(ImageData+j*OldBytes+i);
BYTE b=Palette[Index][0];
BYTE g=Palette[Index][1];
BYTE r=Palette[Index][2];
SetPixel(i,height-j-1,RGB(r,g,b));
}
}
flag=1;
// delete ImageData;
delete FileData;
return TRUE;
}
else
{
AfxMessageBox("Illegal color bit number!");
return FALSE;
}
}
else
{
AfxMessageBox("Unsupported formats!");
return FALSE;
}
return TRUE;
}
void CImgP::operator=(CImgP &bm)
{
if(flag==1)delete []bData;
lenth=bm.lenth;
bData=new BYTE[lenth+54L];
if(!bData)
{
AfxMessageBox("Fail to allocate memory!");
return;
}
CopyMemory(bData,bm.bData,lenth+54L);
flag=1;
bFileHeader=(LPBITMAPFILEHEADER)bData;
bInfoHeader=(LPBITMAPINFOHEADER)(bData+sizeof(BITMAPFILEHEADER));
bBMI=(LPBITMAPINFO)bInfoHeader;
bitData=bData+54;
BytesPerLine=bm.BytesPerLine;
Scale=bm.GetScale();
//////////// new added ://///////////////////////
// fbitnum = bm.fbitnum;
if( bm.fData )
{
if( fData )
delete []fData;
fData = new BYTE[(bInfoHeader->biHeight)*(bInfoHeader->biWidth)];
CopyMemory( fData, bm.fData, (bInfoHeader->biHeight)*(bInfoHeader->biWidth) );
}
/////////////////////////////////////////////////////
}
int CImgP::GetWidth()
{
return bInfoHeader->biWidth;
}
int CImgP::GetHeight()
{
return bInfoHeader->biHeight;
}
float CImgP::GetScale()
{
return Scale;
}
int CImgP::GetDispWidth()
{
return int(Scale*bInfoHeader->biWidth);
}
int CImgP::GetDispHeight()
{
return int(Scale*bInfoHeader->biHeight);
}
void CImgP::Release()
{
if(flag==1)delete []bData;
flag=0;
if( fData )
{
delete []fData;
fData = NULL;
}
}
void CImgP::Display(int x,int y,CDC *dc)
{
StretchDIBits(dc->GetSafeHdc(),\
x,y,int(Scale*bInfoHeader->biWidth),int(Scale*bInfoHeader->biHeight),\
0,0,bInfoHeader->biWidth,bInfoHeader->biHeight,\
bitData,bBMI,DIB_RGB_COLORS,SRCCOPY);
}
void CImgP::Display(int x,int y,int cx,int cy,CDC *dc)
{
StretchDIBits(dc->GetSafeHdc(),\
x,y,cx,cy,\
0,0,bInfoHeader->biWidth,bInfoHeader->biHeight,\
bitData,bBMI,DIB_RGB_COLORS,SRCCOPY);
}
COLORREF CImgP::GetPixel(int x,int y)
{
if(flag==0)
{
AfxMessageBox("Load image file first please!");
return RGB(0,0,0);
}
if((x<0)||(x>=bInfoHeader->biWidth)||\
(y<0)||(y>=bInfoHeader->biHeight) )
{
AfxMessageBox("Outside the image boundary!");
return RGB(0,0,0);
}
y=bInfoHeader->biHeight-y-1;
unsigned long temp=(long)y*(long)BytesPerLine+(long)x*3;
if(temp>=lenth)
{
AfxMessageBox("The data is over the limitation!");
return RGB(0,0,0);
}
unsigned r,g,b;
b=*(bitData+temp);
g=*(bitData+temp+1);
r=*(bitData+temp+2);
return RGB(r,g,b);
}
COLORREF CImgP::GetPixel(int x,int y, unsigned &r, unsigned &g, unsigned &b)
{
if(flag==0)
{
AfxMessageBox("File read error!");
return RGB(0,0,0);
}
if((x<0)||(x>=bInfoHeader->biWidth)||\
(y<0)||(y>=bInfoHeader->biHeight) )
{
AfxMessageBox("Exceed image area!");
return RGB(0,0,0);
}
y=bInfoHeader->biHeight-y-1;
unsigned long temp=(long)y*(long)BytesPerLine+(long)x*3;
if(temp>=lenth)
{
AfxMessageBox("Data exceed!");
return RGB(0,0,0);
}
// unsigned r,g,b;
b=*(bitData+temp);
g=*(bitData+temp+1);
r=*(bitData+temp+2);
return RGB(r,g,b);
}
void CImgP::SetPixel(int x,int y,COLORREF color)
{
if(flag==0)
{
AfxMessageBox("Load image file first please!");
return;
}
if((x<0)||(x>=bInfoHeader->biWidth)||\
(y<0)||(y>=bInfoHeader->biHeight) )
{
AfxMessageBox("Outside the image boundary!");
return;
}
y=bInfoHeader->biHeight-y-1;
unsigned long temp=(long)y*(long)BytesPerLine+(long)x*3;
if(temp>=lenth)
{
AfxMessageBox("The data is over the limitation!");
return;
}
*(bitData+temp)=GetBValue(color);
*(bitData+temp+1)=GetGValue(color);
*(bitData+temp+2)=GetRValue(color);
}
BOOL CImgP::CreateDirect(int ImageWidth,int ImageHeight)
{
Release();
flag=0;
Scale=1.0f;
BytesPerLine=ImageWidth*3;
if(BytesPerLine%4!=0)BytesPerLine=(BytesPerLine/4+1)*4;
lenth=long(ImageHeight)*BytesPerLine;
bData=new BYTE[lenth+54L];
if(!bData)
{
AfxMessageBox("Error allocating memory!");
return FALSE;
}
flag=1;
bFileHeader=(LPBITMAPFILEHEADER)bData;
bInfoHeader=(LPBITMAPINFOHEADER)(bData+sizeof(BITMAPFILEHEADER));
bBMI=(LPBITMAPINFO)bInfoHeader;
bInfoHeader->biWidth=ImageWidth;
bInfoHeader->biHeight=ImageHeight;
bInfoHeader->biSize=40;
WORD bmptemp=(WORD)'M';
bmptemp<<=8;
bmptemp|=(BYTE)'B';
bFileHeader->bfType=bmptemp;
bFileHeader->bfOffBits=54;
bFileHeader->bfReserved1=0;
bFileHeader->bfReserved2=0;
bInfoHeader->biPlanes=1;
bInfoHeader->biBitCount=24;
bInfoHeader->biCompression=0;
bInfoHeader->biXPelsPerMeter=0;
bInfoHeader->biYPelsPerMeter=0;
bInfoHeader->biClrUsed=0;
bInfoHeader->biClrImportant=0;
bitData=bData+54;
bInfoHeader->biSizeImage=lenth;
bFileHeader->bfSize=lenth+54;
return TRUE;
}
BOOL CImgP::Trans2YIQ(CImgP *yimg, CImgP *iimg, CImgP *qimg)
{
int i,j;
unsigned R, G, B;
BYTE *yvalue, *ivalue, *qvalue;
int ImageWidth,ImageHeight;
ImageWidth = bInfoHeader->biWidth;
ImageHeight = bInfoHeader->biHeight;
yvalue = new BYTE[ImageWidth*ImageHeight];
qvalue = new BYTE[ImageWidth*ImageHeight];
ivalue = new BYTE[ImageWidth*ImageHeight];
for( i=0; i<ImageHeight; i++ )
{
for( j=0; j<ImageWidth; j++ )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -