Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/NaiveColorMap.cs @ 13376

Last change on this file since 13376 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Media;
7using System.Windows.Media.Imaging;
8using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
9using Microsoft.Research.DynamicDataDisplay.Common.Palettes;
10using Microsoft.Research.DynamicDataDisplay.DataSources;
11
12namespace Microsoft.Research.DynamicDataDisplay.Charts
13{
14  public class NaiveColorMap
15  {
16    public double[,] Data { get; set; }
17
18    public IPalette Palette { get; set; }
19
20    public BitmapSource BuildImage()
21    {
22      if (Data == null)
23        throw new ArgumentNullException("Data");
24      if (Palette == null)
25        throw new ArgumentNullException("Palette");
26
27
28      int width = Data.GetLength(0);
29      int height = Data.GetLength(1);
30
31      int[] pixels = new int[width * height];
32
33      var minMax = Data.GetMinMax();
34      var min = minMax.Min;
35      var rangeDelta = minMax.GetLength();
36
37      int pointer = 0;
38      for (int iy = 0; iy < height; iy++)
39      {
40        for (int ix = 0; ix < width; ix++)
41        {
42          double value = Data[ix, height - 1 - iy];
43          double ratio = (value - min) / rangeDelta;
44          Color color = Palette.GetColor(ratio);
45          int argb = color.ToArgb();
46
47          pixels[pointer++] = argb;
48        }
49      }
50
51      WriteableBitmap bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, null);
52      int bpp = (bitmap.Format.BitsPerPixel + 7) / 8;
53      int stride = bitmap.PixelWidth * bpp;
54
55      bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, stride, 0);
56
57      return bitmap;
58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.