#import  "MonotoneView.h"
#import  <dpsclient/wraps.h>
#import  <appkit/NXImage.h>
#import  "../common.h"
#import  "imageOperation.h"

@implementation MonotoneView

/* Overload */
- initFrame:(const NXRect *)frameRect
{
	[super initFrame:frameRect];
	cache = [[NXImage alloc] initSize: &frameRect->size];
	tone = NULL;
	[self drawCache];
	return self;
}

/* Overload */
- free
{
	[super free];
	if (cache) [cache free];
	return nil;
}

/* Overload */
- drawSelf:(NXRect *)r :(int) count
{
	[cache composite:NX_COPY fromRect:r toPoint:&(r->origin)];
	return self;
}

- drawCache
{
	int	i;
	float	half, wid, thick, y;

	[cache lockFocus];
	half = (wid = frame.size.width) / 2.0;
	thick = frame.size.height / 256.0;
	PSsetlinewidth(thick);
	if (tone)
		for (i = 0; i < 256; i++) {
			y = thick * i;
			PSsetgray(i/255.0);
			PSmoveto(0.0, y);
			PSlineto(half, y);
			PSstroke();
			PSsetgray(tone[i]/255.0);
			PSmoveto(half, y);
			PSlineto(wid, y);
			PSstroke();
		}
	else
		for (i = 0; i < 256; i++) {
			y = thick * i;
			PSsetgray(i/255.0);
			PSmoveto(0.0, y);
			PSlineto(wid, y);
			PSstroke();
		}
	[cache unlockFocus];
	return self;
}

- setTone:(unsigned char *)buffer
{
	tone = buffer;
	[self drawCache];
	return self;
}

@end
