/*

Java Graphics Demos: 

by Orion Lawlor, 12/1998

olawlor@acm.org



This code is totally free, and

should be considered in the public domain.

Use and modify it at will.

*/

import java.awt.*;

import java.awt.image.*;

//import ColorMap;

//import ByteTable;



class Demo 

{

	int w,h;

	public void init(ColorMap cm,byte pix[],int Nw,int Nh)

	{

		int x,y;

		w=Nw;h=Nh;

		int dex[]={0,255},

			red[]={0,100},

			green[]={0,100},

			blue[]={0,255};

		cm.addBreaks(dex,red,green,blue);

		for (y=0;y<h;y++)

		{

			int l=y*w;

			for (x=0;x<w;x++)

				pix[l+x]=(byte)(x-y);

		}

	}

	public boolean handleAction(Event ev,Object obj) {return false;}

	public void fillBuffer(ColorMap cm,byte pix[])

	{

		int x,y;

		//Do a little animation about the edges

		for (x=0;x<w;x++)//Top edge

			pix[x]++;

		for (y=0;y<h;y++)//Left edge

			pix[y*w]++;

		//Blur

		for (y=1;y<h-1;y++)

		{

			int i=y*w;

			for (x=1;x<w-1;x++)

			{

				int x1=i+x-w;//Index of start of line before current line.

				int x2=i+x;//Index of start of current line

				int x3=i+x+w;//Index of line after current line

				/*Note: Java doesn't have a unsigned types,

				  so to extract an unsigned byte, we cast to 

				  an integer and extract the low 8 bits: 0xff&(int)byte.

				  I think the VM is smart enough to do this efficiently.*/

				int sum=

					(0xff&(int)pix[x1-1])+(0xff&(int)pix[x1])+(0xff&(int)pix[x1+1])+

					(0xff&(int)pix[x2-1])+                    (0xff&(int)pix[x2+1])+

					(0xff&(int)pix[x3-1])+(0xff&(int)pix[x3])+(0xff&(int)pix[x3+1]);

				pix[i+x]=(byte)((sum+4)>>>3);//(note: a>>>3 == a/8)

			}

		}

	}

}

