summaryrefslogtreecommitdiff
path: root/src/ml_cairo_lablgtk.c
blob: b4aebbf9a49366128f404c0c6d134c7fdccc0cd5 (plain)
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
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdkx.h>

#include <cairo.h>

#include <caml/mlvalues.h>
#include <caml/alloc.h>

#include "wrappers.h"
#include "ml_gobject.h"
#include "ml_gdkpixbuf.h"
#include "ml_gdk.h"

#include "ml_cairo.h"
#include "ml_cairo_status.h"

CAMLprim value
cairo_lablgtk_of_pixbuf(value pb)
{
  value v;
  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);

  if ((nchan == 4) && (bps == 8) && alpha)
    format = CAIRO_FORMAT_ARGB32;
  else 
    failwith("bad GdkPixbuf format");

  v = alloc_small(5, 0);
  Field(v, 0) = Val_bp(gdk_pixbuf_get_pixels(pixbuf));
  Field(v, 1) = Val_cairo_format_t(format);
  Field(v, 2) = Val_int(gdk_pixbuf_get_width(pixbuf));
  Field(v, 3) = Val_int(gdk_pixbuf_get_height(pixbuf));
  Field(v, 4) = Val_int(gdk_pixbuf_get_rowstride(pixbuf));

  return v;
}

CAMLprim value
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;
}


#ifdef CAIRO_HAS_XLIB_SURFACE
CAMLprim value
cairo_lablgtk_surface_create_for_drawable(value d, value fmt)
{
  GdkDrawable *draw = GdkDrawable_val(d);
  cairo_surface_t *s;
  
  s = cairo_xlib_surface_create (
				 GDK_DRAWABLE_XDISPLAY(draw),
				 GDK_DRAWABLE_XID(draw),
				 GDK_VISUAL_XVISUAL(gdk_drawable_get_visual(draw)),
				 cairo_format_t_val(fmt),
				 GDK_COLORMAP_XCOLORMAP(gdk_drawable_get_colormap(draw)));
  return Val_cairo_surface_t(s);
}

CAMLprim value
cairo_lablgtk_set_target_drawable(value cr, value d)
{
  cairo_t *c = cairo_t_val(cr);
  GdkDrawable *draw = GdkDrawable_val(d);
  GdkDrawable *real_drawable;
  gint x_offset, y_offset;

  if (GDK_IS_WINDOW(draw))
    gdk_window_get_internal_paint_info(GDK_WINDOW(draw),
				       &real_drawable,
				       &x_offset, &y_offset);
  else {
    real_drawable = draw;
    x_offset = 0;
    y_offset = 0;
  }
    
  cairo_set_target_drawable(c,
			    GDK_DRAWABLE_XDISPLAY(real_drawable),
			    GDK_DRAWABLE_XID(real_drawable));
  check_cairo_status(cr);
  cairo_translate(c, -x_offset, -y_offset);
  check_cairo_status(cr);

  return Val_unit;
}

#else

CAMLprim value
cairo_lablgtk_surface_create_for_drawable(value d, value fmt)
{ failwith("Cairo library does not support this backend") ; return Val_unit; }
 
CAMLprim value
cairo_lablgtk_set_target_drawable(value cr, value d)
{ failwith("Cairo library does not support this backend") ; return Val_unit; }

#endif /* CAIRO_HAS_XLIB_SURFACE */