下图是0.67x,0.67y的图像缩放:
当界面想多放几张图,有时图像太大,界面窗口放不下,就会有孙大圣绣花针的想法:
void ZoomNormal(BYTE * image0,BYTE * &image1,unsigned int w,unsigned int h,
unsigned int & outwidth,unsigned int & outheight,double ZoomX,double ZoomY)
{
outwidth=(int)(w*ZoomX+0.5);
outheight=(int)(h*ZoomY+0.5);
int newbuffersize=outwidth*outheight*4;
image1=new BYTE[newbuffersize];
memset(image1,255,newbuffersize);
BYTE *copypixel=NULL;
BYTE *objpixel=NULL;
int x=0;
int y=0;
int tempY;
int tempJ;
for(int j=0;j<outheight;j++)
{
y=(int)(j/ZoomY+0.5);
if(y>=h) y--;
tempY=y*w*4;
tempJ=j*outwidth*4;
for(int i=0;i<outwidth;i++)
{
x=(int)(i/ZoomX+0.5);
if(x>=w)
x--;
copypixel=image0+tempY+x*4;
objpixel=image1+tempJ+i*4;
memcpy(objpixel,copypixel,4);
}
}
其实除了图像的缩放,图像为了适应窗口,也是需要作出改变的,在此基础上,你能否把他改为有自适应窗口的功能呢?
这也是跟随(following)鼠标自由缩放的基础(博客中已存在),从这里,也隐含了图像尺度的概念,再如,图像金字塔的实现等等。
图像处理,要求速度高,你能否在这个算法上提高速度呢?
缩小会失去像素,放大需要插值,对于图像处理,都是比较谨慎的,因为原图像才是蓝本。
补充说明(202009281719):
以上代码是32位图像的缩放,以下是8位图像的缩放,c#代码,做一对比:
void ZoomNormal(byte[] image0, ref byte[] image1, int w, int h,
ref int outwidth, ref int outheight, double ZoomX, double ZoomY)
{
outwidth = (int)(w * ZoomX + 0.5);
outheight = (int)(h * ZoomY + 0.5);
int newbuffersize = outwidth * outheight ;
image1 = new byte[newbuffersize];
int x = 0;
int y = 0;
int tempY;
int tempJ;
for (int j = 0; j < outheight; j++)
{
y = (int)(j / ZoomY + 0.5);
if (y >= h) y--;
tempY = y * w ;
tempJ = j * outwidth ;
for (int i = 0; i < outwidth; i++)
{
x = (int)(i / ZoomX + 0.5);
if (x >= w)
x--;
image1[tempJ + i ] = image0[tempY + x ];
}
}
}