SixelEncoderTests.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Color = Terminal.Gui.Color;
  7. namespace UnitTests.Drawing;
  8. public class SixelEncoderTests
  9. {
  10. [Fact]
  11. public void EncodeSixel_RedSquare12x12_ReturnsExpectedSixel ()
  12. {
  13. var expected = "\u001bP" + // Start sixel sequence
  14. "0;0;0" + // Defaults for aspect ratio and grid size
  15. "q" + // Signals beginning of sixel image data
  16. "\"1;1;12;2" + // no scaling factors (1x1) and filling 12px width with 2 'sixel' height = 12 px high
  17. /*
  18. * Definition of the color palette
  19. */
  20. "#0;2;100;0;0" + // Red color definition in the format "#<index>;<type>;<R>;<G>;<B>" - 2 means RGB. The values range 0 to 100
  21. /*
  22. * Start of the Pixel data
  23. * We draw 6 rows at once, so end up with 2 'lines'
  24. * Both are basically the same and terminate with dollar hyphen (except last row)
  25. * Format is:
  26. * #0 (selects to use color palette index 0 i.e. red)
  27. * !12 (repeat next byte 12 times i.e. the whole length of the row)
  28. * ~ (the byte 111111 i.e. fill completely)
  29. * $ (return to start of line)
  30. * - (move down to next line)
  31. */
  32. "#0!12~$-" +
  33. "#0!12~$" + // Next 6 rows of red pixels
  34. "\u001b\\"; // End sixel sequence
  35. // Arrange: Create a 12x12 bitmap filled with red
  36. var pixels = new Color [12, 12];
  37. for (int x = 0; x < 12; x++)
  38. {
  39. for (int y = 0; y < 12; y++)
  40. {
  41. pixels [x, y] = new Color(255,0,0);
  42. }
  43. }
  44. // Act: Encode the image
  45. var encoder = new SixelEncoder (); // Assuming SixelEncoder is the class that contains the EncodeSixel method
  46. string result = encoder.EncodeSixel (pixels);
  47. // Since image is only red we should only have 1 color definition
  48. Color c1 = Assert.Single (encoder.Quantizer.Palette);
  49. Assert.Equal (new Color(255,0,0),c1);
  50. Assert.Equal (expected, result);
  51. }
  52. }