From c47beffcee45b165fd8e1e8c1099d686ad672dd7 Mon Sep 17 00:00:00 2001
From: Andy McFadden
Date: Sat, 20 Jun 2020 17:32:57 -0700
Subject: [PATCH] Add Export feature to visualization editor
It's nice to be able to save images from the visualization editor
for display elsewhere. This can be done during HTML export, but
that's inconvenient when you just want one image, and doesn't allow
the output size to be specified.
This change adds an Export button to the Edit Visualization dialog.
The current bitmap, wireframe, or wireframe animation can be saved
to a GIF image. A handful of sizes can be selected from a pop-up
menu.
---
CommonWPF/WPFExtensions.cs | 36 +++-
SourceGen/Res/Strings.xaml | 1 +
SourceGen/Res/Strings.xaml.cs | 2 +
SourceGen/RuntimeData/Help/visualization.html | 3 +
SourceGen/SourceGen.csproj | 7 +
SourceGen/Visualization.cs | 3 +-
SourceGen/WpfGui/EditVisualization.xaml | 4 +
SourceGen/WpfGui/EditVisualization.xaml.cs | 30 ++-
SourceGen/WpfGui/ExportVisualization.xaml | 60 ++++++
SourceGen/WpfGui/ExportVisualization.xaml.cs | 175 ++++++++++++++++++
10 files changed, 312 insertions(+), 9 deletions(-)
create mode 100644 SourceGen/WpfGui/ExportVisualization.xaml
create mode 100644 SourceGen/WpfGui/ExportVisualization.xaml.cs
diff --git a/CommonWPF/WPFExtensions.cs b/CommonWPF/WPFExtensions.cs
index 890b4b9..27a7194 100644
--- a/CommonWPF/WPFExtensions.cs
+++ b/CommonWPF/WPFExtensions.cs
@@ -17,10 +17,10 @@ using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
-using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
+using System.Windows.Media.Imaging;
namespace CommonWPF {
///
@@ -263,4 +263,38 @@ namespace CommonWPF {
}
}
}
+
+ ///
+ /// BitmapSource extensions.
+ ///
+ public static class BitmapSourceExtensions {
+ ///
+ /// Creates a scaled copy of a BitmapSource. Only scales up, using nearest-neighbor.
+ ///
+ public static BitmapSource CreateScaledCopy(this BitmapSource src, int scale) {
+ // Simple approach always does a "blurry" scale.
+ //return new TransformedBitmap(src, new ScaleTransform(scale, scale));
+
+ // Adapted from https://weblogs.asp.net/bleroy/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi
+ // (found via https://stackoverflow.com/a/25570225/294248)
+ BitmapScalingMode scalingMode = BitmapScalingMode.NearestNeighbor;
+
+ int newWidth = (int)src.Width * scale;
+ int newHeight = (int)src.Height * scale;
+
+ var group = new DrawingGroup();
+ RenderOptions.SetBitmapScalingMode(group, scalingMode);
+ group.Children.Add(new ImageDrawing(src,
+ new Rect(0, 0, newWidth, newHeight)));
+ var targetVisual = new DrawingVisual();
+ var targetContext = targetVisual.RenderOpen();
+ targetContext.DrawDrawing(group);
+ var target = new RenderTargetBitmap(
+ newWidth, newHeight, 96, 96, PixelFormats.Default);
+ targetContext.Close();
+ target.Render(targetVisual);
+ var targetFrame = BitmapFrame.Create(target);
+ return targetFrame;
+ }
+ }
}
diff --git a/SourceGen/Res/Strings.xaml b/SourceGen/Res/Strings.xaml
index 8fd9a64..ebda1b4 100644
--- a/SourceGen/Res/Strings.xaml
+++ b/SourceGen/Res/Strings.xaml
@@ -89,6 +89,7 @@ limitations under the License.
C# Source Files (*.cs)|*.csCSV files (*.csv)|*.csvSourceGen projects (*.dis65)|*.dis65
+ GIF images (*.gif)|*.gifHTML files (*.html)|*.htmlSGEC files (*.sgec)|*.sgecSourceGen symbols (*.sym65)|*.sym65
diff --git a/SourceGen/Res/Strings.xaml.cs b/SourceGen/Res/Strings.xaml.cs
index b4be3ce..a427e24 100644
--- a/SourceGen/Res/Strings.xaml.cs
+++ b/SourceGen/Res/Strings.xaml.cs
@@ -159,6 +159,8 @@ namespace SourceGen.Res {
(string)Application.Current.FindResource("str_FileFilterCsv");
public static string FILE_FILTER_DIS65 =
(string)Application.Current.FindResource("str_FileFilterDis65");
+ public static string FILE_FILTER_GIF =
+ (string)Application.Current.FindResource("str_FileFilterGif");
public static string FILE_FILTER_HTML =
(string)Application.Current.FindResource("str_FileFilterHtml");
public static string FILE_FILTER_SGEC =
diff --git a/SourceGen/RuntimeData/Help/visualization.html b/SourceGen/RuntimeData/Help/visualization.html
index 7f1b08e..82f6788 100644
--- a/SourceGen/RuntimeData/Help/visualization.html
+++ b/SourceGen/RuntimeData/Help/visualization.html
@@ -101,6 +101,9 @@ the former a text entry field that accepts decimal and hexadecimal values.
The range of allowable values is shown to the right of the entry field.
If you enter an invalid value, the parameter description will turn red.
+
The "Export" button at the top right can be used to save a copy of
+the bitmap or wireframe rendering with the current parameters.
+
Wireframe View Controls
The wireframe generator may offer the choice of perspective vs.
diff --git a/SourceGen/SourceGen.csproj b/SourceGen/SourceGen.csproj
index 76891de..16d0f7d 100644
--- a/SourceGen/SourceGen.csproj
+++ b/SourceGen/SourceGen.csproj
@@ -155,6 +155,9 @@
Export.xaml
+
+ ExportVisualization.xaml
+ FindBox.xaml
@@ -357,6 +360,10 @@
DesignerMSBuild:Compile
+
+ Designer
+ MSBuild:Compile
+ DesignerMSBuild:Compile
diff --git a/SourceGen/Visualization.cs b/SourceGen/Visualization.cs
index 137e40c..5a46eed 100644
--- a/SourceGen/Visualization.cs
+++ b/SourceGen/Visualization.cs
@@ -17,11 +17,10 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
-using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
-using System.Windows.Shapes;
+
using CommonUtil;
using PluginCommon;
diff --git a/SourceGen/WpfGui/EditVisualization.xaml b/SourceGen/WpfGui/EditVisualization.xaml
index 35ac87a..939d8f6 100644
--- a/SourceGen/WpfGui/EditVisualization.xaml
+++ b/SourceGen/WpfGui/EditVisualization.xaml
@@ -105,6 +105,7 @@ limitations under the License.
+
@@ -119,6 +120,9 @@ limitations under the License.
HorizontalAlignment="Left"
ItemsSource="{Binding VisualizationList}" DisplayMemberPath="VisDescriptor.UiName"
SelectionChanged="VisComboBox_SelectionChanged"/>
+
diff --git a/SourceGen/WpfGui/EditVisualization.xaml.cs b/SourceGen/WpfGui/EditVisualization.xaml.cs
index 374e1c8..3eb89b9 100644
--- a/SourceGen/WpfGui/EditVisualization.xaml.cs
+++ b/SourceGen/WpfGui/EditVisualization.xaml.cs
@@ -374,6 +374,24 @@ namespace SourceGen.WpfGui {
}
private void OkButton_Click(object sender, RoutedEventArgs e) {
+ NewVis = PrepResultVis();
+ sLastVisIdent = NewVis.VisGenIdent;
+ DialogResult = true;
+ }
+
+ private void ExportButton_Click(object sender, RoutedEventArgs e) {
+ Visualization vis = PrepResultVis();
+ ExportVisualization dlg = new ExportVisualization(this, vis, mWireObj,
+ "vis" + mSetOffset.ToString("x6")); // tag may not be valid filename, use offset
+ dlg.ShowDialog();
+ }
+
+ ///
+ /// Generates the visualization that we will return as the result object, based on
+ /// the currents state of the controls.
+ ///
+ /// New Visualization or VisWireframeAnimation object.
+ private Visualization PrepResultVis() {
VisualizationItem item = (VisualizationItem)visComboBox.SelectedItem;
Debug.Assert(item != null);
@@ -384,25 +402,25 @@ namespace SourceGen.WpfGui {
string trimTag = Visualization.TrimAndValidateTag(TagString, out bool isTagValid);
Debug.Assert(isTagValid);
+ Visualization vis;
if (isWireframe && IsWireframeAnimated) {
- NewVis = new VisWireframeAnimation(trimTag, item.VisDescriptor.Ident, valueDict,
+ vis = new VisWireframeAnimation(trimTag, item.VisDescriptor.Ident, valueDict,
mOrigVis, mWireObj);
} else {
- NewVis = new Visualization(trimTag, item.VisDescriptor.Ident, valueDict, mOrigVis);
+ vis = new Visualization(trimTag, item.VisDescriptor.Ident, valueDict, mOrigVis);
}
// Set the thumbnail image.
if (isWireframe) {
Debug.Assert(mWireObj != null);
- NewVis.CachedImage = Visualization.GenerateWireframeImage(mWireObj,
+ vis.CachedImage = Visualization.GenerateWireframeImage(mWireObj,
Visualization.THUMBNAIL_DIM, valueDict);
} else {
Debug.Assert(mThumbnail != null);
- NewVis.CachedImage = mThumbnail;
+ vis.CachedImage = mThumbnail;
}
- sLastVisIdent = NewVis.VisGenIdent;
- DialogResult = true;
+ return vis;
}
private ReadOnlyDictionary CreateVisGenParams(bool includeWire) {
diff --git a/SourceGen/WpfGui/ExportVisualization.xaml b/SourceGen/WpfGui/ExportVisualization.xaml
new file mode 100644
index 0000000..dec939e
--- /dev/null
+++ b/SourceGen/WpfGui/ExportVisualization.xaml
@@ -0,0 +1,60 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/SourceGen/WpfGui/ExportVisualization.xaml.cs b/SourceGen/WpfGui/ExportVisualization.xaml.cs
new file mode 100644
index 0000000..307a8c4
--- /dev/null
+++ b/SourceGen/WpfGui/ExportVisualization.xaml.cs
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2020 faddenSoft
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.IO;
+using System.Runtime.CompilerServices;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+
+using CommonWPF;
+using Microsoft.Win32;
+
+namespace SourceGen.WpfGui {
+ ///
+ /// Export an image from the visualization editor.
+ ///
+ public partial class ExportVisualization : Window, INotifyPropertyChanged {
+ private Visualization mVis;
+ private WireframeObject mWireObj;
+ private string mFileNameBase;
+
+ // INotifyPropertyChanged implementation
+ public event PropertyChangedEventHandler PropertyChanged;
+ private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+
+ public bool IsBitmap {
+ get { return mIsBitmap; }
+ set { mIsBitmap = value; OnPropertyChanged(); }
+ }
+ public bool IsWireframe {
+ get { return !mIsBitmap; }
+ }
+ private bool mIsBitmap;
+
+ ///
+ /// Item for output size combo box.
+ ///
+ public class OutputSize {
+ public int Width { get; private set; }
+ public int Height { get; private set; }
+ public override string ToString() {
+ return Width + "x" + Height;
+ }
+
+ public OutputSize(int width, int height) {
+ Width = width;
+ Height = height;
+ }
+ }
+
+ ///
+ /// List of output sizes, for combo box.
+ ///
+ public List OutputSizeList { get; private set; }
+
+
+ public ExportVisualization(Window owner, Visualization vis, WireframeObject wireObj,
+ string fileNameBase) {
+ InitializeComponent();
+ Owner = owner;
+ DataContext = this;
+
+ mVis = vis;
+ mWireObj = wireObj;
+ mFileNameBase = fileNameBase;
+
+ OutputSizeList = new List();
+
+ // Normally, bitmap and wireframe visualizations don't really differ, because
+ // we're just working off the cached image rendering. It matters for us though,
+ // so we need to see if a wireframe-only parameter exists.
+ bool isWireframe = (vis is VisWireframeAnimation) ||
+ vis.VisGenParams.ContainsKey(VisWireframeAnimation.P_IS_ANIMATED);
+ IsBitmap = !isWireframe;
+
+ if (isWireframe) {
+ int dim = 64;
+ while (dim <= 1024) {
+ OutputSizeList.Add(new OutputSize(dim, dim));
+ dim *= 2;
+ }
+ } else {
+ int baseWidth = (int)vis.CachedImage.Width;
+ int baseHeight = (int)vis.CachedImage.Height;
+ // ensure there's at least one entry, then add other options
+ OutputSizeList.Add(new OutputSize(baseWidth, baseHeight));
+ int mult = 2;
+ while (baseWidth * mult < 2048 && baseHeight * mult < 2048) {
+ OutputSizeList.Add(new OutputSize(baseWidth * mult, baseHeight * mult));
+ mult *= 2;
+ }
+ }
+
+ sizeComboBox.SelectedIndex = 0;
+ }
+
+ private void SaveButton_Click(object sender, RoutedEventArgs e) {
+ SaveFileDialog fileDlg = new SaveFileDialog() {
+ Filter = Res.Strings.FILE_FILTER_GIF + "|" + Res.Strings.FILE_FILTER_ALL,
+ FilterIndex = 1,
+ ValidateNames = true,
+ AddExtension = true,
+ FileName = mFileNameBase + ".gif"
+ };
+ if (fileDlg.ShowDialog() != true) {
+ return;
+ }
+ string pathName = Path.GetFullPath(fileDlg.FileName);
+ Debug.WriteLine("Save path: " + pathName);
+
+ try {
+ OutputSize item = (OutputSize)sizeComboBox.SelectedItem;
+ if (mVis is VisWireframeAnimation) {
+ Debug.Assert(item.Width == item.Height);
+ AnimatedGifEncoder encoder = new AnimatedGifEncoder();
+ ((VisWireframeAnimation)mVis).EncodeGif(encoder, item.Width);
+
+ using (FileStream stream = new FileStream(pathName, FileMode.Create)) {
+ encoder.Save(stream, out int dispWidth, out int dispHeight);
+ }
+ } else {
+ BitmapSource outImage;
+ if (IsBitmap) {
+ int scale = item.Width / (int)mVis.CachedImage.Width;
+ Debug.Assert(scale >= 1);
+ if (scale == 1) {
+ outImage = mVis.CachedImage;
+ } else {
+ outImage = mVis.CachedImage.CreateScaledCopy(scale);
+ }
+ } else {
+ Debug.Assert(item.Width == item.Height);
+ outImage = Visualization.GenerateWireframeImage(mWireObj,
+ item.Width, mVis.VisGenParams);
+ }
+
+ GifBitmapEncoder encoder = new GifBitmapEncoder();
+ encoder.Frames.Add(BitmapFrame.Create(outImage));
+
+ using (FileStream stream = new FileStream(pathName, FileMode.Create)) {
+ encoder.Save(stream);
+ }
+ }
+ } catch (Exception ex) {
+ // Error handling is a little sloppy, but this shouldn't fail often.
+ MessageBox.Show(ex.Message, Res.Strings.ERR_FILE_GENERIC_CAPTION,
+ MessageBoxButton.OK, MessageBoxImage.Error);
+ return;
+ }
+
+ // After successful save, close dialog box.
+ DialogResult = true;
+ }
+ }
+}