图像滤镜艺术---乐高像素拼图特效滤镜的代码实现

这篇博客承接上一篇http://blog.csdn.net/trent1985/article/details/50195747乐高像素拼图特效的PS实现,主要是一些博友告诉我如何用代码实现这个PS的特效,为此,这里以这个特效为例,来说一下。

这个特效的实现我们将使用ZPhotoEngine库来完成,采用C#编程,步骤如下:

1,打开VS构建工程TestDemo,添加一些基本功能:打开图像,保存图像,这个代码我会给出连接,这个不是重点,大家接着往下看。

2,在工程中导入ZPhotoEngine的dll,放在bin/debug,bin/Release文件夹中,主要包含这几个:pthreadVC2.dll、ZPhotoEngine.dll,ZEffectEngine.dll不添加也没关系,这个特效中没有使用到这个滤镜模块库。

3,根据ZPhotoEngine库的说明文档,或者ZPhotoEngine.h的接口说明,对接口进行封装。

实际上ZPhotoEngine库的DEMO中已经有了大部分的封装,都在ZPhotoEngineDll.cs中,这里只缺少了图层混合模式的接口封装,我们封装之后,所有代码如下,大家只需要把这个文件添加到你的工程中即可:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace TestDemo
{
    unsafe class ZPhotoEngineDll
    {
        #region PS基本变换模块
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Desaturate(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Threshold(byte* srcData, int width, int height, int stride, int threshold);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_SaturationAdjust(byte* srcData, int width, int height, int stride, int hue);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Posterize(byte* srcData, int width, int height, int stride, int clusterNum);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_OverExposure(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_LightnessAdjust(byte* srcData, int width, int height, int stride, int lightness);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Invert(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_HueAndSaturationAdjust(byte* srcData, int width, int height, int stride, int hue, int saturation);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_HistagramEqualize(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_CurveAdjust(byte* srcData, int width, int height, int stride, int DestChannel, int InputLeftLimit, int InputMiddle, int InputRightLimit, int OutputLeftLimit, int OutputRightLimit);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_NLinearBrightContrastAdjust(byte* srcData, int width, int height, int stride, int bright, int contrast, int threshold);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_LinearBrightContrastAdjust(byte* srcData, int width, int height, int stride, int brightness, int contrast, int threshold);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_AutoContrastAdjust(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_AutoColorGradationAdjust(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ChannelMixProcess(byte* srcData, int width, int height, int stride, int channel, int kr, int kg, int kb, int N, bool singleColor, bool constAdjust);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Fragment(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_SurfaceBlur(byte* srcData, int width, int height, int stride, int threshold, int radius);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_RadialBlur(byte* srcData, int width, int height, int stride, int cenX, int cenY, int amount);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ZoomBlur(byte* srcData, int width, int height, int stride, int cenX, int cenY, int sampleRadius, int amount);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Relief(byte* srcData, int width, int height, int stride, int angle, int amount);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Mosaic(byte* srcData, int width, int height, int stride, int size);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ColorBalance(byte* srcData, int width, int height, int stride, int cyan, int magenta, int yellow, int channel, bool preserveLuminosity);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Diffusion(byte* srcData, int width, int height, int stride, int intensity);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_FastGaussFilter(byte* srcData, int width, int height, int stride, byte* dstData, float radius);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_HighPass(byte* srcData, int width, int height, int stride, byte* dstData, float mRadius);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_USM(byte* srcData, int width, int height, int stride, byte* dstData, float radius, int amount, int threshold);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_FindEdges(byte* srcData, int width, int height, int stride, byte* dstData);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ShadowAdjust(byte* srcData, int width, int height, int stride, int intensity, int ratio);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_HighlightAdjust(byte* srcData, int width, int height, int stride, int intensity, int ratio);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ExposureAdjust(byte* srcData, int width, int height, int stride, int intensity);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ColorTemperatureAdjust(byte* srcData, int width, int height, int stride, int intensity);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_CalcWH(int[] inputImgSize, float angle, float scale, int transform_method, int[] outputImgSize, float[] H);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ImageTransformation(byte* pSrc, int[] srcImgSize, byte* pDst, int[] dstImgSize, float[] H, int Interpolation_method, int transform_method);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_MotionBlur(byte* srcData, int width, int height, int stride, int angle, int distance);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Mean(byte* srcData, int width, int height, int stride);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_FastMeanFilter(byte* srcData, int width, int height ,int stride, byte* dstData,int radius);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ColorLevelAdjust(byte* srcData, int width, int height, int stride, int destChannel, byte inputLeftLimit, float inputMiddle, byte inputRightLimit, byte outputLeftLimit, byte outputRightLimit);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_Blackwhite(byte* srcData, int width, int height, int stride, int kRed, int kGreen, int kBlue, int kYellow, int kCyan, int kMagenta);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_GammaCorrect(byte* srcData, int width, int height, int stride, int intensity);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ImageBlendEffect(byte* baseData, int width, int height, int stride, byte* mixData, int blendMode);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ZPHOTO_ModeLinearLight(int basePixel, int mixPixel);
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        #endregion

        #region 颜色空间转换模块
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToYUV(int Red, int Green, int Blue, ref int Y, ref int U, ref int V);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_YUVToRGB(int Y, int U, int V, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToYCbCr(int R, int G, int B, ref int Y, ref int Cb, ref int Cr);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_YCbCrToRGB(int Y, int Cb, int Cr, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToXYZ(int Red, int Green, int Blue, ref int X, ref int Y, ref int Z);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_XYZToRGB(int X, int Y, int Z, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToHSL(int Red, int Green, int Blue, ref int h, ref int s, ref int l);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_HSLToRGB(int h, int s, int l, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]///////////////
        private static extern void ZPHOTO_RGBToHSV(int Red, int Green, int Blue, ref double h, ref double s, ref double v);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_HSVToRGB(double h, double s, double v, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToYIQ(int Red, int Green, int Blue, ref double Y, ref double I, ref double Q);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_YIQToRGB(double Y, double I, double Q, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToYDbDr(int Red, int Green, int Blue, ref int Y, ref int Db, ref int Dr);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_YDbDrToRGB(int Y, int Db, int Dr, ref int Red, ref int Green, ref int Blue);

        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_RGBToCMYK(int Red, int Green, int Blue, ref int C, ref int M, ref int Y, ref int K);
        [DllImport("ZPhotoEngine.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void ZPHOTO_CMYKToRGB(int C, int M, int Y, int K, ref int Red, ref int Green, ref int Blue);
        #endregion
        public Bitmap GammaCorrectProcess(Bitmap src, int intensity)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_GammaCorrect((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, intensity);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap ImageBlendEffectProcess(Bitmap baseBmp,Bitmap mixBmp, int blendEffect)
        {
            Bitmap dst = new Bitmap(baseBmp);
            //Bitmap mix = new Bitmap(mixBmp);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            BitmapData mixData = mixBmp.LockBits(new Rectangle(0, 0, mixBmp.Width, mixBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            ZPHOTO_ImageBlendEffect((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, (byte*)mixData.Scan0, blendEffect);
            dst.UnlockBits(srcData);
            return dst;
        }
        public int ModeLinearLight(int basePixel, int mixPixel)
        {
            return ZPHOTO_ModeLinearLight(basePixel, mixPixel);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap ChannelMixProcess(Bitmap src, int channel, int kr, int kg, int kb, int N, bool singleColor, bool constAdjust)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_ChannelMixProcess((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, channel, kr, kg, kb, N, singleColor, constAdjust);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap MeanProcess(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Mean((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap AutoColorGradationAdjust(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_AutoColorGradationAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap AutoContrastAdjust(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_AutoContrastAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap HistagramEqualize(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_HistagramEqualize((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap MotionBlur(Bitmap src, int angle, int distance)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_MotionBlur((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, angle, distance);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap TransformRotation(Bitmap src, float angle, int transform_method, int Interpolation_method)
        {

            BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int[] srcImgSize = new int[3] { src.Width, src.Height, srcData.Stride };
            int[] dstImgSize = new int[3];
            float[] H = new float[6];

            ZPHOTO_CalcWH(srcImgSize, angle, 1.0f, transform_method, dstImgSize, H);

            Bitmap dst = new Bitmap(dstImgSize[0], dstImgSize[1]);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            dstImgSize[2] = dstData.Stride;
            ZPHOTO_ImageTransformation((byte*)srcData.Scan0, srcImgSize, (byte*)dstData.Scan0, dstImgSize, H, Interpolation_method, transform_method);

            src.UnlockBits(srcData);
            dst.UnlockBits(dstData);

            return dst;
        }

        public Bitmap TransformScale(Bitmap src, float scale, int transform_method, int Interpolation_method)
        {

            BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int[] srcImgSize = new int[3] { src.Width, src.Height, srcData.Stride };
            int[] dstImgSize = new int[3];
            float[] H = new float[6];

            ZPHOTO_CalcWH(srcImgSize, 0, scale, transform_method, dstImgSize, H);

            Bitmap dst = new Bitmap(dstImgSize[0], dstImgSize[1]);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            dstImgSize[2] = dstData.Stride;
            ZPHOTO_ImageTransformation((byte*)srcData.Scan0, srcImgSize, (byte*)dstData.Scan0, dstImgSize, H, Interpolation_method, transform_method);

            src.UnlockBits(srcData);
            dst.UnlockBits(dstData);

            return dst;
        }

        public Bitmap TransformRotationScale(Bitmap src, float angle, float scale, int transform_method, int Interpolation_method)
        {

            BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int[] srcImgSize = new int[3] { src.Width, src.Height, srcData.Stride };
            int[] dstImgSize = new int[3];
            float[] H = new float[6];

            ZPHOTO_CalcWH(srcImgSize, angle, scale, transform_method, dstImgSize, H);

            Bitmap dst = new Bitmap(dstImgSize[0], dstImgSize[1]);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            dstImgSize[2] = dstData.Stride;
            ZPHOTO_ImageTransformation((byte*)srcData.Scan0, srcImgSize, (byte*)dstData.Scan0, dstImgSize, H, Interpolation_method, transform_method);

            src.UnlockBits(srcData);
            dst.UnlockBits(dstData);

            return dst;
        }

        public Bitmap TransformAffine(Bitmap src, float[] H, int transform_method, int Interpolation_method)
        {

            BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int[] srcImgSize = new int[3] { src.Width, src.Height, srcData.Stride };
            int[] dstImgSize = new int[3];
            //float[] H = new float[6];

            ZPHOTO_CalcWH(srcImgSize, 0, 1.0f, transform_method, dstImgSize, H);

            Bitmap dst = new Bitmap(dstImgSize[0], dstImgSize[1]);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            dstImgSize[2] = dstData.Stride;
            ZPHOTO_ImageTransformation((byte*)srcData.Scan0, srcImgSize, (byte*)dstData.Scan0, dstImgSize, H, Interpolation_method, transform_method);

            src.UnlockBits(srcData);
            dst.UnlockBits(dstData);

            return dst;
        }
        public Bitmap Threshold(Bitmap src, int threshold)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Threshold((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, threshold);
            dst.UnlockBits(srcData);
            return dst;
        }

        public Bitmap TransformMirror(Bitmap srcBitmap, int transform_method)
        {
            Bitmap src = new Bitmap(srcBitmap);
            BitmapData srcData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int[] srcImgSize = new int[3] { src.Width, src.Height, srcData.Stride };
            int[] dstImgSize = new int[3];
            float[] H = new float[6];

            ZPHOTO_CalcWH(srcImgSize, 0, 1.0f, transform_method, dstImgSize, H);

            Bitmap dst = new Bitmap(dstImgSize[0], dstImgSize[1]);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            dstImgSize[2] = dstData.Stride;

            int Interpolation_method = 0;
            ZPHOTO_ImageTransformation((byte*)srcData.Scan0, srcImgSize, (byte*)dstData.Scan0, dstImgSize, H, Interpolation_method, transform_method);

            src.UnlockBits(srcData);
            dst.UnlockBits(dstData);

            return dst;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap Fragment(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Fragment((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap HueSaturationAdjust(Bitmap src, int hue, int saturation)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_HueAndSaturationAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, hue, saturation);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap ColorTemperatureProcess(Bitmap src, int intensity)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_ColorTemperatureAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, intensity);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap Posterize(Bitmap src, int clusterNum)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Posterize((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, clusterNum);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap Desaturate(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Desaturate((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap OverExposure(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_OverExposure((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap ExposureAdjust(Bitmap src, int intensity)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_ExposureAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, intensity);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap LightnessAdjustProcess(Bitmap src, int lightness)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_LightnessAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, lightness);
            dst.UnlockBits(srcData);
            return dst;
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public Bitmap ShadowAdjust(Bitmap src, int intensity, int ratio)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_ShadowAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, intensity, ratio);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap HighlightAdjust(Bitmap src, int intensity, int ratio)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_HighlightAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, intensity, ratio);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap Invert(Bitmap src)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Invert((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap SurfaceBlur(Bitmap src, int threshold, int radius)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_SurfaceBlur((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, threshold, radius);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap NLinearBrightContrastAdjust(Bitmap src, int bright, int contrast, int threshold)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_NLinearBrightContrastAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, bright, contrast, threshold);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap LinearBrightContrastAdjust(Bitmap src, int bright, int contrast, int threshold)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_LinearBrightContrastAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, bright, contrast, threshold);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap FindEdgesProcess(Bitmap src)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap dst = new Bitmap(src);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_FindEdges((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, (byte*)dstData.Scan0);
            a.UnlockBits(srcData);
            dst.UnlockBits(dstData);
            return dst;
        }
        public Bitmap GaussFilterProcess(Bitmap src, float radius)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap dst = new Bitmap(src);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_FastGaussFilter((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, (byte*)dstData.Scan0, radius);
            a.UnlockBits(srcData);
            dst.UnlockBits(dstData);
            return dst;
        }
        public Bitmap MeanFilterProcess(Bitmap src, int radius)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap dst = new Bitmap(src);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_FastMeanFilter((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, (byte*)dstData.Scan0, radius);
            a.UnlockBits(srcData);
            dst.UnlockBits(dstData);
            return dst;
        }
        public Bitmap HighPassProcess(Bitmap src, float radius)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap dst = new Bitmap(src);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_HighPass((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, (byte*)dstData.Scan0, radius);
            a.UnlockBits(srcData);
            dst.UnlockBits(dstData);
            return dst;
        }
        public Bitmap USMProcess(Bitmap src, float radius, int amount, int threshold)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Bitmap dst = new Bitmap(src);
            BitmapData dstData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_USM((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, (byte*)dstData.Scan0, radius, amount, threshold);
            a.UnlockBits(srcData);
            dst.UnlockBits(dstData);
            return dst;
        }

        public Bitmap SaturationProcess(Bitmap src, int hue)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_SaturationAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, hue);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap ColorBalanceProcess(Bitmap src, int cyan, int magenta, int yellow, int channel, bool preserveLuminosity)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_ColorBalance((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, cyan, magenta, yellow, channel, preserveLuminosity);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap Relief(Bitmap src, int angle, int amount)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Relief((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, angle, amount);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap DiffusionProcess(Bitmap src, int intensity)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Diffusion((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, intensity);
            a.UnlockBits(srcData);
            return a;
        }
        public Bitmap MosaicProcess(Bitmap src, int size)
        {
            Bitmap a = new Bitmap(src);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Mosaic((byte*)srcData.Scan0, a.Width, a.Height, srcData.Stride, size);
            a.UnlockBits(srcData);
            return a;
        }
        public Bitmap RadialBlurProcess(Bitmap src, int amount)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_RadialBlur((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, dst.Width / 2, dst.Height / 2, amount);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap ZoomBlurProcess(Bitmap src, int radius, int amount)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_ZoomBlur((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, dst.Width / 2, dst.Height / 2, radius, amount);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap ColorLevelProcess(Bitmap src, int DestChannel, int InputLeftLimit, float InputMiddle, int InputRightLimit, int OutputLeftLimit, int OutputRightLimit)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            //f_TCurveAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, DestChannel, InputLeftLimit, InputMiddle, InputRightLimit, OutputLeftLimit, OutputRightLimit);
            ZPHOTO_ColorLevelAdjust((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, DestChannel, (byte)InputLeftLimit, InputMiddle, (byte)InputRightLimit, (byte)OutputLeftLimit, (byte)OutputRightLimit);
            dst.UnlockBits(srcData);
            return dst;
        }
        public Bitmap BlackwhiteProcess(Bitmap src, int kRed, int kGreen, int kBlue, int kYellow, int kCyan, int kMagenta)
        {
            Bitmap dst = new Bitmap(src);
            BitmapData srcData = dst.LockBits(new Rectangle(0, 0, dst.Width, dst.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            ZPHOTO_Blackwhite((byte*)srcData.Scan0, dst.Width, dst.Height, srcData.Stride, kRed, kGreen, kBlue, kYellow, kCyan, kMagenta);
            dst.UnlockBits(srcData);
            return dst;
        }
    }
}

4,有了上述步骤后,我们就完成了一个测试DEMO中,添加ZPhotoEngine.dll的过程,下面我们就可以使用了,其实主要就是两步:添加DLL库+ZPhotoEngineDll.cs。

5,下面我们按照上一篇博文中PS的实现步骤,写出代码如下:

        ZPhotoEngineDll zp = new ZPhotoEngineDll();
        int BLEND_MODE_OVERRLAY = 11;
        public Bitmap LegePixelpuzzleFilter(Bitmap src,Bitmap legemap)
        {
            //马赛克处理,参数20
            Bitmap mosic = zp.MosaicProcess(src,20);
            //色调分离,参数4
            Bitmap porize = zp.Posterize(mosic, 4);
            //图层混合模式-叠加
            Bitmap overlay = zp.ImageBlendEffectProcess(mosic, porize, BLEND_MODE_OVERRLAY);
            mosic.Dispose();
            porize.Dispose();
            Bitmap a = new Bitmap(overlay);
            int w = a.Width;
            int h = a.Height;
            Bitmap map = new Bitmap(legemap);
            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            BitmapData mapData = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte* p = (byte*)srcData.Scan0;
            byte* mapP = (byte*)mapData.Scan0;
            int r = 0, g = 0, b = 0, offset = srcData.Stride - w * 4;
            int temp = 0;
            for (int j = 0; j < h; j++)
            {
                for (int i = 0; i < w; i++)
                {
                    b = p[0];
                    g = p[1];
                    r = p[2];
                    ////////////////Process image...
                    //与模板进行线性光图层混合
                    temp = mapP[(i % map.Width) * 4 + (j % map.Height) * mapData.Stride];
                    p[0] = (byte)zp.ModeLinearLight(b, temp);
                    temp = mapP[(i % map.Width) * 4 + (j % map.Height) * mapData.Stride + 1];
                    p[1] = (byte)zp.ModeLinearLight(g, temp);
                    temp = mapP[(i % map.Width) * 4 + (j % map.Height) * mapData.Stride + 2];
                    p[2] = (byte)zp.ModeLinearLight(r, temp);
                    p[3] = (byte)255;
                    p += 4;
                }
                p += offset;
            }
            a.UnlockBits(srcData);
            return a;
        }

大家可以看到,就上面这一点代码,就轻松实现了乐高像素拼图特效的滤镜,最后放上效果图对比:

图像滤镜艺术---乐高像素拼图特效滤镜的代码实现_第1张图片

原图

图像滤镜艺术---乐高像素拼图特效滤镜的代码实现_第2张图片

PS实现效果图

图像滤镜艺术---乐高像素拼图特效滤镜的代码实现_第3张图片

程序实现效果图

大家可以对比一下,几乎是一模一样的呵呵,以上就是整个过程了,代码我写的都是主要代码,这里给出整个工程的源代码免费下载地址:点击打开链接

最后在给出ZPhotoEngine库的下载地址:点击打开链接



你可能感兴趣的:(代码实现,photoshop,滤镜,乐高像素拼图)