1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Windows;
|
---|
6 | using System.Windows.Media;
|
---|
7 | using System.Windows.Media.Imaging;
|
---|
8 | using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
---|
9 | using Microsoft.Research.DynamicDataDisplay.Common.Palettes;
|
---|
10 | using Microsoft.Research.DynamicDataDisplay.DataSources;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|