-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
155 lines (141 loc) · 5.2 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RawViewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
BitmapSource _original;
const int _width = 5120;
const int _height = 5120;
int _bitsPerPixel = 10;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
using (System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog())
{
openFileDialog.InitialDirectory = Properties.Settings.Default.InitialDirectory;
openFileDialog.Filter = "All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.InitialDirectory = openFileDialog.InitialDirectory;
DrawPicture(openFileDialog.FileName);
}
}
}
private void DrawPicture(string aFilePath)
{
try
{
if (!File.Exists(aFilePath))
{
return;
}
ushort[] pix16 = null;
using (BinaryReader br = new BinaryReader(File.OpenRead(aFilePath)))
{
if (_bitsPerPixel > 16)
{
int pixNum = (int)(br.BaseStream.Length / sizeof(float));
float[] pix32 = new float[pixNum];
float max = 0;
for (var i = 0; i < pixNum; ++i)
{
float num = br.ReadSingle();
pix32[i] = num > 0 ? num : 0;
max = Math.Max(pix32[i], max);
}
pix16 = new ushort[pixNum];
for (var i = 0; i < pixNum; ++i)
{
pix16[i] = (ushort)(ushort.MaxValue * pix32[i] / max);
}
}
else
{
int pixNum = (int)(br.BaseStream.Length / sizeof(ushort));
pix16 = new ushort[pixNum];
ushort pixShort;
for (var i = 0; i < pixNum; ++i)
{
pixShort = (ushort)(br.ReadUInt16() * Math.Pow(2, 16 - _bitsPerPixel));
pix16[i] = pixShort;
}
}
}
int bitsPerPixel = 16;
int stride = (_width * bitsPerPixel + 7) / 8;
_original = BitmapSource.Create(_width, _height, 96, 96, PixelFormats.Gray16, null, pix16, stride);
_image.Source = _original;
Properties.Settings.Default.LastOpenedFilePath = aFilePath;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void OnSlider_ValueChanged(object sender, RoutedEventArgs e)
{
_image.Source = BitmapUtils.AdjustBitmap(_original, _sliderBrightness.Value, _sliderContrast.Value, _sliderGamma.Value);
}
private void ZoomBorder_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
DrawPicture(files[0]);
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
private void ImageType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (comboBoxType.SelectedIndex)
{
case 0:
_bitsPerPixel = 8;
break;
case 1:
_bitsPerPixel = 10;
break;
case 2:
_bitsPerPixel = 12;
break;
case 3:
_bitsPerPixel = 14;
break;
case 4:
_bitsPerPixel = 16;
break;
case 5:
_bitsPerPixel = 32;
break;
}
DrawPicture(Properties.Settings.Default.LastOpenedFilePath);
}
}
}