/* xyz */

/*
 * Copyright (c) 1995 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/*
 * This file provide a bunch of interface routines for accessing objects
 * of type _XImage from Xlib from STIPPLE code.
 */

#ifndef XLIB_H
#include "xlib.h"
#endif

/* The initial object: */
Xlib_image_struct xlib_image___initial_object;	/* Initialize to zero */
Xlib_image xlib_image___initial = &xlib_image___initial_object;


/*
 * xlib_image__clear(image)
 *	This routine will set all of the pixels in {image} to 0.
 */
void
xlib_image__clear(
    Xlib_image image)
{
    char *data;

    data = image->data;
    assert(data != (char *)0);
    (void)memset(data, 0, image->bytes_per_line * image->height);
}

/*
 * xlib_image__create(width, height, depth, display, visual)
 *	This routine will create and return an {Xlib_image}
 *	that is associated with {display} and {visual} and is
 *	{width} pixels wide, {height} pixels high, and {debth} bits
 *	per pixel.
 */
Xlib_image
xlib_image__create(
    unsigned width,
    unsigned height,
    unsigned depth,
    Xlib_display display,
    Xlib_visual visual)
{
    Xlib_image image;

    image = xlib_image___initial;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	image = XCreateImage(display->display, visual->visual.visual, depth,
	  ZPixmap, 0, 0, width, height, 32, 0);
	image->data = (char *)malloc(image->bytes_per_line * height);
    }
    return image;
}

/*
 * xlib_image__depth_jget(image)
 *	This routine will return the depth of {image} in pixels.
 */
unsigned
xlib_image__depth_get(
    Xlib_image image)
{
    return image->depth;
}

/*
 * xlib_image__external__initialize()
 *	This routine will "initialize" the {xlib_image} external code.
 */
void
xlib_image__external__initialize(void)
{
    assert(xlib_image___initial == &xlib_image___initial_object);
}

/*
 * xlib_image__fetch2(xlib_image, x, y)
 *	This procedure will return the pixel at ({x}, {y}) in {image}.
 */
unsigned
xlib_image__fetch2(
    Xlib_image image,
    unsigned x,
    unsigned y)
{
    unsigned pixel;

    pixel = XGetPixel(image, x, y);
    return pixel;
}

/*
 * xlib_image__fill(image, pixel)
 *	This procedure will fill {image} with {pixel}.
 */
void
xlib_image__fill(
    Xlib_image image,
    unsigned pixel)
{
    char *data;

    data = image->data;
    assert(data != (char *)0);
    (void)memset(data, pixel, image->bytes_per_line * image->height);
}


/*
 * xlib_image__height_get(image)
 *	This routine will return the height of {image} is pixels.
 */
unsigned
xlib_image__height_get(
    Xlib_image image)
{
    return image->height;
}

/*
 * xlib_image__put_helper(source_image, source_x, source_y,
 *   destination_drawable_number, destination_x, destination_y,
 *   width, height, gc)
 *	This procedure will put the rectangular region that is {width} by
 *	{height} pixels from {source_image} at ({source_x}, {source_y}) into
 *	{destination_drawable_number} at ({destinatino_x}, {destination_y})
 *	using {gc} for the graphics context.  This routine should only be
 *	called from {put@xlib_image}().
 */
void
xlib_image__put_helper(
    Xlib_image source_image,
    unsigned source_x,
    unsigned source_y,
    unsigned destination_drawable_number,
    unsigned destination_x,
    unsigned destination_y,
    unsigned width,
    unsigned height,
    Xlib_gc gc)
{
    Xlib_display display;

    display = gc->screen->display;
    if (display->closed) {
	xlib_display___closed(display);
    } else {
	XPutImage(display->display, destination_drawable_number, gc->gc,
	  source_image, source_x, source_y, destination_x, destination_y,
	  width, height);
    }
}

/*
 * xlib_image__store2(xlib_image, x, y, pixel)
 *	This procedure will return the pixel at ({x}, {y}) in {image}.
 */
unsigned
xlib_image__store2(
    Xlib_image image,
    unsigned x,
    unsigned y,
    unsigned pixel)
{
    XPutPixel(image, x, y, pixel);
}

/*
 * xlib_image__width_get(image)
 *	This routine will return the width of {image} is pixels.
 */
unsigned
xlib_image__width_get(
    Xlib_image image)
{
    return image->width;
}


