Sample C– zooming code

#### 第一篇文章我们讨论了C–的基本图像导入、处理以及多个图片的算术运算,本篇我们学习一下C–的zoom。
##### Forward Transform and inverse transform
以坐标(x0,y0)为center进行zooming,代码为

byte[,] IM(byte[,] f,int x0,int y0,float K){
	int w = f.GetLength(0);
	int h = f.GetLength(1);
	byte[,] g = new byte[w,h];
	for (int x = 0;x<w;x++)
		for(int y = 0;y<h;y++){
			int u = (int)((x-x0)/K + x0);
			int v = (int)((y-y0)/K + y0);
			if(u>=0 && u<w && v>=0 && v<h)
			g[x,y] = f[u,v];
	}
	return g;
}
void main(){
	byte[,] f = LoadImg();
	if(f==null) return;
	ShowImg("f",f);
	ShowImg("g",IM(f),128,128,3);
} 强制类型转换为*int*,相当于inverse transform,f(u,v)=input, g(x,y)=output. ##### Zooming with the limit of circle

byte[,] IM(byte[,] f,int x0,int y0,float K,float R){
	int w = f.GetLength(0);
	int h = f.GetLength(1);
	byte[,] g = new byte[w,h];
	for (int x = 0;x<w;x++)
		for(int y = 0;y<h;y++){
			if((x-x0)*(x-x0)+(y-y0)*(y-y0)<R*R){
		
			int u = (int)((x-x0)/K + x0);
			int v = (int)((y-y0)/K + y0);
			if(u>=0 && u<w && v>=0 && v<h)
			g[x,y] = f[u,v];
			}
			else 
			g[x,y] = f[x,y];
	}
	return g;
}

void main(){
	byte[,] f = LoadImg();
	if(f==null) return;
	ShowImg("f",f);
	ShowImg("g",IM(f,128,128,3,30));
} ##### Better zoom with the limit of circle
byte[,] IM(byte[,] f,int x0,int y0,double K,double R){
	int w = f.GetLength(0);
	int h = f.GetLength(1);
	byte[,] g = new byte[w,h];
	for (int x = 0;x<w;x++)
		for(int y = 0;y<h;y++){
			double d = Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0));
		if(d>R) d = R;		
		double k = K*(1-d/R)+d/R;
		int u = (int)((x-x0)/k + x0);
		int v = (int)((y-y0)/k + y0);
		if(u>=0 && u<w && v>=0 && v<h)
			g[x,y] = f[u,v];
		}
	return g;
	}

void main(){
	byte[,] f = LoadImg();
	if(f==null) return;
	ShowImg("f",f);
	ShowImg("g",IM(f,128,128,3,30));
} ##### Zoom in with a larger picture
byte[,] ZoomIn(byte[,] f){
	int w = f.GetLength(0);
	int h = f.GetLength(1);
	byte[,] g = new byte[4*w,4*h];
	for (int x = 0;x<4*w;x++)
		for(int y = 0;y<4*h;y++){
			int u = x/4;
			int v = y/4;
			g[x,y] = f[u,v];
	}
return g;
}

void main(){
	byte[,] f = LoadImg();
	if(f==null) return;
	ShowImg("f",f);
	ShowImg("g",ZoomIn(f));
}