Graphviz  2.41.20171026.1811
gvdevice_gdk.c
Go to the documentation of this file.
1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #include "config.h"
15 
16 #include "gvplugin_device.h"
17 #include "gvio.h"
18 #ifdef HAVE_PANGOCAIRO
19 #include <gdk-pixbuf/gdk-pixbuf.h>
20 
21 typedef enum {
22  FORMAT_BMP,
23  FORMAT_ICO,
25  FORMAT_PNG,
26  FORMAT_TIFF,
27  } format_type;
28 
29 /*
30  * Does an in-place conversion of a CAIRO ARGB32 image to GDK RGBA
31  */
32 static void
33 argb2rgba ( unsigned int width, unsigned int height, char *data)
34 {
35 /* define indexes to color bytes in each format */
36 #define Ba 0
37 #define Ga 1
38 #define Ra 2
39 #define Aa 3
40 
41 #define Rb 0
42 #define Gb 1
43 #define Bb 2
44 #define Ab 3
45 
46  unsigned int x, y;
47 
48  for (y = 0; y < height; y++) {
49  for (x = 0; x < width; x++) {
50  /* swap red and blue */
51  unsigned char r = data[Ra];
52  data[Bb] = data[Ba];
53  data[Rb] = r;
54  data += 4;
55  }
56  }
57 }
58 
59 static gboolean
60 writer ( const gchar *buf, gsize count, GError **error, gpointer data)
61 {
62  if (count == gvwrite((GVJ_t *)data, buf, count))
63  return TRUE;
64  return FALSE;
65 }
66 
67 static void gdk_format(GVJ_t * job)
68 {
69  char *format_str = "";
70  GdkPixbuf *pixbuf;
71 
72  switch (job->device.id) {
73  case FORMAT_BMP:
74  format_str = "bmp";
75  break;
76  case FORMAT_ICO:
77  format_str = "ico";
78  break;
79  case FORMAT_JPEG:
80  format_str = "jpeg";
81  break;
82  case FORMAT_PNG:
83  format_str = "png";
84  break;
85  case FORMAT_TIFF:
86  format_str = "tiff";
87  break;
88  }
89 
90  argb2rgba(job->width, job->height, job->imagedata);
91 
92  pixbuf = gdk_pixbuf_new_from_data(
93  (unsigned char*)(job->imagedata), // data
94  GDK_COLORSPACE_RGB, // colorspace
95  TRUE, // has_alpha
96  8, // bits_per_sample
97  job->width, // width
98  job->height, // height
99  4 * 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 #if HAVE_G_OBJECT_UNREF
107  g_object_unref(pixbuf);
108 #else
109  gdk_pixbuf_unref(pixbuf);
110 #endif
111 }
112 
113 static gvdevice_engine_t gdk_engine = {
114  NULL, /* gdk_initialize */
115  gdk_format,
116  NULL, /* gdk_finalize */
117 };
118 
119 static gvdevice_features_t device_features_gdk = {
121  | GVDEVICE_DOES_TRUECOLOR,/* flags */
122  {0.,0.}, /* default margin - points */
123  {0.,0.}, /* default page width, height - points */
124  {96.,96.}, /* dpi */
125 };
126 #endif
127 
129 #ifdef HAVE_PANGOCAIRO
130  {FORMAT_BMP, "bmp:cairo", 6, &gdk_engine, &device_features_gdk},
131  {FORMAT_ICO, "ico:cairo", 6, &gdk_engine, &device_features_gdk},
132  {FORMAT_JPEG, "jpe:cairo", 6, &gdk_engine, &device_features_gdk},
133  {FORMAT_JPEG, "jpeg:cairo", 6, &gdk_engine, &device_features_gdk},
134  {FORMAT_JPEG, "jpg:cairo", 6, &gdk_engine, &device_features_gdk},
135  {FORMAT_PNG, "png:cairo", 6, &gdk_engine, &device_features_gdk},
136  {FORMAT_TIFF, "tif:cairo", 6, &gdk_engine, &device_features_gdk},
137  {FORMAT_TIFF, "tiff:cairo", 6, &gdk_engine, &device_features_gdk},
138 #endif
139  {0, NULL, 0, NULL, NULL}
140 };
char * imagedata
Definition: gvcjob.h:306
#define GVDEVICE_BINARY_FORMAT
Definition: gvcjob.h:93
gvplugin_installed_t gvdevice_gdk_types[]
Definition: gvdevice_gdk.c:128
unsigned int width
Definition: gvcjob.h:336
Definition: gvcjob.h:271
gvplugin_active_device_t device
Definition: gvcjob.h:295
format_type
#define NULL
Definition: logic.h:39
unsigned int height
Definition: gvcjob.h:337
#define FALSE
Definition: cgraph.h:35
Definition: legal.c:60
size_t gvwrite(GVJ_t *job, const char *s, size_t len)
Definition: gvdevice.c:190
#define GVDEVICE_DOES_TRUECOLOR
Definition: gvcjob.h:92
#define TRUE
Definition: cgraph.h:38