#define MODULE_PEN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <egb.h>
#include "art.h"
#include "guisub.h"
#include "wgbmac.h"
#include "pen.h"
#include "pensel.h"
#include "desktop.h"

/*
ペン(Ａ段階)

テスト用
*/

Pen pen_new(int wid,int ht)
{
	Pen p;
	p = TL_calloc(1,sizeof(PEN_DATA));
	if (p == NULL)
		return NULL;
	p->buf = NULL;
	p->wid = wid;
	p->ht = ht;
	p->ofsx = wid / 2;
	p->ofsy = ht / 2;
	if (wid * ht > 0)
	{
		if ((p->buf = TL_calloc(wid*ht,1)) == NULL)
			{ TL_free(p);  return NULL; }
	}
	else
		p->buf = NULL;
	return p;
}

Pen pen_createCopy(Pen pen)
{
	Pen p;
	p = TL_calloc(1,sizeof(PEN_DATA));
	if (p == NULL)
		return NULL;
	p->wid = pen->wid;
	p->ht = pen->ht;
	p->ofsx = pen->ofsx;
	p->ofsy = pen->ofsy;
	if ((p->buf = TL_calloc(1,p->wid * p->ht)) == NULL)
		{ TL_free(p);  return NULL; }
	memcpy(p->buf, pen->buf, p->wid * p->ht);
	return p;
}

void	pen_destroy(Pen pen)
{
	if (pen->buf != NULL)
		TL_free(pen->buf);
	TL_free(pen);
}

void	pen_getPattern(Pen pen, char **buf, int *wid, int *ht)
{
	if (buf!=NULL)
		*buf = pen->buf;
	if (wid!=NULL)
		*wid = pen->wid;
	if (ht!=NULL)
		*ht = pen->ht;
}

int		pen_setPattern(Pen pen, char *buf, int wid, int ht)
{
	if (buf == pen->buf)
		return 0;	/* この場合、サイズの変更はしないものとする */
	if (wid != pen->wid || ht != pen->ht)
	{
		if (pen->buf!=NULL)
			TL_free(pen->buf);
		if ((pen->buf = TL_calloc(wid*ht, 1)) == NULL)
			return -1;
	}
	memcpy(pen->buf, buf, wid*ht);
	return 0;
}

void	pen_setSampleData(Pen pen, int n)
{
	

}

void pen_setPixel(Pen pen,int x,int y,int gray)
// gray:0..255
{
	*(pen->buf + pen->wid * y + x) = gray;
}

int pen_getPixel(Pen pen,int x,int y)
// ret:0..255
{
	return *(pen->buf + pen->wid * y + x);
}

BOOL pen_IsNull(Pen pen)
{
	int i;
	char *p = pen->buf;
	for (i=pen->wid*pen->ht; i>0; i--,p++)
	{
		if (*p != 0)
			return FALSE;
	}
	return TRUE;
}
