Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gvdevice_gdk.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#include <assert.h>
13#include <gvc/gvplugin_device.h>
14#include <gvc/gvio.h>
15#include <limits.h>
16#include <util/unreachable.h>
17#ifdef HAVE_PANGOCAIRO
18#include <gdk-pixbuf/gdk-pixbuf.h>
19
20typedef enum {
27
28/*
29 * Does an in-place conversion of a CAIRO ARGB32 image to GDK RGBA
30 */
31static void argb2rgba(unsigned width, unsigned height, unsigned char *data) {
32/* define indexes to color bytes in each format */
33#define Ba 0
34#define Ga 1
35#define Ra 2
36#define Aa 3
37
38#define Rb 0
39#define Gb 1
40#define Bb 2
41#define Ab 3
42
43 unsigned int x, y;
44
45 for (y = 0; y < height; y++) {
46 for (x = 0; x < width; x++) {
47 /* swap red and blue */
48 unsigned char r = data[Ra];
49 data[Bb] = data[Ba];
50 data[Rb] = r;
51 data += 4;
52 }
53 }
54}
55
56static gboolean writer(const char *buf, gsize count, GError **error,
57 void *data) {
58 (void)error;
59 return count == gvwrite(data, buf, count);
60}
61
62static void gdk_format(GVJ_t * job)
63{
64 char *format_str = "";
65 GdkPixbuf *pixbuf;
66
67 switch (job->device.id) {
68 case FORMAT_BMP:
69 format_str = "bmp";
70 break;
71 case FORMAT_ICO:
72 format_str = "ico";
73 break;
74 case FORMAT_JPEG:
75 format_str = "jpeg";
76 break;
77 case FORMAT_PNG:
78 format_str = "png";
79 break;
80 case FORMAT_TIFF:
81 format_str = "tiff";
82 break;
83 default:
85 }
86
87 argb2rgba(job->width, job->height, job->imagedata);
88
89 assert(job->width <= (unsigned)INT_MAX / 4 && "width out of range");
90 assert(job->height <= (unsigned)INT_MAX && "height out of range");
91
92 pixbuf = gdk_pixbuf_new_from_data(
93 job->imagedata, // data
94 GDK_COLORSPACE_RGB, // colorspace
95 TRUE, // has_alpha
96 8, // bits_per_sample
97 (int)job->width, // width
98 (int)job->height, // height
99 4 * (int)job->width, // rowstride
100 NULL, // destroy_fn
101 NULL // destroy_fn_data
102 );
103
104 gdk_pixbuf_save_to_callback(pixbuf, writer, job, format_str, NULL, NULL);
105
106 g_object_unref(pixbuf);
107}
108
109static gvdevice_engine_t gdk_engine = {
110 NULL, /* gdk_initialize */
111 gdk_format,
112 NULL, /* gdk_finalize */
113};
114
115static gvdevice_features_t device_features_gdk = {
117 | GVDEVICE_DOES_TRUECOLOR,/* flags */
118 {0.,0.}, /* default margin - points */
119 {0.,0.}, /* default page width, height - points */
120 {96.,96.}, /* dpi */
121};
122#endif
123
125#ifdef HAVE_PANGOCAIRO
126 {FORMAT_BMP, "bmp:cairo", 6, &gdk_engine, &device_features_gdk},
127 {FORMAT_ICO, "ico:cairo", 6, &gdk_engine, &device_features_gdk},
128 {FORMAT_JPEG, "jpe:cairo", 6, &gdk_engine, &device_features_gdk},
129 {FORMAT_JPEG, "jpeg:cairo", 6, &gdk_engine, &device_features_gdk},
130 {FORMAT_JPEG, "jpg:cairo", 6, &gdk_engine, &device_features_gdk},
131 {FORMAT_PNG, "png:cairo", 6, &gdk_engine, &device_features_gdk},
132 {FORMAT_TIFF, "tif:cairo", 6, &gdk_engine, &device_features_gdk},
133 {FORMAT_TIFF, "tiff:cairo", 6, &gdk_engine, &device_features_gdk},
134#endif
135 {0, NULL, 0, NULL, NULL}
136};
node NULL
Definition grammar.y:163
#define GVDEVICE_DOES_TRUECOLOR
Definition gvcjob.h:90
#define GVDEVICE_BINARY_FORMAT
Definition gvcjob.h:91
size_t gvwrite(GVJ_t *job, const char *s, size_t len)
Definition gvdevice.c:180
gvplugin_installed_t gvdevice_gdk_types[]
format_type
@ FORMAT_TIFF
@ FORMAT_BMP
@ FORMAT_ICO
@ FORMAT_JPEG
Definition gvrender_gd.c:37
@ FORMAT_PNG
Definition gvrender_gd.c:38
static cairo_status_t writer(void *closure, const unsigned char *data, unsigned int length)
table Syntax error
Definition htmlparse.y:294
gvplugin_active_device_t device
Definition gvcjob.h:286
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
ingroup plugin_api
Definition gvplugin.h:35
#define UNREACHABLE()
Definition unreachable.h:30