Pinch滤镜
Pinch滤镜是通过坐标变换来实现以某个点(cenX,cenY)为中心,某个半径R内图像向其挤压变形的效果。实现这个滤镜的算法很多,主要是数学公式的不同,大家可以自行设计,这里给个代码示例,大家可以直接使用。
代码如下:
~~~
/// <summary>
/// Pinch Filter
/// </summary>
/// <param name="src">Source image.</param>
/// <param name="cenX">The X position of sun.</param>
/// <param name="cenY">The Y position of sun.</param>
/// <returns>The result image.</returns>
private Bitmap PinchFilterProcess(Bitmap srcBitmap, int cenX, int cenY)
{
Bitmap a = new Bitmap(srcBitmap);
int w = a.Width;
int h = a.Height;
int radius = 0;
Bitmap dst = new Bitmap(w, h);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p = null;
int sWidth = srcData.Stride;
int stride = sWidth - w * 4;
int offsetX = 0, offsetY = 0;
int newX = 0, newY = 0;
double radian = 0,degree = 10;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
offsetX = x - cenX;
offsetY = y - cenY;
radian = Math.Atan2(offsetY, offsetX);
radius = (int)(Math.Sqrt(offsetX * offsetX + offsetY * offsetY));
radius = (int)(Math.Sqrt(radius) * degree);
newX = (int)(radius * Math.Cos(radian)) + cenX;
newY = (int)(radius * Math.Sin(radian)) + cenY;
newX = Math.Min(w - 1, Math.Max(0, newX));
newY = Math.Min(h - 1, Math.Max(0, newY));
p = pIn + newY * srcData.Stride + newX * 4;
pOut[0] = (byte)p[0];
pOut[1] = (byte)p[1];
pOut[2] = (byte)p[2];
pOut[3] = (byte)255;
pOut += 4;
}
pOut += stride;
}
a.UnlockBits(srcData);
dst.UnlockBits(dstData);
}
return dst;
}
~~~
效果图如下:
[![](https://box.kancloud.cn/2016-01-05_568b3326aa2bd.jpg)](http://www.zealpixel.com/data/attachment/portal/201507/21/221354xqq5q0fkq5fqn0fj.jpg)
原图
[![](https://box.kancloud.cn/2016-01-05_568b3326c4cfd.png)](http://www.zealpixel.com/data/attachment/portal/201507/21/221353y6czmsej4ces55jk.png)
Pinch滤镜效果图
最后放上一个完整的C#版程序Demo给大家下载使用:[http://www.zealpixel.com/forum.php?mod=viewthread&tid=55&extra=page%3D1](http://www.zealpixel.com/forum.php?mod=viewthread&tid=55&extra=page%3D1)
- 前言
- 序言
- Brannan滤镜
- 编码基础(Photoshop基础变换的代码实现)
- Toaster滤镜
- Hudson滤镜(Instagram)
- 暴雨滤镜
- 大雪滤镜
- 图像滤镜实现万能方法研究
- 大雾效果滤镜
- 连环画滤镜
- 暗调滤镜
- 怀旧风格滤镜
- (Nostalgla Filter)老照片滤镜
- (Punch Filter)交叉冲印滤镜
- (Lightleaks Filter)漏光滤镜
- 漫画滤镜
- LOMO Filter
- Glow Filter发光滤镜
- (Instagram)1977滤镜
- (Sketch Filter)素描滤镜
- 水彩画滤镜
- 图像光照效果滤镜
- Oilpaint油画滤镜
- Swirl滤镜
- Wave滤镜
- 球面(Spherize)滤镜
- 挤压(Pinch)滤镜
- 旋转模糊滤镜
- 霓虹、浮雕、木刻滤镜
- 图像滤镜晕影调节算法研究
- PS平均(滤镜-模糊-平均)效果
- Photoshop实现Instagram Amaro滤镜特效
- Photoshop实现Instagram之Nashville滤镜
- Photoshop实现Instagram之Sierra滤镜
- Photoshop实现Instagram之Mayfair滤镜效果
- ZPhotoEngine超级算法库
- 乐高像素拼图特效
- 乐高像素拼图特效滤镜的代码实现
- 保留细节的磨皮滤镜之PS实现
- 保留细节的磨皮之C#程序实现
- 流行艺术风滤镜特效PS实现
- PS图层混合模式之明度模式