123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using System;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using SixLabors.ImageSharp;
- using SixLabors.ImageSharp.PixelFormats;
- using SixLabors.ImageSharp.Processing;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Animation", "Demonstration of how to render animated images with threading.")]
- [ScenarioCategory ("Threading")]
- [ScenarioCategory ("Drawing")]
- public class AnimationScenario : Scenario
- {
- private bool _isDisposed;
- public override void Main ()
- {
- Application.Init();
- var win = new Window
- {
- Title = GetQuitKeyAndName (),
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- };
- var imageView = new ImageView { Width = Dim.Fill (), Height = Dim.Fill () - 2 };
- win.Add (imageView);
- var lbl = new Label { Y = Pos.AnchorEnd (), Text = "Image by Wikiscient" };
- win.Add (lbl);
- var lbl2 = new Label
- {
- X = Pos.AnchorEnd(), Y = Pos.AnchorEnd (), Text = "https://commons.wikimedia.org/wiki/File:Spinning_globe.gif"
- };
- win.Add (lbl2);
- DirectoryInfo dir;
- string assemblyLocation = Assembly.GetExecutingAssembly ().Location;
- if (!string.IsNullOrEmpty (assemblyLocation))
- {
- dir = new DirectoryInfo (Path.GetDirectoryName (assemblyLocation));
- }
- else
- {
- dir = new DirectoryInfo (AppContext.BaseDirectory);
- }
- var f = new FileInfo (
- Path.Combine (dir.FullName, "Scenarios\\AnimationScenario", "Spinning_globe_dark_small.gif")
- );
- if (!f.Exists)
- {
- MessageBox.ErrorQuery ("Could not find gif", "Could not find " + f.FullName, "Ok");
- return;
- }
- imageView.SetImage (Image.Load<Rgba32> (File.ReadAllBytes (f.FullName)));
- Task.Run (
- () =>
- {
- while (!_isDisposed)
- {
- // When updating from a Thread/Task always use Invoke
- Application.Invoke (
- () =>
- {
- imageView.NextFrame ();
- imageView.SetNeedsDisplay ();
- }
- );
- Task.Delay (100).Wait ();
- }
- }
- );
- Application.Run (win);
- win.Dispose ();
- Application.Shutdown ();
- }
- protected override void Dispose (bool disposing)
- {
- _isDisposed = true;
- base.Dispose (disposing);
- }
- // This is a C# port of https://github.com/andraaspar/bitmap-to-braille by Andraaspar
- /// <summary>Renders an image as unicode Braille.</summary>
- public class BitmapToBraille
- {
- public const int CHAR_HEIGHT = 4;
- public const int CHAR_WIDTH = 2;
- private const string CHARS =
- " ⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿";
- public BitmapToBraille (int widthPixels, int heightPixels, Func<int, int, bool> pixelIsLit)
- {
- WidthPixels = widthPixels;
- HeightPixels = heightPixels;
- PixelIsLit = pixelIsLit;
- }
- public int HeightPixels { get; }
- public Func<int, int, bool> PixelIsLit { get; }
- public int WidthPixels { get; }
- public string GenerateImage ()
- {
- var imageHeightChars = (int)Math.Ceiling ((double)HeightPixels / CHAR_HEIGHT);
- var imageWidthChars = (int)Math.Ceiling ((double)WidthPixels / CHAR_WIDTH);
- var result = new StringBuilder ();
- for (var y = 0; y < imageHeightChars; y++)
- {
- for (var x = 0; x < imageWidthChars; x++)
- {
- int baseX = x * CHAR_WIDTH;
- int baseY = y * CHAR_HEIGHT;
- var charIndex = 0;
- var value = 1;
- for (var charX = 0; charX < CHAR_WIDTH; charX++)
- {
- for (var charY = 0; charY < CHAR_HEIGHT; charY++)
- {
- int bitmapX = baseX + charX;
- int bitmapY = baseY + charY;
- bool pixelExists = bitmapX < WidthPixels && bitmapY < HeightPixels;
- if (pixelExists && PixelIsLit (bitmapX, bitmapY))
- {
- charIndex += value;
- }
- value *= 2;
- }
- }
- result.Append (CHARS [charIndex]);
- }
- result.Append ('\n');
- }
- return result.ToString ().TrimEnd ();
- }
- }
- private class ImageView : View
- {
- private string [] brailleCache;
- private int currentFrame;
- private int frameCount;
- private Image<Rgba32> [] fullResImages;
- private Image<Rgba32> [] matchSizes;
- private Rectangle oldSize = Rectangle.Empty;
- public void NextFrame () { currentFrame = (currentFrame + 1) % frameCount; }
- protected override bool OnDrawingContent (Rectangle viewport)
- {
- if (oldSize != Viewport)
- {
- // Invalidate cached images now size has changed
- matchSizes = new Image<Rgba32> [frameCount];
- brailleCache = new string [frameCount];
- oldSize = Viewport;
- }
- Image<Rgba32> imgScaled = matchSizes [currentFrame];
- string braille = brailleCache [currentFrame];
- if (imgScaled == null)
- {
- Image<Rgba32> imgFull = fullResImages [currentFrame];
- // keep aspect ratio
- int newSize = Math.Min (Viewport.Width, Viewport.Height);
- // generate one
- matchSizes [currentFrame] = imgScaled = imgFull.Clone (
- x => x.Resize (
- newSize * BitmapToBraille.CHAR_HEIGHT,
- newSize * BitmapToBraille.CHAR_HEIGHT
- )
- );
- }
- if (braille == null)
- {
- brailleCache [currentFrame] = braille = GetBraille (matchSizes [currentFrame]);
- }
- string [] lines = braille.Split ('\n');
- for (var y = 0; y < lines.Length; y++)
- {
- string line = lines [y];
- for (var x = 0; x < line.Length; x++)
- {
- AddRune (x, y, (Rune)line [x]);
- }
- }
- return true;
- }
- internal void SetImage (Image<Rgba32> image)
- {
- frameCount = image.Frames.Count;
- fullResImages = new Image<Rgba32> [frameCount];
- matchSizes = new Image<Rgba32> [frameCount];
- brailleCache = new string [frameCount];
- for (var i = 0; i < frameCount - 1; i++)
- {
- fullResImages [i] = image.Frames.ExportFrame (0);
- }
- fullResImages [frameCount - 1] = image;
- SetNeedsDisplay ();
- }
- private string GetBraille (Image<Rgba32> img)
- {
- var braille = new BitmapToBraille (
- img.Width,
- img.Height,
- (x, y) => IsLit (img, x, y)
- );
- return braille.GenerateImage ();
- }
- private bool IsLit (Image<Rgba32> img, int x, int y)
- {
- Rgba32 rgb = img [x, y];
- return rgb.R + rgb.G + rgb.B > 50;
- }
- }
- }
|