public Bitmap[] ForB(Bitmap b, IMessage m, Pipe thresholder, Pipe col, string varName, float startValue, float endValue, int steps) { if (steps > 150) { throw new Exception("Too many steps >:("); } float mul = (endValue - startValue) / steps; int i = 0; return(Enumerable.Repeat(b, steps). Select(a => { using (UnsafeBitmapContext con = new UnsafeBitmapContext(b)) for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { Color c = con.GetPixel(x, y); c.ColorToHSV(out double h, out double s, out double v); if ((bool)thresholder.Apply(m, null, new Dictionary <string, object>() { { "h", h }, { "s", s * 100 }, { "v", v * 100 } })) { con.SetPixel(x, y, (Color)col.Apply(m, c, new Dictionary <string, object>() { { "i", i++ } })); } } } return a; }).ToArray()); }
public Bitmap Mandelbrot(EditNull n, IMessage m, double zoom = 1, Vector2 camera = new Vector2(), int passes = 40) { Bitmap bmp = new Bitmap(500, 500); if (passes > 200) { throw new Exception("200 passes should be enough, everything else would be spam and you dont wanna spam"); } zoom = Math.Pow(2, -zoom); using (UnsafeBitmapContext con = new UnsafeBitmapContext(bmp)) for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { double cre = (2 * ((x / (double)bmp.Width) - 0.5)) * zoom - camera.X; double cim = (2 * ((y / (double)bmp.Height) - 0.5)) * zoom - camera.Y; double x2 = 0, y2 = 0; int count = 0; for (int i = 0; i < passes; i++) { if (x2 * x2 + y2 * y2 < 1) { count++; } double xtemp = x2 * x2 - y2 * y2 + cre; y2 = 2 * x2 * y2 + cim; x2 = xtemp; } var float4 = HSVtoRGB((float)((Math.Atan2(y2, x2) + Math.PI) / (Math.PI * 2) * 360), 1, count * 10); con.SetPixel(x, y, Color.FromArgb((x2 + y2 < 1000 ? 255 : 0), (int)(float4.X * 255), (int)(float4.Y * 255), (int)(float4.Z * 255))); } } return(bmp); }
private void WriteImage32() { Logger.Trace("Writing image..."); using (UnsafeBitmapContext unsafeContext = _image.CreateUnsafeContext()) { for (int y = 0; y < _height; ++y) { byte[] buffer = new byte[_rowLengthInBytes]; BitWriter bitWriter = new BitWriter(new MemoryStream(buffer)); for (int x = 0; x < _width; ++x) { Color pixel = unsafeContext.GetPixel(x, y); pixel.ToArgb(); byte alpha = (byte)(byte.MaxValue - (uint)pixel.A); if (!Reader.IsVerge) { bitWriter.WriteBits(pixel.R, 8); bitWriter.WriteBits(pixel.G, 8); bitWriter.WriteBits(pixel.B, 8); bitWriter.WriteBits((byte)(0xff - alpha), 8); } else { bitWriter.WriteBits(pixel.B, 8); bitWriter.WriteBits(pixel.G, 8); bitWriter.WriteBits(pixel.R, 8); bitWriter.WriteBits((byte)(0xff - alpha), 8); } } bitWriter.Flush(); _writer.Write(buffer); } } }
public Gif Rainbow(Bitmap b, IMessage m) { Vector3[,] HSVimage = new Vector3[b.Width, b.Height]; int[,] Alphas = new int[b.Width, b.Height]; using (UnsafeBitmapContext c = ImageExtensions.CreateUnsafeContext(b)) for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { Color col = c.GetPixel(x, y); Alphas[x, y] = col.A; HSVimage[x, y] = new Vector3(col.GetHue(), col.GetSaturation(), col.GetValue()); } } int steps = 20; int stepWidth = 360 / steps; Bitmap[] re = new Bitmap[steps]; for (int i = 0; i < steps; i++) { re[i] = new Bitmap(b.Width, b.Height); using UnsafeBitmapContext c = ImageExtensions.CreateUnsafeContext(re[i]); for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { c.SetPixel(x, y, Color.FromArgb(Alphas[x, y], HSVimage[x, y].HsvToRgb())); HSVimage[x, y].X += stepWidth; while (HSVimage[x, y].X > 360) { HSVimage[x, y].X -= 360; } } } } return(new Gif(re, Enumerable.Repeat(33, re.Length).ToArray())); }