Clean up code structure

This commit is contained in:
Claudio Schaad 2020-12-03 19:18:09 +01:00
parent 5dd28a0f20
commit 18f8c7efb3
12 changed files with 75 additions and 69 deletions

View file

@ -31,7 +31,7 @@
</StackPanel> </StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal"> <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="sourceImage" Stretch="None"/> <Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="sourceImage" Stretch="None"/>
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="scaledImage" Stretch="None"/> <Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="legoArtImage" Stretch="None"/>
</StackPanel> </StackPanel>

View file

@ -7,6 +7,8 @@ using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media; using System.Windows.Media;
using Rectangle = System.Windows.Shapes.Rectangle; using Rectangle = System.Windows.Shapes.Rectangle;
using LegoArtTool.LegoArtColor;
using LegoArtTool.BuildingInstruction;
namespace LegoArt namespace LegoArt
{ {
@ -19,6 +21,7 @@ namespace LegoArt
private readonly BitmapHelperService bitmapHelperService; private readonly BitmapHelperService bitmapHelperService;
private readonly ImageHelperService imageHelperService; private readonly ImageHelperService imageHelperService;
private readonly LegoArtImageGenerationService legoArtImageGenerationService; private readonly LegoArtImageGenerationService legoArtImageGenerationService;
private readonly BuildingInstructionService buildingInstructionService;
public MainWindow() public MainWindow()
{ {
@ -28,6 +31,7 @@ namespace LegoArt
bitmapHelperService = new BitmapHelperService(); bitmapHelperService = new BitmapHelperService();
imageHelperService = new ImageHelperService(); imageHelperService = new ImageHelperService();
legoArtImageGenerationService = new LegoArtImageGenerationService(); legoArtImageGenerationService = new LegoArtImageGenerationService();
buildingInstructionService = new BuildingInstructionService();
btnInstructionsPersister.Visibility = Visibility.Hidden; btnInstructionsPersister.Visibility = Visibility.Hidden;
} }
@ -39,43 +43,55 @@ namespace LegoArt
{ {
tbImagePath.Text = openFileDialog.FileName; tbImagePath.Text = openFileDialog.FileName;
ShowWaitCursor();
LoadImage(openFileDialog.FileName);
HideWaitCursor();
}
}
private void ShowWaitCursor()
{
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
Mouse.OverrideCursor = Cursors.Wait; Mouse.OverrideCursor = Cursors.Wait;
}); });
}
LoadImage(openFileDialog.FileName); private void HideWaitCursor()
{
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
}); });
} }
}
private void btnInstructionsPersister_Click(object sender, RoutedEventArgs e) private void btnInstructionsPersister_Click(object sender, RoutedEventArgs e)
{ {
var d = new DataObject(DataFormats.Bitmap, scaledImage.Source, true); ShowWaitCursor();
var bitmap = d.GetData(typeof(Bitmap)) as Bitmap;
var outputPath = BuildingInstructionService.PersistBuildingInstructions(bitmap, tbImagePath.Text);
MessageBox.Show($"Instructions saved to {outputPath}", "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
var d = new DataObject(DataFormats.Bitmap, legoArtImage.Source, true);
var legoArtBitmap = d.GetData(typeof(Bitmap)) as Bitmap;
var buildingInstructionBitmap = buildingInstructionService.CreateBuildingInstructionImage(legoArtBitmap, legoArtColorService.GetLegoColors());
var outputPath = buildingInstructionService.PersistBuildingInstructions(buildingInstructionBitmap, tbImagePath.Text);
HideWaitCursor();
MessageBox.Show($"Instructions saved to {outputPath}", "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
} }
private void LoadImage(string path) private void LoadImage(string path)
{ {
var bitmap = new System.Drawing.Bitmap(path); var sourceBitmap = new Bitmap(path);
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(bitmap); sourceImage.Source = imageHelperService.LoadToImage(bitmapHelperService.Scale(sourceBitmap, 1));
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(sourceBitmap);
legoArtImage.Source = imageHelperService.LoadToImage(reducedBitMap);
var legoArtColors = legoArtColorService.ParseImage(reducedBitMap); var legoArtColors = legoArtColorService.ParseImage(reducedBitMap);
if (legoArtColors != null) if (legoArtColors != null)
{ {
var sourceBitmap = bitmapHelperService.Scale(reducedBitMap, 1);
sourceImage.Source = imageHelperService.LoadToImage(sourceBitmap);
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitmap, legoArtColorService.GetLegoColors());
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
btnInstructionsPersister.Visibility = Visibility.Visible; btnInstructionsPersister.Visibility = Visibility.Visible;
parentStackPanel.Children.Clear(); parentStackPanel.Children.Clear();

View file

@ -1,6 +1,4 @@
using LegoArtTool.Pixels; using System.Drawing;
using System.Collections.Generic;
using System.Drawing;
namespace LegoArtTool namespace LegoArtTool
{ {
@ -29,11 +27,5 @@ namespace LegoArtTool
return scaledBitmap; return scaledBitmap;
} }
public Bitmap ConvertToPixelMatrix(Bitmap bitmap, List<LegoArtColorInfo> legoArtColors)
{
var pixelMatrix = new PixelMatrix(bitmap, legoArtColors);
return pixelMatrix.ConvertBitmap();
}
} }
} }

View file

@ -1,8 +1,8 @@
using System.Drawing; using System.Drawing;
namespace LegoArtTool.Pixels namespace LegoArtTool.BuildingInstruction
{ {
public class Pixel public class BuildingInstructionElement
{ {
public Color Color { get; private set; } public Color Color { get; private set; }
public int Number { get; private set; } public int Number { get; private set; }
@ -10,7 +10,7 @@ namespace LegoArtTool.Pixels
public const int PixelSize = 50; public const int PixelSize = 50;
private const int NumberSize = 20; private const int NumberSize = 20;
public Pixel(Color color, int number) public BuildingInstructionElement(Color color, int number)
{ {
Color = color; Color = color;
Number = number; Number = number;

View file

@ -1,21 +1,19 @@
using System; using LegoArtTool.LegoArtColor;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
namespace LegoArtTool.Pixels namespace LegoArtTool.BuildingInstruction
{ {
public class PixelMatrix public class BuildingInstructionMatrix
{ {
public Pixel[,] Pixels { get; private set; } public BuildingInstructionElement[,] Pixels { get; private set; }
public int MatrixSize { get; private set; } public int MatrixSize { get; private set; }
private Random rnd = new Random(); public BuildingInstructionMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
public PixelMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
{ {
MatrixSize = sourceBitmap.Width; MatrixSize = sourceBitmap.Width;
Pixels = new Pixel[MatrixSize, MatrixSize]; Pixels = new BuildingInstructionElement[MatrixSize, MatrixSize];
for (int x = 0; x < MatrixSize; x++) for (int x = 0; x < MatrixSize; x++)
{ {
@ -24,14 +22,14 @@ namespace LegoArtTool.Pixels
var pixel = sourceBitmap.GetPixel(x, y); var pixel = sourceBitmap.GetPixel(x, y);
var sourceColor = Color.FromArgb(pixel.ToArgb()); var sourceColor = Color.FromArgb(pixel.ToArgb());
var legoNumber = legoArtColors.Single(l => l.Color == sourceColor).LegoNumber; var legoNumber = legoArtColors.Single(l => l.Color == sourceColor).LegoNumber;
Pixels[x, y] = new Pixel(sourceColor, legoNumber); Pixels[x, y] = new BuildingInstructionElement(sourceColor, legoNumber);
} }
} }
} }
public Bitmap ConvertBitmap() public Bitmap GetBitmap()
{ {
var bitmapSize = MatrixSize * Pixel.PixelSize; var bitmapSize = MatrixSize * BuildingInstructionElement.PixelSize;
var bitmap = new Bitmap(bitmapSize, bitmapSize); var bitmap = new Bitmap(bitmapSize, bitmapSize);
using (var g = Graphics.FromImage(bitmap)) using (var g = Graphics.FromImage(bitmap))
{ {
@ -47,11 +45,5 @@ namespace LegoArtTool.Pixels
return bitmap; return bitmap;
} }
private Color GetRandomColor()
{
Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
return randomColor;
}
} }
} }

View file

@ -1,12 +1,20 @@
using System.Drawing; using LegoArtTool.LegoArtColor;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
namespace LegoArtTool namespace LegoArtTool.BuildingInstruction
{ {
public static class BuildingInstructionService public class BuildingInstructionService
{ {
public static string PersistBuildingInstructions(Bitmap bitmap, string path) public Bitmap CreateBuildingInstructionImage(Bitmap bitmap, List<LegoArtColorInfo> legoArtColors)
{
var pixelMatrix = new BuildingInstructionMatrix(bitmap, legoArtColors);
return pixelMatrix.GetBitmap();
}
public string PersistBuildingInstructions(Bitmap bitmap, string path)
{ {
var segmentSize = bitmap.Width / 3; var segmentSize = bitmap.Width / 3;
var outPutDirectory = CreateOutPutDirectory(path); var outPutDirectory = CreateOutPutDirectory(path);
@ -25,7 +33,7 @@ namespace LegoArtTool
return outPutDirectory; return outPutDirectory;
} }
private static string CreateOutPutDirectory(string path) private string CreateOutPutDirectory(string path)
{ {
var increment = 0; var increment = 0;
@ -39,7 +47,7 @@ namespace LegoArtTool
return outputDirectoryPath; return outputDirectoryPath;
} }
private static string GetOutputDirectoryPath(int increment, string path) private string GetOutputDirectoryPath(int increment, string path)
{ {
var parent = Directory.GetParent(path); var parent = Directory.GetParent(path);
var filename = Path.GetFileNameWithoutExtension(path); var filename = Path.GetFileNameWithoutExtension(path);

View file

@ -1,7 +1,7 @@
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
namespace LegoArtTool.Pixels namespace LegoArtTool.BuildingInstruction
{ {
public static class GraphicsExtensions public static class GraphicsExtensions
{ {

View file

@ -1,6 +1,6 @@
using System.Drawing; using System.Drawing;
namespace LegoArtTool namespace LegoArtTool.LegoArtColor
{ {
public class LegoArtColorInfo public class LegoArtColorInfo
{ {

View file

@ -1,4 +1,4 @@
namespace LegoArtTool namespace LegoArtTool.LegoArtColor
{ {
public class LegoArtColorOffset public class LegoArtColorOffset
{ {
@ -12,7 +12,5 @@
LegoArtColorInfo = legoArtColorInfo; LegoArtColorInfo = legoArtColorInfo;
IsAvailable = true; IsAvailable = true;
} }
} }
} }

View file

@ -2,7 +2,7 @@
using System.Linq; using System.Linq;
using System.Drawing; using System.Drawing;
namespace LegoArtTool namespace LegoArtTool.LegoArtColor
{ {
public class LegoArtColorService public class LegoArtColorService
{ {

View file

@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace LegoArtTool namespace LegoArtTool.LegoArtColor
{ {
public class Pixel public class Pixel
{ {
@ -17,6 +17,5 @@ namespace LegoArtTool
LegoArtColorOffsets = legoArtColorOffsets; LegoArtColorOffsets = legoArtColorOffsets;
IsAssigned = false; IsAssigned = false;
} }
} }
} }

View file

@ -1,4 +1,5 @@
using System; using LegoArtTool.LegoArtColor;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;