private static UndirectedGraph <Node, Edge> GetGraph( CommunityAlgorithm communityAlg, bool showLabels, PaletteGenerator paletteGenerator) { // generates community color palette paletteGenerator = paletteGenerator ?? TolPalettes.CreateTolDivPalette; var colors = paletteGenerator(communityAlg.GetNumberCommunities()); // creates a graphviz from the network and communities var graph = new UndirectedGraph <Node, Edge>(); // adds nodes with community info var nodes = new Node[communityAlg.Size]; foreach (var nodeID in communityAlg.Network.Vertices) { var node = new Node(nodeID, communityAlg.NodesCommunities[nodeID]) { Color = colors[communityAlg.NodesCommunities[nodeID]], ShowLabel = showLabels }; nodes[nodeID] = node; graph.AddVertex(node); } // adds edges from connections foreach (var conn in communityAlg.Network.Edges) { graph.AddEdge(new Edge(nodes[conn.Source], nodes[conn.Target], conn.Weight) { ShowLabel = showLabels }); } return(graph); }
private static void SaveFileTest(PaletteGenerator paletteGenerator, string name) { // creates graph and adds nodes var network = new Network(); for (var i = 0u; i < NUM_NODES; i++) { network.AddVertex(i); } // adds connections network.AddEdge(new Connection(0, 1)); network.AddEdge(new Connection(0, 2)); network.AddEdge(new Connection(0, 9)); network.AddEdge(new Connection(2, 4)); network.AddEdge(new Connection(2, 9)); network.AddEdge(new Connection(4, 7)); network.AddEdge(new Connection(7, 9)); network.AddEdge(new Connection(8, 9)); // creates algorithm and updates communities var communityAlg = new CommunityAlgorithm(network, -1, 0.000001); communityAlg.Update(); communityAlg.DisplayCommunities(); var fullPath = Path.GetFullPath("."); foreach (var imageType in ImageTypes) { var fileName = $"{FILE_NAME}-{name}-{imageType}"; var dotPath = Path.Combine(fullPath, $"{fileName}.dot"); var imgPath = $"{dotPath}.{imageType.ToString().ToLower()}"; File.Delete(dotPath); File.Delete(imgPath); var filePath = communityAlg.ToGraphvizFile( fullPath, fileName, true, imageType, paletteGenerator, WAIT_TIMEOUT); Console.WriteLine(dotPath); Assert.IsTrue(File.Exists(dotPath), $"Dot file should exist in {dotPath}."); Assert.AreEqual(filePath, dotPath, $"Dot file should be exist in {imgPath}"); Console.WriteLine(imgPath); Assert.IsTrue(File.Exists(imgPath), $"Image file should exist in {imgPath}."); Assert.IsTrue(new FileInfo(imgPath).Length > 0, "Image size should be > 0 bytes."); #if !DEBUG File.Delete(dotPath); File.Delete(imgPath); #endif } }
private void ColorSchemeCBoxIndexChanged(object sender, EventArgs e) { // checks selected palette switch (this.colorSchemeCBox.SelectedIndex) { case 0: this._paletteGenerator = TolPalettes.CreateTolDivPalette; break; default: this._paletteGenerator = TolPalettes.CreateTolRainbowPalette; break; } }
/// <summary> /// Saves the given <see cref="Network" /> to an image file. /// </summary> /// <param name="communityAlg">The root node of the tree to be saved.</param> /// <param name="basePath">The path in which to save the given tree</param> /// <param name="fileName">The name of the image file to be saved (without extension)</param> /// <param name="showLabels">Whether to show nodes' labels.</param> /// <param name="imageType">The type of image file in which to save the tree.</param> /// <param name="paletteGenerator">The color palette generator used to represent the different communities.</param> /// <param name="timeout">The maximum time to wait for Graphviz to create the image file.</param> /// <returns>The path to the file where the tree image file was saved.</returns> public static string ToGraphvizFile( this CommunityAlgorithm communityAlg, string basePath, string fileName, bool showLabels = true, GraphvizImageType imageType = GRAPHVIZ_IMAGE_TYPE, PaletteGenerator paletteGenerator = null, int timeout = GRAPHVIZ_TIMEOUT_MS) { var filePath = Path.Combine(basePath, $"{fileName}.dot"); if (File.Exists(filePath)) { File.Delete(filePath); } var graph = GetGraph(communityAlg, showLabels, paletteGenerator); var viz = new GraphvizAlgorithm <Node, Edge>(graph) { ImageType = imageType }; viz.FormatVertex += OnFormatVertex; viz.FormatEdge += OnFormatEdge; return(viz.Generate(new FileDotEngine(timeout), filePath)); }
public static Imgd ToImgd(Bitmap bitmap, int bpp, Func <Bitmap, Bitmap> quantizer, bool swizzle = false) { if (quantizer != null) { var firstSrc = new ReadAs32bppPixels(bitmap); if (PaletteColorUsageCounter.IfMaxColorCountIsOver(firstSrc.Pixels, 1 << bpp)) { bitmap = quantizer(bitmap); } } switch (bpp) { case 4: { const int maxColors = 16; var src = new ReadAs32bppPixels(bitmap); var newPalette = new PaletteGenerator(src.Pixels, maxColors); if (newPalette.MaxUsedColors > newPalette.MostUsedPixels.Length) { Console.WriteLine( $"Trimming color palette entry count from {newPalette.MaxUsedColors} to ${newPalette.MostUsedPixels.Length}" ); } var destBits = new byte[(src.Width * src.Height + 1) / 2]; var clut = new byte[4 * maxColors]; for (int index = 0; index < newPalette.MostUsedPixels.Length; index++) { var pixel = newPalette.MostUsedPixels[index]; clut[4 * index + 0] = (byte)(pixel >> 16); clut[4 * index + 1] = (byte)(pixel >> 8); clut[4 * index + 2] = (byte)(pixel >> 0); clut[4 * index + 3] = (byte)(pixel >> 24); } var srcPointer = 0; var destPointer = 0; for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { var newPixel = newPalette.FindNearest(src.Pixels[srcPointer++]) & 15; if (0 == (x & 1)) { // first pixel: hi byte destBits[destPointer] = (byte)(newPixel << 4); } else { // second pixel: lo byte destBits[destPointer++] |= (byte)(newPixel); } } } return(Imgd.Create(bitmap.Size, Imaging.PixelFormat.Indexed4, destBits, clut, swizzle)); } case 8: { const int maxColors = 256; var src = new ReadAs32bppPixels(bitmap); var newPalette = new PaletteGenerator(src.Pixels, maxColors); if (newPalette.MaxUsedColors > newPalette.MostUsedPixels.Length) { Console.WriteLine( $"Trimming color palette entry count from {newPalette.MaxUsedColors} to ${newPalette.MostUsedPixels.Length}" ); } var destBits = new byte[src.Width * src.Height]; var clut = new byte[4 * maxColors]; for (int index = 0; index < newPalette.MostUsedPixels.Length; index++) { var pixel = newPalette.MostUsedPixels[index]; clut[4 * index + 0] = (byte)(pixel >> 16); clut[4 * index + 1] = (byte)(pixel >> 8); clut[4 * index + 2] = (byte)(pixel >> 0); clut[4 * index + 3] = (byte)(pixel >> 24); } var srcPointer = 0; var destPointer = 0; for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { destBits[destPointer++] = (byte)newPalette.FindNearest(src.Pixels[srcPointer++]); } } return(Imgd.Create(bitmap.Size, Imaging.PixelFormat.Indexed8, destBits, clut, swizzle)); } case 32: { var src = new ReadAs32bppPixels(bitmap); var destBits = new byte[4 * src.Width * src.Height]; var srcPointer = 0; var destPointer = 0; for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { var pixel = src.Pixels[srcPointer++]; destBits[destPointer + 0] = (byte)(pixel >> 16); destBits[destPointer + 1] = (byte)(pixel >> 8); destBits[destPointer + 2] = (byte)(pixel >> 0); destBits[destPointer + 3] = (byte)(pixel >> 24); destPointer += 4; } } return(Imgd.Create(bitmap.Size, Imaging.PixelFormat.Rgba8888, destBits, new byte[0], swizzle)); } } throw new NotSupportedException($"BitsPerPixel {bpp} not recognized!"); }