Images.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ofd.Dispose ();
  59. return;
  60. }
  61. string path = ofd.FilePaths [0];
  62. ofd.Dispose ();
  63. if (string.IsNullOrWhiteSpace (path))
  64. {
  65. return;
  66. }
  67. if (!File.Exists (path))
  68. {
  69. return;
  70. }
  71. Image<Rgba32> img;
  72. try
  73. {
  74. img = Image.Load<Rgba32> (File.ReadAllBytes (path));
  75. }
  76. catch (Exception ex)
  77. {
  78. MessageBox.ErrorQuery ("Could not open file", ex.Message, "Ok");
  79. return;
  80. }
  81. imageView.SetImage (img);
  82. Application.Refresh ();
  83. };
  84. }
  85. private class ImageView : View
  86. {
  87. private readonly ConcurrentDictionary<Rgba32, Attribute> _cache = new ();
  88. private Image<Rgba32> _fullResImage;
  89. private Image<Rgba32> _matchSize;
  90. public override void OnDrawContent (Rectangle bounds)
  91. {
  92. base.OnDrawContent (bounds);
  93. if (_fullResImage == null)
  94. {
  95. return;
  96. }
  97. // if we have not got a cached resized image of this size
  98. if (_matchSize == null || bounds.Width != _matchSize.Width || bounds.Height != _matchSize.Height)
  99. {
  100. // generate one
  101. _matchSize = _fullResImage.Clone (x => x.Resize (bounds.Width, bounds.Height));
  102. }
  103. for (var y = 0; y < bounds.Height; y++)
  104. {
  105. for (var x = 0; x < bounds.Width; x++)
  106. {
  107. Rgba32 rgb = _matchSize [x, y];
  108. Attribute attr = _cache.GetOrAdd (
  109. rgb,
  110. rgb => new Attribute (
  111. new Color (),
  112. new Color (rgb.R, rgb.G, rgb.B)
  113. )
  114. );
  115. Driver.SetAttribute (attr);
  116. AddRune (x, y, (Rune)' ');
  117. }
  118. }
  119. }
  120. internal void SetImage (Image<Rgba32> image)
  121. {
  122. _fullResImage = image;
  123. SetNeedsDisplay ();
  124. }
  125. }
  126. }