Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphBitmap.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
4using SkiaSharp;
6
8{
12 public class GraphBitmap : Graph, IDisposable
13 {
14 private PixelInformation pixels;
15
21 : this(Variables, null, null)
22 {
23 }
24
31 public GraphBitmap(Variables Variables, int? DefaultWidth, int? DefaultHeight)
32 : base(Variables, DefaultWidth, DefaultHeight)
33 {
34 }
35
42 : base(Variables, Pixels.Width, Pixels.Height)
43 {
44 this.pixels = Pixels;
45 }
46
53 : base(Settings, Pixels.Width, Pixels.Height)
54 {
55 this.pixels = Pixels;
56 }
57
64 {
65 if (!(Element is Graph G))
66 return null;
67
69 if (!(this.pixels is null))
70 {
71 Settings.Width = this.pixels.Width;
72 Settings.Height = this.pixels.Height;
73 }
74
75 PixelInformation Pixels = G.CreatePixels(Settings);
76
77 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(Math.Max(Pixels.Width, this.pixels?.Width ?? 0),
78 Math.Max(Pixels.Height, this.pixels?.Height ?? 0), SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
79 {
80 SKCanvas Canvas = Surface.Canvas;
81
82 using (SKImage Bmp = Pixels.CreateBitmap())
83 {
84 Canvas.DrawImage(Bmp, 0, 0);
85 }
86
87 if (!(this.pixels is null))
88 {
89 using (SKImage Image = this.pixels.CreateBitmap())
90 {
91 Canvas.DrawImage(Image, 0, 0);
92 }
93 }
94
95 using (SKImage Result = Surface.Snapshot())
96 {
97 return new GraphBitmap(this.Settings, PixelInformation.FromImage(Result));
98 }
99 }
100 }
101
108 {
109 if (!(Element is Graph G))
110 return null;
111
113 if (!(this.pixels is null))
114 {
115 Settings.Width = this.pixels.Width;
116 Settings.Height = this.pixels.Height;
117 }
118
119 PixelInformation Pixels = G.CreatePixels(Settings);
120
121 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(Math.Max(Pixels.Width, this.pixels?.Width ?? 0),
122 Math.Max(Pixels.Height, this.pixels?.Height ?? 0), SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
123 {
124 SKCanvas Canvas = Surface.Canvas;
125
126 if (!(this.pixels is null))
127 {
128 using (SKImage Image = this.pixels.CreateBitmap())
129 {
130 Canvas.DrawImage(Image, 0, 0);
131 }
132 }
133
134 using (SKImage Bmp = Pixels.CreateBitmap())
135 {
136 Canvas.DrawImage(Bmp, 0, 0);
137 }
138
139 using (SKImage Result = Surface.Snapshot())
140 {
141 return new GraphBitmap(this.Settings, PixelInformation.FromImage(Result));
142 }
143 }
144 }
145
153 public override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
154 {
155 States = new object[0];
156 return this.pixels;
157 }
158
162 public override Tuple<int, int> RecommendedBitmapSize
163 {
164 get
165 {
166 return new Tuple<int, int>(this.pixels?.Width ?? 0, this.pixels?.Height ?? 0);
167 }
168 }
169
177 public override string GetBitmapClickScript(double X, double Y, object[] States)
178 {
179 return "[" + Expression.ToString(X) + "," + Expression.ToString(Y) + "]";
180 }
181
183 public override bool Equals(object obj)
184 {
185 return (obj is GraphBitmap B && (this.pixels?.Equals(B.pixels) ?? (B.pixels is null)));
186 }
187
189 public override int GetHashCode()
190 {
191 return this.pixels?.GetHashCode() ?? 0;
192 }
193
197 public void Dispose()
198 {
199 }
200
205 public override void ExportGraph(XmlWriter Output)
206 {
207 if (!(this.pixels is null))
208 {
209 Output.WriteStartElement("GraphBitmap");
210 Output.WriteAttributeString("width", this.pixels.Width.ToString());
211 Output.WriteAttributeString("height", this.pixels.Height.ToString());
212
213 Output.WriteElementString("Png", Convert.ToBase64String(this.pixels.EncodeAsPng()));
214
215 Output.WriteEndElement();
216 }
217 }
218
223 public override Task ImportGraphAsync(XmlElement Xml)
224 {
225 int Width = int.Parse(Xml.GetAttribute("width"));
226 int Height = int.Parse(Xml.GetAttribute("height"));
227
228 foreach (XmlNode N in Xml.ChildNodes)
229 {
230 if (N is XmlElement E && E.LocalName == "Png")
231 {
232 byte[] Data = Convert.FromBase64String(E.InnerText);
233 this.pixels = new PixelInformationPng(Data, Width, Height);
234 break;
235 }
236 }
237
238 return Task.CompletedTask;
239 }
240
244 public override bool UsesDefaultColor => false;
245
251 public override bool TrySetDefaultColor(SKColor Color)
252 {
253 return false;
254 }
255
256 }
257}
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:13
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: GraphBitmap.cs:183
override Tuple< int, int > RecommendedBitmapSize
The recommended bitmap size of the graph, if such is available.
Definition: GraphBitmap.cs:163
GraphBitmap(Variables Variables)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:20
override ISemiGroupElement AddRight(ISemiGroupElement Element)
Tries to add an element to the current element, from the right.
Definition: GraphBitmap.cs:107
GraphBitmap(Variables Variables, PixelInformation Pixels)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:41
override void ExportGraph(XmlWriter Output)
Exports graph specifics to XML.
Definition: GraphBitmap.cs:205
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: GraphBitmap.cs:189
override Task ImportGraphAsync(XmlElement Xml)
Imports graph specifics from XML.
Definition: GraphBitmap.cs:223
override ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add an element to the current element, from the left.
Definition: GraphBitmap.cs:63
override bool UsesDefaultColor
If graph uses default color
Definition: GraphBitmap.cs:244
override bool TrySetDefaultColor(SKColor Color)
Tries to set the default color.
Definition: GraphBitmap.cs:251
GraphBitmap(GraphSettings Settings, PixelInformation Pixels)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:52
override string GetBitmapClickScript(double X, double Y, object[] States)
Gets script corresponding to a point in a generated bitmap representation of the graph.
Definition: GraphBitmap.cs:177
override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
Creates a bitmap of the graph.
Definition: GraphBitmap.cs:153
GraphBitmap(Variables Variables, int? DefaultWidth, int? DefaultHeight)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:31
void Dispose()
IDisposable.Dispose
Definition: GraphBitmap.cs:197
Base class for graphs.
Definition: Graph.cs:79
GraphSettings Settings
Graph settings available during creation.
Definition: Graph.cs:188
Contains pixel information
abstract SKImage CreateBitmap()
Creates an SKImage image. It must be disposed by the caller.
static PixelInformation FromImage(SKImage Image)
Gets the pixel information from an SKImage.
Contains pixel information in PNG format
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of semigroup elements.