Animation.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using SixLabors.ImageSharp;
  2. using SixLabors.ImageSharp.ColorSpaces;
  3. using SixLabors.ImageSharp.PixelFormats;
  4. using SixLabors.ImageSharp.Processing;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Terminal.Gui;
  13. using Attribute = Terminal.Gui.Attribute;
  14. namespace UICatalog.Scenarios {
  15. [ScenarioMetadata (Name: "Animation", Description: "Demonstration of how to render animated images with threading.")]
  16. [ScenarioCategory ("Colors")]
  17. public class Animation : Scenario
  18. {
  19. private bool isDisposed;
  20. public override void Setup ()
  21. {
  22. base.Setup ();
  23. var imageView = new ImageView () {
  24. Width = Dim.Fill(),
  25. Height = Dim.Fill()-2,
  26. };
  27. Win.Add (imageView);
  28. var lbl = new Label("Image by Wikiscient"){
  29. Y = Pos.AnchorEnd(2)
  30. };
  31. Win.Add(lbl);
  32. var lbl2 = new Label("https://commons.wikimedia.org/wiki/File:Spinning_globe.gif"){
  33. Y = Pos.AnchorEnd(1)
  34. };
  35. Win.Add(lbl2);
  36. var dir = new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
  37. var f = new FileInfo(
  38. Path.Combine(dir.FullName,"Scenarios","Spinning_globe_dark_small.gif"));
  39. if(!f.Exists)
  40. {
  41. MessageBox.ErrorQuery("Could not find gif","Could not find "+ f.FullName,"Ok");
  42. return;
  43. }
  44. imageView.SetImage(Image.Load<Rgba32> (File.ReadAllBytes (f.FullName)));
  45. Task.Run(()=>{
  46. while(!isDisposed)
  47. {
  48. Application.MainLoop.Invoke(()=>
  49. {
  50. imageView.NextFrame();
  51. imageView.SetNeedsDisplay();
  52. });
  53. Task.Delay(100).Wait();
  54. }
  55. });
  56. }
  57. protected override void Dispose(bool disposing)
  58. {
  59. isDisposed = true;
  60. base.Dispose();
  61. }
  62. // This is a C# port of https://github.com/andraaspar/bitmap-to-braille by Andraaspar
  63. /// <summary>
  64. /// Renders an image as unicode Braille.
  65. /// </summary>
  66. public class BitmapToBraille
  67. {
  68. public const int CHAR_WIDTH = 2;
  69. public const int CHAR_HEIGHT = 4;
  70. const string CHARS = " ⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿";
  71. public int WidthPixels {get; }
  72. public int HeightPixels { get; }
  73. public Func<int,int,bool> PixelIsLit {get;}
  74. public BitmapToBraille (int widthPixels, int heightPixels, Func<int, int, bool> pixelIsLit)
  75. {
  76. WidthPixels = widthPixels;
  77. HeightPixels = heightPixels;
  78. PixelIsLit = pixelIsLit;
  79. }
  80. public string GenerateImage() {
  81. int imageHeightChars = (int) Math.Ceiling((double)HeightPixels / CHAR_HEIGHT);
  82. int imageWidthChars = (int) Math.Ceiling((double)WidthPixels / CHAR_WIDTH);
  83. var result = new StringBuilder();
  84. for (int y = 0; y < imageHeightChars; y++) {
  85. for (int x = 0; x < imageWidthChars; x++) {
  86. int baseX = x * CHAR_WIDTH;
  87. int baseY = y * CHAR_HEIGHT;
  88. int charIndex = 0;
  89. int value = 1;
  90. for (int charX = 0; charX < CHAR_WIDTH; charX++) {
  91. for (int charY = 0; charY < CHAR_HEIGHT; charY++) {
  92. int bitmapX = baseX + charX;
  93. int bitmapY = baseY + charY;
  94. bool pixelExists = bitmapX < WidthPixels && bitmapY < HeightPixels;
  95. if (pixelExists && PixelIsLit(bitmapX, bitmapY)) {
  96. charIndex += value;
  97. }
  98. value *= 2;
  99. }
  100. }
  101. result.Append(CHARS[charIndex]);
  102. }
  103. result.Append('\n');
  104. }
  105. return result.ToString().TrimEnd();
  106. }
  107. }
  108. class ImageView : View {
  109. private int frameCount;
  110. private int currentFrame = 0;
  111. private Image<Rgba32>[] fullResImages;
  112. private Image<Rgba32>[] matchSizes;
  113. Rect oldSize = Rect.Empty;
  114. internal void SetImage (Image<Rgba32> image)
  115. {
  116. frameCount = image.Frames.Count;
  117. fullResImages = new Image<Rgba32>[frameCount];
  118. matchSizes = new Image<Rgba32>[frameCount];
  119. for(int i=0;i<frameCount-1;i++)
  120. {
  121. fullResImages[i] = image.Frames.ExportFrame(0);
  122. }
  123. fullResImages[frameCount-1] = image;
  124. this.SetNeedsDisplay ();
  125. }
  126. public void NextFrame()
  127. {
  128. currentFrame = (currentFrame+1)%frameCount;
  129. }
  130. public override void Redraw (Rect bounds)
  131. {
  132. base.Redraw (bounds);
  133. if(oldSize != bounds)
  134. {
  135. // Invalidate cached images now size has changed
  136. matchSizes = new Image<Rgba32>[frameCount];
  137. oldSize = bounds;
  138. }
  139. var imgScaled = matchSizes[currentFrame];
  140. if(imgScaled == null)
  141. {
  142. var imgFull = fullResImages[currentFrame];
  143. // keep aspect ratio
  144. var newSize = Math.Min(bounds.Width,bounds.Height);
  145. // generate one
  146. matchSizes[currentFrame] = imgFull.Clone (
  147. x => x.Resize (
  148. newSize * BitmapToBraille.CHAR_HEIGHT,
  149. newSize * BitmapToBraille.CHAR_HEIGHT));
  150. }
  151. var lines = GetBraille(matchSizes[currentFrame]);
  152. for(int y = 0; y < lines.Length;y++)
  153. {
  154. var line = lines[y];
  155. for(int x = 0;x<line.Length ;x++)
  156. {
  157. AddRune(x,y,line[x]);
  158. }
  159. }
  160. }
  161. private string[] GetBraille (Image<Rgba32> img)
  162. {
  163. var braille = new BitmapToBraille(
  164. img.Width,
  165. img.Height,
  166. (x,y)=>IsLit(img,x,y));
  167. var pix = braille.GenerateImage();
  168. return pix.Split('\n');
  169. }
  170. private bool IsLit (Image<Rgba32> img, int x, int y)
  171. {
  172. var rgb = img[x,y];
  173. return rgb.R + rgb.G + rgb.B > 50;
  174. }
  175. }
  176. }
  177. }