/// <summary> /// helper function for filling a float bitmap /// with a gradient from positions and colors /// colors and positions must be the same length /// as they should be 1 to 1 reference /// </summary> /// <param name="dst"></param> /// <param name="positions"></param> /// <param name="colors"></param> public static void CreateGradient(FloatBitmap dst, float[] positions, MVector[] colors) { if (positions.Length != colors.Length) { return; } //sort from least to greater Array.Sort(positions); List <float> pos = new List <float>(positions); List <MVector> cols = new List <MVector>(colors); if (positions[0] > 0) { pos.Insert(0, 0); MVector c = colors[0]; cols.Insert(0, c); } if (positions[positions.Length - 1] < 1) { pos.Add(1); MVector c = colors[colors.Length - 1]; cols.Add(c); } for (int i = 0; i < pos.Count - 1; i++) { float p1 = pos[i]; float p2 = pos[i + 1]; MVector c1 = cols[i]; MVector c2 = cols[i + 1]; ConvertToLAB(ref c1); ConvertToLAB(ref c2); int imin = (int)(p1 * dst.Width); int imax = (int)(p2 * dst.Width); for (int x = imin; x < imax; x++) { //minus 1 on imax otherwise we won't reach 1 for t float t = (float)(x - imin) / (float)(imax - 1 - imin); MVector n = MVector.Lerp(c1, c2, t); ConvertLABToRGB(ref n); for (int y = 0; y < dst.Height; y++) { dst.SetPixel(x, y, n.X, n.Y, n.Z, n.W); } } } }
static void Main(string[] args) { string fpath = "E:\\Test\\input.png"; string spath = "E:\\Test\\output.png"; using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fpath)) { FloatBitmap bitmap = FloatBitmap.FromBitmap(bmp); float[] f = new float[Math.Max(bitmap.Width, bitmap.Height)]; for (int x = 0; x < bitmap.Width; ++x) { for (int y = 0; y < bitmap.Height; ++y) { float r, g, b, a; bitmap.GetPixel(x, y, out r, out g, out b, out a); f[y] = r; } float[] d = dt(f, bitmap.Height); for (int y = 0; y < bitmap.Height; ++y) { float fx = d[y]; bitmap.SetPixel(x, y, fx, fx, fx, 1); } } for (int y = 0; y < bitmap.Height; ++y) { for (int x = 0; x < bitmap.Width; ++x) { float r, g, b, a; bitmap.GetPixel(x, y, out r, out g, out b, out a); f[x] = r; } float[] d = dt(f, bitmap.Width); for (int x = 0; x < bitmap.Width; ++x) { float fx = d[x]; bitmap.SetPixel(x, y, fx, fx, fx, 1); } } } }
public static void Fill(FloatBitmap dst, int xOffset, int yOffset, float r = 0, float g = 0, float b = 0, float a = 0) { Parallel.For(yOffset, dst.Height, y => { for (int x = xOffset; x < dst.Width; x++) { dst.SetPixel(x, y, r, g, b, a); } }); }