Skip to content

mafik/ansi-art

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Ā 

History

11 Commits
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 
Ā 

Repository files navigation

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