forked from amkovkov/GranuSightSoftware2
1) remove unused components: camera view (due it causes segmentation faults), compute resources and all related, illuminator controller (due in useless without camera view), image storage service, temperature and humidity service 2) database schema - reduce tables references, features storing in records themselves in compressed form, add records creation and editing date and time, add separator comment column 3) analysis - rework of pipeline and ui, now database storing only raw data and all display values calculated from it 4) lights service - add reconnection if disconnected 5) add width and height command line arguments 6) fix some typos and other issues
65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using System.IO.Compression;
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
/// <summary>
|
|
/// Компрессор для больших списков чисел с плавающей точкой
|
|
/// </summary>
|
|
public class CompressedFloatListConverter : ValueConverter<List<float>, string>
|
|
{
|
|
public CompressedFloatListConverter()
|
|
: base(
|
|
v => Compress(v),
|
|
v => Decompress(v)
|
|
)
|
|
{ }
|
|
|
|
private static string Compress(List<float> list)
|
|
{
|
|
if (list is null || list.Count == 0)
|
|
return "";
|
|
|
|
byte[] floatBytes = new byte[list.Count * sizeof(float)];
|
|
Buffer.BlockCopy(list.ToArray(), 0, floatBytes, 0, floatBytes.Length);
|
|
|
|
using var outputStream = new MemoryStream();
|
|
using (var compressionStream = new BrotliStream(outputStream, CompressionLevel.Optimal))
|
|
compressionStream.Write(floatBytes, 0, floatBytes.Length);
|
|
|
|
return Convert.ToBase64String(outputStream.ToArray());
|
|
}
|
|
|
|
private static List<float> Decompress(string base64String)
|
|
{
|
|
if (string.IsNullOrEmpty(base64String))
|
|
return new List<float>();
|
|
|
|
byte[] compressedBytes;
|
|
float[]? floatArray;
|
|
|
|
try
|
|
{
|
|
compressedBytes = Convert.FromBase64String(base64String);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
floatArray = JsonConvert.DeserializeObject<float[]>(base64String);
|
|
if (floatArray is null)
|
|
throw;
|
|
return new List<float>(floatArray);
|
|
}
|
|
|
|
using var inputStream = new MemoryStream(compressedBytes);
|
|
using var decompressionStream = new BrotliStream(inputStream, CompressionMode.Decompress);
|
|
using var outputStream = new MemoryStream();
|
|
decompressionStream.CopyTo(outputStream);
|
|
|
|
byte[] decompressedBytes = outputStream.ToArray();
|
|
floatArray = new float[decompressedBytes.Length / sizeof(float)];
|
|
Buffer.BlockCopy(decompressedBytes, 0, floatArray, 0, decompressedBytes.Length);
|
|
|
|
return new List<float>(floatArray);
|
|
}
|
|
} |