/// <summary> /// Converts an IVisualization2d to a BitmapSource for display. The bitmap will be /// the same size as the original content. /// </summary> public static BitmapSource ConvertToBitmapSource(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); image.Freeze(); return(image); }
/// <summary> /// Updates the cached thumbnail image. /// </summary> /// <param name="vis2d">Visualization, or null to clear the thumbnail.</param> public void SetThumbnail(IVisualization2d vis2d) { if (vis2d == null) { CachedImage = BROKEN_IMAGE; } else { CachedImage = ConvertToBitmapSource(vis2d); } }
/// <summary> /// Attempts to refresh broken thumbnails across all visualization sets in the project. /// </summary> /// <param name="project">Project reference.</param> public static void RefreshAllThumbnails(DisasmProject project) { ScriptSupport iapp = null; Dictionary <string, IPlugin> plugins = null; SortedList <int, VisualizationSet> visSets = project.VisualizationSets; foreach (KeyValuePair <int, VisualizationSet> kvp in visSets) { VisualizationSet visSet = kvp.Value; foreach (Visualization vis in visSet) { if (vis.HasImage) { continue; } //Debug.WriteLine("Vis needs refresh: " + vis.Tag); if (vis is VisBitmapAnimation) { continue; } if (iapp == null) { // Prep the plugins on first need. iapp = new ScriptSupport(); project.PrepareScripts(iapp); } iapp.MsgTag = vis.Tag; if (plugins == null) { plugins = project.GetActivePlugins(); } IPlugin_Visualizer vplug = FindPluginByVisGenIdent(plugins, vis.VisGenIdent, out VisDescr visDescr); if (vplug == null) { Debug.WriteLine("Unable to refresh " + vis.Tag + ": plugin not found"); continue; } IVisualization2d vis2d = null; IVisualizationWireframe visWire = null; ReadOnlyDictionary <string, object> parms = new ReadOnlyDictionary <string, object>(vis.VisGenParams); try { if (visDescr.VisualizationType == VisDescr.VisType.Bitmap) { vis2d = vplug.Generate2d(visDescr, parms); if (vis2d == null) { Debug.WriteLine("Vis2d generator returned null"); } } else if (visDescr.VisualizationType == VisDescr.VisType.Wireframe) { IPlugin_Visualizer_v2 plugin2 = (IPlugin_Visualizer_v2)vplug; visWire = plugin2.GenerateWireframe(visDescr, parms); if (visWire == null) { Debug.WriteLine("VisWire generator returned null"); } } else { Debug.Assert(false); } } catch (Exception ex) { Debug.WriteLine("Vis generation failed: " + ex); } if (vis2d != null) { //Debug.WriteLine(" Rendered thumbnail: " + vis.Tag); vis.SetThumbnail(vis2d); } else if (visWire != null) { vis.SetThumbnail(visWire, parms); } } } if (iapp != null) { project.UnprepareScripts(); } // Now that we've generated images for the Visualizations, update any // VisBitmapAnimation thumbnails that may have been affected. foreach (KeyValuePair <int, VisualizationSet> kvp in visSets) { VisualizationSet visSet = kvp.Value; foreach (Visualization vis in visSet) { if (!(vis is VisBitmapAnimation)) { continue; } VisBitmapAnimation visAnim = (VisBitmapAnimation)vis; visAnim.GenerateImage(visSets); } } }
private void UpdateControls() { IsValid = true; foreach (ParameterValue pv in ParameterList) { pv.ForegroundBrush = mDefaultLabelColor; if (pv.Descr.CsType == typeof(bool)) { // always fine continue; } else if (pv.Descr.CsType == typeof(int)) { // integer, possibly Offset special bool ok = ParseIntObj(pv.Value, pv.Descr.Special, out int intVal); if (ok && (intVal < (int)pv.Descr.Min || intVal > (int)pv.Descr.Max)) { // TODO(someday): make the range text red instead of the label ok = false; } if (!ok) { pv.ForegroundBrush = mErrorLabelColor; IsValid = false; } } else if (pv.Descr.CsType == typeof(float)) { // float bool ok = ParseFloatObj(pv.Value, out float floatVal); if (ok && (floatVal < (float)pv.Descr.Min || floatVal > (float)pv.Descr.Max)) { ok = false; } if (!ok) { pv.ForegroundBrush = mErrorLabelColor; IsValid = false; } } else { // unexpected Debug.Assert(false); } } VisualizationItem item = (VisualizationItem)visComboBox.SelectedItem; BitmapDimensions = "?"; previewGrid.Background = null; wireframePath.Data = new GeometryGroup(); if (!IsValid || item == null) { previewImage.Source = sBadParamsImage; } else { // Invoke the plugin. PluginErrMessage = string.Empty; IVisualization2d vis2d = null; IVisualizationWireframe visWire = null; ReadOnlyDictionary <string, object> parms = null; try { IPlugin_Visualizer plugin = (IPlugin_Visualizer)mProject.GetPlugin(item.ScriptIdent); if (item.VisDescriptor.VisualizationType == VisDescr.VisType.Bitmap) { parms = CreateVisGenParams(false); vis2d = plugin.Generate2d(item.VisDescriptor, parms); if (vis2d == null) { Debug.WriteLine("Vis2d generator returned null"); } } else if (item.VisDescriptor.VisualizationType == VisDescr.VisType.Wireframe) { parms = CreateVisGenParams(true); IPlugin_Visualizer_v2 plugin2 = (IPlugin_Visualizer_v2)plugin; visWire = plugin2.GenerateWireframe(item.VisDescriptor, parms); if (visWire == null) { Debug.WriteLine("VisWire generator returned null"); } } else { Debug.Assert(false); } } catch (Exception ex) { Debug.WriteLine("Vis generation failed: " + ex); if (string.IsNullOrEmpty(LastPluginMessage)) { LastPluginMessage = ex.Message; } } bool failed = false; if (vis2d == null && visWire == null) { failed = true; } else if (vis2d != null) { previewGrid.Background = null; previewImage.Source = Visualization.ConvertToBitmapSource(vis2d); wireframePath.Data = new GeometryGroup(); BitmapDimensions = string.Format("{0}x{1}", previewImage.Source.Width, previewImage.Source.Height); mThumbnail = (BitmapSource)previewImage.Source; } else { WireframeObject wireObj = WireframeObject.Create(visWire); if (wireObj != null) { previewGrid.Background = Brushes.Black; previewImage.Source = Visualization.BLANK_IMAGE; double dim = Math.Floor( Math.Min(previewGrid.ActualWidth, previewGrid.ActualHeight)); wireframePath.Data = Visualization.GenerateWireframePath(wireObj, dim, parms); BitmapDimensions = "n/a"; mWireObj = wireObj; } else { failed = true; } } if (failed) { previewImage.Source = sBadParamsImage; if (!string.IsNullOrEmpty(LastPluginMessage)) { // Report the last message we got as an error. PluginErrMessage = LastPluginMessage; } else { // Generic failure message. PluginErrMessage = (string)FindResource("str_VisGenFailed"); } IsValid = false; } } string trimTag = Visualization.TrimAndValidateTag(TagString, out bool tagOk); Visualization match = EditVisualizationSet.FindVisualizationByTag(mEditedList, trimTag); if (match != null && (mOrigVis == null || trimTag != mOrigVis.Tag)) { // Another vis already has this tag. We're checking the edited list, so we'll // be current with edits to this or other Visualizations in the same set. tagOk = false; } if (!tagOk) { TagLabelBrush = mErrorLabelColor; IsValid = false; } else { TagLabelBrush = mDefaultLabelColor; } }