Save imported image in the same format (WIP)
[master-thesis.git] / Parasitemia / ParasitemiaUI / DPICalculator.fs
1 module ParasitemiaUI.PPICalculator
2
3 open System
4 open System.Windows
5
6 open ParasitemiaUIControls
7 open ParasitemiaCore.Types
8 open ParasitemiaCore.UnitsOfMeasure
9
10 open Types
11
12 let showWindow (parent : Window) : int option =
13 let win = PPICalculatorWindow ()
14 win.Owner <- parent
15 win.Left <- parent.Left + parent.ActualWidth / 2. - win.Width / 2.
16 win.Top <- parent.Top + parent.ActualHeight / 2. - win.Height / 2.
17
18 for size in Utils.sensorSizes do
19 win.cmbSensorSize.Items.Add (size) |> ignore
20 win.cmbSensorSize.SelectedIndex <- 0
21
22 let resolution (w_p : float<px>) (w_mm : float<mm>) (zoom : float) : float<ppi> =
23 w_p * zoom / mmToInch w_mm
24
25 let updateCurrentResolution () =
26 let { w = w; h = h } = win.cmbSensorSize.SelectedValue :?> SensorSize
27 let ratio = h / w
28
29 let parseDouble (txt : string) (errorMess : string) = match Double.TryParse (txt) with true, value -> Success value | _ -> Fail errorMess
30
31 match
32 (result {
33 let! sensorResolution = parseDouble win.txtSensorResolution.Text "The sensor resolution is not valid"
34 let! zoom = parseDouble win.txtZoom.Text "The zoom is not valid"
35 let wPixel = 1.<px> * sqrt (sensorResolution * 1e6 / ratio)
36 return! Success (float <| resolution wPixel w zoom)
37 }) with
38 | Success res -> win.txtImageResolution.Text <- int (res / 1000.) * 1000 |> string
39 | Fail mess -> win.txtImageResolution.Text <- mess
40
41 win.butCancel.Click.AddHandler (fun obj args -> win.DialogResult <- Nullable<bool> (false); win.Close ())
42 win.butOK.Click.AddHandler (fun obj args -> win.DialogResult <- Nullable<bool> (true); win.Close ())
43
44 win.cmbSensorSize.SelectionChanged.AddHandler (fun obj arg -> updateCurrentResolution ())
45 win.txtSensorResolution.TextChanged.AddHandler (fun obj arg -> updateCurrentResolution ())
46 win.txtZoom.TextChanged.AddHandler (fun obj arg -> updateCurrentResolution ())
47
48 let result = win.ShowDialog ()
49 if result.HasValue && result.Value then
50 match Int32.TryParse win.txtImageResolution.Text with
51 | true, res -> Some res
52 | _ -> None
53 else
54 None
55