#include <graphics/gfxbase.h>
#include <graphics/display.h>
#include <intuition/intuition.h>
#include <stdio.h>
#include <math.h>

struct	GfxBase		*GfxBase;	/* Export the library pointers */
struct	IntuitionBase	*IntuitionBase;
struct	RastPort	*rp;		/* Graphics structures           */
struct	ViewPort	*vp;
struct	Window		*w;		/* Intuition structures        */ 
struct	Screen		*screen;

struct	NewScreen	ns = {
	0, 0,				/* start position                */
	320, 400, 4,			/* width, height, depth          */
	15, 0,				/* detail pen, block pen         */
	INTERLACE,			/* Hires ViewMode                */
	CUSTOMSCREEN,			/* screen type                   */
	NULL,				/* font to use                   */
	NULL,				/* default title for screen      */
	NULL				/* pointer to additional gadgets */
};

struct NewWindow nw = {
	0, 0,				/* start position                */
	320, 400,			/* width, height                 */
	15, 0,				/* detail pen, block pen         */
	NULL,				/* IDCMP flags                   */
	BORDERLESS,			/* window flags                  */
	NULL,				/* pointer to first user gadget  */
	NULL,				/* pointer to user checkmark     */
	NULL,				/* window title                  */
	NULL,				/* pointer to screen (set below) */
	NULL,				/* pointer to superbitmap        */
	0, 0, 320, 400,			/* ignored since not sizeable    */
	CUSTOMSCREEN			/* type of screen desired        */
};


char	suzie[300][250];
FILE	*in;

main(argc, argv)
int	argc;
char	**argv;
{
	char	wait[10];

	if( !(in=fopen(argv[1], "r")) ) {
		printf("no input file\n");
		exit(-1);
	}
	if ( !(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library") ) ) {
		cleanup();
		exit(103);
	}

	if ( !( IntuitionBase
		=(struct IntuitionBase *)OpenLibrary("intuition.library") )
	 ) {
		cleanup();
		exit(103);
	}
	if ( !(screen = (struct Screen *)OpenScreen(&ns) ) ) { 
		cleanup();
		exit(103);
	}

	nw.Screen = screen;                /* Open window in our new screen */
	if ( ! (w = (struct Window *)OpenWindow(&nw) ) ) {
		cleanup();
		exit(103);
	}

	vp = &screen->ViewPort;             /* Set colors in screen's VP    */
	rp = w->RPort;                      /* Render into the window's RP  */

	/*  Set the color registers */
	SetRGB4(vp, 0, 00, 00, 00);
	SetRGB4(vp, 1, 01, 00, 01);    
	SetRGB4(vp, 2, 02, 01, 02);   
	SetRGB4(vp, 3, 03, 02, 03);
	SetRGB4(vp, 4, 04, 03, 04);
	SetRGB4(vp, 5, 05, 04, 05);
	SetRGB4(vp, 6, 06, 05, 06);
	SetRGB4(vp, 7, 07, 06, 07);
	SetRGB4(vp, 8, 08, 07, 08);
	SetRGB4(vp, 9, 09, 08, 09);
	SetRGB4(vp,10, 10, 09, 10);
	SetRGB4(vp,11, 11, 10, 11);
	SetRGB4(vp,12, 12, 11, 12);
	SetRGB4(vp,13, 13, 12, 13);
	SetRGB4(vp,14, 14, 13, 14);
	SetRGB4(vp,15, 15, 15, 15);

	show(in);
	
	gets(wait);

	cleanup();
}

cleanup()
{
	fclose(in);

	if (w)
		CloseWindow(w);

	if (screen)
		CloseScreen(screen);

	if (IntuitionBase)
		CloseLibrary(IntuitionBase);

	if (GfxBase)
		CloseLibrary(GfxBase);
}

show(in)
FILE	*in;
{
	register	int	k, x, y, xsue, ysue;
	char	buf[512];
	float	big, little;

	big = 0.0;
	little = HUGE;

	for (ysue = 0; ysue<250; ysue++) {
		if (fgets (buf, 512, in) == NULL)
			break;

		/*get rid of EOL that leaves nasty black dots*/
		xsue = strlen (buf) - 1;

		for (x = 0; x < xsue; x++) {

			k = buf[x];

			suzie[x][ysue] = k;

			if ( big < (float) k)
				big = (float) k;

			if ( little > (float) k)
				little = (float) k;
		}
	}

	/* expand dynamic range */
	big -= little;
	big /= 16.0;	/* because we only have 16 grey levels */

	for (y = 0; y < ysue; y++) {
		for (x = 0; x < xsue; x++) {
			SetAPen(
				rp,
				(int) ( ((float)(suzie[x][y])-little) / big )
			);
			WritePixel(rp, x, y);
		}
	}
}
