滤镜之瓷砖TileReflect

首先看这两张效果图:

滤镜之瓷砖TileReflect_第1张图片

滤镜之瓷砖TileReflect_第2张图片滤镜之瓷砖TileReflect_第3张图片


这个滤镜在ImageStone这个软件中有完整代码,现在改写如下:


#define PI 3.1415926535897932384626433832795

struct Point
{
	float x;
	float y;
};

void TileRefelctRGB(unsigned char* pInput,unsigned char* pOutput,int width,int height,int nStride
	,int nAngle,int nCount,int nSquareSize)
{
	float _sin,_cos,scale,curvature;
	int aasamples = 16;
	struct Point* aapt = (struct Point*)malloc(aasamples*sizeof(struct Point));
	float x,y;
	int i,j,k;
	float u,v,s,t;
	int xSample,ySample;
	float hw = width / 2.0;
	float hh = height / 2.0;
	int r,g,b,index;

	if(nAngle < -45)
		nAngle = -45;
	if(nAngle > 45)
		nAngle = 45;

	_sin = sin(nAngle*PI/180) ;
	_cos = cos(nAngle*PI/180) ;

	if(nSquareSize < 2)
		nSquareSize = 2;
	if(nSquareSize>200)
		nSquareSize = 200;

	scale = PI / (float)nSquareSize ;

	if (nCount < -20)
		nCount = -20 ;
	if(nCount > 20)
		nCount = 20;

	curvature = nCount * nCount / 10.0 * (abs(nCount)/nCount) ;

	for (i=0 ; i < aasamples ; i++)
	{
		x = (i * 4) / (float)aasamples;
		y = i / (float)aasamples;
		x = x - (int)x ;
		aapt[i].x = _cos * x + _sin * y ;
		aapt[i].y = _cos * y - _sin * x ;
	}

	for (y=0;y<height;y++)
	{
		for (x=0;x<width;x++)
		{
			i = x - hw,
			j = y - hh ;

			r=g=b=0;
			for (k=0;k<aasamples;k++)
			{
				u = i + aapt[k].x ;
				v = j - aapt[k].y ;
				s =  _cos * u + _sin * v ;
				t = -_sin * u + _cos * v ;

				s += curvature * tan(s * scale) ;
				t += curvature * tan(t * scale) ;
				u = _cos * s - _sin * t ;
				v = _sin * s + _cos * t ;

				xSample = (int)(hw + u) ;
				ySample = (int)(hh + v) ;

				if(xSample > width-1)
					xSample = width-1;
				if(xSample<0)
					xSample = 0;
				if(ySample>height-1)
					ySample = height-1;
				if(ySample<0)
					ySample = 0;

				index = xSample*3 + ySample*nStride;
				r += pInput[index];
				g += pInput[index+1];
				b += pInput[index+2];
			}

			index = x*3+y*nStride;
			pOutput[index] = r / aasamples;
			pOutput[index+1] = g / aasamples;
			pOutput[index+2] = b / aasamples;
		}
	}

	free(aapt);
}

该滤镜有三个参数:

1.     角度:控制方向

2.     大小:控制瓷砖大小

3.     数量:控制瓷砖中变形的像素数


具体的流程大家可以根据代码去理解,无非是像素点的scale,rotate以及offset等,assample来控制精度。

你可能感兴趣的:(图像,滤镜,瓷砖)