Graphviz  2.41.20171026.1811
gvloadimage_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 <stdlib.h>
17 
18 #include "gvplugin_loadimage.h"
19 #include "gvio.h"
20 
21 #ifdef HAVE_PANGOCAIRO
22 #include <cairo.h>
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <gdk/gdkcairo.h>
25 
26 #ifdef _WIN32 //*dependencies
27  #pragma comment( lib, "gvc.lib" )
28  #pragma comment( lib, "glib-2.0.lib" )
29  #pragma comment( lib, "cairo.lib" )
30  #pragma comment( lib, "gobject-2.0.lib" )
31  #pragma comment( lib, "graph.lib" )
32  #pragma comment( lib, "gdk-pixbuf.lib" )
33 #endif
34 
35 typedef enum {
36  FORMAT_BMP_CAIRO,
37  FORMAT_JPEG_CAIRO,
39  FORMAT_ICO_CAIRO,
40  FORMAT_TIFF_CAIRO,
41 } format_type;
42 
43 #if 0
44 // FIXME - should be using a stream reader
45 static cairo_status_t
46 reader (void *closure, unsigned char *data, unsigned int length)
47 {
48  if (length == fread(data, 1, length, (FILE *)closure)
49  || feof((FILE *)closure))
50  return CAIRO_STATUS_SUCCESS;
51  return CAIRO_STATUS_READ_ERROR;
52 }
53 #endif
54 
55 static void gdk_freeimage(usershape_t *us)
56 {
57  g_object_unref((GdkPixbuf*)(us->data));
58 }
59 
60 static GdkPixbuf* gdk_loadimage(GVJ_t * job, usershape_t *us)
61 {
62  GdkPixbuf *image = NULL;
63 
64  assert(job);
65  assert(us);
66  assert(us->name);
67 
68  if (us->data) {
69  if (us->datafree == gdk_freeimage)
70  image = (GdkPixbuf*)(us->data); /* use cached data */
71  else {
72  us->datafree(us); /* free incompatible cache data */
73  us->datafree = NULL;
74  us->data = NULL;
75  }
76  }
77  if (!image) { /* read file into cache */
78  if (!gvusershape_file_access(us))
79  return NULL;
80  switch (us->type) {
81  case FT_PNG:
82  case FT_JPEG:
83  case FT_BMP:
84  case FT_ICO:
85  case FT_TIFF:
86  // FIXME - should be using a stream reader
87  image = gdk_pixbuf_new_from_file(us->name, NULL);
88  break;
89  default:
90  image = NULL;
91  }
92  if (image) {
93  us->data = (void*)image;
94  us->datafree = gdk_freeimage;
95  }
97  }
98  return image;
99 }
100 
101 static void gdk_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
102 {
103  cairo_t *cr = (cairo_t *) job->context; /* target context */
104  GdkPixbuf *image;
105 
106  image = gdk_loadimage(job, us);
107  if (image) {
108  cairo_save(cr);
109  cairo_translate(cr, b.LL.x, -b.UR.y);
110  cairo_scale(cr, (b.UR.x - b.LL.x)/(us->w), (b.UR.y - b.LL.y)/(us->h));
111  gdk_cairo_set_source_pixbuf (cr, image, 0, 0);
112  cairo_paint (cr);
113  cairo_restore(cr);
114  }
115 }
116 
117 static gvloadimage_engine_t engine_gdk = {
118  gdk_loadimage_cairo
119 };
120 
121 #endif
122 
124 #ifdef HAVE_PANGOCAIRO
125  {FORMAT_BMP_CAIRO, "bmp:cairo", 1, &engine_gdk, NULL},
126  {FORMAT_JPEG_CAIRO, "jpe:cairo", 2, &engine_gdk, NULL},
127  {FORMAT_JPEG_CAIRO, "jpg:cairo", 2, &engine_gdk, NULL},
128  {FORMAT_JPEG_CAIRO, "jpeg:cairo", 2, &engine_gdk, NULL},
129  {FORMAT_PNG_CAIRO, "png:cairo", -1, &engine_gdk, NULL},
130  {FORMAT_ICO_CAIRO, "ico:cairo", 1, &engine_gdk, NULL},
131 // {FORMAT_TIFF_CAIRO, "tif:cairo", 1, &engine_gdk, NULL},
132 // {FORMAT_TIFF_CAIRO, "tiff`:cairo", 1, &engine_gdk, NULL},
133 #endif
134  {0, NULL, 0, NULL, NULL}
135 };
void(* datafree)(usershape_t *us)
Definition: usershape.h:63
void * context
Definition: gvcjob.h:304
#define assert(x)
Definition: cghdr.h:47
void * data
Definition: usershape.h:61
Definition: gvcjob.h:271
gvplugin_installed_t gvloadimage_gdk_types[]
boolean gvusershape_file_access(usershape_t *us)
Definition: gvusershape.c:584
double y
Definition: geom.h:28
imagetype_t type
Definition: usershape.h:58
void gvusershape_file_release(usershape_t *us)
Definition: gvusershape.c:618
format_type
#define NULL
Definition: logic.h:39
double x
Definition: geom.h:28
pointf LL
Definition: geom.h:35
const char * name
Definition: usershape.h:53
pointf UR
Definition: geom.h:35
Definition: geom.h:35
Definition: legal.c:60