這篇博客承接上一篇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的實現步驟,寫出代碼如下:
大家可以看到,就上面這一點代碼,就輕松實現了樂高像素拼圖特效的濾鏡,最后放上效果圖對比:

原圖

PS實現效果圖

程序實現效果圖
大家可以對比一下,幾乎是一模一樣的呵呵,以上就是整個過程了,代碼我寫的都是主要代碼,這里給出整個工程的源代碼免費下載地址:[點擊打開鏈接](http://download.csdn.net/detail/trent1985/9336019)
最后在給出ZPhotoEngine庫的下載地址:[點擊打開鏈接](http://www.zealpixel.com/portal.php?mod=view&aid=45)
- 前言
- 序言
- 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圖層混合模式之明度模式