using System; using System.Linq; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using Chernobyl.Mathematics; using Chernobyl.Mathematics.Geometry; using Chernobyl.Mathematics.Vectors; namespace Chernobyl.App.Patterns { /// /// Extensions for working with WPF images. /// public static class Image { /// /// Creates a bitmap image using the color function. /// /// A method that outputs a value based on a normalized (0 to 1) /// position it is given. For X, 0 is the left most side and 1 is the right most. For Y, /// 0 is the bottom most and 1 the top most. /// The horizontal size of the 2D image. Must be at least 2. /// The vertical size of the 2D image. Must be at least 2. public static ImageSource CreateBitmap(this Func factory, int width, int height) { var pixels = factory .To1D(width, height) .SelectMany(color => color.Bgra.AsEnumerable()) .ToArray(); Int32Rect rect = new Int32Rect(0, 0, width, height); int stride = 4 * width; var bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null); bitmap.WritePixels(rect, pixels, stride, 0); return bitmap; } } }