void ImageScale(CImage *dst, CImage *src, double alpha) //非整数倍缩放
{
int nSize;
BYTE **list, *sc ,*lp;
int Dx, Dy, x, y, bpd;
Dx=(int)(alpha * src->GetWidth()); //计算结果位图宽度
Dy=(int)(alpha * src->GetHeight()); //计算结果位图高度
dst -> Destroy();
dst ->Create(Dx, Dy, src->GetBPP() ); //建立结果位图
bpd = src->GetBPP()/8; //每个画素占字节
nSize = (Dx * src->GetBPP() + 31) / 32 * 4; //计算工作单元大小
sc = (BYTE*) malloc(nSize); //申请像素行工作单元
list = (BYTE**) malloc(src->GetHeight() * sizeof(BYTE*)); // 申请指针数组
for (int i=0;i<src->GetHeight();i++)
list[i] = (BYTE*) src->GetPixelAddress(0, i); //生成二维数组
for (int j=0;j<Dy;j++)
{
y=int(j/alpha+0.5);
for (int i=0;i<Dx;i++)
{
x=int(i/alpha+0.5); //x1,y1为整数部分
if (x>src->GetWidth()||y>src->GetHeight()) // 范围检查
{
continue;
}
if(x==src->GetWidth()) x--;
if(y==src->GetHeight()) y--;
memcpy(&sc[i*bpd],&list[y][x*bpd],bpd); //从源位图复制像素数据
}
lp=(BYTE*)dst->GetPixelAddress(0,j); //处理结果总结果位图
memcpy(lp,sc,Dx*bpd);
}
free(sc); //释放工作单元
free(list);
}
void ImageRotation(CImage *dst, CImage *src, double alpha)
{
int ww, Dx, Dy, bpd;
double centerx, centery, sintheta, costheta;
double X1, Y1, X2, Y2, theta, xx, yy, rr;
BYTE **list, *sc, *lp;
int x, y;
Dx=src->GetWidth();
Dy=src->GetHeight();
sc=(BYTE*)malloc(2*(Dx*src->GetBPP()+31)/32*4); //申请工作单元
list=(BYTE**)malloc(Dy*sizeof(BYTE*)); //对原位图建立二维数组
for (int i=0;i<Dy;i++)
{
list[i]=(BYTE*)src->GetPixelAddress(0, i);
}
centerx=Dx/2.0+0.5; //计算位图中心位置
centery=Dy/2.0+0.5;
rr=sqrt(centerx*centerx+centery*centery); //计算对角线长度
theta=atan(centery/centerx);
X1=fabs(rr*cos(alpha+theta))+0.5;
Y1=fabs(rr*sin(alpha+theta))+0.5;
X2=fabs(rr*cos(alpha-theta))+0.5;
Y2=fabs(rr*sin(alpha-theta))+0.5;
if (X2>X1) X1 = X2; //得外接矩形宽度
if (Y2>Y1) Y1 = Y2; //外接矩形高度
ww=(int)(2 * X1);
dst->Destroy();
dst->Create(ww,(int)(2*Y1),src->GetBPP()); //建立结果位图
bpd = src->GetBPP()/8;
sintheta = sin(alpha);
costheta = cos(alpha);
for (int j=(int)(centery-Y1),Yd=0;j<=(centery + Y1);j++,Yd++)
{
if (src->GetBPP()==8)
memset(sc,255,ww); //256色位图像素行置背景值
else
memset(sc,255,ww*bpd); //真彩色位图像素行置背景值
for (int i=(int)(centerx-X1),Xd=0;i<=centerx+X1;i++,Xd+=bpd)
{
xx=centerx+costheta*(i-centerx)+sintheta*(j-centery);
yy=centery-sintheta*(i-centerx)+costheta*(j-centery);
x=(int)(xx+0.5);
y=(int)(yy+0.5);
if (x<0||x>= src->GetWidth()||y<0||y>=src->GetHeight())
continue;
if(x==src->GetWidth()) x--;
if(y==src->GetHeight()) y--;
memcpy(&sc[Xd],&list[y][x*bpd],bpd); //从源位图复制像素数据
}
lp=(BYTE*)dst->GetPixelAddress(0,Yd); //处理结果总结果位图
memcpy(lp,sc,ww*bpd);
}
free(list); //释放工作单元
free(sc);
}