2019-11-25 22:27:38 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 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.Text;
|
2019-11-28 01:12:26 +00:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2019-11-27 02:54:42 +00:00
|
|
|
|
using CommonUtil;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
using PluginCommon;
|
|
|
|
|
|
|
|
|
|
namespace SourceGen {
|
|
|
|
|
public class Visualization {
|
|
|
|
|
/// <summary>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
/// Unique user-specified tag. Contents are arbitrary, but may not be empty.
|
2019-11-25 22:27:38 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public string Tag { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Name of visualization generator (extension script function).
|
|
|
|
|
/// </summary>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
public string VisGenIdent { get; private set; }
|
2019-11-25 22:27:38 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Parameters passed to the visualization generator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Dictionary<string, object> VisGenParams { get; private set; }
|
|
|
|
|
|
|
|
|
|
public double Thumbnail { get; } // TODO - 64x64(?) bitmap
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tag"></param>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
/// <param name="visGenIdent"></param>
|
2019-11-25 22:27:38 +00:00
|
|
|
|
/// <param name="visGenParams"></param>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
public Visualization(string tag, string visGenIdent,
|
2019-11-25 22:27:38 +00:00
|
|
|
|
Dictionary<string, object> visGenParams) {
|
|
|
|
|
Tag = tag;
|
2019-11-27 02:54:42 +00:00
|
|
|
|
VisGenIdent = visGenIdent;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
VisGenParams = visGenParams;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 01:12:26 +00:00
|
|
|
|
public static BitmapSource CreateBitmapSource(IVisualization2d vis2d) {
|
|
|
|
|
// Create indexed color palette.
|
|
|
|
|
int[] intPal = vis2d.GetPalette();
|
|
|
|
|
List<Color> colors = new List<Color>(intPal.Length);
|
|
|
|
|
foreach (int argb in intPal) {
|
|
|
|
|
Color col = Color.FromArgb((byte)(argb >> 24), (byte)(argb >> 16),
|
|
|
|
|
(byte)(argb >> 8), (byte)argb);
|
|
|
|
|
colors.Add(col);
|
|
|
|
|
}
|
|
|
|
|
BitmapPalette palette = new BitmapPalette(colors);
|
|
|
|
|
|
|
|
|
|
// indexed-color; see https://stackoverflow.com/a/15272528/294248 for direct color
|
|
|
|
|
BitmapSource image = BitmapSource.Create(
|
|
|
|
|
vis2d.Width,
|
|
|
|
|
vis2d.Height,
|
|
|
|
|
96.0,
|
|
|
|
|
96.0,
|
|
|
|
|
PixelFormats.Indexed8,
|
|
|
|
|
palette,
|
|
|
|
|
vis2d.GetPixels(),
|
|
|
|
|
vis2d.Width);
|
|
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
}
|
2019-11-25 22:27:38 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds a plugin that provides the named visualization generator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="proj">Project with script manager.</param>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
/// <param name="visGenIdent">Visualization generator identifier.</param>
|
2019-11-25 22:27:38 +00:00
|
|
|
|
/// <returns>A plugin that matches, or null if none found.</returns>
|
2019-11-27 02:54:42 +00:00
|
|
|
|
public static IPlugin_Visualizer FindPluginByVisGenIdent(DisasmProject proj,
|
|
|
|
|
string visGenIdent, out VisDescr visDescr) {
|
|
|
|
|
List<IPlugin> plugins = proj.GetActivePlugins();
|
|
|
|
|
foreach (IPlugin chkPlug in plugins) {
|
|
|
|
|
if (!(chkPlug is IPlugin_Visualizer)) {
|
|
|
|
|
continue;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
2019-11-27 02:54:42 +00:00
|
|
|
|
IPlugin_Visualizer vplug = (IPlugin_Visualizer)chkPlug;
|
|
|
|
|
foreach (VisDescr descr in vplug.GetVisGenDescrs()) {
|
|
|
|
|
if (descr.Ident == visGenIdent) {
|
|
|
|
|
visDescr = descr;
|
|
|
|
|
return vplug;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-27 02:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
visDescr = null;
|
|
|
|
|
return null;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
2019-11-27 02:54:42 +00:00
|
|
|
|
return "[Vis: " + Tag + " (" + VisGenIdent + ") count=" + VisGenParams.Count + "]";
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator ==(Visualization a, Visualization b) {
|
|
|
|
|
if (ReferenceEquals(a, b)) {
|
|
|
|
|
return true; // same object, or both null
|
|
|
|
|
}
|
|
|
|
|
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) {
|
|
|
|
|
return false; // one is null
|
|
|
|
|
}
|
|
|
|
|
// All fields must be equal.
|
2019-11-27 02:54:42 +00:00
|
|
|
|
if (a.Tag != b.Tag || a.VisGenIdent != b.VisGenIdent || a.Thumbnail != b.Thumbnail) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Compare the vis gen parameter lists.
|
|
|
|
|
if (a.VisGenParams == b.VisGenParams) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (a.VisGenParams.Count != b.VisGenParams.Count) {
|
2019-11-25 22:27:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-11-27 02:54:42 +00:00
|
|
|
|
return Container.CompareDicts(a.VisGenParams, b.VisGenParams);
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
public static bool operator !=(Visualization a, Visualization b) {
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
public override bool Equals(object obj) {
|
|
|
|
|
return obj is Visualization && this == (Visualization)obj;
|
|
|
|
|
}
|
|
|
|
|
public override int GetHashCode() {
|
2019-11-27 02:54:42 +00:00
|
|
|
|
// TODO(maybe): hash code should include up VisGenParams items
|
|
|
|
|
return Tag.GetHashCode() ^ VisGenIdent.GetHashCode() ^ VisGenParams.Count;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|