Skip to content

Commit b32f62f

Browse files
author
Juan Segura
committed
2 parents 1d55129 + d510524 commit b32f62f

File tree

11 files changed

+1350
-21
lines changed

11 files changed

+1350
-21
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
using Avalonia;
2+
using Avalonia.Controls;
3+
using Avalonia.Input;
4+
using Avalonia.Media;
5+
using Avalonia.Media.Imaging;
6+
using Avalonia.Platform;
7+
using Avalonia.Platform.Storage;
8+
using Avalonia.Threading;
9+
using SixLabors.ImageSharp.Formats.Png;
10+
using SixLabors.ImageSharp;
11+
using SixLabors.ImageSharp.PixelFormats;
12+
using SixLabors.ImageSharp.Processing.Processors.Quantization;
13+
using System;
14+
using System.Collections.Generic;
15+
using System.IO;
16+
using System.Linq;
17+
using System.Net;
18+
using System.Runtime.Intrinsics.X86;
19+
using System.Text;
20+
using System.Threading;
21+
using System.Threading.Tasks;
22+
using ZXBasicStudio.DocumentEditors.ZXGraphics.neg;
23+
using ZXBasicStudio.Common;
24+
using SixLabors.ImageSharp.Formats.Bmp;
25+
26+
namespace ZXBasicStudio.DocumentEditors.ZXGraphics
27+
{
28+
/// <summary>
29+
/// Control to show and manipulate imported image
30+
/// Fixed size to 400x400
31+
/// </summary>
32+
public class NextImageViewControl : Control
33+
{
34+
/// <summary>
35+
/// Zoom factor
36+
/// </summary>
37+
public int Zoom
38+
{
39+
get
40+
{
41+
return _Zoom;
42+
}
43+
set
44+
{
45+
_Zoom = value;
46+
this.InvalidateVisual();
47+
}
48+
}
49+
private int _Zoom = 1;
50+
51+
/// <summary>
52+
/// Import image data
53+
/// </summary>
54+
public SixLabors.ImageSharp.Image<Rgba32> imageData = null;
55+
56+
public NextImageViewControl()
57+
{
58+
}
59+
60+
61+
public async void LoadImage(IStorageFile file)
62+
{
63+
try
64+
{
65+
await using (var stream = await file.OpenReadAsync())
66+
{
67+
imageData = SixLabors.ImageSharp.Image.Load<Rgba32>(stream);
68+
}
69+
if (imageData.Width > 320)
70+
{
71+
this.Width = 320;
72+
}
73+
else
74+
{
75+
this.Width = imageData.Width;
76+
}
77+
if (this.imageData.Height > 256)
78+
{
79+
this.Height = 256;
80+
}
81+
else
82+
{
83+
this.Height = imageData.Height;
84+
}
85+
86+
}
87+
catch (Exception ex)
88+
{
89+
}
90+
}
91+
92+
93+
public void Refresh()
94+
{
95+
this.InvalidateVisual();
96+
}
97+
98+
99+
public override void Render(DrawingContext context)
100+
{
101+
try
102+
{
103+
base.Render(context);
104+
105+
// Image
106+
if (imageData == null)
107+
{
108+
return;
109+
}
110+
int iw = imageData.Size.Width;
111+
int ih = imageData.Size.Height;
112+
int cw = 0;
113+
int ch = 0;
114+
int z = _Zoom;
115+
int maxWidth = (int)this.Width;
116+
int maxHeight = (int)this.Height;
117+
118+
int yd = 0;
119+
for (int y = 0; y < ih; y += z)
120+
{
121+
int xd = 0;
122+
for (int x = 0; x < iw; x += z)
123+
{
124+
if (xd >= 0 && xd < iw &&
125+
yd >= 0 && yd < ih)
126+
{
127+
try
128+
{
129+
int xx = x;
130+
int yy = y;
131+
132+
if ((xx + z) > iw)
133+
{
134+
cw = iw - xx;
135+
}
136+
else
137+
{
138+
cw = z;
139+
}
140+
if ((yy + z) > ih)
141+
{
142+
ch = ih - yy;
143+
}
144+
else
145+
{
146+
ch = z;
147+
}
148+
if (xx >= 0 && xx <= maxWidth && yy >= 0 && yy <= maxHeight)
149+
{
150+
Rect r = new Rect(xx, yy, cw, ch);
151+
var pixel = imageData[xd, yd];
152+
var brush = new SolidColorBrush(Avalonia.Media.Color.FromArgb(
153+
255, pixel.R, pixel.G, pixel.B));
154+
context.FillRectangle(brush, r);
155+
}
156+
}
157+
catch (Exception ex)
158+
{
159+
160+
}
161+
}
162+
xd++;
163+
}
164+
yd++;
165+
}
166+
167+
}
168+
catch (Exception ex)
169+
{
170+
171+
}
172+
}
173+
174+
175+
public void SetImageData(int[,] pixels, PaletteColor[] palette, int w, int h)
176+
{
177+
imageData = new Image<Rgba32>(w, h);
178+
for (int x = 0; x < w; x++)
179+
{
180+
for (int y = 0; y < h; y++)
181+
{
182+
var p = palette[pixels[x, y]];
183+
imageData[x, y] = new Rgba32(p.Red, p.Green, p.Blue);
184+
}
185+
}
186+
this.InvalidateArrange();
187+
}
188+
189+
190+
public bool SaveImageAsPng(string filename)
191+
{
192+
try
193+
{
194+
imageData.SaveAsPng(filename);
195+
return true;
196+
}
197+
catch (Exception ex)
198+
{
199+
return false;
200+
}
201+
}
202+
}
203+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<Window xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:local="using:ZXBasicStudio.DocumentEditors.ZXGraphics"
6+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="501"
7+
Width="950" Height="520"
8+
Icon="/Assets/zxbs.ico"
9+
x:Class="ZXBasicStudio.DocumentEditors.ZXGraphics.PaletteBuilderDialog"
10+
Title="Next Palette builder" CanResize="False"
11+
WindowStartupLocation="CenterOwner"
12+
DataContext="{Binding PaletteViewModel}">
13+
14+
<Grid Name="grdMain" ColumnDefinitions="330,5,256,5,330" RowDefinitions="Auto,260,Auto,Auto,Auto" Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
15+
16+
<TextBlock FontSize="18" FontWeight="Bold">Source image</TextBlock>
17+
<TextBlock Grid.Column="2" FontSize="18" FontWeight="Bold">Palette</TextBlock>
18+
<TextBlock Grid.Column="4" FontSize="18" FontWeight="Bold">Converted image</TextBlock>
19+
20+
<Border Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" BorderBrush="Red">
21+
<Grid Width="320" Height="256" Grid.Row="1" Name="grdSource" HorizontalAlignment="Center" VerticalAlignment="Center">
22+
<local:NextImageViewControl Name="imgSource"/>
23+
</Grid>
24+
</Border>
25+
26+
<Grid Grid.Row="1" Grid.Column="2" Grid.RowDefinitions="Auto,Auto,Auto">
27+
<Canvas Grid.ColumnSpan="2" Name="cnvPalette" />
28+
</Grid>
29+
30+
<Border Grid.Row="1" Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" BorderBrush="Red">
31+
<Grid Width="320" Height="256" Grid.Row="1" Grid.Column="2" Name="grdConverted" HorizontalAlignment="Center" VerticalAlignment="Center">
32+
<local:NextImageViewControl Name="imgConverted"/>
33+
</Grid>
34+
</Border>
35+
36+
37+
<Grid Grid.Row="2" Grid.ColumnDefinitions="Auto,*,Auto" Grid.RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto">
38+
<Button Grid.ColumnSpan="2" Name="btnFileSource" Classes="dialog" Width="150" HorizontalAlignment="Left">Load source image</Button>
39+
40+
<TextBlock Grid.Row="3" Classes="dialog" VerticalAlignment="Center">OutFormat</TextBlock>
41+
<ComboBox Grid.Row="3" Grid.Column="1" Name="cmbMode" Classes="dialog" HorizontalAlignment="Left" MaxWidth="Infinity" Margin="4,0,0,2" SelectedIndex="0" Focusable="False">
42+
<ComboBoxItem>256x192 256 colors with palette</ComboBoxItem>
43+
<ComboBoxItem>256x192 256 colors without palette</ComboBoxItem>
44+
</ComboBox>
45+
</Grid>
46+
47+
<Grid Grid.Row="3" Grid.ColumnDefinitions="Auto,*" Grid.RowDefinitions="Auto,Auto,Auto,Auto,Auto">
48+
<CheckBox Grid.Row="0" Name="chkUseCurrent" IsChecked="True" HorizontalAlignment="Right"/>
49+
<TextBlock Grid.Row="0" Grid.Column="1" Classes="dialog" VerticalAlignment="Center">Convert to current palette</TextBlock>
50+
51+
<CheckBox Grid.Row="1" Name="chkAppend" IsChecked="False" HorizontalAlignment="Right"/>
52+
<TextBlock Grid.Row="1" Grid.Column="1" Classes="dialog" VerticalAlignment="Center">Append new colors to current palette</TextBlock>
53+
54+
<CheckBox Grid.Row="2" Name="chkULA" IsChecked="False" IsEnabled="false" HorizontalAlignment="Right"/>
55+
<TextBlock Grid.Row="2" Grid.Column="1" Classes="dialog" VerticalAlignment="Center">Add ULA colors at start</TextBlock>
56+
57+
<CheckBox Grid.Row="3" Name="chkGrayscale" IsChecked="False" HorizontalAlignment="Right" IsEnabled="false"/>
58+
<TextBlock Grid.Row="3" Grid.Column="1" Classes="dialog" VerticalAlignment="Center">Add Grayscale at start</TextBlock>
59+
</Grid>
60+
61+
<Grid Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" RowDefinitions="Auto,Auto,Auto,Auto,5,Auto" ColumnDefinitions="Auto,100,Auto,*">
62+
<TextBlock Grid.ColumnSpan="3" Name="txtSelectedColor"/>
63+
<TextBlock Grid.Row="1" VerticalAlignment="Center">Red</TextBlock>
64+
<Slider Grid.Row="1" Grid.Column="1" Name="sldRed" Width="100" SmallChange="36" Minimum="0" Maximum="255" HorizontalAlignment="Left" VerticalAlignment="Center"/>
65+
<Border Grid.Row="1" Grid.Column="2" Classes="numericborder">
66+
<NumericUpDown Classes="dialog" Name="txtRed" Minimum="0" Increment="36" Maximum="255" Width="60" VerticalAlignment="Center" Value="0"/>
67+
</Border>
68+
69+
<TextBlock Grid.Row="2" VerticalAlignment="Center">Green</TextBlock>
70+
<Slider Grid.Row="2" Grid.Column="1" Name="sldGreen" Width="100" SmallChange="36" Minimum="0" Maximum="255" HorizontalAlignment="Left" VerticalAlignment="Center"/>
71+
<Border Grid.Row="2" Grid.Column="2" Classes="numericborder">
72+
<NumericUpDown Classes="dialog" Name="txtGreen" Minimum="0" Increment="36" Maximum="255" Width="60" VerticalAlignment="Center" Value="0"/>
73+
</Border>
74+
75+
<TextBlock Grid.Row="3" VerticalAlignment="Center">Blue</TextBlock>
76+
<Slider Grid.Row="3" Grid.Column="1" Name="sldBlue" Width="100" SmallChange="36" Minimum="0" Maximum="255" HorizontalAlignment="Left" VerticalAlignment="Center"/>
77+
<Border Grid.Row="3" Grid.Column="2" Classes="numericborder">
78+
<NumericUpDown Classes="dialog" Name="txtBlue" Minimum="0" Increment="36" Maximum="255" Width="60" VerticalAlignment="Center" Value="0"/>
79+
</Border>
80+
81+
<Border Grid.Row="1" Grid.Column="3" Grid.RowSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" BorderThickness="1" BorderBrush="Red">
82+
<Grid Name="grdSelectedColor" Width="50" Height="50"></Grid>
83+
</Border>
84+
85+
<StackPanel Grid.Row="5" Grid.ColumnSpan="4" Orientation="Horizontal">
86+
<Button Name="btnResetPalette" Classes="dialog">Reset to default</Button>
87+
<Button Name="btnLoadPalette" Classes="dialog">Load palette</Button>
88+
<Button Name="btnSavePalette" Classes="dialog">Save palette</Button>
89+
</StackPanel>
90+
</Grid>
91+
92+
<Grid Grid.Row="2" Grid.Column="4" Grid.RowDefinitions="Auto,5,Auto">
93+
<Button Name="btnRefresh" Classes="dialog" HorizontalAlignment="Right">Refresh</Button>
94+
<Button Grid.Row="2" Name="btnSaveImage" Classes="dialog" HorizontalAlignment="Right">Save image</Button>
95+
</Grid>
96+
97+
<StackPanel Grid.Row="4" Grid.ColumnSpan="5" Orientation="Horizontal" HorizontalAlignment="Right">
98+
<Button Name="btnClose" Classes="dialog">Close</Button>
99+
</StackPanel>
100+
101+
</Grid>
102+
</Window>

0 commit comments

Comments
 (0)