public static void Plot <T>(string filename, NumCIL.Generic.NdArray <T> array, Func <int, int, T, Color> encode, Color backgroundColor) { var shape = array.Shape; if (shape.Dimensions.Length != 2) { throw new Exception("Must be 2D array"); } var w = (int)shape.Dimensions[0].Length; var h = (int)shape.Dimensions[1].Length; using (var bmp = new Bitmap(w, h)) { using (var gc = System.Drawing.Graphics.FromImage(bmp)) gc.Clear(backgroundColor); using (var lb = new LockBitmap(bmp)) for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { lb.SetPixel(x, y, encode(x, y, array.Value[y, x])); } } bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Png); } }
/// <summary> /// Recomputes image histogram and draws the result in the given raster image. /// </summary> /// <param name="input">Input image.</param> /// <param name="param">Textual parameter.</param> public static void ComputeHistogram( Bitmap input, string param, int mouseX, int mouseY) { // Text parameters: param = param.ToLower().Trim(); // Graph appearance: alt = param.IndexOf("alt") >= 0; int x, y; bool mc = mouseX >= 0 && mouseY >= 0; // local or global histogram byte radius = 25; // faster bitmap access LockBitmap lockBitmap = new LockBitmap(input); lockBitmap.LockBits(); // 1. Histogram recomputation. int size = int.TryParse(param, out size) ? size : 64; size = Math.Min(Math.Max(size, 2), 255); histArray = new byte[histArray.GetLength(0), size]; int width = input.Width; int height = input.Height; if (mouseX > width) { mouseX = width - 1; } if (mouseY > height) { mouseY = height - 1; } for (y = Math.Max(mc ? mouseY - radius:0, 0); y < Math.Min((mc ? mouseY + radius : height), height); y++) { for (x = Math.Max(mc ? mouseX - radius :0, 0); x < Math.Min((mc ? mouseX + radius : width), height); x++) { Color color = lockBitmap.GetPixel(x, y); byte hue = (byte)(color.GetHue() * (histArray.GetLength(0) - 1) / 360.0); byte saturation = (byte)(color.GetSaturation() * (histArray.GetLength(1) - 1)); if (histArray[hue, saturation] < 255) { histArray[hue, saturation]++; } } } lockBitmap.UnlockBits(); }