图像缩放函数
图像缩放,使用CImage实现。
[cpp]
// 用于缩放图像
bool CDIGTLSView::myScale(float keyX, float keyY) //定义时曾将keyx,keyy定义为int,导致出错
{
//程序编制:李立宗 lilizong@gmail.com
//2012-8-6
keyX=keyX;
keyY=keyY;
//srand((unsigned)time(NULL));
//float keyX,keyY; //放大倍数
//keyX=((rand()%20+1)/10.0);
//keyY=((rand()%20+1)/10.0);
//调试好久总是出问题,后来发现开始时key的值可能出现0,所以有问题,切记!!!
//2012-8-6
// CString str;
//str.Format(TEXT("%f"),key);
//MessageBox(str);
if(!myImage2.IsNull())
myImage2.Destroy();
if(!myImage1.IsNull())
myImage1.Destroy();
if(myImage1.IsNull())
//OnOpenResourceFile();
myImage1.LoadFromResource(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP3));
//OnOpenCustomeFile();
//myImage2.Destroy();
if(myImage2.IsNull())
myImage2.Create((int)(myImage1.GetWidth()*keyX),(int)(myImage1.GetHeight()*keyY),24,0);
COLORREF pixel;
int maxY = myImage2.GetHeight();
int maxX=myImage2.GetWidth();
//int maxX1=myImage1.GetWidth();
//int maxY1=myImage1.GetHeight();
//CString str;
//str.Format(TEXT("%d"),maxX);
//MessageBox(str);
//str.Format(TEXT("%d"),maxX1);
//MessageBox(str);
//str.Format(TEXT("%d"),maxY);
//MessageBox(str);
//str.Format(TEXT("%d"),maxY1);
//MessageBox(str);
int r,g,b,avg;
double c;
byte* pRealData;
byte* pRealData2;
pRealData=(byte*)myImage1.GetBits();
pRealData2=(byte*)myImage2.GetBits();
int pit=myImage1.GetPitch();
int pit2=myImage2.GetPitch();
//需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现
//CString str;
//str.Format(TEXT("%d"),pit2);
//MessageBox(str);
//str.Format(TEXT("%d"),pit2);
//MessageBox(str);
int bitCount=myImage1.GetBPP()/8;
int bitCount2=myImage2.GetBPP()/8;
//CString str;
//str.Format(TEXT("%d"),bitCount);
//MessageBox(str);
//str.Format(TEXT("%d"),bitCount2);
//MessageBox(str);
int newValue;
int tempR,tempG,tempB;
int newX,newY;
for (int y=0; y<maxY; y++) {
for (int x=0; x<maxX; x++) {
newX=(int)(x/keyX);
newY=(int)(y/keyY);
tempR=(int)(int)(*(pRealData+pit*newY+newX*bitCount));
if(bitCount==1)
{
tempG=tempR;
tempB=tempR;
}
else
{
tempG=(int)(int)(*(pRealData+pit*newY+newX*bitCount+1));
tempB=(int)(int)(*(pRealData+pit*newY+newX*bitCount+2));
}
*(pRealData2+pit2*y+x*bitCount2)=tempR;
*(pRealData2+pit2*y+x*bitCount2+1)=tempG;
*(pRealData2+pit2*y+x*bitCount2+2)=tempB;
补充:软件开发 , C++ ,