Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
gvplugin_vt.c
Go to the documentation of this file.
1
3
4#include <assert.h>
5#include <gvc/gvplugin.h>
7#include <limits.h>
8#include <stddef.h>
9
10#include <gvc/gvio.h>
11
13typedef struct {
14 unsigned value;
15 unsigned red;
16 unsigned green;
17 unsigned blue;
18
19} color_t;
20
22static const color_t COLORS[] = {
23 {0, 0x00, 0x00, 0x00},
24 {1, 0xff, 0x00, 0x00},
25 {2, 0x00, 0xff, 0x00},
26 {3, 0xff, 0xff, 0x00},
27 {4, 0x00, 0x00, 0xff},
28 {5, 0xff, 0x00, 0xff},
29 {6, 0x00, 0xff, 0xff},
30 {7, 0xff, 0xff, 0xff},
31};
32
34static unsigned distance(const color_t base, unsigned red, unsigned green,
35 unsigned blue) {
36 unsigned diff = 0;
37 diff += red > base.red ? red - base.red : base.red - red;
38 diff += green > base.green ? green - base.green : base.green - green;
39 diff += blue > base.blue ? blue - base.blue : base.blue - blue;
40 return diff;
41}
42
44static unsigned get_color(unsigned red, unsigned green, unsigned blue) {
45 unsigned winner = 0;
46 unsigned diff = UINT_MAX;
47 for (size_t i = 0; i < sizeof(COLORS) / sizeof(COLORS[0]); ++i) {
48 unsigned d = distance(COLORS[i], red, green, blue);
49 if (d < diff) {
50 diff = d;
51 winner = COLORS[i].value;
52 }
53 }
54 return winner;
55}
56
57static void process(GVJ_t *job, int color_depth) {
58
59 unsigned char *data = (unsigned char *)job->imagedata;
60
61 assert(color_depth == 3 || color_depth == 24);
62
63 for (unsigned y = 0; y < job->height; y += 2) {
64 for (unsigned x = 0; x < job->width; ++x) {
65
66 // number of bytes per pixel
67 const unsigned BPP = 4;
68
69 {
70 // extract the upper pixel
71 unsigned offset = y * job->width * BPP + x * BPP;
72 unsigned red = data[offset + 2];
73 unsigned green = data[offset + 1];
74 unsigned blue = data[offset];
75
76 // use this to select a foreground color
77 if (color_depth == 3) {
78 unsigned fg = get_color(red, green, blue);
79 gvprintf(job, "\033[3%um", fg);
80 } else {
81 assert(color_depth == 24);
82 gvprintf(job, "\033[38;2;%u;%u;%um", red, green, blue);
83 }
84 }
85
86 {
87 // extract the lower pixel
88 unsigned red = 0;
89 unsigned green = 0;
90 unsigned blue = 0;
91 if (y + 1 < job->height) {
92 unsigned offset = (y + 1) * job->width * BPP + x * BPP;
93 red = data[offset + 2];
94 green = data[offset + 1];
95 blue = data[offset];
96 }
97
98 // use this to select a background color
99 if (color_depth == 3) {
100 unsigned bg = get_color(red, green, blue);
101 gvprintf(job, "\033[4%um", bg);
102 } else {
103 assert(color_depth == 24);
104 gvprintf(job, "\033[48;2;%u;%u;%um", red, green, blue);
105 }
106 }
107
108 // print unicode “upper half block” to effectively do two rows of
109 // pixels per one terminal row
110 gvprintf(job, "▀\033[0m");
111 }
112 gvprintf(job, "\n");
113 }
114}
115
116static void process3(GVJ_t *job) { process(job, 3); }
117
118static void process24(GVJ_t *job) { process(job, 24); }
119
121 .format = process3,
122};
123
127
129 .default_dpi = {96, 96},
130};
131
133 {8, "vt:cairo", 0, &engine3, &device_features},
134 {1 << 24, "vt-24bit:cairo", 0, &engine24, &device_features},
135 {0},
136};
137
139 {API_device, device_types},
140 {(api_t)0, 0},
141};
142
143#ifdef GVDLL
144#define GVPLUGIN_VT_API __declspec(dllexport)
145#else
146#define GVPLUGIN_VT_API
147#endif
148
swig_ptr_object_handlers offset
Definition gv_php.cpp:5915
api_t
Definition gvcext.h:32
void gvprintf(GVJ_t *job, const char *format,...)
Definition gvdevice.c:394
static void process(GVJ_t *job, int color_depth)
Definition gvplugin_vt.c:57
static gvplugin_installed_t device_types[]
#define GVPLUGIN_VT_API
static gvdevice_features_t device_features
static gvdevice_engine_t engine24
static const color_t COLORS[]
ANSI 3-bit colors.
Definition gvplugin_vt.c:22
static gvdevice_engine_t engine3
static void process24(GVJ_t *job)
static void process3(GVJ_t *job)
static unsigned distance(const color_t base, unsigned red, unsigned green, unsigned blue)
a metric of “closeness” to a given color
Definition gvplugin_vt.c:34
GVPLUGIN_VT_API gvplugin_library_t gvplugin_vt_LTX_library
static gvplugin_api_t apis[]
static unsigned get_color(unsigned red, unsigned green, unsigned blue)
find closest ANSI color
Definition gvplugin_vt.c:44
char * imagedata
Definition gvcjob.h:297
unsigned int width
Definition gvcjob.h:327
unsigned int height
Definition gvcjob.h:328
an ANSI color
Definition gvplugin_vt.c:13
unsigned green
Definition gvplugin_vt.c:16
unsigned blue
Definition gvplugin_vt.c:17
unsigned value
Definition gvplugin_vt.c:14
unsigned red
Definition gvplugin_vt.c:15
Definition legal.c:50
void(* format)(GVJ_t *firstjob)
ingroup plugin_api
Definition gvplugin.h:35