Graphviz 13.0.0~dev.20241220.2304
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
57// number of bytes per pixel
58static const unsigned BPP = 4;
59
60static void process(GVJ_t *job, int color_depth) {
61
62 unsigned char *data = job->imagedata;
63
64 assert(color_depth == 3 || color_depth == 24);
65
66 for (unsigned y = 0; y < job->height; y += 2) {
67 for (unsigned x = 0; x < job->width; ++x) {
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
121static unsigned rgb_to_grayscale(unsigned red, unsigned green, unsigned blue) {
122
125
126 const double r_linear = red / 255.0;
127 const double g_linear = green / 255.0;
128 const double b_linear = blue / 255.0;
129
130 const double y_linear =
131 0.2126 * r_linear + 0.7152 * g_linear + 0.0722 * b_linear;
132 return (unsigned)(y_linear * 255.999);
133}
134
141static void processNup(GVJ_t *job, unsigned y_stride, unsigned x_stride,
142 const char **tiles) {
143 assert(y_stride > 0);
144 assert(x_stride > 0);
145 assert(tiles != NULL);
146 for (unsigned i = 0; i < y_stride; ++i) {
147 for (unsigned j = 0; j < x_stride; ++j) {
148 assert(tiles[i * x_stride + j] != NULL && "missing or not enough tiles");
149 }
150 }
151
152 unsigned char *data = job->imagedata;
153
154 for (unsigned y = 0; y < job->height; y += y_stride) {
155 for (unsigned x = 0; x < job->width; x += x_stride) {
156
157 unsigned index = 0;
158
159 for (unsigned y_offset = 0;
160 y + y_offset < job->height && y_offset < y_stride; ++y_offset) {
161 for (unsigned x_offset = 0;
162 x + x_offset < job->width && x_offset < x_stride; ++x_offset) {
163
164 const unsigned offset =
165 (y + y_offset) * job->width * BPP + (x + x_offset) * BPP;
166 const unsigned red = data[offset + 2];
167 const unsigned green = data[offset + 1];
168 const unsigned blue = data[offset];
169
170 const unsigned gray = rgb_to_grayscale(red, green, blue);
171 // The [0, 256) grayscale measurement can be quantized into 16
172 // 16-stride buckets. I.e. [0, 16) as bucket 1, [16, 32) as bucket 2,
173 // … Drawing a threshold at 240, and considering only the last bucket
174 // to be white when converting to monochrome empirically seems to
175 // generate reasonable results.
176 const unsigned pixel = gray >= 240;
177
178 index |= pixel << (y_offset * x_stride + x_offset);
179 }
180 }
181
182 gvputs(job, tiles[index]);
183 }
184 gvputc(job, '\n');
185 }
186}
187
189static void process4up(GVJ_t *job) {
190 // block characters from the “Amstrad CPC character set”
191 const char *tiles[] = {" ", "▘", "▝", "▀", "▖", "▍", "▞", "▛",
192 "▗", "▚", "▐", "▜", "▃", "▙", "▟", "█"};
193 const unsigned y_stride = 2;
194 const unsigned x_stride = 2;
195 assert(sizeof(tiles) / sizeof(tiles[0]) == 1 << (y_stride * x_stride));
196 processNup(job, y_stride, x_stride, tiles);
197}
198
200static void process6up(GVJ_t *job) {
201 // the “Teletext G1 Block Mosaics Set”
202 const char *tiles[] = {" ", "🬀", "🬁", "🬂", "🬃", "🬄", "🬅", "🬆", "🬇", "🬈", "🬉",
203 "🬊", "🬋", "🬌", "🬍", "🬎", "🬏", "🬐", "🬑", "🬒", "🬓", "▌",
204 "🬔", "🬕", "🬖", "🬗", "🬘", "🬙", "🬚", "🬛", "🬜", "🬝", "🬞",
205 "🬟", "🬠", "🬡", "🬢", "🬣", "🬤", "🬥", "🬦", "🬧", "▐", "🬨",
206 "🬩", "🬪", "🬫", "🬬", "🬭", "🬮", "🬯", "🬰", "🬱", "🬲", "🬳",
207 "🬴", "🬵", "🬶", "🬷", "🬸", "🬹", "🬺", "🬻", "█"};
208 const unsigned y_stride = 3;
209 const unsigned x_stride = 2;
210 assert(sizeof(tiles) / sizeof(tiles[0]) == 1 << (y_stride * x_stride));
211 processNup(job, y_stride, x_stride, tiles);
212}
213
215static void process8up(GVJ_t *job) {
216 // the Unicode “Braille Patterns” block
217 const char *tiles[] = {
218 " ", "⠁", "⠈", "⠉", "⠂", "⠃", "⠊", "⠋", "⠐", "⠑", "⠘", "⠙", "⠒", "⠓", "⠚",
219 "⠛", "⠄", "⠅", "⠌", "⠍", "⠆", "⠇", "⠎", "⠏", "⠔", "⠕", "⠜", "⠝", "⠖", "⠗",
220 "⠞", "⠟", "⠠", "⠡", "⠨", "⠩", "⠢", "⠣", "⠪", "⠫", "⠰", "⠱", "⠸", "⠹", "⠲",
221 "⠳", "⠺", "⠻", "⠤", "⠥", "⠬", "⠭", "⠦", "⠧", "⠮", "⠯", "⠴", "⠵", "⠼", "⠽",
222 "⠶", "⠷", "⠾", "⠿", "⡀", "⡁", "⡈", "⡉", "⡂", "⡃", "⡊", "⡋", "⡐", "⡑", "⡘",
223 "⡙", "⡒", "⡓", "⡚", "⡛", "⡄", "⡅", "⡌", "⡍", "⡆", "⡇", "⡎", "⡏", "⡔", "⡕",
224 "⡜", "⡝", "⡖", "⡗", "⡞", "⡟", "⡠", "⡡", "⡨", "⡩", "⡢", "⡣", "⡪", "⡫", "⡰",
225 "⡱", "⡸", "⡹", "⡲", "⡳", "⡺", "⡻", "⡤", "⡥", "⡬", "⡭", "⡦", "⡧", "⡮", "⡯",
226 "⡴", "⡵", "⡼", "⡽", "⡶", "⡷", "⡾", "⡿", "⢀", "⢁", "⢈", "⢉", "⢂", "⢃", "⢊",
227 "⢋", "⢐", "⢑", "⢘", "⢙", "⢒", "⢓", "⢚", "⢛", "⢄", "⢅", "⢌", "⢍", "⢆", "⢇",
228 "⢎", "⢏", "⢔", "⢕", "⢜", "⢝", "⢖", "⢗", "⢞", "⢟", "⢠", "⢡", "⢨", "⢩", "⢢",
229 "⢣", "⢪", "⢫", "⢰", "⢱", "⢸", "⢹", "⢲", "⢳", "⢺", "⢻", "⢤", "⢥", "⢬", "⢭",
230 "⢦", "⢧", "⢮", "⢯", "⢴", "⢵", "⢼", "⢽", "⢶", "⢷", "⢾", "⢿", "⣀", "⣁", "⣈",
231 "⣉", "⣂", "⣃", "⣊", "⣋", "⣐", "⣑", "⣘", "⣙", "⣒", "⣓", "⣚", "⣛", "⣄", "⣅",
232 "⣌", "⣍", "⣆", "⣇", "⣎", "⣏", "⣔", "⣕", "⣜", "⣝", "⣖", "⣗", "⣞", "⣟", "⣠",
233 "⣡", "⣨", "⣩", "⣢", "⣣", "⣪", "⣫", "⣰", "⣱", "⣸", "⣹", "⣲", "⣳", "⣺", "⣻",
234 "⣤", "⣥", "⣬", "⣭", "⣦", "⣧", "⣮", "⣯", "⣴", "⣵", "⣼", "⣽", "⣶", "⣷", "⣾",
235 "⣿"};
236 const unsigned y_stride = 4;
237 const unsigned x_stride = 2;
238 assert(sizeof(tiles) / sizeof(tiles[0]) == 1 << (y_stride * x_stride));
239 processNup(job, y_stride, x_stride, tiles);
240}
241
243 .format = process3,
244};
245
249
253
257
261
263 .default_dpi = {96, 96},
264};
265
267 {8, "vt:cairo", 0, &engine3, &device_features},
268 {1 << 24, "vt-24bit:cairo", 0, &engine24, &device_features},
269 {4, "vt-4up:cairo", 0, &engine4up, &device_features},
270 {6, "vt-6up:cairo", 0, &engine6up, &device_features},
271 {7, "vt-8up:cairo", 0, &engine8up, &device_features},
272 {0},
273};
274
276 {API_device, device_types},
277 {(api_t)0, 0},
278};
279
280#ifdef GVDLL
281#define GVPLUGIN_VT_API __declspec(dllexport)
282#else
283#define GVPLUGIN_VT_API
284#endif
285
node NULL
Definition grammar.y:163
swig_ptr_object_handlers offset
Definition gv_php.cpp:5907
api_t
Definition gvcext.h:32
int gvputc(GVJ_t *job, int c)
Definition gvdevice.c:291
int gvputs(GVJ_t *job, const char *s)
Definition gvdevice.c:264
void gvprintf(GVJ_t *job, const char *format,...)
Definition gvdevice.c:395
static void process(GVJ_t *job, int color_depth)
Definition gvplugin_vt.c:60
static void process6up(GVJ_t *job)
draw a 6-pixels-per-character monochrome image
static gvplugin_installed_t device_types[]
static gvdevice_engine_t engine8up
#define GVPLUGIN_VT_API
static gvdevice_features_t device_features
static void process8up(GVJ_t *job)
draw a 8-pixels-per-character monochrome image
static unsigned rgb_to_grayscale(unsigned red, unsigned green, unsigned blue)
convert an RGB color to grayscale
static gvdevice_engine_t engine24
static const color_t COLORS[]
ANSI 3-bit colors.
Definition gvplugin_vt.c:22
static void process4up(GVJ_t *job)
draw a 4-pixels-per-character monochrome image
static gvdevice_engine_t engine3
static void process24(GVJ_t *job)
static void process3(GVJ_t *job)
static gvdevice_engine_t engine4up
static void processNup(GVJ_t *job, unsigned y_stride, unsigned x_stride, const char **tiles)
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 const unsigned BPP
Definition gvplugin_vt.c:58
static gvplugin_api_t apis[]
static gvdevice_engine_t engine6up
static unsigned get_color(unsigned red, unsigned green, unsigned blue)
find closest ANSI color
Definition gvplugin_vt.c:44
unsigned char * imagedata
location of 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