public static Image DiffImages(Image img1, Image img2) { bool foundDifference = false; var bmp1 = img1.ToBitmap (); var bmp2 = img2.ToBitmap (); var res = new ImageBuilder ((int)Math.Min (bmp1.Size.Width, bmp2.Size.Width), (int) Math.Min (bmp1.Size.Height, bmp2.Size.Height)); var bmpr = res.ToBitmap (); res.Dispose (); for (int y=0; y<bmp1.Size.Height && y < bmp2.Size.Height; y++) { for (int x=0; x<bmp1.Size.Width && x<bmp2.Size.Height; x++) { var p1 = bmp1.GetPixel (x, y); var p2 = bmp2.GetPixel (x, y); var col = Colors.White; if (p1 != p2) { foundDifference = true; var r = Math.Pow (p1.Red - p2.Red, 2) + Math.Pow (p1.Green - p2.Green, 2) + Math.Pow (p1.Blue - p2.Blue, 2) + Math.Pow (p1.Alpha - p2.Alpha, 2); if (r < 0.01) col = new Color (0.9, 0.9, 0.9); else if (r < 0.1) col = new Color (0.7, 0.7, 0.7); else col = Colors.Red; } bmpr.SetPixel (x, y, col); } } if (foundDifference) return bmpr; else return null; }
/// <summary> /// Refresh the specified scan. /// </summary> /// <param name="scan">Scan.</param> /// <param name="changedFiberType">Set to true, if the fibertype of the given scan has changed</param> void Refresh(BaseScan scan, bool changedFiberType = false) { Image thumbnail = store.GetNavigatorAt(scan.position).GetValue(thumbnailCol); TreePosition currentNode = scan.position; if (changedFiberType) { TreePosition parentNodePosition; if (fiberTypeNodes.ContainsKey(scan.FiberType)) { parentNodePosition = fiberTypeNodes[scan.FiberType]; } else { TextLayout text = new TextLayout(); text.Text = scan.FiberType; ImageBuilder ib = new ImageBuilder(text.GetSize().Width, text.GetSize().Height); ib.Context.DrawTextLayout(text, Point.Zero); parentNodePosition = store.AddNode(null).SetValue(thumbnailCol, ib.ToBitmap()).CurrentPosition; fiberTypeNodes[scan.FiberType] = parentNodePosition; text.Dispose(); ib.Dispose(); } store.GetNavigatorAt(currentNode).Remove(); scan.position = currentNode = store.AddNode(parentNodePosition).CurrentPosition; ExpandToRow(scan.position); ScrollToRow(scan.position); SelectRow(scan.position); scan.parentPosition = parentNodePosition; } store.GetNavigatorAt(currentNode) .SetValue(nameCol, scan.ToString()) .SetValue(thumbnailCol, thumbnail) .SetValue(finishCol, scan.IsFinish() ? tick : cross) .SetValue(saveStateCol, scan.HasUnsaved() ? "*" : ""); if (DataSource.GetChildrenCount(scan.parentPosition) <= 0) { store.GetNavigatorAt(scan.parentPosition).Remove(); } // update filtered store if (!string.IsNullOrEmpty(filterText)) { storeFilter.GetNavigatorAt(scan.positionFiltered) .SetValue(nameColFilter, scan.ToString()) .SetValue(thumbnailColFilter, thumbnail) .SetValue(finishColFiltered, scan.IsFinish() ? tick : cross) .SetValue(saveStateColFilter, scan.HasUnsaved() ? "*" : ""); if (changedFiberType) { Filter(filterText); } } }
protected override void OnSelectionChanged(EventArgs e) { if (SelectedRow == null) return; object value = store.GetNavigatorAt(SelectedRow).GetValue(nameCol); if (value is BaseAlgorithm) { TextLayout text = new TextLayout(); text.Text = value.ToString(); Size textSize = text.GetSize(); var ib = new ImageBuilder(textSize.Width, textSize.Height); ib.Context.DrawTextLayout(text, 0, 0); var d = CreateDragOperation(); d.Data.AddValue(value.GetType().AssemblyQualifiedName); d.SetDragImage(ib.ToVectorImage(), -6, -4); d.AllowedActions = DragDropAction.Link; d.Start(); d.Finished += (object sender, DragFinishedEventArgs e2) => this.UnselectAll(); text.Dispose(); ib.Dispose(); } else { this.UnselectRow(SelectedRow); } }
/// <summary> /// Reloads file tree information. /// </summary> /// <param name="scans">Collection of loaded scans</param> /// <param name="currentScan">Current focused scan</param> /// <param name="save">Update scan collection</param> public void Reload(ScanCollection scans, BaseScan currentScan = null, bool save = true) { if (scans.Count > 0) { scans.Sort(scans[0]); } if (save) { scanCollection = scans; } DataSource = store; store.Clear(); TreePosition pos = null; fiberTypeNodes = new Dictionary<string, TreePosition>(); foreach (BaseScan scan in scans) { TreePosition currentNode; if (fiberTypeNodes.ContainsKey(scan.FiberType)) { currentNode = fiberTypeNodes[scan.FiberType]; } else { TextLayout text = new TextLayout(); text.Text = scan.FiberType; ImageBuilder ib = new ImageBuilder(text.GetSize().Width, text.GetSize().Height); ib.Context.DrawTextLayout(text, Point.Zero); currentNode = store.AddNode(null).SetValue(thumbnailCol, ib.ToVectorImage()).CurrentPosition; fiberTypeNodes[scan.FiberType] = currentNode; text.Dispose(); ib.Dispose(); } var v = store.AddNode(currentNode) .SetValue(nameCol, scan.ToString()) .SetValue(finishCol, scan.IsFinish() ? tick : cross) .SetValue(saveStateCol, scan.HasUnsaved() ? "*" : "") .CurrentPosition; scan.position = v; scan.parentPosition = currentNode; if (currentScan != null) { if (currentScan == scan) { pos = v; } } else { if (pos == null) { pos = v; } } scan.ScanDataChanged += OnScanDataChanged; } if (scans.Count > 0) { ExpandToRow(pos); SelectRow(pos); } LoadPreviewsAsync(scans); }
public Widget ToWidget() { long coloumns = Width; long rows = Height; if (widget == null) { if (rows <= 16 && coloumns <= 16) { Table t = new Table(); if (isSparse) { foreach (int i in sparseMatrix.GetRows()) { foreach (KeyValuePair<int, float> value in sparseMatrix.GetRowData(i)) { t.Add(new Label(value.Value.ToString()), value.Key, i); } } } else { for (int x = 0; x < coloumns; x++) { for (int y = 0; y < rows; y++) { t.Add(new Label(matrix[x, y].ToString()), x, y); } } } widget = t; } else { BitmapImage bi; int iScaleFactor = MathExtras.NextPowerOf2( (int) Math.Round((float) Math.Max(coloumns, rows) / BaseType<int>.MaxWidgetSize.Width) + 1 ); ImageBuilder ib = new ImageBuilder(coloumns / iScaleFactor, rows / iScaleFactor); bi = ib.ToBitmap(); if (isSparse) { float max = sparseMatrix.Max(); float min = sparseMatrix.Min(); const float toMax = 65536.0f; float toMaxLog = (float) Math.Log(toMax); foreach (int y in sparseMatrix.GetRows()) { foreach (KeyValuePair<int, float> v in sparseMatrix.GetRowData(y)) { float toLog = (toMax - 1.0f) * ((v.Value - min) / (max - min)) + 1.0f; byte c = (byte) ((Math.Log(toLog) / toMaxLog) * 255); bi.SetPixel(v.Key / iScaleFactor, y / iScaleFactor, Color.FromBytes(c, c, c)); } } } else { float max = 0.0f; float[,] copy = matrix.Scale(1.0f, 65536.0f); for (int x = 0; x < coloumns; x++) { for (int y = 0; y < rows; y++) { if (copy[x, y] > 0) { copy[x, y] = (float) (Math.Log(copy[x, y])); } if (copy[x, y] > max) { max = copy[x, y]; } } } for (int x = 0; x < coloumns; x++) { for (int y = 0; y < rows; y++) { byte c = (byte) ((copy[x, y] * 255) / max); if (c > 0) { bi.SetPixel(x / iScaleFactor, y / iScaleFactor, Color.FromBytes(c, c, c)); } } } } ib.Dispose(); widget = new ImageView(bi.WithBoxSize(BaseType<int>.MaxWidgetSize)); } } return widget; }