PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/Main/src/Xbap/DynamicDataDisplay.Xbap.Samples/Demos/v02/ImageHistogram.xaml.cs

#
C# | 118 lines | 94 code | 16 blank | 8 comment | 4 complexity | 04f3b994fd6183e608b479030eaaa430 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Windows;
  6using System.Windows.Controls;
  7using System.Windows.Data;
  8using System.Windows.Documents;
  9using System.Windows.Input;
 10using System.Windows.Media;
 11using System.Windows.Media.Imaging;
 12using System.Windows.Navigation;
 13using System.Windows.Shapes;
 14using Microsoft.Research.DynamicDataDisplay.DataSources;
 15using Microsoft.Win32;
 16
 17namespace Microsoft.Research.DynamicDataDisplay.Samples.Demos.v02
 18{
 19	/// <summary>
 20	/// Interaction logic for ImageHistogram.xaml
 21	/// </summary>
 22	public partial class ImageHistogram : Page
 23	{
 24		public ImageHistogram()
 25		{
 26			InitializeComponent();
 27		}
 28
 29		private void OnOpenImageClick(object sender, RoutedEventArgs e)
 30		{
 31			OpenFileDialog dlg = new OpenFileDialog();
 32			dlg.Filter = "Image|*.bmp;*.jpg;*.png;*.gif";
 33			if (dlg.ShowDialog(Window.GetWindow(this)).GetValueOrDefault(false))
 34			{
 35				OpenImage(dlg.FileName);
 36			}
 37		}
 38
 39		private void OpenImage(string fileName)
 40		{
 41			BitmapImage bmp = new BitmapImage(new Uri(fileName));
 42			bmp.CacheOption = BitmapCacheOption.OnLoad;
 43
 44			image.Source = bmp;
 45			ProcessImage(bmp);
 46		}
 47
 48		EnumerableDataSource<int> red;
 49		EnumerableDataSource<int> green;
 50		EnumerableDataSource<int> blue;
 51
 52		int[] reds = new int[256];
 53		int[] greens = new int[256];
 54		int[] blues = new int[256];
 55		byte[] pixels;
 56		private void ProcessImage(BitmapImage bmp)
 57		{
 58			byte[] pixels = new byte[bmp.PixelWidth * bmp.PixelHeight * 4];
 59			bmp.CopyPixels(pixels, bmp.PixelWidth * 4, 0);
 60
 61			for (int i = 0; i < pixels.Length; )
 62			{
 63				//BGRA
 64				blues[pixels[i++]]++;
 65				greens[pixels[i++]]++;
 66				reds[pixels[i++]]++;
 67				i++;
 68			}
 69
 70			CreateHistograms();
 71		}
 72
 73		private void CreateHistograms()
 74		{
 75			EnumerableDataSource<int> x = new EnumerableDataSource<int>(Enumerable.Range(0, 256).ToArray());
 76			x.SetXMapping(_x => _x);
 77
 78			Func<int, double> mapping;
 79			if (check.IsChecked.GetValueOrDefault())
 80				mapping = logMapping;
 81			else
 82				mapping = linearMapping;
 83
 84			red = new EnumerableDataSource<int>(reds);
 85			red.SetYMapping(mapping);
 86			green = new EnumerableDataSource<int>(greens);
 87			green.SetYMapping(mapping);
 88			blue = new EnumerableDataSource<int>(blues);
 89			blue.SetYMapping(mapping);
 90
 91			CompositeDataSource rDS = new CompositeDataSource(x, red);
 92			CompositeDataSource gDS = new CompositeDataSource(x, green);
 93			CompositeDataSource bDS = new CompositeDataSource(x, blue);
 94
 95			plotter.RemoveUserElements();
 96			// todo
 97			//plotter.AddLineGraph(rDS, Colors.Red, 1, "Red").FilteringEnabled = false;
 98			//plotter.AddLineGraph(gDS, Colors.Green, 1, "Green").FilteringEnabled = false;
 99			//plotter.AddLineGraph(bDS, Colors.Blue, 1, "Blue").FilteringEnabled = false;
100		}
101
102		private Func<int, double> logMapping = i => i > 0 ? Math.Log10(i) : 0;
103		private Func<int, double> linearMapping = i => i;
104
105		private void CheckBox_Checked(object sender, RoutedEventArgs e)
106		{
107			Func<int, double> mapping;
108			if (check.IsChecked.GetValueOrDefault())
109				mapping = logMapping;
110			else
111				mapping = linearMapping;
112
113			red.SetYMapping(mapping);
114			green.SetYMapping(mapping);
115			blue.SetYMapping(mapping);
116		}
117	}
118}