1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/**************************************************************************/
/* cairo-ocaml -- Objective Caml bindings for Cairo */
/* Copyright © 2004-2005 Olivier Andrieu */
/* */
/* This code is free software and is licensed under the terms of the */
/* GNU Lesser General Public License version 2.1 (the "LGPL"). */
/**************************************************************************/
#include "ml_cairo.h"
#if CAIRO_HAS_XLIB_SURFACE
# include <cairo-xlib.h>
#endif
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include "wrappers.h"
#include "ml_gobject.h"
#include "ml_gdkpixbuf.h"
#include "ml_gdk.h"
CAMLprim value
ml_cairo_lablgtk_of_pixbuf (value pb)
{
static const cairo_user_data_key_t pixbuf_key;
GdkPixbuf *pixbuf = GdkPixbuf_val(pb);
cairo_format_t format;
gboolean alpha = gdk_pixbuf_get_has_alpha(pixbuf);
int nchan = gdk_pixbuf_get_n_channels(pixbuf);
int bps = gdk_pixbuf_get_bits_per_sample(pixbuf);
cairo_surface_t *surf;
if ((nchan == 4) && (bps == 8) && alpha)
format = CAIRO_FORMAT_ARGB32;
else
caml_invalid_argument ("bad GdkPixbuf format");
surf = cairo_image_surface_create_for_data (gdk_pixbuf_get_pixels (pixbuf),
format,
gdk_pixbuf_get_width(pixbuf),
gdk_pixbuf_get_height(pixbuf),
gdk_pixbuf_get_rowstride(pixbuf));
ml_cairo_surface_set_user_data (surf, &pixbuf_key, ml_cairo_make_root (pb));
return Val_cairo_surface_t (surf);
}
CAMLprim value
ml_cairo_lablgtk_shuffle_pixels (value pb)
{
GdkPixbuf *pixbuf = GdkPixbuf_val(pb);
guint w, h, s, i, j;
guchar *pixels, *p;
g_return_val_if_fail (gdk_pixbuf_get_has_alpha(pixbuf) &&
(gdk_pixbuf_get_n_channels(pixbuf) == 4) &&
(gdk_pixbuf_get_bits_per_sample(pixbuf) == 8), Val_unit);
w = gdk_pixbuf_get_width (pixbuf);
h = gdk_pixbuf_get_height (pixbuf);
s = gdk_pixbuf_get_rowstride (pixbuf);
pixels = gdk_pixbuf_get_pixels (pixbuf);
for (i=0; i<h; i++) {
p = pixels;
for (j=0; j<w; j++) {
guchar red = p[0];
p[0] = p[2];
p[2] = red;
p += 4;
}
pixels += s;
}
return Val_unit;
}
#if CAIRO_HAS_XLIB_SURFACE
CAMLprim value
ml_cairo_xlib_surface_create (value d)
{
static const cairo_user_data_key_t drawable_key;
cairo_surface_t *surface;
gint width, height;
GdkDrawable *drawable = GdkDrawable_val(d);
GdkVisual *visual = gdk_drawable_get_visual (drawable);
gdk_drawable_get_size (drawable, &width, &height);
if (visual)
surface = cairo_xlib_surface_create (GDK_DRAWABLE_XDISPLAY (drawable),
GDK_DRAWABLE_XID (drawable),
GDK_VISUAL_XVISUAL (visual),
width, height);
else if (gdk_drawable_get_depth (drawable) == 1)
surface =
cairo_xlib_surface_create_for_bitmap (GDK_PIXMAP_XDISPLAY (drawable),
GDK_PIXMAP_XID (drawable),
width, height);
else {
g_warning ("Using Cairo rendering requires the drawable argument to\n"
"have a specified colormap. All windows have a colormap,\n"
"however, pixmaps only have colormap by default if they\n"
"were created with a non-NULL window argument. Otherwise\n"
"a colormap must be set on them with "
"gdk_drawable_set_colormap");
surface = NULL;
}
if (surface != NULL)
ml_cairo_surface_set_user_data (surface, &drawable_key, ml_cairo_make_root (d));
return Val_cairo_surface_t (surface);
}
ML_3 (cairo_xlib_surface_set_size, cairo_surface_t_val, Int_val, Int_val, Unit)
#else
Cairo_Unsupported(cairo_xlib_surface_create, "Xlib backend not supported");
Cairo_Unsupported(cairo_xlib_surface_set_size, "Xlib backend not supported");
#endif /* CAIRO_HAS_XLIB_SURFACE */
|