/* raster.c - main and other misc stuff 
 * 
 *  Raster graphics library
 * 
 *  Copyright (c) 1997 Alfredo K. Kojima
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *  
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *  
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
#include "wraster.h"

#define MAXERRLEN	512


char *WRasterLibVersion="0.1";

char RErrorString[MAXERRLEN];



RImage*
RCreateImage(int width, int height, int alpha)
{
    RImage *image=NULL;
    int i;
    
    image = malloc(sizeof(RImage));
    if (!image)
      goto error;
    memset(image, 0, sizeof(RImage));
    image->width = width;
    image->height = height;
    for (i=0; i<3+(alpha?1:0); i++) {
	image->data[i] = malloc(width*height);
	if (!image->data[i])
	  goto error;
    }
    return image;

    error:
    for (i=0; i<4; i++) {
	if (image->data[i])
	  free(image->data[i]);
    }
    if (image)
      free(image);
    sprintf(RErrorString, "out of memory");
    return NULL;
}



RImage*
RCloneImage(RImage *image)
{
    RImage *new_image;

    new_image = RCreateImage(image->width, image->height, image->data[3]!=NULL);
    if (!new_image)
      return NULL;
    
    new_image->background = image->background;
    memcpy(new_image->data[0], image->data[0], image->width*image->height);
    memcpy(new_image->data[1], image->data[1], image->width*image->height);
    memcpy(new_image->data[2], image->data[2], image->width*image->height);
    if (image->data[3])
      memcpy(new_image->data[3], image->data[3], image->width*image->height);

    return new_image;
}


RImage*
RGetSubImage(RImage *image, int x, int y, int width, int height)
{
    register int i, ofs;
    RImage *new_image;

#ifdef DEBUG
    if (width<1 || height <1 || x<0 || y<0) {
	printf("bad value in RGetSubImage()\n");
	return NULL;
    }
#endif
    new_image = RCreateImage(width, height, image->data[3]!=NULL);
    if (!new_image)
      return NULL;
    new_image->background = image->background;
    ofs = image->width*y+x;

    for (i=0; i<height; i++) {
	memcpy(new_image->data[0], &image->data[0][ofs], width);
	memcpy(new_image->data[1], &image->data[1][ofs], width);
	memcpy(new_image->data[2], &image->data[2][ofs], width);
	if (image->data[3])
	  memcpy(new_image->data[3], &image->data[3][ofs], width);
	ofs += image->width;
    }
    return new_image;
}


int 
RDestroyImage(RImage *image)
{
    int i;
    
    for (i=0; i<4; i++) {
	if (image->data[i])
	  free(image->data[i]);
    }
    free(image);
    return True;
}


/*
 *---------------------------------------------------------------------- 
 * RCombineImages-
 * 	Combines two equal sized images with alpha image. The second
 * image will be placed on top of the first one.
 *---------------------------------------------------------------------- 
 */
int
RCombineImages(RImage *image, RImage *src)
{
    register int i;
    unsigned char *dr, *dg, *db;
    unsigned char *sr, *sg, *sb, *sa;


    if (image->width!=src->width || image->height!=src->height) {
	printf("RCombineImages() called with images of different size\n");
	return False;
    }
    dr = image->data[0];
    dg = image->data[1];
    db = image->data[2];

    sr = src->data[0];
    sg = src->data[1];
    sb = src->data[2];
    sa = src->data[3];

    if (!sa) {
	memcpy(dr, sr, image->height*image->width);
	memcpy(dg, sg, image->height*image->width);
	memcpy(db, sb, image->height*image->width);
    } else {
	for (i=0; i<image->height*image->width; i++) {
	    *dr = (((int)*dr * (int)(255-*sa)) + ((int)*sr * (int)*sa))/256;
	    *dg = (((int)*dg * (int)(255-*sa)) + ((int)*sg * (int)*sa))/256;
	    *db = (((int)*db * (int)(255-*sa)) + ((int)*sb * (int)*sa))/256;
	    dr++;    dg++;    db++;    
	    sr++;    sg++;    sb++;
	    sa++;
	}
    }
    return True;
}


int 
RCombineArea(RImage *image, RImage *src, int sx, int sy, int width, 
	     int height, int dx, int dy)
{
    int x, y, dwi, swi;
    unsigned char *dr, *dg, *db;
    unsigned char *sr, *sg, *sb, *sa;


    if (dy < -image->height || dx < -image->width 
	|| dy > image->height || dx > image->width)
	return False;

    if (sy < -height || sx < -width || sy > height || sx > width)
	return False;

    dr = image->data[0] + dy*image->width + dx;
    dg = image->data[1] + dy*image->width + dx;
    db = image->data[2] + dy*image->width + dx;

    sr = src->data[0] + sy*src->width;
    sg = src->data[1] + sy*src->width;
    sb = src->data[2] + sy*src->width;
    sa = src->data[3] + sy*src->width;

    swi = src->width - width;
    dwi = image->width - width;

    /* clip */
    width -= sx;
    height -= sy;

    if (height > image->height - dy)
	height = image->height - dy;

    if (!src->data[3]) {
	for (y=0; y<height; y++) {
	    for (x=0; x<width; x++) {
		*(dr++) = *(sr++);
		*(dg++) = *(sg++);
		*(db++) = *(sb++);
	    }
	    dr += dwi;	dg += dwi;	db += dwi;
	    sr += swi;	sg += swi;	sb += swi;
	}
    } else {
	for (y=0; y<height; y++) {
	    for (x=0; x<width; x++) {
		*dr = (((int)*dr *(int)(255-*sa)) + ((int)*sr *(int)*sa))/256;
		*dg = (((int)*dg *(int)(255-*sa)) + ((int)*sg *(int)*sa))/256;
		*db = (((int)*db *(int)(255-*sa)) + ((int)*sb *(int)*sa))/256;
		dr++;    dg++;    db++;
		sr++;    sg++;    sb++;
		sa++;
	    }
	    dr += dwi;	dg += dwi;	db += dwi;
	    sr += swi;	sg += swi;	sb += swi;
	    sa += swi;
	}
    }
    return True;
}


