mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-28 16:58:01 +01:00
Run TidyCode on all new classes
This commit is contained in:
@@ -4,12 +4,10 @@
|
||||
// libsixel (C/C++) - https://github.com/saitoha/libsixel
|
||||
// Copyright (c) 2014-2016 Hayaki Saito @license MIT
|
||||
|
||||
using Terminal.Gui;
|
||||
|
||||
namespace Terminal.Gui;
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a images into the sixel console image output format.
|
||||
/// Encodes a images into the sixel console image output format.
|
||||
/// </summary>
|
||||
public class SixelEncoder
|
||||
{
|
||||
@@ -35,32 +33,29 @@ public class SixelEncoder
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the quantizer responsible for building a representative
|
||||
/// limited color palette for images and for mapping novel colors in
|
||||
/// images to their closest palette color
|
||||
/// Gets or sets the quantizer responsible for building a representative
|
||||
/// limited color palette for images and for mapping novel colors in
|
||||
/// images to their closest palette color
|
||||
/// </summary>
|
||||
public ColorQuantizer Quantizer { get; set; } = new ();
|
||||
|
||||
/// <summary>
|
||||
/// Encode the given bitmap into sixel encoding
|
||||
/// Encode the given bitmap into sixel encoding
|
||||
/// </summary>
|
||||
/// <param name="pixels"></param>
|
||||
/// <returns></returns>
|
||||
public string EncodeSixel (Color [,] pixels)
|
||||
{
|
||||
|
||||
const string start = "\u001bP"; // Start sixel sequence
|
||||
|
||||
|
||||
string defaultRatios = this.AnyHasAlphaOfZero(pixels) ? "0;1;0": "0;0;0"; // Defaults for aspect ratio and grid size
|
||||
string defaultRatios = AnyHasAlphaOfZero (pixels) ? "0;1;0" : "0;0;0"; // Defaults for aspect ratio and grid size
|
||||
const string completeStartSequence = "q"; // Signals beginning of sixel image data
|
||||
const string noScaling = "\"1;1;"; // no scaling factors (1x1);
|
||||
|
||||
string fillArea = GetFillArea (pixels);
|
||||
|
||||
string pallette = GetColorPalette (pixels );
|
||||
string pallette = GetColorPalette (pixels);
|
||||
|
||||
string pixelData = WriteSixel (pixels);
|
||||
|
||||
@@ -71,15 +66,14 @@ public class SixelEncoder
|
||||
|
||||
private string WriteSixel (Color [,] pixels)
|
||||
{
|
||||
|
||||
StringBuilder sb = new StringBuilder ();
|
||||
var sb = new StringBuilder ();
|
||||
int height = pixels.GetLength (1);
|
||||
int width = pixels.GetLength (0);
|
||||
|
||||
// Iterate over each 'row' of the image. Because each sixel write operation
|
||||
// outputs a screen area 6 pixels high (and 1+ across) we must process the image
|
||||
// 6 'y' units at once (1 band)
|
||||
for (int y = 0; y < height; y += 6)
|
||||
for (var y = 0; y < height; y += 6)
|
||||
{
|
||||
sb.Append (ProcessBand (pixels, y, Math.Min (6, height - y), width));
|
||||
|
||||
@@ -107,18 +101,18 @@ public class SixelEncoder
|
||||
Array.Fill (accu, (ushort)1);
|
||||
Array.Fill (slots, (short)-1);
|
||||
|
||||
var usedColorIdx = new List<int> ();
|
||||
var targets = new List<List<string>> ();
|
||||
List<int> usedColorIdx = new List<int> ();
|
||||
List<List<string>> targets = new List<List<string>> ();
|
||||
|
||||
// Process columns within the band
|
||||
for (int x = 0; x < width; ++x)
|
||||
for (var x = 0; x < width; ++x)
|
||||
{
|
||||
Array.Clear (code, 0, usedColorIdx.Count);
|
||||
|
||||
// Process each row in the 6-pixel high band
|
||||
for (int row = 0; row < bandHeight; ++row)
|
||||
for (var row = 0; row < bandHeight; ++row)
|
||||
{
|
||||
var color = pixels [x, startY + row];
|
||||
Color color = pixels [x, startY + row];
|
||||
|
||||
int colorIndex = Quantizer.GetNearestColor (color);
|
||||
|
||||
@@ -129,12 +123,14 @@ public class SixelEncoder
|
||||
|
||||
if (slots [colorIndex] == -1)
|
||||
{
|
||||
targets.Add (new List<string> ());
|
||||
targets.Add (new ());
|
||||
|
||||
if (x > 0)
|
||||
{
|
||||
last [usedColorIdx.Count] = 0;
|
||||
accu [usedColorIdx.Count] = (ushort)x;
|
||||
}
|
||||
|
||||
slots [colorIndex] = (short)usedColorIdx.Count;
|
||||
usedColorIdx.Add (colorIndex);
|
||||
}
|
||||
@@ -143,7 +139,7 @@ public class SixelEncoder
|
||||
}
|
||||
|
||||
// Handle transitions between columns
|
||||
for (int j = 0; j < usedColorIdx.Count; ++j)
|
||||
for (var j = 0; j < usedColorIdx.Count; ++j)
|
||||
{
|
||||
if (code [j] == last [j])
|
||||
{
|
||||
@@ -155,6 +151,7 @@ public class SixelEncoder
|
||||
{
|
||||
targets [j].Add (CodeToSixel (last [j], accu [j]));
|
||||
}
|
||||
|
||||
last [j] = (sbyte)code [j];
|
||||
accu [j] = 1;
|
||||
}
|
||||
@@ -162,7 +159,7 @@ public class SixelEncoder
|
||||
}
|
||||
|
||||
// Process remaining data for this band
|
||||
for (int j = 0; j < usedColorIdx.Count; ++j)
|
||||
for (var j = 0; j < usedColorIdx.Count; ++j)
|
||||
{
|
||||
if (last [j] != 0)
|
||||
{
|
||||
@@ -172,7 +169,8 @@ public class SixelEncoder
|
||||
|
||||
// Build the final output for this band
|
||||
var result = new StringBuilder ();
|
||||
for (int j = 0; j < usedColorIdx.Count; ++j)
|
||||
|
||||
for (var j = 0; j < usedColorIdx.Count; ++j)
|
||||
{
|
||||
result.Append ($"#{usedColorIdx [j]}{string.Join ("", targets [j])}$");
|
||||
}
|
||||
@@ -182,7 +180,8 @@ public class SixelEncoder
|
||||
|
||||
private static string CodeToSixel (int code, int repeat)
|
||||
{
|
||||
char c = (char)(code + 63);
|
||||
var c = (char)(code + 63);
|
||||
|
||||
if (repeat > 3)
|
||||
{
|
||||
return "!" + repeat + c;
|
||||
@@ -205,16 +204,18 @@ public class SixelEncoder
|
||||
{
|
||||
Quantizer.BuildPalette (pixels);
|
||||
|
||||
StringBuilder paletteSb = new StringBuilder ();
|
||||
var paletteSb = new StringBuilder ();
|
||||
|
||||
for (int i = 0; i < Quantizer.Palette.Count; i++)
|
||||
for (var i = 0; i < Quantizer.Palette.Count; i++)
|
||||
{
|
||||
var color = Quantizer.Palette.ElementAt (i);
|
||||
paletteSb.AppendFormat ("#{0};2;{1};{2};{3}",
|
||||
i,
|
||||
color.R * 100 / 255,
|
||||
color.G * 100 / 255,
|
||||
color.B * 100 / 255);
|
||||
Color color = Quantizer.Palette.ElementAt (i);
|
||||
|
||||
paletteSb.AppendFormat (
|
||||
"#{0};2;{1};{2};{3}",
|
||||
i,
|
||||
color.R * 100 / 255,
|
||||
color.G * 100 / 255,
|
||||
color.B * 100 / 255);
|
||||
}
|
||||
|
||||
return paletteSb.ToString ();
|
||||
@@ -227,15 +228,16 @@ public class SixelEncoder
|
||||
|
||||
return $"{widthInChars};{heightInChars}";
|
||||
}
|
||||
|
||||
private bool AnyHasAlphaOfZero (Color [,] pixels)
|
||||
{
|
||||
int width = pixels.GetLength (0);
|
||||
int height = pixels.GetLength (1);
|
||||
|
||||
// Loop through each pixel in the 2D array
|
||||
for (int x = 0; x < width; x++)
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
for (int y = 0; y < height; y++)
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
// Check if the alpha component (A) is 0
|
||||
if (pixels [x, y].A == 0)
|
||||
@@ -244,6 +246,7 @@ public class SixelEncoder
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false; // No pixel with A of 0 was found
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user