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
19 public GraphBitmap()
20 : this(new Variables())
21 {
22 }
23
29 : this(Variables, null, null)
30 {
31 }
32
39 public GraphBitmap(Variables Variables, int? DefaultWidth, int? DefaultHeight)
40 : base(Variables, DefaultWidth, DefaultHeight)
41 {
42 }
43
50 : base(Variables, Pixels.Width, Pixels.Height)
51 {
52 this.pixels = Pixels;
53 }
54
61 : base(Settings, Pixels.Width, Pixels.Height)
62 {
63 this.pixels = Pixels;
64 }
65
72 {
73 if (!(Element is Graph G))
74 return null;
75
77 if (!(this.pixels is null))
78 {
79 Settings.Width = this.pixels.Width;
80 Settings.Height = this.pixels.Height;
81 }
82
83 PixelInformation Pixels = G.CreatePixels(Settings);
84 SKSamplingOptions Options = new SKSamplingOptions(SKCubicResampler.Mitchell);
85
86 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(Math.Max(Pixels.Width, this.pixels?.Width ?? 0),
87 Math.Max(Pixels.Height, this.pixels?.Height ?? 0), SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
88 {
89 SKCanvas Canvas = Surface.Canvas;
90
91 using (SKImage Bmp = Pixels.CreateBitmap())
92 {
93 Canvas.DrawImage(Bmp, 0, 0, Options);
94 }
95
96 if (!(this.pixels is null))
97 {
98 using (SKImage Image = this.pixels.CreateBitmap())
99 {
100 Canvas.DrawImage(Image, 0, 0, Options);
101 }
102 }
103
104 using (SKImage Result = Surface.Snapshot())
105 {
106 return new GraphBitmap(this.Settings, PixelInformation.FromImage(Result));
107 }
108 }
109 }
110
117 {
118 if (!(Element is Graph G))
119 return null;
120
122 if (!(this.pixels is null))
123 {
124 Settings.Width = this.pixels.Width;
125 Settings.Height = this.pixels.Height;
126 }
127
128 PixelInformation Pixels = G.CreatePixels(Settings);
129 SKSamplingOptions Options = new SKSamplingOptions(SKCubicResampler.Mitchell);
130
131 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(Math.Max(Pixels.Width, this.pixels?.Width ?? 0),
132 Math.Max(Pixels.Height, this.pixels?.Height ?? 0), SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
133 {
134 SKCanvas Canvas = Surface.Canvas;
135
136 if (!(this.pixels is null))
137 {
138 using (SKImage Image = this.pixels.CreateBitmap())
139 {
140 Canvas.DrawImage(Image, 0, 0, Options);
141 }
142 }
143
144 using (SKImage Bmp = Pixels.CreateBitmap())
145 {
146 Canvas.DrawImage(Bmp, 0, 0, Options);
147 }
148
149 using (SKImage Result = Surface.Snapshot())
150 {
151 return new GraphBitmap(this.Settings, PixelInformation.FromImage(Result));
152 }
153 }
154 }
155
163 public override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
164 {
165 States = Array.Empty<object>();
166 return this.pixels;
167 }
168
172 public override Tuple<int, int> RecommendedBitmapSize
173 {
174 get
175 {
176 return new Tuple<int, int>(this.pixels?.Width ?? 0, this.pixels?.Height ?? 0);
177 }
178 }
179
187 public override string GetBitmapClickScript(double X, double Y, object[] States)
188 {
189 return "[" + Expression.ToString(X) + "," + Expression.ToString(Y) + "]";
190 }
191
193 public override bool Equals(object obj)
194 {
195 return (obj is GraphBitmap B && (this.pixels?.Equals(B.pixels) ?? (B.pixels is null)));
196 }
197
199 public override int GetHashCode()
200 {
201 return this.pixels?.GetHashCode() ?? 0;
202 }
203
207 public void Dispose()
208 {
209 }
210
215 public override void ExportGraph(XmlWriter Output)
216 {
217 if (!(this.pixels is null))
218 {
219 Output.WriteStartElement("GraphBitmap");
220 Output.WriteAttributeString("width", this.pixels.Width.ToString());
221 Output.WriteAttributeString("height", this.pixels.Height.ToString());
222
223 Output.WriteElementString("Png", Convert.ToBase64String(this.pixels.EncodeAsPng()));
224
225 Output.WriteEndElement();
226 }
227 }
228
233 public override Task ImportGraphAsync(XmlElement Xml)
234 {
235 int Width = int.Parse(Xml.GetAttribute("width"));
236 int Height = int.Parse(Xml.GetAttribute("height"));
237
238 foreach (XmlNode N in Xml.ChildNodes)
239 {
240 if (N is XmlElement E && E.LocalName == "Png")
241 {
242 byte[] Data = Convert.FromBase64String(E.InnerText);
243 this.pixels = new PixelInformationPng(Data, Width, Height);
244 break;
245 }
246 }
247
248 return Task.CompletedTask;
249 }
250
254 public override bool UsesDefaultColor => false;
255
261 public override bool TrySetDefaultColor(SKColor Color)
262 {
263 return false;
264 }
265
266 }
267}
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
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:193
override Tuple< int, int > RecommendedBitmapSize
The recommended bitmap size of the graph, if such is available.
Definition: GraphBitmap.cs:173
GraphBitmap(Variables Variables)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:28
override ISemiGroupElement AddRight(ISemiGroupElement Element)
Tries to add an element to the current element, from the right.
Definition: GraphBitmap.cs:116
GraphBitmap(Variables Variables, PixelInformation Pixels)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:49
override void ExportGraph(XmlWriter Output)
Exports graph specifics to XML.
Definition: GraphBitmap.cs:215
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: GraphBitmap.cs:199
override Task ImportGraphAsync(XmlElement Xml)
Imports graph specifics from XML.
Definition: GraphBitmap.cs:233
override ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add an element to the current element, from the left.
Definition: GraphBitmap.cs:71
override bool UsesDefaultColor
If graph uses default color
Definition: GraphBitmap.cs:254
GraphBitmap()
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:19
override bool TrySetDefaultColor(SKColor Color)
Tries to set the default color.
Definition: GraphBitmap.cs:261
GraphBitmap(GraphSettings Settings, PixelInformation Pixels)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:60
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:187
override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
Creates a bitmap of the graph.
Definition: GraphBitmap.cs:163
GraphBitmap(Variables Variables, int? DefaultWidth, int? DefaultHeight)
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:39
void Dispose()
IDisposable.Dispose
Definition: GraphBitmap.cs:207
Base class for graphs.
Definition: Graph.cs:88
GraphSettings Settings
Graph settings available during creation.
Definition: Graph.cs:205
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.