Skip to content

Commit

Permalink
Add dynamic size for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
timia2109 committed Apr 3, 2024
1 parent 8dac255 commit 7187af9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
11 changes: 6 additions & 5 deletions MqttExport/EspImageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using DisplayUtil.Scenes;
using SkiaSharp;

namespace DisplayUtil.MqttExport;

Expand All @@ -19,7 +20,7 @@ ScreenRepository screenRepository
/// </summary>
/// <param name="providerId">Id of the provider</param>
/// <returns>Byte Array</returns>
public async Task<byte[]> GetAsPlainBytesAsync(string providerId)
public async Task<(byte[], SKSize)> GetAsPlainBytesAsync(string providerId)
{
var stopwatch = new Stopwatch();
LogRender(providerId);
Expand All @@ -33,17 +34,17 @@ public async Task<byte[]> GetAsPlainBytesAsync(string providerId)

var binaryData = BinaryImageStreamCreator.GetImageStream(image);
LogPlainBytes(binaryData.Length);
return binaryData;
return (binaryData, new SKSize(image.Width, image.Height));
}

/// <summary>
/// Gets the Image as RunLength Compressed
/// </summary>
/// <param name="providerId">Id of the provider</param>
/// <returns>Compressed Data</returns>
public async Task<byte[]> GetAsRunLengthAsync(string providerId)
public async Task<(byte[], SKSize)> GetAsRunLengthAsync(string providerId)
{
var plainBytes = await GetAsPlainBytesAsync(providerId);
var (plainBytes, size) = await GetAsPlainBytesAsync(providerId);

var runLengthEncoder = new RunLengthCompressor();
var compressedData = runLengthEncoder.WriteStream(plainBytes);
Expand All @@ -54,7 +55,7 @@ public async Task<byte[]> GetAsRunLengthAsync(string providerId)
Math.Round((1 - compressedPercent) * 100, 2)
);

return compressedData;
return (compressedData, size);
}


Expand Down
2 changes: 1 addition & 1 deletion MqttExport/MqttExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ExportingMqttClient exportingMqttClient
{
public async Task ExportScreenToMqtt(string providerId)
{
var data = await espImageProvider.GetAsRunLengthAsync(providerId);
var (data, size) = await espImageProvider.GetAsRunLengthAsync(providerId);
await exportingMqttClient.SendAsync(data);
}
}
12 changes: 8 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,23 @@
.WithName("Publish manual to MQTT")
.WithOpenApi();

app.MapGet("/esp/{providerId}", async (string providerId, EspImageProvider espProvider) =>
app.MapGet("/esp/{providerId}", async (string providerId, HttpContext ctx, EspImageProvider espProvider) =>
{
var data = await espProvider.GetAsRunLengthAsync(providerId);
var (data, size) = await espProvider.GetAsRunLengthAsync(providerId);
var base64 = Convert.ToBase64String(data);
ctx.Response.Headers.Append("X-Width", size.Width.ToString());
ctx.Response.Headers.Append("X-Height", size.Height.ToString());
return Results.Text(base64, "text/plain", Encoding.ASCII);
})
.WithName("Get ESP Image")
.WithOpenApi();

app.MapGet("/esp/bits/{providerId}", async (string providerId, EspImageProvider espProvider) =>
app.MapGet("/esp/bits/{providerId}", async (string providerId, HttpContext ctx, EspImageProvider espProvider) =>
{
var data = await espProvider.GetAsPlainBytesAsync(providerId);
var (data, size) = await espProvider.GetAsPlainBytesAsync(providerId);
var base64 = Convert.ToBase64String(data);
ctx.Response.Headers.Append("X-Width", size.Width.ToString());
ctx.Response.Headers.Append("X-Height", size.Height.ToString());
return Results.Text(base64, "text/plain", Encoding.ASCII);
})
.WithName("Get ESP Bit Image")
Expand Down
8 changes: 7 additions & 1 deletion wwwroot/compression-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@
document.getElementById("targetScreen").value;

fetch(`/esp/${targetScreenId}`)
.then((e) => e.text())
.then((e) => {
const width = parseInt(e.headers.get("X-Width"));
const height = parseInt(e.headers.get("X-Height"));
document.getElementById("canvas").width = width;
document.getElementById("canvas").height = height;
return e.text();
})
.then((out) => {
const rawString = window.atob(out);
const uint8Array = new Uint8Array(rawString.length);
Expand Down

0 comments on commit 7187af9

Please sign in to comment.