int PicZoom(PT_PixelDatas ptOriginPic, PT_PixelDatas ptZoomPic)
{
unsigned long dwDstWidth = ptZoomPic->iWidth;
unsigned long* pdwSrcXTable;
unsigned long x;
unsigned long y;
unsigned long dwSrcY;
unsigned char *pucDest;
unsigned char *pucSrc;
unsigned long dwPixelBytes = ptOriginPic->iBpp/8;
if (ptOriginPic->iBpp != ptZoomPic->iBpp)
{
return -1;
}
pdwSrcXTable = malloc(sizeof(unsigned long) * dwDstWidth);
if (NULL == pdwSrcXTable)
{
DBG_PRINTF("malloc error!\n");
return -1;
}
for (x = 0; x < dwDstWidth; x++)
{
pdwSrcXTable[x]=(x*ptOriginPic->iWidth/ptZoomPic->iWidth);
}
for (y = 0; y < ptZoomPic->iHeight; y++)
{
dwSrcY = (y * ptOriginPic->iHeight / ptZoomPic->iHeight);
pucDest = ptZoomPic->aucPixelDatas + y*ptZoomPic->iLineBytes;
pucSrc = ptOriginPic->aucPixelDatas + dwSrcY*ptOriginPic->iLineBytes;
for (x = 0; x <dwDstWidth; x++)
{
memcpy(pucDest+x*dwPixelBytes, pucSrc+pdwSrcXTable[x]*dwPixelBytes, dwPixelBytes);
}
}
free(pdwSrcXTable);
return 0;
}
参考
void PicZoom0(const TPicRegion& Dst,const TPicRegion& Src)
{
if ( (0==Dst.width)||(0==Dst.height)
||(0==Src.width)||(0==Src.height)) return;
for (long x=0;x<Dst.width;++x)
{
for (long y=0;y<Dst.height;++y)
{
long srcx=(x*Src.width/Dst.width);
long srcy=(y*Src.height/Dst.height);
Pixels(Dst,x,y)=Pixels(Src,srcx,srcy);
}
}
}
总结:按比例缩放