]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/contrib/otherlibs/graph/image.c
update
[l4.git] / l4 / pkg / ocaml / contrib / otherlibs / graph / image.c
1 /***********************************************************************/
2 /*                                                                     */
3 /*                           Objective Caml                            */
4 /*                                                                     */
5 /*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         */
6 /*                                                                     */
7 /*  Copyright 1996 Institut National de Recherche en Informatique et   */
8 /*  en Automatique.  All rights reserved.  This file is distributed    */
9 /*  under the terms of the GNU Library General Public License, with    */
10 /*  the special exception on linking described in file ../../LICENSE.  */
11 /*                                                                     */
12 /***********************************************************************/
13
14 /* $Id: image.c 6171 2004-03-24 15:02:06Z starynke $ */
15
16 #include "libgraph.h"
17 #include "image.h"
18 #include <alloc.h>
19 #include <custom.h>
20
21 static void caml_gr_free_image(value im)
22 {
23   XFreePixmap(caml_gr_display, Data_im(im));
24   if (Mask_im(im) != None) XFreePixmap(caml_gr_display, Mask_im(im));
25 }
26
27 static struct custom_operations image_ops = {
28   "_image",
29   caml_gr_free_image,
30   custom_compare_default,
31   custom_hash_default,
32   custom_serialize_default,
33   custom_deserialize_default
34 };
35
36 #define Max_image_mem 2000000
37
38 value caml_gr_new_image(int w, int h)
39 {
40   value res = alloc_custom(&image_ops, sizeof(struct grimage),
41                            w * h, Max_image_mem);
42   Width_im(res) = w;
43   Height_im(res) = h;
44   Data_im(res) = XCreatePixmap(caml_gr_display, caml_gr_window.win, w, h,
45                                XDefaultDepth(caml_gr_display, caml_gr_screen));
46   Mask_im(res) = None;
47   return res;
48 }
49
50 value caml_gr_create_image(value vw, value vh)
51 {
52   caml_gr_check_open();
53   return caml_gr_new_image(Int_val(vw), Int_val(vh));
54 }
55
56 value caml_gr_blit_image(value im, value vx, value vy)
57 {
58   int x = Int_val(vx);
59   int y = Int_val(vy);
60   caml_gr_check_open();
61   XCopyArea(caml_gr_display, caml_gr_bstore.win, Data_im(im), caml_gr_bstore.gc,
62             x, Bcvt(y) + 1 - Height_im(im),
63             Width_im(im), Height_im(im),
64             0, 0);
65   return Val_unit;
66 }
67
68 value caml_gr_draw_image(value im, value vx, value vy)
69 {
70   int x = Int_val(vx);
71   int y = Int_val(vy);
72   int wy = Wcvt(y) + 1 - Height_im(im);
73   int by = Bcvt(y) + 1 - Height_im(im);
74
75   caml_gr_check_open();
76   if (Mask_im(im) != None) {
77     if(caml_gr_remember_modeflag) {
78       XSetClipOrigin(caml_gr_display, caml_gr_bstore.gc, x, by);
79       XSetClipMask(caml_gr_display, caml_gr_bstore.gc, Mask_im(im));
80     }
81     if(caml_gr_display_modeflag) {
82       XSetClipOrigin(caml_gr_display, caml_gr_window.gc, x, wy);
83       XSetClipMask(caml_gr_display, caml_gr_window.gc, Mask_im(im));
84     }
85   }
86   if(caml_gr_remember_modeflag)
87     XCopyArea(caml_gr_display, Data_im(im), caml_gr_bstore.win, caml_gr_bstore.gc,
88               0, 0,
89               Width_im(im), Height_im(im),
90               x, by);
91   if(caml_gr_display_modeflag)
92     XCopyArea(caml_gr_display, Data_im(im), caml_gr_window.win, caml_gr_window.gc,
93           0, 0,
94           Width_im(im), Height_im(im),
95           x, wy);
96   if (Mask_im(im) != None) {
97     if(caml_gr_remember_modeflag)
98       XSetClipMask(caml_gr_display, caml_gr_bstore.gc, None);
99     if(caml_gr_display_modeflag)
100       XSetClipMask(caml_gr_display, caml_gr_window.gc, None);
101   }
102   if(caml_gr_display_modeflag)
103     XFlush(caml_gr_display);
104   return Val_unit;
105 }
106
107 /* eof $Id: image.c 6171 2004-03-24 15:02:06Z starynke $ */