Implement entropy calculation on GUI.

This commit is contained in:
2018-10-14 19:10:30 +01:00
parent db4a339cc2
commit 00ca3009dd
6 changed files with 353 additions and 1 deletions

View File

@@ -1621,6 +1621,8 @@
<e p="frmConsole.xeto.cs" t="Include" />
<e p="frmDump.xeto" t="Include" />
<e p="frmDump.xeto.cs" t="Include" />
<e p="frmImageEntropy.xeto" t="Include" />
<e p="frmImageEntropy.xeto.cs" t="Include" />
<e p="frmMain.xeto" t="Include" />
<e p="frmMain.xeto.cs" t="Include" />
</e>

View File

@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?><!--
// /***************************************************************************
// The Disc Image Chef
// ============================================================================
//
// Filename : frmEntropy.xeto
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Main window.
//
// ==[ Description ] ==========================================================
//
// Defines the structure for the main GUI window.
//
// ==[ License ] ==============================================================
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ============================================================================
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
-->
<Form xmlns="http://schema.picoe.ca/eto.forms" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Calculate entropy" ClientSize="600, 450" Padding="10">
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
ID="stkOptions">
<CheckBox Text="Calculates how many sectors are duplicated (have same exact data in user area)."
ID="chkDuplicatedSectors" Checked="True"/>
<CheckBox Text="Calculates entropy for each track separately." ID="chkSeparatedTracks" Checked="True"
Visible="False"/>
<CheckBox Text="Calculates entropy for the whole disc." ID="chkWholeDisc" Checked="True" Visible="False"/>
</StackLayout>
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
ID="stkResults" Visible="False">
<Label ID="lblMediaEntropy"/>
<Label ID="lblMediaUniqueSectors"/>
<GroupBox Text="Track entropy" ID="grpTrackEntropy">
<TreeGridView ID="treeTrackEntropy"/>
</GroupBox>
</StackLayout>
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
ID="stkProgress" Visible="False">
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
ID="stkProgress1" Visible="False">
<StackLayoutItem HorizontalAlignment="Center" Expand="True">
<Label ID="lblProgress"/>
</StackLayoutItem>
<StackLayoutItem HorizontalAlignment="Center" Expand="True">
<ProgressBar ID="prgProgress"/>
</StackLayoutItem>
</StackLayout>
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
ID="stkProgress2" Visible="False">
<StackLayoutItem HorizontalAlignment="Center" Expand="True">
<Label ID="lblProgress2"/>
</StackLayoutItem>
<StackLayoutItem HorizontalAlignment="Center" Expand="True">
<ProgressBar ID="prgProgress2"/>
</StackLayoutItem>
</StackLayout>
</StackLayout>
<StackLayoutItem HorizontalAlignment="Right" Expand="True">
<StackLayout Orientation="Horizontal" HorizontalContentAlignment="Right" VerticalContentAlignment="Bottom">
<Button ID="btnStart" Text="Start" Click="OnBtnStart"/>
<Button ID="btnClose" Text="Close" Click="OnBtnClose"/>
<Button ID="btnStop" Text="Stop" Enabled="False" Visible="False" Click="OnBtnStop"/>
</StackLayout>
</StackLayoutItem>
</StackLayout>
</Form>

View File

@@ -0,0 +1,248 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : frmEntropy.xeto.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Image entropy calculation window.
//
// --[ Description ] ----------------------------------------------------------
//
// Implements calculating media image entropy.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Threading;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.Console;
using DiscImageChef.Core;
using Eto.Forms;
using Eto.Serialization.Xaml;
namespace DiscImageChef.Gui.Forms
{
public class frmImageEntropy : Form
{
EntropyResults entropy;
IMediaImage inputFormat;
EntropyResults[] tracksEntropy;
public frmImageEntropy(IMediaImage inputFormat)
{
this.inputFormat = inputFormat;
XamlReader.Load(this);
if(inputFormat.Tracks != null && inputFormat.Tracks.Count > 0)
{
chkSeparatedTracks.Visible = true;
chkWholeDisc.Visible = true;
}
else
{
chkSeparatedTracks.Checked = false;
chkWholeDisc.Checked = true;
}
}
protected void OnBtnStart(object sender, EventArgs e)
{
Entropy entropyCalculator = new Entropy(false, false, inputFormat);
entropyCalculator.InitProgressEvent += InitProgress;
entropyCalculator.InitProgress2Event += InitProgress2;
entropyCalculator.UpdateProgressEvent += UpdateProgress;
entropyCalculator.UpdateProgress2Event += UpdateProgress2;
entropyCalculator.EndProgressEvent += EndProgress;
entropyCalculator.EndProgress2Event += EndProgress2;
chkDuplicatedSectors.Enabled = false;
chkSeparatedTracks.Enabled = false;
chkWholeDisc.Enabled = false;
btnClose.Visible = false;
btnStart.Visible = false;
btnStop.Visible = false;
stkProgress.Visible = true;
Thread thread = new Thread(() =>
{
if(chkSeparatedTracks.Checked == true)
{
tracksEntropy = entropyCalculator.CalculateTracksEntropy(chkDuplicatedSectors.Checked == true);
foreach(EntropyResults trackEntropy in tracksEntropy)
{
DicConsole.WriteLine("Entropy for track {0} is {1:F4}.", trackEntropy.Track,
trackEntropy.Entropy);
if(trackEntropy.UniqueSectors != null)
DicConsole.WriteLine("Track {0} has {1} unique sectors ({2:P3})", trackEntropy.Track,
trackEntropy.UniqueSectors,
(double)trackEntropy.UniqueSectors / (double)trackEntropy.Sectors);
}
}
if(chkWholeDisc.Checked != true) return;
entropy = entropyCalculator.CalculateMediaEntropy(chkDuplicatedSectors.Checked == true);
Application.Instance.Invoke(Finish);
});
Statistics.AddCommand("entropy");
thread.Start();
}
void Finish()
{
stkOptions.Visible = false;
btnClose.Visible = true;
stkProgress.Visible = false;
stkResults.Visible = true;
if(chkSeparatedTracks.Checked == true)
{
TreeGridItemCollection entropyList = new TreeGridItemCollection();
treeTrackEntropy.Columns.Add(new GridColumn {HeaderText = "Track", DataCell = new TextBoxCell(0)});
treeTrackEntropy.Columns.Add(new GridColumn {HeaderText = "Entropy", DataCell = new TextBoxCell(1)});
if(chkDuplicatedSectors.Checked == true)
treeTrackEntropy.Columns.Add(new GridColumn
{
HeaderText = "Unique sectors", DataCell = new TextBoxCell(2)
});
treeTrackEntropy.AllowMultipleSelection = false;
treeTrackEntropy.ShowHeader = true;
treeTrackEntropy.DataStore = entropyList;
foreach(EntropyResults trackEntropy in tracksEntropy)
entropyList.Add(new TreeGridItem
{
Values = new object[]
{
trackEntropy.Track, trackEntropy.Entropy,
$"{trackEntropy.UniqueSectors} ({(double)trackEntropy.UniqueSectors / (double)trackEntropy.Sectors:P3})"
}
});
grpTrackEntropy.Visible = true;
}
if(chkWholeDisc.Checked != true) return;
lblMediaEntropy.Text = $"Entropy for disk is {entropy.Entropy:F4}.";
lblMediaEntropy.Visible = true;
if(entropy.UniqueSectors == null) return;
lblMediaUniqueSectors.Text =
$"Disk has {entropy.UniqueSectors} unique sectors ({(double)entropy.UniqueSectors / (double)entropy.Sectors:P3})";
lblMediaUniqueSectors.Visible = true;
}
protected void OnBtnClose(object sender, EventArgs e)
{
Close();
}
protected void OnBtnStop(object sender, EventArgs e)
{
throw new NotImplementedException();
}
void InitProgress()
{
stkProgress1.Visible = true;
}
void EndProgress()
{
stkProgress1.Visible = false;
}
void InitProgress2()
{
stkProgress2.Visible = true;
}
void EndProgress2()
{
stkProgress2.Visible = false;
}
void UpdateProgress(string text, long current, long maximum)
{
UpdateProgress(text, current, maximum, lblProgress, prgProgress);
}
void UpdateProgress2(string text, long current, long maximum)
{
UpdateProgress(text, current, maximum, lblProgress2, prgProgress2);
}
void UpdateProgress(string text, long current, long maximum, Label label, ProgressBar progressBar)
{
Application.Instance.Invoke(() =>
{
label.Text = text;
if(maximum == 0)
{
progressBar.Indeterminate = true;
return;
}
if(progressBar.Indeterminate) progressBar.Indeterminate = false;
if(maximum > int.MaxValue || current > int.MaxValue)
{
progressBar.MaxValue = (int)(maximum / int.MaxValue);
progressBar.Value = (int)(current / int.MaxValue);
}
else
{
progressBar.MaxValue = (int)maximum;
progressBar.Value = (int)current;
}
});
}
#region XAML IDs
StackLayout stkOptions;
CheckBox chkDuplicatedSectors;
CheckBox chkSeparatedTracks;
CheckBox chkWholeDisc;
StackLayout stkResults;
Label lblMediaEntropy;
Label lblMediaUniqueSectors;
GroupBox grpTrackEntropy;
TreeGridView treeTrackEntropy;
StackLayout stkProgress;
StackLayout stkProgress1;
Label lblProgress;
ProgressBar prgProgress;
StackLayout stkProgress2;
Label lblProgress2;
ProgressBar prgProgress2;
Button btnStart;
Button btnClose;
Button btnStop;
#endregion
}
}

View File

@@ -111,5 +111,6 @@
</StackLayout>
</TabPage>
</TabControl>
<Button ID="btnEntropy" Text="Calculate entropy" Click="OnBtnEntropy"/>
</StackLayout>
</Panel>

View File

@@ -43,6 +43,7 @@ using DiscImageChef.Decoders.SCSI;
using DiscImageChef.Decoders.Xbox;
using DiscImageChef.Devices;
using DiscImageChef.Gui.Controls;
using DiscImageChef.Gui.Forms;
using DiscImageChef.Gui.Tabs;
using Eto.Drawing;
using Eto.Forms;
@@ -54,8 +55,12 @@ namespace DiscImageChef.Gui.Panels
{
public class pnlImageInfo : Panel
{
frmImageEntropy frmImageEntropy;
IMediaImage imageFormat;
public pnlImageInfo(string imagePath, IFilter filter, IMediaImage imageFormat)
{
this.imageFormat = imageFormat;
XamlReader.Load(this);
Stream logo =
@@ -780,6 +785,19 @@ namespace DiscImageChef.Gui.Panels
tabDumpHardware.Visible = true;
}
protected void OnBtnEntropy(object sender, EventArgs e)
{
if(frmImageEntropy != null)
{
frmImageEntropy.Show();
return;
}
frmImageEntropy = new frmImageEntropy(imageFormat);
frmImageEntropy.Closed += (s, ea) => { frmImageEntropy = null; };
frmImageEntropy.Show();
}
#region XAML controls
#pragma warning disable 169
#pragma warning disable 649
@@ -825,6 +843,7 @@ namespace DiscImageChef.Gui.Panels
TreeGridView treeDumpHardware;
ImageView imgMediaLogo;
SvgImageView svgMediaLogo;
Button btnEntropy;
#pragma warning restore 169
#pragma warning restore 649
#endregion

View File

@@ -85,7 +85,7 @@ namespace DiscImageChef.Commands
{
DicConsole.WriteLine("Entropy for track {0} is {1:F4}.", trackEntropy.Track, trackEntropy.Entropy);
if(trackEntropy.UniqueSectors != null)
DicConsole.WriteLine("Track {0} has {1} unique sectors ({1:P3})", trackEntropy.Track,
DicConsole.WriteLine("Track {0} has {1} unique sectors ({2:P3})", trackEntropy.Track,
trackEntropy.UniqueSectors,
(double)trackEntropy.UniqueSectors / (double)trackEntropy.Sectors);
}