File: example.c

package info (click to toggle)
chafa 1.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,828 kB
  • sloc: ansic: 52,456; xml: 881; sh: 610; makefile: 466; python: 334
file content (63 lines) | stat: -rw-r--r-- 1,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* This file is in the public domain, and you are free to use it as you see fit.
 *
 * Compile with:
 *
 * cc example.c -o example $(pkg-config --libs --cflags chafa)
 */

#include <chafa.h>
#include <stdio.h>

#define PIX_WIDTH 3
#define PIX_HEIGHT 3
#define N_CHANNELS 4

int
main (int argc, char *argv [])
{
    const guint8 pixels [PIX_WIDTH * PIX_HEIGHT * N_CHANNELS] =
    {
        0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff,
        0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff,
        0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff
    };
    ChafaSymbolMap *symbol_map;
    ChafaCanvasConfig *config;
    ChafaCanvas *canvas;
    GString *gs;

    /* Specify the symbols we want */
    symbol_map = chafa_symbol_map_new ();
    chafa_symbol_map_add_by_tags (symbol_map, CHAFA_SYMBOL_TAG_ALL);

    /* Set up a configuration with the symbols and the canvas size in characters */
    config = chafa_canvas_config_new ();
    chafa_canvas_config_set_geometry (config, 40, 20);
    chafa_canvas_config_set_symbol_map (config, symbol_map);

    /* Create canvas */
    canvas = chafa_canvas_new (config);

    /* Draw pixels to canvas */
    chafa_canvas_draw_all_pixels (canvas,
                                  CHAFA_PIXEL_RGBA8_UNASSOCIATED,
                                  pixels,
                                  PIX_WIDTH,
                                  PIX_HEIGHT,
                                  PIX_WIDTH * N_CHANNELS);

    /* Generate a string that will show the canvas contents on a terminal */
    gs = chafa_canvas_print (canvas, NULL);

    /* Print the string */
    fwrite (gs->str, sizeof (char), gs->len, stdout);
    fputc ('\n', stdout);

    /* Free resources */
    g_string_free (gs, TRUE);
    chafa_canvas_unref (canvas);
    chafa_canvas_config_unref (config);
    chafa_symbol_map_unref (symbol_map);

    return 0;
}