Terminal Graphics
Single-file, header-only C++20 functionality for producing graphics on the terminal using the sixel protocol
Loading...
Searching...
No Matches
TG Namespace Reference

The namespace within which all functionality is placed. More...

Classes

class  Font
 A class to hold the information about the font used for text rendering. More...
 
class  Image
 A simple class to hold a 2D image using datatype specified as ValueType template parameter. More...
 
class  magnify
 Adapter class to magnify an image. More...
 
class  Plot
 A class to provide plotting capabilities. More...
 
class  Rescale
 Adapter class to rescale intensities of image to colourmap indices. More...
 

Typedefs

using ctype = unsigned char
 the data type to use to store intensities:
 
using ColourMap = std::vector<std::array<ctype,3>>
 The data structure used to hold a colourmap.
 

Functions

ColourMap gray (int number=101)
 convenience function to generate a ready-made grayscale colourmap
 
ColourMap hot (int number=101)
 convenience function to generate a ready-made hot colourmap
 
ColourMap jet (int number=101)
 convenience function to generate a ready-made jet colourmap
 
template<class ImageType>
void imshow (const ImageType &image, const ColourMap &cmap, const bool zero_is_transparent=false)
 Display an indexed image to the terminal, according to the colourmap supplied.
 
template<class ImageType>
void imshow (const ImageType &image, double min, double max, const bool zero_is_transparent=false, const ColourMap &cmap=gray())
 Display a scalar image to the terminal, rescaled between (min, max)
 
Plot plot (int width=512, int height=256)
 Convenience function to use for immediate rendering.
 

Variables

constexpr std::string Home = "\033[H"
 VT100 code to set the cursor position to the top left of the screen.
 
constexpr std::string Clear = "\033[2J"
 VT100 code to clear the screen.
 
constexpr float lim_expand_by_factor = 0.1f
 

Detailed Description

The namespace within which all functionality is placed.

Typedef Documentation

◆ ctype

using TG::ctype = unsigned char

the data type to use to store intensities:

◆ ColourMap

using TG::ColourMap = std::vector<std::array<ctype,3>>

The data structure used to hold a colourmap.

A ColourMap is a way to associate an index with a colour. It can be represented as a simple table with 3 columns per row, to represent the red, green & blue components of each colour, one colour per row. When in use, the colour to be used is retrieved by looking up the values in the row at the specified index.

Since this is a simple structure, we use existing C++ objects to store this information, as a (dynamically-sized) vector of (fixed-sized) arrays of 3 values. For convenience, we define the shorthand name 'ColourMap' for this specific type.

Note that the sixel protocol we rely on in this implementation expects colourmap intensity values between 0 & 100.

Defining a simple colourmap can be done by list initialisation, for example:

const ColourMap my_cmap = {
               { 0, 0, 100 },     // pure red
               { 0, 100, 0 },     // pure green
               { 100, 0, 0 }      // pure blue
           };

More complex colourmaps can be generated if required, see implementation of the gray() colourmap for inspiration.

Function Documentation

◆ gray()

ColourMap TG::gray ( int number = 101)
inline

convenience function to generate a ready-made grayscale colourmap

◆ hot()

ColourMap TG::hot ( int number = 101)
inline

convenience function to generate a ready-made hot colourmap

◆ jet()

ColourMap TG::jet ( int number = 101)
inline

convenience function to generate a ready-made jet colourmap

◆ imshow() [1/2]

template<class ImageType>
void TG::imshow ( const ImageType & image,
const ColourMap & cmap,
const bool zero_is_transparent = false )
inline

Display an indexed image to the terminal, according to the colourmap supplied.

ImageType can be any object that implements the following methods:

  • int width() const
  • int height() const
  • integer_type operator() (int x, int y) const

Indexed images contain integer values that correspond to entries in the associated ColourMap. Different image values can have completely different colours, depending on the ColourMap used.

The ColourMap must be specified via the cmap argument. See the documentation for ColourMap for details.

◆ imshow() [2/2]

template<class ImageType>
void TG::imshow ( const ImageType & image,
double min,
double max,
const bool zero_is_transparent = false,
const ColourMap & cmap = gray() )
inline

Display a scalar image to the terminal, rescaled between (min, max)

ImageType can be any object that implements the following methods:

  • int width() const
  • int height() const
  • scalar_type operator() (int x, int y) const (where scalar_type can be any integer or floating-point type)

Note that as for most image formats, the x index rasters from left to right, while the y index rasters from top to bottom.

min & max specify how image values map to displayed intensities. Values <= min will render as pure black, while values >= max will render as pure white (assuming the default gray colourmap).

A different colourmap can be specified via the cmap argument. See the documentation for ColourMap for details on how to generate different colourmaps if necessary.

◆ plot()

Plot TG::plot ( int width = 512,
int height = 256 )
inline

Convenience function to use for immediate rendering.

This returns a (temporary) Plot object, which methods can be called on, and daisy-chain to achieve the desired series of commands. The Plot::show() method will implicitly be called when the temporary object is destroyed, in other words immediately after the closing semicolon.

For example:

std::vector x (10), y(10);
...
TG::plot (256,128)
  .set_ylim (-1,2)
  .add_line (x, y, 3)
  .set_grid (2, 0.5)
  .add_text ("my plot", 5, 2);

See methods in TG::Plot for details.

Variable Documentation

◆ Home

std::string TG::Home = "\033[H"
constexpr

VT100 code to set the cursor position to the top left of the screen.

This can be used in conjuction with TG::Clear to provide running updates. Simply feed the string to std::cout to issue the instruction, for example:

std::cout << TG::Clear;
while (true) {
std::cout << TG::Home << "Current progress:\n";
...
// perform computations, etc.
...
}
Plot & add_line(float x0, float y0, float x1, float y1, int colour_index=2, int stiple=0, float stiple_frac=0.5)
add a single line connection point (x0,y0) to (x1,y1).
Definition terminal_graphics.h:916
constexpr std::string Home
VT100 code to set the cursor position to the top left of the screen.
Definition terminal_graphics.h:141
constexpr std::string Clear
VT100 code to clear the screen.
Definition terminal_graphics.h:146
Plot plot(int width=512, int height=256)
Convenience function to use for immediate rendering.
Definition terminal_graphics.h:461
See also
TG::Clear

◆ Clear

std::string TG::Clear = "\033[2J"
constexpr

VT100 code to clear the screen.

See also
TG::Home

◆ lim_expand_by_factor

float TG::lim_expand_by_factor = 0.1f
constexpr