/* Copyright 2001-2003 by Norbert Freudemann, David Frese */

#include "xlib.h"

s48_ref_t scx_Create_Pixmap(s48_call_t call, s48_ref_t display, 
	    s48_ref_t drawable, s48_ref_t w, s48_ref_t h, s48_ref_t depth) {
  Pixmap pm;
  pm = XCreatePixmap(scx_extract_display(call, display), 
		     scx_extract_drawable(call, drawable), 
		     (int)s48_extract_long_2(call, w), 
		     (int)s48_extract_long_2(call, h),
		     (int)s48_extract_long_2(call, depth));
  return scx_enter_pixmap(call, pm);
}

s48_ref_t scx_Free_Pixmap(s48_call_t call, s48_ref_t display, s48_ref_t pixmap) {
  XFreePixmap(scx_extract_display(call, display), 
	      scx_extract_pixmap(call, pixmap));
  return s48_unspecific_2(call);
}

s48_ref_t scx_Read_Bitmap_File(s48_call_t call, s48_ref_t display,
	    s48_ref_t drawable, s48_ref_t filename) 
{
  unsigned width, height;
  int res, xhot, yhot;
  Pixmap bitmap;
  s48_ref_t ret = s48_false_2(call);

  res = XReadBitmapFile(scx_extract_display(call, display), 
			scx_extract_drawable(call, drawable),
			s48_extract_string(call, filename), 
			&width, &height, &bitmap,
			&xhot, &yhot);

  if (res != BitmapSuccess)
    return s48_enter_long_2(call, res);
  
  ret = s48_cons_2(call, s48_enter_long_2(call, yhot), s48_null_2(call));
  ret = s48_cons_2(call, s48_enter_long_2(call, xhot), ret);
  ret = s48_cons_2(call, s48_enter_long_2(call, height), ret);
  ret = s48_cons_2(call, s48_enter_long_2(call, width), ret);
  ret = s48_cons_2(call, scx_enter_pixmap(call, bitmap), ret);
  return ret;
}

s48_ref_t scx_Write_Bitmap_File(s48_call_t call, s48_ref_t display, 
            s48_ref_t filename, s48_ref_t bitmap, s48_ref_t w, s48_ref_t h,
	    s48_ref_t x, s48_ref_t y) 
{
  int ret;
  ret = XWriteBitmapFile(scx_extract_display(call, display), 
			 s48_extract_string(call, filename),
			 scx_extract_pixmap(call, bitmap), 
			 (int)s48_extract_long_2(call, w),
			 (int)s48_extract_long_2(call, h),
			 (int)s48_extract_long_2(call, x), 
			 (int)s48_extract_long_2(call, y));
  return s48_enter_long_2(call, ret);
}

s48_ref_t scx_Create_Bitmap_From_Data(s48_call_t call, s48_ref_t display, 
	     s48_ref_t drawable, s48_ref_t data, s48_ref_t w, s48_ref_t h) 
{
  Pixmap pm;
  pm = XCreateBitmapFromData(scx_extract_display(call, display), 
			     scx_extract_drawable(call, drawable),
			     s48_extract_string(call, data), 
			     s48_extract_long_2(call, w),
			     s48_extract_long_2(call, h));
  return scx_enter_pixmap(call, pm);
}

s48_ref_t scx_Create_Pixmap_From_Bitmap_Data(s48_call_t call, 
            s48_ref_t display, s48_ref_t drawable, s48_ref_t data,
	    s48_ref_t w,s48_ref_t h, s48_ref_t f, s48_ref_t b, s48_ref_t depth) 
{
  Pixmap pm;
  pm = XCreatePixmapFromBitmapData(scx_extract_display(call, display),
				   scx_extract_drawable(call, drawable),
				   s48_extract_string(call, data), 
				   (int)s48_extract_long_2(call, w),
				   (int)s48_extract_long_2(call, h),
				   scx_extract_pixel(call, f),
				   scx_extract_pixel(call, b),
				   (int)s48_extract_long_2(call, depth));
  return scx_enter_pixmap(call, pm);
}

void scx_init_pixmap(void) {
  S48_EXPORT_FUNCTION(scx_Free_Pixmap);
  S48_EXPORT_FUNCTION(scx_Create_Pixmap);
  S48_EXPORT_FUNCTION(scx_Read_Bitmap_File);
  S48_EXPORT_FUNCTION(scx_Write_Bitmap_File);
  S48_EXPORT_FUNCTION(scx_Create_Bitmap_From_Data);
  S48_EXPORT_FUNCTION(scx_Create_Pixmap_From_Bitmap_Data);
}

