Http://www.cnblogs.com/EndOfYear/p/4334952.html
First on the code:
Using System.Threading;
Using Unityengine;
Using System.IO;
Using System.Collections;
public class Textureutility
{
public class Threaddata
{
public int start;
public int end;
Public Threaddata (int s, int e) {
start = s;
end = e;
}
}
private static color[] texcolors;
private static color[] newcolors;
private static int w;
private static float Ratiox;
private static float Ratioy;
private static int W2;
private static int finishcount;
private static mutex mutex;
public static void Scalepoint (texture2d tex, int newwidth, int newheight)
{
Threadedscale (Tex, Newwidth, Newheight, false);
}
public static void Scalebilinear (texture2d tex, int newwidth, int newheight)
{
Threadedscale (Tex, Newwidth, Newheight, true);
}
private static void Threadedscale (texture2d tex, int newwidth, int newheight, BOOL usebilinear)
{
Texcolors = Tex. Getpixels ();
newcolors = new color[newwidth * Newheight];
if (usebilinear)
{
Ratiox = 1.0f/((float) newwidth/(tex.width-1));
Ratioy = 1.0f/((float) newheight/(tex.height-1));
}
else {
Ratiox = ((float) tex.width)/newwidth;
Ratioy = ((float) tex.height)/newheight;
}
W = tex.width;
W2 = Newwidth;
var cores = Mathf.min (Systeminfo.processorcount, newheight);
var slice = newheight/cores;
Finishcount = 0;
if (mutex = = null) {
Mutex = new Mutex (false);
}
if (Cores > 1)
{
int i = 0;
Threaddata Threaddata;
for (i = 0; i < cores-1; i++) {
Threaddata = new Threaddata (slice * I, slice * (i + 1));
Parameterizedthreadstart ts = usebilinear? New Parameterizedthreadstart (Bilinearscale): New Parameterizedthreadstart (Pointscale);
Thread thread = new thread (TS);
Thread. Start (Threaddata);
}
Threaddata = new Threaddata (slice*i, newheight);
if (usebilinear)
{
Bilinearscale (Threaddata);
}
Else
{
Pointscale (Threaddata);
}
while (Finishcount < cores)
{
Thread.Sleep (1);
}
}
Else
{
Threaddata threaddata = new Threaddata (0, newheight);
if (usebilinear)
{
Bilinearscale (Threaddata);
}
Else
{
Pointscale (Threaddata);
}
}
Tex. Resize (Newwidth, newheight);
Tex. SetPixels (newcolors);
Tex. Apply ();
}
public static void Bilinearscale (System.Object obj)
{
Threaddata Threaddata = (threaddata) obj;
for (var y = Threaddata.start; y < threaddata.end; y++)
{
int yfloor = (int) mathf.floor (y * ratioy);
var y1 = Yfloor * W;
var y2 = (yfloor+1) * W;
var yw = y * W2;
for (var x = 0; x < W2; × x + +) {
int xfloor = (int) mathf.floor (x * ratiox);
var xlerp = x * Ratiox-xfloor;
Newcolors[yw + x] = colorlerpunclamped (colorlerpunclamped (texcolors[y1 + Xfloor], texcolors[y1 + xFloor+1], xLerp),
colorlerpunclamped (Texcolors[y2 + Xfloor], Texcolors[y2 + xfloor+1], xlerp),
Y*ratioy-yfloor);
}
}
Mutex. WaitOne ();
finishcount++;
Mutex. ReleaseMutex ();
}
public static void Pointscale (System.Object obj)
{
Threaddata Threaddata = (threaddata) obj;
for (var y = Threaddata.start; y < threaddata.end; y++)
{
var thisy = (int) (Ratioy * y) * W;
var yw = y * W2;
for (var x = 0; x < W2; × x + +) {
Newcolors[yw + x] = texcolors[(int) (Thisy + ratiox*x)];
}
}
Mutex. WaitOne ();
finishcount++;
Mutex. ReleaseMutex ();
}
private static Color colorlerpunclamped (color c1, color c2, float value)
{
return new Color (C1.R + (C2.R-C1.R) *value,
C1.G + (C2.G-C1.G) *value,
c1.b + (c2.b-c1.b) *value,
c1.a + (c2.a-c1.a) *value);
}
public static texture2d Loadtexture (string filePath) {
Texture2d tex = null;
Byte[] FileData;
if (file.exists (FilePath)) {
FileData = File.readallbytes (FilePath);
Tex = new Texture2d (2, 2,textureformat.argb32,false);
Tex. LoadImage (FileData);
}
Return Tex;
}
Save screenshot
public static void Savescreenshotasync (string path, Vector2 size)
{
SWMain.sharedSWMain.StartCoroutine (Textureutility.savescreenshot (path, size));
}
public static void Savescreenshotasync (string path, rect rect, Vector2 size)
{
SWMain.sharedSWMain.StartCoroutine (Textureutility.savescreenshot (path, rect, size));
}
public static IEnumerator Savescreenshot (string path, Vector2 size)
{
Rect rect = new Rect (0, 0, screen.width, screen.height);
Yield return SWMain.sharedSWMain.StartCoroutine (Textureutility.savescreenshot (path,rect, size));
}
public static IEnumerator Savescreenshot (string path, rect rect, Vector2 size = default (Vector2))
{
We should only read the Bufferafter rendering are complete
Yield return new waitforendofframe ();
To save the size of the picture
Texture2d tex = new texture2d ((int) rect.width, (int) rect.height, textureformat.rgb24, false);
Intercepted area
Tex. Readpixels (rect, 0, 0);
Tex. Apply ();
Convert picture data to byte array
if (Size! = Default (Vector2))
{
Scalebilinear (Tex, (int) size.x, (int) size.y);
}
byte[] buffer = Tex. Encodetojpg (100);
Object.destroy (Tex);
Then save as Picture
File.writeallbytes (path, buffer);
}
public static texture2d Copy (Texture2d Tex)
{
return new Texture2d (Tex.width, Tex.height, Tex.format, false);
}
<summary>
Applies sepia effect to the texture.
</summary>
<param name= "Tex" > Texture to Process.</param>
public static texture2d Setsepia (Texture2d Tex)
{
Texture2d t = Copy (Tex);
color[] Colors = Tex. Getpixels ();
for (int i = 0; i < colors. Length; i++)
{
float alpha = colors[i].a;
float grayscale = ((COLORS[I].R *. 299f) + (COLORS[I].G *. 587f) + (COLORS[I].B *. 114f));
Color c = new color (grayscale, grayscale, grayscale);
Colors[i] = new Color (C.R * 1, C.G * 0.95f, C.B * 0.82f, Alpha);
}
T.setpixels (colors);
T.apply ();
return t;
}
<summary>
Applies grayscale effect to the texture and changes colors to grayscale.
</summary>
<param name= "Tex" > Texture to Process.</param>
public static texture2d Setgrayscale (Texture2d Tex)
{
Texture2d t = Copy (Tex);
color[] Colors = Tex. Getpixels ();
for (int i = 0; i < colors. Length; i++)
{
float val = (colors [I].R + colors [i].g + colors [i].b]/3;
colors [i] = new Color (Val, Val, Val);
}
T.setpixels (colors);
T.apply ();
return t;
}
<summary>
Pixelates the texture.
</summary>
<param name= "Tex" > Texture to Process.</param>
<param name= "Size" > Size of the pixel.</param>
public static texture2d Setpixelate (texture2d tex, int size)
{
Texture2d t = Copy (Tex);
Rect rectangle = new Rect (0, 0, tex.width, tex.height);
for (int xx = (int) rectangle.x; xx < rectangle.x + rectangle.width && xx < tex.width; xx + = size)
{
for (int yy = (int) rectangle.y; yy < Rectangle.y + rectangle.height && yy < tex.height; yy + = size)
{
int OffsetX = SIZE/2;
int OffsetY = SIZE/2;
while (xx + OffsetX >= tex.width) offsetx--;
while (yy + OffsetY >= tex.height) offsety--;
Color pixel = Tex. GetPixel (xx + OffsetX, yy + OffsetY);
for (int x = XX; x < xx + size && x < tex.width;
for (int y = yy; y < yy + size && y < tex.height; y++)
T.setpixel (x, y, pixel);
}
}
T.apply ();
return t;
}
<summary>
Inverts colors of the texture.
</summary>
<param name= "Tex" > Texture to Process.</param>
public static texture2d setnegative (Texture2d Tex)
{
Texture2d t = Copy (Tex);
color[] Colors = Tex. Getpixels ();
Color Pixel;
for (int i = 0; i < colors. Length; i++)
{
pixel = Colors[i];
Colors[i] = new Color (1-PIXEL.R, 1-PIXEL.G, 1-pixel.b);
}
T.setpixels (colors);
T.apply ();
return t;
}
<summary>
Sets the foggy effect. Atomization effect
</summary>
<returns>texture processed.</returns>
<param name= "Tex" >texture to Process.</param>
public static texture2d Setfoggy (Texture2d Tex)
{
Texture2d t = Copy (Tex);
Color Pixel;
for (int x = 1; x < tex.width-1; × x + +)
for (int y = 1; y < tex.height-1; y++)
{
int k = UnityEngine.Random.Range (0, 123456);
Pixel block Size
int dx = x + k% 19;
int dy = y + k% 19;
if (DX >= tex.width)
DX = tex.width-1;
if (dy >= tex.height)
dy = tex.height-1;
Pixel = Tex. GetPixel (dx, dy);
T.setpixel (x, y, pixel);
}
T.apply ();
return t;
}
<summary>
Sets the soft effect. Softening effect
</summary>
<returns>texture processed.</returns>
<param name= "Tex" >texture to Process.</param>
public static texture2d Setsoft (Texture2d Tex)
{
Texture2d t = Copy (Tex);
Int[] Gauss = {1, 2, 1, 2, 4, 2, 1, 2, 1};
for (int x = 1; x < tex.width-1; × x + +)
for (int y = 1; y < tex.height-1; y++)
{
float r = 0, g = 0, b = 0;
int Index = 0;
for (int col =-1; Col <= 1; col++)
for (int row =-1; row <= 1; row++)
{
Color pixel = Tex. GetPixel (x + row, y + col);
R + = PIXEL.R * Gauss[index];
G + = PIXEL.G * Gauss[index];
b + = pixel.b * Gauss[index];
index++;
}
R/= 16;
G/= 16;
b/= 16;
Handling Color Value Overflow
R = r > 1? 1:r;
R = R < 0? 0:r;
g = g > 1? 1:g;
g = G < 0? 0:g;
b = b > 1? 1:b;
B = B < 0? 0:b;
T.setpixel (X-1, Y-1, New Color (R, G, b));
}
T.apply ();
return t;
}
<summary>
Sets the sharp effect of sharpening
</summary>
<returns>the sharp.</returns>
<param name= "Tex" >Tex.</param>
public static texture2d Setsharp (Texture2d Tex)
{
Texture2d t = Copy (Tex);
Color Pixel;
Laplace templates
Int[] Laplacian ={-1,-1,-1,-1, 9,-1,-1,-1,-1};
for (int x = 1; x < tex.width-1; × x + +)
for (int y = 1; y < tex.height-1; y++)
{
float r = 0, g = 0, b = 0;
int index = 0;
for (int col =-1; Col <= 1; col++)
for (int row =-1; row <= 1; row++)
{
Pixel = Tex. GetPixel (x + row, y + col); R + = PIXEL.R * Laplacian[index];
G + = PIXEL.G * Laplacian[index];
b + = pixel.b * Laplacian[index];
index++;
}
Handling Color Value Overflow
R = r > 1? 1:r;
R = R < 0? 0:r;
g = g > 1? 1:g;
g = G < 0? 0:g;
b = b > 1? 1:b;
B = B < 0? 0:b;
T.setpixel (X-1, Y-1, New Color (R, G, b));
}
T.apply ();
return t;
}
<summary>
Sets the relief. Emboss Effect
</summary>
<returns>the relief.</returns>
<param name= "Tex" >Tex.</param>
public static texture2d Setrelief (Texture2d Tex)
{
Texture2d t = Copy (Tex);
for (int x = 0; x < tex.width-1; × x + +)
{
for (int y = 0; y < tex.height-1; y++)
{
float r = 0, g = 0, b = 0;
Color Pixel1 = Tex. GetPixel (x, y);
Color Pixel2 = Tex. GetPixel (x + 1, y + 1);
R = Mathf.abs (PIXEL1.R-PIXEL2.R + 0.5f);
g = mathf.abs (pixel1.g-pixel2.g + 0.5f);
b = mathf.abs (pixel1.b-pixel2.b + 0.5f);
if (R > 1)
R = 1;
if (R < 0)
R = 0;
if (g > 1)
g = 1;
if (g < 0)
g = 0;
if (b > 1)
b = 1;
if (b < 0)
b = 0;
T.setpixel (x, Y, new Color (R, G, b));
}
}
T.apply ();
return t;
}
}
How to Invoke:
1. Compress the Image:
Textureutility.scalepoint (texture2d Tex, int newwidth, int newheight), the first parameter is the texture2d to be passed in, the second parameter is the width of the new picture, The third parameter is the height of the new picture. You can also call Textureutility.scalebilinear (texture2d Tex, int newwidth, int newheight).
2. Screenshot:
Yield return Startcoroutine (textureutility.savescreenshot (string path, Vector2 size, Vector2 size = default (Vector2)))
or call the Async method Textureutility.savescreenshotasync (string path, rect rect, Vector2 size)
3. Image filter:
such as gray-scale filter: Textureutility.setgrayscale (texture2d Tex);
Film Filter: Textureutility.setnegative (texture2d Tex) and so on.
Unity picture processing class, including compression, screenshots, and filters