123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /*************************************************************************
- * Copyright (c) 2011 AT&T Intellectual Property
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors: Details at https://graphviz.org
- *************************************************************************/
- #include "config.h"
- #include <assert.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <common/types.h>
- #include <gvc/gvio.h>
- #include <gvc/gvplugin_device.h>
- #include <util/alloc.h>
- #ifdef HAVE_LIBZ
- #include <zlib.h>
- #endif
- static void fix_colors(unsigned char *imagedata, size_t imagedata_size) {
- for (size_t i = 0; i < imagedata_size; i += 4) {
- const unsigned char blue = imagedata[i];
- const unsigned char red = imagedata[i + 2];
- imagedata[i] = red;
- imagedata[i + 2] = blue;
- }
- }
- static size_t div_up(size_t dividend, size_t divisor) {
- return dividend / divisor + (dividend % divisor != 0);
- }
- static const char base64_alphabet[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- static size_t base64_encoded_size(size_t original_size) {
- return div_up(original_size, 3) * 4;
- }
- static char *base64_encode(const unsigned char *data, size_t size) {
- size_t buf_i = 0;
- size_t data_i = 0;
- char *buf = gv_alloc(base64_encoded_size(size));
- while (data_i < size) {
- int v;
- unsigned char d0 = data[data_i];
- v = (d0 & 0xFC) >> 2; // 1111_1100
- buf[buf_i++] = base64_alphabet[v];
- unsigned char d1 = data_i + 1 < size ? data[data_i + 1] : 0;
- v = (d0 & 0x03) << 4 // 0000_0011
- | (d1 & 0xF0) >> 4; // 1111_0000
- buf[buf_i++] = base64_alphabet[v];
- if (size <= data_i + 1) {
- goto end;
- }
- unsigned char d2 = data_i + 2 < size ? data[data_i + 2] : 0;
- v = (d1 & 0x0F) << 2 // 0000_1111
- | (d2 & 0xC0) >> 6; // 1100_0000
- buf[buf_i++] = base64_alphabet[v];
- if (size <= data_i + 2) {
- goto end;
- }
- v = d2 & 0x3F; // 0011_1111
- buf[buf_i++] = base64_alphabet[v];
- data_i += 3;
- }
- end:
- while (buf_i % 4 != 0) {
- buf[buf_i++] = base64_alphabet[64];
- }
- return buf;
- }
- static void kitty_write(unsigned char *data, size_t data_size, unsigned width,
- unsigned height, bool is_compressed) {
- const size_t chunk_size = 4096;
- char *output = base64_encode(data, data_size);
- size_t offset = 0;
- size_t size = base64_encoded_size(data_size);
- while (offset < size) {
- int has_next_chunk = offset + chunk_size <= size;
- if (offset == 0) {
- printf("\033_Ga=T,f=32,s=%u,v=%u%s%s;", width, height,
- chunk_size < size ? ",m=1" : "", is_compressed ? ",o=z" : "");
- } else {
- printf("\033_Gm=%d;", has_next_chunk);
- }
- size_t this_chunk_size = has_next_chunk ? chunk_size : size - offset;
- fwrite(output + offset, this_chunk_size, 1, stdout);
- printf("\033\\");
- offset += chunk_size;
- }
- printf("\n");
- free(output);
- }
- static void kitty_format(GVJ_t *job) {
- unsigned char *imagedata = (unsigned char *)job->imagedata;
- size_t imagedata_size = job->width * job->height * 4;
- fix_colors(imagedata, imagedata_size);
- kitty_write(imagedata, imagedata_size, job->width, job->height, false);
- }
- static gvdevice_features_t device_features_kitty = {
- GVDEVICE_DOES_TRUECOLOR, /* flags */
- {0., 0.}, /* default margin - points */
- {0., 0.}, /* default page width, height - points */
- {96., 96.}, /* dpi */
- };
- static gvdevice_engine_t device_engine_kitty = {.format = kitty_format};
- #ifdef HAVE_LIBZ
- static int zlib_compress(unsigned char *source, uLong source_len,
- unsigned char **dest, size_t *dest_len) {
- uLong dest_cap = compressBound(source_len);
- *dest = gv_alloc(dest_cap);
- const int ret = compress(*dest, &dest_cap, source, source_len);
- *dest_len = dest_cap;
- return ret;
- }
- static void zkitty_format(GVJ_t *job) {
- unsigned char *imagedata = (unsigned char *)job->imagedata;
- const uLong imagedata_size = job->width * job->height * 4;
- fix_colors(imagedata, imagedata_size);
- unsigned char *zbuf;
- size_t zsize;
- int ret = zlib_compress(imagedata, imagedata_size, &zbuf, &zsize);
- assert(ret == Z_OK);
- (void)ret;
- kitty_write(zbuf, zsize, job->width, job->height, true);
- free(zbuf);
- }
- static gvdevice_features_t device_features_zkitty = {
- GVDEVICE_DOES_TRUECOLOR, /* flags */
- {0., 0.}, /* default margin - points */
- {0., 0.}, /* default page width, height - points */
- {96., 96.}, /* dpi */
- };
- static gvdevice_engine_t device_engine_zkitty = {.format = zkitty_format};
- #endif
- gvplugin_installed_t gvdevice_types_kitty[] = {
- {0, "kitty:cairo", 0, &device_engine_kitty, &device_features_kitty},
- #ifdef HAVE_LIBZ
- {1, "kittyz:cairo", 1, &device_engine_zkitty, &device_features_zkitty},
- #endif
- {0, NULL, 0, NULL, NULL}};
|