using System;
using System.Collections.Generic;
using System.Linq;
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;
using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
namespace CSRender
{
///
/// MainWindow.xaml の相互作用ロジック
///
public partial class MainWindow : Window
{
//comboBoxのインスタンス
private ObservableCollection PagePattern = new ObservableCollection();
private ObservableCollection FilePattern = new ObservableCollection();
public MainWindow()
{
InitializeComponent();
//解像度選択UpDownバーの設定
this.Title = this.ToString();
ResolutionBox.MouseWheel += NumericTextBox_MouseWheel;
ResolutionBox.GotFocus += NumericTextBox_GotFocusSelectAll;
ResolutionBox.TextChanged += NumericTextBox_TextChanged;
ResolutionBox.TextChanged += NumericTextBox_NumChanged;
NumericScroll1.MouseWheel += NumericScroll1_MouseWheel;
//位置ズレ許容量選択UpDownバーの設定
this.Title = this.ToString();
PositionBox.MouseWheel += NumericTextBox_MouseWheel;
PositionBox.GotFocus += NumericTextBox_GotFocusSelectAll;
PositionBox.TextChanged += NumericTextBox_TextChanged;
PositionBox.TextChanged += PosiTextBox_NumChanged;
PosiScroll1.MouseWheel += PosiScroll1_MouseWheel;
//諧調差許容量選択UpDownバーの設定
this.Title = this.ToString();
GradationBox.MouseWheel += NumericTextBox_MouseWheel;
GradationBox.GotFocus += NumericTextBox_GotFocusSelectAll;
GradationBox.TextChanged += NumericTextBox_TextChanged;
GradationBox.TextChanged += GradTextBox_NumChanged;
GradScroll.MouseWheel += GradScroll_MouseWheel;
//微小誤差許容量選択UpDownバーの設定
this.Title = this.ToString();
SmallDiffBox.MouseWheel += NumericTextBox_MouseWheel;
SmallDiffBox.GotFocus += NumericTextBox_GotFocusSelectAll;
SmallDiffBox.TextChanged += NumericTextBox_TextChanged;
SmallDiffBox.TextChanged += SmallDiffTextBox_NumChanged;
SmallDiffScroll.MouseWheel += SmallDiffScroll_MouseWheel;
//視覚的あいまい量選択UpDownバーの設定
this.Title = this.ToString();
VisualBox.MouseWheel += NumericTextBox_MouseWheel;
VisualBox.GotFocus += NumericTextBox_GotFocusSelectAll;
VisualBox.TextChanged += NumericTextBox_TextChanged;
VisualBox.TextChanged += VisualTextBox_NumChanged;
VisualScroll.MouseWheel += VisualScroll_MouseWheel;
//ページ入力メニューの設定
PagePattern.Add((new ComboBoxSet { Id = 1, Item = "全ページ"}));
PagePattern.Add((new ComboBoxSet { Id = 2, Item = "指定" }));
PageRange.ItemsSource = PagePattern;
pageBox.GotFocus += PageBox_GotFocusSelectAll;
//出力ファイル形式メニューの設定
FilePattern.Add((new ComboBoxSet { Id = 1, Item = "JPG" }));
FilePattern.Add((new ComboBoxSet { Id = 2, Item = "JPEG" }));
FilePattern.Add((new ComboBoxSet { Id = 3, Item = "PNG" }));
FilePattern.Add((new ComboBoxSet { Id = 4, Item = "TIF" }));
FilePattern.Add((new ComboBoxSet { Id = 5, Item = "TIFF" }));
FilePattern.Add((new ComboBoxSet { Id = 6, Item = "GIF" }));
FilePattern.Add((new ComboBoxSet { Id = 7, Item = "BMP" }));
FileSelect.ItemsSource = FilePattern;
//デフォルトに前回の設定値を表示
ResolutionBox.Text = Properties.Settings.Default.resolutionSetting;
FileSelect.Text = Properties.Settings.Default.formatSetting;
PositionBox.Text = Properties.Settings.Default.shiftPixelSetting;
GradationBox.Text = Properties.Settings.Default.colorMarginSetting;
SmallDiffBox.Text = Properties.Settings.Default.removePointSetting;
VisualBox.Text = Properties.Settings.Default.shadingOffSetting;
//ドラッグ&ドロップイベントの追加
targetBox.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true);
targetBox.AddHandler(TextBox.DropEvent, new DragEventHandler(TargetBox_Drop), true);
referenceBox.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true);
referenceBox.AddHandler(TextBox.DropEvent, new DragEventHandler(RefBox_Drop), true);
workBox.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true);
workBox.AddHandler(TextBox.DropEvent, new DragEventHandler(WorkBox_Drop), true);
}
//Closeボタン実行
private void CloseButton_Click(object sender, RoutedEventArgs e) =>
Close();
//比較ボタン実行
private void CompareButtonClick(object sender, RoutedEventArgs e)
{
//対象フォルダのpathチェック
if (!Directory.Exists(targetBox.Text) && !File.Exists(targetBox.Text))
{
CustomMsgBox.Show(mainWindow, "指定されたファイルあるいはフォルダが存在しません。" +
"\nもう一度設定してください。", "The specified file or folder does not exist", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
//リファレンスファイルのpathチェック
if (!Directory.Exists(referenceBox.Text) && !File.Exists(referenceBox.Text))
{
CustomMsgBox.Show(mainWindow, "指定されたリファレンスファイルあるいはフォルダが存在しません。" +
"\nもう一度設定してください。", "The specified reference file or folder does not exist", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
//出力ファイルpathのチェック
if (!Directory.Exists(workBox.Text) && !File.Exists(workBox.Text))
{
CustomMsgBox.Show(mainWindow, "指定された作業フォルダが存在しません。" +
"\nもう一度設定してください。", "The specified reference file or folder does not exist", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
//検版パラメータの取得
string shiftPixel = PositionBox.Text;
string colorMargin = GradationBox.Text;
string removePoint = SmallDiffBox.Text;
string shadingOff = VisualBox.Text;
//TIFF画像の生成
ResultConsole.Text += "\r\n";
ResultConsole.Text += " 対象ファイルをTIFF画像に変換中\r\n";
//必要に応じてページ引数を指定
bool pageFlag = false;
string selectedPage = "";
if (PageRange.Text == "指定")
{
pageFlag = true;
selectedPage = pageBox.Text;
}
//対象フォルダ
string targetTIFFPath = System.IO.Path.Combine(workBox.Text, "work", "targetTIFF");
if (!Directory.Exists(targetTIFFPath))//指定先にTIFF用フォルダがあるか
{
Directory.CreateDirectory(targetTIFFPath);//なければ新規でTIFF用フォルダを作成
}
//CSRenderの実行
string targetTIFFResult =
OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, targetBox.Text, targetTIFFPath);
ResultConsole.Text += " リファレンスファイルをTIFF画像に変換中\r\n";
//リファレンスフォルダ
string referenceTIFFPath = System.IO.Path.Combine(workBox.Text, "work", "referenceTIFF");
if (!Directory.Exists(referenceTIFFPath))//指定先にTIFF用フォルダがあるか
{
Directory.CreateDirectory(referenceTIFFPath);//なければ新規でTIFF用フォルダを作成
}
//CSRenderの実行
string referenceTIFFResult =
OutputTIFF.runCSRender(ResolutionBox.Text, "TIFF", pageFlag, selectedPage, referenceBox.Text, referenceTIFFPath);
ResultConsole.Text += "\r\n\r\n";
//PureVerifyの実行
//string workDir = CompareTIFF.runPureVerify(targetBox.Text, referenceBox.Text, workBox.Text);
string workDir = CompareTIFF.runPureVerify(targetTIFFPath, referenceTIFFPath, workBox.Text
, shiftPixel, colorMargin, removePoint, shadingOff);
//レポートを出力
outputLog(System.IO.Path.Combine(workDir, "report"));
//指定先にレポートを移動
CompareTIFF.moveReport(workBox.Text, workDir);
//パラメータを次回初期値用に保存
Properties.Settings.Default.resolutionSetting = ResolutionBox.Text;
Properties.Settings.Default.formatSetting = FileSelect.Text;
Properties.Settings.Default.shiftPixelSetting = PositionBox.Text;
Properties.Settings.Default.colorMarginSetting = GradationBox.Text;
Properties.Settings.Default.removePointSetting = SmallDiffBox.Text;
Properties.Settings.Default.shadingOffSetting = VisualBox.Text;
Properties.Settings.Default.Save();
}
//UIに検版結果を出力
private void outputLog(string workDir)
{
//logファイルを読み取り
string[] reportFiles = System.IO.Directory.GetFiles(workDir, "*.log", System.IO.SearchOption.TopDirectoryOnly);
string logfileName = reportFiles[0];
StreamReader logString = new StreamReader(logfileName, Encoding.GetEncoding("Shift-JIS"));//Shift-JISでエンコーディング
string tranceString = "";
int lineCount = 0;
//ファイルから読み取った結果をUIに出力
while (logString.EndOfStream == false)
{
tranceString = logString.ReadLine();
lineCount++;
if (lineCount > 13 && lineCount < 40)//必要なところだけをUIに記述
{
ResultConsole.Text += tranceString + "\r\n";
ResultConsole.ScrollToEnd();
}
}
logString.Close();
}
//TIFFボタンを実行
private void TIFFButtonClick(object sender, RoutedEventArgs e)
{
ResultConsole.Text += "<変換開始>\r\n";
ResultConsole.Text += "PDFを" + FileSelect.Text + "に変換します\r\n";
ResultConsole.Text += "\r\n";
//必要に応じてページ引数を指定
bool pageFlag = false;
string selectedPage = "";
if (PageRange.Text == "指定")
{
pageFlag = true;
selectedPage = pageBox.Text;
}
//対象フォルダのpathが存在するかチェック
if (!Directory.Exists(targetBox.Text) && !File.Exists(targetBox.Text))
{
CustomMsgBox.Show(mainWindow, "指定されたファイルあるいはフォルダが存在しません。" +
"\nもう一度TIFF出力する対象を設定してください。", "The specified file or folder does not exist", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
//出力フォルダのpathチェック
if (workBox.Text != "")
{
if (!Directory.Exists(workBox.Text) && !File.Exists(workBox.Text))//pathのチェック
{
CustomMsgBox.Show(mainWindow, "指定された作業フォルダが存在しません。" +
"\nもう一度設定してください。", "The specified reference file or folder does not exist", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
}
//CSRenderの実行
string TIFFresult =
OutputTIFF.runCSRender(ResolutionBox.Text, FileSelect.Text, pageFlag, selectedPage, targetBox.Text, workBox.Text);
ResultConsole.Text += TIFFresult;
ResultConsole.Text += "\r\n<変換終了>\r\n";
ResultConsole.ScrollToEnd();
//パラメータを次回初期値用に保存
Properties.Settings.Default.resolutionSetting = ResolutionBox.Text;
Properties.Settings.Default.formatSetting = FileSelect.Text;
Properties.Settings.Default.Save();
}
}
}