Images.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Text;
  5. using SixLabors.ImageSharp;
  6. using SixLabors.ImageSharp.PixelFormats;
  7. using SixLabors.ImageSharp.Processing;
  8. using Terminal.Gui;
  9. using Color = Terminal.Gui.Color;
  10. namespace UICatalog.Scenarios;
  11. [ScenarioMetadata ("Images", "Demonstration of how to render an image with/without true color support.")]
  12. [ScenarioCategory ("Colors")]
  13. [ScenarioCategory ("Drawing")]
  14. public class Images : Scenario
  15. {
  16. public override void Setup ()
  17. {
  18. base.Setup ();
  19. bool canTrueColor = Application.Driver.SupportsTrueColor;
  20. var lblDriverName = new Label { X = 0, Y = 0, Text = $"Driver is {Application.Driver.GetType ().Name}" };
  21. Win.Add (lblDriverName);
  22. var cbSupportsTrueColor = new CheckBox
  23. {
  24. X = Pos.Right (lblDriverName) + 2,
  25. Y = 0,
  26. Checked = canTrueColor,
  27. CanFocus = false,
  28. Text = "supports true color "
  29. };
  30. Win.Add (cbSupportsTrueColor);
  31. var cbUseTrueColor = new CheckBox
  32. {
  33. X = Pos.Right (cbSupportsTrueColor) + 2,
  34. Y = 0,
  35. Checked = !Application.Force16Colors,
  36. Enabled = canTrueColor,
  37. Text = "Use true color"
  38. };
  39. cbUseTrueColor.Toggled += (_, evt) => Application.Force16Colors = !evt.NewValue ?? false;
  40. Win.Add (cbUseTrueColor);
  41. var btnOpenImage = new Button { X = Pos.Right (cbUseTrueColor) + 2, Y = 0, Text = "Open Image" };
  42. Win.Add (btnOpenImage);
  43. var imageView = new ImageView
  44. {
  45. X = 0, Y = Pos.Bottom (lblDriverName), Width = Dim.Fill (), Height = Dim.Fill ()
  46. };
  47. Win.Add (imageView);
  48. btnOpenImage.Accept += (_, _) =>
  49. {
  50. var ofd = new OpenDialog { Title = "Open Image", AllowsMultipleSelection = false };
  51. Application.Run (ofd);
  52. if (ofd.Path is { })
  53. {
  54. Directory.SetCurrentDirectory (Path.GetFullPath (Path.GetDirectoryName (ofd.Path)!));
  55. }
  56. if (ofd.Canceled)
  57. {
  58. return;
  59. }
  60. string path = ofd.FilePaths [0];
  61. if (string.IsNullOrWhiteSpace (path))
  62. {
  63. return;
  64. }
  65. if (!File.Exists (path))
  66. {
  67. return;
  68. }
  69. Image<Rgba32> img;
  70. try
  71. {
  72. img = Image.Load<Rgba32> (File.ReadAllBytes (path));
  73. }
  74. catch (Exception ex)
  75. {
  76. MessageBox.ErrorQuery ("Could not open file", ex.Message, "Ok");
  77. return;
  78. }
  79. imageView.SetImage (img);
  80. Application.Refresh ();
  81. };
  82. }
  83. private class ImageView : View
  84. {
  85. private readonly ConcurrentDictionary<Rgba32, Attribute> _cache = new ();
  86. private Image<Rgba32> _fullResImage;
  87. private Image<Rgba32> _matchSize;
  88. public override void OnDrawContent (Rectangle bounds)
  89. {
  90. base.OnDrawContent (bounds);
  91. if (_fullResImage == null)
  92. {
  93. return;
  94. }
  95. // if we have not got a cached resized image of this size
  96. if (_matchSize == null || bounds.Width != _matchSize.Width || bounds.Height != _matchSize.Height)
  97. {
  98. // generate one
  99. _matchSize = _fullResImage.Clone (x => x.Resize (bounds.Width, bounds.Height));
  100. }
  101. for (var y = 0; y < bounds.Height; y++)
  102. {
  103. for (var x = 0; x < bounds.Width; x++)
  104. {
  105. Rgba32 rgb = _matchSize [x, y];
  106. Attribute attr = _cache.GetOrAdd (
  107. rgb,
  108. rgb => new Attribute (
  109. new Color (),
  110. new Color (rgb.R, rgb.G, rgb.B)
  111. )
  112. );
  113. Driver.SetAttribute (attr);
  114. AddRune (x, y, (Rune)' ');
  115. }
  116. }
  117. }
  118. internal void SetImage (Image<Rgba32> image)
  119. {
  120. _fullResImage = image;
  121. SetNeedsDisplay ();
  122. }
  123. }
  124. }