滤镜之变形Distort特效

来源:互联网 发布:淘宝宝贝拍照要求 编辑:程序博客网 时间:2024/05/10 21:35

一些扭曲Distort特效也在滤镜中比较常见,而且如果在视频拍摄的时候使用则一般会比较有趣,如在Mac电脑上的PhotoBooth软件拍摄就有很多变形的Effect。这里介绍几种特效包括有:挤压(pinch)、球面特效(Spherize)、漩涡(Swirl)、波浪(Wave)等,这几个特效在PhotoSprite软件中都有代码。其他的一些变形也可以根据公式写出来,写出自己独特的一些变形特效出来。

这些特效都是对像素的位置进行改变,而不改变像素值,利用一些数学上的变换公式来进行。

原图:



l       挤压效果


代码:

 // 弧度、半径        double radian, radius;        for (int y = 0; y < height; y++)        {          for (int x = 0; x < width; x++)          {            // 当前点与图像中心点的偏移量            offsetX = x - midX;            offsetY = y - midY;            // 弧度            radian = Math.Atan2(offsetY, offsetX);            // 半径            radius = Math.Sqrt(offsetX * offsetX + offsetY * offsetY);            radius = Math.Sqrt(radius) * degree;            // 映射实际像素点            X = (int)(radius * Math.Cos(radian)) + midX;            Y = (int)(radius * Math.Sin(radian)) + midY;            // 边界约束            if (X < 0) X = 0;            if (X >= width) X = width - 1;            if (Y < 0) Y = 0;            if (Y >= height) Y = height - 1;            src = (byte*)srcScan0 + Y * stride + X * BPP;            dst[3] = src[3]; // A            dst[2] = src[2]; // R            dst[1] = src[1]; // G            dst[0] = src[0]; // B            dst += BPP;          } // x          dst += offset;        } // y      }

l       球面效果


代码:
// 弧度、半径        double radian, radius;        int midX = width / 2;        int midY = height / 2;        // Max(midX, midY)        double maxMidXY = (midX > midY ? midX : midY);        for (int y = 0; y < height; y++)        {          for (int x = 0; x < width; x++)          {            // 当前点与图像中心点的偏移量            offsetX = x - midX;            offsetY = y - midY;            // 弧度            radian = Math.Atan2(offsetY, offsetX);            // 注意,这里并非实际半径            radius = (offsetX * offsetX + offsetY * offsetY) / maxMidXY;            // 映射实际像素点            X = (int)(radius * Math.Cos(radian)) + midX;            Y = (int)(radius * Math.Sin(radian)) + midY;            // 边界约束            if (X < 0) X = 0;            if (X >= width) X = width - 1;            if (Y < 0) Y = 0;            if (Y >= height) Y = height - 1;            src = (byte*)srcScan0 + Y * stride + X * BPP;            dst[3] = src[3]; // A            dst[2] = src[2]; // R            dst[1] = src[1]; // G            dst[0] = src[0]; // B            dst += BPP;          } // x          dst += offset;        } // y      }

l       漩涡效果


代码:

 // 弧度、半径        double radian, radius;        for (int y = 0; y < height; y++)        {          for (int x = 0; x < width; x++)          {            // 当前点与图像中心点的偏移量            offsetX = x - midX;            offsetY = y - midY;            // 弧度            radian = Math.Atan2(offsetY, offsetX);            // 半径,即两点间的距离            radius = Math.Sqrt(offsetX * offsetX + offsetY * offsetY);            // 映射实际像素点            X = (int)(radius * Math.Cos(radian + swirlDegree * radius)) + midX;            Y = (int)(radius * Math.Sin(radian + swirlDegree * radius)) + midY;            // 边界约束            if (X < 0) X = 0;            if (X >= width) X = width - 1;            if (Y < 0) Y = 0;            if (Y >= height) Y = height - 1;            src = (byte*)srcScan0 + Y * stride + X * BPP;            dst[3] = src[3]; // A            dst[2] = src[2]; // R            dst[1] = src[1]; // G            dst[0] = src[0]; // B            dst += BPP;          } // x          dst += offset;        } // y      }

l       波浪效果


代码:
double PI2 = Math.PI * 2.0;        for (int y = 0; y < height; y++)        {          for (int x = 0; x < width; x++)          {            X = (int)(degree * Math.Sin(PI2 * y / 128.0)) + x;            Y = (int)(degree * Math.Cos(PI2 * x / 128.0)) + y;            // 边界约束            if (X < 0) X = 0;            if (X >= width) X = width - 1;            if (Y < 0) Y = 0;            if (Y >= height) Y = height - 1;            src = (byte*)srcScan0 + Y * stride + X * BPP;            dst[3] = src[3]; // A            dst[2] = src[2]; // R            dst[1] = src[1]; // G            dst[0] = src[0]; // B            dst += BPP;          } // x          dst += offset;        } // y      }



原创粉丝点击