Graphviz 13.0.0~dev.20250121.0651
Loading...
Searching...
No Matches
gvdevice_kitty.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <assert.h>
14#include <stdbool.h>
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <common/types.h>
19#include <gvc/gvio.h>
20#include <gvc/gvplugin_device.h>
21#include <util/alloc.h>
22#include <util/gv_math.h>
23
24#ifdef HAVE_LIBZ
25#include <zlib.h>
26#endif
27
28static size_t div_up(size_t dividend, size_t divisor) {
29 return dividend / divisor + (dividend % divisor != 0);
30}
31
32static const char base64_alphabet[] =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
34
35static size_t base64_encoded_size(size_t original_size) {
36 return div_up(original_size, 3) * 4;
37}
38
39static char *base64_encode(const unsigned char *data, size_t size) {
40 size_t buf_i = 0;
41 size_t data_i = 0;
42 char *buf = gv_alloc(base64_encoded_size(size));
43
44 while (data_i < size) {
45 int v;
46
47 unsigned char d0 = data[data_i];
48 v = (d0 & 0xFC) >> 2; // 1111_1100
49 buf[buf_i++] = base64_alphabet[v];
50
51 unsigned char d1 = data_i + 1 < size ? data[data_i + 1] : 0;
52 v = (d0 & 0x03) << 4 // 0000_0011
53 | (d1 & 0xF0) >> 4; // 1111_0000
54 buf[buf_i++] = base64_alphabet[v];
55 if (size <= data_i + 1) {
56 goto end;
57 }
58
59 unsigned char d2 = data_i + 2 < size ? data[data_i + 2] : 0;
60 v = (d1 & 0x0F) << 2 // 0000_1111
61 | (d2 & 0xC0) >> 6; // 1100_0000
62 buf[buf_i++] = base64_alphabet[v];
63 if (size <= data_i + 2) {
64 goto end;
65 }
66
67 v = d2 & 0x3F; // 0011_1111
68 buf[buf_i++] = base64_alphabet[v];
69
70 data_i += 3;
71 }
72
73end:
74 while (buf_i % 4 != 0) {
75 buf[buf_i++] = base64_alphabet[64];
76 }
77
78 return buf;
79}
80
81static void kitty_write(unsigned char *data, size_t data_size, unsigned width,
82 unsigned height, bool is_compressed) {
83 const size_t chunk_size = 4096;
84 char *output = base64_encode(data, data_size);
85 size_t offset = 0;
86 size_t size = base64_encoded_size(data_size);
87
88 while (offset < size) {
89 int has_next_chunk = offset + chunk_size <= size;
90 if (offset == 0) {
91 printf("\033_Ga=T,f=32,s=%u,v=%u%s%s;", width, height,
92 chunk_size < size ? ",m=1" : "", is_compressed ? ",o=z" : "");
93 } else {
94 printf("\033_Gm=%d;", has_next_chunk);
95 }
96
97 size_t this_chunk_size = has_next_chunk ? chunk_size : size - offset;
98 fwrite(output + offset, this_chunk_size, 1, stdout);
99 printf("\033\\");
100 offset += chunk_size;
101 }
102 printf("\n");
103
104 free(output);
105}
106
107static void kitty_format(GVJ_t *job) {
108 unsigned char *imagedata = job->imagedata;
109 size_t imagedata_size = job->width * job->height * BYTES_PER_PIXEL;
110 argb2rgba(job->width, job->height, imagedata);
111
112 kitty_write(imagedata, imagedata_size, job->width, job->height, false);
113}
114
116 GVDEVICE_DOES_TRUECOLOR, /* flags */
117 {0., 0.}, /* default margin - points */
118 {0., 0.}, /* default page width, height - points */
119 {96., 96.}, /* dpi */
120};
121
123
124#ifdef HAVE_LIBZ
125static int zlib_compress(unsigned char *source, uLong source_len,
126 unsigned char **dest, size_t *dest_len) {
127 uLong dest_cap = compressBound(source_len);
128 *dest = gv_alloc(dest_cap);
129
130 const int ret = compress(*dest, &dest_cap, source, source_len);
131 *dest_len = dest_cap;
132 return ret;
133}
134
135static void zkitty_format(GVJ_t *job) {
136 unsigned char *imagedata = job->imagedata;
137 const uLong imagedata_size = job->width * job->height * BYTES_PER_PIXEL;
138 argb2rgba(job->width, job->height, imagedata);
139
140 unsigned char *zbuf;
141 size_t zsize;
142 int ret = zlib_compress(imagedata, imagedata_size, &zbuf, &zsize);
143 assert(ret == Z_OK);
144 (void)ret;
145
146 kitty_write(zbuf, zsize, job->width, job->height, true);
147
148 free(zbuf);
149}
150
151static gvdevice_features_t device_features_zkitty = {
152 GVDEVICE_DOES_TRUECOLOR, /* flags */
153 {0., 0.}, /* default margin - points */
154 {0., 0.}, /* default page width, height - points */
155 {96., 96.}, /* dpi */
156};
157
158static gvdevice_engine_t device_engine_zkitty = {.format = zkitty_format};
159#endif
160
162 {0, "kitty:cairo", 0, &device_engine_kitty, &device_features_kitty},
163#ifdef HAVE_LIBZ
164 {1, "kittyz:cairo", 1, &device_engine_zkitty, &device_features_zkitty},
165#endif
166 {0, NULL, 0, NULL, NULL}};
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
static double compress(info *nl, int nn)
Definition constraint.c:646
void free(void *)
node NULL
Definition grammar.y:163
Arithmetic helper functions.
@ BYTES_PER_PIXEL
Definition gv_math.h:87
static void argb2rgba(size_t width, size_t height, unsigned char *data)
Definition gv_math.h:105
swig_ptr_object_handlers offset
Definition gv_php.cpp:5907
#define GVDEVICE_DOES_TRUECOLOR
Definition gvcjob.h:90
static void kitty_format(GVJ_t *job)
static char * base64_encode(const unsigned char *data, size_t size)
static gvdevice_engine_t device_engine_kitty
static size_t base64_encoded_size(size_t original_size)
static size_t div_up(size_t dividend, size_t divisor)
static const char base64_alphabet[]
gvplugin_installed_t gvdevice_types_kitty[]
static gvdevice_features_t device_features_kitty
static void kitty_write(unsigned char *data, size_t data_size, unsigned width, unsigned height, bool is_compressed)
unsigned char * imagedata
location of imagedata
Definition gvcjob.h:297
unsigned int width
Definition gvcjob.h:327
unsigned int height
Definition gvcjob.h:328
Definition legal.c:50
void(* format)(GVJ_t *firstjob)
ingroup plugin_api
Definition gvplugin.h:35
graphs, nodes and edges info: Agraphinfo_t, Agnodeinfo_t and Agedgeinfo_t