C:用MMX指令来改进二次线性插值的旋转
inline TARGB32 AlphaBlend_MMX(TARGB32 dst,TARGB32 src)
{
unsigned long result;
asm
{
PXOR MM7,MM7
MOVD MM0,src
MOVD MM2,dst
PUNPCKLBW MM0,MM7
PUNPCKLBW MM2,MM7
MOVQ MM1,MM0
PUNPCKHWD MM1,MM1
PSUBW MM0,MM2
PUNPCKHDQ MM1,MM1
PSLLW MM2,8
PMULLW MM0,MM1
PADDW MM2,MM0
PSRLW MM2,8
PACKUSWB MM2,MM7
MOVD result,MM2
}
return *(TARGB32*)&result;
}
void __declspec(naked) __stdcall BilInear_Fast_MMX(const TPicRegion& pic,const long x_16,const long y_16,TARGB32* result)
{
asm
{
mov edx,[esp+12] //y_16
mov eax,[esp+8] //x_16
PXOR mm7,mm7
shl edx,16
shl eax,16
shr edx,24 //edx=v_8
shr eax,24 //eax=u_8
MOVD MM6,edx
MOVD MM5,eax
mov ecx,[esp+4]//pic
mov edx,[esp+12]//y_16
mov eax,[ecx]TPicRegion.byte_width
sar edx,16
imul edx,eax
add edx,[ecx]TPicRegion.pdata
add eax,edx
mov ecx,[esp+8] //x_16
sar ecx,16 //srcx_16>>16
MOVD MM2,dword ptr [eax+ecx*4] //MM2=Color1
MOVD MM0,dword ptr [eax+ecx*4+4]//MM0=Color3
PUNPCKLWD MM5,MM5
PUNPCKLWD MM6,MM6
MOVD MM3,dword ptr [edx+ecx*4] //MM3=Color0
MOVD MM1,dword ptr [edx+ecx*4+4]//MM1=Color2
PUNPCKLDQ MM5,MM5 //mm5=u_8
PUNPCKLBW MM0,MM7
PUNPCKLBW MM1,MM7
PUNPCKLBW MM2,MM7
PUNPCKLBW MM3,MM7
PSUBw MM0,MM2
PSUBw MM1,MM3
PSLLw MM2,8
PSLLw MM3,8
PMULlw MM0,MM5
PMULlw MM1,MM5
PUNPCKLDQ MM6,MM6 //mm6=v_8
PADDw MM0,MM2
PADDw MM1,MM3
PSRLw MM0,8
PSRLw MM1,8
PSUBw MM0,MM1
PSLLw MM1,8
PMULlw MM0,MM6
mov eax,[esp+16]//result
PADDw MM0,MM1
PSRLw MM0,8
PACKUSwb MM0,MM7
movd [eax],MM0
//emms
ret 16
}
}
void BilInear_Border_MMX(const TPicRegion& pic,const long x_16,const long y_16,TARGB32* result)
{
unsigned long x0=(x_16>>16);
unsigned long y0=(y_16>>16);
TARGB32 pixel[4];
bool IsInPic;
pixel[0]=Pixels_Bound(pic,x0,y0,IsInPic);
if (!IsInPic) pixel[0].a=0; else pixel[0].a=255;
pixel[2]=Pixels_Bound(pic,x0,y0+1,IsInPic);
if (!IsInPic) pixel[2].a=0; else pixel[2].a=255;
pixel[1]=Pixels_Bound(pic,x0+1,y0,IsInPic);
if (!IsInPic) pixel[1].a=0; else pixel[1].a=255;
pixel[3]=Pixels_Bound(pic,x0+1,y0+1,IsInPic);
if (!IsInPic) pixel[3].a=0; else pixel[3].a=255;
TPicRegion npic;
npic.pdata =&pixel[0];
npic.byte_width=2*sizeof(TARGB32);
//npic.width =2;
//npic.height =2;
BilInear_Fast_MMX(npic,(unsigned short)x_16,(unsigned short)y_16,result);
}
void PicRotary_BilInear_CopyLine_MMX(TARGB32* pDstLine,long dst_border_x0,long dst_in_x0,long dst_in_x1,long dst_border_x1,
const TPicRegion& SrcPic,long srcx0_16,long srcy0_16,long Ax_16,long Ay_16)
{
long x;
for (x=dst_border_x0;x<dst_in_x0;++x)
{
TARGB32 src_color;
BilInear_Border_MMX(SrcPic,srcx0_16,srcy0_16,&src_color);
pDstLine[x]=AlphaBlend_MMX(pDstLine[x],src_color);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
for (x=dst_in_x0;x<dst_in_x1;++x)
{
BilInear_Fast_MMX(SrcPic,srcx0_16,srcy0_16,&pDstLine[x]);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
for (x=dst_in_x1;x<dst_border_x1;++x)
{
TARGB32 src_color;
BilInear_Border_MMX(SrcPic,srcx0_16,srcy0_16,&src_color);
pDstLine[x]=AlphaBlend_MMX(pDstLine[x],src_color);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
asm emms
}
void PicRotaryBilInear_MMX(const TPicRegion& Dst,const TPicRegion& Src,double RotaryAngle,double ZoomX,double ZoomY,double move_x,double move_y)
{
if ( (fabs(ZoomX*Src.width)<1.0e-4) || (fabs(ZoomY*Src.height)<1.0e-4) ) return; //太小的缩放比例认为已经不可见
double tmprZoomXY=1.0/(ZoomX*ZoomY);
double rZoomX=tmprZoomXY*ZoomY;
double rZoomY=tmprZoomXY*ZoomX;
double sinA,cosA;
SinCos(RotaryAngle,sinA,cosA);
long Ax_16=(long)(rZoomX*cosA*(1<<16));
long Ay_16=(long)(rZoomX*sinA*(1<<16));
long Bx_16=(long)(-rZoomY*sinA*(1<<16));
long By_16=(long)(rZoomY*cosA*(1<<16));
double rx0=Src.width*0.5; //(rx0,ry0)为旋转中心
double ry0=Src.height*0.5;
long Cx_16=(long)((-(rx0+move_x)*rZoomX*cosA+(ry0+move_y)*rZoomY*sinA+rx0)*(1<<16));
long Cy_16=(long)((-(rx0+move_x)*rZoomX*sinA-(ry0+move_y)*rZoomY*cosA+ry0)*(1<<16));
TRotaryClipData rcData;
rcData.Ax_16=Ax_16;
rcData.Bx_16=Bx_16;
rcData.Cx_16=Cx_16;
rcData.Ay_16=Ay_16;
rcData.By_16=By_16;
rcData.Cy_16=Cy_16;
rcData.dst_width=Dst.width;
rcData.dst_height=Dst.height;
rcData.src_width=Src.width;
rcData.src_height=Src.height;
if (!rcData.inti_clip(move_x,move_y,1)) return;
TARGB32* pDstLine=Dst.pdata;
((TUInt8*&)pDstLine)+=(Dst.byte_width*rcData.out_dst_down_y);
while (true) //to down
{
long y=rcData.out_dst_down_y;
if (y>=Dst.height) break;
if (y>=0)
{
PicRotary_BilInear_CopyLine_MMX(pDstLine,rcData.out_dst_x0_boder,rcData.out_dst_x0_in,
rcData.out_dst_x1_in,rcData.out_dst_x1_boder,Src,rcData.out_src_x0_16,rcData.out_src_y0_16,Ax_16,Ay_16);
}
if (!rcData.next_clip_line_down()) break;
((TUInt8*&)pDstLine)+=Dst.byte_width;
}
pDstLine=Dst.pdata;
((TUInt8*&)pDstLine)+=(Dst.byte_width*rcData.out_dst_up_y);
while (rcData.next_clip_line_up()) //to up
{
long y=rcData.out_dst_up_y;
if (y<0) break;
((TUInt8*&)pDstLine)-=Dst.byte_width;
if (y<Dst.height)
{
PicRotary_BilInear_CopyLine_MMX(pDstLine,rcData.out_dst_x0_boder,rcData.out_dst_x0_in,
rcData.out_dst_x1_in,rcData.out_dst_x1_boder,Src,rcData.out_src_x0_16,rcData.out_src_y0_16,Ax_16,Ay_16);
}
}
}
//注:测试图片都是800*600的图片旋转到1004*1004的图片中心 测试成绩取各个旋转角度的平均速度值
////////////////////////////////////////////////////////////////////////////////
//速度测试:
//==============================================================================
// PicRotaryBilInear_MMX 100.2 fps
////////////////////////////////////////////////////////////////////////////////
D:三次卷积插值的旋转
(实现就比较简单了,几乎就是拷贝代码,然后稍微改写几个单词:)
(很多代码从《图形图像处理-之-高质量的快速的图像缩放 中篇 二次线性插值和三次卷积插值》文章来的)
inline double SinXDivX(double x)
{
//该函数计算插值曲线sin(x*PI)/(x*PI)的值 //PI=3.1415926535897932385;
//下面是它的近似拟合表达式
const float a = -1; //a还可以取 a=-2,-1,-0.75,-0.5等等,起到调节锐化或模糊程度的作用
if (x<0) x=-x; //x=abs(x);
double x2=x*x;
double x3=x2*x;
if (x<=1)
return (a+2)*x3 - (a+3)*x2 + 1;
else if (x<=2)
return a*x3 - (5*a)*x2 + (8*a)*x - (4*a);
else
return 0;
}
inline TUInt8 ColorBound(long Color)
{
if (Color<=0)
return 0;
else if (Color>=255)
return 255;
else
return Color;
}
static long SinXDivX_Table_8[(2<<8)+1];
class _CAutoInti_SinXDivX_Table {
private:
void _Inti_SinXDivX_Table()
{
for (long i=0;i<=(2<<8);++i)
SinXDivX_Table_8[i]=long(0.5+256*SinXDivX(i*(1.0/(256))));
};
public:
_CAutoInti_SinXDivX_Table() { _Inti_SinXDivX_Table(); }
};
static _CAutoInti_SinXDivX_Table __tmp_CAutoInti_SinXDivX_Table;
void ThreeOrder_Fast(const TPicRegion& pic,const long x_16,const long y_16,TARGB32* result)
{
long u_8=(unsigned char)((x_16)>>8);
long v_8=(unsigned char)((y_16)>>8);
const TARGB32* pixel=&Pixels(pic,(x_16>>16)-1,(y_16>>16)-1);
long pic_byte_width=pic.byte_width;
long au_8[4],av_8[4];
//
au_8[0]=SinXDivX_Table_8[(1<<8)+u_8];
au_8[1]=SinXDivX_Table_8[u_8];
au_8[2]=SinXDivX_Table_8[(1<<8)-u_8];
au_8[3]=SinXDivX_Table_8[(2<<8)-u_8];
av_8[0]=SinXDivX_Table_8[(1<<8)+v_8];
av_8[1]=SinXDivX_Table_8[v_8];
av_8[2]=SinXDivX_Table_8[(1<<8)-v_8];
av_8[3]=SinXDivX_Table_8[(2<<8)-v_8];
long sR=0,sG=0,sB=0,sA=0;
for (long i=0;i<4;++i)
{
long aA=au_8[0]*pixel[0].a + au_8[1]*pixel[1].a + au_8[2]*pixel[2].a + au_8[3]*pixel[3].a;
long aR=au_8[0]*pixel[0].r + au_8[1]*pixel[1].r + au_8[2]*pixel[2].r + au_8[3]*pixel[3].r;
long aG=au_8[0]*pixel[0].g + au_8[1]*pixel[1].g + au_8[2]*pixel[2].g + au_8[3]*pixel[3].g;
long aB=au_8[0]*pixel[0].b + au_8[1]*pixel[1].b + au_8[2]*pixel[2].b + au_8[3]*pixel[3].b;
sA+=aA*av_8[i];
sR+=aR*av_8[i];
sG+=aG*av_8[i];
sB+=aB*av_8[i];
((TUInt8*&)pixel)+=pic_byte_width;
}
*(unsigned long*)result=ColorBound(sB>>16) | (ColorBound(sG>>16)<<8) | (ColorBound(sR>>16)<<16)| (ColorBound(sA>>16)<<24);
}
void ThreeOrder_Border(const TPicRegion& pic,const long x_16,const long y_16,TARGB32* result)
{
unsigned long x0_sub1=(x_16>>16)-1;
unsigned long y0_sub1=(y_16>>16)-1;
long u_16_add1=((unsigned short)(x_16))+(1<<16);
long v_16_add1=((unsigned short)(y_16))+(1<<16);
TARGB32 pixel[16];
long i,j;
for (i=0;i<4;++i)
{
long y=y0_sub1+i;
for (j=0;j<4;++j)
{
long x=x0_sub1+j;
bool IsInPic;
pixel[i*4+j]=Pixels_Bound(pic,x,y,IsInPic);
if (!IsInPic) pixel[i*4+j].a=0; else pixel[i*4+j].a=255;
}
}
TPicRegion npic;
npic.pdata =&pixel[0];
npic.byte_width=4*sizeof(TARGB32);
//npic.width =4;
//npic.height =4;
ThreeOrder_Fast(npic,u_16_add1,v_16_add1,result);
}
void PicRotary_ThreeOrder_CopyLine(TARGB32* pDstLine,long dst_border_x0,long dst_in_x0,long dst_in_x1,long dst_border_x1,
const TPicRegion& SrcPic,long srcx0_16,long srcy0_16,long Ax_16,long Ay_16)
{
long x;
for (x=dst_border_x0;x<dst_in_x0;++x)
{
TARGB32 src_color;
ThreeOrder_Border(SrcPic,srcx0_16,srcy0_16,&src_color);
pDstLine[x]=AlphaBlend(pDstLine[x],src_color);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
for (x=dst_in_x0;x<dst_in_x1;++x)
{
ThreeOrder_Fast(SrcPic,srcx0_16,srcy0_16,&pDstLine[x]);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
for (x=dst_in_x1;x<dst_border_x1;++x)
{
TARGB32 src_color;
ThreeOrder_Border(SrcPic,srcx0_16,srcy0_16,&src_color);
pDstLine[x]=AlphaBlend(pDstLine[x],src_color);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
}
void PicRotaryThreeOrder(const TPicRegion& Dst,const TPicRegion& Src,double RotaryAngle,double ZoomX,double ZoomY,double move_x,double move_y)
{
if ( (fabs(ZoomX*Src.width)<1.0e-4) || (fabs(ZoomY*Src.height)<1.0e-4) ) return; //太小的缩放比例认为已经不可见
double tmprZoomXY=1.0/(ZoomX*ZoomY);
double rZoomX=tmprZoomXY*ZoomY;
double rZoomY=tmprZoomXY*ZoomX;
double sinA,cosA;
SinCos(RotaryAngle,sinA,cosA);
long Ax_16=(long)(rZoomX*cosA*(1<<16));
long Ay_16=(long)(rZoomX*sinA*(1<<16));
long Bx_16=(long)(-rZoomY*sinA*(1<<16));
long By_16=(long)(rZoomY*cosA*(1<<16));
double rx0=Src.width*0.5; //(rx0,ry0)为旋转中心
double ry0=Src.height*0.5;
long Cx_16=(long)((-(rx0+move_x)*rZoomX*cosA+(ry0+move_y)*rZoomY*sinA+rx0)*(1<<16));
long Cy_16=(long)((-(rx0+move_x)*rZoomX*sinA-(ry0+move_y)*rZoomY*cosA+ry0)*(1<<16));
TRotaryClipData rcData;
rcData.Ax_16=Ax_16;
rcData.Bx_16=Bx_16;
rcData.Cx_16=Cx_16;
rcData.Ay_16=Ay_16;
rcData.By_16=By_16;
rcData.Cy_16=Cy_16;
rcData.dst_width=Dst.width;
rcData.dst_height=Dst.height;
rcData.src_width=Src.width;
rcData.src_height=Src.height;
if (!rcData.inti_clip(move_x,move_y,2)) return;
TARGB32* pDstLine=Dst.pdata;
((TUInt8*&)pDstLine)+=(Dst.byte_width*rcData.out_dst_down_y);
while (true) //to down
{
long y=rcData.out_dst_down_y;
if (y>=Dst.height) break;
if (y>=0)
{
PicRotary_ThreeOrder_CopyLine(pDstLine,rcData.out_dst_x0_boder,rcData.out_dst_x0_in,
rcData.out_dst_x1_in,rcData.out_dst_x1_boder,Src,rcData.out_src_x0_16,rcData.out_src_y0_16,Ax_16,Ay_16);
}
if (!rcData.next_clip_line_down()) break;
((TUInt8*&)pDstLine)+=Dst.byte_width;
}
pDstLine=Dst.pdata;
((TUInt8*&)pDstLine)+=(Dst.byte_width*rcData.out_dst_up_y);
while (rcData.next_clip_line_up()) //to up
{
long y=rcData.out_dst_up_y;
if (y<0) break;
((TUInt8*&)pDstLine)-=Dst.byte_width;
if (y<Dst.height)
{
PicRotary_ThreeOrder_CopyLine(pDstLine,rcData.out_dst_x0_boder,rcData.out_dst_x0_in,
rcData.out_dst_x1_in,rcData.out_dst_x1_boder,Src,rcData.out_src_x0_16,rcData.out_src_y0_16,Ax_16,Ay_16);
}
}
}
//注:测试图片都是800*600的图片旋转到1004*1004的图片中心 测试成绩取各个旋转角度的平均速度值
////////////////////////////////////////////////////////////////////////////////
//速度测试:
//==============================================================================
// PicRotaryThreeOrder 22.8 fps
////////////////////////////////////////////////////////////////////////////////
三次卷积插值旋转结果图示:
30度 60度 90度
120度 150度 180度
210度 240度 270度
300度 330度 360度
E:用MMX优化三次卷积插值的旋转
//注:测试图片都是800*600的图片旋转到1004*1004的图片中心 测试成绩取各个旋转角度的平均速度值
////////////////////////////////////////////////////////////////////////////////
//速度测试:
//==============================================================================
// PicRotaryThreeOrder_MMX 44.2 fps
////////////////////////////////////////////////////////////////////////////////
typedef unsigned long TMMXData32;
static TMMXData32 SinXDivX_Table_MMX[(2<<8)+1];
class _CAutoInti_SinXDivX_Table_MMX {
private:
void _Inti_SinXDivX_Table_MMX()
{
for (long i=0;i<=(2<<8);++i)
{
unsigned short t=long(0.5+(1<<14)*SinXDivX(i*(1.0/(256))));
unsigned long tl=t | (((unsigned long)t)<<16);
SinXDivX_Table_MMX[i]=tl;
}
};
public:
_CAutoInti_SinXDivX_Table_MMX() { _Inti_SinXDivX_Table_MMX(); }
};
static _CAutoInti_SinXDivX_Table_MMX __tmp_CAutoInti_SinXDivX_Table_MMX;
void __declspec(naked) _private_ThreeOrder_Fast_MMX()
{
asm
{
movd mm1,dword ptr [edx]
movd mm2,dword ptr [edx+4]
movd mm3,dword ptr [edx+8]
movd mm4,dword ptr [edx+12]
movd mm5,dword ptr [(offset SinXDivX_Table_MMX)+256*4+eax*4]
movd mm6,dword ptr [(offset SinXDivX_Table_MMX)+eax*4]
punpcklbw mm1,mm7
punpcklbw mm2,mm7
punpcklwd mm5,mm5
punpcklwd mm6,mm6
psllw mm1,7
psllw mm2,7
pmulhw mm1,mm5
pmulhw mm2,mm6
punpcklbw mm3,mm7
punpcklbw mm4,mm7
movd mm5,dword ptr [(offset SinXDivX_Table_MMX)+256*4+ecx*4]
movd mm6,dword ptr [(offset SinXDivX_Table_MMX)+512*4+ecx*4]
punpcklwd mm5,mm5
punpcklwd mm6,mm6
psllw mm3,7
psllw mm4,7
pmulhw mm3,mm5
pmulhw mm4,mm6
paddsw mm1,mm2
paddsw mm3,mm4
movd mm6,dword ptr [ebx] //v
paddsw mm1,mm3
punpcklwd mm6,mm6
pmulhw mm1,mm6
add edx,esi //+pic.byte_width
paddsw mm0,mm1
ret
}
}
inline void ThreeOrder_Fast_MMX(const TPicRegion& pic,const long x_16,const long y_16,TARGB32* result)
{
asm
{
mov ecx,pic
mov eax,y_16
mov ebx,x_16
movzx edi,ah //v_8
mov edx,[ecx+TPicRegion.pdata]
shr eax,16
mov esi,[ecx+TPicRegion.byte_width]
dec eax
movzx ecx,bh //u_8
shr ebx,16
imul eax,esi
lea edx,[edx+ebx*4-4]
add edx,eax //pixel
mov eax,ecx
neg ecx
pxor mm7,mm7 //0
//mov edx,pixel
pxor mm0,mm0 //result=0
//lea eax,auv_7
lea ebx,[(offset SinXDivX_Table_MMX)+256*4+edi*4]
call _private_ThreeOrder_Fast_MMX
lea ebx,[(offset SinXDivX_Table_MMX)+edi*4]
call _private_ThreeOrder_Fast_MMX
neg edi
lea ebx,[(offset SinXDivX_Table_MMX)+256*4+edi*4]
call _private_ThreeOrder_Fast_MMX
lea ebx,[(offset SinXDivX_Table_MMX)+512*4+edi*4]
call _private_ThreeOrder_Fast_MMX
psraw mm0,3
mov eax,result
packuswb mm0,mm7
movd [eax],mm0
emms
}
}
void ThreeOrder_Border_MMX(const TPicRegion& pic,const long x_16,const long y_16,TARGB32* result)
{
unsigned long x0_sub1=(x_16>>16)-1;
unsigned long y0_sub1=(y_16>>16)-1;
long u_16_add1=((unsigned short)(x_16))+(1<<16);
long v_16_add1=((unsigned short)(y_16))+(1<<16);
TARGB32 pixel[16];
long i,j;
for (i=0;i<4;++i)
{
long y=y0_sub1+i;
for (j=0;j<4;++j)
{
long x=x0_sub1+j;
bool IsInPic;
pixel[i*4+j]=Pixels_Bound(pic,x,y,IsInPic);
if (!IsInPic) pixel[i*4+j].a=0; else pixel[i*4+j].a=255;
}
}
TPicRegion npic;
npic.pdata =&pixel[0];
npic.byte_width=4*sizeof(TARGB32);
//npic.width =4;
//npic.height =4;
ThreeOrder_Fast_MMX(npic,u_16_add1,v_16_add1,result);
}
void PicRotary_ThreeOrder_CopyLine_MMX(TARGB32* pDstLine,long dst_border_x0,long dst_in_x0,long dst_in_x1,long dst_border_x1,
const TPicRegion& SrcPic,long srcx0_16,long srcy0_16,long Ax_16,long Ay_16)
{
long x;
for (x=dst_border_x0;x<dst_in_x0;++x)
{
TARGB32 src_color;
ThreeOrder_Border_MMX(SrcPic,srcx0_16,srcy0_16,&src_color);
pDstLine[x]=AlphaBlend_MMX(pDstLine[x],src_color);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
for (x=dst_in_x0;x<dst_in_x1;++x)
{
ThreeOrder_Fast_MMX(SrcPic,srcx0_16,srcy0_16,&pDstLine[x]);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
for (x=dst_in_x1;x<dst_border_x1;++x)
{
TARGB32 src_color;
ThreeOrder_Border_MMX(SrcPic,srcx0_16,srcy0_16,&src_color);
pDstLine[x]=AlphaBlend_MMX(pDstLine[x],src_color);
srcx0_16+=Ax_16;
srcy0_16+=Ay_16;
}
asm emms
}
void PicRotaryThreeOrder_MMX(const TPicRegion& Dst,const TPicRegion& Src,double RotaryAngle,double ZoomX,double ZoomY,double move_x,double move_y)
{
if ( (fabs(ZoomX*Src.width)<1.0e-4) || (fabs(ZoomY*Src.height)<1.0e-4) ) return; //太小的缩放比例认为已经不可见
double tmprZoomXY=1.0/(ZoomX*ZoomY);
double rZoomX=tmprZoomXY*ZoomY;
double rZoomY=tmprZoomXY*ZoomX;
double sinA,cosA;
SinCos(RotaryAngle,sinA,cosA);
long Ax_16=(long)(rZoomX*cosA*(1<<16));
long Ay_16=(long)(rZoomX*sinA*(1<<16));
long Bx_16=(long)(-rZoomY*sinA*(1<<16));
long By_16=(long)(rZoomY*cosA*(1<<16));
double rx0=Src.width*0.5; //(rx0,ry0)为旋转中心
double ry0=Src.height*0.5;
long Cx_16=(long)((-(rx0+move_x)*rZoomX*cosA+(ry0+move_y)*rZoomY*sinA+rx0)*(1<<16));
long Cy_16=(long)((-(rx0+move_x)*rZoomX*sinA-(ry0+move_y)*rZoomY*cosA+ry0)*(1<<16));
TRotaryClipData rcData;
rcData.Ax_16=Ax_16;
rcData.Bx_16=Bx_16;
rcData.Cx_16=Cx_16;
rcData.Ay_16=Ay_16;
rcData.By_16=By_16;
rcData.Cy_16=Cy_16;
rcData.dst_width=Dst.width;
rcData.dst_height=Dst.height;
rcData.src_width=Src.width;
rcData.src_height=Src.height;
if (!rcData.inti_clip(move_x,move_y,2)) return;
TARGB32* pDstLine=Dst.pdata;
((TUInt8*&)pDstLine)+=(Dst.byte_width*rcData.out_dst_down_y);
while (true) //to down
{
long y=rcData.out_dst_down_y;
if (y>=Dst.height) break;
if (y>=0)
{
PicRotary_ThreeOrder_CopyLine_MMX(pDstLine,rcData.out_dst_x0_boder,rcData.out_dst_x0_in,
rcData.out_dst_x1_in,rcData.out_dst_x1_boder,Src,rcData.out_src_x0_16,rcData.out_src_y0_16,Ax_16,Ay_16);
}
if (!rcData.next_clip_line_down()) break;
((TUInt8*&)pDstLine)+=Dst.byte_width;
}
pDstLine=Dst.pdata;
((TUInt8*&)pDstLine)+=(Dst.byte_width*rcData.out_dst_up_y);
while (rcData.next_clip_line_up()) //to up
{
long y=rcData.out_dst_up_y;
if (y<0) break;
((TUInt8*&)pDstLine)-=Dst.byte_width;
if (y<Dst.height)
{
PicRotary_ThreeOrder_CopyLine_MMX(pDstLine,rcData.out_dst_x0_boder,rcData.out_dst_x0_in,
rcData.out_dst_x1_in,rcData.out_dst_x1_boder,Src,rcData.out_src_x0_16,rcData.out_src_y0_16,Ax_16,Ay_16);
}
}
}
F 效果图:
//程序使用的调用参数:
const long testcount=2000;
long dst_wh=1004;
for (int i=0;i {
double zoom=rand()*(1.0/RAND_MAX)+0.5;
PicRotary_XXX(ppicDst,ppicSrc,rand()*(PI*2/RAND_MAX),zoom,zoom,((dst_wh+ppicSrc.width)*rand()*(1.0/RAND_MAX)-ppicSrc.width),(dst_wh+ppicSrc.height)*rand()*(1.0/RAND_MAX)-ppicSrc.height);
}
近邻取样插值旋转效果图:
二次线性插值旋转效果图:
三次卷积插值旋转效果图:
G:旋转测试的结果放到一起:
//注:测试图片都是800*600的图片旋转到1004*1004的图片中心,测试成绩取各个旋转角度的平均速度值
////////////////////////////////////////////////////////////////////////////////
//速度测试: (测试CPU为AMD64x2 4200+(2.37G),单线程)
//==============================================================================
// PicRotary3 280.9 fps
// PicRotarySEE 306.3 fps
// PicRotarySEE2 304.2 fps
//
// PicRotaryBilInear 68.9 fps
// PicRotaryBilInear_MMX 100.2 fps
// PicRotaryThreeOrder 22.8 fps
// PicRotaryThreeOrder_MMX 44.2 fps
////////////////////////////////////////////////////////////////////////////////
补充Intel Core2 4400上的测试成绩:
////////////////////////////////////////////////////////////////////////////////
//速度测试: (测试CPU为Intel Core2 4400(2.00G)单线程)
//==============================================================================
// PicRotary3 334.9 fps
// PicRotarySEE 463.1 fps
// PicRotarySEE2 449.3 fps
//
// PicRotaryBilInear 68.9 fps
// PicRotaryBilInear_MMX 109.5 fps
// PicRotaryThreeOrder 24.0 fps
// PicRotaryThreeOrder_MMX 45.9 fps
////////////////////////////////////////////////////////////////////////////////
(针对大图片的预读缓冲区优化的旋转请参见《下篇 补充话题》中的优化版本)
(对于旋转的MipMap处理和三次线性插值,可以参考《图形图像处理-之-高质量的快速的图像缩放 下篇 三次线性插值和MipMap链》文章)
(这里为了函数的独立性和容易理解,都是拷贝代码然后稍作修改;实际的程序中,建议把他们合并到同一个函数中,
减少代码量,提高可维护性; 对于MMX、SSE、SSE2等的使用建议用CPUID指令测试看CPU是否支持这些指令,
动态决定调用不同的实现)