1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-05-08 10:04:35 -07:00
|
|
|
#include "libgraph.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include <memory.h>
|
|
|
|
|
2004-03-24 07:02:06 -08:00
|
|
|
value caml_gr_make_image(value m)
|
1995-05-08 10:04:35 -07:00
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
value im;
|
|
|
|
Bool has_transp;
|
|
|
|
XImage * idata, * imask;
|
|
|
|
char * bdata, * bmask;
|
|
|
|
int i, j, rgb;
|
|
|
|
value line;
|
|
|
|
GC gc;
|
|
|
|
|
2004-03-24 07:02:06 -08:00
|
|
|
caml_gr_check_open();
|
1995-05-08 10:04:35 -07:00
|
|
|
height = Wosize_val(m);
|
2004-03-24 07:02:06 -08:00
|
|
|
if (height == 0) return caml_gr_new_image(0, 0);
|
1995-05-08 10:04:35 -07:00
|
|
|
width = Wosize_val(Field(m, 0));
|
|
|
|
for (i = 1; i < height; i++)
|
|
|
|
if (Wosize_val(Field(m, i)) != width)
|
2004-03-24 07:02:06 -08:00
|
|
|
caml_gr_fail("make_image: lines of different lengths", NULL);
|
1995-05-08 10:04:35 -07:00
|
|
|
|
|
|
|
/* Build an XImage for the data part of the image */
|
|
|
|
idata =
|
2013-09-04 08:12:37 -07:00
|
|
|
XCreateImage(caml_gr_display,
|
|
|
|
DefaultVisual(caml_gr_display, caml_gr_screen),
|
2004-03-24 07:02:06 -08:00
|
|
|
XDefaultDepth(caml_gr_display, caml_gr_screen),
|
1995-05-08 10:04:35 -07:00
|
|
|
ZPixmap, 0, NULL, width, height,
|
2004-03-24 07:02:06 -08:00
|
|
|
BitmapPad(caml_gr_display), 0);
|
1999-02-19 09:08:27 -08:00
|
|
|
|
2013-02-24 19:01:31 -08:00
|
|
|
bdata = (char *) caml_stat_alloc(height * idata->bytes_per_line);
|
1995-05-08 10:04:35 -07:00
|
|
|
idata->data = bdata;
|
|
|
|
has_transp = False;
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
line = Field(m, i);
|
|
|
|
for (j = 0; j < width; j++) {
|
|
|
|
rgb = Int_val(Field(line, j));
|
|
|
|
if (rgb == Transparent) { has_transp = True; rgb = 0; }
|
2004-03-24 07:02:06 -08:00
|
|
|
XPutPixel(idata, j, i, caml_gr_pixel_rgb(rgb));
|
1995-05-08 10:04:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the matrix contains transparent points,
|
|
|
|
build an XImage for the mask part of the image */
|
|
|
|
if (has_transp) {
|
|
|
|
imask =
|
2013-09-04 08:12:37 -07:00
|
|
|
XCreateImage(caml_gr_display,
|
|
|
|
DefaultVisual(caml_gr_display, caml_gr_screen),
|
1995-05-08 10:04:35 -07:00
|
|
|
1, ZPixmap, 0, NULL, width, height,
|
2004-03-24 07:02:06 -08:00
|
|
|
BitmapPad(caml_gr_display), 0);
|
2013-02-24 19:01:31 -08:00
|
|
|
bmask = (char *) caml_stat_alloc(height * imask->bytes_per_line);
|
1995-05-08 10:04:35 -07:00
|
|
|
imask->data = bmask;
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
line = Field(m, i);
|
|
|
|
for (j = 0; j < width; j++) {
|
|
|
|
rgb = Int_val(Field(line, j));
|
|
|
|
XPutPixel(imask, j, i, rgb != Transparent);
|
|
|
|
}
|
|
|
|
}
|
1996-09-03 01:25:04 -07:00
|
|
|
} else {
|
|
|
|
imask = NULL;
|
1995-05-08 10:04:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate the image and store the XImages into the Pixmaps */
|
2004-03-24 07:02:06 -08:00
|
|
|
im = caml_gr_new_image(width, height);
|
|
|
|
gc = XCreateGC(caml_gr_display, Data_im(im), 0, NULL);
|
|
|
|
XPutImage(caml_gr_display, Data_im(im), gc, idata, 0, 0, 0, 0, width, height);
|
1995-05-08 10:04:35 -07:00
|
|
|
XDestroyImage(idata);
|
2004-03-24 07:02:06 -08:00
|
|
|
XFreeGC(caml_gr_display, gc);
|
1995-05-08 10:04:35 -07:00
|
|
|
if (has_transp) {
|
2013-09-04 08:12:37 -07:00
|
|
|
Mask_im(im) = XCreatePixmap(caml_gr_display, caml_gr_window.win, width,
|
|
|
|
height, 1);
|
2004-03-24 07:02:06 -08:00
|
|
|
gc = XCreateGC(caml_gr_display, Mask_im(im), 0, NULL);
|
2013-09-04 08:12:37 -07:00
|
|
|
XPutImage(caml_gr_display, Mask_im(im), gc, imask, 0, 0, 0, 0, width,
|
|
|
|
height);
|
1995-05-08 10:04:35 -07:00
|
|
|
XDestroyImage(imask);
|
2004-03-24 07:02:06 -08:00
|
|
|
XFreeGC(caml_gr_display, gc);
|
1995-05-08 10:04:35 -07:00
|
|
|
}
|
2004-03-24 07:02:06 -08:00
|
|
|
XFlush(caml_gr_display);
|
1995-05-08 10:04:35 -07:00
|
|
|
return im;
|
|
|
|
}
|