Some UI stuff

This commit is contained in:
Claudio Schaad 2020-12-03 22:44:30 +01:00
parent 18f8c7efb3
commit 27c1ca6a9b
5 changed files with 51 additions and 24 deletions

View file

@ -18,8 +18,8 @@
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Height="25" VerticalAlignment="Top">
<Label Width="50" Content="Bild:"/>
<TextBox Width="300" x:Name="tbImagePath" />
<Label Width="Auto" Content="Source Image:"/>
<TextBox Width="265" x:Name="tbImagePath" />
<Button Width="50" Name="btnImageChooser" Content="..." Click="btnImageChooser_Click"/>
</StackPanel>
@ -27,10 +27,15 @@
<Button Width="200" Name="btnInstructionsPersister" Content="Save Instructions" Click="btnInstructionsPersister_Click"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Vertical" Name="parentStackPanel" VerticalAlignment="Top">
<StackPanel Grid.Row="2" Orientation="Vertical" VerticalAlignment="Top">
<Label Name="lblLegoArtSet" />
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Name="parentStackPanel">
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Vertical">
<Label Name="lblSourceImage" Visibility="Hidden">Source Image:</Label>
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="sourceImage" Stretch="None"/>
<Label Name="lblLegoArtImage" Visibility="Hidden">LEGO® Art Image:</Label>
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="legoArtImage" Stretch="None"/>
</StackPanel>

View file

@ -9,6 +9,7 @@ using System.Windows.Media;
using Rectangle = System.Windows.Shapes.Rectangle;
using LegoArtTool.LegoArtColor;
using LegoArtTool.BuildingInstruction;
using System.Diagnostics;
namespace LegoArt
{
@ -23,6 +24,8 @@ namespace LegoArt
private readonly LegoArtImageGenerationService legoArtImageGenerationService;
private readonly BuildingInstructionService buildingInstructionService;
private string currentLegoArtSet;
public MainWindow()
{
InitializeComponent();
@ -34,6 +37,7 @@ namespace LegoArt
buildingInstructionService = new BuildingInstructionService();
btnInstructionsPersister.Visibility = Visibility.Hidden;
currentLegoArtSet = legoArtColorService.LegoArtSets.First();
}
private void btnImageChooser_Click(object sender, RoutedEventArgs e)
@ -73,7 +77,7 @@ namespace LegoArt
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 buildingInstructionBitmap = buildingInstructionService.CreateBuildingInstructionImage(legoArtBitmap, legoArtColorService.GetLegoArtColorSet(currentLegoArtSet));
var outputPath = buildingInstructionService.PersistBuildingInstructions(buildingInstructionBitmap, tbImagePath.Text);
HideWaitCursor();
@ -85,14 +89,17 @@ namespace LegoArt
{
var sourceBitmap = new Bitmap(path);
sourceImage.Source = imageHelperService.LoadToImage(bitmapHelperService.Scale(sourceBitmap, 1));
lblSourceImage.Visibility = Visibility.Visible;
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(sourceBitmap);
var reducedBitMap = legoArtImageGenerationService.GenerateLegoArtImageFromFullColorImage(sourceBitmap, currentLegoArtSet);
legoArtImage.Source = imageHelperService.LoadToImage(reducedBitMap);
lblLegoArtImage.Visibility = Visibility.Visible;
var legoArtColors = legoArtColorService.ParseImage(reducedBitMap);
var legoArtColors = legoArtColorService.ParseImage(reducedBitMap, currentLegoArtSet);
if (legoArtColors != null)
{
btnInstructionsPersister.Visibility = Visibility.Visible;
lblLegoArtSet.Content = currentLegoArtSet;
parentStackPanel.Children.Clear();

View file

@ -1,4 +1,5 @@
using LegoArtTool.LegoArtColor;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
@ -26,7 +27,7 @@ namespace LegoArtTool.BuildingInstruction
{
var rectangle = new Rectangle(w * segmentSize, h * segmentSize, segmentSize, segmentSize);
var segment = bitmap.Clone(rectangle, bitmap.PixelFormat);
segment.Save($"{outPutDirectory}/{segmentNumber++}.png", ImageFormat.Png);
segment.Save($"{outPutDirectory}{Path.DirectorySeparatorChar}{segmentNumber++}.png", ImageFormat.Png);
}
}
@ -51,7 +52,7 @@ namespace LegoArtTool.BuildingInstruction
{
var parent = Directory.GetParent(path);
var filename = Path.GetFileNameWithoutExtension(path);
return increment == 0 ? $"{parent}/legoArtInstructions" : $"{parent}/legoArtInstructions_{filename} ({increment})";
return increment == 0 ? $"{parent}{Path.DirectorySeparatorChar}legoArtInstructions" : $"{parent}{Path.DirectorySeparatorChar}legoArtInstructions_{filename} ({increment})";
}
}
}

View file

@ -6,10 +6,16 @@ namespace LegoArtTool.LegoArtColor
{
public class LegoArtColorService
{
public List<LegoArtColorInfo> GetLegoColors()
{
var legoColors = new List<LegoArtColorInfo>();
public List<string> LegoArtSets { get; private set;}
private readonly Dictionary<string, List<LegoArtColorInfo>> legoArtColorSets;
public LegoArtColorService()
{
legoArtColorSets = new Dictionary<string, List<LegoArtColorInfo>>();
var key = "LEGO® Art The Beatles #31198";
var legoColors = new List<LegoArtColorInfo>();
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 0, 0), 1, 698));
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(102, 101, 96), 2, 141));
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(167, 166, 162), 3, 51));
@ -25,13 +31,20 @@ namespace LegoArtTool.LegoArtColor
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(206, 81, 0), 13, 85));
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(119, 40, 0), 14, 250));
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(66, 23, 3), 15, 554));
legoArtColorSets.Add(key, legoColors);
return legoColors;
LegoArtSets = new List<string>();
LegoArtSets.Add(key);
}
public List<LegoArtColorInfo> ParseImage(Bitmap bitmap)
public List<LegoArtColorInfo> GetLegoArtColorSet(string legoArtSetKey)
{
var parsedLegoColors = GetLegoColors();
return legoArtColorSets[legoArtSetKey];
}
public List<LegoArtColorInfo> ParseImage(Bitmap bitmap, string legoArtSetKey)
{
var parsedLegoColors = GetLegoArtColorSet(legoArtSetKey);
var width = bitmap.Width;
var height = bitmap.Height;

View file

@ -8,16 +8,17 @@ namespace LegoArtTool
{
public class LegoArtImageGenerationService
{
private readonly List<LegoArtColorInfo> legoArtColorInfos;
private readonly LegoArtColorService legoArtColorService;
public LegoArtImageGenerationService()
{
var legoArtColorService = new LegoArtColorService();
legoArtColorInfos = legoArtColorService.GetLegoColors();
legoArtColorService = new LegoArtColorService();
}
public Bitmap GenerateLegoArtImageFromFullColorImage(Bitmap bitmap)
public Bitmap GenerateLegoArtImageFromFullColorImage(Bitmap bitmap, string legoArtSetKey)
{
var legoArtColorInfos = legoArtColorService.GetLegoArtColorSet(legoArtSetKey);
var width = bitmap.Width;
var height = bitmap.Height;
@ -28,7 +29,7 @@ namespace LegoArtTool
for (int w = 0; w < width; w++)
{
var color = bitmap.GetPixel(h, w);
var pixel = MapPixel(color, h ,w);
var pixel = MapPixel(legoArtColorInfos, color, h ,w);
pixels.Add(pixel);
}
}
@ -63,7 +64,7 @@ namespace LegoArtTool
return reducedBitmap;
}
private Pixel MapPixel(Color inputColor, int xPosition, int yPosition)
private Pixel MapPixel(List<LegoArtColorInfo> legoArtColorInfos, Color inputColor, int xPosition, int yPosition)
{
var legoArtColorOffsets = legoArtColorInfos.Select(i => ColorDistance(inputColor, i)).ToList();
return new Pixel(xPosition, yPosition, legoArtColorOffsets);