Skip to content

Commit b25fdb5

Browse files
committed
Tidyup and fix warnings
1 parent c65c505 commit b25fdb5

File tree

5 files changed

+77
-66
lines changed

5 files changed

+77
-66
lines changed

Terminal.Gui/Application/Application.Driver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ public static partial class Application // Driver abstractions
2727
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
2828
public static string ForceDriver { get; set; } = string.Empty;
2929

30+
31+
/// <summary>
32+
/// Encoded sixel graphics to render on each loop of the application render loop.
33+
/// </summary>
3034
public static List<SixelToRender> Sixel = new List<SixelToRender> ();
3135
}

Terminal.Gui/ConsoleDrivers/NetDriver.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,6 @@ private bool SetCursorPosition (int col, int row)
13381338
}
13391339

13401340
private CursorVisibility? _cachedCursorVisibility;
1341-
private static bool _supportsSixel;
13421341

13431342
public override void UpdateCursor ()
13441343
{

Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class ColorQuantizer
3232

3333
private readonly ConcurrentDictionary<Color, int> _nearestColorCache = new ();
3434

35+
/// <summary>
36+
/// Populates <see cref="Palette"/> based on the <see cref="PaletteBuildingAlgorithm"/> and supplied
37+
/// bitmap image.
38+
/// </summary>
39+
/// <param name="pixels"></param>
3540
public void BuildPalette (Color [,] pixels)
3641
{
3742
List<Color> allColors = new List<Color> ();
@@ -50,6 +55,12 @@ public void BuildPalette (Color [,] pixels)
5055
Palette = PaletteBuildingAlgorithm.BuildPalette (allColors, MaxColors);
5156
}
5257

58+
/// <summary>
59+
/// Returns the index of the color in <see cref="Palette"/> that most closely
60+
/// matches <paramref name="toTranslate"/> based on the current <see cref="DistanceAlgorithm"/>.
61+
/// </summary>
62+
/// <param name="toTranslate"></param>
63+
/// <returns></returns>
5364
public int GetNearestColor (Color toTranslate)
5465
{
5566
if (_nearestColorCache.TryGetValue (toTranslate, out var cachedAnswer))

Terminal.Gui/Drawing/Quant/PopularityPaletteWithThreshold.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ public class PopularityPaletteWithThreshold : IPaletteBuilder
1212
private readonly IColorDistance _colorDistance;
1313
private readonly double _mergeThreshold;
1414

15+
/// <summary>
16+
/// Creates a new instance of the <see cref="PopularityPaletteWithThreshold"/> class using
17+
/// the given threshold for merging similar colors.
18+
/// </summary>
19+
/// <param name="colorDistance"></param>
20+
/// <param name="mergeThreshold"></param>
1521
public PopularityPaletteWithThreshold (IColorDistance colorDistance, double mergeThreshold)
1622
{
1723
_colorDistance = colorDistance;
1824
_mergeThreshold = mergeThreshold; // Set the threshold for merging similar colors
1925
}
2026

27+
/// <inheritdoc/>
2128
public List<Color> BuildPalette (List<Color> colors, int maxColors)
2229
{
2330
if (colors == null || colors.Count == 0 || maxColors <= 0)
@@ -62,6 +69,7 @@ public List<Color> BuildPalette (List<Color> colors, int maxColors)
6269
/// Merge colors in the histogram if they are within the threshold distance
6370
/// </summary>
6471
/// <param name="colorHistogram"></param>
72+
/// <param name="maxColors"></param>
6573
/// <returns></returns>
6674
private Dictionary<Color, int> MergeSimilarColors (Dictionary<Color, int> colorHistogram, int maxColors)
6775
{

Terminal.Gui/Drawing/SixelEncoder.cs

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
// libsixel (C/C++) - https://github.com/saitoha/libsixel
55
// Copyright (c) 2014-2016 Hayaki Saito @license MIT
66

7-
using Terminal.Gui;
8-
97
namespace Terminal.Gui;
108

119
/// <summary>
12-
/// Encodes a images into the sixel console image output format.
10+
/// Encodes a images into the sixel console image output format.
1311
/// </summary>
1412
public class SixelEncoder
1513
{
@@ -35,32 +33,29 @@ e.g. to draw more color layers.
3533
3634
*/
3735

38-
3936
/// <summary>
40-
/// Gets or sets the quantizer responsible for building a representative
41-
/// limited color palette for images and for mapping novel colors in
42-
/// images to their closest palette color
37+
/// Gets or sets the quantizer responsible for building a representative
38+
/// limited color palette for images and for mapping novel colors in
39+
/// images to their closest palette color
4340
/// </summary>
4441
public ColorQuantizer Quantizer { get; set; } = new ();
4542

4643
/// <summary>
47-
/// Encode the given bitmap into sixel encoding
44+
/// Encode the given bitmap into sixel encoding
4845
/// </summary>
4946
/// <param name="pixels"></param>
5047
/// <returns></returns>
5148
public string EncodeSixel (Color [,] pixels)
5249
{
53-
5450
const string start = "\u001bP"; // Start sixel sequence
5551

56-
57-
string defaultRatios = this.AnyHasAlphaOfZero(pixels) ? "0;1;0": "0;0;0"; // Defaults for aspect ratio and grid size
52+
string defaultRatios = AnyHasAlphaOfZero (pixels) ? "0;1;0" : "0;0;0"; // Defaults for aspect ratio and grid size
5853
const string completeStartSequence = "q"; // Signals beginning of sixel image data
5954
const string noScaling = "\"1;1;"; // no scaling factors (1x1);
6055

6156
string fillArea = GetFillArea (pixels);
6257

63-
string pallette = GetColorPalette (pixels );
58+
string pallette = GetColorPalette (pixels);
6459

6560
string pixelData = WriteSixel (pixels);
6661

@@ -71,15 +66,14 @@ public string EncodeSixel (Color [,] pixels)
7166

7267
private string WriteSixel (Color [,] pixels)
7368
{
74-
75-
StringBuilder sb = new StringBuilder ();
69+
var sb = new StringBuilder ();
7670
int height = pixels.GetLength (1);
7771
int width = pixels.GetLength (0);
7872

7973
// Iterate over each 'row' of the image. Because each sixel write operation
8074
// outputs a screen area 6 pixels high (and 1+ across) we must process the image
8175
// 6 'y' units at once (1 band)
82-
for (int y = 0; y < height; y += 6)
76+
for (var y = 0; y < height; y += 6)
8377
{
8478
sb.Append (ProcessBand (pixels, y, Math.Min (6, height - y), width));
8579

@@ -107,69 +101,45 @@ private string ProcessBand (Color [,] pixels, int startY, int bandHeight, int wi
107101
Array.Fill (accu, (ushort)1);
108102
Array.Fill (slots, (short)-1);
109103

110-
var usedColorIdx = new List<int> ();
111-
var targets = new List<List<string>> ();
104+
List<int> usedColorIdx = new List<int> ();
105+
List<List<string>> targets = new List<List<string>> ();
112106

113107
// Process columns within the band
114-
for (int x = 0; x < width; ++x)
108+
for (var x = 0; x < width; ++x)
115109
{
116110
Array.Clear (code, 0, usedColorIdx.Count);
117-
bool anyNonTransparentPixel = false; // Track if any non-transparent pixels are found in this column
118111

119112
// Process each row in the 6-pixel high band
120-
for (int row = 0; row < bandHeight; ++row)
113+
for (var row = 0; row < bandHeight; ++row)
121114
{
122-
var color = pixels [x, startY + row];
115+
Color color = pixels [x, startY + row];
123116

124117
int colorIndex = Quantizer.GetNearestColor (color);
125118

126119
if (color.A == 0) // Skip fully transparent pixels
127120
{
128121
continue;
129122
}
130-
else
131-
{
132-
anyNonTransparentPixel = true;
133-
}
134123

135124
if (slots [colorIndex] == -1)
136125
{
137-
targets.Add (new List<string> ());
126+
targets.Add (new ());
127+
138128
if (x > 0)
139129
{
140130
last [usedColorIdx.Count] = 0;
141131
accu [usedColorIdx.Count] = (ushort)x;
142132
}
133+
143134
slots [colorIndex] = (short)usedColorIdx.Count;
144135
usedColorIdx.Add (colorIndex);
145136
}
146137

147138
code [slots [colorIndex]] |= (byte)(1 << row); // Accumulate SIXEL data
148139
}
149140

150-
/*
151-
// If no non-transparent pixels are found in the entire column, it's fully transparent
152-
if (!anyNonTransparentPixel)
153-
{
154-
// Emit fully transparent pixel data: #0!<width>?$
155-
result.Append ($"#0!{width}?");
156-
157-
// Add the line terminator: use "$-" if it's not the last line, "$" if it's the last line
158-
if (x < width - 1)
159-
{
160-
result.Append ("$-");
161-
}
162-
else
163-
{
164-
result.Append ("$");
165-
}
166-
167-
// Skip to the next column as we have already handled transparency
168-
continue;
169-
}*/
170-
171141
// Handle transitions between columns
172-
for (int j = 0; j < usedColorIdx.Count; ++j)
142+
for (var j = 0; j < usedColorIdx.Count; ++j)
173143
{
174144
if (code [j] == last [j])
175145
{
@@ -181,14 +151,15 @@ private string ProcessBand (Color [,] pixels, int startY, int bandHeight, int wi
181151
{
182152
targets [j].Add (CodeToSixel (last [j], accu [j]));
183153
}
154+
184155
last [j] = (sbyte)code [j];
185156
accu [j] = 1;
186157
}
187158
}
188159
}
189160

190161
// Process remaining data for this band
191-
for (int j = 0; j < usedColorIdx.Count; ++j)
162+
for (var j = 0; j < usedColorIdx.Count; ++j)
192163
{
193164
if (last [j] != 0)
194165
{
@@ -198,7 +169,8 @@ private string ProcessBand (Color [,] pixels, int startY, int bandHeight, int wi
198169

199170
// Build the final output for this band
200171
var result = new StringBuilder ();
201-
for (int j = 0; j < usedColorIdx.Count; ++j)
172+
173+
for (var j = 0; j < usedColorIdx.Count; ++j)
202174
{
203175
result.Append ($"#{usedColorIdx [j]}{string.Join ("", targets [j])}$");
204176
}
@@ -208,27 +180,42 @@ private string ProcessBand (Color [,] pixels, int startY, int bandHeight, int wi
208180

209181
private static string CodeToSixel (int code, int repeat)
210182
{
211-
char c = (char)(code + 63);
212-
if (repeat > 3) return "!" + repeat + c;
213-
if (repeat == 3) return c.ToString () + c + c;
214-
if (repeat == 2) return c.ToString () + c;
183+
var c = (char)(code + 63);
184+
185+
if (repeat > 3)
186+
{
187+
return "!" + repeat + c;
188+
}
189+
190+
if (repeat == 3)
191+
{
192+
return c.ToString () + c + c;
193+
}
194+
195+
if (repeat == 2)
196+
{
197+
return c.ToString () + c;
198+
}
199+
215200
return c.ToString ();
216201
}
217202

218203
private string GetColorPalette (Color [,] pixels)
219204
{
220205
Quantizer.BuildPalette (pixels);
221206

222-
StringBuilder paletteSb = new StringBuilder ();
207+
var paletteSb = new StringBuilder ();
223208

224-
for (int i = 0; i < Quantizer.Palette.Count; i++)
209+
for (var i = 0; i < Quantizer.Palette.Count; i++)
225210
{
226-
var color = Quantizer.Palette.ElementAt (i);
227-
paletteSb.AppendFormat ("#{0};2;{1};{2};{3}",
228-
i,
229-
color.R * 100 / 255,
230-
color.G * 100 / 255,
231-
color.B * 100 / 255);
211+
Color color = Quantizer.Palette.ElementAt (i);
212+
213+
paletteSb.AppendFormat (
214+
"#{0};2;{1};{2};{3}",
215+
i,
216+
color.R * 100 / 255,
217+
color.G * 100 / 255,
218+
color.B * 100 / 255);
232219
}
233220

234221
return paletteSb.ToString ();
@@ -241,15 +228,16 @@ private string GetFillArea (Color [,] pixels)
241228

242229
return $"{widthInChars};{heightInChars}";
243230
}
231+
244232
private bool AnyHasAlphaOfZero (Color [,] pixels)
245233
{
246234
int width = pixels.GetLength (0);
247235
int height = pixels.GetLength (1);
248236

249237
// Loop through each pixel in the 2D array
250-
for (int x = 0; x < width; x++)
238+
for (var x = 0; x < width; x++)
251239
{
252-
for (int y = 0; y < height; y++)
240+
for (var y = 0; y < height; y++)
253241
{
254242
// Check if the alpha component (A) is 0
255243
if (pixels [x, y].A == 0)
@@ -258,6 +246,7 @@ private bool AnyHasAlphaOfZero (Color [,] pixels)
258246
}
259247
}
260248
}
249+
261250
return false; // No pixel with A of 0 was found
262251
}
263-
}
252+
}

0 commit comments

Comments
 (0)