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 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="scaledImage" Stretch="None"/>
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="legoArtImage" Stretch="None"/>
</StackPanel>

View file

@ -7,6 +7,8 @@ using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Rectangle = System.Windows.Shapes.Rectangle;
using LegoArtTool.LegoArtColor;
using LegoArtTool.BuildingInstruction;
namespace LegoArt
{
@ -19,6 +21,7 @@ namespace LegoArt
private readonly BitmapHelperService bitmapHelperService;
private readonly ImageHelperService imageHelperService;
private readonly LegoArtImageGenerationService legoArtImageGenerationService;
private readonly BuildingInstructionService buildingInstructionService;
public MainWindow()
{
@ -28,6 +31,7 @@ namespace LegoArt
bitmapHelperService = new BitmapHelperService();
imageHelperService = new ImageHelperService();
legoArtImageGenerationService = new LegoArtImageGenerationService();
buildingInstructionService = new BuildingInstructionService();
btnInstructionsPersister.Visibility = Visibility.Hidden;
}
@ -39,43 +43,55 @@ namespace LegoArt
{
tbImagePath.Text = openFileDialog.FileName;
Application.Current.Dispatcher.Invoke(() =>
{
Mouse.OverrideCursor = Cursors.Wait;
});
ShowWaitCursor();
LoadImage(openFileDialog.FileName);
Application.Current.Dispatcher.Invoke(() =>
{
Mouse.OverrideCursor = null;
});
HideWaitCursor();
}
}
private void ShowWaitCursor()
{
Application.Current.Dispatcher.Invoke(() =>
{
Mouse.OverrideCursor = Cursors.Wait;
});
}
private void HideWaitCursor()
{
Application.Current.Dispatcher.Invoke(() =>
{
Mouse.OverrideCursor = null;
});
}
private void btnInstructionsPersister_Click(object sender, RoutedEventArgs e)
{
var d = new DataObject(DataFormats.Bitmap, scaledImage.Source, true);
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);
ShowWaitCursor();
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)
{
var bitmap = new System.Drawing.Bitmap(path);
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(bitmap);
var sourceBitmap = new Bitmap(path);
sourceImage.Source = imageHelperService.LoadToImage(bitmapHelperService.Scale(sourceBitmap, 1));
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(sourceBitmap);
legoArtImage.Source = imageHelperService.LoadToImage(reducedBitMap);
var legoArtColors = legoArtColorService.ParseImage(reducedBitMap);
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;
parentStackPanel.Children.Clear();

View file

@ -1,6 +1,4 @@
using LegoArtTool.Pixels;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing;
namespace LegoArtTool
{
@ -29,11 +27,5 @@ namespace LegoArtTool
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;
namespace LegoArtTool.Pixels
namespace LegoArtTool.BuildingInstruction
{
public class Pixel
public class BuildingInstructionElement
{
public Color Color { get; private set; }
public int Number { get; private set; }
@ -10,7 +10,7 @@ namespace LegoArtTool.Pixels
public const int PixelSize = 50;
private const int NumberSize = 20;
public Pixel(Color color, int number)
public BuildingInstructionElement(Color color, int number)
{
Color = color;
Number = number;

View file

@ -1,21 +1,19 @@
using System;
using LegoArtTool.LegoArtColor;
using System.Collections.Generic;
using System.Drawing;
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; }
private Random rnd = new Random();
public PixelMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
public BuildingInstructionMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
{
MatrixSize = sourceBitmap.Width;
Pixels = new Pixel[MatrixSize, MatrixSize];
Pixels = new BuildingInstructionElement[MatrixSize, MatrixSize];
for (int x = 0; x < MatrixSize; x++)
{
@ -24,14 +22,14 @@ namespace LegoArtTool.Pixels
var pixel = sourceBitmap.GetPixel(x, y);
var sourceColor = Color.FromArgb(pixel.ToArgb());
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);
using (var g = Graphics.FromImage(bitmap))
{
@ -47,11 +45,5 @@ namespace LegoArtTool.Pixels
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.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 outPutDirectory = CreateOutPutDirectory(path);
@ -25,7 +33,7 @@ namespace LegoArtTool
return outPutDirectory;
}
private static string CreateOutPutDirectory(string path)
private string CreateOutPutDirectory(string path)
{
var increment = 0;
@ -39,7 +47,7 @@ namespace LegoArtTool
return outputDirectoryPath;
}
private static string GetOutputDirectoryPath(int increment, string path)
private string GetOutputDirectoryPath(int increment, string path)
{
var parent = Directory.GetParent(path);
var filename = Path.GetFileNameWithoutExtension(path);

View file

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

View file

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

View file

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

View file

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

View file

@ -1,15 +1,15 @@
using System.Collections.Generic;
namespace LegoArtTool
namespace LegoArtTool.LegoArtColor
{
public class Pixel
{
{
public int X { get; private set; }
public int Y { get; private set; }
public List<LegoArtColorOffset> LegoArtColorOffsets { get; private set; }
public bool IsAssigned { get; set; }
public Pixel(int x, int y, List<LegoArtColorOffset> legoArtColorOffsets)
{
X = x;
@ -17,6 +17,5 @@ namespace LegoArtTool
LegoArtColorOffsets = legoArtColorOffsets;
IsAssigned = false;
}
}
}

View file

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