Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sixel support #3734

Draft
wants to merge 32 commits into
base: v2_develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4d1d740
Initial exploration of how to sixel encode
tznind Sep 9, 2024
f096062
Add ColorQuantizer
tznind Sep 9, 2024
943fa11
Work on SixelEncoder
tznind Sep 9, 2024
c6281dd
Build color palette using median cut instead of naive method
tznind Sep 11, 2024
b482306
Fix build errors
tznind Sep 11, 2024
3081765
Refactoring and comments
tznind Sep 11, 2024
e334bfd
Refactor and split into seperate files WIP
tznind Sep 11, 2024
891adec
Switch to a new WriteSixel algorithm
tznind Sep 13, 2024
484b75a
Output palette in Images scenario
tznind Sep 14, 2024
68d5e99
Fix ConvertToColorArray and namespaces
tznind Sep 14, 2024
d747867
Fix comment
tznind Sep 14, 2024
f103b04
Attribution for the WriteSixel method
tznind Sep 15, 2024
cbef6c5
Add comments
tznind Sep 15, 2024
eaa5c0e
Simplify and speed up palette building
tznind Sep 15, 2024
f8bb2f0
Fix infinite loop building palette
tznind Sep 15, 2024
f40b7b4
Fix test and make comments clearer
tznind Sep 22, 2024
93ce9a8
Add sixel test for grid 3x3 to make 12x12 checkerboard
tznind Sep 22, 2024
ef56998
Tidy up test file and comments in NetDriver
tznind Sep 23, 2024
a7c65bf
Move lab colors to UICatalog
tznind Sep 23, 2024
f07ab92
Switch to simpler and faster palette builder
tznind Sep 23, 2024
2378570
Fix early exit bug in palette builder and change to far more conserva…
tznind Sep 23, 2024
b9bb2ba
Merge branch 'v2_develop' into sixel-encoder-tinkering
tznind Sep 23, 2024
4571978
Fix fill area - y is not in sixels its in pixels
tznind Sep 25, 2024
3c6804a
Move license to top of page and credit both source repos
tznind Sep 25, 2024
6149c5f
Add 2 tabs to Image scenario - one for sixel one for basic
tznind Sep 26, 2024
050db7a
Merge branch 'v2_develop' into sixel-encoder-tinkering
tznind Sep 26, 2024
9322b9a
Output at specific position
tznind Sep 26, 2024
519d8c0
Investigate changing sixel to output as part of view render
tznind Sep 28, 2024
94cbc1c
Restore the static approach to rendering for now and fix dispose
tznind Sep 28, 2024
d16f1b6
Fix tabbing into sixel tab view
tznind Sep 28, 2024
18f185d
Fix scenario dispose
tznind Sep 28, 2024
db0fc41
Determine whether sixel is supported by issuing a Device Attributes R…
tznind Sep 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Terminal.Gui/Application/Application.Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
/// </remarks>
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
public static string ForceDriver { get; set; } = string.Empty;

public static string Sixel;

Check warning on line 30 in Terminal.Gui/Application/Application.Driver.cs

View workflow job for this annotation

GitHub Actions / build_release

Missing XML comment for publicly visible type or member 'Application.Sixel'

Check warning on line 30 in Terminal.Gui/Application/Application.Driver.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (ubuntu-latest)

Missing XML comment for publicly visible type or member 'Application.Sixel'

Check warning on line 30 in Terminal.Gui/Application/Application.Driver.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (windows-latest)

Missing XML comment for publicly visible type or member 'Application.Sixel'

Check warning on line 30 in Terminal.Gui/Application/Application.Driver.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Missing XML comment for publicly visible type or member 'Application.Sixel'
}
6 changes: 6 additions & 0 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ public override void UpdateScreen ()
SetCursorPosition (lastCol, row);
Console.Write (output);
}

if (!string.IsNullOrWhiteSpace(Application.Sixel))
{
Console.SetCursorPosition (0,0);
Console.Write (Application.Sixel);
}
}

SetCursorPosition (0, 0);
Expand Down
17 changes: 17 additions & 0 deletions Terminal.Gui/Drawing/Quant/CIE76ColorDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Terminal.Gui;

/// <summary>
/// This is the simplest method to measure color difference in the CIE Lab color space. The Euclidean distance in Lab
/// space is more aligned with human perception than RGB space, as Lab attempts to model how humans perceive color differences.
/// </summary>
public class CIE76ColorDistance : LabColorDistance
{
public override double CalculateDistance (Color c1, Color c2)

Check warning on line 9 in Terminal.Gui/Drawing/Quant/CIE76ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_release

Missing XML comment for publicly visible type or member 'CIE76ColorDistance.CalculateDistance(Color, Color)'

Check warning on line 9 in Terminal.Gui/Drawing/Quant/CIE76ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (ubuntu-latest)

Missing XML comment for publicly visible type or member 'CIE76ColorDistance.CalculateDistance(Color, Color)'

Check warning on line 9 in Terminal.Gui/Drawing/Quant/CIE76ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (windows-latest)

Missing XML comment for publicly visible type or member 'CIE76ColorDistance.CalculateDistance(Color, Color)'

Check warning on line 9 in Terminal.Gui/Drawing/Quant/CIE76ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Missing XML comment for publicly visible type or member 'CIE76ColorDistance.CalculateDistance(Color, Color)'
{
var lab1 = RgbToLab (c1);
var lab2 = RgbToLab (c2);

// Euclidean distance in Lab color space
return Math.Sqrt (Math.Pow (lab1.L - lab2.L, 2) + Math.Pow (lab1.A - lab2.A, 2) + Math.Pow (lab1.B - lab2.B, 2));
}
}
45 changes: 45 additions & 0 deletions Terminal.Gui/Drawing/Quant/CIE94ColorDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Terminal.Gui;

/// <summary>
/// CIE94 improves on CIE76 by introducing adjustments for chroma (color intensity) and lightness.
/// This algorithm considers human visual perception more accurately by scaling differences in lightness and chroma.
/// It is better but slower than <see cref="CIE76ColorDistance"/>.
/// </summary>
public class CIE94ColorDistance : LabColorDistance
tznind marked this conversation as resolved.
Show resolved Hide resolved
{
// Constants for CIE94 formula (can be modified for different use cases like textiles or graphics)
private const double kL = 1.0;
private const double kC = 1.0;
private const double kH = 1.0;

public override double CalculateDistance (Color first, Color second)

Check warning on line 15 in Terminal.Gui/Drawing/Quant/CIE94ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_release

Missing XML comment for publicly visible type or member 'CIE94ColorDistance.CalculateDistance(Color, Color)'

Check warning on line 15 in Terminal.Gui/Drawing/Quant/CIE94ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (ubuntu-latest)

Missing XML comment for publicly visible type or member 'CIE94ColorDistance.CalculateDistance(Color, Color)'

Check warning on line 15 in Terminal.Gui/Drawing/Quant/CIE94ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (windows-latest)

Missing XML comment for publicly visible type or member 'CIE94ColorDistance.CalculateDistance(Color, Color)'

Check warning on line 15 in Terminal.Gui/Drawing/Quant/CIE94ColorDistance.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Missing XML comment for publicly visible type or member 'CIE94ColorDistance.CalculateDistance(Color, Color)'
{
var lab1 = RgbToLab (first);
var lab2 = RgbToLab (second);

// Delta L, A, B
double deltaL = lab1.L - lab2.L;
double deltaA = lab1.A - lab2.A;
double deltaB = lab1.B - lab2.B;

// Chroma values for both colors
double c1 = Math.Sqrt (lab1.A * lab1.A + lab1.B * lab1.B);
double c2 = Math.Sqrt (lab2.A * lab2.A + lab2.B * lab2.B);
double deltaC = c1 - c2;

// Delta H (calculated indirectly)
double deltaH = Math.Sqrt (Math.Pow (deltaA, 2) + Math.Pow (deltaB, 2) - Math.Pow (deltaC, 2));

// Scaling factors
double sL = 1.0;
double sC = 1.0 + 0.045 * c1;
double sH = 1.0 + 0.015 * c1;

// CIE94 color difference formula
return Math.Sqrt (
Math.Pow (deltaL / (kL * sL), 2) +
Math.Pow (deltaC / (kC * sC), 2) +
Math.Pow (deltaH / (kH * sH), 2)
);
}
}
70 changes: 70 additions & 0 deletions Terminal.Gui/Drawing/Quant/ColorQuantizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@


namespace Terminal.Gui;

/// <summary>
/// Translates colors in an image into a Palette of up to 256 colors.
/// </summary>
public class ColorQuantizer
{
/// <summary>
/// Gets the current colors in the palette based on the last call to
/// <see cref="BuildPalette"/>.
/// </summary>
public IReadOnlyCollection<Color> Palette { get; private set; } = new List<Color> ();

/// <summary>
/// Gets or sets the maximum number of colors to put into the <see cref="Palette"/>.
/// Defaults to 256 (the maximum for sixel images).
/// </summary>
public int MaxColors { get; set; } = 256;

/// <summary>
/// Gets or sets the algorithm used to map novel colors into existing
/// palette colors (closest match). Defaults to <see cref="CIE94ColorDistance"/>
/// </summary>
public IColorDistance DistanceAlgorithm { get; set; } = new CIE94ColorDistance ();

/// <summary>
/// Gets or sets the algorithm used to build the <see cref="Palette"/>.
/// </summary>
public IPaletteBuilder PaletteBuildingAlgorithm { get; set; } = new MedianCutPaletteBuilder (new EuclideanColorDistance ()) ;

public void BuildPalette (Color [,] pixels)

Check warning on line 33 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_release

Missing XML comment for publicly visible type or member 'ColorQuantizer.BuildPalette(Color[*,*])'

Check warning on line 33 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (ubuntu-latest)

Missing XML comment for publicly visible type or member 'ColorQuantizer.BuildPalette(Color[*,*])'

Check warning on line 33 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (windows-latest)

Missing XML comment for publicly visible type or member 'ColorQuantizer.BuildPalette(Color[*,*])'

Check warning on line 33 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Missing XML comment for publicly visible type or member 'ColorQuantizer.BuildPalette(Color[*,*])'
{
List<Color> allColors = new List<Color> ();
int width = pixels.GetLength (0);
int height = pixels.GetLength (1);

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
allColors.Add (pixels [x, y]);
}
}

Palette = PaletteBuildingAlgorithm.BuildPalette (allColors, MaxColors);
}

public int GetNearestColor (Color toTranslate)

Check warning on line 50 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_release

Missing XML comment for publicly visible type or member 'ColorQuantizer.GetNearestColor(Color)'

Check warning on line 50 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (ubuntu-latest)

Missing XML comment for publicly visible type or member 'ColorQuantizer.GetNearestColor(Color)'

Check warning on line 50 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (windows-latest)

Missing XML comment for publicly visible type or member 'ColorQuantizer.GetNearestColor(Color)'

Check warning on line 50 in Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

View workflow job for this annotation

GitHub Actions / build_and_test_debug (macos-latest)

Missing XML comment for publicly visible type or member 'ColorQuantizer.GetNearestColor(Color)'
{
// Simple nearest color matching based on DistanceAlgorithm
double minDistance = double.MaxValue;
int nearestIndex = 0;

for (var index = 0; index < Palette.Count; index++)
{
Color color = Palette.ElementAt (index);
double distance = DistanceAlgorithm.CalculateDistance (color, toTranslate);

if (distance < minDistance)
{
minDistance = distance;
nearestIndex = index;
}
}

return nearestIndex;
}
}
16 changes: 16 additions & 0 deletions Terminal.Gui/Drawing/Quant/EuclideanColorDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Terminal.Gui;

/// <summary>
/// Calculates the distance between two colors using Euclidean distance in 3D RGB space.
/// This measures the straight-line distance between the two points representing the colors.
/// </summary>
public class EuclideanColorDistance : IColorDistance
{
public double CalculateDistance (Color c1, Color c2)
{
int rDiff = c1.R - c2.R;
int gDiff = c1.G - c2.G;
int bDiff = c1.B - c2.B;
return Math.Sqrt (rDiff * rDiff + gDiff * gDiff + bDiff * bDiff);
}
}
18 changes: 18 additions & 0 deletions Terminal.Gui/Drawing/Quant/IColorDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Terminal.Gui;

/// <summary>
/// Interface for algorithms that compute the relative distance between pairs of colors.
/// This is used for color matching to a limited palette, such as in Sixel rendering.
/// </summary>
public interface IColorDistance
{
/// <summary>
/// Computes a similarity metric between two <see cref="Color"/> instances.
/// A larger value indicates more dissimilar colors, while a smaller value indicates more similar colors.
/// The metric is internally consistent for the given algorithm.
/// </summary>
/// <param name="c1">The first color.</param>
/// <param name="c2">The second color.</param>
/// <returns>A numeric value representing the distance between the two colors.</returns>
double CalculateDistance (Color c1, Color c2);
}
17 changes: 17 additions & 0 deletions Terminal.Gui/Drawing/Quant/IPaletteBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Terminal.Gui;

/// <summary>
/// Builds a palette of a given size for a given set of input colors.
/// </summary>
public interface IPaletteBuilder
{
/// <summary>
/// Reduce the number of <paramref name="colors"/> to <paramref name="maxColors"/> (or less)
/// using an appropriate selection algorithm.
/// </summary>
/// <param name="colors">Color of every pixel in the image. Contains duplication in order
/// to support algorithms that weigh how common a color is.</param>
/// <param name="maxColors">The maximum number of colours that should be represented.</param>
/// <returns></returns>
List<Color> BuildPalette (List<Color> colors, int maxColors);
}
52 changes: 52 additions & 0 deletions Terminal.Gui/Drawing/Quant/LabColorDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using ColorHelper;

namespace Terminal.Gui;

public abstract class LabColorDistance : IColorDistance
{
// Reference white point for D65 illuminant (can be moved to constants)
private const double RefX = 95.047;
private const double RefY = 100.000;
private const double RefZ = 108.883;

// Conversion from RGB to Lab
protected LabColor RgbToLab (Color c)
{
var xyz = ColorHelper.ColorConverter.RgbToXyz (new RGB (c.R, c.G, c.B));

// Normalize XYZ values by reference white point
double x = xyz.X / RefX;
double y = xyz.Y / RefY;
double z = xyz.Z / RefZ;

// Apply the nonlinear transformation for Lab
x = x > 0.008856 ? Math.Pow (x, 1.0 / 3.0) : 7.787 * x + 16.0 / 116.0;
y = y > 0.008856 ? Math.Pow (y, 1.0 / 3.0) : 7.787 * y + 16.0 / 116.0;
z = z > 0.008856 ? Math.Pow (z, 1.0 / 3.0) : 7.787 * z + 16.0 / 116.0;

// Calculate Lab values
double l = 116.0 * y - 16.0;
double a = 500.0 * (x - y);
double b = 200.0 * (y - z);

return new LabColor (l, a, b);
}

// LabColor class encapsulating L, A, and B values
protected class LabColor
{
public double L { get; }
public double A { get; }
public double B { get; }

public LabColor (double l, double a, double b)
{
L = l;
A = a;
B = b;
}
}

/// <inheritdoc />
public abstract double CalculateDistance (Color c1, Color c2);
}
Loading
Loading