Coder Social home page Coder Social logo

ansi-art's Introduction

ANSI🔥Art

C++ ANSI🔥Art rendering library. Check out https://mrogalski.eu/ansi-art for an interactive demo.

Background

Original ANSI Art is typically limited to 16 colors and the character set used by the original IBM PC.

Times have changed. Modern terminal emulators support 24-bit RGB colors & can display arbitrary Unicode characters. This opens entirely new possibilities for the art that can be displayed in the terminal.

Let's call this 24-bit, Unicode-capable version of ANSI Art, an ANSI🔥Art (pronounced just like a regular ANSI Art, although with a sound of blazing fire in the background).

ANSI🔥Art can be used in the output of interactive CLI commands, SSH MOTD, an element of a ncurses interface or even for animation.

Here are some examples of what can be achieved:

Usage

This C++ library allows you to render any bitmap as an ANSI🔥Art.

The only dependency is FreeType. On Debian-based distributions it can be installed with sudo apt install libfreetype-dev.

In order to try out the library, run ./run.sh and observe the results in your terminal:

Example usage can be found in example.cc:

#include "maf/ansi_art.hh"

#include "example-image.h"
#include "example-font.h"

int main() {
  printf("Rendering... (this may take a few seconds)\n");
  auto art = maf::AnsiArt::New();
  art->LoadImage(example_image.width, example_image.height, example_image.pixel_data);
  art->LoadTTF(UbuntuMono_R_ttf, UbuntuMono_R_ttf_len);
  art->width = 80;
  art->Render();
  printf("%s\n", art->result_raw.c_str());
  delete art;
  return 0;
}

API

Full API can be found in maf/ansi_art.hh:

#pragma once

#include <string>

namespace maf {

class AnsiArt {
public:
  static AnsiArt *New();
  
  virtual ~AnsiArt(){};
  virtual std::string LoadTTF(const uint8_t *data, size_t size) = 0;
  virtual void LoadImage(int width, int height, const uint8_t *rgba_bytes) = 0;
  virtual void Render() = 0;

  virtual void StartRender(int n_threads) = 0;
  virtual float GetRenderProgress() = 0;
  virtual void CancelRender() = 0;

  int width = 80;
  std::string forbidden_characters = "";

  std::string glyphs_utf8;       // populated by LoadTTF
  std::string result_c;          // populated by Render
  std::string result_bash;       // populated by Render
  std::string result_raw;        // populated by Render
  int result_rgba_width;         // populated by Render
  int result_rgba_height;        // populated by Render
  std::string result_rgba_bytes; // populated by Render
};

} // namespace maf

ansi-art's People

Contributors

mafik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ansi-art's Issues

request for allow list

Similar to the forbidden list of characters, having a allow list of characters would be beneficial if a user wanted to recreate an image using only a selection of characters.

This behavior can be achieved with setting all non wanted characters in the forbidden list, but having an allow list can be a much easier option if the user only wants a relatively small list of characters.

Question about sample1.png, sample2.png, etc

Nice. I'm curious what font, emulator, width, etc were used to produce sample1.png? It looks crisper than example-result.png, which is what the result looks like on my setup.

"OffscreenCanvas is not defined" on load in Firefox 103.0

Doesn't seem to be working on the latest Firefox, 103.0.

App doesn't function, and dumps this to the console:

Uncaught ReferenceError: OffscreenCanvas is not defined
    LoadImage https://mrogalski.eu/ansi-art/script.js:104
    onRuntimeInitialized https://mrogalski.eu/ansi-art/script.js:136
    doRun https://mrogalski.eu/ansi-art/ansi.js:1
    run https://mrogalski.eu/ansi-art/ansi.js:1
    runCaller https://mrogalski.eu/ansi-art/ansi.js:1
    removeRunDependency https://mrogalski.eu/ansi-art/ansi.js:1
    receiveInstance https://mrogalski.eu/ansi-art/ansi.js:1
    onmessage https://mrogalski.eu/ansi-art/ansi.js:1
    loadWasmModuleToWorker https://mrogalski.eu/ansi-art/ansi.js:1
    receiveInstance https://mrogalski.eu/ansi-art/ansi.js:1
    receiveInstance https://mrogalski.eu/ansi-art/ansi.js:1
    receiveInstantiationResult https://mrogalski.eu/ansi-art/ansi.js:1
    promise callback*createWasm/instantiateAsync/< https://mrogalski.eu/ansi-art/ansi.js:1
    promise callback*instantiateAsync https://mrogalski.eu/ansi-art/ansi.js:1
    createWasm https://mrogalski.eu/ansi-art/ansi.js:1
    <anonymous> https://mrogalski.eu/ansi-art/ansi.js:1
script.js:104:16

Looks cool, though. Can't wait to give it a spin. 😎

Easy fixes

Neat package; better output than most ANSI renderers I've seen.

Few thoughts:

  1. You probably want to remove that 1MB ogg file from the repo that serves no purpose
  2. I'd be useful to have an example that didn't read from a pre-baked example-image.h and example-font.h. I think most people just want maf --font <optional font> image.png and get output. Next level would be a simple makefile that just has make and make install to .local/bin or whatever.
  3. On ubuntu 20.04 I needed this line to run your example:
g++ -pthread -std=c++2a -I. example.cc maf/*.cc `pkg-config --cflags --libs freetype2` -o example
  1. maybe toss all those example images in a docs\ folder?
  2. the repo could be quite a bit smaller if you convert those pngs to webp cwebp -q 80 sample1.png -o sample1.webp

:D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.