|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections; |
| 3 | + |
| 4 | +// This Delegate is used to find best suitable color from limited set of colors |
| 5 | +public delegate Color32 FindColor(Color32 original); |
| 6 | + |
| 7 | +public abstract class DitheringBase { |
| 8 | + |
| 9 | + protected int width; |
| 10 | + protected int height; |
| 11 | + |
| 12 | + protected FindColor colorFunction = null; |
| 13 | + |
| 14 | + protected string methodLongName = ""; |
| 15 | + protected string fileNameAddition = ""; |
| 16 | + |
| 17 | + private Color32[] pixels = null; |
| 18 | + |
| 19 | + public DitheringBase(FindColor colorfunc) |
| 20 | + { |
| 21 | + this.colorFunction = colorfunc; |
| 22 | + } |
| 23 | + |
| 24 | + // |
| 25 | + public Texture2D DoDithering(Texture2D input) |
| 26 | + { |
| 27 | + this.width = input.width; |
| 28 | + this.height = input.height; |
| 29 | + |
| 30 | + // Copy all pixels of input Texture2D to new array, which we are going to edit |
| 31 | + this.pixels = input.GetPixels32(); |
| 32 | + |
| 33 | + Color32 originalPixel = Color.white; // Default value isn't used |
| 34 | + Color32 newPixel = Color.white; // Default value isn't used |
| 35 | + short[] quantError = null; // Default values aren't used |
| 36 | + |
| 37 | + for (int y = 0; y < this.height; y++) |
| 38 | + { |
| 39 | + for (int x = 0; x < this.width; x++) |
| 40 | + { |
| 41 | + originalPixel = this.pixels[GetIndexWith(x, y)]; |
| 42 | + newPixel = this.colorFunction(originalPixel); |
| 43 | + |
| 44 | + this.pixels[GetIndexWith(x, y)] = newPixel; |
| 45 | + |
| 46 | + quantError = GetQuantError(originalPixel, newPixel); |
| 47 | + this.PushError(x, y, quantError); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Create the texture we are going to return from pixels array |
| 52 | + Texture2D returnTexture = new Texture2D(width, height, TextureFormat.ARGB32, false); |
| 53 | + returnTexture.SetPixels32(this.pixels); |
| 54 | + returnTexture.Apply(); |
| 55 | + |
| 56 | + return returnTexture; |
| 57 | + } |
| 58 | + |
| 59 | + // Implement this for every dithering method |
| 60 | + protected abstract void PushError(int x, int y, short[] quantError); |
| 61 | + |
| 62 | + protected bool IsValidCoordinate(int x, int y) |
| 63 | + { |
| 64 | + return (0 <= x && x < this.width && 0 <= y && y < this.height); |
| 65 | + } |
| 66 | + |
| 67 | + protected int GetIndexWith(int x, int y) |
| 68 | + { |
| 69 | + return y * this.width + x; |
| 70 | + } |
| 71 | + |
| 72 | + public string GetMethodName() |
| 73 | + { |
| 74 | + return this.methodLongName; |
| 75 | + } |
| 76 | + |
| 77 | + public string GetFilenameAddition() |
| 78 | + { |
| 79 | + return this.fileNameAddition; |
| 80 | + } |
| 81 | + |
| 82 | + protected short[] GetQuantError(Color32 originalPixel, Color32 newPixel) |
| 83 | + { |
| 84 | + short[] returnValue = new short[4]; |
| 85 | + |
| 86 | + returnValue [0] = (short)(originalPixel.r - newPixel.r); |
| 87 | + returnValue [1] = (short)(originalPixel.g - newPixel.g); |
| 88 | + returnValue [2] = (short)(originalPixel.b - newPixel.b); |
| 89 | + returnValue [3] = (short)(originalPixel.a - newPixel.a); |
| 90 | + |
| 91 | + return returnValue; |
| 92 | + } |
| 93 | + |
| 94 | + public void ModifyImageWithErrorAndMultiplier(int x, int y, short[] quantError, float multiplier) |
| 95 | + { |
| 96 | + Color32 oldColor = this.pixels[GetIndexWith(x, y)]; |
| 97 | + |
| 98 | + // We limit the color here because we don't want the value go over 255 or under 0 |
| 99 | + Color32 newColor = new Color32( |
| 100 | + GetLimitedValue(oldColor.r, Mathf.RoundToInt(quantError[0] * multiplier)), |
| 101 | + GetLimitedValue(oldColor.g, Mathf.RoundToInt(quantError[1] * multiplier)), |
| 102 | + GetLimitedValue(oldColor.b, Mathf.RoundToInt(quantError[2] * multiplier)), |
| 103 | + GetLimitedValue(oldColor.a, Mathf.RoundToInt(quantError[3] * multiplier))); |
| 104 | + |
| 105 | + this.pixels[GetIndexWith(x, y)] = newColor; |
| 106 | + } |
| 107 | + |
| 108 | + private static byte GetLimitedValue(byte original, int error) |
| 109 | + { |
| 110 | + int newValue = original + error; |
| 111 | + return (byte)Mathf.Clamp(newValue, byte.MinValue, byte.MaxValue); |
| 112 | + } |
| 113 | +} |
0 commit comments